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@1858
|
128 m_reader->startSerialised("DecodingWavFileReader::Decode",
|
Chris@1858
|
129 &m_reader->m_cancelled);
|
Chris@1858
|
130 if (m_reader->m_cancelled) {
|
Chris@1858
|
131 return;
|
Chris@1858
|
132 }
|
Chris@297
|
133 }
|
Chris@297
|
134
|
Chris@1038
|
135 sv_frame_t blockSize = 16384;
|
Chris@1038
|
136 sv_frame_t total = m_reader->m_original->getFrameCount();
|
Chris@297
|
137
|
Chris@1326
|
138 floatvec_t block;
|
Chris@297
|
139
|
Chris@1038
|
140 for (sv_frame_t i = 0; i < total; i += blockSize) {
|
Chris@297
|
141
|
Chris@1038
|
142 sv_frame_t count = blockSize;
|
Chris@297
|
143 if (i + count > total) count = total - i;
|
Chris@297
|
144
|
Chris@1041
|
145 block = m_reader->m_original->getInterleavedFrames(i, count);
|
Chris@297
|
146 m_reader->addBlock(block);
|
Chris@297
|
147
|
Chris@297
|
148 if (m_reader->m_cancelled) break;
|
Chris@297
|
149 }
|
Chris@297
|
150
|
Chris@297
|
151 if (m_reader->isDecodeCacheInitialised()) m_reader->finishDecodeCache();
|
Chris@297
|
152 m_reader->m_completion = 100;
|
Chris@297
|
153
|
Chris@297
|
154 m_reader->endSerialised();
|
Chris@297
|
155
|
Chris@297
|
156 delete m_reader->m_original;
|
Chris@1582
|
157 m_reader->m_original = nullptr;
|
Chris@297
|
158 }
|
Chris@297
|
159
|
Chris@297
|
160 void
|
Chris@1326
|
161 DecodingWavFileReader::addBlock(const floatvec_t &frames)
|
Chris@297
|
162 {
|
Chris@297
|
163 addSamplesToDecodeCache(frames);
|
Chris@297
|
164
|
Chris@297
|
165 m_processed += frames.size();
|
Chris@297
|
166
|
Chris@1038
|
167 double ratio = double(m_sampleRate) / double(m_fileRate);
|
Chris@403
|
168
|
Chris@1038
|
169 int progress = int(lrint((double(m_processed) * ratio * 100) /
|
Chris@1038
|
170 double(m_original->getFrameCount())));
|
Chris@297
|
171
|
Chris@297
|
172 if (progress > 99) progress = 99;
|
Chris@297
|
173 m_completion = progress;
|
Chris@297
|
174
|
Chris@392
|
175 if (m_reporter) {
|
Chris@392
|
176 m_reporter->setProgress(progress);
|
Chris@297
|
177 }
|
Chris@297
|
178 }
|
Chris@297
|
179
|
Chris@297
|
180 void
|
Chris@1096
|
181 DecodingWavFileReader::getSupportedExtensions(set<QString> &extensions)
|
Chris@297
|
182 {
|
Chris@297
|
183 WavFileReader::getSupportedExtensions(extensions);
|
Chris@297
|
184 }
|
Chris@297
|
185
|
Chris@316
|
186 bool
|
Chris@823
|
187 DecodingWavFileReader::supportsExtension(QString extension)
|
Chris@316
|
188 {
|
Chris@316
|
189 return WavFileReader::supportsExtension(extension);
|
Chris@316
|
190 }
|
Chris@297
|
191
|
Chris@316
|
192 bool
|
Chris@823
|
193 DecodingWavFileReader::supportsContentType(QString type)
|
Chris@316
|
194 {
|
Chris@316
|
195 return WavFileReader::supportsContentType(type);
|
Chris@316
|
196 }
|
Chris@316
|
197
|
Chris@316
|
198 bool
|
Chris@823
|
199 DecodingWavFileReader::supports(FileSource &source)
|
Chris@316
|
200 {
|
Chris@316
|
201 return WavFileReader::supports(source);
|
Chris@316
|
202 }
|
Chris@316
|
203
|
Chris@316
|
204
|