annotate data/fileio/DecodingWavFileReader.cpp @ 1700:c1208b211d8c single-point

Ensure test fails rather than crashing if this reader doesn't get created
author Chris Cannam <cannam@all-day-breakfast.com>
date Fri, 03 May 2019 15:02:09 +0100
parents ce185d4dd408
children 14747f24ad04
rev   line source
Chris@297 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@297 2
Chris@297 3 /*
Chris@297 4 Sonic Visualiser
Chris@297 5 An audio file viewer and annotation editor.
Chris@297 6 Centre for Digital Music, Queen Mary, University of London.
Chris@297 7 This file copyright 2007 QMUL.
Chris@297 8
Chris@297 9 This program is free software; you can redistribute it and/or
Chris@297 10 modify it under the terms of the GNU General Public License as
Chris@297 11 published by the Free Software Foundation; either version 2 of the
Chris@297 12 License, or (at your option) any later version. See the file
Chris@297 13 COPYING included with this distribution for more information.
Chris@297 14 */
Chris@297 15
Chris@823 16 #include "DecodingWavFileReader.h"
Chris@297 17
Chris@297 18 #include "WavFileReader.h"
Chris@297 19 #include "base/Profiler.h"
Chris@392 20 #include "base/ProgressReporter.h"
Chris@297 21
Chris@297 22 #include <QFileInfo>
Chris@297 23
Chris@1096 24 using namespace std;
Chris@1096 25
Chris@823 26 DecodingWavFileReader::DecodingWavFileReader(FileSource source,
Chris@1097 27 DecodeMode decodeMode,
Chris@920 28 CacheMode mode,
Chris@1040 29 sv_samplerate_t targetRate,
Chris@920 30 bool normalised,
Chris@920 31 ProgressReporter *reporter) :
Chris@920 32 CodedAudioFileReader(mode, targetRate, normalised),
Chris@316 33 m_source(source),
Chris@316 34 m_path(source.getLocalFilename()),
Chris@297 35 m_cancelled(false),
Chris@297 36 m_processed(0),
Chris@297 37 m_completion(0),
Chris@1582 38 m_original(nullptr),
Chris@392 39 m_reporter(reporter),
Chris@1582 40 m_decodeThread(nullptr)
Chris@297 41 {
Chris@1279 42 SVDEBUG << "DecodingWavFileReader: local path: \"" << m_path
Chris@1279 43 << "\", decode mode: " << decodeMode << " ("
Chris@1279 44 << (decodeMode == DecodeAtOnce ? "DecodeAtOnce" : "DecodeThreaded")
Chris@1279 45 << ")" << endl;
Chris@1279 46
Chris@297 47 m_channelCount = 0;
Chris@297 48 m_fileRate = 0;
Chris@297 49
Chris@1295 50 Profiler profiler("DecodingWavFileReader::DecodingWavFileReader");
Chris@297 51
Chris@316 52 m_original = new WavFileReader(m_path);
Chris@297 53 if (!m_original->isOK()) {
Chris@297 54 m_error = m_original->getError();
Chris@297 55 return;
Chris@297 56 }
Chris@297 57
Chris@297 58 m_channelCount = m_original->getChannelCount();
Chris@297 59 m_fileRate = m_original->getSampleRate();
Chris@297 60
Chris@1592 61 m_title = m_original->getTitle();
Chris@1592 62 m_maker = m_original->getMaker();
Chris@1592 63
Chris@297 64 initialiseDecodeCache();
Chris@297 65
Chris@1097 66 if (decodeMode == DecodeAtOnce) {
Chris@297 67
Chris@392 68 if (m_reporter) {
Chris@392 69 connect(m_reporter, SIGNAL(cancelled()), this, SLOT(cancelled()));
Chris@392 70 m_reporter->setMessage
Chris@823 71 (tr("Decoding %1...").arg(QFileInfo(m_path).fileName()));
Chris@327 72 }
Chris@297 73
Chris@1038 74 sv_frame_t blockSize = 16384;
Chris@1038 75 sv_frame_t total = m_original->getFrameCount();
Chris@297 76
Chris@1326 77 floatvec_t block;
Chris@297 78
Chris@1038 79 for (sv_frame_t i = 0; i < total; i += blockSize) {
Chris@297 80
Chris@1038 81 sv_frame_t count = blockSize;
Chris@297 82 if (i + count > total) count = total - i;
Chris@297 83
Chris@1041 84 block = m_original->getInterleavedFrames(i, count);
Chris@297 85 addBlock(block);
Chris@297 86
Chris@297 87 if (m_cancelled) break;
Chris@297 88 }
Chris@297 89
Chris@297 90 if (isDecodeCacheInitialised()) finishDecodeCache();
Chris@398 91 endSerialised();
Chris@297 92
Chris@403 93 if (m_reporter) m_reporter->setProgress(100);
Chris@403 94
Chris@297 95 delete m_original;
Chris@1582 96 m_original = nullptr;
Chris@297 97
Chris@392 98 } else {
Chris@297 99
Chris@392 100 if (m_reporter) m_reporter->setProgress(100);
Chris@297 101
Chris@297 102 m_decodeThread = new DecodeThread(this);
Chris@297 103 m_decodeThread->start();
Chris@297 104 }
Chris@297 105 }
Chris@297 106
Chris@823 107 DecodingWavFileReader::~DecodingWavFileReader()
Chris@297 108 {
Chris@297 109 if (m_decodeThread) {
Chris@297 110 m_cancelled = true;
Chris@297 111 m_decodeThread->wait();
Chris@297 112 delete m_decodeThread;
Chris@297 113 }
Chris@297 114
Chris@297 115 delete m_original;
Chris@297 116 }
Chris@297 117
Chris@297 118 void
Chris@823 119 DecodingWavFileReader::cancelled()
Chris@392 120 {
Chris@392 121 m_cancelled = true;
Chris@392 122 }
Chris@392 123
Chris@392 124 void
Chris@823 125 DecodingWavFileReader::DecodeThread::run()
Chris@297 126 {
Chris@297 127 if (m_reader->m_cacheMode == CacheInTemporaryFile) {
Chris@823 128 m_reader->startSerialised("DecodingWavFileReader::Decode");
Chris@297 129 }
Chris@297 130
Chris@1038 131 sv_frame_t blockSize = 16384;
Chris@1038 132 sv_frame_t total = m_reader->m_original->getFrameCount();
Chris@297 133
Chris@1326 134 floatvec_t block;
Chris@297 135
Chris@1038 136 for (sv_frame_t i = 0; i < total; i += blockSize) {
Chris@297 137
Chris@1038 138 sv_frame_t count = blockSize;
Chris@297 139 if (i + count > total) count = total - i;
Chris@297 140
Chris@1041 141 block = m_reader->m_original->getInterleavedFrames(i, count);
Chris@297 142 m_reader->addBlock(block);
Chris@297 143
Chris@297 144 if (m_reader->m_cancelled) break;
Chris@297 145 }
Chris@297 146
Chris@297 147 if (m_reader->isDecodeCacheInitialised()) m_reader->finishDecodeCache();
Chris@297 148 m_reader->m_completion = 100;
Chris@297 149
Chris@297 150 m_reader->endSerialised();
Chris@297 151
Chris@297 152 delete m_reader->m_original;
Chris@1582 153 m_reader->m_original = nullptr;
Chris@297 154 }
Chris@297 155
Chris@297 156 void
Chris@1326 157 DecodingWavFileReader::addBlock(const floatvec_t &frames)
Chris@297 158 {
Chris@297 159 addSamplesToDecodeCache(frames);
Chris@297 160
Chris@297 161 m_processed += frames.size();
Chris@297 162
Chris@1038 163 double ratio = double(m_sampleRate) / double(m_fileRate);
Chris@403 164
Chris@1038 165 int progress = int(lrint((double(m_processed) * ratio * 100) /
Chris@1038 166 double(m_original->getFrameCount())));
Chris@297 167
Chris@297 168 if (progress > 99) progress = 99;
Chris@297 169 m_completion = progress;
Chris@297 170
Chris@392 171 if (m_reporter) {
Chris@392 172 m_reporter->setProgress(progress);
Chris@297 173 }
Chris@297 174 }
Chris@297 175
Chris@297 176 void
Chris@1096 177 DecodingWavFileReader::getSupportedExtensions(set<QString> &extensions)
Chris@297 178 {
Chris@297 179 WavFileReader::getSupportedExtensions(extensions);
Chris@297 180 }
Chris@297 181
Chris@316 182 bool
Chris@823 183 DecodingWavFileReader::supportsExtension(QString extension)
Chris@316 184 {
Chris@316 185 return WavFileReader::supportsExtension(extension);
Chris@316 186 }
Chris@297 187
Chris@316 188 bool
Chris@823 189 DecodingWavFileReader::supportsContentType(QString type)
Chris@316 190 {
Chris@316 191 return WavFileReader::supportsContentType(type);
Chris@316 192 }
Chris@316 193
Chris@316 194 bool
Chris@823 195 DecodingWavFileReader::supports(FileSource &source)
Chris@316 196 {
Chris@316 197 return WavFileReader::supports(source);
Chris@316 198 }
Chris@316 199
Chris@316 200