Mercurial > hg > svcore
comparison data/fileio/CodedAudioFileReader.cpp @ 758:babed5be1ae7
Better general approach
author | Chris Cannam |
---|---|
date | Fri, 08 Mar 2013 20:05:49 +0000 |
parents | b98f5daab19e |
children | a43acbe3988f |
comparison
equal
deleted
inserted
replaced
757:b98f5daab19e | 758:babed5be1ae7 |
---|---|
293 } | 293 } |
294 | 294 |
295 void | 295 void |
296 CodedAudioFileReader::pushBuffer(float *buffer, size_t sz, bool final) | 296 CodedAudioFileReader::pushBuffer(float *buffer, size_t sz, bool final) |
297 { | 297 { |
298 m_fileFrameCount += sz; | |
299 | |
300 float ratio = 1.f; | |
301 if (m_resampler && m_fileRate != 0) { | |
302 ratio = float(m_sampleRate) / float(m_fileRate); | |
303 } | |
304 | |
305 if (ratio != 1.f) { | |
306 pushBufferResampling(buffer, sz, ratio, final); | |
307 } else { | |
308 pushBufferNonResampling(buffer, sz); | |
309 } | |
310 } | |
311 | |
312 void | |
313 CodedAudioFileReader::pushBufferNonResampling(float *buffer, size_t sz) | |
314 { | |
298 float max = 1.0; | 315 float max = 1.0; |
299 size_t count = sz * m_channelCount; | 316 size_t count = sz * m_channelCount; |
300 | |
301 m_fileFrameCount += sz; | |
302 | |
303 float ratio = 1.f; | |
304 | |
305 if (m_resampler && m_fileRate != 0) { | |
306 | |
307 ratio = float(m_sampleRate) / float(m_fileRate); | |
308 | |
309 if (ratio != 1.f) { | |
310 | |
311 size_t out = m_resampler->resampleInterleaved | |
312 (buffer, | |
313 m_resampleBuffer, | |
314 sz, | |
315 ratio, | |
316 final); | |
317 | |
318 buffer = m_resampleBuffer; | |
319 sz = out; | |
320 count = sz * m_channelCount; | |
321 } | |
322 } | |
323 | 317 |
324 for (size_t i = 0; i < count; ++i) { | 318 for (size_t i = 0; i < count; ++i) { |
325 if (buffer[i] > max) buffer[i] = max; | 319 if (buffer[i] > max) buffer[i] = max; |
326 } | 320 } |
327 for (size_t i = 0; i < count; ++i) { | 321 for (size_t i = 0; i < count; ++i) { |
347 } | 341 } |
348 MUNLOCK_SAMPLEBLOCK(m_data); | 342 MUNLOCK_SAMPLEBLOCK(m_data); |
349 m_dataLock.unlock(); | 343 m_dataLock.unlock(); |
350 break; | 344 break; |
351 } | 345 } |
352 | 346 } |
353 if (final && m_resampler && ratio != 1.f) { | 347 |
354 size_t equivFileFrames = m_frameCount / ratio; | 348 void |
355 if (equivFileFrames < m_fileFrameCount) { | 349 CodedAudioFileReader::pushBufferResampling(float *buffer, size_t sz, |
356 size_t padFrames = m_fileFrameCount - equivFileFrames + 32; | 350 float ratio, bool final) |
357 size_t padSamples = padFrames * m_channelCount; | 351 { |
358 std::cerr << "frameCount = " << m_frameCount << ", equivFileFrames = " << equivFileFrames << ", m_fileFrameCount = " << m_fileFrameCount << ", padFrames= " << padFrames << ", padSamples = " << padSamples << std::endl; | 352 size_t out = m_resampler->resampleInterleaved |
359 float *padding = new float[padSamples]; | 353 (buffer, |
360 for (int i = 0; i < padSamples; ++i) padding[i] = 0.f; | 354 m_resampleBuffer, |
361 | 355 sz, |
362 //!!! these are not file frames (ugh this is horrible) | 356 ratio, |
363 m_fileFrameCount -= padFrames; | 357 false); |
364 | 358 |
365 pushBuffer(padding, padFrames, true); | 359 pushBufferNonResampling(m_resampleBuffer, out); |
366 delete[] padding; | 360 |
367 } | 361 if (final) { |
362 | |
363 size_t padFrames = 1; | |
364 if (m_frameCount / ratio < m_fileFrameCount) { | |
365 padFrames = m_fileFrameCount - (m_frameCount / ratio) + 1; | |
366 } | |
367 | |
368 size_t padSamples = padFrames * m_channelCount; | |
369 | |
370 std::cerr << "frameCount = " << m_frameCount << ", equivFileFrames = " << m_frameCount / ratio << ", m_fileFrameCount = " << m_fileFrameCount << ", padFrames= " << padFrames << ", padSamples = " << padSamples << std::endl; | |
371 | |
372 float *padding = new float[padSamples]; | |
373 for (int i = 0; i < padSamples; ++i) padding[i] = 0.f; | |
374 | |
375 out = m_resampler->resampleInterleaved | |
376 (padding, | |
377 m_resampleBuffer, | |
378 padFrames, | |
379 ratio, | |
380 true); | |
381 | |
382 pushBufferNonResampling(m_resampleBuffer, out); | |
383 delete[] padding; | |
368 } | 384 } |
369 } | 385 } |
370 | 386 |
371 void | 387 void |
372 CodedAudioFileReader::getInterleavedFrames(size_t start, size_t count, | 388 CodedAudioFileReader::getInterleavedFrames(size_t start, size_t count, |