Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: CheckMate on Sun 30/07/2023 13:17:48

Title: Low volume on sounds while music playing.
Post by: CheckMate on Sun 30/07/2023 13:17:48
Hi,

I need some help. When I play a sound in my game I find out that the music has a loud volume, so I need to set the music very low when a sound is playing. How can I fix that?

Thanks.
Title: Re: Low volume on sounds while music playing.
Post by: Nahuel on Sun 30/07/2023 13:32:39
Hey. Isn't this for the AudioPriority optional arg?

https://adventuregamestudio.github.io/ags-manual/StandardEnums.html?highlight=audiopriority&case_sensitive=0

enum AudioPriority {
  eAudioPriorityVeryLow = 1,
  eAudioPriorityLow = 25,
  eAudioPriorityNormal = 50,
  eAudioPriorityHigh = 75,
  eAudioPriorityVeryHigh = 100
};
Title: Re: Low volume on sounds while music playing.
Post by: Snarky on Sun 30/07/2023 13:55:10
Quote from: Nahuel on Sun 30/07/2023 13:32:39Hey. Isn't this for the AudioPriority optional arg?

No. AudioPriority is to decide which sounds play and which are skipped if you run out of AudioChannels when trying to play too many sounds simultaneously.

To change the volume of an AudioClip you play, you need to do something like:

Code (ags) Select
AudioChannel* ac = myClip.Play();
if(ac != null) ac.Volume = 50; // Play at 50% volume

(This assumes that your AudioClip is called myClip.)
Title: Re: Low volume on sounds while music playing.
Post by: Crimson Wizard on Sun 30/07/2023 14:11:45
First of all, I think this may be a case of XY problem, as Khris likes to call it, that is when a user asks how to do something, while the problem may have a better solution.

So I'd propose to investigate what the problem is. Is current music volume is fine overall, maybe it should be lower from the start?

From what I heard, when doing sound design for the game, experienced game developers align all of their music and sounds to some "baseline", ensuring that all of them play nicely together.

The in-game solution is to provide volume sliders per sound type (music, sound) and let player adjust them. You may set the starting values for these sliders to some default values.

Setting a volume of one type in AGS is done using Game.SetAudioTypeVolume (https://adventuregamestudio.github.io/ags-manual/Game.html#gamesetaudiotypevolume) command.



If you really want to lower the music only during the sound, then it's possible to use Game.SetAudioTypeVolume to lower music temporarily too. Probably for convenience you should make a extender custom function (https://adventuregamestudio.github.io/ags-manual/ExtenderFunctions.html) that both plays a sound and lowers music volume:

Code (ags) Select
AudioChannel *PlaySound(this AudioClip*)
{
    Game.SetAudioTypeVolume(eAudioTypeMusic, 50, eVolExistingAndFuture);
    return this.Play();
}

Such function may then be called similarly to how you call standard Play:
Code (ags) Select
aSoundClip.PlaySound();


But since you also need to rise the volume back once the sound finishes, then you'll have to:
1) save that it has been lowered in some global variable; this may be done in that custom function I suggested above:
Code (ags) Select
AudioChannel *PlaySound(this AudioClip*)
{
    Game.SetAudioTypeVolume(eAudioTypeMusic, 50, eVolExistingAndFuture);
    MusicWasLowered = true; // assuming you created this global variable
    return this.Play();
}


2) keep testing whether any sound is playing in function repeatedly_execute_always. The latter may be done by scanning all channels and checking their playback type:
Code (ags) Select
function repeatedly_execute_always()
{
    if (MusicWasLowered) // assuming you created this global variable
    {
        bool found_one;
        for (int i = 0; i < System.AudioChannelCount; i++)
        {
            AudioChannel* ch = System.AudioChannels[i];
            if (ch.PlayingClip != null && ch.PlayingClip.Type == eAudioTypeSound) // or whatever it's called in your game
            {
                found_one = true;
                break;
            }
        }

        if (!found_one)
        {
            Game.SetAudioTypeVolume(eAudioTypeMusic, 100, eVolExistingAndFuture);
            MusicWasLowered = false;
        }
    }
}