I am trying to get my first attempt at Thread
use off the ground as summarized in this thread. I am first trying to run a custom thread in my synth PluginProcessor.cpp and assign the basic renderNextBlock
synthesiser processing to that thread.
I need to get a reference to my synth and the buffer into the Thread
inherited class object testThread
so it can process them there.
My attempt however is giving me a read access violation upon attempting to give the testThread
a reference to the usual buffers. I presume this is just a minor syntax error but I am not good enough at C++ to know where it’s coming from.
Here’s what I am running in my PluginProcessor.cpp:
void AudioPlugInAudioProcessor::processBlock (AudioBuffer<float>& buffer, MidiBuffer& midiMessages) {
buffer.clear();
//OLD RENDER METHOD:
//mMpeSynth.renderNextBlockCustom(buffer, midiMessages, 0, buffer.getNumSamples());
//NEW RENDER METHOD:
testThread.inputReferenceBuffer(buffer, midiMessages, 0, buffer.getNumSamples());
while (testThread.newBufferToProcess) {
//wait until testThread is done before finishing this processBlock
}
}
My Thread class is:
class ThreadInherited : public Thread {
public:
ThreadInherited(const String& threadName, MPESynthesiserInherited *mMPESynthIn, size_t threadStackSize = 0) : Thread(threadName) {
mMpeSynthPtr = mMPESynthIn;
}
//this is the function that is breaking:
void inputReferenceBuffer(AudioBuffer<float>& outputAudio, const MidiBuffer& inputMidi, int startSampleIn, int numSamplesIn) {
*outputAudioBufPtr = outputAudio; //this line gives read access violation
*inputMidiBufPtr = inputMidi; //this line gives read access violation
DBG(outputAudioBufPtr->getNumSamples()); //this line gives read access violation
startSample = startSampleIn;
numSamples = numSamplesIn;
newBufferToProcess = true;
}
void run() override {
while (!threadShouldExit()) {
if (newBufferToProcess) {
//not even getting this far ...
mMpeSynthPtr->renderNextBlockCustom(*outputAudioBufPtr, *inputMidiBufPtr, 0, outputAudioBufPtr->getNumSamples());
newBufferToProcess = false;
}
else { //thread running idle
}
}
}
bool newBufferToProcess = false; //to tell when it needs to process or if it is done
private:
MPESynthesiserInherited* mMpeSynthPtr;
AudioBuffer<float>* outputAudioBufPtr;
MidiBuffer* inputMidiBufPtr;
int startSample = 0;
int numSamples = 512;
};
I’ve explored the threading issues a bit in the other discussion and I know it can be complicated but in this case it seems like I’m just not getting the buffer reference into the Thread
properly because it’s not even getting to the point of trying to process the buffer. It gives me a read access as soon as I try to even put the buffer references into the thread.
So I presume this is just a syntax error of some kind regarding pointers/references but I’m not sure what/why it is happening. How do I get a working pointer/reference to the buffers inside my testThread
here so I can even run simple functions on them in Thread
like getNumSamples()
?
Thanks.