Legacy GM Is there any way I can get how loud a sound is?

H

HavanHUN

Guest
I mean how LOUD is it at the moment. Just like if you look at the Sound Mixer in Windows, you see the green bar to see how loud an application is. I wanna get a value like that. Is that possible somehow?
 

jazzzar

Member
I mean how LOUD is it at the moment. Just like if you look at the Sound Mixer in Windows, you see the green bar to see how loud an application is. I wanna get a value like that. Is that possible somehow?
audio_sound_get_gain(index of the sound to get the gain of)
check the manual for more info about this :)
 
H

HavanHUN

Guest
That's not what I meant.
It stays the same.

Let's say there's a song. It has parts where it's getting louder, so the value should get higher too.
So I wanna get the value of the ACTUAL volume what is playing.
Any ideas?
 

Nallebeorn

Member
audio_sound_get_gain() will only get the sound's volume relative to other sounds in your game, not the actual amplitude at the current position.
There's no easy way to do what you want, but it would be possible by using audio buffers. Basically, you would load a WAVE file into a buffer. This will be somewhat more complicated than just doing buffer_load("sound.wav") since WAVEs contain some metadata and headers, though. You could convert your audio files into raw, headerless PCM-audio (Audacity can do this), or you could write a system to parse the additional data (which would allow you to determine sample rate etc. automatically). There's an example on /r/GameMaker about doing that, but the poster's Dropbox has apparently been disabled...
Anyway, once you have an audio buffer, you can create a sound from the buffer with audio_create_buffer_sound(), which you can play as normally – and since you have all the audio data in a buffer, you can quite easily check the amplitude of the audio at a given moment. This should be as simple as
Code:
volume = buffer_peek(buffer, rate * position_in_seconds, buffer_u16) / buffer_sizeof(buffer_u16); // If you have an 8-bit sound file, you'd use buffer_u8 instead of buffer_u16, of course.
Which would return a value from 0 to 1 (where 1 is the highest amplitude possible). But don't quote me on that, I haven't tested it. You can get the current position of a sound that is playing (in seconds, conveniently) with audio_sound_get_track_position()
 

Fredrik

Member
If you're thinking about the overall volume of your entire game (not just spesific sounds or music) you can use audio_master_gain(volume 0 - 1); with 1 being max volume and 0 being muted.

I guess if you want a read variable from that you can do something like var_volume = audio_master_gain(vol);

and if you're gonna display it: draw_text(x,y,var_volume);
 
Top