Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Carles on Tue 30/04/2024 22:47:16

Title: Audio channels and volume [SOLVED]
Post by: Carles on Tue 30/04/2024 22:47:16
Hi,

I have these audio channels in the function function game_start()
  chansound = System.AudioChannels[1]; //ambient sound
  chanmusic = System.AudioChannels[2]; //music
  chanSoundE1 = System.AudioChannels[3]; //sound effect
  chanSoundE2 = System.AudioChannels[4]; //sound effect
  chanSoundE3 = System.AudioChannels[5]; //sound effect
They are also defined as global variables. Then I have 3 sliders to control the volume:
function Slider1_OnChange(GUIControl *control) //ambient sound.
{
Game.SetAudioTypeVolume(eAudioTypeAmbientSound, Slider1.Value, eVolExistingAndFuture);
chansound.Volume = Slider1.Value;
}

function Slider2_OnChange(GUIControl *control) //Music.
{
Game.SetAudioTypeVolume(eAudioTypeMusic, Slider2.Value, eVolExistingAndFuture);
chanmusic.Volume = Slider2.Value;
}

function Slider3_OnChange(GUIControl *control) //sound effect.
{
Game.SetAudioTypeVolume(eAudioTypeSound, Slider3.Value, eVolExistingAndFuture);
chanSoundE1.Volume = Slider3.Value;
chanSoundE2.Volume = Slider3.Value;
chanSoundE3.Volume = Slider3.Value;
}
But every time an audio plays, I have to indicate its volume afterwards, because if I don't do it, it has the volume at 100%:
chanmusic = aHideout.Play();
chanmusic.Volume = Slider2.Value;
Is there any way not to have to indicate each time its volume? Maybe this can be done in a more correct way. Thanks!
Title: Re: Audio channels and volume
Post by: Crimson Wizard on Tue 30/04/2024 23:12:40
Normally you do not have to do "chansound.Volume = Slider1.Value;" at all. That is a redundant part in the above code.

Channel's volume does not persist after playback end, it is overwritten with the every new Play command, but if you call Game.SetAudioTypeVolume with eVolExistingAndFuture, then this volume will be saved, and applied to both currently playing and any future sound of the given type.

As for why it is not working... Possible reasons that come to mind:
1. These clips do not belong to appropriate types.
2. Something else resets volume in your script.
3. Slider1_OnChange and similar functions are not connected to slider events.
4. Bug in the engine...

Can you tell which version of AGS are you using?

EDIT: don't know if this is the cause of the problem, but I think that you also need to call Game.SetAudioTypeVolume with slider values in game_start, just in case.

Title: Re: Audio channels and volume
Post by: Carles on Tue 30/04/2024 23:42:10
Sorry, it was a silly mistake. Indeed it was missing to add this in the game_start. Otherwise it only had effect if I modified the slider.  :-[
  Game.SetAudioTypeVolume(eAudioTypeAmbientSound, Slider1.Value, eVolExistingAndFuture);
  Game.SetAudioTypeVolume(eAudioTypeMusic, Slider2.Value, eVolExistingAndFuture);
  Game.SetAudioTypeVolume(eAudioTypeSound, Slider3.Value, eVolExistingAndFuture);