Skip to content

Commit cc102e7

Browse files
committed
add simple FileReader pre-buffer cache
1 parent a600740 commit cc102e7

3 files changed

Lines changed: 41 additions & 29 deletions

File tree

Source/Processors/Events/Events.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@ String SystemEvent::getSyncText(const MidiMessage& msg)
219219

220220
//Event
221221
Event::Event(const Event& other)
222-
: Event(other)
222+
//: Event(other)
223+
: Event(other.m_channelInfo, other.m_timestamp, other.m_channel)
223224
{
224225
size_t size = other.m_channelInfo->getDataSize();
225226
m_data.malloc(size);

Source/Processors/FileReader/FileReader.cpp

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ FileReader::FileReader()
3838
, startSample (0)
3939
, stopSample (0)
4040
, counter (0)
41+
, bufferCacheWindow (0)
4142
{
4243
setProcessorType (PROCESSOR_TYPE_SOURCE);
4344

@@ -231,45 +232,55 @@ void FileReader::updateSettings()
231232

232233
void FileReader::process (AudioSampleBuffer& buffer)
233234
{
234-
235-
236235
const int samplesNeededPerBuffer = int (float (buffer.getNumSamples()) * (getDefaultSampleRate() / 44100.0f));
237-
// const int samplesNeeded = int (float (buffer.getNumSamples()) * (getDefaultSampleRate() / 44100.0f));
238236
const int samplesNeeded = samplesNeededPerBuffer * BUFFER_WINDOW_CACHE_SIZE;
239237
// FIXME: needs to account for the fact that the ratio might not be an exact
240238
// integer value
241-
239+
242240
int samplesRead = 0;
243-
244-
while (bufferCacheWindow == 0 && samplesRead < samplesNeeded) // only read every 5th window
241+
242+
// if window id == 0, we need to read and cache BUFFER_WINDOW_CACHE_SIZE more buffer windows
243+
if (bufferCacheWindow == 0)
245244
{
246-
int samplesToRead = samplesNeeded - samplesRead;
247-
if ( (currentSample + samplesToRead) > stopSample) // if there are enough samples for full buffer
245+
246+
// should only loop if reached end of file and resuming from start
247+
while (samplesRead < samplesNeeded)
248248
{
249-
samplesToRead = stopSample - currentSample;
250-
if (samplesToRead > 0)
249+
int samplesToRead = samplesNeeded - samplesRead;
250+
251+
// if reached end of file stream
252+
if ( (currentSample + samplesToRead) > stopSample)
253+
{
254+
samplesToRead = stopSample - currentSample;
255+
if (samplesToRead > 0)
256+
input->readData (readBuffer + samplesRead * currentNumChannels, samplesToRead);
257+
258+
// reset stream to beginning
259+
input->seekTo (startSample);
260+
currentSample = startSample;
261+
}
262+
else // else read the block needed
263+
{
251264
input->readData (readBuffer + samplesRead * currentNumChannels, samplesToRead);
252-
253-
input->seekTo (startSample);
254-
currentSample = startSample;
265+
266+
currentSample += samplesToRead;
267+
}
268+
269+
samplesRead += samplesToRead;
255270
}
256-
else // if there aren't enough samples for full buffer
257-
{
258-
input->readData (readBuffer + samplesRead * currentNumChannels, samplesToRead);
259-
260-
currentSample += samplesToRead;
261-
}
262-
263-
samplesRead += samplesToRead;
264271
}
265-
272+
266273
for (int i = 0; i < currentNumChannels; ++i)
267274
{
268-
input->processChannelData (readBuffer + samplesRead * currentNumChannels * bufferCacheWindow, buffer.getWritePointer (i, 0), i, samplesNeededPerBuffer);
275+
// offset readBuffer index by current cache window count * buffer window size * num channels
276+
input->processChannelData (readBuffer + (samplesNeededPerBuffer * currentNumChannels * bufferCacheWindow),
277+
buffer.getWritePointer (i, 0),
278+
i,
279+
samplesNeededPerBuffer);
269280
}
270-
271-
timestamp += samplesNeeded;
272-
setTimestampAndSamples(timestamp, samplesNeededPerBuffer);
281+
282+
timestamp += samplesNeededPerBuffer;
283+
setTimestampAndSamples(timestamp, samplesNeededPerBuffer);
273284

274285
bufferCacheWindow += 1;
275286
bufferCacheWindow %= BUFFER_WINDOW_CACHE_SIZE;

Source/Processors/FileReader/FileReader.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#include "FileSource.h"
3333

3434
#define BUFFER_SIZE 1024
35-
#define BUFFER_WINDOW_CACHE_SIZE 5
35+
#define BUFFER_WINDOW_CACHE_SIZE 3
3636

3737

3838
/**
@@ -84,7 +84,7 @@ class FileReader : public GenericProcessor
8484
int64 currentNumSamples;
8585
int64 startSample;
8686
int64 stopSample;
87-
int64 bufferCacheWindow;
87+
int64 bufferCacheWindow; // the current buffer window to read from readBuffer
8888
Array<RecordedChannelInfo> channelInfo;
8989

9090
// for testing purposes only

0 commit comments

Comments
 (0)