SFML community forums

Help => Audio => Topic started by: dk123 on January 30, 2017, 02:36:36 am

Title: Getting audio amplitude
Post by: dk123 on January 30, 2017, 02:36:36 am
Hi, I'm trying to implement a function that opens / closes the character's mouth (graphic) depending on the volume (amplitude) of the voice file currently being played.

I'm trying to read in whether the audio is above a certain amplitude (then thus switch to the open mouth graphic), or not (then thus switch to the closed mouth graphic).

I wasn't sure how to do this with SFML - any ideas?
Title: Re: Getting audio amplitude
Post by: Hapax on January 30, 2017, 03:07:59 am
The amplitude of a sound wave can be calculated using multiple methods. The main two are "peak" and "RMS".

For both, you need to first take a number of consecutive samples. This should be at least the number of samples it takes for a single cycle of the wave.

Then, to calculate the peak amplitude, you simply take the highest absolute value of all of those samples.

RMS (Root Means Square) (https://en.wikipedia.org/wiki/Root_mean_square), however, is more complicated.

For this particular task, peak should be sufficient.

Note that sample values can be negative. The absolute value (converts negative to its positive equivalent) will give you the value to compare here.
Title: Re: Getting audio amplitude
Post by: dk123 on January 30, 2017, 03:48:54 am
I'm unsure of how to take samples of sf::audio, could you provide a short example?
Title: Re: Getting audio amplitude
Post by: Hapax on January 30, 2017, 02:03:48 pm
The samples are the values that represent the sound. Assuming you are using sf::Sound (http://www.sfml-dev.org/documentation/2.4.1/classsf_1_1Sound.php) with sf::SoundBuffer (http://www.sfml-dev.org/documentation/2.4.1/classsf_1_1SoundBuffer.php), the samples will be available in the sound buffer using getSamples() (http://www.sfml-dev.org/documentation/2.4.1/classsf_1_1SoundBuffer.php#a4ba0c1e5b5be500af42de30b1360eb2e).