annotate data/model/WaveFileModel.cpp @ 370:e73a1a1e8f24

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