Catch amplitude in period of time of any playing sounds

Apr 19, 2015 at 1:54pm
Hello, I really need your help.
So, I decided to create a simple game, using SFML library.
In my game behavior of any objects will change in real time depending of the music amplitude (Or intensity. For example, dubstep beat means a great jump)
I don't understand:
How to catch values of amplidute of playing sound(every 1/4 second?). I need any method (good or bad, not important), but I need this amplitude values. How can I do this?
Sorry if my English was bad.
Apr 19, 2015 at 6:43pm
The easiest (but not most accurate) way is to just examine a chunk of audio and use the distance between the highest and lowest peaks to determine overall amplitude.

For example if you have the below samples:

1
2
3
4
5
6
7
15
0
134
-15
-150
137
15


Highest peak = 137
Lowest peak = -150

So your overall amplitude would be the distance between them:

Overall amp = 137 - (-150) = 287
Apr 20, 2015 at 5:57am
But how to realize that by code?
Apr 20, 2015 at 6:18am
Are you asking how to GET the audio data? Or are you asking what to do with the audio data once you have it?
Apr 20, 2015 at 2:06pm
I asking how to GET the audio data, or how to catch it, or simply extract amplitude from audio file. What libraries I should use? I'm new in C++ and in programming obviously (a few months) I just simply need a values for making dynamic world in my game. Is it really I need to write a lot of code? Do you know a game called "Audiosufr". That's simillary.
I planned that user can put mp3 file in special folder, then program reads it, analyzes and make something based on music amplitude.
Apr 20, 2015 at 4:37pm
I asking how to GET the audio data, or how to catch it, or simply extract amplitude from audio file.


You can use SFML to load an audio file (as long as it supports the format -- I'm not sure it supports MP3 because of license issues, but it definitely supports .ogg). Just use the SoundBuffer class:

1
2
3
4
5
6
sf::SoundBuffer sndbuf;
sndbuf.loadFromFile( "yourfile.ogg" );

// then you can get the samples:
const sf::Int16* samples = sndbuf.getSamples();
std::size_t samplecount = sndbuf.getSampleCount();


'samples' can be treated like an array.
Topic archived. No new replies allowed.