comparison data/model/WaveFileModel.cpp @ 935:f960d67ce842 tonioni

Merge from branch warnfix_no_size_t
author Chris Cannam
date Wed, 18 Jun 2014 13:42:01 +0100
parents d03b3d956358
children 6d2ece0fe356
comparison
equal deleted inserted replaced
925:3efc20c59a94 935:f960d67ce842
35 //#define DEBUG_WAVE_FILE_MODEL 1 35 //#define DEBUG_WAVE_FILE_MODEL 1
36 36
37 PowerOfSqrtTwoZoomConstraint 37 PowerOfSqrtTwoZoomConstraint
38 WaveFileModel::m_zoomConstraint; 38 WaveFileModel::m_zoomConstraint;
39 39
40 WaveFileModel::WaveFileModel(FileSource source, size_t targetRate) : 40 WaveFileModel::WaveFileModel(FileSource source, int targetRate) :
41 m_source(source), 41 m_source(source),
42 m_path(source.getLocation()), 42 m_path(source.getLocation()),
43 m_myReader(true), 43 m_myReader(true),
44 m_startFrame(0), 44 m_startFrame(0),
45 m_fillThread(0), 45 m_fillThread(0),
127 { 127 {
128 WaveFileModel *model = new WaveFileModel(m_source); 128 WaveFileModel *model = new WaveFileModel(m_source);
129 return model; 129 return model;
130 } 130 }
131 131
132 size_t 132 int
133 WaveFileModel::getFrameCount() const 133 WaveFileModel::getFrameCount() const
134 { 134 {
135 if (!m_reader) return 0; 135 if (!m_reader) return 0;
136 return m_reader->getFrameCount(); 136 return m_reader->getFrameCount();
137 } 137 }
138 138
139 size_t 139 int
140 WaveFileModel::getChannelCount() const 140 WaveFileModel::getChannelCount() const
141 { 141 {
142 if (!m_reader) return 0; 142 if (!m_reader) return 0;
143 return m_reader->getChannelCount(); 143 return m_reader->getChannelCount();
144 } 144 }
145 145
146 size_t 146 int
147 WaveFileModel::getSampleRate() const 147 WaveFileModel::getSampleRate() const
148 { 148 {
149 if (!m_reader) return 0; 149 if (!m_reader) return 0;
150 return m_reader->getSampleRate(); 150 return m_reader->getSampleRate();
151 } 151 }
152 152
153 size_t 153 int
154 WaveFileModel::getNativeRate() const 154 WaveFileModel::getNativeRate() const
155 { 155 {
156 if (!m_reader) return 0; 156 if (!m_reader) return 0;
157 size_t rate = m_reader->getNativeRate(); 157 int rate = m_reader->getNativeRate();
158 if (rate == 0) rate = getSampleRate(); 158 if (rate == 0) rate = getSampleRate();
159 return rate; 159 return rate;
160 } 160 }
161 161
162 QString 162 QString
180 { 180 {
181 if (m_reader) return m_reader->getLocation(); 181 if (m_reader) return m_reader->getLocation();
182 return ""; 182 return "";
183 } 183 }
184 184
185 size_t 185 int
186 WaveFileModel::getData(int channel, size_t start, size_t count, 186 WaveFileModel::getData(int channel, int start, int count,
187 float *buffer) const 187 float *buffer) const
188 { 188 {
189 // Always read these directly from the file. 189 // Always read these directly from the file.
190 // This is used for e.g. audio playback. 190 // This is used for e.g. audio playback.
191 // Could be much more efficient (although compiler optimisation will help) 191 // Could be much more efficient (although compiler optimisation will help)
195 #endif 195 #endif
196 196
197 if (start >= m_startFrame) { 197 if (start >= m_startFrame) {
198 start -= m_startFrame; 198 start -= m_startFrame;
199 } else { 199 } else {
200 for (size_t i = 0; i < count; ++i) buffer[i] = 0.f; 200 for (int i = 0; i < count; ++i) buffer[i] = 0.f;
201 if (count <= m_startFrame - start) { 201 if (count <= m_startFrame - start) {
202 return 0; 202 return 0;
203 } else { 203 } else {
204 count -= (m_startFrame - start); 204 count -= (m_startFrame - start);
205 start = 0; 205 start = 0;
206 } 206 }
207 } 207 }
208 208
209 if (!m_reader || !m_reader->isOK() || count == 0) { 209 if (!m_reader || !m_reader->isOK() || count == 0) {
210 for (size_t i = 0; i < count; ++i) buffer[i] = 0.f; 210 for (int i = 0; i < count; ++i) buffer[i] = 0.f;
211 return 0; 211 return 0;
212 } 212 }
213 213
214 #ifdef DEBUG_WAVE_FILE_MODEL 214 #ifdef DEBUG_WAVE_FILE_MODEL
215 // SVDEBUG << "WaveFileModel::getValues(" << channel << ", " 215 // SVDEBUG << "WaveFileModel::getValues(" << channel << ", "
219 int channels = getChannelCount(); 219 int channels = getChannelCount();
220 220
221 SampleBlock frames(count * channels); 221 SampleBlock frames(count * channels);
222 m_reader->getInterleavedFrames(start, count, frames); 222 m_reader->getInterleavedFrames(start, count, frames);
223 223
224 size_t i = 0; 224 int i = 0;
225 225
226 int ch0 = channel, ch1 = channel; 226 int ch0 = channel, ch1 = channel;
227 if (channel == -1) { 227 if (channel == -1) {
228 ch0 = 0; 228 ch0 = 0;
229 ch1 = channels - 1; 229 ch1 = channels - 1;
233 233
234 buffer[i] = 0.0; 234 buffer[i] = 0.0;
235 235
236 for (int ch = ch0; ch <= ch1; ++ch) { 236 for (int ch = ch0; ch <= ch1; ++ch) {
237 237
238 size_t index = i * channels + ch; 238 int index = i * channels + ch;
239 if (index >= frames.size()) break; 239 if (index >= (int)frames.size()) break;
240 240
241 float sample = frames[index]; 241 float sample = frames[index];
242 buffer[i] += sample; 242 buffer[i] += sample;
243 } 243 }
244 244
246 } 246 }
247 247
248 return i; 248 return i;
249 } 249 }
250 250
251 size_t 251 int
252 WaveFileModel::getData(int channel, size_t start, size_t count, 252 WaveFileModel::getData(int channel, int start, int count,
253 double *buffer) const 253 double *buffer) const
254 { 254 {
255 #ifdef DEBUG_WAVE_FILE_MODEL 255 #ifdef DEBUG_WAVE_FILE_MODEL
256 cout << "WaveFileModel::getData(double)[" << this << "]: " << channel << ", " << start << ", " << count << ", " << buffer << endl; 256 cout << "WaveFileModel::getData(double)[" << this << "]: " << channel << ", " << start << ", " << count << ", " << buffer << endl;
257 #endif 257 #endif
258 258
259 if (start > m_startFrame) { 259 if (start > m_startFrame) {
260 start -= m_startFrame; 260 start -= m_startFrame;
261 } else { 261 } else {
262 for (size_t i = 0; i < count; ++i) buffer[i] = 0.0; 262 for (int i = 0; i < count; ++i) buffer[i] = 0.0;
263 if (count <= m_startFrame - start) { 263 if (count <= m_startFrame - start) {
264 return 0; 264 return 0;
265 } else { 265 } else {
266 count -= (m_startFrame - start); 266 count -= (m_startFrame - start);
267 start = 0; 267 start = 0;
268 } 268 }
269 } 269 }
270 270
271 if (!m_reader || !m_reader->isOK() || count == 0) { 271 if (!m_reader || !m_reader->isOK() || count == 0) {
272 for (size_t i = 0; i < count; ++i) buffer[i] = 0.0; 272 for (int i = 0; i < count; ++i) buffer[i] = 0.0;
273 return 0; 273 return 0;
274 } 274 }
275 275
276 int channels = getChannelCount(); 276 int channels = getChannelCount();
277 277
278 SampleBlock frames(count * channels); 278 SampleBlock frames(count * channels);
279 m_reader->getInterleavedFrames(start, count, frames); 279 m_reader->getInterleavedFrames(start, count, frames);
280 280
281 size_t i = 0; 281 int i = 0;
282 282
283 int ch0 = channel, ch1 = channel; 283 int ch0 = channel, ch1 = channel;
284 if (channel == -1) { 284 if (channel == -1) {
285 ch0 = 0; 285 ch0 = 0;
286 ch1 = channels - 1; 286 ch1 = channels - 1;
290 290
291 buffer[i] = 0.0; 291 buffer[i] = 0.0;
292 292
293 for (int ch = ch0; ch <= ch1; ++ch) { 293 for (int ch = ch0; ch <= ch1; ++ch) {
294 294
295 size_t index = i * channels + ch; 295 int index = i * channels + ch;
296 if (index >= frames.size()) break; 296 if (index >= (int)frames.size()) break;
297 297
298 float sample = frames[index]; 298 float sample = frames[index];
299 buffer[i] += sample; 299 buffer[i] += sample;
300 } 300 }
301 301
303 } 303 }
304 304
305 return i; 305 return i;
306 } 306 }
307 307
308 size_t 308 int
309 WaveFileModel::getData(size_t fromchannel, size_t tochannel, 309 WaveFileModel::getData(int fromchannel, int tochannel,
310 size_t start, size_t count, 310 int start, int count,
311 float **buffer) const 311 float **buffer) const
312 { 312 {
313 #ifdef DEBUG_WAVE_FILE_MODEL 313 #ifdef DEBUG_WAVE_FILE_MODEL
314 cout << "WaveFileModel::getData[" << this << "]: " << fromchannel << "," << tochannel << ", " << start << ", " << count << ", " << buffer << endl; 314 cout << "WaveFileModel::getData[" << this << "]: " << fromchannel << "," << tochannel << ", " << start << ", " << count << ", " << buffer << endl;
315 #endif 315 #endif
316 316
317 size_t channels = getChannelCount(); 317 int channels = getChannelCount();
318 318
319 if (fromchannel > tochannel) { 319 if (fromchannel > tochannel) {
320 cerr << "ERROR: WaveFileModel::getData: fromchannel (" 320 cerr << "ERROR: WaveFileModel::getData: fromchannel ("
321 << fromchannel << ") > tochannel (" << tochannel << ")" 321 << fromchannel << ") > tochannel (" << tochannel << ")"
322 << endl; 322 << endl;
332 332
333 if (fromchannel == tochannel) { 333 if (fromchannel == tochannel) {
334 return getData(fromchannel, start, count, buffer[0]); 334 return getData(fromchannel, start, count, buffer[0]);
335 } 335 }
336 336
337 size_t reqchannels = (tochannel - fromchannel) + 1; 337 int reqchannels = (tochannel - fromchannel) + 1;
338 338
339 // Always read these directly from the file. 339 // Always read these directly from the file.
340 // This is used for e.g. audio playback. 340 // This is used for e.g. audio playback.
341 // Could be much more efficient (although compiler optimisation will help) 341 // Could be much more efficient (although compiler optimisation will help)
342 342
343 if (start >= m_startFrame) { 343 if (start >= m_startFrame) {
344 start -= m_startFrame; 344 start -= m_startFrame;
345 } else { 345 } else {
346 for (size_t c = 0; c < reqchannels; ++c) { 346 for (int c = 0; c < reqchannels; ++c) {
347 for (size_t i = 0; i < count; ++i) buffer[c][i] = 0.f; 347 for (int i = 0; i < count; ++i) buffer[c][i] = 0.f;
348 } 348 }
349 if (count <= m_startFrame - start) { 349 if (count <= m_startFrame - start) {
350 return 0; 350 return 0;
351 } else { 351 } else {
352 count -= (m_startFrame - start); 352 count -= (m_startFrame - start);
353 start = 0; 353 start = 0;
354 } 354 }
355 } 355 }
356 356
357 if (!m_reader || !m_reader->isOK() || count == 0) { 357 if (!m_reader || !m_reader->isOK() || count == 0) {
358 for (size_t c = 0; c < reqchannels; ++c) { 358 for (int c = 0; c < reqchannels; ++c) {
359 for (size_t i = 0; i < count; ++i) buffer[c][i] = 0.f; 359 for (int i = 0; i < count; ++i) buffer[c][i] = 0.f;
360 } 360 }
361 return 0; 361 return 0;
362 } 362 }
363 363
364 SampleBlock frames(count * channels); 364 SampleBlock frames(count * channels);
365 m_reader->getInterleavedFrames(start, count, frames); 365 m_reader->getInterleavedFrames(start, count, frames);
366 366
367 size_t i = 0; 367 int i = 0;
368 368
369 int ch0 = fromchannel, ch1 = tochannel; 369 int index = 0, available = frames.size();
370
371 size_t index = 0, available = frames.size();
372 370
373 while (i < count) { 371 while (i < count) {
374 372
375 if (index >= available) break; 373 if (index >= available) break;
376 374
377 size_t destc = 0; 375 int destc = 0;
378 376
379 for (size_t c = 0; c < channels; ++c) { 377 for (int c = 0; c < channels; ++c) {
380 378
381 if (c >= fromchannel && c <= tochannel) { 379 if (c >= fromchannel && c <= tochannel) {
382 buffer[destc][i] = frames[index]; 380 buffer[destc][i] = frames[index];
383 ++destc; 381 ++destc;
384 } 382 }
390 } 388 }
391 389
392 return i; 390 return i;
393 } 391 }
394 392
395 size_t 393 int
396 WaveFileModel::getSummaryBlockSize(size_t desired) const 394 WaveFileModel::getSummaryBlockSize(int desired) const
397 { 395 {
398 int cacheType = 0; 396 int cacheType = 0;
399 int power = m_zoomConstraint.getMinCachePower(); 397 int power = m_zoomConstraint.getMinCachePower();
400 size_t roundedBlockSize = m_zoomConstraint.getNearestBlockSize 398 int roundedBlockSize = m_zoomConstraint.getNearestBlockSize
401 (desired, cacheType, power, ZoomConstraint::RoundDown); 399 (desired, cacheType, power, ZoomConstraint::RoundDown);
402 if (cacheType != 0 && cacheType != 1) { 400 if (cacheType != 0 && cacheType != 1) {
403 // We will be reading directly from file, so can satisfy any 401 // We will be reading directly from file, so can satisfy any
404 // blocksize requirement 402 // blocksize requirement
405 return desired; 403 return desired;
407 return roundedBlockSize; 405 return roundedBlockSize;
408 } 406 }
409 } 407 }
410 408
411 void 409 void
412 WaveFileModel::getSummaries(size_t channel, size_t start, size_t count, 410 WaveFileModel::getSummaries(int channel, int start, int count,
413 RangeBlock &ranges, size_t &blockSize) const 411 RangeBlock &ranges, int &blockSize) const
414 { 412 {
415 ranges.clear(); 413 ranges.clear();
416 if (!isOK()) return; 414 if (!isOK()) return;
417 ranges.reserve((count / blockSize) + 1); 415 ranges.reserve((count / blockSize) + 1);
418 416
423 start = 0; 421 start = 0;
424 } 422 }
425 423
426 int cacheType = 0; 424 int cacheType = 0;
427 int power = m_zoomConstraint.getMinCachePower(); 425 int power = m_zoomConstraint.getMinCachePower();
428 size_t roundedBlockSize = m_zoomConstraint.getNearestBlockSize 426 int roundedBlockSize = m_zoomConstraint.getNearestBlockSize
429 (blockSize, cacheType, power, ZoomConstraint::RoundDown); 427 (blockSize, cacheType, power, ZoomConstraint::RoundDown);
430 428
431 size_t channels = getChannelCount(); 429 int channels = getChannelCount();
432 430
433 if (cacheType != 0 && cacheType != 1) { 431 if (cacheType != 0 && cacheType != 1) {
434 432
435 // We need to read directly from the file. We haven't got 433 // We need to read directly from the file. We haven't got
436 // this cached. Hope the requested area is small. This is 434 // this cached. Hope the requested area is small. This is
450 m_lastDirectReadStart = start; 448 m_lastDirectReadStart = start;
451 m_lastDirectReadCount = count; 449 m_lastDirectReadCount = count;
452 } 450 }
453 451
454 float max = 0.0, min = 0.0, total = 0.0; 452 float max = 0.0, min = 0.0, total = 0.0;
455 size_t i = 0, got = 0; 453 int i = 0, got = 0;
456 454
457 while (i < count) { 455 while (i < count) {
458 456
459 size_t index = i * channels + channel; 457 int index = i * channels + channel;
460 if (index >= m_directRead.size()) break; 458 if (index >= (int)m_directRead.size()) break;
461 459
462 float sample = m_directRead[index]; 460 float sample = m_directRead[index];
463 if (sample > max || got == 0) max = sample; 461 if (sample > max || got == 0) max = sample;
464 if (sample < min || got == 0) min = sample; 462 if (sample < min || got == 0) min = sample;
465 total += fabsf(sample); 463 total += fabsf(sample);
488 486
489 const RangeBlock &cache = m_cache[cacheType]; 487 const RangeBlock &cache = m_cache[cacheType];
490 488
491 blockSize = roundedBlockSize; 489 blockSize = roundedBlockSize;
492 490
493 size_t cacheBlock, div; 491 int cacheBlock, div;
494 492
495 if (cacheType == 0) { 493 if (cacheType == 0) {
496 cacheBlock = (1 << m_zoomConstraint.getMinCachePower()); 494 cacheBlock = (1 << m_zoomConstraint.getMinCachePower());
497 div = (1 << power) / cacheBlock; 495 div = (1 << power) / cacheBlock;
498 } else { 496 } else {
499 cacheBlock = ((unsigned int)((1 << m_zoomConstraint.getMinCachePower()) * sqrt(2.) + 0.01)); 497 cacheBlock = ((unsigned int)((1 << m_zoomConstraint.getMinCachePower()) * sqrt(2.) + 0.01));
500 div = ((unsigned int)((1 << power) * sqrt(2.) + 0.01)) / cacheBlock; 498 div = ((unsigned int)((1 << power) * sqrt(2.) + 0.01)) / cacheBlock;
501 } 499 }
502 500
503 size_t startIndex = start / cacheBlock; 501 int startIndex = start / cacheBlock;
504 size_t endIndex = (start + count) / cacheBlock; 502 int endIndex = (start + count) / cacheBlock;
505 503
506 float max = 0.0, min = 0.0, total = 0.0; 504 float max = 0.0, min = 0.0, total = 0.0;
507 size_t i = 0, got = 0; 505 int i = 0, got = 0;
508 506
509 #ifdef DEBUG_WAVE_FILE_MODEL 507 #ifdef DEBUG_WAVE_FILE_MODEL
510 cerr << "blockSize is " << blockSize << ", cacheBlock " << cacheBlock << ", start " << start << ", count " << count << " (frame count " << getFrameCount() << "), power is " << power << ", div is " << div << ", startIndex " << startIndex << ", endIndex " << endIndex << endl; 508 cerr << "blockSize is " << blockSize << ", cacheBlock " << cacheBlock << ", start " << start << ", count " << count << " (frame count " << getFrameCount() << "), power is " << power << ", div is " << div << ", startIndex " << startIndex << ", endIndex " << endIndex << endl;
511 #endif 509 #endif
512 510
513 for (i = 0; i <= endIndex - startIndex; ) { 511 for (i = 0; i <= endIndex - startIndex; ) {
514 512
515 size_t index = (i + startIndex) * channels + channel; 513 int index = (i + startIndex) * channels + channel;
516 if (index >= cache.size()) break; 514 if (index >= (int)cache.size()) break;
517 515
518 const Range &range = cache[index]; 516 const Range &range = cache[index];
519 if (range.max() > max || got == 0) max = range.max(); 517 if (range.max() > max || got == 0) max = range.max();
520 if (range.min() < min || got == 0) min = range.min(); 518 if (range.min() < min || got == 0) min = range.min();
521 total += range.absmean(); 519 total += range.absmean();
540 #endif 538 #endif
541 return; 539 return;
542 } 540 }
543 541
544 WaveFileModel::Range 542 WaveFileModel::Range
545 WaveFileModel::getSummary(size_t channel, size_t start, size_t count) const 543 WaveFileModel::getSummary(int channel, int start, int count) const
546 { 544 {
547 Range range; 545 Range range;
548 if (!isOK()) return range; 546 if (!isOK()) return range;
549 547
550 if (start > m_startFrame) start -= m_startFrame; 548 if (start > m_startFrame) start -= m_startFrame;
552 else { 550 else {
553 count -= (m_startFrame - start); 551 count -= (m_startFrame - start);
554 start = 0; 552 start = 0;
555 } 553 }
556 554
557 size_t blockSize; 555 int blockSize;
558 for (blockSize = 1; blockSize <= count; blockSize *= 2); 556 for (blockSize = 1; blockSize <= count; blockSize *= 2);
559 if (blockSize > 1) blockSize /= 2; 557 if (blockSize > 1) blockSize /= 2;
560 558
561 bool first = false; 559 bool first = false;
562 560
563 size_t blockStart = (start / blockSize) * blockSize; 561 int blockStart = (start / blockSize) * blockSize;
564 size_t blockEnd = ((start + count) / blockSize) * blockSize; 562 int blockEnd = ((start + count) / blockSize) * blockSize;
565 563
566 if (blockStart < start) blockStart += blockSize; 564 if (blockStart < start) blockStart += blockSize;
567 565
568 if (blockEnd > blockStart) { 566 if (blockEnd > blockStart) {
569 RangeBlock ranges; 567 RangeBlock ranges;
570 getSummaries(channel, blockStart, blockEnd - blockStart, ranges, blockSize); 568 getSummaries(channel, blockStart, blockEnd - blockStart, ranges, blockSize);
571 for (size_t i = 0; i < ranges.size(); ++i) { 569 for (int i = 0; i < (int)ranges.size(); ++i) {
572 if (first || ranges[i].min() < range.min()) range.setMin(ranges[i].min()); 570 if (first || ranges[i].min() < range.min()) range.setMin(ranges[i].min());
573 if (first || ranges[i].max() > range.max()) range.setMax(ranges[i].max()); 571 if (first || ranges[i].max() > range.max()) range.setMax(ranges[i].max());
574 if (first || ranges[i].absmean() < range.absmean()) range.setAbsmean(ranges[i].absmean()); 572 if (first || ranges[i].absmean() < range.absmean()) range.setAbsmean(ranges[i].absmean());
575 first = false; 573 first = false;
576 } 574 }
615 613
616 void 614 void
617 WaveFileModel::fillTimerTimedOut() 615 WaveFileModel::fillTimerTimedOut()
618 { 616 {
619 if (m_fillThread) { 617 if (m_fillThread) {
620 size_t fillExtent = m_fillThread->getFillExtent(); 618 int fillExtent = m_fillThread->getFillExtent();
621 #ifdef DEBUG_WAVE_FILE_MODEL 619 #ifdef DEBUG_WAVE_FILE_MODEL
622 SVDEBUG << "WaveFileModel::fillTimerTimedOut: extent = " << fillExtent << endl; 620 SVDEBUG << "WaveFileModel::fillTimerTimedOut: extent = " << fillExtent << endl;
623 #endif 621 #endif
624 if (fillExtent > m_lastFillExtent) { 622 if (fillExtent > m_lastFillExtent) {
625 emit modelChanged(m_lastFillExtent, fillExtent); 623 emit modelChangedWithin(m_lastFillExtent, fillExtent);
626 m_lastFillExtent = fillExtent; 624 m_lastFillExtent = fillExtent;
627 } 625 }
628 } else { 626 } else {
629 #ifdef DEBUG_WAVE_FILE_MODEL 627 #ifdef DEBUG_WAVE_FILE_MODEL
630 SVDEBUG << "WaveFileModel::fillTimerTimedOut: no thread" << endl; 628 SVDEBUG << "WaveFileModel::fillTimerTimedOut: no thread" << endl;
641 m_fillThread = 0; 639 m_fillThread = 0;
642 delete m_updateTimer; 640 delete m_updateTimer;
643 m_updateTimer = 0; 641 m_updateTimer = 0;
644 m_mutex.unlock(); 642 m_mutex.unlock();
645 if (getEndFrame() > m_lastFillExtent) { 643 if (getEndFrame() > m_lastFillExtent) {
646 emit modelChanged(m_lastFillExtent, getEndFrame()); 644 emit modelChangedWithin(m_lastFillExtent, getEndFrame());
647 } 645 }
648 emit modelChanged(); 646 emit modelChanged();
649 emit ready(); 647 emit ready();
650 #ifdef DEBUG_WAVE_FILE_MODEL 648 #ifdef DEBUG_WAVE_FILE_MODEL
651 SVDEBUG << "WaveFileModel::cacheFilled" << endl; 649 SVDEBUG << "WaveFileModel::cacheFilled" << endl;
653 } 651 }
654 652
655 void 653 void
656 WaveFileModel::RangeCacheFillThread::run() 654 WaveFileModel::RangeCacheFillThread::run()
657 { 655 {
658 size_t cacheBlockSize[2]; 656 int cacheBlockSize[2];
659 cacheBlockSize[0] = (1 << m_model.m_zoomConstraint.getMinCachePower()); 657 cacheBlockSize[0] = (1 << m_model.m_zoomConstraint.getMinCachePower());
660 cacheBlockSize[1] = ((unsigned int)((1 << m_model.m_zoomConstraint.getMinCachePower()) * 658 cacheBlockSize[1] = ((unsigned int)((1 << m_model.m_zoomConstraint.getMinCachePower()) *
661 sqrt(2.) + 0.01)); 659 sqrt(2.) + 0.01));
662 660
663 size_t frame = 0; 661 int frame = 0;
664 int readBlockSize = 16384; 662 int readBlockSize = 16384;
665 SampleBlock block; 663 SampleBlock block;
666 664
667 if (!m_model.isOK()) return; 665 if (!m_model.isOK()) return;
668 666
669 size_t channels = m_model.getChannelCount(); 667 int channels = m_model.getChannelCount();
670 bool updating = m_model.m_reader->isUpdating(); 668 bool updating = m_model.m_reader->isUpdating();
671 669
672 if (updating) { 670 if (updating) {
673 while (channels == 0 && !m_model.m_exiting) { 671 while (channels == 0 && !m_model.m_exiting) {
674 // SVDEBUG << "WaveFileModel::fill: Waiting for channels..." << endl; 672 // SVDEBUG << "WaveFileModel::fill: Waiting for channels..." << endl;
677 } 675 }
678 } 676 }
679 677
680 Range *range = new Range[2 * channels]; 678 Range *range = new Range[2 * channels];
681 float *means = new float[2 * channels]; 679 float *means = new float[2 * channels];
682 size_t count[2]; 680 int count[2];
683 count[0] = count[1] = 0; 681 count[0] = count[1] = 0;
684 for (int i = 0; i < 2 * channels; ++i) { 682 for (int i = 0; i < 2 * channels; ++i) {
685 means[i] = 0.f; 683 means[i] = 0.f;
686 } 684 }
687 685
704 702
705 // cerr << "block is " << block.size() << endl; 703 // cerr << "block is " << block.size() << endl;
706 704
707 for (int i = 0; i < readBlockSize; ++i) { 705 for (int i = 0; i < readBlockSize; ++i) {
708 706
709 if (channels * i + channels > block.size()) break; 707 if (channels * i + channels > (int)block.size()) break;
710 708
711 for (int ch = 0; ch < channels; ++ch) { 709 for (int ch = 0; ch < channels; ++ch) {
712 710
713 int index = channels * i + ch; 711 int index = channels * i + ch;
714 float sample = block[index]; 712 float sample = block[index];
728 } 726 }
729 } 727 }
730 728
731 QMutexLocker locker(&m_model.m_mutex); 729 QMutexLocker locker(&m_model.m_mutex);
732 730
733 for (size_t ct = 0; ct < 2; ++ct) { 731 for (int ct = 0; ct < 2; ++ct) {
734 732
735 if (++count[ct] == cacheBlockSize[ct]) { 733 if (++count[ct] == cacheBlockSize[ct]) {
736 734
737 for (size_t ch = 0; ch < size_t(channels); ++ch) { 735 for (int ch = 0; ch < int(channels); ++ch) {
738 size_t rangeIndex = ch * 2 + ct; 736 int rangeIndex = ch * 2 + ct;
739 means[rangeIndex] /= count[ct]; 737 means[rangeIndex] /= count[ct];
740 range[rangeIndex].setAbsmean(means[rangeIndex]); 738 range[rangeIndex].setAbsmean(means[rangeIndex]);
741 m_model.m_cache[ct].push_back(range[rangeIndex]); 739 m_model.m_cache[ct].push_back(range[rangeIndex]);
742 range[rangeIndex] = Range(); 740 range[rangeIndex] = Range();
743 means[rangeIndex] = 0.f; 741 means[rangeIndex] = 0.f;
767 765
768 if (!m_model.m_exiting) { 766 if (!m_model.m_exiting) {
769 767
770 QMutexLocker locker(&m_model.m_mutex); 768 QMutexLocker locker(&m_model.m_mutex);
771 769
772 for (size_t ct = 0; ct < 2; ++ct) { 770 for (int ct = 0; ct < 2; ++ct) {
773 771
774 if (count[ct] > 0) { 772 if (count[ct] > 0) {
775 773
776 for (size_t ch = 0; ch < size_t(channels); ++ch) { 774 for (int ch = 0; ch < int(channels); ++ch) {
777 size_t rangeIndex = ch * 2 + ct; 775 int rangeIndex = ch * 2 + ct;
778 means[rangeIndex] /= count[ct]; 776 means[rangeIndex] /= count[ct];
779 range[rangeIndex].setAbsmean(means[rangeIndex]); 777 range[rangeIndex].setAbsmean(means[rangeIndex]);
780 m_model.m_cache[ct].push_back(range[rangeIndex]); 778 m_model.m_cache[ct].push_back(range[rangeIndex]);
781 range[rangeIndex] = Range(); 779 range[rangeIndex] = Range();
782 means[rangeIndex] = 0.f; 780 means[rangeIndex] = 0.f;
794 delete[] range; 792 delete[] range;
795 793
796 m_fillExtent = m_frameCount; 794 m_fillExtent = m_frameCount;
797 795
798 #ifdef DEBUG_WAVE_FILE_MODEL 796 #ifdef DEBUG_WAVE_FILE_MODEL
799 for (size_t ct = 0; ct < 2; ++ct) { 797 for (int ct = 0; ct < 2; ++ct) {
800 cerr << "Cache type " << ct << " now contains " << m_model.m_cache[ct].size() << " ranges" << endl; 798 cerr << "Cache type " << ct << " now contains " << m_model.m_cache[ct].size() << " ranges" << endl;
801 } 799 }
802 #endif 800 #endif
803 } 801 }
804 802