Skip to content

Commit 5a07d90

Browse files
committed
Fix buffering issues in File Reader
1 parent 8b34b7b commit 5a07d90

2 files changed

Lines changed: 40 additions & 16 deletions

File tree

Source/Processors/FileReader/FileReader.cpp

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "FileReaderEditor.h"
2626
#include <stdio.h>
2727
#include "../../AccessClass.h"
28+
#include "../../Audio/AudioComponent.h"
2829
#include "../PluginManager/PluginManager.h"
2930

3031

@@ -41,6 +42,8 @@ FileReader::FileReader()
4142
, counter (0)
4243
, bufferCacheWindow (0)
4344
, m_shouldFillBackBuffer(false)
45+
, m_bufferSize(1024)
46+
, m_sysSampleRate(44100)
4447
{
4548
setProcessorType (PROCESSOR_TYPE_SOURCE);
4649

@@ -135,9 +138,38 @@ void FileReader::setEnabledState (bool t)
135138
bool FileReader::enable()
136139
{
137140
timestamp = 0;
141+
142+
AudioDeviceManager& adm = AccessClass::getAudioComponent()->deviceManager;
143+
AudioDeviceManager::AudioDeviceSetup ads;
144+
adm.getAudioDeviceSetup(ads);
145+
m_sysSampleRate = ads.sampleRate;
146+
m_bufferSize = ads.bufferSize;
147+
if (m_bufferSize == 0) m_bufferSize = 1024;
148+
149+
m_samplesPerBuffer.set(m_bufferSize * (getDefaultSampleRate() / m_sysSampleRate));
150+
151+
bufferA.malloc(currentNumChannels * m_bufferSize * BUFFER_WINDOW_CACHE_SIZE);
152+
bufferB.malloc(currentNumChannels * m_bufferSize * BUFFER_WINDOW_CACHE_SIZE);
153+
154+
readAndFillBufferCache(bufferA); // pre-fill the front buffer with a blocking read
155+
156+
// set the backbuffer so that on the next call to process() we start with bufferA and buffer
157+
// cache window id = 0
158+
readBuffer = &bufferB;
159+
bufferCacheWindow = 0;
160+
m_shouldFillBackBuffer.set(false);
161+
162+
startThread(); // start async file reader thread
163+
138164
return isEnabled;
139165
}
140166

167+
bool FileReader::disable()
168+
{
169+
stopThread(100);
170+
return true;
171+
}
172+
141173
bool FileReader::isFileSupported (const String& fileName) const
142174
{
143175
const File file (fileName);
@@ -196,12 +228,6 @@ bool FileReader::setFile (String fullpath)
196228
static_cast<FileReaderEditor*> (getEditor())->populateRecordings (input);
197229
setActiveRecording (0);
198230

199-
m_samplesPerBuffer.set(float(BUFFER_SIZE) * (getDefaultSampleRate() / 44100.0f));
200-
201-
readAndFillBufferCache(bufferA); // pre-fill the front buffer with a blocking read
202-
203-
startThread(); // start async file reader thread
204-
205231
return true;
206232
}
207233

@@ -225,14 +251,9 @@ void FileReader::setActiveRecording (int index)
225251
}
226252

227253
static_cast<FileReaderEditor*> (getEditor())->setTotalTime (samplesToMilliseconds (currentNumSamples));
254+
input->seekTo(startSample);
228255

229-
bufferA.malloc (currentNumChannels * BUFFER_SIZE * BUFFER_WINDOW_CACHE_SIZE);
230-
bufferB.malloc (currentNumChannels * BUFFER_SIZE * BUFFER_WINDOW_CACHE_SIZE);
231-
232-
// set the backbuffer so that on the next call to process() we start with bufferA and buffer
233-
// cache window id = 0
234-
readBuffer = &bufferB;
235-
bufferCacheWindow = 0;
256+
236257
}
237258

238259

@@ -258,7 +279,7 @@ void FileReader::updateSettings()
258279

259280
void FileReader::process (AudioSampleBuffer& buffer)
260281
{
261-
const int samplesNeededPerBuffer = int (float (buffer.getNumSamples()) * (getDefaultSampleRate() / 44100.0f));
282+
const int samplesNeededPerBuffer = int (float (buffer.getNumSamples()) * (getDefaultSampleRate() / m_sysSampleRate));
262283
m_samplesPerBuffer.set(samplesNeededPerBuffer);
263284
// FIXME: needs to account for the fact that the ratio might not be an exact
264285
// integer value
@@ -280,7 +301,7 @@ void FileReader::process (AudioSampleBuffer& buffer)
280301

281302
setTimestampAndSamples(timestamp, samplesNeededPerBuffer);
282303
timestamp += samplesNeededPerBuffer;
283-
304+
284305
bufferCacheWindow += 1;
285306
bufferCacheWindow %= BUFFER_WINDOW_CACHE_SIZE;
286307
}

Source/Processors/FileReader/FileReader.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#include "../GenericProcessor/GenericProcessor.h"
3232
#include "FileSource.h"
3333

34-
#define BUFFER_SIZE 1024
3534
#define BUFFER_WINDOW_CACHE_SIZE 10
3635

3736

@@ -64,6 +63,7 @@ class FileReader : public GenericProcessor,
6463
void updateSettings() override;
6564
void setEnabledState (bool t) override;
6665
bool enable() override;
66+
bool disable() override;
6767

6868
String getFile() const;
6969
bool setFile (String fullpath);
@@ -105,6 +105,9 @@ class FileReader : public GenericProcessor,
105105

106106
Atomic<int> m_shouldFillBackBuffer;
107107
Atomic<int> m_samplesPerBuffer;
108+
109+
unsigned int m_bufferSize;
110+
float m_sysSampleRate;
108111

109112
/** Swaps the backbuffer to the front and flags the background reader
110113
thread to update the new backbuffer */

0 commit comments

Comments
 (0)