Chris@148
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@148
|
2
|
Chris@148
|
3 /*
|
Chris@148
|
4 Sonic Visualiser
|
Chris@148
|
5 An audio file viewer and annotation editor.
|
Chris@148
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@297
|
7 This file copyright 2006-2007 Chris Cannam and QMUL.
|
Chris@148
|
8
|
Chris@148
|
9 This program is free software; you can redistribute it and/or
|
Chris@148
|
10 modify it under the terms of the GNU General Public License as
|
Chris@148
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@148
|
12 License, or (at your option) any later version. See the file
|
Chris@148
|
13 COPYING included with this distribution for more information.
|
Chris@148
|
14 */
|
Chris@148
|
15
|
Chris@148
|
16 #include "CodedAudioFileReader.h"
|
Chris@148
|
17
|
Chris@148
|
18 #include "WavFileReader.h"
|
Chris@148
|
19 #include "base/TempDirectory.h"
|
Chris@148
|
20 #include "base/Exceptions.h"
|
Chris@192
|
21 #include "base/Profiler.h"
|
Chris@297
|
22 #include "base/Serialiser.h"
|
Chris@1098
|
23 #include "base/StorageAdviser.h"
|
Chris@148
|
24
|
Chris@1318
|
25 #include <bqresample/Resampler.h>
|
Chris@1318
|
26
|
Chris@723
|
27 #include <stdint.h>
|
Chris@148
|
28 #include <iostream>
|
Chris@148
|
29 #include <QDir>
|
Chris@263
|
30 #include <QMutexLocker>
|
Chris@148
|
31
|
Chris@1096
|
32 using namespace std;
|
Chris@1096
|
33
|
Chris@297
|
34 CodedAudioFileReader::CodedAudioFileReader(CacheMode cacheMode,
|
Chris@1040
|
35 sv_samplerate_t targetRate,
|
Chris@920
|
36 bool normalised) :
|
Chris@148
|
37 m_cacheMode(cacheMode),
|
Chris@148
|
38 m_initialised(false),
|
Chris@297
|
39 m_serialiser(0),
|
Chris@297
|
40 m_fileRate(0),
|
Chris@148
|
41 m_cacheFileWritePtr(0),
|
Chris@148
|
42 m_cacheFileReader(0),
|
Chris@148
|
43 m_cacheWriteBuffer(0),
|
Chris@148
|
44 m_cacheWriteBufferIndex(0),
|
Chris@1320
|
45 m_cacheWriteBufferFrames(65536),
|
Chris@297
|
46 m_resampler(0),
|
Chris@757
|
47 m_resampleBuffer(0),
|
Chris@1320
|
48 m_resampleBufferFrames(0),
|
Chris@920
|
49 m_fileFrameCount(0),
|
Chris@920
|
50 m_normalised(normalised),
|
Chris@920
|
51 m_max(0.f),
|
Chris@1285
|
52 m_gain(1.f),
|
Chris@1305
|
53 m_trimFromStart(0),
|
Chris@1305
|
54 m_trimFromEnd(0),
|
Chris@1285
|
55 m_clippedCount(0),
|
Chris@1286
|
56 m_firstNonzero(0),
|
Chris@1286
|
57 m_lastNonzero(0)
|
Chris@148
|
58 {
|
Chris@1279
|
59 SVDEBUG << "CodedAudioFileReader:: cache mode: " << cacheMode
|
Chris@1279
|
60 << " (" << (cacheMode == CacheInTemporaryFile
|
Chris@1279
|
61 ? "CacheInTemporaryFile" : "CacheInMemory") << ")"
|
Chris@1279
|
62 << ", rate: " << targetRate
|
Chris@1279
|
63 << (targetRate == 0 ? " (use source rate)" : "")
|
Chris@1279
|
64 << ", normalised: " << normalised << endl;
|
Chris@297
|
65
|
Chris@297
|
66 m_frameCount = 0;
|
Chris@297
|
67 m_sampleRate = targetRate;
|
Chris@148
|
68 }
|
Chris@148
|
69
|
Chris@148
|
70 CodedAudioFileReader::~CodedAudioFileReader()
|
Chris@148
|
71 {
|
Chris@263
|
72 QMutexLocker locker(&m_cacheMutex);
|
Chris@263
|
73
|
Chris@1279
|
74 if (m_serialiser) endSerialised();
|
Chris@1098
|
75
|
Chris@148
|
76 if (m_cacheFileWritePtr) sf_close(m_cacheFileWritePtr);
|
Chris@297
|
77
|
Chris@742
|
78 SVDEBUG << "CodedAudioFileReader::~CodedAudioFileReader: deleting cache file reader" << endl;
|
Chris@532
|
79
|
Chris@297
|
80 delete m_cacheFileReader;
|
Chris@297
|
81 delete[] m_cacheWriteBuffer;
|
Chris@1279
|
82
|
Chris@148
|
83 if (m_cacheFileName != "") {
|
Chris@1279
|
84 SVDEBUG << "CodedAudioFileReader::~CodedAudioFileReader: deleting cache file " << m_cacheFileName << endl;
|
Chris@290
|
85 if (!QFile(m_cacheFileName).remove()) {
|
Chris@1279
|
86 SVDEBUG << "WARNING: CodedAudioFileReader::~CodedAudioFileReader: Failed to delete cache file \"" << m_cacheFileName << "\"" << endl;
|
Chris@148
|
87 }
|
Chris@148
|
88 }
|
Chris@297
|
89
|
Chris@297
|
90 delete m_resampler;
|
Chris@297
|
91 delete[] m_resampleBuffer;
|
Chris@1098
|
92
|
Chris@1098
|
93 if (!m_data.empty()) {
|
Chris@1098
|
94 StorageAdviser::notifyDoneAllocation
|
Chris@1098
|
95 (StorageAdviser::MemoryAllocation,
|
Chris@1098
|
96 (m_data.size() * sizeof(float)) / 1024);
|
Chris@1098
|
97 }
|
Chris@297
|
98 }
|
Chris@297
|
99
|
Chris@297
|
100 void
|
Chris@1307
|
101 CodedAudioFileReader::setFramesToTrim(sv_frame_t fromStart, sv_frame_t fromEnd)
|
Chris@1305
|
102 {
|
Chris@1305
|
103 m_trimFromStart = fromStart;
|
Chris@1305
|
104 m_trimFromEnd = fromEnd;
|
Chris@1305
|
105 }
|
Chris@1305
|
106
|
Chris@1305
|
107 void
|
Chris@297
|
108 CodedAudioFileReader::startSerialised(QString id)
|
Chris@297
|
109 {
|
Chris@1279
|
110 SVDEBUG << "CodedAudioFileReader(" << this << ")::startSerialised: id = " << id << endl;
|
Chris@297
|
111
|
Chris@297
|
112 delete m_serialiser;
|
Chris@297
|
113 m_serialiser = new Serialiser(id);
|
Chris@297
|
114 }
|
Chris@297
|
115
|
Chris@297
|
116 void
|
Chris@297
|
117 CodedAudioFileReader::endSerialised()
|
Chris@297
|
118 {
|
Chris@844
|
119 SVDEBUG << "CodedAudioFileReader(" << this << ")::endSerialised: id = " << (m_serialiser ? m_serialiser->getId() : "(none)") << endl;
|
Chris@297
|
120
|
Chris@297
|
121 delete m_serialiser;
|
Chris@297
|
122 m_serialiser = 0;
|
Chris@148
|
123 }
|
Chris@148
|
124
|
Chris@148
|
125 void
|
Chris@148
|
126 CodedAudioFileReader::initialiseDecodeCache()
|
Chris@148
|
127 {
|
Chris@263
|
128 QMutexLocker locker(&m_cacheMutex);
|
Chris@263
|
129
|
Chris@742
|
130 SVDEBUG << "CodedAudioFileReader::initialiseDecodeCache: file rate = " << m_fileRate << endl;
|
Chris@297
|
131
|
Chris@1307
|
132 if (m_channelCount == 0) {
|
Chris@1307
|
133 SVCERR << "CodedAudioFileReader::initialiseDecodeCache: No channel count set!" << endl;
|
Chris@1307
|
134 throw std::logic_error("No channel count set");
|
Chris@1307
|
135 }
|
Chris@1307
|
136
|
Chris@297
|
137 if (m_fileRate == 0) {
|
Chris@1279
|
138 SVDEBUG << "CodedAudioFileReader::initialiseDecodeCache: ERROR: File sample rate unknown (bug in subclass implementation?)" << endl;
|
Chris@754
|
139 throw FileOperationFailed("(coded file)", "File sample rate unknown (bug in subclass implementation?)");
|
Chris@297
|
140 }
|
Chris@297
|
141 if (m_sampleRate == 0) {
|
Chris@297
|
142 m_sampleRate = m_fileRate;
|
Chris@690
|
143 SVDEBUG << "CodedAudioFileReader::initialiseDecodeCache: rate (from file) = " << m_fileRate << endl;
|
Chris@297
|
144 }
|
Chris@297
|
145 if (m_fileRate != m_sampleRate) {
|
Chris@757
|
146 SVDEBUG << "CodedAudioFileReader: resampling " << m_fileRate << " -> " << m_sampleRate << endl;
|
Chris@1318
|
147 m_resampler = new breakfastquay::Resampler
|
Chris@1318
|
148 (breakfastquay::Resampler::FastestTolerable,
|
Chris@1318
|
149 m_channelCount,
|
Chris@1320
|
150 int(m_cacheWriteBufferFrames));
|
Chris@1040
|
151 double ratio = m_sampleRate / m_fileRate;
|
Chris@1320
|
152 m_resampleBufferFrames = int(ceil(double(m_cacheWriteBufferFrames) *
|
Chris@1320
|
153 ratio + 1));
|
Chris@1320
|
154 m_resampleBuffer = new float[m_resampleBufferFrames * m_channelCount];
|
Chris@297
|
155 }
|
Chris@297
|
156
|
Chris@1320
|
157 m_cacheWriteBuffer = new float[m_cacheWriteBufferFrames * m_channelCount];
|
Chris@297
|
158 m_cacheWriteBufferIndex = 0;
|
Chris@297
|
159
|
Chris@148
|
160 if (m_cacheMode == CacheInTemporaryFile) {
|
Chris@148
|
161
|
Chris@148
|
162 try {
|
Chris@148
|
163 QDir dir(TempDirectory::getInstance()->getPath());
|
Chris@148
|
164 m_cacheFileName = dir.filePath(QString("decoded_%1.wav")
|
Chris@290
|
165 .arg((intptr_t)this));
|
Chris@148
|
166
|
Chris@148
|
167 SF_INFO fileInfo;
|
Chris@1040
|
168 int fileRate = int(round(m_sampleRate));
|
Chris@1040
|
169 if (m_sampleRate != sv_samplerate_t(fileRate)) {
|
Chris@1279
|
170 SVDEBUG << "CodedAudioFileReader: WARNING: Non-integer sample rate "
|
Chris@1040
|
171 << m_sampleRate << " presented for writing, rounding to " << fileRate
|
Chris@1040
|
172 << endl;
|
Chris@1040
|
173 }
|
Chris@1040
|
174 fileInfo.samplerate = fileRate;
|
Chris@148
|
175 fileInfo.channels = m_channelCount;
|
Chris@1161
|
176
|
Chris@1161
|
177 // Previously we were writing SF_FORMAT_PCM_16 and in a
|
Chris@1161
|
178 // comment I wrote: "No point in writing 24-bit or float;
|
Chris@1161
|
179 // generally this class is used for decoding files that
|
Chris@1161
|
180 // have come from a 16 bit source or that decode to only
|
Chris@1161
|
181 // 16 bits anyway." That was naive -- we want to preserve
|
Chris@1161
|
182 // the original values to the same float precision that we
|
Chris@1161
|
183 // use internally. Saving PCM_16 obviously doesn't
|
Chris@1161
|
184 // preserve values for sources at bit depths greater than
|
Chris@1161
|
185 // 16, but it also doesn't always do so for sources at bit
|
Chris@1161
|
186 // depths less than 16.
|
Chris@1161
|
187 //
|
Chris@1161
|
188 // (This came to light with a bug in libsndfile 1.0.26,
|
Chris@1161
|
189 // which always reports every file as non-seekable, so
|
Chris@1161
|
190 // that coded readers were being used even for WAV
|
Chris@1161
|
191 // files. This changed the values that came from PCM_8 WAV
|
Chris@1161
|
192 // sources, breaking Sonic Annotator's output comparison
|
Chris@1161
|
193 // tests.)
|
Chris@1161
|
194 //
|
Chris@1161
|
195 // So: now we write floats.
|
Chris@1161
|
196 fileInfo.format = SF_FORMAT_WAV | SF_FORMAT_FLOAT;
|
Chris@148
|
197
|
Chris@290
|
198 m_cacheFileWritePtr = sf_open(m_cacheFileName.toLocal8Bit(),
|
Chris@148
|
199 SFM_WRITE, &fileInfo);
|
Chris@148
|
200
|
Chris@265
|
201 if (m_cacheFileWritePtr) {
|
Chris@265
|
202
|
Chris@297
|
203 // Ideally we would do this now only if we were in a
|
Chris@297
|
204 // threaded mode -- creating the reader later if we're
|
Chris@297
|
205 // not threaded -- but we don't have access to that
|
Chris@297
|
206 // information here
|
Chris@265
|
207
|
Chris@265
|
208 m_cacheFileReader = new WavFileReader(m_cacheFileName);
|
Chris@265
|
209
|
Chris@265
|
210 if (!m_cacheFileReader->isOK()) {
|
Chris@1279
|
211 SVDEBUG << "ERROR: CodedAudioFileReader::initialiseDecodeCache: Failed to construct WAV file reader for temporary file: " << m_cacheFileReader->getError() << endl;
|
Chris@265
|
212 delete m_cacheFileReader;
|
Chris@265
|
213 m_cacheFileReader = 0;
|
Chris@265
|
214 m_cacheMode = CacheInMemory;
|
Chris@265
|
215 sf_close(m_cacheFileWritePtr);
|
Chris@265
|
216 }
|
Chris@297
|
217
|
Chris@265
|
218 } else {
|
Chris@1279
|
219 SVDEBUG << "CodedAudioFileReader::initialiseDecodeCache: failed to open cache file \"" << m_cacheFileName << "\" (" << m_channelCount << " channels, sample rate " << m_sampleRate << " for writing, falling back to in-memory cache" << endl;
|
Chris@148
|
220 m_cacheMode = CacheInMemory;
|
Chris@148
|
221 }
|
Chris@265
|
222
|
Chris@148
|
223 } catch (DirectoryCreationFailed f) {
|
Chris@1279
|
224 SVDEBUG << "CodedAudioFileReader::initialiseDecodeCache: failed to create temporary directory! Falling back to in-memory cache" << endl;
|
Chris@148
|
225 m_cacheMode = CacheInMemory;
|
Chris@148
|
226 }
|
Chris@148
|
227 }
|
Chris@148
|
228
|
Chris@148
|
229 if (m_cacheMode == CacheInMemory) {
|
Chris@148
|
230 m_data.clear();
|
Chris@148
|
231 }
|
Chris@148
|
232
|
Chris@1320
|
233 if (m_trimFromEnd >= (m_cacheWriteBufferFrames * m_channelCount)) {
|
Chris@1320
|
234 SVCERR << "WARNING: CodedAudioFileReader::setSamplesToTrim: Can't handle trimming more frames from end (" << m_trimFromEnd << ") than can be stored in cache-write buffer (" << (m_cacheWriteBufferFrames * m_channelCount) << "), won't trim anything from the end after all";
|
Chris@1307
|
235 m_trimFromEnd = 0;
|
Chris@1307
|
236 }
|
Chris@1307
|
237
|
Chris@148
|
238 m_initialised = true;
|
Chris@148
|
239 }
|
Chris@148
|
240
|
Chris@148
|
241 void
|
Chris@1038
|
242 CodedAudioFileReader::addSamplesToDecodeCache(float **samples, sv_frame_t nframes)
|
Chris@148
|
243 {
|
Chris@263
|
244 QMutexLocker locker(&m_cacheMutex);
|
Chris@263
|
245
|
Chris@148
|
246 if (!m_initialised) return;
|
Chris@148
|
247
|
Chris@1038
|
248 for (sv_frame_t i = 0; i < nframes; ++i) {
|
Chris@1305
|
249
|
Chris@1305
|
250 if (m_trimFromStart > 0) {
|
Chris@1305
|
251 --m_trimFromStart;
|
Chris@1305
|
252 continue;
|
Chris@1305
|
253 }
|
Chris@297
|
254
|
Chris@929
|
255 for (int c = 0; c < m_channelCount; ++c) {
|
Chris@148
|
256
|
Chris@297
|
257 float sample = samples[c][i];
|
Chris@297
|
258 m_cacheWriteBuffer[m_cacheWriteBufferIndex++] = sample;
|
Chris@148
|
259
|
Chris@1306
|
260 }
|
Chris@297
|
261
|
Chris@1306
|
262 pushCacheWriteBufferMaybe(false);
|
Chris@297
|
263 }
|
Chris@297
|
264 }
|
Chris@297
|
265
|
Chris@297
|
266 void
|
Chris@1038
|
267 CodedAudioFileReader::addSamplesToDecodeCache(float *samples, sv_frame_t nframes)
|
Chris@297
|
268 {
|
Chris@297
|
269 QMutexLocker locker(&m_cacheMutex);
|
Chris@297
|
270
|
Chris@297
|
271 if (!m_initialised) return;
|
Chris@297
|
272
|
Chris@1038
|
273 for (sv_frame_t i = 0; i < nframes; ++i) {
|
Chris@1305
|
274
|
Chris@1305
|
275 if (m_trimFromStart > 0) {
|
Chris@1305
|
276 --m_trimFromStart;
|
Chris@1305
|
277 continue;
|
Chris@1305
|
278 }
|
Chris@297
|
279
|
Chris@929
|
280 for (int c = 0; c < m_channelCount; ++c) {
|
Chris@297
|
281
|
Chris@297
|
282 float sample = samples[i * m_channelCount + c];
|
Chris@297
|
283
|
Chris@297
|
284 m_cacheWriteBuffer[m_cacheWriteBufferIndex++] = sample;
|
Chris@1306
|
285 }
|
Chris@297
|
286
|
Chris@1306
|
287 pushCacheWriteBufferMaybe(false);
|
Chris@297
|
288 }
|
Chris@297
|
289 }
|
Chris@297
|
290
|
Chris@297
|
291 void
|
Chris@1096
|
292 CodedAudioFileReader::addSamplesToDecodeCache(const vector<float> &samples)
|
Chris@297
|
293 {
|
Chris@297
|
294 QMutexLocker locker(&m_cacheMutex);
|
Chris@297
|
295
|
Chris@297
|
296 if (!m_initialised) return;
|
Chris@297
|
297
|
Chris@1038
|
298 for (float sample: samples) {
|
Chris@1305
|
299
|
Chris@1305
|
300 if (m_trimFromStart > 0) {
|
Chris@1305
|
301 --m_trimFromStart;
|
Chris@1305
|
302 continue;
|
Chris@1305
|
303 }
|
Chris@297
|
304
|
Chris@148
|
305 m_cacheWriteBuffer[m_cacheWriteBufferIndex++] = sample;
|
Chris@148
|
306
|
Chris@1306
|
307 pushCacheWriteBufferMaybe(false);
|
Chris@148
|
308 }
|
Chris@148
|
309 }
|
Chris@148
|
310
|
Chris@148
|
311 void
|
Chris@148
|
312 CodedAudioFileReader::finishDecodeCache()
|
Chris@148
|
313 {
|
Chris@263
|
314 QMutexLocker locker(&m_cacheMutex);
|
Chris@263
|
315
|
Chris@1295
|
316 Profiler profiler("CodedAudioFileReader::finishDecodeCache");
|
Chris@192
|
317
|
Chris@148
|
318 if (!m_initialised) {
|
Chris@1279
|
319 SVDEBUG << "WARNING: CodedAudioFileReader::finishDecodeCache: Cache was never initialised!" << endl;
|
Chris@148
|
320 return;
|
Chris@148
|
321 }
|
Chris@148
|
322
|
Chris@1306
|
323 pushCacheWriteBufferMaybe(true);
|
Chris@297
|
324
|
Chris@297
|
325 delete[] m_cacheWriteBuffer;
|
Chris@297
|
326 m_cacheWriteBuffer = 0;
|
Chris@297
|
327
|
Chris@297
|
328 delete[] m_resampleBuffer;
|
Chris@297
|
329 m_resampleBuffer = 0;
|
Chris@297
|
330
|
Chris@297
|
331 delete m_resampler;
|
Chris@297
|
332 m_resampler = 0;
|
Chris@297
|
333
|
Chris@297
|
334 if (m_cacheMode == CacheInTemporaryFile) {
|
Chris@1098
|
335
|
Chris@297
|
336 sf_close(m_cacheFileWritePtr);
|
Chris@297
|
337 m_cacheFileWritePtr = 0;
|
Chris@297
|
338 if (m_cacheFileReader) m_cacheFileReader->updateFrameCount();
|
Chris@1098
|
339
|
Chris@1098
|
340 } else {
|
Chris@1098
|
341 // I know, I know, we already allocated it...
|
Chris@1098
|
342 StorageAdviser::notifyPlannedAllocation
|
Chris@1098
|
343 (StorageAdviser::MemoryAllocation,
|
Chris@1098
|
344 (m_data.size() * sizeof(float)) / 1024);
|
Chris@297
|
345 }
|
Chris@1285
|
346
|
Chris@1285
|
347 SVDEBUG << "CodedAudioFileReader: File decodes to " << m_fileFrameCount
|
Chris@1285
|
348 << " frames" << endl;
|
Chris@1285
|
349 if (m_fileFrameCount != m_frameCount) {
|
Chris@1285
|
350 SVDEBUG << "CodedAudioFileReader: Resampled to " << m_frameCount
|
Chris@1285
|
351 << " frames" << endl;
|
Chris@1285
|
352 }
|
Chris@1285
|
353 SVDEBUG << "CodedAudioFileReader: Signal abs max is " << m_max
|
Chris@1285
|
354 << ", " << m_clippedCount
|
Chris@1285
|
355 << " samples clipped, first non-zero frame is at "
|
Chris@1286
|
356 << m_firstNonzero << ", last at " << m_lastNonzero << endl;
|
Chris@1285
|
357 if (m_normalised) {
|
Chris@1285
|
358 SVDEBUG << "CodedAudioFileReader: Normalising, gain is " << m_gain << endl;
|
Chris@1285
|
359 }
|
Chris@297
|
360 }
|
Chris@297
|
361
|
Chris@297
|
362 void
|
Chris@1306
|
363 CodedAudioFileReader::pushCacheWriteBufferMaybe(bool final)
|
Chris@1306
|
364 {
|
Chris@1306
|
365 if (final ||
|
Chris@1306
|
366 (m_cacheWriteBufferIndex ==
|
Chris@1320
|
367 m_cacheWriteBufferFrames * m_channelCount)) {
|
Chris@1307
|
368
|
Chris@1307
|
369 if (m_trimFromEnd > 0) {
|
Chris@1306
|
370
|
Chris@1307
|
371 sv_frame_t framesToPush =
|
Chris@1307
|
372 (m_cacheWriteBufferIndex / m_channelCount) - m_trimFromEnd;
|
Chris@1307
|
373
|
Chris@1307
|
374 if (framesToPush <= 0 && !final) {
|
Chris@1307
|
375 // This won't do, the buffer is full so we have to push
|
Chris@1307
|
376 // something. Should have checked for this earlier
|
Chris@1307
|
377 throw std::logic_error("Buffer full but nothing to push");
|
Chris@1307
|
378 }
|
Chris@1307
|
379
|
Chris@1307
|
380 pushBuffer(m_cacheWriteBuffer, framesToPush, final);
|
Chris@1307
|
381
|
Chris@1307
|
382 m_cacheWriteBufferIndex -= framesToPush * m_channelCount;
|
Chris@1307
|
383
|
Chris@1307
|
384 for (sv_frame_t i = 0; i < m_cacheWriteBufferIndex; ++i) {
|
Chris@1307
|
385 m_cacheWriteBuffer[i] =
|
Chris@1307
|
386 m_cacheWriteBuffer[framesToPush * m_channelCount + i];
|
Chris@1307
|
387 }
|
Chris@1307
|
388
|
Chris@1307
|
389 } else {
|
Chris@1307
|
390
|
Chris@1307
|
391 pushBuffer(m_cacheWriteBuffer,
|
Chris@1307
|
392 m_cacheWriteBufferIndex / m_channelCount,
|
Chris@1307
|
393 final);
|
Chris@1307
|
394
|
Chris@1307
|
395 m_cacheWriteBufferIndex = 0;
|
Chris@1307
|
396 }
|
Chris@1306
|
397
|
Chris@1306
|
398 if (m_cacheFileReader) {
|
Chris@1306
|
399 m_cacheFileReader->updateFrameCount();
|
Chris@1306
|
400 }
|
Chris@1306
|
401 }
|
Chris@1306
|
402 }
|
Chris@1306
|
403
|
Chris@1306
|
404 sv_frame_t
|
Chris@1038
|
405 CodedAudioFileReader::pushBuffer(float *buffer, sv_frame_t sz, bool final)
|
Chris@297
|
406 {
|
Chris@757
|
407 m_fileFrameCount += sz;
|
Chris@757
|
408
|
Chris@1040
|
409 double ratio = 1.0;
|
Chris@758
|
410 if (m_resampler && m_fileRate != 0) {
|
Chris@1040
|
411 ratio = m_sampleRate / m_fileRate;
|
Chris@758
|
412 }
|
Chris@758
|
413
|
Chris@1040
|
414 if (ratio != 1.0) {
|
Chris@758
|
415 pushBufferResampling(buffer, sz, ratio, final);
|
Chris@758
|
416 } else {
|
Chris@758
|
417 pushBufferNonResampling(buffer, sz);
|
Chris@758
|
418 }
|
Chris@1306
|
419
|
Chris@1306
|
420 return sz;
|
Chris@758
|
421 }
|
Chris@757
|
422
|
Chris@758
|
423 void
|
Chris@1038
|
424 CodedAudioFileReader::pushBufferNonResampling(float *buffer, sv_frame_t sz)
|
Chris@758
|
425 {
|
Chris@920
|
426 float clip = 1.0;
|
Chris@1038
|
427 sv_frame_t count = sz * m_channelCount;
|
Chris@318
|
428
|
Chris@1305
|
429 // statistics
|
Chris@1286
|
430 for (sv_frame_t j = 0; j < sz; ++j) {
|
Chris@1286
|
431 for (int c = 0; c < m_channelCount; ++c) {
|
Chris@1286
|
432 sv_frame_t i = j * m_channelCount + c;
|
Chris@1286
|
433 float v = buffer[i];
|
Chris@1286
|
434 if (!m_normalised) {
|
Chris@1286
|
435 if (v > clip) {
|
Chris@1286
|
436 buffer[i] = clip;
|
Chris@1286
|
437 ++m_clippedCount;
|
Chris@1286
|
438 } else if (v < -clip) {
|
Chris@1286
|
439 buffer[i] = -clip;
|
Chris@1286
|
440 ++m_clippedCount;
|
Chris@1286
|
441 }
|
Chris@1285
|
442 }
|
Chris@1286
|
443 v = fabsf(v);
|
Chris@1286
|
444 if (v != 0.f) {
|
Chris@1286
|
445 if (m_firstNonzero == 0) {
|
Chris@1286
|
446 m_firstNonzero = m_frameCount;
|
Chris@1286
|
447 }
|
Chris@1286
|
448 m_lastNonzero = m_frameCount;
|
Chris@1286
|
449 if (v > m_max) {
|
Chris@1286
|
450 m_max = v;
|
Chris@1286
|
451 }
|
Chris@920
|
452 }
|
Chris@920
|
453 }
|
Chris@1286
|
454 ++m_frameCount;
|
Chris@297
|
455 }
|
Chris@297
|
456
|
Chris@1286
|
457 if (m_max > 0.f) {
|
Chris@1286
|
458 m_gain = 1.f / m_max; // used when normalising only
|
Chris@1286
|
459 }
|
Chris@297
|
460
|
Chris@148
|
461 switch (m_cacheMode) {
|
Chris@148
|
462
|
Chris@148
|
463 case CacheInTemporaryFile:
|
Chris@1038
|
464 if (sf_writef_float(m_cacheFileWritePtr, buffer, sz) < sz) {
|
Chris@544
|
465 sf_close(m_cacheFileWritePtr);
|
Chris@544
|
466 m_cacheFileWritePtr = 0;
|
Chris@544
|
467 throw InsufficientDiscSpace(TempDirectory::getInstance()->getPath());
|
Chris@544
|
468 }
|
Chris@148
|
469 break;
|
Chris@148
|
470
|
Chris@148
|
471 case CacheInMemory:
|
Chris@1100
|
472 m_dataLock.lock();
|
Chris@1096
|
473 m_data.insert(m_data.end(), buffer, buffer + count);
|
Chris@543
|
474 m_dataLock.unlock();
|
Chris@148
|
475 break;
|
Chris@148
|
476 }
|
Chris@758
|
477 }
|
Chris@757
|
478
|
Chris@758
|
479 void
|
Chris@1038
|
480 CodedAudioFileReader::pushBufferResampling(float *buffer, sv_frame_t sz,
|
Chris@1038
|
481 double ratio, bool final)
|
Chris@758
|
482 {
|
Chris@1306
|
483 // SVDEBUG << "pushBufferResampling: ratio = " << ratio << ", sz = " << sz << ", final = " << final << endl;
|
Chris@757
|
484
|
Chris@759
|
485 if (sz > 0) {
|
Chris@759
|
486
|
Chris@1038
|
487 sv_frame_t out = m_resampler->resampleInterleaved
|
Chris@1320
|
488 (m_resampleBuffer,
|
Chris@1320
|
489 m_resampleBufferFrames,
|
Chris@1320
|
490 buffer,
|
Chris@1323
|
491 int(sz),
|
Chris@759
|
492 ratio,
|
Chris@759
|
493 false);
|
Chris@759
|
494
|
Chris@759
|
495 pushBufferNonResampling(m_resampleBuffer, out);
|
Chris@759
|
496 }
|
Chris@757
|
497
|
Chris@758
|
498 if (final) {
|
Chris@758
|
499
|
Chris@1038
|
500 sv_frame_t padFrames = 1;
|
Chris@1038
|
501 if (double(m_frameCount) / ratio < double(m_fileFrameCount)) {
|
Chris@1038
|
502 padFrames = m_fileFrameCount - sv_frame_t(double(m_frameCount) / ratio) + 1;
|
Chris@757
|
503 }
|
Chris@758
|
504
|
Chris@1038
|
505 sv_frame_t padSamples = padFrames * m_channelCount;
|
Chris@758
|
506
|
Chris@1307
|
507 SVDEBUG << "CodedAudioFileReader::pushBufferResampling: frameCount = " << m_frameCount << ", equivFileFrames = " << double(m_frameCount) / ratio << ", m_fileFrameCount = " << m_fileFrameCount << ", padFrames = " << padFrames << ", padSamples = " << padSamples << endl;
|
Chris@758
|
508
|
Chris@758
|
509 float *padding = new float[padSamples];
|
Chris@1038
|
510 for (sv_frame_t i = 0; i < padSamples; ++i) padding[i] = 0.f;
|
Chris@758
|
511
|
Chris@1038
|
512 sv_frame_t out = m_resampler->resampleInterleaved
|
Chris@1320
|
513 (m_resampleBuffer,
|
Chris@1320
|
514 m_resampleBufferFrames,
|
Chris@1320
|
515 padding,
|
Chris@1323
|
516 int(padFrames),
|
Chris@758
|
517 ratio,
|
Chris@758
|
518 true);
|
Chris@758
|
519
|
Chris@1038
|
520 if (m_frameCount + out > sv_frame_t(double(m_fileFrameCount) * ratio)) {
|
Chris@1038
|
521 out = sv_frame_t(double(m_fileFrameCount) * ratio) - m_frameCount;
|
Chris@759
|
522 }
|
Chris@759
|
523
|
Chris@758
|
524 pushBufferNonResampling(m_resampleBuffer, out);
|
Chris@758
|
525 delete[] padding;
|
Chris@757
|
526 }
|
Chris@148
|
527 }
|
Chris@148
|
528
|
Chris@1096
|
529 vector<float>
|
Chris@1041
|
530 CodedAudioFileReader::getInterleavedFrames(sv_frame_t start, sv_frame_t count) const
|
Chris@148
|
531 {
|
Chris@543
|
532 // Lock is only required in CacheInMemory mode (the cache file
|
Chris@543
|
533 // reader is expected to be thread safe and manage its own
|
Chris@543
|
534 // locking)
|
Chris@263
|
535
|
Chris@265
|
536 if (!m_initialised) {
|
Chris@690
|
537 SVDEBUG << "CodedAudioFileReader::getInterleavedFrames: not initialised" << endl;
|
Chris@1096
|
538 return {};
|
Chris@265
|
539 }
|
Chris@148
|
540
|
Chris@1096
|
541 vector<float> frames;
|
Chris@1041
|
542
|
Chris@148
|
543 switch (m_cacheMode) {
|
Chris@148
|
544
|
Chris@148
|
545 case CacheInTemporaryFile:
|
Chris@148
|
546 if (m_cacheFileReader) {
|
Chris@1041
|
547 frames = m_cacheFileReader->getInterleavedFrames(start, count);
|
Chris@148
|
548 }
|
Chris@148
|
549 break;
|
Chris@148
|
550
|
Chris@148
|
551 case CacheInMemory:
|
Chris@148
|
552 {
|
Chris@1096
|
553 if (!isOK()) return {};
|
Chris@1096
|
554 if (count == 0) return {};
|
Chris@148
|
555
|
Chris@1100
|
556 sv_frame_t ix0 = start * m_channelCount;
|
Chris@1100
|
557 sv_frame_t ix1 = ix0 + (count * m_channelCount);
|
Chris@148
|
558
|
Chris@1100
|
559 // This lock used to be a QReadWriteLock, but it appears that
|
Chris@1100
|
560 // its lock mechanism is significantly slower than QMutex so
|
Chris@1100
|
561 // it's not a good idea in cases like this where we don't
|
Chris@1100
|
562 // really have threads taking a long time to read concurrently
|
Chris@1100
|
563 m_dataLock.lock();
|
Chris@1100
|
564 sv_frame_t n = sv_frame_t(m_data.size());
|
Chris@1282
|
565 if (ix0 > n) ix0 = n;
|
Chris@1100
|
566 if (ix1 > n) ix1 = n;
|
Chris@1100
|
567 frames = vector<float>(m_data.begin() + ix0, m_data.begin() + ix1);
|
Chris@543
|
568 m_dataLock.unlock();
|
Chris@1282
|
569 break;
|
Chris@148
|
570 }
|
Chris@148
|
571 }
|
Chris@920
|
572
|
Chris@920
|
573 if (m_normalised) {
|
Chris@1052
|
574 for (auto &f: frames) f *= m_gain;
|
Chris@920
|
575 }
|
Chris@1041
|
576
|
Chris@1041
|
577 return frames;
|
Chris@148
|
578 }
|
Chris@148
|
579
|