comparison data/fileio/CodedAudioFileReader.cpp @ 1306:b325e91505b5 mp3-gapless

Refactor in CodedAudioFileReader in preparation for trimming at end
author Chris Cannam
date Tue, 29 Nov 2016 12:06:49 +0000
parents 9f9f55a8af92
children c84629395040
comparison
equal deleted inserted replaced
1305:9f9f55a8af92 1306:b325e91505b5
239 } 239 }
240 240
241 for (int c = 0; c < m_channelCount; ++c) { 241 for (int c = 0; c < m_channelCount; ++c) {
242 242
243 float sample = samples[c][i]; 243 float sample = samples[c][i];
244
245 m_cacheWriteBuffer[m_cacheWriteBufferIndex++] = sample; 244 m_cacheWriteBuffer[m_cacheWriteBufferIndex++] = sample;
246 245
247 if (m_cacheWriteBufferIndex == 246 }
248 m_cacheWriteBufferSize * m_channelCount) { 247
249 248 pushCacheWriteBufferMaybe(false);
250 pushBuffer(m_cacheWriteBuffer, m_cacheWriteBufferSize, false);
251 m_cacheWriteBufferIndex = 0;
252 }
253
254 if (m_cacheWriteBufferIndex % 10240 == 0 &&
255 m_cacheFileReader) {
256 m_cacheFileReader->updateFrameCount();
257 }
258 }
259 } 249 }
260 } 250 }
261 251
262 void 252 void
263 CodedAudioFileReader::addSamplesToDecodeCache(float *samples, sv_frame_t nframes) 253 CodedAudioFileReader::addSamplesToDecodeCache(float *samples, sv_frame_t nframes)
276 for (int c = 0; c < m_channelCount; ++c) { 266 for (int c = 0; c < m_channelCount; ++c) {
277 267
278 float sample = samples[i * m_channelCount + c]; 268 float sample = samples[i * m_channelCount + c];
279 269
280 m_cacheWriteBuffer[m_cacheWriteBufferIndex++] = sample; 270 m_cacheWriteBuffer[m_cacheWriteBufferIndex++] = sample;
281 271 }
282 if (m_cacheWriteBufferIndex == 272
283 m_cacheWriteBufferSize * m_channelCount) { 273 pushCacheWriteBufferMaybe(false);
284
285 pushBuffer(m_cacheWriteBuffer, m_cacheWriteBufferSize, false);
286 m_cacheWriteBufferIndex = 0;
287 }
288
289 if (m_cacheWriteBufferIndex % 10240 == 0 &&
290 m_cacheFileReader) {
291 m_cacheFileReader->updateFrameCount();
292 }
293 }
294 } 274 }
295 } 275 }
296 276
297 void 277 void
298 CodedAudioFileReader::addSamplesToDecodeCache(const vector<float> &samples) 278 CodedAudioFileReader::addSamplesToDecodeCache(const vector<float> &samples)
308 continue; 288 continue;
309 } 289 }
310 290
311 m_cacheWriteBuffer[m_cacheWriteBufferIndex++] = sample; 291 m_cacheWriteBuffer[m_cacheWriteBufferIndex++] = sample;
312 292
313 if (m_cacheWriteBufferIndex == 293 pushCacheWriteBufferMaybe(false);
314 m_cacheWriteBufferSize * m_channelCount) {
315
316 pushBuffer(m_cacheWriteBuffer, m_cacheWriteBufferSize, false);
317 m_cacheWriteBufferIndex = 0;
318 }
319
320 if (m_cacheWriteBufferIndex % 10240 == 0 &&
321 m_cacheFileReader) {
322 m_cacheFileReader->updateFrameCount();
323 }
324 } 294 }
325 } 295 }
326 296
327 void 297 void
328 CodedAudioFileReader::finishDecodeCache() 298 CodedAudioFileReader::finishDecodeCache()
334 if (!m_initialised) { 304 if (!m_initialised) {
335 SVDEBUG << "WARNING: CodedAudioFileReader::finishDecodeCache: Cache was never initialised!" << endl; 305 SVDEBUG << "WARNING: CodedAudioFileReader::finishDecodeCache: Cache was never initialised!" << endl;
336 return; 306 return;
337 } 307 }
338 308
339 pushBuffer(m_cacheWriteBuffer, 309 pushCacheWriteBufferMaybe(true);
340 m_cacheWriteBufferIndex / m_channelCount,
341 true);
342 310
343 delete[] m_cacheWriteBuffer; 311 delete[] m_cacheWriteBuffer;
344 m_cacheWriteBuffer = 0; 312 m_cacheWriteBuffer = 0;
345 313
346 delete[] m_resampleBuffer; 314 delete[] m_resampleBuffer;
376 SVDEBUG << "CodedAudioFileReader: Normalising, gain is " << m_gain << endl; 344 SVDEBUG << "CodedAudioFileReader: Normalising, gain is " << m_gain << endl;
377 } 345 }
378 } 346 }
379 347
380 void 348 void
349 CodedAudioFileReader::pushCacheWriteBufferMaybe(bool final)
350 {
351 if (final ||
352 (m_cacheWriteBufferIndex ==
353 m_cacheWriteBufferSize * m_channelCount)) {
354
355 pushBuffer(m_cacheWriteBuffer,
356 m_cacheWriteBufferIndex / m_channelCount,
357 final);
358
359 m_cacheWriteBufferIndex = 0;
360
361 if (m_cacheFileReader) {
362 m_cacheFileReader->updateFrameCount();
363 }
364 }
365 }
366
367 sv_frame_t
381 CodedAudioFileReader::pushBuffer(float *buffer, sv_frame_t sz, bool final) 368 CodedAudioFileReader::pushBuffer(float *buffer, sv_frame_t sz, bool final)
382 { 369 {
383 m_fileFrameCount += sz; 370 m_fileFrameCount += sz;
384 371
385 double ratio = 1.0; 372 double ratio = 1.0;
390 if (ratio != 1.0) { 377 if (ratio != 1.0) {
391 pushBufferResampling(buffer, sz, ratio, final); 378 pushBufferResampling(buffer, sz, ratio, final);
392 } else { 379 } else {
393 pushBufferNonResampling(buffer, sz); 380 pushBufferNonResampling(buffer, sz);
394 } 381 }
382
383 return sz;
395 } 384 }
396 385
397 void 386 void
398 CodedAudioFileReader::pushBufferNonResampling(float *buffer, sv_frame_t sz) 387 CodedAudioFileReader::pushBufferNonResampling(float *buffer, sv_frame_t sz)
399 { 388 {
442 } 431 }
443 break; 432 break;
444 433
445 case CacheInMemory: 434 case CacheInMemory:
446 m_dataLock.lock(); 435 m_dataLock.lock();
447 /*
448 if (m_data.size() < 5120) {
449 for (int i = 0; i < count && i < 5120; ++i) {
450 if (i % 8 == 0) cerr << i << ": ";
451 cerr << buffer[i] << " ";
452 if (i % 8 == 7) cerr << endl;
453 }
454 }
455 cerr << endl;
456 */
457 m_data.insert(m_data.end(), buffer, buffer + count); 436 m_data.insert(m_data.end(), buffer, buffer + count);
458 m_dataLock.unlock(); 437 m_dataLock.unlock();
459 break; 438 break;
460 } 439 }
461 } 440 }
462 441
463 void 442 void
464 CodedAudioFileReader::pushBufferResampling(float *buffer, sv_frame_t sz, 443 CodedAudioFileReader::pushBufferResampling(float *buffer, sv_frame_t sz,
465 double ratio, bool final) 444 double ratio, bool final)
466 { 445 {
467 SVDEBUG << "pushBufferResampling: ratio = " << ratio << ", sz = " << sz << ", final = " << final << endl; 446 // SVDEBUG << "pushBufferResampling: ratio = " << ratio << ", sz = " << sz << ", final = " << final << endl;
468 447
469 if (sz > 0) { 448 if (sz > 0) {
470 449
471 sv_frame_t out = m_resampler->resampleInterleaved 450 sv_frame_t out = m_resampler->resampleInterleaved
472 (buffer, 451 (buffer,
485 padFrames = m_fileFrameCount - sv_frame_t(double(m_frameCount) / ratio) + 1; 464 padFrames = m_fileFrameCount - sv_frame_t(double(m_frameCount) / ratio) + 1;
486 } 465 }
487 466
488 sv_frame_t padSamples = padFrames * m_channelCount; 467 sv_frame_t padSamples = padFrames * m_channelCount;
489 468
490 SVDEBUG << "frameCount = " << m_frameCount << ", equivFileFrames = " << double(m_frameCount) / ratio << ", m_fileFrameCount = " << m_fileFrameCount << ", padFrames= " << padFrames << ", padSamples = " << padSamples << endl; 469 SVDEBUG << "pushBufferResampling: frameCount = " << m_frameCount << ", equivFileFrames = " << double(m_frameCount) / ratio << ", m_fileFrameCount = " << m_fileFrameCount << ", padFrames = " << padFrames << ", padSamples = " << padSamples << endl;
491 470
492 float *padding = new float[padSamples]; 471 float *padding = new float[padSamples];
493 for (sv_frame_t i = 0; i < padSamples; ++i) padding[i] = 0.f; 472 for (sv_frame_t i = 0; i < padSamples; ++i) padding[i] = 0.f;
494 473
495 sv_frame_t out = m_resampler->resampleInterleaved 474 sv_frame_t out = m_resampler->resampleInterleaved