Get AudioOut buffer

Hi. I’m working some project that image distortion with sound interaction.
I’m from South korea, so some of english is poor. please understand that .

I want to access AudioOut buffer, and get soundVolume, from ofSoundPlayer (actually any Sound from App to SoundCard).

but it doesn’t work.
I already read the document, and it said

audioOut() method is called when the system needs one buffer worth of audio to send to the sound card. The method sends the array of floating-point information that represents the buffer of audio data, the size of the buffer, and the number of channels: void audioOut() (float * output, int bufferSize, int nChannels) To have the audioOut() callback triggered by the system, you would need to call ofSoundStreamSetup() with one or two channels in the output. If you want to alter the data before its sent to the sound buffer, you must do it within this method.

I tried ofSoundStreamSetup(), and with instance of “ofSoundStream”. but nothing happen.

or maybe I’m misunderstanding usage of audioOut().
I checked other example. is “audioOut()” method can only writable?
then, how can I get volume, from any sound, just before passed to sound card??

please help.

ofApp.h

#include "ofMain.h"
#include "Sample.h"
#define MESH_RESOLUTION	100

class ofApp : public ofBaseApp{

public:
	void setup();
	void update();
	void draw();

	// audio Input
	void audioOut(float *output, int bufferSize, int nChannels);
	void audioRequested(float *output, int bufferSize, int nChannels);

	vector <float> left;
	vector <float> right;
	vector <float> volHistory;
	
	int bufferCounter;
	int drawCounter;
	
	float smoothedVol;
	float scaledVol;
	
	ofSoundStream soundStream;
	ofSoundPlayer sample;
		
	// sound reactive mesh
	ofMesh mesh;	// distortion Mesh
	ofImage img;	// image container
		
};

ofApp.cpp

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
	ofSetVerticalSync(true);
	ofBackground(0);
	
	// 2 output channels,
	// 0 input channels
	// 44100 samples per second
	// 256 samples per buffer
	// 4 num buffers (latency)
	
//	soundStream.listDevices();
	
	//if you want to set a different device id
	//soundStream.setDeviceID(0); //bear in mind the device id corresponds to all audio devices, including  input-only and output-only devices.
	
	int bufferSize = 256;
		
	left.assign(bufferSize, 0.0);
	right.assign(bufferSize, 0.0);
	volHistory.assign(400, 0.0);
	
	bufferCounter	= 0;
	drawCounter		= 0;
	smoothedVol     = 0.0;
	scaledVol		= 0.0;
	

	
	sample.setLoop(true);
	sample.loadSound("bmo.wav");
	sample.play();


//	soundStream.setup(<#ofBaseApp *app#>, <#int outChannels#>, <#int inChannels#>, <#int sampleRate#>, <#int bufferSize#>, <#int nBuffers#>)
	soundStream.setup(this, 2, 0, 44100, bufferSize, 4);
	soundStream.setOutput(this);
	
//	ofSoundStreamSetup(0, 2, this, 44100, bufferSize, 4);

	for(int y = 0; y < MESH_RESOLUTION-1; y++){
		for(int x = 0; x < MESH_RESOLUTION-1; x++) {
			mesh.addIndex(x+y*MESH_RESOLUTION);
			mesh.addIndex((x+1)+y*MESH_RESOLUTION);
			mesh.addIndex(x+(y+1)*MESH_RESOLUTION);
			
			mesh.addIndex((x+1)+y*MESH_RESOLUTION);
			mesh.addIndex((x+1)+(y+1)*MESH_RESOLUTION);
			mesh.addIndex(x+(y+1)*MESH_RESOLUTION);
		}
	}

	// load image for texture
	img.loadImage("texture.jpg");
}

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


//	scaledVol = ofMap(smoothedVol, 0.0, 0.17, 0.0, 1.0, true);
//	smoothedVol = ofMap(*ofSoundGetSpectrum(1000), 0.0, 0.17, 0.0, 1.0, true);
	volHistory.push_back(scaledVol);
	
	if (volHistory.size() >= 400) {
		volHistory.erase(volHistory.begin(), volHistory.begin()+1);
	}
	
	cout << smoothedVol << endl;
	
	// distortion, with  perlin noise
	for(int y=0; y < MESH_RESOLUTION; y++){
		for(int x =0; x < MESH_RESOLUTION; x++){
			float a = x * .01;
			float b = y * .01;
			float c = ofGetFrameNum() / 50.0;
			float noise = ofNoise(a,b,c)*smoothedVol*10;
			mesh.addVertex(ofPoint(
								   ofMap(ofGetWidth()/MESH_RESOLUTION*x+noise, 0, ofGetWidth(), -100, ofGetWidth()+100),
								   ofMap(ofGetHeight()/MESH_RESOLUTION*y+noise,0, ofGetHeight(), -100, ofGetHeight()+100),
								   0)
						   );
			mesh.addTexCoord(ofPoint(1200/MESH_RESOLUTION*x, 1200/MESH_RESOLUTION*y));
		}
	}
}

//--------------------------------------------------------------
void ofApp::draw(){
	img.bind();
	mesh.draw();
	img.unbind();
	mesh.clearTexCoords();
	mesh.clearVertices();
}

//void ofApp::audioIn(float *input, int bufferSize, int nChannels){
//	float curVol = 0.0;
//	
//	// samples are "interleaved"
//	int numCounted = 0;
//	
//	// 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;
//	
//	bufferCounter++;
//}

void ofApp::audioOut(float *output, int bufferSize, int nChannels){
		
	float curVol = 0.0;
	
	// samples are "interleaved"
	int numCounted = 0;
	
	// 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] = output[i*nChannels]*0.5;
		right[i] = output[i*nChannels+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;
	
	bufferCounter++;
	
}


void ofApp::audioRequested(float *output, int bufferSize, int nChannels){
//	float curVol = 0.0;
//
//	// samples are "interleaved"
//	int numCounted = 0;
//
//	// 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++) {
//		//		sample.play();
//		left[i] = output[i*nChannels]*0.5;
//		right[i] = output[i*nChannels+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;
//
//	bufferCounter++;

}

You need to use ofxMaxim, code should llok something like this

// in setup
sample1.load(ofToDataPath("beat2.wav"));

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

	float curVol = 0.0;

	// samples are "interleaved"
	int numCounted = 0;

	// 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++) {
		sample=sample1.play(1.);		
        channel1.stereo(sample,outputs1,0.5);
		output[i*nChannels    ] = outputs1[0];		
        output[i*nChannels + 1] = outputs1[1];
        left[i] = output[i*nChannels]*0.5;
		right[i] = output[i*nChannels+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;

	bufferCounter++;

}