annotate data/model/ReadOnlyWaveFileModel.cpp @ 1550:1ae6a19464a7 zoom-double

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