I’m trying to get the magnitude of a song played through the ofSoundPlayer
. What I would like to have is the RMS value as explained in the chapter “Reacting to live audio” of the ofBook http://openframeworks.cc/ofBook/chapters/sound.html
What I’ve done until now is to follow the example soundPlayerFFTExample
.
In my .h file
ofSoundPlayer soundPlayer;
float * fftSmoothed;
int nBandsToGet;
In the setup method
void ofApp::setup(){
soundPlayer.load("chainsaw-01.mp3");
nBandsToGet = 6; // i do not care about all the frequencies
fftSmoothed = new float[8192];
for (int i = 0; i < 8192; i++){
fftSmoothed[i] = 0;
}
float * val = ofSoundGetSpectrum(nBandsToGet);// request 6 values for fft
for (int i = 0;i < nBandsToGet; i++){
fftSmoothed[i] *= 0.96f;
if (fftSmoothed[i] < val[i]) fftSmoothed[i] = val[i];
}
soundPlayer.play();
}
void ofApp::update(){
ofSoundUpdate();
float * val = ofSoundGetSpectrum(nBandsToGet);
for (int i = 0;i < nBandsToGet; i++){
fftSmoothed[i] *= 0.96f;
if (fftSmoothed[i] < val[i]) fftSmoothed[i] = val[i];
}
cout << ofToString(fftSmoothed[0]*2) << endl;
}
The cout
is printing out values that are changing accordingly to the volume, that is less or more what I was looking for, but I’ve some questions:
- Is this really the simplest way to get the “loudness” out of a played audio file?
- Does it makes sense to reduce the bands to
nBandsToGet = 1
? As I’m interested only in the volume, and the first band contains enough information? - The values are scattering, is there a way to have a smoother differences through the values?
4)How to get RMS out of this? in the ofBook there is this snippet:
// modified from audioInputExample
float rms = 0.0;
int numCounted = 0;
for (int i = 0; i < bufferSize; i++){
float leftSample = input[i * 2] * 0.5;
float rightSample = input[i * 2 + 1] * 0.5;
rms += leftSample * leftSample;
rms += rightSample * rightSample;
numCounted += 2;
}
rms /= (float)numCounted;
rms = sqrt(rms);
// rms is now calculated
The problem is that I do not have a buffer to iterate.
Any suggestion is more than welcome