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)
135138bool 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+
141173bool 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
259280void 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}
0 commit comments