ofSoundStream OF_CORE_EVENTS_ENABLED

Hi there,

I have a question regarding ofSoundStream. What is this OF_CORE_EVENTS_ENABLED?
I’m creating a class that uses ofSoundStream and I’d like to be able to register it to receive the audioReceived event. Looks like OF_CORE_EVENTS_ENABLED needs to be defined if I want this event sent to anything that isn’t an ofBaseApp.
Is it safe to just define OF_CORE_EVENTS_ENABLED in the ofSoundStream.h or in OfConstants.h? Seems to work that way.
Is there any way to define that value without messing with the original OF code?

hey jaime

that is actually a bug, that define was called:

#ifdef OF_USING_POCO

in the final 006, will change it for 0061, by now, just change all that with OF_USING_POCO

arturo

Ah, that makes sense. Thanks. :slight_smile:

hi jaime,
any chance you got ofSoundStream events sending to your custom class?
its something im trying to work out at the moment…
if you could share the code, that woud be great!

thx,
L.

julapy, been a while w/ no answer so i’m guessing you figured this out
but for the good of the hive: you just need to derive your custom class from ofBaseApp,
so your class declaration (in e.g. myClass.h) would look something like:

  
class myClass : public ofBaseApp {  
...  
}  

lots more on inheritance at http://www.learncpp.com/cpp-tutorial/112-basic-inheritance-in-c/

cheers,
m

hi

you don’t need to inherit from ofBaseApp and indeed ofBaseApp is only meant to be used for the testApp. if you want to get events in any class just register to them using ofAddListener:

  
ofAddListener(ofEvents.audioReceived,this,&myClass::audioReceived);  

where audioReceived is a method in myClass with the following signature:

  
void audioRecieved(ofAudioEventArgs & audio){  
}  

oops, guess i was a little hasty there,
was getting an error like this:

error: no matching function for call to ‘ofSoundStreamSetup(int, int, myClass* const, int, int, int)’
note: candidates are: void ofSoundStreamSetup(int, int, ofBaseApp*)
note: void ofSoundStreamSetup(int, int, int, int, int)
note: void ofSoundStreamSetup(int, int, ofBaseApp*, int, int, int)

and extending ofBaseApp killed the errors…

but i hadn’t actually done anything w/ the data yet
and i am now seeing that it wouldn’t have worked.

hadn’t come across ofAddListener before, but i’ll look into that…

cheers,
mark

so, going a little further with this,
just trying to put the audioInputExample in a class…

i’ve got this in testApp.cpp:

  
void testApp::setup(){  
	ofBackground(255,255,255);	  
	ofSoundStreamSetup(0,2,this, 44100, 256, 4);	  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
	_audioInput.draw();  
}  
  

this in the header of a custom AudioInput class (AudioInput.h):

  
#ifndef _AUDIO_INPUT  
#define _AUDIO_INPUT  
  
#include "ofMain.h"  
  
class audioInput{  
  
	public:  
		audioInput();  
  
		// public fcns  
		void setup();  
		void update();  
		void draw();  
  
		void audioReceived(ofAudioEventArgs &audio);  
	  
		float *left;  
		float *right;  
	  
};  
 #endif  

and this in its .cpp file:

  
#include "audioInput.h"  
  
//--------------------------------------------------------------  
audioInput::audioInput() {  
	left = new float[256];  
	right = new float[256];  
	ofAddListener(ofEvents.audioReceived, this, &audioInput::audioReceived);  
}  
  
//--------------------------------------------------------------  
void audioInput::setup(){  
}  
  
//--------------------------------------------------------------  
void audioInput::update(){  
}  
  
//--------------------------------------------------------------  
void audioInput::draw(){  
	  
	// draw the left:  
	ofSetColor(0x333333);  
	ofRect(100,100,256,200);  
	ofSetColor(0xFF0000);  
	for (int i = 0; i < 256; i++){  
		ofLine(100+i,200,100+i,200+left[i]*100.0f);  
	}  
	  
	// draw the right:  
	ofSetColor(0x333333);  
	ofRect(600,100,256,200);  
	ofSetColor(0xFF0000);  
	for (int i = 0; i < 256; i++){  
		ofLine(600+i,200,600+i,200+right[i]*100.0f);  
	}  
	  
}  
  
//--------------------------------------------------------------  
void audioInput::audioReceived(ofAudioEventArgs &audio){  
  
	// samples are "interleaved"  
	for (int i=0; i < audio.bufferSize; i++){  
		left[i] = audio.buffer[i*2];  
		right[i] = audio.buffer[i*2+1];  
	}  
  
}  
  

audio.bufferSize & audio.nChannels respond accordingly,
but i’m getting no love from audio.buffer,
which is of course the interesting part.

no problem getting the the buffer data in testApp, fwiw…

audioInputClass.zip

oops, there’s a bug in ofSoundStream, in line 35, this:

  
  
		OFSAptr->audioReceived(fPtrIn, bufferSize, nInputChannels);  
		memset(fPtrIn, 0, bufferSize * nInputChannels * sizeof(float));  
		#ifdef OF_USING_POCO  
			audioEventArgs.buffer = fPtrIn;  
			audioEventArgs.bufferSize = bufferSize;  
			audioEventArgs.nChannels = nInputChannels;  
			ofNotifyEvent( ofEvents.audioReceived, audioEventArgs );  
		#endif  

should be:

  
  
		OFSAptr->audioReceived(fPtrIn, bufferSize, nInputChannels);  
		#ifdef OF_USING_POCO  
			audioEventArgs.buffer = fPtrIn;  
			audioEventArgs.bufferSize = bufferSize;  
			audioEventArgs.nChannels = nInputChannels;  
			ofNotifyEvent( ofEvents.audioReceived, audioEventArgs );  
		#endif  
		memset(fPtrIn, 0, bufferSize * nInputChannels * sizeof(float));  

yep that was it, thx!
is there a bug report somewhere
so we can make sure that’s fixed
in the next release?

not sure how that works…

cheers,
m

yes, you can report bugs here:

http://github.com/openframeworks/openFrameworks/issues

but it’s already reported and fixed ; )

http://github.com/openframeworks/openFr-…-#issue/130

Hi all, I wanted to do the same thing with iOS, but it didn’t seem to have the exact functionality as it does in the main OpenFrameworks. I got stuck for a long while, so if you need to do this in iOS, add the listener like Zach said but also add this in testApp:

(thank you James george!)

  
  
void testApp::audioReceived(float * input, int bufferSize, int nChannels) /* input method */  
{   
    ofAudioEventArgs args;  
    args.buffer = input;  
    args.bufferSize = bufferSize;  
    args.nChannels = nChannels;  
    ofNotifyEvent(ofEvents.audioReceived, args);  
  
}