annotate data/model/WaveFileModel.cpp @ 1036:682d64f05e72

Merge
author Chris Cannam
date Mon, 02 Mar 2015 17:21:34 +0000
parents 36f79bc5c3d7
children cc27f35aa75c
rev   line source
Chris@147 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@147 2
Chris@147 3 /*
Chris@147 4 Sonic Visualiser
Chris@147 5 An audio file viewer and annotation editor.
Chris@147 6 Centre for Digital Music, Queen Mary, University of London.
Chris@202 7 This file copyright 2006 Chris Cannam and QMUL.
Chris@147 8
Chris@147 9 This program is free software; you can redistribute it and/or
Chris@147 10 modify it under the terms of the GNU General Public License as
Chris@147 11 published by the Free Software Foundation; either version 2 of the
Chris@147 12 License, or (at your option) any later version. See the file
Chris@147 13 COPYING included with this distribution for more information.
Chris@147 14 */
Chris@147 15
Chris@150 16 #include "WaveFileModel.h"
Chris@147 17
Chris@147 18 #include "fileio/AudioFileReader.h"
Chris@147 19 #include "fileio/AudioFileReaderFactory.h"
Chris@147 20
Chris@150 21 #include "system/System.h"
Chris@147 22
Chris@921 23 #include "base/Preferences.h"
Chris@921 24
Chris@147 25 #include <QFileInfo>
Chris@314 26 #include <QTextStream>
Chris@147 27
Chris@147 28 #include <iostream>
Chris@147 29 #include <unistd.h>
Chris@572 30 #include <cmath>
Chris@147 31 #include <sndfile.h>
Chris@147 32
Chris@147 33 #include <cassert>
Chris@147 34
Chris@236 35 //#define DEBUG_WAVE_FILE_MODEL 1
Chris@236 36
Chris@179 37 PowerOfSqrtTwoZoomConstraint
Chris@179 38 WaveFileModel::m_zoomConstraint;
Chris@179 39
Chris@929 40 WaveFileModel::WaveFileModel(FileSource source, int targetRate) :
Chris@316 41 m_source(source),
Chris@316 42 m_path(source.getLocation()),
Chris@971 43 m_reader(0),
Chris@175 44 m_myReader(true),
Chris@300 45 m_startFrame(0),
Chris@147 46 m_fillThread(0),
Chris@147 47 m_updateTimer(0),
Chris@147 48 m_lastFillExtent(0),
Chris@752 49 m_exiting(false),
Chris@752 50 m_lastDirectReadStart(0),
Chris@752 51 m_lastDirectReadCount(0)
Chris@147 52 {
Chris@316 53 m_source.waitForData();
Chris@316 54 if (m_source.isOK()) {
Chris@921 55 bool normalise = Preferences::getInstance()->getNormaliseAudio();
Chris@327 56 m_reader = AudioFileReaderFactory::createThreadingReader
Chris@921 57 (m_source, targetRate, normalise);
Chris@316 58 if (m_reader) {
Chris@690 59 SVDEBUG << "WaveFileModel::WaveFileModel: reader rate: "
Chris@687 60 << m_reader->getSampleRate() << endl;
Chris@316 61 }
Chris@316 62 }
Chris@292 63 if (m_reader) setObjectName(m_reader->getTitle());
Chris@316 64 if (objectName() == "") setObjectName(QFileInfo(m_path).fileName());
Chris@175 65 if (isOK()) fillCache();
Chris@175 66 }
Chris@175 67
Chris@317 68 WaveFileModel::WaveFileModel(FileSource source, AudioFileReader *reader) :
Chris@316 69 m_source(source),
Chris@316 70 m_path(source.getLocation()),
Chris@971 71 m_reader(0),
Chris@175 72 m_myReader(false),
Chris@300 73 m_startFrame(0),
Chris@175 74 m_fillThread(0),
Chris@175 75 m_updateTimer(0),
Chris@175 76 m_lastFillExtent(0),
Chris@175 77 m_exiting(false)
Chris@175 78 {
Chris@175 79 m_reader = reader;
Chris@292 80 if (m_reader) setObjectName(m_reader->getTitle());
Chris@316 81 if (objectName() == "") setObjectName(QFileInfo(m_path).fileName());
Chris@187 82 fillCache();
Chris@147 83 }
Chris@147 84
Chris@147 85 WaveFileModel::~WaveFileModel()
Chris@147 86 {
Chris@147 87 m_exiting = true;
Chris@147 88 if (m_fillThread) m_fillThread->wait();
Chris@175 89 if (m_myReader) delete m_reader;
Chris@147 90 m_reader = 0;
Chris@147 91 }
Chris@147 92
Chris@147 93 bool
Chris@147 94 WaveFileModel::isOK() const
Chris@147 95 {
Chris@147 96 return m_reader && m_reader->isOK();
Chris@147 97 }
Chris@147 98
Chris@147 99 bool
Chris@147 100 WaveFileModel::isReady(int *completion) const
Chris@147 101 {
Chris@147 102 bool ready = (isOK() && (m_fillThread == 0));
Chris@147 103 double c = double(m_lastFillExtent) / double(getEndFrame() - getStartFrame());
Chris@266 104 static int prevCompletion = 0;
Chris@265 105 if (completion) {
Chris@265 106 *completion = int(c * 100.0 + 0.01);
Chris@265 107 if (m_reader) {
Chris@265 108 int decodeCompletion = m_reader->getDecodeCompletion();
Chris@266 109 if (decodeCompletion < 90) *completion = decodeCompletion;
Chris@266 110 else *completion = std::min(*completion, decodeCompletion);
Chris@265 111 }
Chris@266 112 if (*completion != 0 &&
Chris@266 113 *completion != 100 &&
Chris@266 114 prevCompletion != 0 &&
Chris@266 115 prevCompletion > *completion) {
Chris@266 116 // just to avoid completion going backwards
Chris@266 117 *completion = prevCompletion;
Chris@266 118 }
Chris@266 119 prevCompletion = *completion;
Chris@265 120 }
Chris@236 121 #ifdef DEBUG_WAVE_FILE_MODEL
Chris@690 122 SVDEBUG << "WaveFileModel::isReady(): ready = " << ready << ", completion = " << (completion ? *completion : -1) << endl;
Chris@236 123 #endif
Chris@147 124 return ready;
Chris@147 125 }
Chris@147 126
Chris@147 127 Model *
Chris@147 128 WaveFileModel::clone() const
Chris@147 129 {
Chris@316 130 WaveFileModel *model = new WaveFileModel(m_source);
Chris@147 131 return model;
Chris@147 132 }
Chris@147 133
Chris@929 134 int
Chris@147 135 WaveFileModel::getFrameCount() const
Chris@147 136 {
Chris@147 137 if (!m_reader) return 0;
Chris@147 138 return m_reader->getFrameCount();
Chris@147 139 }
Chris@147 140
Chris@929 141 int
Chris@147 142 WaveFileModel::getChannelCount() const
Chris@147 143 {
Chris@147 144 if (!m_reader) return 0;
Chris@147 145 return m_reader->getChannelCount();
Chris@147 146 }
Chris@147 147
Chris@929 148 int
Chris@147 149 WaveFileModel::getSampleRate() const
Chris@147 150 {
Chris@147 151 if (!m_reader) return 0;
Chris@147 152 return m_reader->getSampleRate();
Chris@147 153 }
Chris@147 154
Chris@929 155 int
Chris@297 156 WaveFileModel::getNativeRate() const
Chris@297 157 {
Chris@297 158 if (!m_reader) return 0;
Chris@929 159 int rate = m_reader->getNativeRate();
Chris@297 160 if (rate == 0) rate = getSampleRate();
Chris@297 161 return rate;
Chris@297 162 }
Chris@297 163
Chris@333 164 QString
Chris@333 165 WaveFileModel::getTitle() const
Chris@333 166 {
Chris@333 167 QString title;
Chris@333 168 if (m_reader) title = m_reader->getTitle();
Chris@333 169 if (title == "") title = objectName();
Chris@333 170 return title;
Chris@333 171 }
Chris@333 172
Chris@333 173 QString
Chris@333 174 WaveFileModel::getMaker() const
Chris@333 175 {
Chris@333 176 if (m_reader) return m_reader->getMaker();
Chris@333 177 return "";
Chris@333 178 }
Chris@345 179
Chris@345 180 QString
Chris@345 181 WaveFileModel::getLocation() const
Chris@345 182 {
Chris@345 183 if (m_reader) return m_reader->getLocation();
Chris@345 184 return "";
Chris@345 185 }
Chris@1010 186
Chris@1010 187 QString
Chris@1010 188 WaveFileModel::getLocalFilename() const
Chris@1010 189 {
Chris@1010 190 if (m_reader) return m_reader->getLocalFilename();
Chris@1010 191 return "";
Chris@1010 192 }
Chris@333 193
Chris@929 194 int
Chris@929 195 WaveFileModel::getData(int channel, int start, int count,
Chris@300 196 float *buffer) const
Chris@147 197 {
Chris@147 198 // Always read these directly from the file.
Chris@147 199 // This is used for e.g. audio playback.
Chris@147 200 // Could be much more efficient (although compiler optimisation will help)
Chris@147 201
Chris@429 202 #ifdef DEBUG_WAVE_FILE_MODEL
Chris@843 203 cout << "WaveFileModel::getData[" << this << "]: " << channel << ", " << start << ", " << count << ", " << buffer << endl;
Chris@429 204 #endif
Chris@429 205
Chris@363 206 if (start >= m_startFrame) {
Chris@300 207 start -= m_startFrame;
Chris@300 208 } else {
Chris@948 209 for (int i = 0; i < count; ++i) {
Chris@948 210 buffer[i] = 0.f;
Chris@948 211 }
Chris@300 212 if (count <= m_startFrame - start) {
Chris@300 213 return 0;
Chris@300 214 } else {
Chris@300 215 count -= (m_startFrame - start);
Chris@300 216 start = 0;
Chris@300 217 }
Chris@147 218 }
Chris@147 219
Chris@300 220 if (!m_reader || !m_reader->isOK() || count == 0) {
Chris@929 221 for (int i = 0; i < count; ++i) buffer[i] = 0.f;
Chris@300 222 return 0;
Chris@300 223 }
Chris@147 224
Chris@236 225 #ifdef DEBUG_WAVE_FILE_MODEL
Chris@690 226 // SVDEBUG << "WaveFileModel::getValues(" << channel << ", "
Chris@687 227 // << start << ", " << end << "): calling reader" << endl;
Chris@236 228 #endif
Chris@175 229
Chris@363 230 int channels = getChannelCount();
Chris@363 231
Chris@363 232 SampleBlock frames(count * channels);
Chris@300 233 m_reader->getInterleavedFrames(start, count, frames);
Chris@147 234
Chris@929 235 int i = 0;
Chris@147 236
Chris@363 237 int ch0 = channel, ch1 = channel;
Chris@147 238 if (channel == -1) {
Chris@147 239 ch0 = 0;
Chris@147 240 ch1 = channels - 1;
Chris@147 241 }
Chris@147 242
Chris@300 243 while (i < count) {
Chris@147 244
Chris@147 245 buffer[i] = 0.0;
Chris@147 246
Chris@147 247 for (int ch = ch0; ch <= ch1; ++ch) {
Chris@147 248
Chris@929 249 int index = i * channels + ch;
Chris@929 250 if (index >= (int)frames.size()) break;
Chris@147 251
Chris@147 252 float sample = frames[index];
Chris@147 253 buffer[i] += sample;
Chris@147 254 }
Chris@147 255
Chris@147 256 ++i;
Chris@147 257 }
Chris@147 258
Chris@147 259 return i;
Chris@147 260 }
Chris@147 261
Chris@929 262 int
Chris@929 263 WaveFileModel::getData(int channel, int start, int count,
Chris@300 264 double *buffer) const
Chris@147 265 {
Chris@429 266 #ifdef DEBUG_WAVE_FILE_MODEL
Chris@843 267 cout << "WaveFileModel::getData(double)[" << this << "]: " << channel << ", " << start << ", " << count << ", " << buffer << endl;
Chris@429 268 #endif
Chris@429 269
Chris@300 270 if (start > m_startFrame) {
Chris@300 271 start -= m_startFrame;
Chris@300 272 } else {
Chris@929 273 for (int i = 0; i < count; ++i) buffer[i] = 0.0;
Chris@300 274 if (count <= m_startFrame - start) {
Chris@300 275 return 0;
Chris@300 276 } else {
Chris@300 277 count -= (m_startFrame - start);
Chris@300 278 start = 0;
Chris@300 279 }
Chris@147 280 }
Chris@147 281
Chris@300 282 if (!m_reader || !m_reader->isOK() || count == 0) {
Chris@929 283 for (int i = 0; i < count; ++i) buffer[i] = 0.0;
Chris@300 284 return 0;
Chris@300 285 }
Chris@147 286
Chris@363 287 int channels = getChannelCount();
Chris@363 288
Chris@363 289 SampleBlock frames(count * channels);
Chris@300 290 m_reader->getInterleavedFrames(start, count, frames);
Chris@147 291
Chris@929 292 int i = 0;
Chris@147 293
Chris@363 294 int ch0 = channel, ch1 = channel;
Chris@147 295 if (channel == -1) {
Chris@147 296 ch0 = 0;
Chris@147 297 ch1 = channels - 1;
Chris@147 298 }
Chris@147 299
Chris@300 300 while (i < count) {
Chris@147 301
Chris@147 302 buffer[i] = 0.0;
Chris@147 303
Chris@147 304 for (int ch = ch0; ch <= ch1; ++ch) {
Chris@147 305
Chris@929 306 int index = i * channels + ch;
Chris@929 307 if (index >= (int)frames.size()) break;
Chris@147 308
Chris@147 309 float sample = frames[index];
Chris@147 310 buffer[i] += sample;
Chris@147 311 }
Chris@147 312
Chris@147 313 ++i;
Chris@147 314 }
Chris@147 315
Chris@147 316 return i;
Chris@147 317 }
Chris@147 318
Chris@929 319 int
Chris@929 320 WaveFileModel::getData(int fromchannel, int tochannel,
Chris@929 321 int start, int count,
Chris@363 322 float **buffer) const
Chris@363 323 {
Chris@429 324 #ifdef DEBUG_WAVE_FILE_MODEL
Chris@843 325 cout << "WaveFileModel::getData[" << this << "]: " << fromchannel << "," << tochannel << ", " << start << ", " << count << ", " << buffer << endl;
Chris@429 326 #endif
Chris@429 327
Chris@929 328 int channels = getChannelCount();
Chris@363 329
Chris@363 330 if (fromchannel > tochannel) {
Chris@843 331 cerr << "ERROR: WaveFileModel::getData: fromchannel ("
Chris@363 332 << fromchannel << ") > tochannel (" << tochannel << ")"
Chris@843 333 << endl;
Chris@363 334 return 0;
Chris@363 335 }
Chris@363 336
Chris@363 337 if (tochannel >= channels) {
Chris@843 338 cerr << "ERROR: WaveFileModel::getData: tochannel ("
Chris@363 339 << tochannel << ") >= channel count (" << channels << ")"
Chris@843 340 << endl;
Chris@363 341 return 0;
Chris@363 342 }
Chris@363 343
Chris@363 344 if (fromchannel == tochannel) {
Chris@363 345 return getData(fromchannel, start, count, buffer[0]);
Chris@363 346 }
Chris@363 347
Chris@929 348 int reqchannels = (tochannel - fromchannel) + 1;
Chris@363 349
Chris@363 350 // Always read these directly from the file.
Chris@363 351 // This is used for e.g. audio playback.
Chris@363 352 // Could be much more efficient (although compiler optimisation will help)
Chris@363 353
Chris@363 354 if (start >= m_startFrame) {
Chris@363 355 start -= m_startFrame;
Chris@363 356 } else {
Chris@929 357 for (int c = 0; c < reqchannels; ++c) {
Chris@929 358 for (int i = 0; i < count; ++i) buffer[c][i] = 0.f;
Chris@363 359 }
Chris@363 360 if (count <= m_startFrame - start) {
Chris@363 361 return 0;
Chris@363 362 } else {
Chris@363 363 count -= (m_startFrame - start);
Chris@363 364 start = 0;
Chris@363 365 }
Chris@363 366 }
Chris@363 367
Chris@363 368 if (!m_reader || !m_reader->isOK() || count == 0) {
Chris@929 369 for (int c = 0; c < reqchannels; ++c) {
Chris@929 370 for (int i = 0; i < count; ++i) buffer[c][i] = 0.f;
Chris@363 371 }
Chris@363 372 return 0;
Chris@363 373 }
Chris@363 374
Chris@363 375 SampleBlock frames(count * channels);
Chris@363 376 m_reader->getInterleavedFrames(start, count, frames);
Chris@363 377
Chris@929 378 int i = 0;
Chris@363 379
Chris@929 380 int index = 0, available = frames.size();
Chris@363 381
Chris@363 382 while (i < count) {
Chris@363 383
Chris@363 384 if (index >= available) break;
Chris@363 385
Chris@929 386 int destc = 0;
Chris@363 387
Chris@929 388 for (int c = 0; c < channels; ++c) {
Chris@363 389
Chris@363 390 if (c >= fromchannel && c <= tochannel) {
Chris@363 391 buffer[destc][i] = frames[index];
Chris@363 392 ++destc;
Chris@363 393 }
Chris@363 394
Chris@363 395 ++index;
Chris@363 396 }
Chris@363 397
Chris@363 398 ++i;
Chris@363 399 }
Chris@363 400
Chris@363 401 return i;
Chris@363 402 }
Chris@363 403
Chris@929 404 int
Chris@929 405 WaveFileModel::getSummaryBlockSize(int desired) const
Chris@377 406 {
Chris@377 407 int cacheType = 0;
Chris@377 408 int power = m_zoomConstraint.getMinCachePower();
Chris@929 409 int roundedBlockSize = m_zoomConstraint.getNearestBlockSize
Chris@377 410 (desired, cacheType, power, ZoomConstraint::RoundDown);
Chris@377 411 if (cacheType != 0 && cacheType != 1) {
Chris@377 412 // We will be reading directly from file, so can satisfy any
Chris@377 413 // blocksize requirement
Chris@377 414 return desired;
Chris@377 415 } else {
Chris@377 416 return roundedBlockSize;
Chris@377 417 }
Chris@377 418 }
Chris@377 419
Chris@225 420 void
Chris@929 421 WaveFileModel::getSummaries(int channel, int start, int count,
Chris@929 422 RangeBlock &ranges, int &blockSize) const
Chris@147 423 {
Chris@225 424 ranges.clear();
Chris@225 425 if (!isOK()) return;
Chris@377 426 ranges.reserve((count / blockSize) + 1);
Chris@147 427
Chris@300 428 if (start > m_startFrame) start -= m_startFrame;
Chris@300 429 else if (count <= m_startFrame - start) return;
Chris@300 430 else {
Chris@300 431 count -= (m_startFrame - start);
Chris@300 432 start = 0;
Chris@147 433 }
Chris@147 434
Chris@147 435 int cacheType = 0;
Chris@179 436 int power = m_zoomConstraint.getMinCachePower();
Chris@929 437 int roundedBlockSize = m_zoomConstraint.getNearestBlockSize
Chris@377 438 (blockSize, cacheType, power, ZoomConstraint::RoundDown);
Chris@147 439
Chris@929 440 int channels = getChannelCount();
Chris@147 441
Chris@147 442 if (cacheType != 0 && cacheType != 1) {
Chris@147 443
Chris@147 444 // We need to read directly from the file. We haven't got
Chris@147 445 // this cached. Hope the requested area is small. This is
Chris@147 446 // not optimal -- we'll end up reading the same frames twice
Chris@147 447 // for stereo files, in two separate calls to this method.
Chris@147 448 // We could fairly trivially handle this for most cases that
Chris@147 449 // matter by putting a single cache in getInterleavedFrames
Chris@147 450 // for short queries.
Chris@147 451
Chris@377 452 m_directReadMutex.lock();
Chris@377 453
Chris@377 454 if (m_lastDirectReadStart != start ||
Chris@377 455 m_lastDirectReadCount != count ||
Chris@377 456 m_directRead.empty()) {
Chris@377 457
Chris@377 458 m_reader->getInterleavedFrames(start, count, m_directRead);
Chris@377 459 m_lastDirectReadStart = start;
Chris@377 460 m_lastDirectReadCount = count;
Chris@377 461 }
Chris@377 462
Chris@147 463 float max = 0.0, min = 0.0, total = 0.0;
Chris@929 464 int i = 0, got = 0;
Chris@147 465
Chris@300 466 while (i < count) {
Chris@147 467
Chris@929 468 int index = i * channels + channel;
Chris@929 469 if (index >= (int)m_directRead.size()) break;
Chris@147 470
Chris@377 471 float sample = m_directRead[index];
Chris@300 472 if (sample > max || got == 0) max = sample;
Chris@300 473 if (sample < min || got == 0) min = sample;
Chris@147 474 total += fabsf(sample);
Chris@838 475
Chris@147 476 ++i;
Chris@300 477 ++got;
Chris@147 478
Chris@300 479 if (got == blockSize) {
Chris@300 480 ranges.push_back(Range(min, max, total / got));
Chris@147 481 min = max = total = 0.0f;
Chris@300 482 got = 0;
Chris@147 483 }
Chris@147 484 }
Chris@147 485
Chris@377 486 m_directReadMutex.unlock();
Chris@377 487
Chris@300 488 if (got > 0) {
Chris@300 489 ranges.push_back(Range(min, max, total / got));
Chris@147 490 }
Chris@147 491
Chris@225 492 return;
Chris@147 493
Chris@147 494 } else {
Chris@147 495
Chris@147 496 QMutexLocker locker(&m_mutex);
Chris@147 497
Chris@147 498 const RangeBlock &cache = m_cache[cacheType];
Chris@147 499
Chris@377 500 blockSize = roundedBlockSize;
Chris@377 501
Chris@929 502 int cacheBlock, div;
Chris@147 503
Chris@147 504 if (cacheType == 0) {
Chris@179 505 cacheBlock = (1 << m_zoomConstraint.getMinCachePower());
Chris@147 506 div = (1 << power) / cacheBlock;
Chris@147 507 } else {
Chris@608 508 cacheBlock = ((unsigned int)((1 << m_zoomConstraint.getMinCachePower()) * sqrt(2.) + 0.01));
Chris@608 509 div = ((unsigned int)((1 << power) * sqrt(2.) + 0.01)) / cacheBlock;
Chris@147 510 }
Chris@147 511
Chris@929 512 int startIndex = start / cacheBlock;
Chris@929 513 int endIndex = (start + count) / cacheBlock;
Chris@147 514
Chris@147 515 float max = 0.0, min = 0.0, total = 0.0;
Chris@929 516 int i = 0, got = 0;
Chris@147 517
Chris@236 518 #ifdef DEBUG_WAVE_FILE_MODEL
Chris@300 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;
Chris@236 520 #endif
Chris@147 521
Chris@300 522 for (i = 0; i <= endIndex - startIndex; ) {
Chris@147 523
Chris@929 524 int index = (i + startIndex) * channels + channel;
Chris@929 525 if (index >= (int)cache.size()) break;
Chris@147 526
Chris@147 527 const Range &range = cache[index];
Chris@410 528 if (range.max() > max || got == 0) max = range.max();
Chris@410 529 if (range.min() < min || got == 0) min = range.min();
Chris@410 530 total += range.absmean();
Chris@147 531
Chris@147 532 ++i;
Chris@300 533 ++got;
Chris@147 534
Chris@300 535 if (got == div) {
Chris@300 536 ranges.push_back(Range(min, max, total / got));
Chris@147 537 min = max = total = 0.0f;
Chris@300 538 got = 0;
Chris@147 539 }
Chris@147 540 }
Chris@147 541
Chris@300 542 if (got > 0) {
Chris@300 543 ranges.push_back(Range(min, max, total / got));
Chris@147 544 }
Chris@147 545 }
Chris@147 546
Chris@236 547 #ifdef DEBUG_WAVE_FILE_MODEL
Chris@690 548 SVDEBUG << "returning " << ranges.size() << " ranges" << endl;
Chris@236 549 #endif
Chris@225 550 return;
Chris@147 551 }
Chris@147 552
Chris@147 553 WaveFileModel::Range
Chris@929 554 WaveFileModel::getSummary(int channel, int start, int count) const
Chris@147 555 {
Chris@147 556 Range range;
Chris@147 557 if (!isOK()) return range;
Chris@147 558
Chris@300 559 if (start > m_startFrame) start -= m_startFrame;
Chris@300 560 else if (count <= m_startFrame - start) return range;
Chris@300 561 else {
Chris@300 562 count -= (m_startFrame - start);
Chris@300 563 start = 0;
Chris@147 564 }
Chris@147 565
Chris@929 566 int blockSize;
Chris@300 567 for (blockSize = 1; blockSize <= count; blockSize *= 2);
Chris@300 568 if (blockSize > 1) blockSize /= 2;
Chris@147 569
Chris@147 570 bool first = false;
Chris@147 571
Chris@929 572 int blockStart = (start / blockSize) * blockSize;
Chris@929 573 int blockEnd = ((start + count) / blockSize) * blockSize;
Chris@147 574
Chris@147 575 if (blockStart < start) blockStart += blockSize;
Chris@147 576
Chris@147 577 if (blockEnd > blockStart) {
Chris@225 578 RangeBlock ranges;
Chris@300 579 getSummaries(channel, blockStart, blockEnd - blockStart, ranges, blockSize);
Chris@929 580 for (int i = 0; i < (int)ranges.size(); ++i) {
Chris@410 581 if (first || ranges[i].min() < range.min()) range.setMin(ranges[i].min());
Chris@410 582 if (first || ranges[i].max() > range.max()) range.setMax(ranges[i].max());
Chris@410 583 if (first || ranges[i].absmean() < range.absmean()) range.setAbsmean(ranges[i].absmean());
Chris@147 584 first = false;
Chris@147 585 }
Chris@147 586 }
Chris@147 587
Chris@147 588 if (blockStart > start) {
Chris@300 589 Range startRange = getSummary(channel, start, blockStart - start);
Chris@410 590 range.setMin(std::min(range.min(), startRange.min()));
Chris@410 591 range.setMax(std::max(range.max(), startRange.max()));
Chris@410 592 range.setAbsmean(std::min(range.absmean(), startRange.absmean()));
Chris@147 593 }
Chris@147 594
Chris@300 595 if (blockEnd < start + count) {
Chris@300 596 Range endRange = getSummary(channel, blockEnd, start + count - blockEnd);
Chris@410 597 range.setMin(std::min(range.min(), endRange.min()));
Chris@410 598 range.setMax(std::max(range.max(), endRange.max()));
Chris@410 599 range.setAbsmean(std::min(range.absmean(), endRange.absmean()));
Chris@147 600 }
Chris@147 601
Chris@147 602 return range;
Chris@147 603 }
Chris@147 604
Chris@147 605 void
Chris@147 606 WaveFileModel::fillCache()
Chris@147 607 {
Chris@147 608 m_mutex.lock();
Chris@188 609
Chris@147 610 m_updateTimer = new QTimer(this);
Chris@147 611 connect(m_updateTimer, SIGNAL(timeout()), this, SLOT(fillTimerTimedOut()));
Chris@147 612 m_updateTimer->start(100);
Chris@188 613
Chris@147 614 m_fillThread = new RangeCacheFillThread(*this);
Chris@147 615 connect(m_fillThread, SIGNAL(finished()), this, SLOT(cacheFilled()));
Chris@188 616
Chris@147 617 m_mutex.unlock();
Chris@147 618 m_fillThread->start();
Chris@188 619
Chris@236 620 #ifdef DEBUG_WAVE_FILE_MODEL
Chris@690 621 SVDEBUG << "WaveFileModel::fillCache: started fill thread" << endl;
Chris@236 622 #endif
Chris@147 623 }
Chris@147 624
Chris@147 625 void
Chris@147 626 WaveFileModel::fillTimerTimedOut()
Chris@147 627 {
Chris@147 628 if (m_fillThread) {
Chris@929 629 int fillExtent = m_fillThread->getFillExtent();
Chris@236 630 #ifdef DEBUG_WAVE_FILE_MODEL
Chris@690 631 SVDEBUG << "WaveFileModel::fillTimerTimedOut: extent = " << fillExtent << endl;
Chris@236 632 #endif
Chris@147 633 if (fillExtent > m_lastFillExtent) {
Chris@931 634 emit modelChangedWithin(m_lastFillExtent, fillExtent);
Chris@147 635 m_lastFillExtent = fillExtent;
Chris@147 636 }
Chris@147 637 } else {
Chris@236 638 #ifdef DEBUG_WAVE_FILE_MODEL
Chris@690 639 SVDEBUG << "WaveFileModel::fillTimerTimedOut: no thread" << endl;
Chris@236 640 #endif
Chris@147 641 emit modelChanged();
Chris@147 642 }
Chris@147 643 }
Chris@147 644
Chris@147 645 void
Chris@147 646 WaveFileModel::cacheFilled()
Chris@147 647 {
Chris@147 648 m_mutex.lock();
Chris@147 649 delete m_fillThread;
Chris@147 650 m_fillThread = 0;
Chris@147 651 delete m_updateTimer;
Chris@147 652 m_updateTimer = 0;
Chris@147 653 m_mutex.unlock();
Chris@267 654 if (getEndFrame() > m_lastFillExtent) {
Chris@931 655 emit modelChangedWithin(m_lastFillExtent, getEndFrame());
Chris@267 656 }
Chris@147 657 emit modelChanged();
Chris@411 658 emit ready();
Chris@236 659 #ifdef DEBUG_WAVE_FILE_MODEL
Chris@690 660 SVDEBUG << "WaveFileModel::cacheFilled" << endl;
Chris@236 661 #endif
Chris@175 662 }
Chris@175 663
Chris@175 664 void
Chris@147 665 WaveFileModel::RangeCacheFillThread::run()
Chris@147 666 {
Chris@929 667 int cacheBlockSize[2];
Chris@179 668 cacheBlockSize[0] = (1 << m_model.m_zoomConstraint.getMinCachePower());
Chris@179 669 cacheBlockSize[1] = ((unsigned int)((1 << m_model.m_zoomConstraint.getMinCachePower()) *
Chris@608 670 sqrt(2.) + 0.01));
Chris@147 671
Chris@929 672 int frame = 0;
Chris@411 673 int readBlockSize = 16384;
Chris@290 674 SampleBlock block;
Chris@147 675
Chris@147 676 if (!m_model.isOK()) return;
Chris@147 677
Chris@929 678 int channels = m_model.getChannelCount();
Chris@187 679 bool updating = m_model.m_reader->isUpdating();
Chris@187 680
Chris@187 681 if (updating) {
Chris@187 682 while (channels == 0 && !m_model.m_exiting) {
Chris@690 683 // SVDEBUG << "WaveFileModel::fill: Waiting for channels..." << endl;
Chris@187 684 sleep(1);
Chris@187 685 channels = m_model.getChannelCount();
Chris@187 686 }
Chris@187 687 }
Chris@147 688
Chris@147 689 Range *range = new Range[2 * channels];
Chris@410 690 float *means = new float[2 * channels];
Chris@929 691 int count[2];
Chris@147 692 count[0] = count[1] = 0;
Chris@411 693 for (int i = 0; i < 2 * channels; ++i) {
Chris@411 694 means[i] = 0.f;
Chris@411 695 }
Chris@176 696
Chris@176 697 bool first = true;
Chris@176 698
Chris@176 699 while (first || updating) {
Chris@176 700
Chris@176 701 updating = m_model.m_reader->isUpdating();
Chris@187 702 m_frameCount = m_model.getFrameCount();
Chris@175 703
Chris@690 704 // SVDEBUG << "WaveFileModel::fill: frame = " << frame << ", count = " << m_frameCount << endl;
Chris@147 705
Chris@176 706 while (frame < m_frameCount) {
Chris@147 707
Chris@690 708 // SVDEBUG << "WaveFileModel::fill inner loop: frame = " << frame << ", count = " << m_frameCount << ", blocksize " << readBlockSize << endl;
Chris@265 709
Chris@176 710 if (updating && (frame + readBlockSize > m_frameCount)) break;
Chris@176 711
Chris@176 712 m_model.m_reader->getInterleavedFrames(frame, readBlockSize, block);
Chris@176 713
Chris@843 714 // cerr << "block is " << block.size() << endl;
Chris@265 715
Chris@411 716 for (int i = 0; i < readBlockSize; ++i) {
Chris@147 717
Chris@929 718 if (channels * i + channels > (int)block.size()) break;
Chris@232 719
Chris@411 720 for (int ch = 0; ch < channels; ++ch) {
Chris@147 721
Chris@411 722 int index = channels * i + ch;
Chris@176 723 float sample = block[index];
Chris@176 724
Chris@411 725 for (int ct = 0; ct < 2; ++ct) { // cache type
Chris@176 726
Chris@411 727 int rangeIndex = ch * 2 + ct;
Chris@176 728
Chris@410 729 if (sample > range[rangeIndex].max() || count[ct] == 0) {
Chris@410 730 range[rangeIndex].setMax(sample);
Chris@176 731 }
Chris@410 732 if (sample < range[rangeIndex].min() || count[ct] == 0) {
Chris@410 733 range[rangeIndex].setMin(sample);
Chris@176 734 }
Chris@410 735
Chris@410 736 means[rangeIndex] += fabsf(sample);
Chris@176 737 }
Chris@176 738 }
Chris@147 739
Chris@176 740 QMutexLocker locker(&m_model.m_mutex);
Chris@232 741
Chris@929 742 for (int ct = 0; ct < 2; ++ct) {
Chris@232 743
Chris@176 744 if (++count[ct] == cacheBlockSize[ct]) {
Chris@410 745
Chris@929 746 for (int ch = 0; ch < int(channels); ++ch) {
Chris@929 747 int rangeIndex = ch * 2 + ct;
Chris@410 748 means[rangeIndex] /= count[ct];
Chris@410 749 range[rangeIndex].setAbsmean(means[rangeIndex]);
Chris@176 750 m_model.m_cache[ct].push_back(range[rangeIndex]);
Chris@176 751 range[rangeIndex] = Range();
Chris@411 752 means[rangeIndex] = 0.f;
Chris@176 753 }
Chris@232 754
Chris@176 755 count[ct] = 0;
Chris@176 756 }
Chris@176 757 }
Chris@147 758
Chris@176 759 ++frame;
Chris@147 760 }
Chris@147 761
Chris@176 762 if (m_model.m_exiting) break;
Chris@176 763
Chris@176 764 m_fillExtent = frame;
Chris@147 765 }
Chris@147 766
Chris@843 767 // cerr << "WaveFileModel: inner loop ended" << endl;
Chris@265 768
Chris@176 769 first = false;
Chris@177 770 if (m_model.m_exiting) break;
Chris@187 771 if (updating) {
Chris@843 772 // cerr << "sleeping..." << endl;
Chris@187 773 sleep(1);
Chris@187 774 }
Chris@147 775 }
Chris@147 776
Chris@177 777 if (!m_model.m_exiting) {
Chris@177 778
Chris@177 779 QMutexLocker locker(&m_model.m_mutex);
Chris@232 780
Chris@929 781 for (int ct = 0; ct < 2; ++ct) {
Chris@232 782
Chris@177 783 if (count[ct] > 0) {
Chris@232 784
Chris@929 785 for (int ch = 0; ch < int(channels); ++ch) {
Chris@929 786 int rangeIndex = ch * 2 + ct;
Chris@410 787 means[rangeIndex] /= count[ct];
Chris@410 788 range[rangeIndex].setAbsmean(means[rangeIndex]);
Chris@177 789 m_model.m_cache[ct].push_back(range[rangeIndex]);
Chris@177 790 range[rangeIndex] = Range();
Chris@411 791 means[rangeIndex] = 0.f;
Chris@177 792 }
Chris@232 793
Chris@177 794 count[ct] = 0;
Chris@147 795 }
Chris@177 796
Chris@177 797 const Range &rr = *m_model.m_cache[ct].begin();
Chris@177 798 MUNLOCK(&rr, m_model.m_cache[ct].capacity() * sizeof(Range));
Chris@147 799 }
Chris@147 800 }
Chris@147 801
Chris@410 802 delete[] means;
Chris@147 803 delete[] range;
Chris@147 804
Chris@175 805 m_fillExtent = m_frameCount;
Chris@236 806
Chris@236 807 #ifdef DEBUG_WAVE_FILE_MODEL
Chris@929 808 for (int ct = 0; ct < 2; ++ct) {
Chris@236 809 cerr << "Cache type " << ct << " now contains " << m_model.m_cache[ct].size() << " ranges" << endl;
Chris@236 810 }
Chris@236 811 #endif
Chris@147 812 }
Chris@147 813
Chris@163 814 void
Chris@163 815 WaveFileModel::toXml(QTextStream &out,
Chris@163 816 QString indent,
Chris@163 817 QString extraAttributes) const
Chris@163 818 {
Chris@163 819 Model::toXml(out, indent,
Chris@163 820 QString("type=\"wavefile\" file=\"%1\" %2")
Chris@279 821 .arg(encodeEntities(m_path)).arg(extraAttributes));
Chris@163 822 }
Chris@163 823
Chris@147 824