changeset 758:babed5be1ae7

Better general approach
author Chris Cannam
date Fri, 08 Mar 2013 20:05:49 +0000
parents b98f5daab19e
children a43acbe3988f
files data/fileio/AudioFileReaderFactory.cpp data/fileio/CodedAudioFileReader.cpp data/fileio/CodedAudioFileReader.h
diffstat 3 files changed, 51 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/data/fileio/AudioFileReaderFactory.cpp	Fri Mar 08 18:19:05 2013 +0000
+++ b/data/fileio/AudioFileReaderFactory.cpp	Fri Mar 08 20:05:49 2013 +0000
@@ -289,9 +289,6 @@
 #endif
 
 #ifdef HAVE_COREAUDIO
-
-    std::cerr << "AudioFileReaderFactory: HAVE_COREAUDIO" << std::endl;
-
     if (!reader) {
         reader = new CoreAudioFileReader
             (source,
--- a/data/fileio/CodedAudioFileReader.cpp	Fri Mar 08 18:19:05 2013 +0000
+++ b/data/fileio/CodedAudioFileReader.cpp	Fri Mar 08 20:05:49 2013 +0000
@@ -295,31 +295,25 @@
 void
 CodedAudioFileReader::pushBuffer(float *buffer, size_t sz, bool final)
 {
-    float max = 1.0;
-    size_t count = sz * m_channelCount;
-
     m_fileFrameCount += sz;
 
     float ratio = 1.f;
+    if (m_resampler && m_fileRate != 0) {
+        ratio = float(m_sampleRate) / float(m_fileRate);
+    }
+        
+    if (ratio != 1.f) {
+        pushBufferResampling(buffer, sz, ratio, final);
+    } else {
+        pushBufferNonResampling(buffer, sz);
+    }
+}
 
-    if (m_resampler && m_fileRate != 0) {
-        
-        ratio = float(m_sampleRate) / float(m_fileRate);
-        
-        if (ratio != 1.f) {
-
-            size_t out = m_resampler->resampleInterleaved
-                (buffer,
-                 m_resampleBuffer,
-                 sz,
-                 ratio,
-                 final);
-
-            buffer = m_resampleBuffer;
-            sz = out;
-            count = sz * m_channelCount;
-        }
-    }
+void
+CodedAudioFileReader::pushBufferNonResampling(float *buffer, size_t sz)
+{
+    float max = 1.0;
+    size_t count = sz * m_channelCount;
 
     for (size_t i = 0; i < count; ++i) {
         if (buffer[i] >  max) buffer[i] =  max;
@@ -349,22 +343,44 @@
         m_dataLock.unlock();
         break;
     }
+}
 
-    if (final && m_resampler && ratio != 1.f) {
-        size_t equivFileFrames = m_frameCount / ratio;
-        if (equivFileFrames < m_fileFrameCount) {
-            size_t padFrames = m_fileFrameCount - equivFileFrames + 32;
-            size_t padSamples = padFrames * m_channelCount;
-            std::cerr << "frameCount = " << m_frameCount << ", equivFileFrames = " << equivFileFrames << ", m_fileFrameCount = " << m_fileFrameCount << ", padFrames= " << padFrames << ", padSamples = " << padSamples << std::endl;
-            float *padding = new float[padSamples];
-            for (int i = 0; i < padSamples; ++i) padding[i] = 0.f;
+void
+CodedAudioFileReader::pushBufferResampling(float *buffer, size_t sz,
+                                           float ratio, bool final)
+{
+    size_t out = m_resampler->resampleInterleaved
+        (buffer,
+         m_resampleBuffer,
+         sz,
+         ratio,
+         false);
 
-            //!!! these are not file frames (ugh this is horrible)
-            m_fileFrameCount -= padFrames;
+    pushBufferNonResampling(m_resampleBuffer, out);
 
-            pushBuffer(padding, padFrames, true);
-            delete[] padding;
+    if (final) {
+
+        size_t padFrames = 1;
+        if (m_frameCount / ratio < m_fileFrameCount) {
+            padFrames = m_fileFrameCount - (m_frameCount / ratio) + 1;
         }
+
+        size_t padSamples = padFrames * m_channelCount;
+
+        std::cerr << "frameCount = " << m_frameCount << ", equivFileFrames = " << m_frameCount / ratio << ", m_fileFrameCount = " << m_fileFrameCount << ", padFrames= " << padFrames << ", padSamples = " << padSamples << std::endl;
+
+        float *padding = new float[padSamples];
+        for (int i = 0; i < padSamples; ++i) padding[i] = 0.f;
+
+        out = m_resampler->resampleInterleaved
+            (padding,
+             m_resampleBuffer,
+             padFrames,
+             ratio,
+             true);
+
+        pushBufferNonResampling(m_resampleBuffer, out);
+        delete[] padding;
     }
 }
 
--- a/data/fileio/CodedAudioFileReader.h	Fri Mar 08 18:19:05 2013 +0000
+++ b/data/fileio/CodedAudioFileReader.h	Fri Mar 08 20:05:49 2013 +0000
@@ -66,6 +66,8 @@
 
 private:
     void pushBuffer(float *interleaved, size_t sz, bool final);
+    void pushBufferResampling(float *interleaved, size_t sz, float ratio, bool final);
+    void pushBufferNonResampling(float *interleaved, size_t sz);
 
 protected:
     QMutex m_cacheMutex;