comparison data/fileio/CodedAudioFileReader.cpp @ 1307:c84629395040 mp3-gapless

Handle trimming the padding from end as well
author Chris Cannam
date Tue, 29 Nov 2016 13:34:51 +0000
parents b325e91505b5
children f830a10bfbd6
comparison
equal deleted inserted replaced
1306:b325e91505b5 1307:c84629395040
39 m_fileRate(0), 39 m_fileRate(0),
40 m_cacheFileWritePtr(0), 40 m_cacheFileWritePtr(0),
41 m_cacheFileReader(0), 41 m_cacheFileReader(0),
42 m_cacheWriteBuffer(0), 42 m_cacheWriteBuffer(0),
43 m_cacheWriteBufferIndex(0), 43 m_cacheWriteBufferIndex(0),
44 m_cacheWriteBufferSize(16384), 44 m_cacheWriteBufferSize(65536),
45 m_resampler(0), 45 m_resampler(0),
46 m_resampleBuffer(0), 46 m_resampleBuffer(0),
47 m_fileFrameCount(0), 47 m_fileFrameCount(0),
48 m_normalised(normalised), 48 m_normalised(normalised),
49 m_max(0.f), 49 m_max(0.f),
94 (m_data.size() * sizeof(float)) / 1024); 94 (m_data.size() * sizeof(float)) / 1024);
95 } 95 }
96 } 96 }
97 97
98 void 98 void
99 CodedAudioFileReader::setSamplesToTrim(sv_frame_t fromStart, sv_frame_t fromEnd) 99 CodedAudioFileReader::setFramesToTrim(sv_frame_t fromStart, sv_frame_t fromEnd)
100 { 100 {
101 m_trimFromStart = fromStart; 101 m_trimFromStart = fromStart;
102 m_trimFromEnd = fromEnd; 102 m_trimFromEnd = fromEnd;
103 } 103 }
104 104
125 { 125 {
126 QMutexLocker locker(&m_cacheMutex); 126 QMutexLocker locker(&m_cacheMutex);
127 127
128 SVDEBUG << "CodedAudioFileReader::initialiseDecodeCache: file rate = " << m_fileRate << endl; 128 SVDEBUG << "CodedAudioFileReader::initialiseDecodeCache: file rate = " << m_fileRate << endl;
129 129
130 if (m_channelCount == 0) {
131 SVCERR << "CodedAudioFileReader::initialiseDecodeCache: No channel count set!" << endl;
132 throw std::logic_error("No channel count set");
133 }
134
130 if (m_fileRate == 0) { 135 if (m_fileRate == 0) {
131 SVDEBUG << "CodedAudioFileReader::initialiseDecodeCache: ERROR: File sample rate unknown (bug in subclass implementation?)" << endl; 136 SVDEBUG << "CodedAudioFileReader::initialiseDecodeCache: ERROR: File sample rate unknown (bug in subclass implementation?)" << endl;
132 throw FileOperationFailed("(coded file)", "File sample rate unknown (bug in subclass implementation?)"); 137 throw FileOperationFailed("(coded file)", "File sample rate unknown (bug in subclass implementation?)");
133 } 138 }
134 if (m_sampleRate == 0) { 139 if (m_sampleRate == 0) {
219 224
220 if (m_cacheMode == CacheInMemory) { 225 if (m_cacheMode == CacheInMemory) {
221 m_data.clear(); 226 m_data.clear();
222 } 227 }
223 228
229 if (m_trimFromEnd >= (m_cacheWriteBufferSize * m_channelCount)) {
230 SVCERR << "WARNING: CodedAudioFileReader::setSamplesToTrim: Can't handle trimming more frames from end (" << m_trimFromEnd << ") than can be stored in cache-write buffer (" << (m_cacheWriteBufferSize * m_channelCount) << "), won't trim anything from the end after all";
231 m_trimFromEnd = 0;
232 }
233
224 m_initialised = true; 234 m_initialised = true;
225 } 235 }
226 236
227 void 237 void
228 CodedAudioFileReader::addSamplesToDecodeCache(float **samples, sv_frame_t nframes) 238 CodedAudioFileReader::addSamplesToDecodeCache(float **samples, sv_frame_t nframes)
349 CodedAudioFileReader::pushCacheWriteBufferMaybe(bool final) 359 CodedAudioFileReader::pushCacheWriteBufferMaybe(bool final)
350 { 360 {
351 if (final || 361 if (final ||
352 (m_cacheWriteBufferIndex == 362 (m_cacheWriteBufferIndex ==
353 m_cacheWriteBufferSize * m_channelCount)) { 363 m_cacheWriteBufferSize * m_channelCount)) {
364
365 if (m_trimFromEnd > 0) {
354 366
355 pushBuffer(m_cacheWriteBuffer, 367 sv_frame_t framesToPush =
356 m_cacheWriteBufferIndex / m_channelCount, 368 (m_cacheWriteBufferIndex / m_channelCount) - m_trimFromEnd;
357 final); 369
358 370 if (framesToPush <= 0 && !final) {
359 m_cacheWriteBufferIndex = 0; 371 // This won't do, the buffer is full so we have to push
372 // something. Should have checked for this earlier
373 throw std::logic_error("Buffer full but nothing to push");
374 }
375
376 pushBuffer(m_cacheWriteBuffer, framesToPush, final);
377
378 m_cacheWriteBufferIndex -= framesToPush * m_channelCount;
379
380 for (sv_frame_t i = 0; i < m_cacheWriteBufferIndex; ++i) {
381 m_cacheWriteBuffer[i] =
382 m_cacheWriteBuffer[framesToPush * m_channelCount + i];
383 }
384
385 } else {
386
387 pushBuffer(m_cacheWriteBuffer,
388 m_cacheWriteBufferIndex / m_channelCount,
389 final);
390
391 m_cacheWriteBufferIndex = 0;
392 }
360 393
361 if (m_cacheFileReader) { 394 if (m_cacheFileReader) {
362 m_cacheFileReader->updateFrameCount(); 395 m_cacheFileReader->updateFrameCount();
363 } 396 }
364 } 397 }
464 padFrames = m_fileFrameCount - sv_frame_t(double(m_frameCount) / ratio) + 1; 497 padFrames = m_fileFrameCount - sv_frame_t(double(m_frameCount) / ratio) + 1;
465 } 498 }
466 499
467 sv_frame_t padSamples = padFrames * m_channelCount; 500 sv_frame_t padSamples = padFrames * m_channelCount;
468 501
469 SVDEBUG << "pushBufferResampling: frameCount = " << m_frameCount << ", equivFileFrames = " << double(m_frameCount) / ratio << ", m_fileFrameCount = " << m_fileFrameCount << ", padFrames = " << padFrames << ", padSamples = " << padSamples << endl; 502 SVDEBUG << "CodedAudioFileReader::pushBufferResampling: frameCount = " << m_frameCount << ", equivFileFrames = " << double(m_frameCount) / ratio << ", m_fileFrameCount = " << m_fileFrameCount << ", padFrames = " << padFrames << ", padSamples = " << padSamples << endl;
470 503
471 float *padding = new float[padSamples]; 504 float *padding = new float[padSamples];
472 for (sv_frame_t i = 0; i < padSamples; ++i) padding[i] = 0.f; 505 for (sv_frame_t i = 0; i < padSamples; ++i) padding[i] = 0.f;
473 506
474 sv_frame_t out = m_resampler->resampleInterleaved 507 sv_frame_t out = m_resampler->resampleInterleaved