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