Question about setting volume

I want to play a sound like this:

playSound("someSound.mp3", .5)

where .5 is the volume.

However, when you call audio.setVolume(), you have to specify a channel. And since I may have multiple sound effects playing at the same time, I have to let the system choose which channel is available to play a given sound. So how does one set the volume when they don’t know what channel the sound will play on?

[code]

function playSound(snd, vol)

local volume = vol or 1;

local sound = audio.loadStream ( snd );
–audio.setVolume ( volume, { channel=sfxChannel } );
audio.play( sound );

return;
end
[/code] [import]uid: 52127 topic_id: 12749 reply_id: 312749[/import]

@ NuPlayEntertainment,

Thanks for the detailed response!

I was actually in the process of preloading my sounds when I posted. I have cleared all that up and used your code to find a free channel. However, when I play many sounds close together (for example, when my character runs through a group of collectible coins), it only plays sound effects for a few of them. Seems like findFreeChannel() isn’t getting it right:

  
-- Find a free channel to play the sound  
local sndChanFree = audio.findFreeChannel();  
print("found free channel: " .. sndChanFree)  
-- Set the volume in this free channel  
audio.setVolume ( volume, { channel=sndChanFree } );  
   
-- Play the sound in the free channel  
audio.play( soundLibrary[snd], { channel = sndChanFree } )  

“found free channel” prints something like this when I run through a group of coins: 3,3,3,4,4,3,4,4,4,3,3

Why doesn’t it look for a channel besides 3 or 4? [import]uid: 52127 topic_id: 12749 reply_id: 46827[/import]

nevermind, I was using loadStream() instead of loadSound(). Works now. Thanks again. [import]uid: 52127 topic_id: 12749 reply_id: 46833[/import]

This answers your question and will also generally help you out with performance.

I see that you are loading the sound every time it is played. This will hinder performance.

A suggestion to solve this and improve performance is to pre-load all of your sounds into a table.

Here is an example using a modified version of your “playSound” function.

Instead of passing the actual “sound” file name, you’d be passing the “key” of the index in the table.

[lua]local GameSounds =
{
[“S1”] = audio.loadSound( “Sound1.ogg” ),
[“S2”] = audio.loadSound( “Sound2.ogg” ),
[“S3”] = audio.loadSound( “Sound3.ogg” ),
[“S4”] = audio.loadSound( “Sound4.ogg” ),
[“S5”] = audio.loadSound( “Sound5.ogg” )
}

function playSound(sndKey, sndVol)

– If there is no volume don’t play the sound
if (sndVol <= 0) then
return
end

– Find a free channel to play the sound
local sndChanFree = audio.findFreeChannel()

– Set the volume in this free channel
audio.setVolume ( sndVol, { channel=sndChanFree } );

– Play the sound in the free channel
audio.play( GameSounds[sndKey], { channel = sndChanFree } )

return;

end[/lua]

For example, to play the sound called “S4” you would call the function as such:

[lua]playSound(“S4”, 1)[/lua]

I hope that helps you in the future!

Regards,

Andreas Ricci
NuPlay Entertainment
Founder & Lead Developer

[import]uid: 7366 topic_id: 12749 reply_id: 46754[/import]