audioRequested in a class..... getting sound working outside of testApp

hi guys i am writing an Audio Visual synthesiser in oF and would like to have the video and the audio is their own classes.
At the moment everything us working but the audio is not going to the sound card for some reason.

I have posted a super modified version of what i am trying to achieve below. Would anyone know how to get audio playing through the sound card if it’s outside of testApp?

here is what i have so far.

//testApp:

  
#ifndef _TEST_APP  
#define _TEST_APP  
  
#include "ofMain.h"  
#include "AudioPlayer.h"  
  
class testApp : public ofBaseApp{  
	  
public:  
	void setup();  
	void update();  
	void draw();	  
	void keyPressed  (int key);  
  
	AudioPlayer audio;  
};  
  
#endif  
  

  
#include "testApp.h"  
//--------------------------------------------------------------  
void testApp::setup(){  
	  
	ofBackground(0, 0, 0);  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
}  
  
//--------------------------------------------------------------  
void testApp::keyPressed(int key){  
}  
  

audioClass:

  
#ifndef _AUDIOPLAYER  
#define _AUDIOPLAYER  
  
#include "ofMain.h"  
  
class AudioPlayer {  
	  
	public :  
		void setup();	  
		void audioRequested (float * output, int bufferSize, int nChannels);  
//		void audioRequested(ofAudioEventArgs & audio);  
	  
	private :   
	  
		ofSoundStream soundStream;  
	  
		float 	pan;  
		int	sampleRate;  
		float 	volume;  
	  
		vector <float> lAudio;  
		vector <float> rAudio;  
};  
  
#endif  

  
#include "AudioPlayer.h"  
  
//--------------------------------------------------------------  
void AudioPlayer::setup()  
{  
  
	int bufferSize		= 512;  
	sampleRate 			= 44100;  
	volume				= 0.1f;  
	  
	lAudio.assign(bufferSize, 0.0);  
	rAudio.assign(bufferSize, 0.0);  
	  
	soundStream.setup(this, 2, 0, sampleRate, bufferSize, 4);  
		  
}  
  
//--------------------------------------------------------------  
void AudioPlayer::audioRequested (float * output, int bufferSize, int nChannels){  
//void AudioPlayer::audioRequested 	(ofAudioEventArgs &audio){  
	  
	float leftScale = 1 - pan;  
	float rightScale = pan;  
	  
	for (int i = 0; i < bufferSize; i++){  
		lAudio[i] = output[i*nChannels    ] = ofRandom(0, 1) * volume * leftScale;  
		rAudio[i] = output[i*nChannels + 1] = ofRandom(0, 1) * volume * rightScale;  
	}  
}  
  

sound input and sound output works via the functions audioIn and audioOut, which are part of ofBaseApp. these functions are called regularly by oF. so you could implement/override audioOut in your testApp (just as in the audioOutputExample) and redirect the call to your audioRequested(…).

  
void testApp::audioOut(float * output, int bufferSize, int nChannels){  
     audio.audioRequested(output,bufferSize,nChannels);  
}  

or you use arturos better solution :slight_smile:

you need to make that class inherit from ofBaseSoundOutput:

  
  
class AudioPlayer: public ofBaseSoundOutput{  
...  
  

then call the setup version that receives no base app and call setOutput:

  
  
soundStream.setup(2, 0, sampleRate, bufferSize, 4);  
soundStream.setOutput(this);  
  

2 Likes

*edit*

Thanks guys I got working following your suggestions and looking at the ofxPd example.
Code is below of a simple demo if someone else is trying to figure this out.

testApp.h

  
#ifndef _TEST_APP  
#define _TEST_APP  
  
#include "ofMain.h"  
#include "AudioPlayer.h"  
  
class testApp : public ofBaseApp{  
	  
public:  
	void setup();  
	void audioRequested(float * output, int bufferSize, int nChannels);  
	  
	AudioPlayer audio;  
	ofSoundStream soundStream;  
  
};  
  
#endif  
  

testApp.cpp

  
#include "testApp.h"  
  
//--------------------------------------------------------------  
void testApp::setup(){  
	  
	ofBackground(0, 0, 0);  
	  
	int bufferSize		= 512;  
	int sampleRate 		= 44100;  
  
	soundStream.setup(2, 0, sampleRate, bufferSize, 4);    
	soundStream.setOutput(this);    
  
}  
  
//--------------------------------------------------------------  
void testApp::audioRequested(float * output, int bufferSize, int nChannels) {  
	audio.audioRequested(output, bufferSize, nChannels);  
}  

AudioPlayer.h

  
#ifndef _AUDIOPLAYER  
#define _AUDIOPLAYER  
  
#include "ofMain.h"  
  
class AudioPlayer: public ofBaseSoundOutput{    
	  
	public :  
		void setup();	  
		void audioRequested (float * output, int bufferSize, int nChannels);  
};  
  
#endif  

AudioPlayer.cpp

  
#include "AudioPlayer.h"  
  
//--------------------------------------------------------------  
void AudioPlayer::setup()  
{  
}  
  
//--------------------------------------------------------------  
void AudioPlayer::audioRequested (float * output, int bufferSize, int nChannels){  
	  
	for (int i = 0; i < bufferSize; i++){  
		output[i*nChannels    ] = ofRandom(0, 1) * 0.3;  
		output[i*nChannels + 1] = ofRandom(0, 1) * 0.3;  
	}  
}