This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
merithes's profile picture

probelm w/ music program

Started by merithes, 30 May 2014 - 10:02 AM
merithes #1
Posted 30 May 2014 - 12:02 PM
I would like that this program detect the sound lenght and then play the music repeatedly but idk why it doest, a little help please ? :)/>


term = peripheral.wrap("top")
local C,Y = term.getCursorPos()
term.setTextScale(1.5)
term.setBackgroundColor(colors.gray)
term.setTextColor(colors.yellow)
term.clear()
term.setCursorPos(1, 1)
term.write("Playing:")
term.setCursorPos(1, 2)
term.write(disk.getLabel("left"))
local name = disk.getLabel("left")
local timer = os.startTimer(0)
while true do
disk.playAudio("left")
os.pullEvent("timer")
local name = disk.getLabel("left")
if name == 13 then
  lenght =178
elseif name == cat or chirp then
  lenght = 185
elseif name == blocks then
  lenght = 345
elseif name == far then
  lenght = 174
elseif name == mall then
  lenght = 197
elseif name == mellohi then
  lenght = 96
elseif name == stal or "portalgun:wantyougone" then
  lenght = 150
elseif name == strad then
  lenght = 183
elseif name == ward then
  lenght = 251
elseif name == 11 then
  lenght = 71
elseif name == wait then
  lenght = 238
elseif name == "portalgun:stillalive" then
  lenght = 176
elseif name == "portalgun:radioloop" then
  lenght = 22
end
term.clear()
term.setCursorPos(1, 1)
term.write("Playing:")
term.setCursorPos(1, 2)
term.write(disk.getLabel("left"))
os.startTimer(lenght)
end
Edited on 30 May 2014 - 01:48 PM
Bomb Bloke #2
Posted 30 May 2014 - 03:52 PM
This line here:

os.startTimer(lenght)

… triggers a timer event that'll go into the event queue after "lenght" seconds have passed.

This line here:

os.pullEvent("timer")

… sits and does nothing until a timer event is found in the event queue.

You could likely change that latter line into a code block that either waits until a timer event occurs, or until a disk (or I guess "disc", in this case) is removed from a drive:

while true do
	local myEvent = {os.pullEvent()}
	
	if myEvent[1] == "timer" or myEvent[1] == "disk_eject" then break end
end

Ideally you'd confirm the timer event is the one generated by the timer you created, or that the disk ejected from the drive you were playing from, but hopefully this gives you the general idea. By all means ask questions if you're not sure what I'm on about.
Edited on 30 May 2014 - 01:52 PM
merithes #3
Posted 30 May 2014 - 04:15 PM
it still don't work :(/> I don't know if I failed at copying what you told me to do but it still play the music only one time :/

here's the pastebin, if you could try to resolve it that would be mega great :)/>


pastebin get LzY1u6V7 PlayAudio
merithes #4
Posted 30 May 2014 - 04:25 PM
juste noticed, by changing the disc, that it works well except for portalgun:radioloop
Edited on 30 May 2014 - 02:31 PM