f724bfb3bfc7 — Chris Cannam a month ago
Don't sync on every single write! What if we're writing one sample at a time (real example: SV converting from CSV files)
M src/SimpleWavFileWriteStream.cpp +10 -2
@@ 57,10 57,14 @@ simplewavbuilder(
     getSimpleWavWriterExtensions()
     );
 
+size_t
+SimpleWavFileWriteStream::m_syncBlockSize = 4096;
+
 SimpleWavFileWriteStream::SimpleWavFileWriteStream(Target target) :
     AudioWriteStream(target),
     m_bitDepth(24),
-    m_file(0)
+    m_file(0),
+    m_sinceSync(0)
 {
     std::string path = getPath();
     

          
@@ 230,7 234,11 @@ SimpleWavFileWriteStream::putInterleaved
         }
     }
 
-    m_file->flush();
+    m_sinceSync += count;
+    if (m_sinceSync > m_syncBlockSize) {
+        m_file->flush();
+        m_sinceSync = 0;
+    }
 }
 
 }

          
M src/SimpleWavFileWriteStream.h +2 -0
@@ 61,6 61,8 @@ protected:
     int m_bitDepth;
     std::string m_error;
     std::ofstream *m_file;
+    size_t m_sinceSync;
+    static size_t m_syncBlockSize;
 
     void writeFormatChunk();
     void putBytes(const std::string &);

          
M src/WavFileWriteStream.cpp +10 -2
@@ 57,9 57,13 @@ wavbuilder(
     getWavWriterExtensions()
     );
 
+size_t
+WavFileWriteStream::m_syncBlockSize = 4096;
+
 WavFileWriteStream::WavFileWriteStream(Target target) :
     AudioWriteStream(target),
-    m_file(0)
+    m_file(0),
+    m_sinceSync(0)
 {
     memset(&m_fileInfo, 0, sizeof(SF_INFO));
     m_fileInfo.format = SF_FORMAT_WAV | SF_FORMAT_FLOAT;

          
@@ 105,7 109,11 @@ WavFileWriteStream::putInterleavedFrames
         throw FileOperationFailed(getPath(), "write sf data");
     }
 
-    sf_write_sync(m_file);
+    m_sinceSync += count;
+    if (m_sinceSync > m_syncBlockSize) {
+        sf_write_sync(m_file);
+        m_sinceSync = 0;
+    }
 }
 
 }

          
M src/WavFileWriteStream.h +2 -0
@@ 63,6 63,8 @@ protected:
     SF_INFO m_fileInfo;
     SNDFILE *m_file;
 
+    size_t m_sinceSync;
+    static size_t m_syncBlockSize;
     std::string m_error;
 };