Separate controls for music and sound effects [SOLVED]

Started by eri0o, Fri 01/09/2017 00:40:57

Previous topic - Next topic

eri0o

Hello!

I am trying to understand if it's possible to have separate audio controls for music and sound effects. My first approach was to implement two modules, one to deal with music and another to deal with sound effects. The sound effects module is very simple so I will show it below.

SFXPlayer.asc
Spoiler
Code: ags

AudioChannel *acSound;
int gloabalSoundVol;

static void SFXPlayer::play(AudioClip * soundClip){ 
  acSound = soundClip.Play(eAudioPriorityNormal, eOnce);
  acSound.Volume = gloabalSoundVol;
}

static void SFXPlayer::stop(AudioClip * soundClip){
  soundClip.Stop();  
}

static void SFXPlayer::setGlobalVolume(int soundVolume){
  gloabalSoundVol = soundVolume;
  if(acSound != null){
    acSound.Volume = gloabalSoundVol;
  }
}

static void SFXPlayer::setVolume(int soundVolume){
  if(acSound != null){
    acSound.Volume = FloatToInt(IntToFloat(soundVolume) * IntToFloat(gloabalSoundVol)/100.0);
  }
}

static int SFXPlayer::getGlobalVolume(){
  return gloabalSoundVol;  
}

function game_start()
{
  gloabalSoundVol=100;
}
[close]

So the idea was that manipulating the volume of the returned audioChannel I could set it's volume to the appropriate one from a previously set volume. So this approach kind of works, but some sounds are not passed through neither my MusicPlayer module or my SFXPlayer module. I am talking about the sounds originating from view frames, like the sounds of the main character walking and the sounds in between complex animations... So my question is, is there any way to set those sounds volume?

I know there is a variable called System.AudioChannelCount that tells me the number of SystemChannels, and I can access them using System.AudioChannels[ i] ... But I couldn't find out how to use this information to create a sounds volume control. I think if I could understand which audio channel is currently holding the current music, I could assume all other are dealing with the SFX, but I have no idea what to do with this information...

EDIT:

Maybe I need to do something like this, but I don't know where yet.

Code: ags

//AudioClip * currentMusic;
//this variable holds the current playing music

int i = 0;
AudioChannel *ac;

while (i < System.AudioChannelCount)
{
  ac = System.AudioChannels[i];
 
  if (ac.PlayingClip != currentMusic)
  {
    System.AudioChannels[i].Volume = globalSoundVolume;
  } else {
    System.AudioChannels[i].Volume = globalMusicVolume;
  }
  i++;
}


EDIT2:

Apparently the AGS Manual says the following:

QuoteAGS currently has an 8-channel audio system, which means that up to 8 sounds can be playing at the same time. With the default Audio Types settings, one channel is reserved for speech, one for music and one for ambient sounds; thus leaving 5 available for sound effects.

So, when I play and audioClip, how do I specify it's a music or an ambient sound so it attaches to the correct audio channel?

EDIT3:

Apparently I am an idiot... Ok, so each audio clip has a type, on the interface you can set each of the audio clips type. You can even create a folder and set the default type for the audio clips in that folder. Now if you do this correctly, and it's pretty easy, you can then set the volumes for the audio types using:


Code: ags

  Game.SetAudioTypeVolume(eAudioTypeAmbientSound, 100, eVolExistingAndFuture);
  Game.SetAudioTypeVolume(eAudioTypeMusic, 100, eVolExistingAndFuture);
  Game.SetAudioTypeVolume(eAudioTypeSound, 100, eVolExistingAndFuture);


So... Yeah... That's it, it doesn't require all my ramblings on there...

Crimson Wizard

#1
Quote from: eri0o on Fri 01/09/2017 00:40:57
So, when I play and audioClip, how do I specify it's a music or an ambient sound so it attaches to the correct audio channel?

You define this by assigning clips to corresponding types in the project tree. You may also create custom types and define how many channels they have (8 is still total limit).

To set the volume of all clips of the same type you can use Game.SetAudioTypeVolume(AudioType, int volume, ChangeVolumeType).

BTW, in my opinion using floats when setting percentage of volume is overkill, because the precision loss you get by dividing integers should not be noticeable by human ear.

eri0o

Hey CW!

I was trying to understand everything and apparently understood what I was supposed to do at the same time you wrote the answer... :-[

I marked the thread as solved. I never thought about looking in the project tree... I get so used to coding... :X

Crimson Wizard

To be fair, Game.SetAudioTypeVolume does not let you keep individual volumes per clip, unfortunately, because it sets absolute value, not relative to "default clip volume", so you may still find a use for your system.

SMF spam blocked by CleanTalk