• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

[possible bug?] unable to use audio_sound_gain with a variable

PixelLicker

Member
Hello,

I am relatively new to using the audio functions of GM, but I found two issues that I am not sure are bugs or not.
1. I am setting a sound to a local var with:
GML:
currentBGM = audio_play_sound(snd_area00_wave_bgm_loop, 1, true);
Then I am trying to set the gain with:
Code:
audio_sound_gain(currentBGM, 1, 500);
I have checked the local var and it is being set to a value, but the code is not working. Is this a bug or am I simply not understanding how to store the index of that sound instance?

2. The docs for audio_sound_gain have a few formatting issues. The example there states:
Code:
if val
   {
   var snd = audio_play_sound(snd_fountain);
   audio_sound_gain(snd, 0, 0);
   audio_sound_gain(snd, 1, 5000);
   }
The indents seem off and the first line is missing all the parameters of that expression.

If anyone can clarify the issue with using the stored index, I would really appreciate it.
 
The manual seems to have a mistake in it, it's missing a couple arguments. I'm guessing that example doesn't even compile.

GML:
if val
   {
   var snd = audio_play_sound(snd_fountain, 1, false);
   audio_sound_gain(snd, 0, 0);
   audio_sound_gain(snd, 1, 5000);            //This will take 5 seconds to reach full volume, FIY
   }
 
Last edited:

kburkhart84

Firehammer Games
The manual seems to have a mistake in it, it's missing a couple arguments. I'm guessing that example doesn't even compile.

GML:
if val
   {
   var snd = audio_play_sound(snd_fountain, 1 false);
   audio_sound_gain(snd, 0, 0);
   audio_sound_gain(snd, 1, 5000);            //This will take 5 seconds to reach full volume, FIY
   }
And YOU forgot a comma between arguments in the audio_play_sound() function. You are as bad as the manual :p :p:eek: I'm glad I'm the perfect being and never make mistakes ;)

@PixelLicker The way you are doing this is correct, and works fine for me...once you fix the function arguments. There is nothing wrong with using local variables to store sound instances like this, as long as you don't need to access it later on like a looping sound you need to stop or pause.
 

xDGameStudios

GameMaker Staff
GameMaker Dev.
@PixelLicker what you are doing is completely okay :)


GML:
currentBGM = audio_play_sound(snd_area00_wave_bgm_loop, 1, true);
// ....
audio_sound_gain(currentBGM, 1, 500);
"It's not working" you say but the question is, what is not working? are you trying to do? is the sound not playing?

As I don't know what's "not working" I'll will through some things you might need to consider

[DESKTOP EXPORTS]
1) By default the gain of the sound is already set to 1
2) The gain goes from 0-1
3) So your code will not change the gain if that is what you mean :)

[HTML5]
1) In HTML playing sound is not that straight forward
2) you need to know if WebAudio is ready before making the call to audio_play_sound
3) For that you can look into the manual for System Asynchronous event[/HTML]
 

xDGameStudios

GameMaker Staff
GameMaker Dev.
If done right, that 500 argument means it would go from 0 to 1 in 500 milliseconds, basically making the sound fade in. I do it all over my sound system.
Well the OP @PixelLicker didn't stated he was setting the gain to 0 to begin with, he just said he did:
GML:
audio_sound_gain(currentBGM, 1, 500);
the sound will fade in if you use a combination of:
GML:
audio_sound_gain(currentBGM, 0, 0);
audio_sound_gain(currentBGM, 1, 500);
 

kburkhart84

Firehammer Games
Well the OP @PixelLicker didn't stated he was setting the gain to 0 to begin with, he just said he did:
This is true. I kinda inferred but didn't verify since they referenced the manual that does exactly that(albeit with a couple arguments missing from the play function call).
 

PixelLicker

Member
ok, I think I found a clue and possible cause to my problem. I am using the gain to fade sounds in and out depending on another variable. When I was testing this function with the variable as the argument (instead of the sound asset name) I had done it for the fade out but for the fade in, I was still using the sound name in the argument. so one was working but the other wasn't. My guess is that the sound index changes when you use use the audio_sound_gain function. Is that possibly happening? Because when I go and replace all arguments with the variable, it does indeed work as expected.

edit: And to clarify, I am starting the gain at 1, then changing the gain with this function once it is already playing.
 

kburkhart84

Firehammer Games
ok, I think I found a clue and possible cause to my problem. I am using the gain to fade sounds in and out depending on another variable. When I was testing this function with the variable as the argument (instead of the sound asset name) I had done it for the fade out but for the fade in, I was still using the sound name in the argument. so one was working but the other wasn't. My guess is that the sound index changes when you use use the audio_sound_gain function. Is that possibly happening? Because when I go and replace all arguments with the variable, it does indeed work as expected.

edit: And to clarify, I am starting the gain at 1, then changing the gain with this function once it is already playing.
The gain changing function doesn't change the ID of the sound at all. However, since you are using the single instance of the sound when using the return of the play function, it will only change that instance of the sound. If your argument you are referring to is the sound resource name, then indeed, changing the gain of that one will change for ALL instances of it in the future. Is it possible that this is what you are running into?
 

PixelLicker

Member
I am only creating one instance of the sound, and my intention was to only access it through the id stored in a variable. but when I was testing my code, I had been accessing the sound through both the stored id and its name. I had a setup like this:

GML:
if (global.musicState == "INTRO") && (!audio_is_playing(snd_area00_wave_bgm_loop))
{
    currentBGM = audio_play_sound(snd_area00_wave_bgm_loop, 100, true);
}

//RAISE LOOP VOLUME
if (global.musicState == "RAISE")
{
    audio_sound_gain(currentBGM, 1, 500);
}
//LOWER LOOP VOLUME
if (global.musicState == "LOWER")
{
    audio_sound_gain(snd_area00_wave_bgm_loop, 0.25, 500);
}

global.musicState = "";
When gameState == "LOWER" it was working, but "RAISE" was not working. In game, the volume gain did not change for "RAISE". However, when I changed both of these to use the currentBGM var for the argument, it all worked correctly.
 

kburkhart84

Firehammer Games
According to the manual, using the name of the sound should also change ALL currently playing sounds, so I'm not sure why it isn't working for you using that. It is possible(and even almost likely IMO) that the manual is wrong about this. It makes more sense to me that the gain change would only affect the sound the next time you play it instead of the currently playing one. I've never even tried changing the gain of the sound resource and have always just changed it for the current instance of the sound...that always made more sense to me.
 

PixelLicker

Member
I only ran into this issue when I had been using the two approaches interchangeably. I had been changing them one at a time to use the var and when I tested it I noticed the problem. Since the ultimate goal was to use the var for all arguments, I am fine with how it is working now. But I was curious to find out why this problem was happening in the first place. It didn't make a lot of sense to me, but I am new to GM in general and this is the first time I started working with audio.
 
Top