[SOLVED] ofSoundStreamSetup vs soundStream.setup()

Hi,
I’m using audio in a project and I’m having troubles with the setup() function of the ofSoundStream class.

If I use

ofSoundStreamSetup(outputs, inputs, sampleRate, bufferSize, nBuffers);

to initialize the sound, everything works fine.

However, if I create a variable

ofSoundStream mySoundStream;

in the ofApp.h and try to use the

mySoundStream.setup(outputs, inputs, sampleRate, bufferSize, nBuffers);

function in ofApp.cpp I don’t get any sound. Any idea why?

Thanks

The reason is that the mySoundStream that you create as a variable doesn’t have a reference to your app, so it can’t get any sound. ofSoundStreamSetup uses a bit of trickery to attach a sound stream to your app, but you can get the same effect by calling setup and passing this to it:

soundStream.setup(this, outputs, inputs, sampleRate, bufferSize, nBuffers);

or you could get the same effect by calling setup as you already are, then doing:

soundStream.setOutput(this);

I tried both

soundStream.setup(outputs, inputs, sampleRate, bufferSize, nBuffers);
    soundStream.setOutput(this);

and

    soundStream.setup(this,outputs, inputs, sampleRate, bufferSize, nBuffers);

but still no sound =/ I can use the ofSoundStreamSetup though… at this point it’s more of a curiosity.

Can you share the code in your setup()? It might be easier to find out what’s up that way :smile:

Ok, it;s a bit of a mess… I’ve been testing a lot of stuff in it:

#pragma once

#include "ofMain.h"
#include "ofxMaxim.h"
#include "ofxMidi.h"
#include "ofxGui.h"

class ofApp : public ofBaseApp{
    
public:
    void setup();
    void update();
    void draw();
    
    void keyPressed(int key);
    void keyReleased(int key);
    void mouseMoved(int x, int y );
    void mouseDragged(int x, int y, int button);
    void mousePressed(int x, int y, int button);
    void mouseReleased(int x, int y, int button);
    void windowResized(int w, int h);
    void dragEvent(ofDragInfo dragInfo);
    void gotMessage(ofMessage msg);
    void exitApp();
    
    void audioIn (float * input, int bufferSize, int nChannels);
    void audioOut (float * output, int bufferSize, int nChannels);
    
    void turnAudioOn(bool & isOn);
    
    ofSoundStream soundStream;
    maxiOsc sine1,sine2;
    
    
    
    int sampleRate, bufferSize, inputs, outputs, nBuffers;
    
    float * audioIn1;
    float * testeBuffer;
    vector<float *> audioBuffer;
    

    
    bool myBool;
    ofParameter<bool> audioOnOff;
    ofxPanel gui;
    float fromFreq, toFreq, freq;
    
    float newTime, oldTime, duration, targetRate, x;
};

and for the .cpp

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
    
    // VIDEO SETUP
    ofSetFrameRate(30);
    myBool = false;
    // AUDIO SETUP
    
    fromFreq = 440;
    toFreq = 880;
    
    sampleRate = 44100;
    bufferSize = 512;
    inputs = outputs = 2;
    nBuffers = inputs*outputs*2;
    ofSoundStreamSetup(outputs, inputs, sampleRate, bufferSize, nBuffers);

//    soundStream.setup(outputs, inputs, sampleRate, bufferSize, nBuffers);
//    soundStream.setOutput(this);
    
    audioOnOff.addListener(this, &ofApp::turnAudioOn);
    
    gui.setup("panel");
    gui.add(audioOnOff.set("Audio ON/OFF", true));
    
    audioIn1 = new float[bufferSize];
    
    
    for (int i = 0; i<bufferSize; i++) {
        audioIn1[i] = 0;
    }
    targetRate = 0;
    newTime = oldTime = 0;
    duration = 5000;
    x = 0;

}

//--------------------------------------------------------------
void ofApp::update(){


}

//--------------------------------------------------------------
void ofApp::draw(){
    ofBackground(0);
    gui.draw();
}

//--------------------------------------------------------------
void ofApp::keyPressed(int key){
    
    if (key == 'a') {
        targetRate = 0;
        fromFreq = 880;
        toFreq = 440;
        duration = 1000;
    }
    
    if (key == 'b') {
        targetRate = 0;
        fromFreq = 440;
        toFreq = 880;
        duration = 10000;
    }
}

//--------------------------------------------------------------
void ofApp::keyReleased(int key){
    
    
}

//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
    
}

//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
    
}

//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
    
}

//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
    
}

//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){
    
}

//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
    
}

//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){
    
}

//--------------------------------------------------------------
void ofApp::audioIn (float * input, int bufferSize, int nChannels){
    if (audioOnOff) {
        
        
        for (int i = 0; i<bufferSize; i++) {
            audioIn1[i] = input[i*nChannels];
        }
    }
}

//--------------------------------------------------------------
void ofApp::audioOut (float * output, int bufferSize, int nChannels){

    if (audioOnOff) {

        newTime = ofGetElapsedTimeMillis();
        
        targetRate+= (newTime - oldTime)/duration;
        targetRate = ofClamp(targetRate, 0, 1);
        freq = ofLerp(fromFreq, toFreq, targetRate);
        
        oldTime = newTime;
        
        //    printf ("%f  %f  %f \n", newTime, oldTime, targetRate );
        
        for (int i =0; i<bufferSize; i++) {
            output[i*nChannels] = sine1.sinewave(freq)*0.3;
            output[i*nChannels+1] = sine2.sinewave(freq)*0.3;
            
        }
    }
    
}

void ofApp::exitApp(){
    
    soundStream.close();
    
}

void ofApp::turnAudioOn(bool & isOn){
    isOn = audioOnOff;
    if (isOn) {

        soundStream.start();
    }
    soundStream.close();
}

It looks like your ofApp::turnAudioOn function needs an else, or soundStream.close() is always going to trigger. That’s the only thing jumping out at me, though

When I use the ofSoundStreamSetup(); it doesn’t interfere; i behaves as I expected it to
(when true the audio is on and when false the audio is off);
I’ll try to change that and see if it works though
Thanks a lot for your help!

Well, got it to work! I added the else, but it was not enough. I think it was having problems because the soundStream was started twice (when with the setup and the the other in the turnAudioOn function).
Here are the changes:

void ofApp::turnAudioOn(bool & isOn){
    
    isOn = audioOnOff;
    if (isOn) {
           soundStream.setup(this,outputs, inputs, sampleRate, bufferSize, nBuffers);
    }
    else{
    soundStream.close();
    }
}

Thanks a lot, @admsyn, woudn’t have fixed it out without your help

1 Like