67{
68 wxASSERT(len > 0);
71 if (s0 >= numSamples)
72
73 return false;
74
75
76
77
78 const auto s1 = std::clamp(where[len], 1 + where[len - 1], numSamples);
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
91
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
102
103 const SeqBlock &seqBlock = blocks[b];
104 const auto start = seqBlock.
start;
105 nextSrcX =
std::min(s1, start + seqBlock.
sb->GetSampleCount());
106
107
108
109
110
111
112 decltype(len) nextPixel;
113 if (nextSrcX >= s1)
114
115 nextPixel = len;
116 else {
117 nextPixel = pixel;
118
119
120 while (nextPixel < len &&
121 (whereNext =
std::min(s1 - 1, where[nextPixel])) < nextSrcX)
122 ++nextPixel;
123 }
124 if (nextPixel == pixel)
125
126
127
128
129
130
131
132 continue;
133 if (nextPixel == len)
134 whereNext = s1;
135
136
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
145
146 const size_t startPosition =
147
148 std::max(
sampleCount(0), (srcX - start) / divisor).as_size_t();
149 const size_t inclusiveEndPosition =
150
152 (nextSrcX - 1 - start) / divisor).as_size_t();
153 const auto num = 1 + inclusiveEndPosition - startPosition;
154 if (num <= 0) {
155
156 wxASSERT(false);
157
158 while (pixel < nextPixel) {
159 min[pixel] = max[pixel] = rms[pixel] = 0;
160 ++pixel;
161 }
162 continue;
163 }
164
165
166 switch (divisor) {
167 default:
168 case 1:
169
170
173 break;
174 case 256:
175
176
177
178 seqBlock.
sb->GetSummary256(temp.get(), startPosition, num);
179 break;
180 case 65536:
181
182
183
184 seqBlock.
sb->GetSummary64k(temp.get(), startPosition, num);
185 break;
186 }
187
188 auto filePosition = startPosition;
189
190
191
192 if (b > block0 && pixel > 0) {
193
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];
201 float &lastMax = max[lastPixel];
202 lastMax = std::max(lastMax,
values.max);
203 float &lastRms = rms[lastPixel];
204 int lastNumSamples = lastRmsDenom * lastDivisor;
206 (lastRms * lastRms * lastNumSamples +
values.sumsq * divisor) /
207 (lastNumSamples + diff * divisor)
208 );
209
210 filePosition = midPosition;
211 }
212 }
213
214
215 int rmsDenom = 0;
216 for (; filePosition <= inclusiveEndPosition;) {
217
218
219
220
221 auto pixelX = pixel + 1;
222 decltype(filePosition) positionX = 0;
223 while (pixelX < nextPixel &&
224 filePosition ==
225 (positionX = (
226
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
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
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 }
255
256 wxASSERT(pixel == len);
257
258 return true;
259}
Data structure containing pointer to a sample block and a start time. Element of a BlockArray.
sampleCount start
the sample in the global wavetrack that this block starts at.
size_t GetMaxBlockSize() const
sampleCount GetNumSamples() const
static bool Read(samplePtr buffer, sampleFormat format, const SeqBlock &b, size_t blockRelativeStart, size_t len, bool mayThrow)
int FindBlock(sampleCount pos) const
BlockArray & GetBlockArray()
Positions or offsets within audio files need a wide type.
__finl float_x4 __vecc sqrt(const float_x4 &a)