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

Repeat music

Started by Backplague, 01 June 2012 - 01:45 PM
Backplague #1
Posted 01 June 2012 - 03:45 PM
Hi!

This is a simple program i made to infinitely repeat music from a disk drive. It can detect when the disc is removed so to stop music, just take the disc out. I have not tested this on every single music disc, so the time until the song starts over may vary. Will adjust if needed.

Install:
1. Go to "%appdata%/.minecraft/mods/ComputerCraft/lua/programs"
2. Create a new file called "repeat" without any extensions
3. Open that file in Notepad or some other text editor
4. Copy-Paste the code to that file
5. Save and quit

Usage:
Just type in "repeat <side of disk drive>" to the computer console.



lengths = {["C418 - 13"]=180, ["C418 - cat"]=186, ["C418 - blocks"]=347, ["C418 - chirp"]=186, ["C418 - far"]=172, ["C418 - mall"]=197, ["C418 - mellohi"]=98, ["C418 - stal"]=151, ["C418 - strad"]=191, ["C418 - ward"]=250, ["C418 - 11"]=70}
args = { ... }
if #args == 0 then
print("Usage: repeat <side>")
return
end
command = args[1]
if disk.isPresent(command) then
if disk.hasAudio(command) then
  while disk.isPresent(command) do
   print("Playing ".. disk.getLabel(command))
   disk.playAudio(command)
  
   timesSlept = 0
   length = lengths[disk.getLabel(command)]
   while timesSlept <= length do
    if disk.isPresent(command) == false then
  print("Disk removed")
  return
end
    sleep(1)
timesSlept = timesSlept + 1
   end
  end
else
  print("Disk in ".. command .. " drive is not an audio disk")
end
else
print("No disk in ".. command .. " drive")
end

Please give feedback and suggestions if you want to.
MysticT #2
Posted 01 June 2012 - 06:31 PM
It would be better to use a timer and the "disk_eject" event instead of the sleep and disk.isPresent:

print("Playing ", disk.getLabel(command))
local lenght = lenghts[disk.getLabel(command)]
local timer = os.startTimer(lenght)
while true do
  local evt, arg = os.pullEvent()
  if evt == "disk_ejected" then
    if arg == command then
	  break
    end
  elseif evt == "timer" then
    if arg == timer then
	  disk.playAudio(command)
	  timer = os.startTimer(lenght)
    end
  end
end
It would wait for the disk to be ejected/removed or until time ends instead of checking every second.
Backplague #3
Posted 01 June 2012 - 07:14 PM
It would be better to use a timer and the "disk_eject" event instead of the sleep and disk.isPresent:

print("Playing ", disk.getLabel(command))
local lenght = lenghts[disk.getLabel(command)]
local timer = os.startTimer(lenght)
while true do
  local evt, arg = os.pullEvent()
  if evt == "disk_ejected" then
	if arg == command then
	  break
	end
  elseif evt == "timer" then
	if arg == timer then
	  disk.playAudio(command)
	  timer = os.startTimer(lenght)
	end
  end
end
It would wait for the disk to be ejected/removed or until time ends instead of checking every second.
Cool, i didnt know about that. :)/>/>
_Spy_Crab_ #4
Posted 01 September 2013 - 11:29 AM
I absolutely love this program, but I have one question. I have the Portal music, from the Portal Gun Mod, and I was wondering how you timed the music discs. Was it in seconds or ticks?
Mitchfizz05 #5
Posted 06 September 2013 - 07:58 AM
Great job, it must have been a nightmare to get all that timing right.

I absolutely love this program, but I have one question. I have the Portal music, from the Portal Gun Mod, and I was wondering how you timed the music discs. Was it in seconds or ticks?
Looking at the code, it would be seconds.
Geforce Fan #6
Posted 07 September 2013 - 08:00 PM
How did you time the disks? Did you some sort of CC program, or just did you just use a stopwatch?