comparison data/model/WaveFileModel.cpp @ 1038:cc27f35aa75c cxx11

Introducing the signed 64-bit frame index type, and fixing build failures from inclusion of -Wconversion with -Werror. Not finished yet.
author Chris Cannam
date Tue, 03 Mar 2015 15:18:24 +0000
parents 36f79bc5c3d7
children a1cd5abcb38b
comparison
equal deleted inserted replaced
1037:bf0e5944289b 1038:cc27f35aa75c
129 { 129 {
130 WaveFileModel *model = new WaveFileModel(m_source); 130 WaveFileModel *model = new WaveFileModel(m_source);
131 return model; 131 return model;
132 } 132 }
133 133
134 int 134 sv_frame_t
135 WaveFileModel::getFrameCount() const 135 WaveFileModel::getFrameCount() const
136 { 136 {
137 if (!m_reader) return 0; 137 if (!m_reader) return 0;
138 return m_reader->getFrameCount(); 138 return m_reader->getFrameCount();
139 } 139 }
189 { 189 {
190 if (m_reader) return m_reader->getLocalFilename(); 190 if (m_reader) return m_reader->getLocalFilename();
191 return ""; 191 return "";
192 } 192 }
193 193
194 int 194 sv_frame_t
195 WaveFileModel::getData(int channel, int start, int count, 195 WaveFileModel::getData(int channel, sv_frame_t start, sv_frame_t count,
196 float *buffer) const 196 float *buffer) const
197 { 197 {
198 // Always read these directly from the file. 198 // Always read these directly from the file.
199 // This is used for e.g. audio playback. 199 // This is used for e.g. audio playback.
200 // Could be much more efficient (although compiler optimisation will help) 200 // Could be much more efficient (although compiler optimisation will help)
204 #endif 204 #endif
205 205
206 if (start >= m_startFrame) { 206 if (start >= m_startFrame) {
207 start -= m_startFrame; 207 start -= m_startFrame;
208 } else { 208 } else {
209 for (int i = 0; i < count; ++i) { 209 for (sv_frame_t i = 0; i < count; ++i) {
210 buffer[i] = 0.f; 210 buffer[i] = 0.f;
211 } 211 }
212 if (count <= m_startFrame - start) { 212 if (count <= m_startFrame - start) {
213 return 0; 213 return 0;
214 } else { 214 } else {
216 start = 0; 216 start = 0;
217 } 217 }
218 } 218 }
219 219
220 if (!m_reader || !m_reader->isOK() || count == 0) { 220 if (!m_reader || !m_reader->isOK() || count == 0) {
221 for (int i = 0; i < count; ++i) buffer[i] = 0.f; 221 for (sv_frame_t i = 0; i < count; ++i) buffer[i] = 0.f;
222 return 0; 222 return 0;
223 } 223 }
224 224
225 #ifdef DEBUG_WAVE_FILE_MODEL 225 #ifdef DEBUG_WAVE_FILE_MODEL
226 // SVDEBUG << "WaveFileModel::getValues(" << channel << ", " 226 // SVDEBUG << "WaveFileModel::getValues(" << channel << ", "
230 int channels = getChannelCount(); 230 int channels = getChannelCount();
231 231
232 SampleBlock frames(count * channels); 232 SampleBlock frames(count * channels);
233 m_reader->getInterleavedFrames(start, count, frames); 233 m_reader->getInterleavedFrames(start, count, frames);
234 234
235 int i = 0; 235 sv_frame_t i = 0;
236 236
237 int ch0 = channel, ch1 = channel; 237 int ch0 = channel, ch1 = channel;
238 if (channel == -1) { 238 if (channel == -1) {
239 ch0 = 0; 239 ch0 = 0;
240 ch1 = channels - 1; 240 ch1 = channels - 1;
244 244
245 buffer[i] = 0.0; 245 buffer[i] = 0.0;
246 246
247 for (int ch = ch0; ch <= ch1; ++ch) { 247 for (int ch = ch0; ch <= ch1; ++ch) {
248 248
249 int index = i * channels + ch; 249 sv_frame_t index = i * channels + ch;
250 if (index >= (int)frames.size()) break; 250 if (index >= (sv_frame_t)frames.size()) break;
251 251
252 float sample = frames[index]; 252 float sample = frames[index];
253 buffer[i] += sample; 253 buffer[i] += sample;
254 } 254 }
255 255
257 } 257 }
258 258
259 return i; 259 return i;
260 } 260 }
261 261
262 int 262 sv_frame_t
263 WaveFileModel::getData(int channel, int start, int count, 263 WaveFileModel::getData(int channel, sv_frame_t start, sv_frame_t count,
264 double *buffer) const 264 double *buffer) const
265 { 265 {
266 #ifdef DEBUG_WAVE_FILE_MODEL 266 #ifdef DEBUG_WAVE_FILE_MODEL
267 cout << "WaveFileModel::getData(double)[" << this << "]: " << channel << ", " << start << ", " << count << ", " << buffer << endl; 267 cout << "WaveFileModel::getData(double)[" << this << "]: " << channel << ", " << start << ", " << count << ", " << buffer << endl;
268 #endif 268 #endif
269 269
270 if (start > m_startFrame) { 270 if (start > m_startFrame) {
271 start -= m_startFrame; 271 start -= m_startFrame;
272 } else { 272 } else {
273 for (int i = 0; i < count; ++i) buffer[i] = 0.0; 273 for (sv_frame_t i = 0; i < count; ++i) buffer[i] = 0.0;
274 if (count <= m_startFrame - start) { 274 if (count <= m_startFrame - start) {
275 return 0; 275 return 0;
276 } else { 276 } else {
277 count -= (m_startFrame - start); 277 count -= (m_startFrame - start);
278 start = 0; 278 start = 0;
279 } 279 }
280 } 280 }
281 281
282 if (!m_reader || !m_reader->isOK() || count == 0) { 282 if (!m_reader || !m_reader->isOK() || count == 0) {
283 for (int i = 0; i < count; ++i) buffer[i] = 0.0; 283 for (sv_frame_t i = 0; i < count; ++i) buffer[i] = 0.0;
284 return 0; 284 return 0;
285 } 285 }
286 286
287 int channels = getChannelCount(); 287 int channels = getChannelCount();
288 288
289 SampleBlock frames(count * channels); 289 SampleBlock frames(count * channels);
290 m_reader->getInterleavedFrames(start, count, frames); 290 m_reader->getInterleavedFrames(start, count, frames);
291 291
292 int i = 0; 292 sv_frame_t i = 0;
293 293
294 int ch0 = channel, ch1 = channel; 294 int ch0 = channel, ch1 = channel;
295 if (channel == -1) { 295 if (channel == -1) {
296 ch0 = 0; 296 ch0 = 0;
297 ch1 = channels - 1; 297 ch1 = channels - 1;
301 301
302 buffer[i] = 0.0; 302 buffer[i] = 0.0;
303 303
304 for (int ch = ch0; ch <= ch1; ++ch) { 304 for (int ch = ch0; ch <= ch1; ++ch) {
305 305
306 int index = i * channels + ch; 306 sv_frame_t index = i * channels + ch;
307 if (index >= (int)frames.size()) break; 307 if (index >= (sv_frame_t)frames.size()) break;
308 308
309 float sample = frames[index]; 309 float sample = frames[index];
310 buffer[i] += sample; 310 buffer[i] += sample;
311 } 311 }
312 312
314 } 314 }
315 315
316 return i; 316 return i;
317 } 317 }
318 318
319 int 319 sv_frame_t
320 WaveFileModel::getData(int fromchannel, int tochannel, 320 WaveFileModel::getData(int fromchannel, int tochannel,
321 int start, int count, 321 sv_frame_t start, sv_frame_t count,
322 float **buffer) const 322 float **buffer) const
323 { 323 {
324 #ifdef DEBUG_WAVE_FILE_MODEL 324 #ifdef DEBUG_WAVE_FILE_MODEL
325 cout << "WaveFileModel::getData[" << this << "]: " << fromchannel << "," << tochannel << ", " << start << ", " << count << ", " << buffer << endl; 325 cout << "WaveFileModel::getData[" << this << "]: " << fromchannel << "," << tochannel << ", " << start << ", " << count << ", " << buffer << endl;
326 #endif 326 #endif
353 353
354 if (start >= m_startFrame) { 354 if (start >= m_startFrame) {
355 start -= m_startFrame; 355 start -= m_startFrame;
356 } else { 356 } else {
357 for (int c = 0; c < reqchannels; ++c) { 357 for (int c = 0; c < reqchannels; ++c) {
358 for (int i = 0; i < count; ++i) buffer[c][i] = 0.f; 358 for (sv_frame_t i = 0; i < count; ++i) buffer[c][i] = 0.f;
359 } 359 }
360 if (count <= m_startFrame - start) { 360 if (count <= m_startFrame - start) {
361 return 0; 361 return 0;
362 } else { 362 } else {
363 count -= (m_startFrame - start); 363 count -= (m_startFrame - start);
365 } 365 }
366 } 366 }
367 367
368 if (!m_reader || !m_reader->isOK() || count == 0) { 368 if (!m_reader || !m_reader->isOK() || count == 0) {
369 for (int c = 0; c < reqchannels; ++c) { 369 for (int c = 0; c < reqchannels; ++c) {
370 for (int i = 0; i < count; ++i) buffer[c][i] = 0.f; 370 for (sv_frame_t i = 0; i < count; ++i) buffer[c][i] = 0.f;
371 } 371 }
372 return 0; 372 return 0;
373 } 373 }
374 374
375 SampleBlock frames(count * channels); 375 SampleBlock frames(count * channels);
376 m_reader->getInterleavedFrames(start, count, frames); 376 m_reader->getInterleavedFrames(start, count, frames);
377 377
378 int i = 0; 378 sv_frame_t i = 0;
379 379
380 int index = 0, available = frames.size(); 380 sv_frame_t index = 0, available = frames.size();
381 381
382 while (i < count) { 382 while (i < count) {
383 383
384 if (index >= available) break; 384 if (index >= available) break;
385 385
416 return roundedBlockSize; 416 return roundedBlockSize;
417 } 417 }
418 } 418 }
419 419
420 void 420 void
421 WaveFileModel::getSummaries(int channel, int start, int count, 421 WaveFileModel::getSummaries(int channel, sv_frame_t start, sv_frame_t count,
422 RangeBlock &ranges, int &blockSize) const 422 RangeBlock &ranges, int &blockSize) const
423 { 423 {
424 ranges.clear(); 424 ranges.clear();
425 if (!isOK()) return; 425 if (!isOK()) return;
426 ranges.reserve((count / blockSize) + 1); 426 ranges.reserve((count / blockSize) + 1);
459 m_lastDirectReadStart = start; 459 m_lastDirectReadStart = start;
460 m_lastDirectReadCount = count; 460 m_lastDirectReadCount = count;
461 } 461 }
462 462
463 float max = 0.0, min = 0.0, total = 0.0; 463 float max = 0.0, min = 0.0, total = 0.0;
464 int i = 0, got = 0; 464 sv_frame_t i = 0, got = 0;
465 465
466 while (i < count) { 466 while (i < count) {
467 467
468 int index = i * channels + channel; 468 sv_frame_t index = i * channels + channel;
469 if (index >= (int)m_directRead.size()) break; 469 if (index >= (sv_frame_t)m_directRead.size()) break;
470 470
471 float sample = m_directRead[index]; 471 float sample = m_directRead[index];
472 if (sample > max || got == 0) max = sample; 472 if (sample > max || got == 0) max = sample;
473 if (sample < min || got == 0) min = sample; 473 if (sample < min || got == 0) min = sample;
474 total += fabsf(sample); 474 total += fabsf(sample);
475 475
476 ++i; 476 ++i;
477 ++got; 477 ++got;
478 478
479 if (got == blockSize) { 479 if (got == blockSize) {
480 ranges.push_back(Range(min, max, total / got)); 480 ranges.push_back(Range(min, max, total / float(got)));
481 min = max = total = 0.0f; 481 min = max = total = 0.0f;
482 got = 0; 482 got = 0;
483 } 483 }
484 } 484 }
485 485
486 m_directReadMutex.unlock(); 486 m_directReadMutex.unlock();
487 487
488 if (got > 0) { 488 if (got > 0) {
489 ranges.push_back(Range(min, max, total / got)); 489 ranges.push_back(Range(min, max, total / float(got)));
490 } 490 }
491 491
492 return; 492 return;
493 493
494 } else { 494 } else {
497 497
498 const RangeBlock &cache = m_cache[cacheType]; 498 const RangeBlock &cache = m_cache[cacheType];
499 499
500 blockSize = roundedBlockSize; 500 blockSize = roundedBlockSize;
501 501
502 int cacheBlock, div; 502 sv_frame_t cacheBlock, div;
503 503
504 if (cacheType == 0) { 504 if (cacheType == 0) {
505 cacheBlock = (1 << m_zoomConstraint.getMinCachePower()); 505 cacheBlock = (1 << m_zoomConstraint.getMinCachePower());
506 div = (1 << power) / cacheBlock; 506 div = (1 << power) / cacheBlock;
507 } else { 507 } else {
508 cacheBlock = ((unsigned int)((1 << m_zoomConstraint.getMinCachePower()) * sqrt(2.) + 0.01)); 508 cacheBlock = sv_frame_t((1 << m_zoomConstraint.getMinCachePower()) * sqrt(2.) + 0.01);
509 div = ((unsigned int)((1 << power) * sqrt(2.) + 0.01)) / cacheBlock; 509 div = sv_frame_t(((1 << power) * sqrt(2.) + 0.01) / double(cacheBlock));
510 } 510 }
511 511
512 int startIndex = start / cacheBlock; 512 sv_frame_t startIndex = start / cacheBlock;
513 int endIndex = (start + count) / cacheBlock; 513 sv_frame_t endIndex = (start + count) / cacheBlock;
514 514
515 float max = 0.0, min = 0.0, total = 0.0; 515 float max = 0.0, min = 0.0, total = 0.0;
516 int i = 0, got = 0; 516 sv_frame_t i = 0, got = 0;
517 517
518 #ifdef DEBUG_WAVE_FILE_MODEL 518 #ifdef DEBUG_WAVE_FILE_MODEL
519 cerr << "blockSize is " << blockSize << ", cacheBlock " << cacheBlock << ", start " << start << ", count " << count << " (frame count " << getFrameCount() << "), power is " << power << ", div is " << div << ", startIndex " << startIndex << ", endIndex " << endIndex << endl; 519 cerr << "blockSize is " << blockSize << ", cacheBlock " << cacheBlock << ", start " << start << ", count " << count << " (frame count " << getFrameCount() << "), power is " << power << ", div is " << div << ", startIndex " << startIndex << ", endIndex " << endIndex << endl;
520 #endif 520 #endif
521 521
522 for (i = 0; i <= endIndex - startIndex; ) { 522 for (i = 0; i <= endIndex - startIndex; ) {
523 523
524 int index = (i + startIndex) * channels + channel; 524 sv_frame_t index = (i + startIndex) * channels + channel;
525 if (index >= (int)cache.size()) break; 525 if (index >= (sv_frame_t)cache.size()) break;
526 526
527 const Range &range = cache[index]; 527 const Range &range = cache[index];
528 if (range.max() > max || got == 0) max = range.max(); 528 if (range.max() > max || got == 0) max = range.max();
529 if (range.min() < min || got == 0) min = range.min(); 529 if (range.min() < min || got == 0) min = range.min();
530 total += range.absmean(); 530 total += range.absmean();
531 531
532 ++i; 532 ++i;
533 ++got; 533 ++got;
534 534
535 if (got == div) { 535 if (got == div) {
536 ranges.push_back(Range(min, max, total / got)); 536 ranges.push_back(Range(min, max, total / float(got)));
537 min = max = total = 0.0f; 537 min = max = total = 0.0f;
538 got = 0; 538 got = 0;
539 } 539 }
540 } 540 }
541 541
542 if (got > 0) { 542 if (got > 0) {
543 ranges.push_back(Range(min, max, total / got)); 543 ranges.push_back(Range(min, max, total / float(got)));
544 } 544 }
545 } 545 }
546 546
547 #ifdef DEBUG_WAVE_FILE_MODEL 547 #ifdef DEBUG_WAVE_FILE_MODEL
548 SVDEBUG << "returning " << ranges.size() << " ranges" << endl; 548 SVDEBUG << "returning " << ranges.size() << " ranges" << endl;
549 #endif 549 #endif
550 return; 550 return;
551 } 551 }
552 552
553 WaveFileModel::Range 553 WaveFileModel::Range
554 WaveFileModel::getSummary(int channel, int start, int count) const 554 WaveFileModel::getSummary(int channel, sv_frame_t start, sv_frame_t count) const
555 { 555 {
556 Range range; 556 Range range;
557 if (!isOK()) return range; 557 if (!isOK()) return range;
558 558
559 if (start > m_startFrame) start -= m_startFrame; 559 if (start > m_startFrame) start -= m_startFrame;
567 for (blockSize = 1; blockSize <= count; blockSize *= 2); 567 for (blockSize = 1; blockSize <= count; blockSize *= 2);
568 if (blockSize > 1) blockSize /= 2; 568 if (blockSize > 1) blockSize /= 2;
569 569
570 bool first = false; 570 bool first = false;
571 571
572 int blockStart = (start / blockSize) * blockSize; 572 sv_frame_t blockStart = (start / blockSize) * blockSize;
573 int blockEnd = ((start + count) / blockSize) * blockSize; 573 sv_frame_t blockEnd = ((start + count) / blockSize) * blockSize;
574 574
575 if (blockStart < start) blockStart += blockSize; 575 if (blockStart < start) blockStart += blockSize;
576 576
577 if (blockEnd > blockStart) { 577 if (blockEnd > blockStart) {
578 RangeBlock ranges; 578 RangeBlock ranges;
624 624
625 void 625 void
626 WaveFileModel::fillTimerTimedOut() 626 WaveFileModel::fillTimerTimedOut()
627 { 627 {
628 if (m_fillThread) { 628 if (m_fillThread) {
629 int fillExtent = m_fillThread->getFillExtent(); 629 sv_frame_t fillExtent = m_fillThread->getFillExtent();
630 #ifdef DEBUG_WAVE_FILE_MODEL 630 #ifdef DEBUG_WAVE_FILE_MODEL
631 SVDEBUG << "WaveFileModel::fillTimerTimedOut: extent = " << fillExtent << endl; 631 SVDEBUG << "WaveFileModel::fillTimerTimedOut: extent = " << fillExtent << endl;
632 #endif 632 #endif
633 if (fillExtent > m_lastFillExtent) { 633 if (fillExtent > m_lastFillExtent) {
634 emit modelChangedWithin(m_lastFillExtent, fillExtent); 634 emit modelChangedWithin(m_lastFillExtent, fillExtent);
664 void 664 void
665 WaveFileModel::RangeCacheFillThread::run() 665 WaveFileModel::RangeCacheFillThread::run()
666 { 666 {
667 int cacheBlockSize[2]; 667 int cacheBlockSize[2];
668 cacheBlockSize[0] = (1 << m_model.m_zoomConstraint.getMinCachePower()); 668 cacheBlockSize[0] = (1 << m_model.m_zoomConstraint.getMinCachePower());
669 cacheBlockSize[1] = ((unsigned int)((1 << m_model.m_zoomConstraint.getMinCachePower()) * 669 cacheBlockSize[1] = (int((1 << m_model.m_zoomConstraint.getMinCachePower()) *
670 sqrt(2.) + 0.01)); 670 sqrt(2.) + 0.01));
671 671
672 int frame = 0; 672 sv_frame_t frame = 0;
673 int readBlockSize = 16384; 673 sv_frame_t readBlockSize = 16384;
674 SampleBlock block; 674 SampleBlock block;
675 675
676 if (!m_model.isOK()) return; 676 if (!m_model.isOK()) return;
677 677
678 int channels = m_model.getChannelCount(); 678 int channels = m_model.getChannelCount();
711 711
712 m_model.m_reader->getInterleavedFrames(frame, readBlockSize, block); 712 m_model.m_reader->getInterleavedFrames(frame, readBlockSize, block);
713 713
714 // cerr << "block is " << block.size() << endl; 714 // cerr << "block is " << block.size() << endl;
715 715
716 for (int i = 0; i < readBlockSize; ++i) { 716 for (sv_frame_t i = 0; i < readBlockSize; ++i) {
717 717
718 if (channels * i + channels > (int)block.size()) break; 718 if (channels * i + channels > (int)block.size()) break;
719 719
720 for (int ch = 0; ch < channels; ++ch) { 720 for (int ch = 0; ch < channels; ++ch) {
721 721
722 int index = channels * i + ch; 722 sv_frame_t index = channels * i + ch;
723 float sample = block[index]; 723 float sample = block[index];
724 724
725 for (int ct = 0; ct < 2; ++ct) { // cache type 725 for (int ct = 0; ct < 2; ++ct) { // cache type
726 726
727 int rangeIndex = ch * 2 + ct; 727 sv_frame_t rangeIndex = ch * 2 + ct;
728 728
729 if (sample > range[rangeIndex].max() || count[ct] == 0) { 729 if (sample > range[rangeIndex].max() || count[ct] == 0) {
730 range[rangeIndex].setMax(sample); 730 range[rangeIndex].setMax(sample);
731 } 731 }
732 if (sample < range[rangeIndex].min() || count[ct] == 0) { 732 if (sample < range[rangeIndex].min() || count[ct] == 0) {
743 743
744 if (++count[ct] == cacheBlockSize[ct]) { 744 if (++count[ct] == cacheBlockSize[ct]) {
745 745
746 for (int ch = 0; ch < int(channels); ++ch) { 746 for (int ch = 0; ch < int(channels); ++ch) {
747 int rangeIndex = ch * 2 + ct; 747 int rangeIndex = ch * 2 + ct;
748 means[rangeIndex] /= count[ct]; 748 means[rangeIndex] = means[rangeIndex] / float(count[ct]);
749 range[rangeIndex].setAbsmean(means[rangeIndex]); 749 range[rangeIndex].setAbsmean(means[rangeIndex]);
750 m_model.m_cache[ct].push_back(range[rangeIndex]); 750 m_model.m_cache[ct].push_back(range[rangeIndex]);
751 range[rangeIndex] = Range(); 751 range[rangeIndex] = Range();
752 means[rangeIndex] = 0.f; 752 means[rangeIndex] = 0.f;
753 } 753 }
782 782
783 if (count[ct] > 0) { 783 if (count[ct] > 0) {
784 784
785 for (int ch = 0; ch < int(channels); ++ch) { 785 for (int ch = 0; ch < int(channels); ++ch) {
786 int rangeIndex = ch * 2 + ct; 786 int rangeIndex = ch * 2 + ct;
787 means[rangeIndex] /= count[ct]; 787 means[rangeIndex] = means[rangeIndex] / float(count[ct]);
788 range[rangeIndex].setAbsmean(means[rangeIndex]); 788 range[rangeIndex].setAbsmean(means[rangeIndex]);
789 m_model.m_cache[ct].push_back(range[rangeIndex]); 789 m_model.m_cache[ct].push_back(range[rangeIndex]);
790 range[rangeIndex] = Range(); 790 range[rangeIndex] = Range();
791 means[rangeIndex] = 0.f; 791 means[rangeIndex] = 0.f;
792 } 792 }