Hi guys! Today I need help to understand something.
Managing audio and music has been a difficult task to manage and understand. I have managed to do it but I think my method is very long and there must be some way to simplify things. I would like some opinions and help.
In my external dada module I code this:
local M M.soundTable = { menuMusic = audio.loadSound("sounds/menu\_music.wav"), gameMusic = audio.loadSound("sounds/game\_music.wav"), fx1 = audio.loadSound("sounds/bounce.wav"), fx2 = audio.loadSound("sounds/bounce\_2.wav"), fx3 = audio.loadSound("sounds/bounce\_3.wav"), fx4 = audio.loadSound("sounds/win.wav"), fx5 = audio.loadSound("sounds/lose.wav"), fx6 = audio.loadSound("sounds/return.wav"), } --volume up M.volumeUp = function() audio.setVolume( 0.4, {channel=3} );audio.setVolume( 0.4, {channel=4} ) audio.setVolume( 0.4, {channel=5} );audio.setVolume( 0.4, {channel=6} ) audio.setVolume( 0.4, {channel=7} );audio.setVolume( 0.4, {channel=8} ) audio.setVolume( 0.4, {channel=9} );audio.setVolume( 0.4, {channel=10} ) audio.setVolume( 0.4, {channel=11} );audio.setVolume( 0.4, {channel=12} ) audio.setVolume( 0.4, {channel=13} );audio.setVolume( 0.4, {channel=14} ) audio.setVolume( 0.4, {channel=15} );audio.setVolume( 0.4, {channel=16} ) audio.setVolume( 0.4, {channel=17} );audio.setVolume( 0.4, {channel=18} ) audio.setVolume( 0.4, {channel=19} );audio.setVolume( 0.4, {channel=20} ) audio.setVolume( 0.4, {channel=21} );audio.setVolume( 0.4, {channel=22} ) audio.setVolume( 0.4, {channel=23} );audio.setVolume( 0.4, {channel=24} ) audio.setVolume( 0.4, {channel=25} );audio.setVolume( 0.4, {channel=26} ) audio.setVolume( 0.4, {channel=27} );audio.setVolume( 0.4, {channel=28} ) audio.setVolume( 0.4, {channel=29} );audio.setVolume( 0.4, {channel=30} ) audio.setVolume( 0.4, {channel=31} );audio.setVolume( 0.4, {channel=32} ) end --volume down M.volumeDown = function() audio.setVolume( 0, {channel=3} );audio.setVolume( 0, {channel=4} ) audio.setVolume( 0, {channel=5} );audio.setVolume( 0, {channel=6} ) audio.setVolume( 0, {channel=7} );audio.setVolume( 0, {channel=8} ) audio.setVolume( 0, {channel=9} );audio.setVolume( 0, {channel=10} ) audio.setVolume( 0, {channel=11} );audio.setVolume( 0, {channel=12} ) audio.setVolume( 0, {channel=13} );audio.setVolume( 0, {channel=14} ) audio.setVolume( 0, {channel=15} );audio.setVolume( 0, {channel=16} ) audio.setVolume( 0, {channel=17} );audio.setVolume( 0, {channel=18} ) audio.setVolume( 0, {channel=19} );audio.setVolume( 0, {channel=20} ) audio.setVolume( 0, {channel=21} );audio.setVolume( 0, {channel=22} ) audio.setVolume( 0, {channel=23} );audio.setVolume( 0, {channel=24} ) audio.setVolume( 0, {channel=25} );audio.setVolume( 0, {channel=26} ) audio.setVolume( 0, {channel=27} );audio.setVolume( 0, {channel=28} ) audio.setVolume( 0, {channel=29} );audio.setVolume( 0, {channel=30} ) audio.setVolume( 0, {channel=31} );audio.setVolume( 0, {channel=32} ) end return M
I reserve { channel=1} and {channel=2} in main.lua for menu music and game music respectively, and set channels volume to 0.4.
Because my game is a test and I realized that I have a lot to understand before make money programming games, I decided to use it to learn.
I do not want to use a scene for the settings because I’m just going to mute the music and the sounds. so I decide to make the sound function in menu scene.
and in menu scene I use this functions
--manage music local function manageMusic( event ) if ( event.phase == "ended" ) then if audio.getVolume( { channel=1 } ) == 0 and audio.getVolume( { channel=2 } ) == 0 then audio.setVolume( 0.4, { channel=1 } ) audio.setVolume( 0.4, { channel=2 } ) musicBtn.fill = loads.musicOn --save music state to JSON loads.musicStatus = "on" loads.updateGameProgress() print("Music is == ", loads.musicStatus ) else audio.setVolume( 0, { channel=1 } ) audio.setVolume( 0, { channel=2 } ) musicBtn.fill = loads.musicOff --save music state to JSON loads.musicStatus = "off" loads.updateGameProgress() print("Music is == ", loads.musicStatus ) end end return true end --manage sound local function manageSound( event ) if ( event.phase == "ended" ) then if audio.getVolume( { channel=3 } ) == 0 and audio.getVolume( { channel=4 } ) == 0 and audio.getVolume( { channel=5 } ) == 0 and audio.getVolume( { channel=6 } ) == 0 and audio.getVolume( { channel=7 } ) == 0 and audio.getVolume( { channel=8 } ) == 0 and audio.getVolume( { channel=9 } ) == 0 and audio.getVolume( { channel=10 } ) == 0 and audio.getVolume( { channel=11 } ) == 0 and audio.getVolume( { channel=12 } ) == 0 and audio.getVolume( { channel=13 } ) == 0 and audio.getVolume( { channel=14 } ) == 0 and audio.getVolume( { channel=15 } ) == 0 and --------TOO MUCH AND!!! audio.getVolume( { channel=16 } ) == 0 and audio.getVolume( { channel=17 } ) == 0 and audio.getVolume( { channel=18 } ) == 0 and audio.getVolume( { channel=19 } ) == 0 and audio.getVolume( { channel=20 } ) == 0 and audio.getVolume( { channel=21 } ) == 0 and audio.getVolume( { channel=22 } ) == 0 and audio.getVolume( { channel=23 } ) == 0 and audio.getVolume( { channel=24 } ) == 0 and audio.getVolume( { channel=25 } ) == 0 and audio.getVolume( { channel=26 } ) == 0 and audio.getVolume( { channel=27 } ) == 0 and audio.getVolume( { channel=28 } ) == 0 and audio.getVolume( { channel=29 } ) == 0 and audio.getVolume( { channel=30 } ) == 0 and audio.getVolume( { channel=31 } ) == 0 and audio.getVolume( { channel=32 } ) == 0 then loads.volumeUp() soundBtn.fill = loads.soundOn --save music state to JSON loads.soundStatus = "on" loads.updateGameProgress() print("Music is == ", loads.soundStatus ) else loads.volumeDown() soundBtn.fill = loads.soundOff --save music state to JSON loads.soundStatus = "off" loads.updateGameProgress() print("Music is == ", loads.soundStatus ) end end return true end
It’s working but there is a way to use a sound channel table instead of call them manually?