No audio from mic

hi all
i am completely new to audio in openframesworks.
I started off with examples/sound/audioInputExample
and created a stripped down version of the example as a separate class that I load in my app.

Then I try to grab microphone input but i only get zeros…
I have a HUGE question mark about the member function “audioIn”, this is not called from anyway in the example, so I assume this is a callback function that is setup… somewhere else???
I am not sure. can someone please clarify?

my application is in a testApp.cpp/.h pair of files, and I am #including the cAudioIn.cpp/.h which are based on the aforementioned example (and which I attach below).
Since the ofSoundStream::setup(…) member function requires the ofBaseApp pointer to be passed in , I had to modify the constructor of my “classAudioIn” abit to get that from testApp and pass it on to ofSoundStream. Regardless, it seems that the “audioIn” member function, is never called on its own (as is the case in the example)

Any clues as to why I get no input? note that from the example I do get input without any problem…

this is audioIn.h

  #pragma once

   #include "ofMain.h"

class classAudioIn {

	public:
		void setup(ofBaseApp *app);
		float updateVol();

		bool audioInEnabled ; //enable - disable processing

		void audioIn(float * input, int bufferSize, int nChannels);

    private:
		//void draw();

		vector <float> left;
		vector <float> right;
		//vector <float> volHistory;

		float smoothedVol;

		ofSoundStream soundStream;
};

and this is audioIn.cpp

void classAudioIn::setup(ofBaseApp *app){

	soundStream.listDevices();

	int bufferSize = 256;
	smoothedVol     = 0.0;

	left.assign(bufferSize, 0.0);
	right.assign(bufferSize, 0.0);

        audioInEnabled = false ;
	soundStream.setup(app, 0, 2, 44100, bufferSize, 4);
}

float classAudioIn::updateVol(){
    float outVol;

    //lets scale the vol up to a 0-1 range
    outVol = ofMap(smoothedVol, 0.0, 0.17, 0.0, 1.0, true);

    return outVol;
}


//--------------------------------------------------------------
void classAudioIn::audioIn(float * input, int bufferSize, int nChannels){

	if ( audioInEnabled ){
        float curVol = 0.0;
        int numCounted = 0;	 // samples are "interleaved"

        //lets go through each sample and calculate the root mean square which is a rough way to calculate volume
        for (int i = 0; i < bufferSize; i++){
            left[i]		= input[i*2]*0.5;
            right[i]	= input[i*2+1]*0.5;

            curVol += left[i] * left[i];
            curVol += right[i] * right[i];
            numCounted+=2;
        }

        //this is how we get the mean of rms :)
        curVol /= (float)numCounted;

        // this is how we get the root of rms :)
        curVol = sqrt( curVol );

        smoothedVol *= 0.93;
        smoothedVol += 0.07 * curVol;
    }
}

running the application I get the following info on startup:

[notice ] ofxBox2d:: - world created -
[notice ] ofRtAudioSoundStream: device 0 hw:HDA Intel,0
[notice ] ofRtAudioSoundStream: ----* default ----*
[notice ] ofRtAudioSoundStream: maximum output channels 8
[notice ] ofRtAudioSoundStream: maximum input channels 2
[notice ] ofRtAudioSoundStream: -----------------------------------------
[notice ] ofRtAudioSoundStream: device 1 hw:HDA Intel,1
[notice ] ofRtAudioSoundStream: maximum output channels 2
[notice ] ofRtAudioSoundStream: maximum input channels 0
[notice ] ofRtAudioSoundStream: -----------------------------------------
[notice ] ofRtAudioSoundStream: device 2 hw:HDA Intel,2
[notice ] ofRtAudioSoundStream: maximum output channels 0
[notice ] ofRtAudioSoundStream: maximum input channels 2
[notice ] ofRtAudioSoundStream: -----------------------------------------
[notice ] ofRtAudioSoundStream: device 3 default
[notice ] ofRtAudioSoundStream: maximum output channels 32
[notice ] ofRtAudioSoundStream: maximum input channels 32
[notice ] ofRtAudioSoundStream: -----------------------------------------

thank you in advance for your help

In the code you posted, the audioIn() function of your baseApp will start being called after the soundStream.setup(...) call, and then if you want to pass it on to your classAudioIn, then you’d need to explicitly call out to it from the base app.

How you’ve set it up is a bit convoluted, though… can you post more of the code? If all you did was create a new class called classAudioIn, then it’s going to exist entirely outside of the openFrameworks ecosystem (i.e. setup() update() and draw() aren’t going to be called either).

there’s 2 things you can do if you want to get the audioIn callback in a class that is not the main app.

  • Make it inherit from ofBaseSoundInput/ofBaseSoundOutput like:
class classAudioIn: public ofBaseSoundInput{
    ....

and then call soundStream.setup() like:

soundStream.setup(0, 2, 44100, bufferSize, 4);
soundStream.setIn(this);
  • Or from testApp::audioIn call it’s audioIn
1 Like

Hi there,
The code is like this (i’m highlighting some important stuff):

main.cpp

#include "ofMain.h"
#include "testApp.h"
#include "ofAppGlutWindow.h"
//======================================================================
int main( ){
    ofAppGlutWindow window;
    ofSetupOpenGL(&window, 1920,1080,OF_FULLSCREEN); // <-------- setup the GL context
    
	ofRunApp(new testApp());
}

testApp.h

#pragma once

#include "ofMain.h"
#include "ofxOsc.h"
#include "ofxSvg.h"
#include "audioIn.h" <========================================

//...maaaany more #includes


class testApp : public ofBaseApp
{

public:

    void setup();
    void update();
    void draw();

    //audio input related variabled
    classAudioIn cAudioIn;  <=====================================
    float audioVol; //store current volume locally

    //...many more member functions and variables.

};

testApp.cpp

#include "testApp.h"

void testApp::setup()
{
    cAudioIn.setup(this); //<======"this" is the baseApp and I'm sending it
                         // <======to "classAudioIn". is that possible?

    audioVol = 0.0 ;
}

void testApp::update()
{
    if (cAudioIn.audioInEnabled)
        audioVol = cAudioIn.updateVol(); //<=====updateVol is really just an "update" fn=======

    //...then use the audioVol value elsewhere in update...

}

afterwards the audioIn.cpp/.h files are as I posted them above.

so basically, the audioIn function resides in a non ofBaseApp class.
I am stating that because you said

In the code you posted, the audioIn() function of your baseApp

Could this be the reason?

I just saw the answer Arturo.
thank you for the solution!

soundStream.setIn(this)

produces an error.
It claims member “setIn” does not exist…

EDIT: and it doesn’t seem to exist indeed in latest version of OF .
I fgrep for “setIn” under “libs/openFrameworks” to no avail.

EDIT2:
ok the actual command is

ofSoundStream::setInput(ofBaseSoundInput *soundInput)

so this can be used to acquire audio using the 1st of arturo’s 2 possible solutions.