Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local panel = component.proxy("E843058744625592E02188867504BC4E") -- Get panel component
- local speaker = component.proxy("0055A0414AD11202172C73AF14C57DF8") -- get speaker component
- local playbutton = panel:getModule(4, 4) -- get play button on panel
- local skipbutton = panel:getModule(7, 4) -- get skip button on panel
- local volumedial = panel:getModule(5, 4) -- get volume dial on panel
- local loopbutton = panel:getModule(8, 5) -- get loop button on panel
- local playing = false -- whether a song is playing
- local songarray = {"cpmoonshine", "cprussian", "cpcrocdandies"} -- song files go in here
- local currentsong = 1 -- the arrays/tables in lua start at 1
- local numofsongs = #songarray -- the number of songs in the array/table
- local currentvolume = 100
- local loop = 0
- speaker:setVolume(currentvolume) -- setting volume manually
- playbutton:setColor(0,1,0,4) -- play button color
- skipbutton:setColor(1,1,1,4) -- skip button color
- loopbutton:setColor(0,0,0,4) -- loop default button color
- event.listen(playbutton) -- listen for events from the play button so we know if it got pressed
- event.listen(speaker) -- listen for events from the speaker so we know when the sound begins or ends
- event.listen(skipbutton) -- listen for events from the skip button so we know if it got pressed
- event.listen(loopbutton) -- listen for events from the skip button so we know if it got pressed
- event.listen(volumedial) -- listen for events from the volume dial so we know if it got cranked
- local function eventHandler(evnt, compo, typ, snd) -- push the event handler into its own function, so its easier to keep organized
- if evnt == "Trigger" and compo == playbutton and not playing then -- if the event is a "Trigger" and it came from the play button and a sound is not currently playing then
- speaker:playSound(songarray[currentsong]) -- play the selected song
- elseif evnt == "SpeakerSound" and compo == speaker then -- if the event is a "SpeakerSound" and it came from the speaker
- if typ == 1 then -- if it stopped because Manually or End of File respectively then
- playing = false -- set playing to false so we can play something again
- elseif typ == 2 then
- if loop == 1 then
- if currentsong == numofsongs then -- if the current song is the last in the array/table
- currentsong = 1
- speaker:playSound(songarray[currentsong]) -- play the selected song
- playing = true -- set playing to true
- else
- currentsong = currentsong + 1
- speaker:playSound(songarray[currentsong]) -- play the selected song
- playing = true -- set playing to true
- end
- elseif loop == 2 then
- speaker:playSound(songarray[currentsong])
- playing = true -- set playing to true
- end
- elseif typ == 0 then -- if it began to play something
- playing = true -- set playing to true
- end
- elseif evnt == "Trigger" and compo == skipbutton then -- if the event is "Trigger" and it came from the skip button
- if currentsong == numofsongs then -- if the current song is the last in the array/table
- currentsong = 1
- speaker:playSound(songarray[currentsong]) -- play the selected song
- else
- currentsong = currentsong + 1
- speaker:playSound(songarray[currentsong]) -- play the selected song
- end
- elseif evnt == "PotRotate" and compo == volumedial then -- if the event is "potrotate" and it came from the volume dial
- if typ == false then -- if it was turned clockwise
- currentvolume = currentvolume + 10 -- increase volume by ten
- speaker:setVolume(currentvolume) -- set volume
- elseif typ == true then
- currentvolume = currentvolume - 10 -- decrease volume by ten
- speaker:setVolume(currentvolume) -- set volume
- end
- elseif evnt == "Trigger" and compo == loopbutton then -- if the event is "Trigger" and it came from the loop button
- if loop == 0 then
- loop = 1
- loopbutton:setColor(0.25,0.25,0.25,4) -- loop button color
- elseif loop == 1 then
- loop = 2
- loopbutton:setColor(1,1,1,4) -- loop button color
- elseif loop == 2 then
- loop = 0
- loopbutton:setColor(0,0,0,4) -- loop button color
- end
- end
- end
- while true do -- a infinite loop
- local evnt, compo, typ, snd = event.pull(0) -- get any events that come in, evnt is signal, compo is the component that sent the signal.
- --everything after is optional parameters a component can send along with the signal
- if evnt ~= nil then -- if there was an evnt this cycle
- eventHandler(evnt, compo, typ, snd) -- send it to the event handler
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement