Advertisement
Xylic

[ComputerCraft Tweaked 1.17.1] disk player 2 (use with music-turtle)

Oct 16th, 2021 (edited)
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.26 KB | None | 0 0
  1. local trackList = {}
  2. local modemSide = "top"
  3. local musicTurtleID = 5
  4. local termWidth, termHeight = term.getSize()
  5. local menuFocus = 1
  6. local AudioIsPlay = false
  7. local PlayTimer = 0
  8. local startTime = 0
  9. local DiskDriveIsPresent = false
  10. local needReDraw = true
  11. local installedDisk = "no disk"
  12. local diskDriveStateChange = false
  13. ---------------------------------------------------------------
  14. local DefaultDiskDriveSide = "right"
  15. -- local Tracks = {"C418 - 13","C418 - cat","C418 - blocks","C418 - chirp","C418 - far","C418 - mall","C418 - mellohi","C418 - stal","C418 - strad","C418 - ward","C418 - 11","C418 - wait"}
  16. local Tracks = {
  17.     ["C418 - 13"] = 178,
  18.     ["C418 - cat"] = 188,
  19.     ["C418 - blocks"] = 345,
  20.     ["C418 - chirp"] = 188,
  21.     ["C418 - far"] = 174,
  22.     ["C418 - mall"] = 197,
  23.     ["C418 - mellohi"] = 97,
  24.     ["C418 - stal"] = 150,
  25.     ["C418 - strad"] = 189,
  26.     ["C418 - ward"] = 251,
  27.     ["C418 - 11"] = 71,
  28.     ["C418 - wait"] = 238,
  29.     ["Lena Raine - otherside"] = 196,
  30.     ["Lena Raine - Pigstep"] = 149,
  31.     ["Samuel Åberg - 5"] = 179,
  32.     ["no disk"] = 0,
  33.     ["disk has no audio!"] = 0
  34. }
  35. -- local TrackDurability = {178,188,345,188,174,197,97,150,189,251,71,238}
  36. local TotalTrackCount = 12
  37. local exitFromProgram = false
  38. local diskID = 0
  39. local AutoPlayMode = false
  40. local UserState = false
  41. local DiskList = {}
  42. local Debug = false
  43.  
  44. ------------------------SIMPLE GRAPHIC FUNCTIONS-------------------------------
  45. function drawLine(x1,y1,x2,y2,...)
  46.     local function setPixel(x,y,monitorOrTerm)
  47.         monitorOrTerm.setCursorPos(x,y)
  48.         monitorOrTerm.write("-")
  49.     end
  50.  
  51.     local args = {...}
  52.     local monitorOrTerm = term
  53.     if args[1] ~= nil then monitorOrTerm = peripheral.wrap(args[1]) end
  54.  
  55.     local dx = x2 - x1
  56.     local dy = y2 - y1
  57.  
  58.     local sign_x = (dx > 0 and 1 or -1)
  59.     local sign_y = (dy > 0 and 1 or -1)
  60.  
  61.     if dx < 0 then dx = dx * -1 end
  62.     if dy < 0 then dy = dy * -1 end
  63.  
  64.     local pdx, pdy
  65.     local es, el
  66.  
  67.     if dx > dy then
  68.         pdx, pdy = sign_x, 0
  69.         es, el = dy, dx
  70.     else
  71.         pdx, pdy = 0, sign_y
  72.         es, el = dx, dy
  73.     end
  74.  
  75.     local x, y = x1, y1
  76.     local err, t = el/2, 0
  77.  
  78.     setPixel(x,y,monitorOrTerm)
  79.  
  80.     while t < el do
  81.         err = err - es
  82.         if err < 0 then
  83.             err = err + el
  84.             x = x + sign_x
  85.             y = y + sign_y
  86.         else
  87.             x = x + pdx
  88.             y = y + pdy
  89.         end
  90.         t = t + 1
  91.         setPixel(x,y,monitorOrTerm)
  92.     end
  93. end
  94. ---------------------------------------------------------------
  95. function getTrackList()
  96.     rednet.send(musicTurtleID,"getList")
  97.     local from, msg = rednet.receive()
  98.     local tracks = {}
  99.  
  100.     if from == musicTurtleID then
  101.         for i=1, tonumber(msg) do
  102.             from, trackName = rednet.receive()
  103.             table.insert(tracks,trackName)
  104.         end
  105.  
  106.         from, msg = rednet.receive()
  107.         if msg == "OK" then
  108.             term.write("Get "..#tracks.." tracks!")
  109.         end
  110.     end
  111.     return tracks
  112. end
  113.  
  114. function next()
  115.     rednet.send(musicTurtleID,"next")
  116.     local from, msg = rednet.receive()
  117.     if msg == "OK" and from == musicTurtleID then
  118.         AudioIsPlay = true
  119.         startTime = os.clock()
  120.         disk.playAudio(DefaultDiskDriveSide)
  121.     end
  122. end
  123.  
  124. function set(trackName)
  125.     rednet.send(musicTurtleID,"set")
  126.     rednet.send(musicTurtleID,trackName)
  127.  
  128.     local from, msg = rednet.receive()
  129.     if msg == "OK" and from == musicTurtleID then
  130.         --term.write("Set!")
  131.     end
  132. end
  133.  
  134. function writeTextCenter(y,text)
  135.     term.setCursorPos(termWidth/2-(string.len(text)/2),y)
  136.     term.write(text)
  137. end
  138.  
  139. function writeText(x,y,text)
  140.     term.setCursorPos(x,y)
  141.     term.write(text)
  142. end
  143.  
  144. function setToNormalTime(durability)
  145.     local minutes = 0
  146.     durability = math.floor(durability)
  147.  
  148.     while durability >= 60 do
  149.         minutes = minutes + 1
  150.         durability = durability - 60
  151.     end
  152.     if string.len(durability)==1 then
  153.         return "0"..minutes..":0"..durability
  154.     else
  155.         return "0"..minutes..":"..durability
  156.     end
  157. end
  158.  
  159.  
  160. function progressBar(durability, timer)
  161.     local maxWidth = termWidth
  162.     local percent = timer/durability*100
  163.     local progress = math.floor(maxWidth * percent / 100)
  164.     local progressBar = ""
  165.  
  166.     for i=1, progress do
  167.         progressBar = progressBar.."#"
  168.     end
  169.  
  170.     return progressBar
  171. end
  172.  
  173. function newMenu(menu)
  174.     table.insert(menu,"back")
  175.     local menuFocus = 1
  176.     local exitMenu = false
  177.     local PressedKey = 0
  178.  
  179.     while exitMenu==false do
  180.         term.clear()
  181.         for key, value in pairs(menu) do
  182.             if menuFocus == key then
  183.                 local xPos = (termWidth/2)-(string.len(menu[key])/2)-2
  184.                 local yPos = key+(termHeight/2)-(#menu/2)
  185.                 term.setCursorPos(xPos,yPos)
  186.                 term.write("->"..value)
  187.             else
  188.                 local xPos = (termWidth/2)-(string.len(menu[key])/2)
  189.                 local yPos = key+(termHeight/2)-(#menu/2)
  190.                 term.setCursorPos(xPos,yPos)
  191.                 term.write(value)
  192.             end    
  193.         end
  194.  
  195.     ----------------------KeyPress Event and Timeout-----------------
  196.         local timeout = os.startTimer(.2)
  197.         while true do
  198.             local event = {os.pullEvent()}
  199.             if event[1] == "key" then
  200.                 PressedKey = event[2]
  201.             elseif event[1] == "timer" and event[2] == timeout then
  202.                 break
  203.             end
  204.         end
  205.  
  206.         ----------------KeyPress and menuFocus-----------------
  207.  
  208.         if PressedKey ~= 0 then
  209.             if PressedKey == 265 then
  210.                 menuFocus=menuFocus-1
  211.                 PressedKey=0
  212.             elseif PressedKey == 264 then
  213.                 menuFocus=menuFocus+1
  214.                 PressedKey=0
  215.             elseif PressedKey == 257 then
  216.                 if menu[menuFocus] == "back" then
  217.                     exitMenu = true
  218.                 else
  219.                     return menu[menuFocus]
  220.                 end
  221.                 PressedKey=0
  222.             end
  223.         else
  224.             if menuFocus < 1 then
  225.                 menuFocus = #menu
  226.             elseif menuFocus > #menu then
  227.                 menuFocus = 1
  228.             end
  229.         end
  230.  
  231.     end
  232. end
  233.  
  234. -----------------------------------------------------------------------------
  235.  
  236. --search of a disk drive
  237. local devices = peripheral.getNames()
  238.     for i=1, #devices do
  239.         if peripheral.getType(devices[i]) == "drive" then
  240.             DiskDriveIsPresent = true
  241.             DefaultDiskDriveSide = devices[i]
  242.             break
  243.         end
  244.     end
  245.  
  246.  
  247.  
  248. rednet.open(modemSide)
  249.  
  250. while rednet.isOpen() do
  251.     local timeout = os.startTimer(.2)
  252.     local event = {os.pullEvent()}
  253.     if event[1] == "key" and event[2] == 257 then --play/stop
  254.         if AudioIsPlay then
  255.             --stop playing
  256.             AudioIsPlay = false
  257.             startTime = 0
  258.             disk.stopAudio(DefaultDiskDriveSide)
  259.             PlayTimer = 0
  260.         else
  261.             --playing
  262.             AudioIsPlay = true
  263.             startTime = os.clock()
  264.             disk.playAudio(DefaultDiskDriveSide)
  265.         end
  266.         needReDraw = true
  267.     elseif event[1] == "key" and event[2] == 76 then --list menu L
  268.         term.clear()
  269.         writeTextCenter(termHeight/2,"Waiting for list downloading...")
  270.  
  271.         trackList = getTrackList()
  272.         if #trackList > 0 then
  273.             local selectedTrack = newMenu(trackList)
  274.             term.clear()
  275.             writeTextCenter(termHeight/2,"Changing disk to "..selectedTrack)
  276.             set(selectedTrack)
  277.             AudioIsPlay = true
  278.             startTime = os.clock()
  279.             disk.playAudio(DefaultDiskDriveSide)
  280.         end
  281.  
  282.         needReDraw = true
  283.     elseif event[1] == "key" and event[2] == 65 then --automode On/Off A
  284.         AutoPlayMode = not AutoPlayMode
  285.     elseif event[1] == "key" and event[2] == 78 then --next disk N
  286.         next()
  287.     elseif event[1] == "key" and event[2] == 81 then --QUIT Q
  288.         term.clear()
  289.         term.setCursorPos(1,1)
  290.         rednet.close()
  291.     end
  292.     ---------------------------------checking disk is disk drive----------------------------------------
  293.     if disk.isPresent(DefaultDiskDriveSide) ~= diskDrivePresentState then
  294.         if disk.hasAudio(DefaultDiskDriveSide) then
  295.             installedDisk = disk.getAudioTitle(DefaultDiskDriveSide)
  296.         else
  297.             installedDisk = "no disk"
  298.             AudioIsPlay = false
  299.             startTime = 0
  300.             PlayTimer = 0
  301.         end
  302.     else
  303.         installedDisk = "no disk"
  304.         --------------------------
  305.         AudioIsPlay = false
  306.         startTime = 0
  307.         PlayTimer = 0
  308.         -------------------------
  309.     end
  310.  
  311.     if diskDriveStateChange then needReDraw=true end --- ??????????????????????? REALY ?
  312.     --------------------------------------------------------------------------------------------------
  313.     if AudioIsPlay then
  314.         if os.clock()-startTime >= Tracks[installedDisk] then --music is end play
  315.  
  316.             AudioIsPlay = false
  317.             startTime = 0
  318.             PlayTimer = 0
  319.             ----------------
  320.             if AutoPlayMode then next() end
  321.         end
  322.     end
  323.     ------------------------------drawing menu-------------------------------------
  324.    
  325.     if needReDraw then
  326.         local centerByY = termHeight/2
  327.         local centerByX = termWidth/2
  328.         local yShift = -3
  329.         term.clear()
  330.         drawLine(1,1,termWidth,1)
  331.         drawLine(1,centerByY+yShift+2,termWidth,centerByY+yShift+2)
  332.         writeTextCenter((termHeight/2)-3+yShift,installedDisk)
  333.  
  334.         if AudioIsPlay then
  335.             local writeString = "   Play          -> Stop"
  336.             writeTextCenter(centerByY+yShift,writeString)
  337.  
  338.             PlayTimer = setToNormalTime(os.clock()-startTime)
  339.             writeText(1,centerByY+yShift+7,PlayTimer)      
  340.         else
  341.             local writeString = "-> Play             Stop"
  342.             writeTextCenter(centerByY+yShift,writeString)
  343.  
  344.             writeText(1,centerByY+yShift+7,"00:00")
  345.         end
  346.  
  347.         writeText(termWidth-string.len(setToNormalTime(Tracks[installedDisk]))+1,centerByY+yShift+7,setToNormalTime(Tracks[installedDisk]))
  348.        
  349.         writeText(1,centerByY+yShift+5,"[")
  350.         if disk.isPresent(DefaultDiskDriveSide) and AudioIsPlay then writeText(2,centerByY+yShift+5,progressBar(Tracks[installedDisk],os.clock()-startTime)) end
  351.         writeText(termWidth-string.len("]")+1,centerByY+yShift+5,"]")
  352.  
  353.         writeText(1,termHeight,"A-APM:"..(AutoPlayMode and "on" or "off"))
  354.         writeTextCenter(termHeight,"N-Next disk")
  355.         writeText(termWidth-string.len("L-Track list")+1,termHeight,"L-Track list")
  356.         --needReDraw=false
  357.     end
  358.  
  359. end
  360.  
  361. rednet.close(modemSide)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement