I finally fixed the problem by following @danomatika's suggestion!
My code is as below.
in ofApp.h file,
int ticksPerBuffer = 16; //default ticksPerBuffer
int curBufferSize = -1; //current bufferSize
in ofApp.mm file,
//-------------------------------------------------------------
void ofApp::setup() {
//setup OF sound stream
ofSoundStreamSetup(2, 1, this, 44100, ofxPd::blockSize()*ticksPerBuffer, 3);
}
//--------------------------------------------------------------
void ofApp::audioRequested(float * output, int bufferSize, int nChannels) {
if (bufferSize == curBufferSize) { //if the new bufferSize is same as the current one
pd.audioOut(output, bufferSize, nChannels);
}
else { //if the new bufferSize is different from the current one, init pd with different ticksPerBuffer
ticksPerBuffer = bufferSize / 64; //calculate new ticksPerBuffer
if (pd.isInited()) {
pd.unsubscribeAll();
pd.removeReceiver(*this);
pd.removeMidiReceiver(*this);
}
if(!pd.init(2, 1, 44100, ticksPerBuffer, false)) {
OF_EXIT_APP(1);
}
// subscribe to receive source names
pd.subscribe("blahblah1");
pd.subscribe("blahblah2");
// add message receiver, required if you want to receieve messages
pd.addReceiver(*this); // automatically receives from all subscribed sources
// add midi receiver, required if you want to recieve midi messages
pd.addMidiReceiver(*this); // automatically receives from all channels
pd.start();//turn pd dsp on
curBufferSize = bufferSize; //set the updated bufferSize
}
}
I tried several times to get it working.
This even works when bufferSize changes while the patch is already running. I could still run the patch without closing and re-opening the patch after the re-init. I don't know why though.
Thank you very much for your help @danomatika! I owe you so much.
Maybe you can add the code to ofxPd iOS examples for other users? it's up to you though.
Thank you!! Image may be NSFW.
Clik here to view.