annotate data/model/WaveFileModel.cpp @ 1076:106081811ccd

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