Help needed for getting audio tones to play and stop when clicking graphical buttons

Hi everyone.  I am working on a music application and am trying to create code that allows the user to touch a set of buttons that turns on a different audio file assigned to each button on endless loop as well as touch any one of the buttons again to turn off their assigned audio file.  The user can have multiple buttons turned on simultaneously.  I can get one button to work properly with the sound but am puzzled on how to achieve multiple buttons and multiple sounds turned on simultaneously.  I have one function created for all the buttons which makes things a bit more challenging for assigning each sound and that is where most my trouble comes in.  I want the code as neat and consolidated as possible.

Here is my existing code with no sounds and no errors (preload.lua loads my sound files):

require("precode") require("shapePrecode") require("preload") audio.reserveChannels( 10 ) audio.setVolume( 1.0 ) local sndA = audio.loadSound ( "audio/toneA1.mp3" ) local sndB = audio.loadSound ( "audio/toneB1.mp3" ) local sndC = audio.loadSound ( "audio/toneC1.mp3" ) local sndD = audio.loadSound ( "audio/toneD1.mp3" ) local sndE = audio.loadSound ( "audio/toneE1.mp3" ) local sndF = audio.loadSound ( "audio/toneF1.mp3" ) local sndG = audio.loadSound ( "audio/toneG1.mp3" ) local sndC2 = audio.loadSound ( "audio/toneC2.mp3" ) local function A1() audio.play(sndA,{channel=1, loops=-1} ) audio.setVolume( 1, { channel=3 } ) end local function B1() audio.play(sndB,{channel=2, loops=-1} ) audio.setVolume( 1, { channel=3 } ) end local function C1() audio.play(sndC,{channel=3, loops=-1} ) audio.setVolume( 1, { channel=3 } ) end local function D1() audio.play(sndD,{channel=4, loops=-1} ) audio.setVolume( 1, { channel=3 } ) end local function E1() audio.play(sndE,{channel=5, loops=-1} ) audio.setVolume( 1, { channel=3 } ) end local function F1() audio.play(sndF,{channel=6, loops=-1} ) audio.setVolume( 1, { channel=3 } ) end local function G1() audio.play(sndG,{channel=7, loops=-1} ) audio.setVolume( 1, { channel=3 } ) end local function C2() audio.play(sndC2,{channel=8, loops=-1} ) audio.setVolume( 1, { channel=3 } ) end --function makes button visible local function callSound() end local NoteButton = {} NoteButton.new = function() local btn = display.newGroup() local off = display.newCircle(centerX,centerY, 30) off.strokeWidth = 7 local on = display.newCircle(centerX,centerY,30) on:setFillColor(1,.6,0) on.strokeWidth = 7 on.alpha = 0 btn:insert(off) btn:insert(on) --create functions for transitions local function onScaleUp(t) transition.to(on, { time = t, xScale = 1.3, yScale = 1.3 }) end local function onScaleDown(t) transition.to(on, { time = t, xScale = 1, yScale = 1 }) end local function offScaleUp(t) transition.to(off, { time = t, xScale = 1.3, yScale = 1.3 }) end local function offScaleDown(t) transition.to(off, { time = t, xScale = 1, yScale = 1 }) end btn.x = 0 btn.y = 0 --function, tap "off" image to turn on sound function off:touch(e) if(e.phase == "began") then offScaleUp(200) display.getCurrentStage():setFocus(off) off.hasFocus = true onScaleUp(200) print("off clicked") elseif(self.hasFocus) then if(e.phase == "ended" or e.phase == "cancelled") then on.alpha = 1 offScaleDown(200) onScaleDown(200) display.getCurrentStage():setFocus(nil) off.hasFocus = false print("off ended") --[[A1() B1() C1() D1() E1() F1() G1() C2()]] end end return true end --fuction, tap "on" image to turn off sound function on:touch(e) if(e.phase == "began") then onScaleUp(200) display.getCurrentStage():setFocus(on) on.hasFocus = true offScaleUp(200) print("on clicked") elseif(self.hasFocus) then if(e.phase == "ended" or e.phase == "cancelled") then on.alpha = 0 onScaleDown(200) offScaleDown(200) display.getCurrentStage():setFocus(nil) on.hasFocus = false print("on ended") end end return true end off:addEventListener("touch", off) on:addEventListener("touch", on) return btn end --Note text display function local function noteTxt(text) txt = display.newText(text, centerX, centerY, native.systemFontBold, 18) txt:setFillColor( 1, 1, 1 ) return txt end local cNote = NoteButton.new() local cTxtPos = noteTxt("C Note") cTxtPos.x=centerX-160 cTxtPos.y=centerY-120 cNote.x=centerX-400 cNote.y=centerY-220 local dNote = NoteButton.new() dNote.x=centerX-300 dNote.y=centerY-220 local dTxtPos = noteTxt("D Note") dTxtPos.x=centerX-60 dTxtPos.y=centerY-120 local eNote = NoteButton.new() eNote.x=centerX-200 eNote.y=centerY-220 local eTxtPos = noteTxt("E Note") eTxtPos.x=centerX+40 eTxtPos.y=centerY-120 local fNote = NoteButton.new() fNote.x=centerX-100 fNote.y=centerY-220 local fTxtPos = noteTxt("F Note") fTxtPos.x=centerX+140 fTxtPos.y=centerY-120 local gNote = NoteButton.new() gNote.x=centerX-400 gNote.y=centerY-100 local gTxtPos = noteTxt("G Note") gTxtPos.x=centerX-160 gTxtPos.y=centerY+120 local aNote = NoteButton.new() aNote.x=centerX-300 aNote.y=centerY-100 local gTxtPos = noteTxt("A Note") gTxtPos.x=centerX-60 gTxtPos.y=centerY+120 local bNote = NoteButton.new() bNote.x=centerX-200 bNote.y=centerY-100 local bTxtPos = noteTxt("B Note") bTxtPos.x=centerX+40 bTxtPos.y=centerY+120 local c2Note = NoteButton.new() c2Note.x=centerX-100 c2Note.y=centerY-100 local c2TxtPos = noteTxt("C Note") c2TxtPos.x=centerX+140 c2TxtPos.y=centerY+120

I’ve tried many different ways of getting this to work by preloading the sound functions as well as the button function and assigning it a completely new value with it’s own function… It’s been over a month since I last tried to make this work so It’s all kind of lost in memory now.

Any help is greatly appreciated!