annotate data/model/WaveFileModel.cpp @ 382:9867f99e0bb7 sv1-v1.2

* Fix #1894369 1.2pre5 crashes on copying data
author Chris Cannam
date Fri, 15 Feb 2008 15:15:29 +0000
parents 166c22eff678
children 183ee2a55fc7
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@377 382 size_t
Chris@377 383 WaveFileModel::getSummaryBlockSize(size_t desired) const
Chris@377 384 {
Chris@377 385 int cacheType = 0;
Chris@377 386 int power = m_zoomConstraint.getMinCachePower();
Chris@377 387 size_t roundedBlockSize = m_zoomConstraint.getNearestBlockSize
Chris@377 388 (desired, cacheType, power, ZoomConstraint::RoundDown);
Chris@377 389 if (cacheType != 0 && cacheType != 1) {
Chris@377 390 // We will be reading directly from file, so can satisfy any
Chris@377 391 // blocksize requirement
Chris@377 392 return desired;
Chris@377 393 } else {
Chris@377 394 return roundedBlockSize;
Chris@377 395 }
Chris@377 396 }
Chris@377 397
Chris@225 398 void
Chris@300 399 WaveFileModel::getSummaries(size_t channel, size_t start, size_t count,
Chris@300 400 RangeBlock &ranges, size_t &blockSize) const
Chris@147 401 {
Chris@225 402 ranges.clear();
Chris@225 403 if (!isOK()) return;
Chris@377 404 ranges.reserve((count / blockSize) + 1);
Chris@147 405
Chris@300 406 if (start > m_startFrame) start -= m_startFrame;
Chris@300 407 else if (count <= m_startFrame - start) return;
Chris@300 408 else {
Chris@300 409 count -= (m_startFrame - start);
Chris@300 410 start = 0;
Chris@147 411 }
Chris@147 412
Chris@147 413 int cacheType = 0;
Chris@179 414 int power = m_zoomConstraint.getMinCachePower();
Chris@377 415 size_t roundedBlockSize = m_zoomConstraint.getNearestBlockSize
Chris@377 416 (blockSize, cacheType, power, ZoomConstraint::RoundDown);
Chris@147 417
Chris@147 418 size_t channels = getChannelCount();
Chris@147 419
Chris@147 420 if (cacheType != 0 && cacheType != 1) {
Chris@147 421
Chris@147 422 // We need to read directly from the file. We haven't got
Chris@147 423 // this cached. Hope the requested area is small. This is
Chris@147 424 // not optimal -- we'll end up reading the same frames twice
Chris@147 425 // for stereo files, in two separate calls to this method.
Chris@147 426 // We could fairly trivially handle this for most cases that
Chris@147 427 // matter by putting a single cache in getInterleavedFrames
Chris@147 428 // for short queries.
Chris@147 429
Chris@377 430 m_directReadMutex.lock();
Chris@377 431
Chris@377 432 if (m_lastDirectReadStart != start ||
Chris@377 433 m_lastDirectReadCount != count ||
Chris@377 434 m_directRead.empty()) {
Chris@377 435
Chris@377 436 m_reader->getInterleavedFrames(start, count, m_directRead);
Chris@377 437 m_lastDirectReadStart = start;
Chris@377 438 m_lastDirectReadCount = count;
Chris@377 439 }
Chris@377 440
Chris@147 441 float max = 0.0, min = 0.0, total = 0.0;
Chris@300 442 size_t i = 0, got = 0;
Chris@147 443
Chris@300 444 while (i < count) {
Chris@147 445
Chris@147 446 size_t index = i * channels + channel;
Chris@377 447 if (index >= m_directRead.size()) break;
Chris@147 448
Chris@377 449 float sample = m_directRead[index];
Chris@300 450 if (sample > max || got == 0) max = sample;
Chris@300 451 if (sample < min || got == 0) min = sample;
Chris@147 452 total += fabsf(sample);
Chris@147 453
Chris@147 454 ++i;
Chris@300 455 ++got;
Chris@147 456
Chris@300 457 if (got == blockSize) {
Chris@300 458 ranges.push_back(Range(min, max, total / got));
Chris@147 459 min = max = total = 0.0f;
Chris@300 460 got = 0;
Chris@147 461 }
Chris@147 462 }
Chris@147 463
Chris@377 464 m_directReadMutex.unlock();
Chris@377 465
Chris@300 466 if (got > 0) {
Chris@300 467 ranges.push_back(Range(min, max, total / got));
Chris@147 468 }
Chris@147 469
Chris@225 470 return;
Chris@147 471
Chris@147 472 } else {
Chris@147 473
Chris@147 474 QMutexLocker locker(&m_mutex);
Chris@147 475
Chris@147 476 const RangeBlock &cache = m_cache[cacheType];
Chris@147 477
Chris@377 478 blockSize = roundedBlockSize;
Chris@377 479
Chris@147 480 size_t cacheBlock, div;
Chris@147 481
Chris@147 482 if (cacheType == 0) {
Chris@179 483 cacheBlock = (1 << m_zoomConstraint.getMinCachePower());
Chris@147 484 div = (1 << power) / cacheBlock;
Chris@147 485 } else {
Chris@179 486 cacheBlock = ((unsigned int)((1 << m_zoomConstraint.getMinCachePower()) * sqrt(2) + 0.01));
Chris@147 487 div = ((unsigned int)((1 << power) * sqrt(2) + 0.01)) / cacheBlock;
Chris@147 488 }
Chris@147 489
Chris@147 490 size_t startIndex = start / cacheBlock;
Chris@300 491 size_t endIndex = (start + count) / cacheBlock;
Chris@147 492
Chris@147 493 float max = 0.0, min = 0.0, total = 0.0;
Chris@300 494 size_t i = 0, got = 0;
Chris@147 495
Chris@236 496 #ifdef DEBUG_WAVE_FILE_MODEL
Chris@300 497 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 498 #endif
Chris@147 499
Chris@300 500 for (i = 0; i <= endIndex - startIndex; ) {
Chris@147 501
Chris@147 502 size_t index = (i + startIndex) * channels + channel;
Chris@147 503 if (index >= cache.size()) break;
Chris@147 504
Chris@147 505 const Range &range = cache[index];
Chris@300 506 if (range.max > max || got == 0) max = range.max;
Chris@300 507 if (range.min < min || got == 0) min = range.min;
Chris@147 508 total += range.absmean;
Chris@147 509
Chris@147 510 ++i;
Chris@300 511 ++got;
Chris@147 512
Chris@300 513 if (got == div) {
Chris@300 514 ranges.push_back(Range(min, max, total / got));
Chris@147 515 min = max = total = 0.0f;
Chris@300 516 got = 0;
Chris@147 517 }
Chris@147 518 }
Chris@147 519
Chris@300 520 if (got > 0) {
Chris@300 521 ranges.push_back(Range(min, max, total / got));
Chris@147 522 }
Chris@147 523 }
Chris@147 524
Chris@236 525 #ifdef DEBUG_WAVE_FILE_MODEL
Chris@232 526 cerr << "returning " << ranges.size() << " ranges" << endl;
Chris@236 527 #endif
Chris@225 528 return;
Chris@147 529 }
Chris@147 530
Chris@147 531 WaveFileModel::Range
Chris@300 532 WaveFileModel::getSummary(size_t channel, size_t start, size_t count) const
Chris@147 533 {
Chris@147 534 Range range;
Chris@147 535 if (!isOK()) return range;
Chris@147 536
Chris@300 537 if (start > m_startFrame) start -= m_startFrame;
Chris@300 538 else if (count <= m_startFrame - start) return range;
Chris@300 539 else {
Chris@300 540 count -= (m_startFrame - start);
Chris@300 541 start = 0;
Chris@147 542 }
Chris@147 543
Chris@147 544 size_t blockSize;
Chris@300 545 for (blockSize = 1; blockSize <= count; blockSize *= 2);
Chris@300 546 if (blockSize > 1) blockSize /= 2;
Chris@147 547
Chris@147 548 bool first = false;
Chris@147 549
Chris@147 550 size_t blockStart = (start / blockSize) * blockSize;
Chris@300 551 size_t blockEnd = ((start + count) / blockSize) * blockSize;
Chris@147 552
Chris@147 553 if (blockStart < start) blockStart += blockSize;
Chris@147 554
Chris@147 555 if (blockEnd > blockStart) {
Chris@225 556 RangeBlock ranges;
Chris@300 557 getSummaries(channel, blockStart, blockEnd - blockStart, ranges, blockSize);
Chris@147 558 for (size_t i = 0; i < ranges.size(); ++i) {
Chris@147 559 if (first || ranges[i].min < range.min) range.min = ranges[i].min;
Chris@147 560 if (first || ranges[i].max > range.max) range.max = ranges[i].max;
Chris@147 561 if (first || ranges[i].absmean < range.absmean) range.absmean = ranges[i].absmean;
Chris@147 562 first = false;
Chris@147 563 }
Chris@147 564 }
Chris@147 565
Chris@147 566 if (blockStart > start) {
Chris@300 567 Range startRange = getSummary(channel, start, blockStart - start);
Chris@147 568 range.min = std::min(range.min, startRange.min);
Chris@147 569 range.max = std::max(range.max, startRange.max);
Chris@147 570 range.absmean = std::min(range.absmean, startRange.absmean);
Chris@147 571 }
Chris@147 572
Chris@300 573 if (blockEnd < start + count) {
Chris@300 574 Range endRange = getSummary(channel, blockEnd, start + count - blockEnd);
Chris@147 575 range.min = std::min(range.min, endRange.min);
Chris@147 576 range.max = std::max(range.max, endRange.max);
Chris@147 577 range.absmean = std::min(range.absmean, endRange.absmean);
Chris@147 578 }
Chris@147 579
Chris@147 580 return range;
Chris@147 581 }
Chris@147 582
Chris@147 583 void
Chris@147 584 WaveFileModel::fillCache()
Chris@147 585 {
Chris@147 586 m_mutex.lock();
Chris@188 587
Chris@147 588 m_updateTimer = new QTimer(this);
Chris@147 589 connect(m_updateTimer, SIGNAL(timeout()), this, SLOT(fillTimerTimedOut()));
Chris@147 590 m_updateTimer->start(100);
Chris@188 591
Chris@147 592 m_fillThread = new RangeCacheFillThread(*this);
Chris@147 593 connect(m_fillThread, SIGNAL(finished()), this, SLOT(cacheFilled()));
Chris@188 594
Chris@147 595 m_mutex.unlock();
Chris@147 596 m_fillThread->start();
Chris@188 597
Chris@236 598 #ifdef DEBUG_WAVE_FILE_MODEL
Chris@236 599 std::cerr << "WaveFileModel::fillCache: started fill thread" << std::endl;
Chris@236 600 #endif
Chris@147 601 }
Chris@147 602
Chris@147 603 void
Chris@147 604 WaveFileModel::fillTimerTimedOut()
Chris@147 605 {
Chris@147 606 if (m_fillThread) {
Chris@147 607 size_t fillExtent = m_fillThread->getFillExtent();
Chris@236 608 #ifdef DEBUG_WAVE_FILE_MODEL
Chris@236 609 cerr << "WaveFileModel::fillTimerTimedOut: extent = " << fillExtent << endl;
Chris@236 610 #endif
Chris@147 611 if (fillExtent > m_lastFillExtent) {
Chris@147 612 emit modelChanged(m_lastFillExtent, fillExtent);
Chris@147 613 m_lastFillExtent = fillExtent;
Chris@147 614 }
Chris@147 615 } else {
Chris@236 616 #ifdef DEBUG_WAVE_FILE_MODEL
Chris@236 617 cerr << "WaveFileModel::fillTimerTimedOut: no thread" << std::endl;
Chris@236 618 #endif
Chris@147 619 emit modelChanged();
Chris@147 620 }
Chris@147 621 }
Chris@147 622
Chris@147 623 void
Chris@147 624 WaveFileModel::cacheFilled()
Chris@147 625 {
Chris@147 626 m_mutex.lock();
Chris@147 627 delete m_fillThread;
Chris@147 628 m_fillThread = 0;
Chris@147 629 delete m_updateTimer;
Chris@147 630 m_updateTimer = 0;
Chris@147 631 m_mutex.unlock();
Chris@267 632 if (getEndFrame() > m_lastFillExtent) {
Chris@267 633 emit modelChanged(m_lastFillExtent, getEndFrame());
Chris@267 634 }
Chris@147 635 emit modelChanged();
Chris@236 636 #ifdef DEBUG_WAVE_FILE_MODEL
Chris@236 637 cerr << "WaveFileModel::cacheFilled" << endl;
Chris@236 638 #endif
Chris@175 639 }
Chris@175 640
Chris@175 641 void
Chris@147 642 WaveFileModel::RangeCacheFillThread::run()
Chris@147 643 {
Chris@147 644 size_t cacheBlockSize[2];
Chris@179 645 cacheBlockSize[0] = (1 << m_model.m_zoomConstraint.getMinCachePower());
Chris@179 646 cacheBlockSize[1] = ((unsigned int)((1 << m_model.m_zoomConstraint.getMinCachePower()) *
Chris@147 647 sqrt(2) + 0.01));
Chris@147 648
Chris@147 649 size_t frame = 0;
Chris@147 650 size_t readBlockSize = 16384;
Chris@290 651 SampleBlock block;
Chris@147 652
Chris@147 653 if (!m_model.isOK()) return;
Chris@147 654
Chris@147 655 size_t channels = m_model.getChannelCount();
Chris@187 656 bool updating = m_model.m_reader->isUpdating();
Chris@187 657
Chris@187 658 if (updating) {
Chris@187 659 while (channels == 0 && !m_model.m_exiting) {
Chris@188 660 // std::cerr << "WaveFileModel::fill: Waiting for channels..." << std::endl;
Chris@187 661 sleep(1);
Chris@187 662 channels = m_model.getChannelCount();
Chris@187 663 }
Chris@187 664 }
Chris@147 665
Chris@147 666 Range *range = new Range[2 * channels];
Chris@147 667 size_t count[2];
Chris@147 668 count[0] = count[1] = 0;
Chris@176 669
Chris@176 670 bool first = true;
Chris@176 671
Chris@176 672 while (first || updating) {
Chris@176 673
Chris@176 674 updating = m_model.m_reader->isUpdating();
Chris@187 675 m_frameCount = m_model.getFrameCount();
Chris@175 676
Chris@188 677 // std::cerr << "WaveFileModel::fill: frame = " << frame << ", count = " << m_frameCount << std::endl;
Chris@147 678
Chris@176 679 while (frame < m_frameCount) {
Chris@147 680
Chris@265 681 // std::cerr << "WaveFileModel::fill inner loop: frame = " << frame << ", count = " << m_frameCount << ", blocksize " << readBlockSize << std::endl;
Chris@265 682
Chris@176 683 if (updating && (frame + readBlockSize > m_frameCount)) break;
Chris@176 684
Chris@176 685 m_model.m_reader->getInterleavedFrames(frame, readBlockSize, block);
Chris@176 686
Chris@265 687 // std::cerr << "block is " << block.size() << std::endl;
Chris@265 688
Chris@176 689 for (size_t i = 0; i < readBlockSize; ++i) {
Chris@147 690
Chris@232 691 if (channels * i + channels > block.size()) break;
Chris@232 692
Chris@176 693 for (size_t ch = 0; ch < size_t(channels); ++ch) {
Chris@147 694
Chris@176 695 size_t index = channels * i + ch;
Chris@176 696 float sample = block[index];
Chris@176 697
Chris@232 698 for (size_t ct = 0; ct < 2; ++ct) { // cache type
Chris@176 699
Chris@176 700 size_t rangeIndex = ch * 2 + ct;
Chris@176 701
Chris@176 702 if (sample > range[rangeIndex].max || count[ct] == 0) {
Chris@176 703 range[rangeIndex].max = sample;
Chris@176 704 }
Chris@176 705 if (sample < range[rangeIndex].min || count[ct] == 0) {
Chris@176 706 range[rangeIndex].min = sample;
Chris@176 707 }
Chris@176 708 range[rangeIndex].absmean += fabsf(sample);
Chris@176 709 }
Chris@176 710 }
Chris@147 711
Chris@176 712 QMutexLocker locker(&m_model.m_mutex);
Chris@232 713
Chris@147 714 for (size_t ct = 0; ct < 2; ++ct) {
Chris@232 715
Chris@176 716 if (++count[ct] == cacheBlockSize[ct]) {
Chris@232 717
Chris@176 718 for (size_t ch = 0; ch < size_t(channels); ++ch) {
Chris@176 719 size_t rangeIndex = ch * 2 + ct;
Chris@176 720 range[rangeIndex].absmean /= count[ct];
Chris@176 721 m_model.m_cache[ct].push_back(range[rangeIndex]);
Chris@176 722 range[rangeIndex] = Range();
Chris@176 723 }
Chris@232 724
Chris@176 725 count[ct] = 0;
Chris@176 726 }
Chris@176 727 }
Chris@147 728
Chris@176 729 ++frame;
Chris@147 730 }
Chris@147 731
Chris@176 732 if (m_model.m_exiting) break;
Chris@176 733
Chris@176 734 m_fillExtent = frame;
Chris@147 735 }
Chris@147 736
Chris@265 737 // std::cerr << "WaveFileModel: inner loop ended" << std::endl;
Chris@265 738
Chris@176 739 first = false;
Chris@177 740 if (m_model.m_exiting) break;
Chris@187 741 if (updating) {
Chris@265 742 // std::cerr << "sleeping..." << std::endl;
Chris@187 743 sleep(1);
Chris@187 744 }
Chris@147 745 }
Chris@147 746
Chris@177 747 if (!m_model.m_exiting) {
Chris@177 748
Chris@177 749 QMutexLocker locker(&m_model.m_mutex);
Chris@232 750
Chris@177 751 for (size_t ct = 0; ct < 2; ++ct) {
Chris@232 752
Chris@177 753 if (count[ct] > 0) {
Chris@232 754
Chris@177 755 for (size_t ch = 0; ch < size_t(channels); ++ch) {
Chris@177 756 size_t rangeIndex = ch * 2 + ct;
Chris@177 757 range[rangeIndex].absmean /= count[ct];
Chris@177 758 m_model.m_cache[ct].push_back(range[rangeIndex]);
Chris@177 759 range[rangeIndex] = Range();
Chris@177 760 }
Chris@232 761
Chris@177 762 count[ct] = 0;
Chris@147 763 }
Chris@177 764
Chris@177 765 const Range &rr = *m_model.m_cache[ct].begin();
Chris@177 766 MUNLOCK(&rr, m_model.m_cache[ct].capacity() * sizeof(Range));
Chris@147 767 }
Chris@147 768 }
Chris@147 769
Chris@147 770 delete[] range;
Chris@147 771
Chris@175 772 m_fillExtent = m_frameCount;
Chris@236 773
Chris@236 774 #ifdef DEBUG_WAVE_FILE_MODEL
Chris@236 775 for (size_t ct = 0; ct < 2; ++ct) {
Chris@236 776 cerr << "Cache type " << ct << " now contains " << m_model.m_cache[ct].size() << " ranges" << endl;
Chris@236 777 }
Chris@236 778 #endif
Chris@147 779 }
Chris@147 780
Chris@163 781 void
Chris@163 782 WaveFileModel::toXml(QTextStream &out,
Chris@163 783 QString indent,
Chris@163 784 QString extraAttributes) const
Chris@163 785 {
Chris@163 786 Model::toXml(out, indent,
Chris@163 787 QString("type=\"wavefile\" file=\"%1\" %2")
Chris@279 788 .arg(encodeEntities(m_path)).arg(extraAttributes));
Chris@163 789 }
Chris@163 790
Chris@147 791