Quantcast
Channel: beginners - openFrameworks
Viewing all articles
Browse latest Browse all 4929

Questions about code whose radius changes depending on the size of the sound

$
0
0

@jewel wrote:

Hi I want to get a number for the volume of sound. So I found an example and entered the code.

 for (int i = 0; i<bufferSize;i++){
  
    left = input[i*2]*0.5;
    right = input[i*2+1]*0.5;

    volume += left * left;
    volume += right * right;
}
volume = sqrt(volume/(bufferSize*2));
}

But I don’t know the meaning of the audioIn function.

I wonder why input[i*2] means and why 0.5 times and what the formula for volume means.
For your information, I know the sample rate and buffer size.

And I wonder if there is a way to make the circle grow bigger and smaller when the sound gets louder. I am thinking about it, too.

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

ofSoundStream soundInput;

float volume;

};


#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
soundInput.setup(this,0,2,44100,256,4);
}

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

}

//--------------------------------------------------------------
void ofApp::draw(){
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
ofDrawBitmapString("Volume = " + to_string(volume) ,0, 50);
ofDrawCircle(0,0,ofMap(volume,0.0,2.0,60.0,300.0));
}

//--------------------------------------------------------------
void ofApp::audioIn(float *input, int bufferSize, int nChannels){
float left,right;
volume = 0.0;
for (int i = 0; i<bufferSize;i++){
    left = input[i*2]*0.5;
    right = input[i*2+1]*0.5;
    
    volume += left * left;
    volume += right * right;
}
volume = sqrt(volume/(bufferSize*2));
}

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 4929

Trending Articles