Changing sound volume with different vols for each sound

Started by tor.brandt, Tue 06/12/2016 11:51:56

Previous topic - Next topic

tor.brandt

I have assigned different volumes for each sound in my game, to make a balanced sound experience.

I have a GUI slider that allows the player to control the SFX volume of the game, and now I just noticed that (of course) when I use this slider, all the sounds are assigned the same volume, and then keeps this volume for the rest of the game (unless I slide the slider some more, of course, in which case they're all assigned this new value).

Is there a way to keep my different volumes for each sound, while still allowing the slider to control the master SFX volume?

Another solution would of course be to edit all the sound files, and make them the correct volumes relative to each other before importing them anew - but before I resort to that, I'm interested in knowing if I can fix it in some other way (although of course this might very well end up being more complicated than simply adjusting the sound files)...

Slasher

A long way would be to assign the new values (volumes) for each sfx individually when using slider - +.

Crimson Wizard

I can think of following workaround:

1. Do not set actual global volume, but save slider value in a variable instead, e.g. MySoundVolume.
2. When playing your clips, apply that volume as a modifier yourself, for example:
Code: ags

AudioChannel *PlayWithVolume(AudioClip *clip)
{
    AudioChannel *ch = clip.Play();
    if (ch != null)
    {
        ch->Volume = ch->Volume + MySoundVolume;
    }
    return ch;
}

tor.brandt

Thanks for the replies!

I got to think, however, if I do something like Crimson Wizard suggests (which I would prefer over something cumbersome like what Slasher suggests), if I have a sound (Sound1, say) that is set to play at e.g. volume 30%, whereas the SFX slider starts at position 75%, and I then slide the slider to, say, 20%, won't Sound1 then be set to -45%, effectively rendering it mute?

Hmmm, may actually be much easier to just edit the sound files :-D

Snarky

Instead of addition, use multiplication:

Code: ags
AudioChannel *PlayWithVolume(AudioClip *clip)
{
    float volumeFactor = Maths.IntToFloat(MySoundVolume)/100.0;
    AudioChannel *ch = clip.Play();
    if (ch != null)
    {
        ch->Volume = Maths.FloatToInt(Maths.IntToFloat(ch->Volume)*volumeFactor,eRoundNearest);
    }
    return ch;
}


Edit: On second thought, this won't work very well because you are always adjusting based on the current volume, not the default volume of the clip, which I don't see any way to access. That's a pretty big flaw of the AGS audio system. :-\

Snarky

Oh wait, I see that CW meant you would always call this function to start playing the sound, so the volume should be the default. That's OK, but then you also have to change the volume on currently playing clips when you adjust the slider. And if you've set the volume to 0 (or close to it so that you lose most of it in rounding), there's then no way to recover the original volume. You'll have to store it in some separate variable.

I tried something similar for the AGS Awards Ceremony, and it was a mess. I think in the end I gave up and just adjusted the volume of the clips outside of AGS.

Crimson Wizard

#6
EDIT: scrapped the post. There is more work to do here, and I do not have time to write script examples right now.

Simplistically, you may remember default volume and clip you played per each AudioChannel, and then use these for reference every time Slider is adjusted.

SMF spam blocked by CleanTalk