Audacity 3.2.0
Classes | Public Types | Public Member Functions | Private Attributes | List of all members
PixelSampleMapper Class Referencefinal

Utility class to calculate sample range for a given column. More...

#include <PixelSampleMapper.h>

Classes

struct  LinearMapper
 

Public Types

using CustomMapper = std::function< sampleCount(uint32_t)>
 

Public Member Functions

 PixelSampleMapper ()=default
 
 PixelSampleMapper (const PixelSampleMapper &)=default
 
 PixelSampleMapper (PixelSampleMapper &&)=default
 
PixelSampleMapperoperator= (const PixelSampleMapper &)=default
 
PixelSampleMapperoperator= (PixelSampleMapper &&)=default
 
 PixelSampleMapper (double t0, double rate, double samplesPerPixel) noexcept
 
void applyBias (double bias) noexcept
 
double applyCorrection (const PixelSampleMapper &oldMapper, size_t oldLen, size_t newLen)
 
sampleCount GetFirstSample (uint32_t column) const
 
sampleCount GetLastSample (uint32_t column) const
 
std::pair< sampleCount, sampleCountGetSampleRange (uint32_t column) const
 
void setCustomMapper (CustomMapper mapper)
 
bool IsValid () const
 
bool IsLinear () const noexcept
 

Private Attributes

std::variant< LinearMapper, CustomMappermMapper { LinearMapper {} }
 

Detailed Description

Utility class to calculate sample range for a given column.


Audacity: A Digital Audio Editor

PixelSampleMapper.h

Dmitry Vedenko

Definition at line 22 of file PixelSampleMapper.h.

Member Typedef Documentation

◆ CustomMapper

using PixelSampleMapper::CustomMapper = std::function<sampleCount(uint32_t)>

Definition at line 42 of file PixelSampleMapper.h.

Constructor & Destructor Documentation

◆ PixelSampleMapper() [1/4]

PixelSampleMapper::PixelSampleMapper ( )
default

◆ PixelSampleMapper() [2/4]

PixelSampleMapper::PixelSampleMapper ( const PixelSampleMapper )
default

◆ PixelSampleMapper() [3/4]

PixelSampleMapper::PixelSampleMapper ( PixelSampleMapper &&  )
default

◆ PixelSampleMapper() [4/4]

PixelSampleMapper::PixelSampleMapper ( double  t0,
double  rate,
double  samplesPerPixel 
)
noexcept

Audacity: A Digital Audio Editor

PixelSampleMapper.h

Dmitry Vedenko

Definition at line 19 of file PixelSampleMapper.cpp.

21 : mMapper(LinearMapper { (0.5 + t0 * rate), samplesPerPixel })
22{
23 assert((0.5 + t0 * rate) >= 0.0);
24}
std::variant< LinearMapper, CustomMapper > mMapper

Member Function Documentation

◆ applyBias()

void PixelSampleMapper::applyBias ( double  bias)
noexcept

Definition at line 26 of file PixelSampleMapper.cpp.

27{
28 auto mapper = std::get_if<LinearMapper>(&mMapper);
29
30 if (mapper != nullptr)
31 mapper->mInitialValue += bias;
32}

◆ applyCorrection()

double PixelSampleMapper::applyCorrection ( const PixelSampleMapper oldMapper,
size_t  oldLen,
size_t  newLen 
)

Definition at line 34 of file PixelSampleMapper.cpp.

36{
37 assert(mMapper.index() == 0);
38 assert(oldMapper.mMapper.index() == 0);
39
40 LinearMapper* currentMapper = std::get_if<LinearMapper>(&mMapper);
41
42 if (currentMapper == nullptr)
43 return {};
44
45 const LinearMapper* oldLinearMapper =
46 std::get_if<LinearMapper>(&oldMapper.mMapper);
47
48 if (oldLinearMapper == nullptr)
49 return {};
50
51 // Find the sample position that is the origin in the old cache.
52 const double oldWhere0 =
53 (*oldLinearMapper)(1).as_double() - currentMapper->mSamplesPerPixel;
54 const double oldWhereLast =
55 oldWhere0 + oldLen * currentMapper->mSamplesPerPixel;
56 // Find the length in samples of the old cache.
57 const double denom = oldWhereLast - oldWhere0;
58
59 // What sample would go in where[0] with no correction?
60 const double guessWhere0 =
61 currentMapper->mInitialValue - 0.5; // Why do we ignore initial bias?
62
63 if ( // Skip if old and NEW are disjoint:
64 oldWhereLast <= guessWhere0 ||
65 guessWhere0 + newLen * currentMapper->mSamplesPerPixel <= oldWhere0 ||
66 // Skip unless denom rounds off to at least 1.
67 denom < 0.5)
68 {
69 // The computation of oldX0 in the other branch
70 // may underflow and the assertion would be violated.
71 return oldLen;
72 }
73 else
74 {
75 // What integer position in the old cache array does that map to?
76 // (even if it is out of bounds)
77 const auto oldX0 =
78 std::floor(0.5 + oldLen * (guessWhere0 - oldWhere0) / denom);
79 // What sample count would the old cache have put there?
80 const double where0 =
81 oldWhere0 + double(oldX0) * currentMapper->mSamplesPerPixel;
82 // What correction is needed to align the NEW cache with the old?
83 const double correction0 = where0 - guessWhere0;
84 const double correction = std::max(
85 -currentMapper->mSamplesPerPixel,
86 std::min(currentMapper->mSamplesPerPixel, correction0));
87
88 assert(correction == correction0);
89
90 currentMapper->mInitialValue += correction;
91
92 return oldX0;
93 }
94}
int min(int a, int b)

References min(), PixelSampleMapper::LinearMapper::mInitialValue, mMapper, and PixelSampleMapper::LinearMapper::mSamplesPerPixel.

Here is the call graph for this function:

◆ GetFirstSample()

sampleCount PixelSampleMapper::GetFirstSample ( uint32_t  column) const

Definition at line 96 of file PixelSampleMapper.cpp.

97{
98 return Variant::Visit(
99 [column](const auto& mapper) { return mapper(column); }, mMapper);
100}
decltype(auto) Visit(Visitor &&vis, Variant &&var)
Mimic some of std::visit, for the case of one visitor only.
Definition: Variant.h:138

References mMapper, and Variant::Visit().

Referenced by GetLastSample(), and GetSampleRange().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetLastSample()

sampleCount PixelSampleMapper::GetLastSample ( uint32_t  column) const

Definition at line 102 of file PixelSampleMapper.cpp.

103{
104 return GetFirstSample(column + 1);
105}
sampleCount GetFirstSample(uint32_t column) const

References GetFirstSample().

Referenced by GetSampleRange().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetSampleRange()

std::pair< sampleCount, sampleCount > PixelSampleMapper::GetSampleRange ( uint32_t  column) const

Definition at line 108 of file PixelSampleMapper.cpp.

109{
110 return { GetFirstSample(column), GetLastSample(column) };
111}
sampleCount GetLastSample(uint32_t column) const

References GetFirstSample(), and GetLastSample().

Here is the call graph for this function:

◆ IsLinear()

bool PixelSampleMapper::IsLinear ( ) const
noexcept

Definition at line 123 of file PixelSampleMapper.cpp.

124{
125 return std::get_if<LinearMapper>(&mMapper) != nullptr;
126}

References mMapper.

◆ IsValid()

bool PixelSampleMapper::IsValid ( ) const

Definition at line 118 of file PixelSampleMapper.cpp.

119{
120 return Variant::Visit([](const auto& mapper) { return !!mapper; }, mMapper);
121}

References mMapper, and Variant::Visit().

Here is the call graph for this function:

◆ operator=() [1/2]

PixelSampleMapper & PixelSampleMapper::operator= ( const PixelSampleMapper )
default

◆ operator=() [2/2]

PixelSampleMapper & PixelSampleMapper::operator= ( PixelSampleMapper &&  )
default

◆ setCustomMapper()

void PixelSampleMapper::setCustomMapper ( CustomMapper  mapper)

Definition at line 113 of file PixelSampleMapper.cpp.

114{
115 mMapper = std::move(mapper);
116}

References mMapper.

Member Data Documentation

◆ mMapper

std::variant<LinearMapper, CustomMapper> PixelSampleMapper::mMapper { LinearMapper {} }
private

The documentation for this class was generated from the following files: