Sound FX volume and Music volume?

Hey guys, 

Im messing around with sound and music in my game…

for my music I did this:

[lua]

audio.reserveChannels( 1 )

local backgroundMusic = audio.loadStream(“media/MenuMusic.mp3”)

audio.play( backgroundMusic ,{     channel=1,   loops=-1} )

[/lua]

See how I reserved channel 1 for the music… is it a good approach?

for my sound effects I use this for example:

[lua]

local shineOnBoard = audio.loadSound(“media/shineOnBoard.wav”)

audio.play (shineOnBoard)

[/lua]

Which from my understanding will grab a free channel automatically and play the sound on this channel.

In the game there can be a situation that there are multiple sound effects firing simultaneously.

Now say I want to control the volume of the sounds separately and the volume of the music.

So I have 2 buttons

button 1: sounds on-off (by on-off I mean, on is volume=1 and off is volume=0)

button 2: music on-off (by on-off I mean on is volume=1 and off is volume=0)

How can I set it in a way that control the volume separately when pressing those buttons?

Roy.

Your approach so far is good, setting certain sounds (e.g. background music) is a good idea as it gives you better control, which is what you want.

If you want to set the volume on an individual channel, just pass the channel number as a parameter to the setVolume function

audio.setVolume( 1, { channel=1 } ) -- set the volume on channel 1 to "on" audio.setVolume( 0, { channel=1 } ) -- set the volume on channel 1 to "off"

You could simplify your button press by also using “getVolume”, to allow you to toggle the volume between 2 values

local function toggleBGM() --if it is already off then turn it on, else turn it off. if audio.getVolume( { channel=1 } ) == 0 then audio.setVolume( 1, { channel=1 } ) else audio.setVolume( 0, { channel=1 } ) end end

Ok so say I want to control the music volume I then use this:

[lua]

audio.setVolume( 1, { channel=1 } ) – for on

audio.setVolume( 0, { channel=1 } ) – for off

[/lua]

But how do I control the sound FX volume? after all it picks a free channel automatically.

Roy.

You have a few options.

  1. You set your sound FX channels manually, like you do with the bgm. Then you set each channel volume manually. This lets you do things like only turn certain sound off (perhaps turn off button press sounds, but leave other in game sound FX on)

  2. You loop through ALL channels which are not bgm and toggle them on/off:

    local function toggleSFX() --Start at 2, so we don’t also toggle BGM on/off --I believe max number of channels is 32, but I could be wrong. for i = 2, 32 do --if it is already off then turn it on, else turn it off. if audio.getVolume( { channel=i } ) == 0 then audio.setVolume( 1, { channel=i } ) else audio.setVolume( 0, { channel=i } ) end end end

You might actually find it more reliable to actually use separate soundOn and soundOff functions, as I think sometimes there are rounding errors with the volume. So volume may be 0.99999 and because this is not 0, it would be set to 1. If that only happened on half of the channels, you’d be stuck with some sounds on and some off. It was a while ago that I noticed this problem though, so maybe it’s been fixed.

Your approach so far is good, setting certain sounds (e.g. background music) is a good idea as it gives you better control, which is what you want.

If you want to set the volume on an individual channel, just pass the channel number as a parameter to the setVolume function

audio.setVolume( 1, { channel=1 } ) -- set the volume on channel 1 to "on" audio.setVolume( 0, { channel=1 } ) -- set the volume on channel 1 to "off"

You could simplify your button press by also using “getVolume”, to allow you to toggle the volume between 2 values

local function toggleBGM() --if it is already off then turn it on, else turn it off. if audio.getVolume( { channel=1 } ) == 0 then audio.setVolume( 1, { channel=1 } ) else audio.setVolume( 0, { channel=1 } ) end end

Ok so say I want to control the music volume I then use this:

[lua]

audio.setVolume( 1, { channel=1 } ) – for on

audio.setVolume( 0, { channel=1 } ) – for off

[/lua]

But how do I control the sound FX volume? after all it picks a free channel automatically.

Roy.

You have a few options.

  1. You set your sound FX channels manually, like you do with the bgm. Then you set each channel volume manually. This lets you do things like only turn certain sound off (perhaps turn off button press sounds, but leave other in game sound FX on)

  2. You loop through ALL channels which are not bgm and toggle them on/off:

    local function toggleSFX() --Start at 2, so we don’t also toggle BGM on/off --I believe max number of channels is 32, but I could be wrong. for i = 2, 32 do --if it is already off then turn it on, else turn it off. if audio.getVolume( { channel=i } ) == 0 then audio.setVolume( 1, { channel=i } ) else audio.setVolume( 0, { channel=i } ) end end end

You might actually find it more reliable to actually use separate soundOn and soundOff functions, as I think sometimes there are rounding errors with the volume. So volume may be 0.99999 and because this is not 0, it would be set to 1. If that only happened on half of the channels, you’d be stuck with some sounds on and some off. It was a while ago that I noticed this problem though, so maybe it’s been fixed.