changeset 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
files data/fileio/CodedAudioFileReader.cpp data/fileio/CodedAudioFileReader.h data/fileio/MP3FileReader.cpp
diffstat 3 files changed, 38 insertions(+), 53 deletions(-) [+]
line wrap: on
line diff
--- a/data/fileio/CodedAudioFileReader.cpp	Tue Nov 29 11:35:56 2016 +0000
+++ b/data/fileio/CodedAudioFileReader.cpp	Tue Nov 29 12:06:49 2016 +0000
@@ -241,21 +241,11 @@
         for (int c = 0; c < m_channelCount; ++c) {
 
             float sample = samples[c][i];
-        
             m_cacheWriteBuffer[m_cacheWriteBufferIndex++] = sample;
 
-            if (m_cacheWriteBufferIndex ==
-                m_cacheWriteBufferSize * m_channelCount) {
+        }
 
-                pushBuffer(m_cacheWriteBuffer, m_cacheWriteBufferSize, false);
-                m_cacheWriteBufferIndex = 0;
-            }
-
-            if (m_cacheWriteBufferIndex % 10240 == 0 &&
-                m_cacheFileReader) {
-                m_cacheFileReader->updateFrameCount();
-            }
-        }
+        pushCacheWriteBufferMaybe(false);
     }
 }
 
@@ -278,19 +268,9 @@
             float sample = samples[i * m_channelCount + c];
         
             m_cacheWriteBuffer[m_cacheWriteBufferIndex++] = sample;
+        }
 
-            if (m_cacheWriteBufferIndex ==
-                m_cacheWriteBufferSize * m_channelCount) {
-
-                pushBuffer(m_cacheWriteBuffer, m_cacheWriteBufferSize, false);
-                m_cacheWriteBufferIndex = 0;
-            }
-
-            if (m_cacheWriteBufferIndex % 10240 == 0 &&
-                m_cacheFileReader) {
-                m_cacheFileReader->updateFrameCount();
-            }
-        }
+        pushCacheWriteBufferMaybe(false);
     }
 }
 
@@ -310,17 +290,7 @@
         
         m_cacheWriteBuffer[m_cacheWriteBufferIndex++] = sample;
 
-        if (m_cacheWriteBufferIndex ==
-            m_cacheWriteBufferSize * m_channelCount) {
-
-            pushBuffer(m_cacheWriteBuffer, m_cacheWriteBufferSize, false);
-            m_cacheWriteBufferIndex = 0;
-        }
-
-        if (m_cacheWriteBufferIndex % 10240 == 0 &&
-            m_cacheFileReader) {
-            m_cacheFileReader->updateFrameCount();
-        }
+        pushCacheWriteBufferMaybe(false);
     }
 }
 
@@ -336,9 +306,7 @@
         return;
     }
 
-    pushBuffer(m_cacheWriteBuffer,
-               m_cacheWriteBufferIndex / m_channelCount,
-               true);
+    pushCacheWriteBufferMaybe(true);
 
     delete[] m_cacheWriteBuffer;
     m_cacheWriteBuffer = 0;
@@ -378,6 +346,25 @@
 }
 
 void
+CodedAudioFileReader::pushCacheWriteBufferMaybe(bool final)
+{
+    if (final ||
+        (m_cacheWriteBufferIndex ==
+         m_cacheWriteBufferSize * m_channelCount)) {
+        
+        pushBuffer(m_cacheWriteBuffer,
+                   m_cacheWriteBufferIndex / m_channelCount,
+                   final);
+        
+        m_cacheWriteBufferIndex = 0;
+
+        if (m_cacheFileReader) {
+            m_cacheFileReader->updateFrameCount();
+        }
+    }
+}
+
+sv_frame_t
 CodedAudioFileReader::pushBuffer(float *buffer, sv_frame_t sz, bool final)
 {
     m_fileFrameCount += sz;
@@ -392,6 +379,8 @@
     } else {
         pushBufferNonResampling(buffer, sz);
     }
+
+    return sz;
 }
 
 void
@@ -444,16 +433,6 @@
 
     case CacheInMemory:
         m_dataLock.lock();
-        /*
-        if (m_data.size() < 5120) {
-            for (int i = 0; i < count && i < 5120; ++i) {
-                if (i % 8 == 0) cerr << i << ": ";
-                cerr << buffer[i] << " ";
-                if (i % 8 == 7) cerr << endl;
-            }
-        }
-        cerr << endl;
-        */
         m_data.insert(m_data.end(), buffer, buffer + count);
         m_dataLock.unlock();
         break;
@@ -464,7 +443,7 @@
 CodedAudioFileReader::pushBufferResampling(float *buffer, sv_frame_t sz,
                                            double ratio, bool final)
 {
-    SVDEBUG << "pushBufferResampling: ratio = " << ratio << ", sz = " << sz << ", final = " << final << endl;
+//    SVDEBUG << "pushBufferResampling: ratio = " << ratio << ", sz = " << sz << ", final = " << final << endl;
 
     if (sz > 0) {
 
@@ -487,7 +466,7 @@
 
         sv_frame_t padSamples = padFrames * m_channelCount;
 
-        SVDEBUG << "frameCount = " << m_frameCount << ", equivFileFrames = " << double(m_frameCount) / ratio << ", m_fileFrameCount = " << m_fileFrameCount << ", padFrames= " << padFrames << ", padSamples = " << padSamples << endl;
+        SVDEBUG << "pushBufferResampling: frameCount = " << m_frameCount << ", equivFileFrames = " << double(m_frameCount) / ratio << ", m_fileFrameCount = " << m_fileFrameCount << ", padFrames = " << padFrames << ", padSamples = " << padSamples << endl;
 
         float *padding = new float[padSamples];
         for (sv_frame_t i = 0; i < padSamples; ++i) padding[i] = 0.f;
--- a/data/fileio/CodedAudioFileReader.h	Tue Nov 29 11:35:56 2016 +0000
+++ b/data/fileio/CodedAudioFileReader.h	Tue Nov 29 12:06:49 2016 +0000
@@ -79,8 +79,14 @@
     void endSerialised();
 
 private:
-    void pushBuffer(float *interleaved, sv_frame_t sz, bool final);
+    void pushCacheWriteBufferMaybe(bool final);
+    
+    sv_frame_t pushBuffer(float *interleaved, sv_frame_t sz, bool final);
+
+    // to be called only by pushBuffer
     void pushBufferResampling(float *interleaved, sv_frame_t sz, double ratio, bool final);
+
+    // to be called only by pushBuffer and pushBufferResampling
     void pushBufferNonResampling(float *interleaved, sv_frame_t sz);
 
 protected:
--- a/data/fileio/MP3FileReader.cpp	Tue Nov 29 11:35:56 2016 +0000
+++ b/data/fileio/MP3FileReader.cpp	Tue Nov 29 12:06:49 2016 +0000
@@ -448,8 +448,8 @@
                     << ", padding = " << padding << endl;
 
             SVDEBUG << "MP3FileReader: Will be trimming " << delayToDrop
-                    << " samples at start and " << paddingToDrop
-                    << " at end" << endl;
+                    << " samples from start and " << paddingToDrop
+                    << " from end" << endl;
 
             CodedAudioFileReader::setSamplesToTrim(delayToDrop, paddingToDrop);