annotate data/model/ReadOnlyWaveFileModel.cpp @ 1711:d32d16e8883f single-point

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