Skip to content

Commit

Permalink
added mutex to pdMultiExample for instance func thread safety
Browse files Browse the repository at this point in the history
  • Loading branch information
danomatika committed Aug 12, 2015
1 parent dfb9c0a commit ec58196
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pdMultiExample/src/ofApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,16 @@ void ofApp::setup() {
ofSoundStreamSetup(numOutputs, numInputs, this, 44100, ofxPd::blockSize()*ticksPerBuffer, 4);

// allocate pd instance handles
instanceMutex.lock();
pdinstance1 = pdinstance_new();
pdinstance2 = pdinstance_new();
instanceMutex.unlock();

// set a "current" instance before pd.init() or else Pd will make
// an unnecessary third "default" instance
instanceMutex.lock();
pd_setinstance(pdinstance1);
instanceMutex.unlock();

// setup Pd
//
Expand All @@ -65,15 +69,19 @@ void ofApp::setup() {
memset(outputBuffer1, 0, bufferSize);
memset(outputBuffer2, 0, bufferSize);

instanceMutex.lock();
pd_setinstance(pdinstance1); // talk to first pd instance
instanceMutex.unlock();

// audio processing on
pd.start();

// open patch
pd.openPatch("test.pd");

instanceMutex.lock();
pd_setinstance(pdinstance2); // talk to the second pd instance
instanceMutex.unlock();

// audio processing on
pd.start();
Expand Down Expand Up @@ -123,7 +131,9 @@ void ofApp::draw() {}
void ofApp::audioReceived(float * input, int bufferSize, int nChannels) {

// process audio input for instance 1
instanceMutex.lock();
pd_setinstance(pdinstance1);
instanceMutex.unlock();
pd.audioIn(input, bufferSize, nChannels);

// process audio input for instance 2
Expand All @@ -135,11 +145,15 @@ void ofApp::audioReceived(float * input, int bufferSize, int nChannels) {
void ofApp::audioRequested(float * output, int bufferSize, int nChannels) {

// process audio output for instance 1
instanceMutex.lock();
pd_setinstance(pdinstance1);
instanceMutex.unlock();
pd.audioOut(outputBuffer1, bufferSize, nChannels);

// process audio output for instance 2
instanceMutex.lock();
pd_setinstance(pdinstance2);
instanceMutex.unlock();
pd.audioOut(outputBuffer2, bufferSize, nChannels);

// mix the two instance output buffers together
Expand Down
4 changes: 4 additions & 0 deletions pdMultiExample/src/ofApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ class ofApp : public ofBaseApp, public PdReceiver{
// pd types which tell libpd to address a separate internal "instance"
t_pdinstance *pdinstance1, *pdinstance2;

// used to avoid concurrency segfaults between main and audio thread
// when accessing currently non thread safe pd instance functions
ofMutex instanceMutex;

float* outputBuffer1; //< interleaved audio output buffer for instance 1
float* outputBuffer2; //< interleaved audio output buffer for instance 2
};

0 comments on commit ec58196

Please sign in to comment.