Skip to content

Commit 5a0524f

Browse files
committed
Remove trailing zeroes in binary format
1 parent 2232816 commit 5a0524f

3 files changed

Lines changed: 34 additions & 5 deletions

File tree

Source/Plugins/BinaryWriter/FileMemoryBlock.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,28 @@ namespace BinaryRecordingEngine
4040
m_offset(offset)
4141
{};
4242
~FileMemoryBlock() {
43-
m_file->write(m_data, m_blockSize*sizeof(StorageType));
43+
if (!m_flushed)
44+
{
45+
m_file->write(m_data, m_blockSize*sizeof(StorageType));
46+
}
4447
};
4548

4649
inline uint64 getOffset() { return m_offset; }
4750
inline StorageType* getData() { return m_data.getData(); }
51+
void partialFlush(size_t size, bool markFlushed = true)
52+
{
53+
std::cout << "flushing last block " << size << std::endl;
54+
m_file->write(m_data, size*sizeof(StorageType));
55+
if (markFlushed)
56+
m_flushed = true;
57+
}
4858

4959
private:
5060
HeapBlock<StorageType> m_data;
5161
FileOutputStream* const m_file;
5262
const int m_blockSize;
5363
const uint64 m_offset;
64+
bool m_flushed{ false };
5465
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(FileMemoryBlock);
5566
};
5667
}

Source/Plugins/BinaryWriter/SequentialBlockFile.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ SequentialBlockFile::SequentialBlockFile(int nChannels, int samplesPerBlock) :
2929
m_file(nullptr),
3030
m_nChannels(nChannels),
3131
m_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

3940
SequentialBlockFile::~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

4753
bool 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

Source/Plugins/BinaryWriter/SequentialBlockFile.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@ namespace BinaryRecordingEngine
4747
const int m_blockSize;
4848
OwnedArray<FileBlock> m_memBlocks;
4949
Array<int> m_currentBlock;
50+
size_t m_lastBlockFill;
5051

5152
void allocateBlocks(uint64 startIndex, int numSamples);
5253

54+
5355
//Compile-time parameters
5456
const int streamBufferSize{ 0 };
5557
const int blockArrayInitSize{ 128 };

0 commit comments

Comments
 (0)