ofSoundBuffer &outBuffer multiple outputs

Hi everyone,
I have made a class “Pulse” out of the basic SoundBuffer example from OF0908.
What I am trying to do is to create an array of Pulse that plays in the main ofApp with different settings.
I managed to make one Pulse object sound but if I create an array or just 2 separate objects I can hear no sound.

here is the code in my ofApp

void ofApp::audioOut(ofSoundBuffer &outBuffer) {   
   
   for ( int j=0; j<NUM_SENS; j++ ) {
        myPulse[j].audioOut(outBuffer, 100 * avrgRMSAcc , lastSens, avrgRMSSens);      //this dowsn't work
            }
    
    myPulse1.audioOut(outBuffer, 100 * avrgRMSAcc, lastSens, avrgRMSSens);        //this is working ok alone
}

Any idea anyone?

Cheers!

What are you trying to achieve?
The ofSoundBuffer object is just an array that holds the sound sample data and some info like sample rate, bitrate, number of channels, etc. So, the idea is that in the ofApp::audioOut method you put the data that you want to be sent out through the sound card. I have no idea what the auduiOut function of your pulse class is doing, but I’m guessing that it is simply writing the sample data into the outBuffer object that you pass to it, and it is not mixing it with whatever else was stored before in this object.
So, what I suspect is that with each pass of the for loop you are rewriting over the previously placed data, so at the end only the last will sound. So, in the code you posted the myPulse1 object ends up writing its data to the outBuffer on top of anything written in the for loop and that´s why it is the only one that works.
Hope it helps!

Hi @roymacdonald,
thanks for replying. I am attaching the Pulse class.
Pulse.zip (5.2 KB)

There are 2 audioOut() in Pulse. The one I am using is the second. Is basically passing the time, frequency, and multipliers of the harmonics and sending everything to the Pulse::audioOut(ofSoundBuffer &outBuffer etc.) . When I call one object Pulse in the ofApp::audioOut(ofSoundBuffer &outBuffer) everything is working as expected

//--------------------------------------------------------------
void ofApp::setup(){
        myPulse[0].setup(100);
}
//--------------------------------------------------------------
void ofApp::update(){
        myPulse[0].update();
}
//--------------------------------------------------------------
void ofApp::audioOut(ofSoundBuffer &buffer){        
        myPulse[0].audioOut(buffer, 100, 100, 2, 1, 2, 1.3);
}

but if I create a loop, this doesn’t work. if it’s like you say that the last one is overwriting the previous I should hear something, if I understood your point. However I hear nothing at all. The only thing I can say is that I get this warning in the console [warning] ofRtAudioSoundStream: stream over/underflow detected

//--------------------------------------------------------------
void ofApp::setup(){
    
    for ( int j=0; j<8; j++ ) {
        myPulse[j].setup(100);
    }
}
//--------------------------------------------------------------
void ofApp::update(){
    for ( int j=0; j<8; j++ ) {
        myPulse[j].update();
    }
}
//--------------------------------------------------------------
void ofApp::audioOut(ofSoundBuffer &buffer){
    for ( int j=0; j<8; j++ ) {
        myPulse[j].audioOut(buffer, 100, 100, j*2, 1, 2, 1.3);
    }
}

Hi,
if nothing is sounding maybe there’s a problem with your code but as I said, you are over writing the buffer data with each pass of the for loop.
In the Pulse.cpp on lines 195 and 196

outBuffer.getSample(i, 0) = fullSample;
outBuffer.getSample(i, 1) = fullSample;

instead of setting the sample add it.
like this

outBuffer.getSample(i, 0) += fullSample;
outBuffer.getSample(i, 1) += fullSample;

be carefull that this might end up in high volume so you might want to scale down fullSample

this might be useful
http://openframeworks.cc/ofBook/chapters/sound.html

Also, go through ofxaddons.com there a several addons for dealing with sound, synthesis and analysis.
Also, this one I made might be useful http://github.com/roymacdonald/ofxSoundObjects/

best

Hi @roymacdonald thank you. I think I found the issue.
The code you indicated should be ok as it’s from the OF example. I don’t think I’ve touched that part.
But your hint put me on the right path. the issue is in the setup
ofSoundStreamSetup(2, 0, sampleRate, audioBufferSize, 3);
If I call the ofSoundStreamSetup(); in the ofApp, the loop of Pulses is working. I guess I have to read more on this.
Thank you for support and the links. I will look int it!
:slight_smile:

@roymacdonald I think what I just said is not correct. In this way I can hear sound with the loop of Pulse but I think I hear just the last one as you said. is overwriting the previous ones.

I’ll be back when I find the solution.

Cheers!

Hi, it shouldn’t matter where you call the ofSoundStreamSetup, as it will point to the ofApp instance anyways, but it is not a good idea to call it several times.
as said, you need to mix the samples. Check the addon I linked previously.