### Author Topic: Getting audio amplitude  (Read 2119 times)

0 Members and 1 Guest are viewing this topic.

#### dk123

• Newbie
• Posts: 49
##### Getting audio amplitude
« 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?

#### Hapax

• Hero Member
• Posts: 3075
• My number of posts is shown in hexadecimal.
##### Re: Getting audio amplitude
« Reply #1 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), 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.
Selba Ward - SFML drawables
Kairos - Timing Library
Rectangular Boundary Collision - Rectangular SAT Collision

#### dk123

• Newbie
• Posts: 49
##### Re: Getting audio amplitude
« Reply #2 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?

#### Hapax

• Hero Member
• Posts: 3075
• My number of posts is shown in hexadecimal.
##### Re: Getting audio amplitude
« Reply #3 on: January 30, 2017, 02:03:48 pm »
The samples are the values that represent the sound. Assuming you are using sf::Sound with sf::SoundBuffer, the samples will be available in the sound buffer using getSamples().
Selba Ward - SFML drawables
Kairos - Timing Library
Rectangular Boundary Collision - Rectangular SAT Collision