OwlSim icon indicating copy to clipboard operation
OwlSim copied to clipboard

getSampleRate() returns 0 when called from patch constructor

Open olilarkin opened this issue 10 years ago • 3 comments

Would be good to have a dedicated Reset() method etc where samplerate is known, or to default to a sensible SR such as 44100 rather than 0.!

olilarkin avatar Jul 09 '15 22:07 olilarkin

That's a bug! The idea is that sample rate and blocksize is always known when the patch constructor is called, so there's no need to resize buffers et c.

ghost avatar Jul 10 '15 11:07 ghost

Problem:

double AudioProcessor::getSampleRate()
Returns the current sample rate.
This can be called from your processBlock() method - it's not guaranteed to be valid at any other time, and may return 0 if it's unknown.

https://www.juce.com/api/classAudioProcessor.html#a99b216c491ed4896424a9a075858ca03

Is there no way of getting the host sample rate and blocksize before processBlock()? Otherwise we might have to instantiate at the first call to the loop, which would kind of suck, but we can use placement new to re-initialise the pre-allocated object which shouldn't be too expensive.

ghost avatar Jul 10 '15 12:07 ghost

I think we can safely move patch instantiation to the prepareToPlay() callback:

--- a/Source/PluginProcessor.cpp
+++ b/Source/PluginProcessor.cpp
@@ -57,7 +57,6 @@ void StompBoxAudioProcessor::setPatch(std::string name){
   currentPatchName = name;
   instance = this; // thread local instance must be set before Patch constructo
   patchprocessor = new PluginPatchProcessor(this);
-  patchprocessor->setPatch(patches.create(name));
 }

 const String StompBoxAudioProcessor::getName() const{
@@ -150,7 +149,7 @@ void StompBoxAudioProcessor::changeProgramName(int index, co
 void StompBoxAudioProcessor::prepareToPlay(double sampleRate, int samplesPerBlo
   // Use this method as the place to do any pre-playback
   // initialisation that you need..
-    
+  patchprocessor->setPatch(patches.create(currentPatchName));    
 }

I can't test this right now but will push a branch called pptp

ghost avatar Jul 10 '15 12:07 ghost