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

Music Player With BuildCraft: Automtically Switch Discs

Started by Negeator, 27 April 2012 - 12:32 AM
Negeator #1
Posted 27 April 2012 - 02:32 AM
This is my first program I will be putting on the website.

Using BuildCraft, this program is able to play music discs from a chest, and can automatically switch to a different random disc at any time.

You can manually tell the program to play/stop/switch discs, or you can use an AutoPlay feature that will automatically switch to a new disc every so often. This is nice if you want various songs playing in the background automatically.

Screenshots:
Spoiler




Code:
Spoiler

--------------------------
-- Minecraft Music Player with BuildCraft
-- By: Negeator, 2012
--------------------------
-- When set up correcty, this program can automatically play and switch music discs without the player touching the disk drive.
--------------------------
--Variables--
--DO NOT MODIFY THESE--
local side, title, time
local state = "none" --What the media player is currently doing
local stateView = "" --What to display
local quit = false --Whether to exit the program or not
local timing = false --Whether the program is timing (Autoplay0
local currTime = 0 --Current Time
local visual = 0 --Visualization state variable
--MODIFY THESE IF YOU WANT--
local timeAmount = 300 --How long (in seconds) until we switch disks for Autoplay
--Check for a disk in
function checkSide()
  if disk.isPresent("top") == true and disk.hasAudio("top") == true then
	side = "top"
  elseif disk.isPresent("bottom") == true and disk.hasAudio("bottom") == true  then
	side = "bottom"
  elseif disk.isPresent("left") == true and disk.hasAudio("left") == true  then
	side = "left"
  elseif disk.isPresent("right") == true and disk.hasAudio("right") == true  then
	side = "right"
  elseif disk.isPresent("front") == true and disk.hasAudio("front") == true  then
	side = "front"
  elseif disk.isPresent("back") == true and disk.hasAudio("back") == true  then
	side = "back"
  else
side = "No Disc Found"
  end
end
--Clears the Screen
function clear()
  term.clear()
  term.setCursorPos(1,1)
end
--Display
function printInfo()
  term.write("Minecraft Music Player")
  term.setCursorPos(1,2)
  term.write(stateView)
  term.setCursorPos(1,3)
   term.write("Side: " .. side)
  if side ~= "No Disc Found" and state ~= "play" then
if disk.hasAudio(side) then
	  term.setCursorPos(1,4)
	  term.write("Disk Inserted: "..disk.getAudioTitle(side))
end
  end
  term.setCursorPos(1,5)
  if timing == true then
	term.write("Autoplay Turned ON	"..timeAmount - currTime.." Seconds Remaining.")
  else
	term.write("Autoplay Turned OFF")
  end
  term.setCursorPos(1,6)
  term.write("-------------------------------------------------")
  term.setCursorPos(1,7)
  term.write("1 - Play | 2 - Stop | 3 - Switch Disk")
  term.setCursorPos(1,8)
  term.write("4 - Autoplay | 0 - Quit ")
  term.setCursorPos(1,9)
  term.write("-------------------------------------------------")
  term.setCursorPos(1,10)
end
--Does stuff based on the state variable
function musicControl()
  --Play the disk automatically if in Autoplay mode
  if timing == true and state ~= "stop" then
	if state ~= "play" then
   state = "play"
end
  end

  if state == "play" then
	if side ~= "No Disc Found" then
   if stateView ~= "<Playing>"  then
	 disk.playAudio(side)
	 stateView = "<Playing>"
   end
end
  elseif state == "stop" then
	if side ~= "No Disc Found" then
   timing = false
   disk.stopAudio(side)
   stateView = "<Stopped>"
end
  end

  --If no disc is present, change the display
  if side == "No Disc Found" then
	stateView = "<INSERT DISC>"
  end

  --Make sure it displays as stopped when a new disc is just inserted
  if stateView == "<INSERT DISC" and side ~= "No Disc Found" then
	stateView = "<Stopped>"
  end
  state = ""

end
--Switch Disks
function cycleDisk()
  clear()
  term.write("Switching Disk")
  rs.setOutput("bottom",true)
  sleep(1)
  rs.setOutput("bottom",false)
  if timing == true then
	os.startTimer(9) --Wait for new Disc
currTime = 0
  end
end
--Updates the timer
function updateTimer()
  if timing == true then
	currTime = currTime + 1
	if currTime >= timeAmount then
	  currTime = 0
   cycleDisk()
   os.startTimer(9) --Wait for new Disc
else
   os.startTimer(1)
end
  end
end
--Main Function
function main()
  while quit == false do
	clear()
	checkSide()
   musicControl()
	printInfo()
	event, p1, p2 = os.pullEvent()
	if event == "key" then
	  if p1 == 2 then
	 state = "play"
   elseif p1 == 3 then
	 state = "stop"
	  elseif p1 == 4 then
		cycleDisk()
	  elseif p1 == 5 then
		if timing == true then
	timing = false
  else
	timing = true
	currTime = 0
	os.startTimer(1)
  end
  sleep(.25)
	  elseif p1 == 11 then
	 clear()
		quit = true
	  end
	elseif event == "timer" then
   updateTimer()
end

  end
end
--START PROGRAM--
main()
--END PROGRAM--

How to Set Up:
(See Pictures)
Place your computer and a disk drive next to it. Then place a chest next to the disk drive with a wooden pipe between them. Place a wooden pipe behind the chest and behind the disk drive, and then connect the wooden pipes with cobblestone pipes. You then need to place a redstone engine beneath both wooden pipes connected to the disk drive (one powers a disc in, the other powers a disc out). Place redstone directly underneath the computer (the program outputs redstone through the bottom), and connect the redstone to both engines. If you did this corectly, turning the redstone on should power both the the redstone engines, which pipes things in/out of the disk drive. Then just place your music discs in the chest.

Known Issues:
Unfortunately BuildCraft pipes take the item in the very first slot out of the chest, meaning things are not taken out entirely "randomly." This will lead to only a few discs ending up being cycled through. To solve this you may be able to set up a more sophisticated machine to allow more variety.


If you find any problems or have any suggestions feel free to say them here.
JonathanVB #2
Posted 05 July 2012 - 07:22 PM
I liked the idea behind this, but the "known issue" was too critical (after some time it only plays two disks). I managed to solve it by using a completely different system, though yours seems more elegant, mine is more correct. I have used some snippets of your code, which I will change if asked.

http://www.computercraft.info/forums2/index.php?/topic/2420-truly-random-or-sequential-music-player/

I also had the problem (with your player) that the wooden pipes wouldn't take the disk from the drive, which I fixed by rotating the disk drive, ejecting the disk, and picking it up with an obsidian pipe.
Deathknight0897 #3
Posted 06 July 2012 - 11:05 AM
why do it this way with build craft use a series of computers connected via bundled cable have each one on a different color so 3 pcs bundled cable in the back on all of them then a color yo out of the front you know what actually if your willing to wait i will look into this and make one an post it i see loads of these requests im going to make a fully digital player and show you how to do it
Mr_Spiffy #4
Posted 07 September 2012 - 10:36 PM
There seems to be a problem with the Autoplay feature for me. What it does is it just repeatedly plays the very beginning of the song that is in the disk drive over and over again like a broken record or something. I would like some help on this as the Autoplay feature was the only reason I was interested in the program.
waffles05 #5
Posted 03 November 2012 - 06:02 AM
I think this is a good idea and program and looks quite good, the only thing stopping it from working is mods not this. Your program is really good, are you a beginner? Because if you are that program is amazing for someone like you.

You don't need the wooden pipe behind the chest though, as nothing is being taken out from that side
SomniusX #6
Posted 13 November 2012 - 03:35 AM
can we use midi files instead of music disk which are predefined ?
Llurendt #7
Posted 31 May 2013 - 12:30 AM
Hello! I am really enjoying your program! I have run into a problem, though, which I can't seem to figure out the solution to, haha. How do you set autoplay to be on by default? I have changed trues and falses and anything else I can think of, yet the only thing I can accomplish is either to simply break it, or to make it SAY that it is on, but not actually be on, lol. Can you help me out? :D/>
techno156 #8
Posted 31 May 2013 - 03:36 AM
Hm… I think using turtles should work, if someone is able to program the turtle to choose a random slot on the chest. Whoops. XD I didn't read it properly. :P/> Maybe you could have a pipe loop, so that the discs would choose a random pipe themselves?
Llurendt #9
Posted 31 May 2013 - 09:00 PM
Oh, I resolved the problem of changing discs using buildcraft filters and pneumatic pipes, with chests in between, but still have the issue of autoplay being turned off by default, lol.