Audacity 3.2.0
Functions
GetWaveDisplay.h File Reference
#include <cstddef>
Include dependency graph for GetWaveDisplay.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

bool GetWaveDisplay (const Sequence &sequence, float *min, float *max, float *rms, size_t len, const sampleCount *where)
 

Function Documentation

◆ GetWaveDisplay()

bool GetWaveDisplay ( const Sequence sequence,
float *  min,
float *  max,
float *  rms,
size_t  len,
const sampleCount where 
)

Definition at line 64 of file GetWaveDisplay.cpp.

67{
68 wxASSERT(len > 0);
69 const auto s0 = std::max(sampleCount(0), where[0]);
70 const auto numSamples = sequence.GetNumSamples();
71 if (s0 >= numSamples)
72 // None of the samples asked for are in range. Abandon.
73 return false;
74
75 // In case where[len - 1] == where[len], raise the limit by one,
76 // so we load at least one pixel for column len - 1
77 // ... unless the mNumSamples ceiling applies, and then there are other defenses
78 const auto s1 = std::clamp(where[len], 1 + where[len - 1], numSamples);
79 const auto maxSamples = sequence.GetMaxBlockSize();
80 Floats temp{ maxSamples };
81
82 decltype(len) pixel = 0;
83
84 auto srcX = s0;
85 decltype(srcX) nextSrcX = 0;
86 int lastRmsDenom = 0;
87 int lastDivisor = 0;
88 auto whereNow = std::min(s1 - 1, where[0]);
89 decltype(whereNow) whereNext = 0;
90 // Loop over block files, opening and reading and closing each
91 // not more than once
92 const auto &blocks = sequence.GetBlockArray();
93 unsigned nBlocks = blocks.size();
94 const unsigned int block0 = sequence.FindBlock(s0);
95 for (unsigned int b = block0; b < nBlocks; ++b) {
96 if (b > block0)
97 srcX = nextSrcX;
98 if (srcX >= s1)
99 break;
100
101 // Find the range of sample values for this block that
102 // are in the display.
103 const SeqBlock &seqBlock = blocks[b];
104 const auto start = seqBlock.start;
105 nextSrcX = std::min(s1, start + seqBlock.sb->GetSampleCount());
106
107 // The column for pixel p covers samples from
108 // where[p] up to but excluding where[p + 1].
109
110 // Find the range of pixels covered by the current block file
111 // (Their starting samples covered by it, to be exact)
112 decltype(len) nextPixel;
113 if (nextSrcX >= s1)
114 // last pass
115 nextPixel = len;
116 else {
117 nextPixel = pixel;
118 // Taking min with s1 - 1, here and elsewhere, is another defense
119 // to be sure the last pixel column gets at least one sample
120 while (nextPixel < len &&
121 (whereNext = std::min(s1 - 1, where[nextPixel])) < nextSrcX)
122 ++nextPixel;
123 }
124 if (nextPixel == pixel)
125 // The entire block's samples fall within one pixel column.
126 // Either it's a rare odd block at the end, or else,
127 // we must be really zoomed out!
128 // Omit the entire block's contents from min/max/rms
129 // calculation, which is not correct, but correctness might not
130 // be worth the compute time if this happens every pixel
131 // column. -- PRL
132 continue;
133 if (nextPixel == len)
134 whereNext = s1;
135
136 // Decide the summary level
137 const double samplesPerPixel =
138 (whereNext - whereNow).as_double() / (nextPixel - pixel);
139 const int divisor =
140 (samplesPerPixel >= 65536) ? 65536
141 : (samplesPerPixel >= 256) ? 256
142 : 1;
143
144 // How many samples or triples are needed?
145
146 const size_t startPosition =
147 // srcX and start are in the same block
148 std::max(sampleCount(0), (srcX - start) / divisor).as_size_t();
149 const size_t inclusiveEndPosition =
150 // nextSrcX - 1 and start are in the same block
151 std::min((sampleCount(maxSamples) / divisor) - 1,
152 (nextSrcX - 1 - start) / divisor).as_size_t();
153 const auto num = 1 + inclusiveEndPosition - startPosition;
154 if (num <= 0) {
155 // What? There was a zero length block file?
156 wxASSERT(false);
157 // Do some defense against this case anyway
158 while (pixel < nextPixel) {
159 min[pixel] = max[pixel] = rms[pixel] = 0;
160 ++pixel;
161 }
162 continue;
163 }
164
165 // Read from the block file or its summary
166 switch (divisor) {
167 default:
168 case 1:
169 // Read samples
170 // no-throw for display operations!
171 sequence.Read(
172 (samplePtr)temp.get(), floatSample, seqBlock, startPosition, num, false);
173 break;
174 case 256:
175 // Read triples
176 // Ignore the return value.
177 // This function fills with zeroes if read fails
178 seqBlock.sb->GetSummary256(temp.get(), startPosition, num);
179 break;
180 case 65536:
181 // Read triples
182 // Ignore the return value.
183 // This function fills with zeroes if read fails
184 seqBlock.sb->GetSummary64k(temp.get(), startPosition, num);
185 break;
186 }
187
188 auto filePosition = startPosition;
189
190 // The previous pixel column might straddle blocks.
191 // If so, impute some of the data to it.
192 if (b > block0 && pixel > 0) {
193 // whereNow and start are in the same block
194 auto midPosition = ((whereNow - start) / divisor).as_size_t();
195 int diff(midPosition - filePosition);
196 if (diff > 0) {
197 MinMaxSumsq values(temp.get(), diff, divisor);
198 const int lastPixel = pixel - 1;
199 float &lastMin = min[lastPixel];
200 lastMin = std::min(lastMin, values.min);
201 float &lastMax = max[lastPixel];
202 lastMax = std::max(lastMax, values.max);
203 float &lastRms = rms[lastPixel];
204 int lastNumSamples = lastRmsDenom * lastDivisor;
205 lastRms = sqrt(
206 (lastRms * lastRms * lastNumSamples + values.sumsq * divisor) /
207 (lastNumSamples + diff * divisor)
208 );
209
210 filePosition = midPosition;
211 }
212 }
213
214 // Loop over file positions
215 int rmsDenom = 0;
216 for (; filePosition <= inclusiveEndPosition;) {
217 // Find range of pixel columns for this file position
218 // (normally just one, but maybe more when zoomed very close)
219 // and the range of positions for those columns
220 // (normally one or more, for that one column)
221 auto pixelX = pixel + 1;
222 decltype(filePosition) positionX = 0;
223 while (pixelX < nextPixel &&
224 filePosition ==
225 (positionX = (
226 // s1 - 1 or where[pixelX] and start are in the same block
227 (std::min(s1 - 1, where[pixelX]) - start) / divisor).as_size_t() )
228 )
229 ++pixelX;
230 if (pixelX >= nextPixel)
231 positionX = 1 + inclusiveEndPosition;
232
233 // Find results to assign
234 rmsDenom = (positionX - filePosition);
235 wxASSERT(rmsDenom > 0);
236 const float *const pv =
237 temp.get() + (filePosition - startPosition) * (divisor == 1 ? 1 : 3);
238 MinMaxSumsq values(pv, std::max(0, rmsDenom), divisor);
239
240 // Assign results
241 std::fill(&min[pixel], &min[pixelX], values.min);
242 std::fill(&max[pixel], &max[pixelX], values.max);
243 std::fill(&rms[pixel], &rms[pixelX], (float)sqrt(values.sumsq / rmsDenom));
244
245 pixel = pixelX;
246 filePosition = positionX;
247 }
248
249 wxASSERT(pixel == nextPixel);
250 whereNow = whereNext;
251 pixel = nextPixel;
252 lastDivisor = divisor;
253 lastRmsDenom = rmsDenom;
254 } // for each block file
255
256 wxASSERT(pixel == len);
257
258 return true;
259}
int min(int a, int b)
const wxChar * values
constexpr sampleFormat floatSample
Definition: SampleFormat.h:45
char * samplePtr
Definition: SampleFormat.h:57
Data structure containing pointer to a sample block and a start time. Element of a BlockArray.
Definition: Sequence.h:29
sampleCount start
the sample in the global wavetrack that this block starts at.
Definition: Sequence.h:34
SampleBlockPtr sb
Definition: Sequence.h:32
size_t GetMaxBlockSize() const
Definition: Sequence.cpp:77
sampleCount GetNumSamples() const
Definition: Sequence.h:84
static bool Read(samplePtr buffer, sampleFormat format, const SeqBlock &b, size_t blockRelativeStart, size_t len, bool mayThrow)
Definition: Sequence.cpp:1096
int FindBlock(sampleCount pos) const
Definition: Sequence.cpp:1046
BlockArray & GetBlockArray()
Definition: Sequence.h:218
Positions or offsets within audio files need a wide type.
Definition: SampleCount.h:19
__finl float_x4 __vecc sqrt(const float_x4 &a)

References Sequence::FindBlock(), floatSample, Sequence::GetBlockArray(), Sequence::GetMaxBlockSize(), Sequence::GetNumSamples(), min(), Sequence::Read(), SeqBlock::sb, staffpad::audio::simd::sqrt(), SeqBlock::start, and values.

Here is the call graph for this function: