@@ -29,7 +29,8 @@ SequentialBlockFile::SequentialBlockFile(int nChannels, int samplesPerBlock) :
2929m_file(nullptr ),
3030m_nChannels(nChannels),
3131m_samplesPerBlock(samplesPerBlock),
32- m_blockSize(nChannels*samplesPerBlock)
32+ m_blockSize(nChannels*samplesPerBlock),
33+ m_lastBlockFill(0 )
3334{
3435 m_memBlocks.ensureStorageAllocated (blockArrayInitSize);
3536 for (int i = 0 ; i < nChannels; i++)
@@ -38,10 +39,15 @@ m_blockSize(nChannels*samplesPerBlock)
3839
3940SequentialBlockFile::~SequentialBlockFile ()
4041{
41- // Ensure that all remaining blocks are flushed in order
42+ // Ensure that all remaining blocks are flushed in order. Keep the last one
4243 int n = m_memBlocks.size ();
43- for (int i = 0 ; i < n; i++)
44+ for (int i = 0 ; i < n - 1 ; i++)
45+ {
4446 m_memBlocks.remove (0 );
47+ }
48+
49+ // manually flush the last one to avoid trailing zeroes
50+ m_memBlocks[0 ]->partialFlush (m_lastBlockFill * m_nChannels);
4551}
4652
4753bool SequentialBlockFile::openFile (String filename)
@@ -86,6 +92,7 @@ bool SequentialBlockFile::writeChannel(uint64 startPos, int channel, int16* data
8692 int startIdx = startPos - m_memBlocks[bIndex]->getOffset ();
8793 int startMemPos = startIdx*m_nChannels;
8894 int dataIdx = 0 ;
95+ int lastBlockIdx = m_memBlocks.size () - 1 ;
8996 while (writtenSamples < nSamples)
9097 {
9198 int16* blockPtr = m_memBlocks[bIndex]->getData ();
@@ -96,6 +103,14 @@ bool SequentialBlockFile::writeChannel(uint64 startPos, int channel, int16* data
96103 dataIdx++;
97104 }
98105 writtenSamples += samplesToWrite;
106+
107+ // Update the last block fill index
108+ size_t samplePos = startIdx + samplesToWrite;
109+ if (bIndex == lastBlockIdx && samplePos > m_lastBlockFill)
110+ {
111+ m_lastBlockFill = samplePos;
112+ }
113+
99114 startIdx = 0 ;
100115 startMemPos = 0 ;
101116 bIndex++;
@@ -140,6 +155,7 @@ void SequentialBlockFile::allocateBlocks(uint64 startIndex, int numSamples)
140155 lastOffset += m_samplesPerBlock;
141156 m_memBlocks.add (new FileBlock (m_file, m_blockSize, lastOffset));
142157 }
143-
158+ if (newBlocks > 0 )
159+ m_lastBlockFill = 0 ; // we've added some new blocks, so the last one will be empty
144160}
145161
0 commit comments