Skip to content

Commit 611cca0

Browse files
committed
add a cached file reading buffer to decrease number of file accesses
1 parent db64af0 commit 611cca0

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

Source/Processors/FileReader/FileReader.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ void FileReader::setActiveRecording (int index)
195195
currentSample = 0;
196196
startSample = 0;
197197
stopSample = currentNumSamples;
198+
bufferCacheWindow = 0;
198199

199200
for (int i = 0; i < currentNumChannels; ++i)
200201
{
@@ -203,7 +204,7 @@ void FileReader::setActiveRecording (int index)
203204

204205
static_cast<FileReaderEditor*> (getEditor())->setTotalTime (samplesToMilliseconds (currentNumSamples));
205206

206-
readBuffer.malloc (currentNumChannels * BUFFER_SIZE);
207+
readBuffer.malloc (currentNumChannels * BUFFER_SIZE * BUFFER_WINDOW_CACHE_SIZE);
207208
}
208209

209210

@@ -232,16 +233,18 @@ void FileReader::process (AudioSampleBuffer& buffer)
232233
{
233234

234235

235-
const int samplesNeeded = int (float (buffer.getNumSamples()) * (getDefaultSampleRate() / 44100.0f));
236+
const int samplesNeededPerBuffer = int (float (buffer.getNumSamples()) * (getDefaultSampleRate() / 44100.0f));
237+
// const int samplesNeeded = int (float (buffer.getNumSamples()) * (getDefaultSampleRate() / 44100.0f));
238+
const int samplesNeeded = samplesNeededPerBuffer * BUFFER_WINDOW_CACHE_SIZE;
236239
// FIXME: needs to account for the fact that the ratio might not be an exact
237240
// integer value
238241

239242
int samplesRead = 0;
240243

241-
while (samplesRead < samplesNeeded)
244+
while (bufferCacheWindow == 0 && samplesRead < samplesNeeded) // only read every 5th window
242245
{
243246
int samplesToRead = samplesNeeded - samplesRead;
244-
if ( (currentSample + samplesToRead) > stopSample)
247+
if ( (currentSample + samplesToRead) > stopSample) // if there are enough samples for full buffer
245248
{
246249
samplesToRead = stopSample - currentSample;
247250
if (samplesToRead > 0)
@@ -250,7 +253,7 @@ void FileReader::process (AudioSampleBuffer& buffer)
250253
input->seekTo (startSample);
251254
currentSample = startSample;
252255
}
253-
else
256+
else // if there aren't enough samples for full buffer
254257
{
255258
input->readData (readBuffer + samplesRead * currentNumChannels, samplesToRead);
256259

@@ -262,12 +265,14 @@ void FileReader::process (AudioSampleBuffer& buffer)
262265

263266
for (int i = 0; i < currentNumChannels; ++i)
264267
{
265-
input->processChannelData (readBuffer, buffer.getWritePointer (i, 0), i, samplesNeeded);
268+
input->processChannelData (readBuffer + samplesRead * currentNumChannels * bufferCacheWindow, buffer.getWritePointer (i, 0), i, samplesNeededPerBuffer);
266269
}
267270

268271
timestamp += samplesNeeded;
269-
setTimestampAndSamples(timestamp, samplesNeeded);
272+
setTimestampAndSamples(timestamp, samplesNeededPerBuffer);
270273

274+
bufferCacheWindow += 1;
275+
bufferCacheWindow %= BUFFER_WINDOW_CACHE_SIZE;
271276
}
272277

273278

Source/Processors/FileReader/FileReader.h

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

3434
#define BUFFER_SIZE 1024
35+
#define BUFFER_WINDOW_CACHE_SIZE 5
3536

3637

3738
/**
@@ -83,6 +84,7 @@ class FileReader : public GenericProcessor
8384
int64 currentNumSamples;
8485
int64 startSample;
8586
int64 stopSample;
87+
int64 bufferCacheWindow;
8688
Array<RecordedChannelInfo> channelInfo;
8789

8890
// for testing purposes only

0 commit comments

Comments
 (0)