Managing sound channels

How do I best manage sound channels?

My current setup involves loading the sound in my main.lua

bgMusic = audio.loadSound( "sounds/Torukia.mp3" ) musicIsPlaying = false

Creating and playing the sound channel on the first scene (I’m using Director to move between scenes)

[code]if musicIsPlaying == false then
if gameSettings.soundOn == true then
backgroundMusicChannel = audio.play( bgMusic, { loops=-1 } )
musicIsPlaying = true
elseif gameSettings.soundOff == true then

end
end[/code]

And stopping/playing the song on a different scene (also using a save/load function to make sure settings are remembered)

local function toggleSound( event ) if event.phase == "ended" then if gameSettings.soundOn == true then gameSettings.soundOn = false gameSettings.soundOff = true audio.stop( ) soundBtn:setFillColor( 255,0,0 ) elseif gameSettings.soundOff == true then gameSettings.soundOn = true gameSettings.soundOff = false backgroundMusicChannel = audio.play( bgMusic, { loops=-1 } ) soundBtn:setFillColor( 0,255,0 ) end saveSettings(gameSettings, "gameSettings.json") end end

This works fine, but it obviously wouldnt work if I want to create options for either music and sound effects, since currently it stops all sound channels from playing.

How would I optimize these bits of code? (is it possible to create sound channels without automatically playing the sound? I’ve tried a few things, but couldnt get that to work). [import]uid: 200198 topic_id: 36279 reply_id: 336279[/import]

Hey Tomas,

To answer your question about creating sound channels. Do you mean reserving the channels? If so, yes you can like so; http://docs.coronalabs.com/api/library/audio/reserveChannels.html

And within the audio.play API you can set the channel you want that file to play on.

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

In infinite (the game I worked on) I created a seperate module to handle all my music tracks, within that module I loaded the saved options file which would classify whether or not to mute music. I then ran through a for loop with the total number of music tracks (which I reserved) and set the volume to zero, like so;

for i = 1, 7 do  
 if Controls[3] == "off" then  
 audio.setVolume( 0, {channel = i})  
 elseif Controls[3] == "on" then  
 audio.setVolume( 0.5, {channel = i})  
 end  
end  

Hope this helps!
[import]uid: 40731 topic_id: 36279 reply_id: 144206[/import]

That’s not exactly what I was looking for, but I can certainly alter it to my needs.

Out of curiosity, is there a difference in memory usage between stopping a channel from playing, or lowering its volume all the way down to 0? [import]uid: 200198 topic_id: 36279 reply_id: 144252[/import]

Well stopping it will be much better than letting it play with volume set to zero. I need to leave it playing for other purposes in the code. In your case instead of the volume change you can use audio.stop( [channel] ) to stop the specific channel(s). [import]uid: 40731 topic_id: 36279 reply_id: 144312[/import]

Hey Tomas,

To answer your question about creating sound channels. Do you mean reserving the channels? If so, yes you can like so; http://docs.coronalabs.com/api/library/audio/reserveChannels.html

And within the audio.play API you can set the channel you want that file to play on.

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

In infinite (the game I worked on) I created a seperate module to handle all my music tracks, within that module I loaded the saved options file which would classify whether or not to mute music. I then ran through a for loop with the total number of music tracks (which I reserved) and set the volume to zero, like so;

for i = 1, 7 do  
 if Controls[3] == "off" then  
 audio.setVolume( 0, {channel = i})  
 elseif Controls[3] == "on" then  
 audio.setVolume( 0.5, {channel = i})  
 end  
end  

Hope this helps!
[import]uid: 40731 topic_id: 36279 reply_id: 144206[/import]

That’s not exactly what I was looking for, but I can certainly alter it to my needs.

Out of curiosity, is there a difference in memory usage between stopping a channel from playing, or lowering its volume all the way down to 0? [import]uid: 200198 topic_id: 36279 reply_id: 144252[/import]

Well stopping it will be much better than letting it play with volume set to zero. I need to leave it playing for other purposes in the code. In your case instead of the volume change you can use audio.stop( [channel] ) to stop the specific channel(s). [import]uid: 40731 topic_id: 36279 reply_id: 144312[/import]

Hey Tomas,

To answer your question about creating sound channels. Do you mean reserving the channels? If so, yes you can like so; http://docs.coronalabs.com/api/library/audio/reserveChannels.html

And within the audio.play API you can set the channel you want that file to play on.

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

In infinite (the game I worked on) I created a seperate module to handle all my music tracks, within that module I loaded the saved options file which would classify whether or not to mute music. I then ran through a for loop with the total number of music tracks (which I reserved) and set the volume to zero, like so;

for i = 1, 7 do  
 if Controls[3] == "off" then  
 audio.setVolume( 0, {channel = i})  
 elseif Controls[3] == "on" then  
 audio.setVolume( 0.5, {channel = i})  
 end  
end  

Hope this helps!
[import]uid: 40731 topic_id: 36279 reply_id: 144206[/import]

That’s not exactly what I was looking for, but I can certainly alter it to my needs.

Out of curiosity, is there a difference in memory usage between stopping a channel from playing, or lowering its volume all the way down to 0? [import]uid: 200198 topic_id: 36279 reply_id: 144252[/import]

Well stopping it will be much better than letting it play with volume set to zero. I need to leave it playing for other purposes in the code. In your case instead of the volume change you can use audio.stop( [channel] ) to stop the specific channel(s). [import]uid: 40731 topic_id: 36279 reply_id: 144312[/import]

Hey Tomas,

To answer your question about creating sound channels. Do you mean reserving the channels? If so, yes you can like so; http://docs.coronalabs.com/api/library/audio/reserveChannels.html

And within the audio.play API you can set the channel you want that file to play on.

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

In infinite (the game I worked on) I created a seperate module to handle all my music tracks, within that module I loaded the saved options file which would classify whether or not to mute music. I then ran through a for loop with the total number of music tracks (which I reserved) and set the volume to zero, like so;

for i = 1, 7 do  
 if Controls[3] == "off" then  
 audio.setVolume( 0, {channel = i})  
 elseif Controls[3] == "on" then  
 audio.setVolume( 0.5, {channel = i})  
 end  
end  

Hope this helps!
[import]uid: 40731 topic_id: 36279 reply_id: 144206[/import]

That’s not exactly what I was looking for, but I can certainly alter it to my needs.

Out of curiosity, is there a difference in memory usage between stopping a channel from playing, or lowering its volume all the way down to 0? [import]uid: 200198 topic_id: 36279 reply_id: 144252[/import]

Well stopping it will be much better than letting it play with volume set to zero. I need to leave it playing for other purposes in the code. In your case instead of the volume change you can use audio.stop( [channel] ) to stop the specific channel(s). [import]uid: 40731 topic_id: 36279 reply_id: 144312[/import]