annotate data/fileio/DecodingWavFileReader.cpp @ 1184:e5a1d815f331 3.0-integration

Merge from the default branch
author Chris Cannam
date Thu, 21 Apr 2016 15:29:30 +0100
parents abc309f507ae
children 0a9193dc136b
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@297 38 m_original(0),
Chris@392 39 m_reporter(reporter),
Chris@297 40 m_decodeThread(0)
Chris@297 41 {
Chris@297 42 m_channelCount = 0;
Chris@297 43 m_fileRate = 0;
Chris@297 44
Chris@823 45 SVDEBUG << "DecodingWavFileReader::DecodingWavFileReader(\""
Chris@687 46 << m_path << "\"): rate " << targetRate << endl;
Chris@297 47
Chris@823 48 Profiler profiler("DecodingWavFileReader::DecodingWavFileReader", true);
Chris@297 49
Chris@316 50 m_original = new WavFileReader(m_path);
Chris@297 51 if (!m_original->isOK()) {
Chris@297 52 m_error = m_original->getError();
Chris@297 53 return;
Chris@297 54 }
Chris@297 55
Chris@297 56 m_channelCount = m_original->getChannelCount();
Chris@297 57 m_fileRate = m_original->getSampleRate();
Chris@297 58
Chris@297 59 initialiseDecodeCache();
Chris@297 60
Chris@1097 61 if (decodeMode == DecodeAtOnce) {
Chris@297 62
Chris@392 63 if (m_reporter) {
Chris@392 64 connect(m_reporter, SIGNAL(cancelled()), this, SLOT(cancelled()));
Chris@392 65 m_reporter->setMessage
Chris@823 66 (tr("Decoding %1...").arg(QFileInfo(m_path).fileName()));
Chris@327 67 }
Chris@297 68
Chris@1038 69 sv_frame_t blockSize = 16384;
Chris@1038 70 sv_frame_t total = m_original->getFrameCount();
Chris@297 71
Chris@1096 72 vector<float> block;
Chris@297 73
Chris@1038 74 for (sv_frame_t i = 0; i < total; i += blockSize) {
Chris@297 75
Chris@1038 76 sv_frame_t count = blockSize;
Chris@297 77 if (i + count > total) count = total - i;
Chris@297 78
Chris@1041 79 block = m_original->getInterleavedFrames(i, count);
Chris@297 80 addBlock(block);
Chris@297 81
Chris@297 82 if (m_cancelled) break;
Chris@297 83 }
Chris@297 84
Chris@297 85 if (isDecodeCacheInitialised()) finishDecodeCache();
Chris@398 86 endSerialised();
Chris@297 87
Chris@403 88 if (m_reporter) m_reporter->setProgress(100);
Chris@403 89
Chris@297 90 delete m_original;
Chris@297 91 m_original = 0;
Chris@297 92
Chris@392 93 } else {
Chris@297 94
Chris@392 95 if (m_reporter) m_reporter->setProgress(100);
Chris@297 96
Chris@297 97 m_decodeThread = new DecodeThread(this);
Chris@297 98 m_decodeThread->start();
Chris@297 99 }
Chris@297 100 }
Chris@297 101
Chris@823 102 DecodingWavFileReader::~DecodingWavFileReader()
Chris@297 103 {
Chris@297 104 if (m_decodeThread) {
Chris@297 105 m_cancelled = true;
Chris@297 106 m_decodeThread->wait();
Chris@297 107 delete m_decodeThread;
Chris@297 108 }
Chris@297 109
Chris@297 110 delete m_original;
Chris@297 111 }
Chris@297 112
Chris@297 113 void
Chris@823 114 DecodingWavFileReader::cancelled()
Chris@392 115 {
Chris@392 116 m_cancelled = true;
Chris@392 117 }
Chris@392 118
Chris@392 119 void
Chris@823 120 DecodingWavFileReader::DecodeThread::run()
Chris@297 121 {
Chris@297 122 if (m_reader->m_cacheMode == CacheInTemporaryFile) {
Chris@823 123 m_reader->startSerialised("DecodingWavFileReader::Decode");
Chris@297 124 }
Chris@297 125
Chris@1038 126 sv_frame_t blockSize = 16384;
Chris@1038 127 sv_frame_t total = m_reader->m_original->getFrameCount();
Chris@297 128
Chris@1096 129 vector<float> block;
Chris@297 130
Chris@1038 131 for (sv_frame_t i = 0; i < total; i += blockSize) {
Chris@297 132
Chris@1038 133 sv_frame_t count = blockSize;
Chris@297 134 if (i + count > total) count = total - i;
Chris@297 135
Chris@1041 136 block = m_reader->m_original->getInterleavedFrames(i, count);
Chris@297 137 m_reader->addBlock(block);
Chris@297 138
Chris@297 139 if (m_reader->m_cancelled) break;
Chris@297 140 }
Chris@297 141
Chris@297 142 if (m_reader->isDecodeCacheInitialised()) m_reader->finishDecodeCache();
Chris@297 143 m_reader->m_completion = 100;
Chris@297 144
Chris@297 145 m_reader->endSerialised();
Chris@297 146
Chris@297 147 delete m_reader->m_original;
Chris@297 148 m_reader->m_original = 0;
Chris@297 149 }
Chris@297 150
Chris@297 151 void
Chris@1096 152 DecodingWavFileReader::addBlock(const vector<float> &frames)
Chris@297 153 {
Chris@297 154 addSamplesToDecodeCache(frames);
Chris@297 155
Chris@297 156 m_processed += frames.size();
Chris@297 157
Chris@1038 158 double ratio = double(m_sampleRate) / double(m_fileRate);
Chris@403 159
Chris@1038 160 int progress = int(lrint((double(m_processed) * ratio * 100) /
Chris@1038 161 double(m_original->getFrameCount())));
Chris@297 162
Chris@297 163 if (progress > 99) progress = 99;
Chris@297 164 m_completion = progress;
Chris@297 165
Chris@392 166 if (m_reporter) {
Chris@392 167 m_reporter->setProgress(progress);
Chris@297 168 }
Chris@297 169 }
Chris@297 170
Chris@297 171 void
Chris@1096 172 DecodingWavFileReader::getSupportedExtensions(set<QString> &extensions)
Chris@297 173 {
Chris@297 174 WavFileReader::getSupportedExtensions(extensions);
Chris@297 175 }
Chris@297 176
Chris@316 177 bool
Chris@823 178 DecodingWavFileReader::supportsExtension(QString extension)
Chris@316 179 {
Chris@316 180 return WavFileReader::supportsExtension(extension);
Chris@316 181 }
Chris@297 182
Chris@316 183 bool
Chris@823 184 DecodingWavFileReader::supportsContentType(QString type)
Chris@316 185 {
Chris@316 186 return WavFileReader::supportsContentType(type);
Chris@316 187 }
Chris@316 188
Chris@316 189 bool
Chris@823 190 DecodingWavFileReader::supports(FileSource &source)
Chris@316 191 {
Chris@316 192 return WavFileReader::supports(source);
Chris@316 193 }
Chris@316 194
Chris@316 195