Advertisement
Fyrhtu

startup.lua

Mar 27th, 2023 (edited)
2,589
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.66 KB | Gaming | 0 0
  1. --[[
  2.  
  3. Jukebox program
  4. Original By The Juice
  5. Minor Update by Fyrhtu (added Lena Raine disks)
  6.  
  7. version 1.2.0
  8.  
  9. Free to distribute/alter
  10. so long as proper credit to original
  11. author is maintained.
  12.  
  13. Simply connect some drives with music disks
  14. and an advanced monitor(at least two blocks wide)
  15. in any way and start this program
  16.  
  17. --]]
  18.  
  19.  
  20. function loadPref()
  21.  if not fs.exists("jukeboxconfig") then
  22.   setColors.text=colors.white
  23.   setColors.background=colors.black
  24.   setColors.skip=colors.blue
  25.   setColors.enabled=colors.green
  26.   setColors.disabled=colors.red
  27.   setColors.selected=colors.green
  28.   setColors.progress1=colors.lightBlue
  29.   setColors.progress2=colors.cyan
  30.   setColors.progress3=colors.blue
  31.   setColors.prefBack=colors.blue
  32.   setColors.prefText=colors.white
  33.  
  34.  else
  35.   local fil=fs.open("jukeboxconfig","r")
  36.  
  37.   setColors.text=tonumber(fil.readLine())
  38.   setColors.background=tonumber(fil.readLine())
  39.   setColors.skip=tonumber(fil.readLine())
  40.   setColors.enabled=tonumber(fil.readLine())
  41.   setColors.disabled=tonumber(fil.readLine())
  42.   setColors.selected=tonumber(fil.readLine())
  43.   setColors.progress1=tonumber(fil.readLine())
  44.   setColors.progress2=tonumber(fil.readLine())
  45.   setColors.progress3=tonumber(fil.readLine())
  46.   setColors.prefBack=tonumber(fil.readLine())
  47.   setColors.prefText=tonumber(fil.readLine())
  48.  
  49.  
  50.   playing=fil.readLine()=="true"
  51.   shuffle=fil.readLine()=="true"
  52.   playingDefault=playing
  53.   shuffleDefault=shuffle
  54.  
  55.   fil.close()
  56.  end
  57. end
  58.  
  59. function savePref()
  60.  fil=fs.open("jukeboxconfig","w")
  61.  fil.writeLine(setColors.text)
  62.  fil.writeLine(setColors.background)
  63.  fil.writeLine(setColors.skip)
  64.  fil.writeLine(setColors.enabled)
  65.  fil.writeLine(setColors.disabled)
  66.  fil.writeLine(setColors.selected)
  67.  fil.writeLine(setColors.progress1)
  68.  fil.writeLine(setColors.progress2)
  69.  fil.writeLine(setColors.progress3)
  70.  fil.writeLine(setColors.prefBack)
  71.  fil.writeLine(setColors.prefText)
  72.  
  73.  
  74.  fil.writeLine(playingDefault)
  75.  fil.writeLine(shuffleDefault)
  76.  
  77.  fil.close()
  78. end
  79.  
  80. function changePref()
  81.  while true do
  82.   mon.setBackgroundColor(colors.black)
  83.   mon.setTextColor(colors.white)
  84.   mon.clear()
  85.  
  86.   mon.setCursorPos(1,1)
  87.   mon.write("Preferences")
  88.  
  89.   mon.setCursorPos(1,3)
  90.   mon.write("Edit colors")
  91.  
  92.   mon.setCursorPos(1,4)
  93.   if playingDefault then
  94.    mon.setBackgroundColor(colors.green)
  95.   else
  96.    mon.setBackgroundColor(colors.red)
  97.   end
  98.   mon.write("Play by default")
  99.  
  100.   mon.setCursorPos(1,5)
  101.   if shuffleDefault then
  102.    mon.setBackgroundColor(colors.green)
  103.   else
  104.    mon.setBackgroundColor(colors.red)
  105.   end
  106.   mon.write("Shuffle by default")
  107.  
  108.   mon.setBackgroundColor(colors.black)
  109.  
  110.   mon.setCursorPos(1,7)
  111.   mon.write("Save")
  112.  
  113.   mon.setCursorPos(1,8)
  114.   mon.write("Load")
  115.  
  116.   mon.setCursorPos(1,9)
  117.   mon.write("Back")
  118.  
  119.   mon.setCursorPos(1,11)
  120.   mon.write("Close jukebox")
  121.  
  122.  
  123.   local eve,id,cx,cy
  124.   eve,id,cx,cy=os.pullEvent("monitor_touch")
  125.  
  126.   if cy==3 then
  127.    changeColors()
  128.   elseif cy==4 then
  129.    playingDefault=not playingDefault
  130.   elseif cy==5 then
  131.    shuffleDefault=not shuffleDefault
  132.   elseif cy==7 then
  133.    savePref()
  134.   elseif cy==8 then
  135.    loadPref()
  136.   elseif cy==9 then
  137.    return false
  138.   elseif cy==11 then
  139.    return true
  140.   end
  141.  
  142.  end
  143. end
  144.  
  145. function changeColors()
  146.  while true do
  147.   mon.setBackgroundColor(colors.black)
  148.   mon.clear()
  149.  
  150.  
  151.   mon.setCursorPos(1,1)
  152.   mon.setTextColor(setColors.text)
  153.   if setColors.text==colors.black then
  154.    mon.setBackgroundColor(colors.white)
  155.   end
  156.   mon.write("Text")
  157.  
  158.   mon.setCursorPos(1,2)
  159.   mon.setBackgroundColor(setColors.background)
  160.   if setColors.background==colors.white then
  161.    mon.setTextColor(colors.black)
  162.   else
  163.    mon.setTextColor(colors.white)
  164.   end
  165.   mon.write("Background")
  166.  
  167.   mon.setCursorPos(1,3)
  168.   mon.setBackgroundColor(setColors.skip)
  169.   if setColors.skip==colors.white then
  170.    mon.setTextColor(colors.black)
  171.   else
  172.    mon.setTextColor(colors.white)
  173.   end
  174.   mon.write("Skip buttons")
  175.  
  176.   mon.setCursorPos(1,4)
  177.   mon.setBackgroundColor(setColors.enabled)
  178.   if setColors.enabled==colors.white then
  179.    mon.setTextColor(colors.black)
  180.   else
  181.    mon.setTextColor(colors.white)
  182.   end
  183.   mon.write("Enabled")
  184.  
  185.   mon.setCursorPos(1,5)
  186.   mon.setBackgroundColor(setColors.disabled)
  187.   if setColors.disabled==colors.white then
  188.    mon.setTextColor(colors.black)
  189.   else
  190.    mon.setTextColor(colors.white)
  191.   end
  192.   mon.write("Disabled")
  193.  
  194.   mon.setCursorPos(1,6)
  195.   mon.setBackgroundColor(setColors.selected)
  196.   if setColors.selected==colors.white then
  197.    mon.setTextColor(colors.black)
  198.   else
  199.    mon.setTextColor(colors.white)
  200.   end
  201.   mon.write("Selected track")
  202.  
  203.   mon.setCursorPos(1,7)
  204.   mon.setBackgroundColor(setColors.progress1)
  205.   if setColors.progress1==colors.white then
  206.    mon.setTextColor(colors.black)
  207.   else
  208.    mon.setTextColor(colors.white)
  209.   end
  210.   mon.write("Progress1")
  211.  
  212.   mon.setCursorPos(1,8)
  213.   mon.setBackgroundColor(setColors.progress2)
  214.   if setColors.progress2==colors.white then
  215.    mon.setTextColor(colors.black)
  216.   else
  217.    mon.setTextColor(colors.white)
  218.   end
  219.   mon.write("Progress2")
  220.  
  221.   mon.setCursorPos(1,9)
  222.   mon.setBackgroundColor(setColors.progress3)
  223.   if setColors.progress3==colors.white then
  224.    mon.setTextColor(colors.black)
  225.   else
  226.    mon.setTextColor(colors.white)
  227.   end
  228.   mon.write("Progress3")
  229.  
  230.   mon.setCursorPos(1,10)
  231.   mon.setBackgroundColor(setColors.prefBack)
  232.   if setColors.prefBack==colors.white then
  233.    mon.setTextColor(colors.black)
  234.   else
  235.    mon.setTextColor(colors.white)
  236.   end
  237.   mon.write("p button backgnd")
  238.  
  239.   mon.setCursorPos(1,11)
  240.   mon.setTextColor(setColors.prefText)
  241.   if setColors.prefText==colors.black then
  242.    mon.setBackgroundColor(colors.white)
  243.   else
  244.    mon.setBackgroundColor(colors.black)
  245.   end
  246.   mon.write("p button text")
  247.  
  248.   mon.setCursorPos(1,13)
  249.   mon.setBackgroundColor(colors.black)
  250.   mon.setTextColor(colors.white)
  251.   mon.write("back")
  252.  
  253.  
  254.   local eve,id,cx,cy
  255.   eve,id,cx,cy=os.pullEvent("monitor_touch")
  256.  
  257.   if cy==1 then
  258.    setColors.text=colorPicker()
  259.   elseif cy==2 then
  260.    setColors.background=colorPicker()
  261.   elseif cy==3 then
  262.    setColors.skip=colorPicker()
  263.   elseif cy==4 then
  264.    setColors.enabled=colorPicker()
  265.   elseif cy==5 then
  266.    setColors.disabled=colorPicker()
  267.   elseif cy==6 then
  268.    setColors.selected=colorPicker()
  269.   elseif cy==7 then
  270.    setColors.progress1=colorPicker()
  271.   elseif cy==8 then
  272.    setColors.progress2=colorPicker()
  273.   elseif cy==9 then
  274.    setColors.progress3=colorPicker()
  275.   elseif cy==10 then
  276.    setColors.prefBack=colorPicker()
  277.   elseif cy==11 then
  278.    setColors.prefText=colorPicker()
  279.   elseif cy==13 then
  280.    return
  281.   end
  282.  
  283.  end
  284. end
  285.  
  286. function colorPicker()
  287.  mon.setCursorPos(15,1)
  288.  mon.setBackgroundColor(colors.white)
  289.  mon.write(" ")
  290.  mon.setBackgroundColor(colors.lightGray)
  291.  mon.write(" ")
  292.  mon.setBackgroundColor(colors.gray)
  293.  mon.write(" ")
  294.  mon.setBackgroundColor(colors.black)
  295.  mon.write(" ")
  296.  
  297.  mon.setCursorPos(15,2)
  298.  mon.setBackgroundColor(colors.blue)
  299.  mon.write(" ")
  300.  mon.setBackgroundColor(colors.cyan)
  301.  mon.write(" ")
  302.  mon.setBackgroundColor(colors.lightBlue)
  303.  mon.write(" ")
  304.  mon.setBackgroundColor(colors.brown)
  305.  mon.write(" ")
  306.  
  307.  mon.setCursorPos(15,3)
  308.  mon.setBackgroundColor(colors.green)
  309.  mon.write(" ")
  310.  mon.setBackgroundColor(colors.lime)
  311.  mon.write(" ")
  312.  mon.setBackgroundColor(colors.orange)
  313.  mon.write(" ")
  314.  mon.setBackgroundColor(colors.yellow)
  315.  mon.write(" ")
  316.  
  317.  mon.setCursorPos(15,4)
  318.  mon.setBackgroundColor(colors.red)
  319.  mon.write(" ")
  320.  mon.setBackgroundColor(colors.purple)
  321.  mon.write(" ")
  322.  mon.setBackgroundColor(colors.magenta)
  323.  mon.write(" ")
  324.  mon.setBackgroundColor(colors.pink)
  325.  mon.write(" ")
  326.  
  327.  
  328.  local eve,id,cx,cy
  329.  repeat
  330.   eve,id,cx,cy=os.pullEvent("monitor_touch")
  331.  until cx>=15 and cy<=4 and cx<=18 and cy>=1
  332.  cx=cx-14
  333.  
  334.  if cx==1 and cy==1 then
  335.   return colors.white
  336.  elseif cx==2 and cy==1 then
  337.   return colors.lightGray
  338.  elseif cx==3 and cy==1 then
  339.   return colors.gray
  340.  elseif cx==4 and cy==1 then
  341.   return colors.black
  342.  elseif cx==1 and cy==2 then
  343.   return colors.blue
  344.  elseif cx==2 and cy==2 then
  345.   return colors.cyan
  346.  elseif cx==3 and cy==2 then
  347.   return colors.lightBlue
  348.  elseif cx==4 and cy==2 then
  349.   return colors.brown
  350.  elseif cx==1 and cy==3 then
  351.   return colors.green
  352.  elseif cx==2 and cy==3 then
  353.   return colors.lime
  354.  elseif cx==3 and cy==3 then
  355.   return colors.orange
  356.  elseif cx==4 and cy==3 then
  357.   return colors.yellow
  358.  elseif cx==1 and cy==4 then
  359.   return colors.red
  360.  elseif cx==2 and cy==4 then
  361.   return colors.purple
  362.  elseif cx==3 and cy==4 then
  363.   return colors.magenta
  364.  elseif cx==4 and cy==4 then
  365.   return colors.pink
  366.  else
  367.   error("uh oh! tell my daddy im broke!")
  368.  end
  369. end
  370.  
  371.  
  372.  
  373. function restart() --restarts playback (more useful than it sounds)
  374.  stop()
  375.  play()
  376. end
  377.  
  378. function stop() --stops playback
  379.  playing=false
  380.  os.cancelTimer(timer)
  381.  os.cancelTimer(tickTimer)
  382.  elapsed=0
  383.  disk.stopAudio()
  384. end
  385.  
  386. function play() --starts playback
  387.  playing=true
  388.  timer=os.startTimer(lengths[disks[track]])
  389.  tickTimer=os.startTimer(0.25)
  390.  drives[track].playAudio()
  391. end
  392.  
  393. function skip() --skips to the next track
  394.  if shuffle then
  395.   local rd
  396.   repeat
  397.    rd=math.random(#drives)
  398.   until rd~=track
  399.   track=rd
  400.  else
  401.   track=track+1
  402.   if track>#disks then
  403.    track=1
  404.   end
  405.  end
  406.  restart() --see?
  407. end
  408.  
  409. function back() --goes back to the previous track
  410.  track=track-1
  411.  if track<1 then
  412.   track=#disks
  413.  end
  414.  restart()
  415. end
  416.  
  417. function skipto(tr) --skips to a particular track according to 'tr'
  418.  if tr==track then
  419.   return
  420.  end
  421.  track=tr
  422.  if track>#disks or track<1 then
  423.   return
  424.  end
  425.  restart()
  426. end
  427.  
  428.  
  429. -------------------------------------------------------------------------------
  430.  
  431.  
  432. disk.stopAudio() --stop all currently playing disks
  433.  
  434. lengths={} -- length of all the disks in seconds
  435. lengths["C418 - 13"]=180
  436. lengths["C418 - cat"]=187
  437. lengths["C418 - blocks"]=347
  438. lengths["C418 - chirp"]=187
  439. lengths["C418 - far"]=176
  440. lengths["C418 - mall"]=199
  441. lengths["C418 - mellohi"]=98
  442. lengths["C418 - stal"]=152
  443. lengths["C418 - strad"]=190
  444. lengths["C418 - ward"]=253
  445. lengths["C418 - 11"]=73
  446. lengths["C418 - wait"]=240
  447. lengths["Lena Raine - otherside"]=195
  448. lengths["Lena Raine - Pigstep"]=248
  449.  
  450. per=peripheral.getNames()
  451. drives={} --all the drives with audio wrapped in one variable. handy, right?
  452. for k,v in pairs(per) do
  453.  if peripheral.getType(v)=="drive" then
  454.   if peripheral.wrap(v).hasAudio() then
  455.    drives[#drives+1]=peripheral.wrap(v)
  456.   end
  457.  elseif peripheral.getType(v)=="monitor" then
  458.   mon=peripheral.wrap(v)
  459.  end
  460. end
  461. per=nil
  462.  
  463.  
  464. disks={} --the name of the disk in the drive in the same corresponding 'drives' drive
  465. for k,v in pairs(drives) do
  466.  disks[k]=drives[k].getAudioTitle()
  467. end
  468.  
  469.  
  470.  
  471.  
  472.  
  473. setColors={}
  474.  
  475. playing=false --i'm not going to insult you by explaining this one
  476. shuffle=false --when true; selects a random track when track is over
  477. playingDefault=false
  478. shuffleDefault=false
  479. track=1 --selected track
  480. timer=0 --token of the timer that signals the end of a track
  481. elapsed=0 --time that track was started
  482. tickTimer=0 --token of the timer that signals to update 'elapsed'
  483.  
  484. loadPref()
  485.  
  486.  
  487. --------------------------------------------------------------------------------
  488.  
  489. if shuffle then
  490.  skip()
  491. end
  492. if playing then
  493.  play()
  494. end
  495.  
  496. repeat --main loop
  497.  
  498.  --refresh display
  499.  mon.setTextColor(setColors.text)
  500.  mon.setBackgroundColor(setColors.background) --clearing
  501.  mon.clear()
  502.  
  503.  mon.setCursorPos(1,1) --drawing back, play, skip, and suffle
  504.  mon.setBackgroundColor(setColors.skip)
  505.  mon.write("<-")
  506.  
  507.  mon.setCursorPos(6,1)
  508.  mon.setBackgroundColor(setColors.skip)
  509.  mon.write("->")
  510.  
  511.  mon.setCursorPos(4,1)
  512.  if playing then
  513.   mon.setBackgroundColor(setColors.enabled)
  514.  else
  515.   mon.setBackgroundColor(setColors.disabled)
  516.  end
  517.  mon.write(">")
  518.  
  519.  mon.setCursorPos(9,1)
  520.  if shuffle then
  521.   mon.setBackgroundColor(setColors.enabled)
  522.  else
  523.   mon.setBackgroundColor(setColors.disabled)
  524.  end
  525.  mon.write("Shuffle")
  526.  
  527.  for k,v in pairs(disks) do --drawing tracks
  528.   mon.setCursorPos(1,k+2)
  529.   if k==track then
  530.    mon.setBackgroundColor(setColors.progress3)
  531.    local leng=string.len(v)
  532.    local blueLeng=(elapsed)/lengths[disks[track]]*leng
  533.    for n=1,leng do
  534.     if n>math.ceil(blueLeng) then
  535.      mon.setBackgroundColor(colors.green)
  536.     elseif n==math.ceil(blueLeng) then
  537.      if n-blueLeng<.3333 then
  538.       mon.setBackgroundColor(setColors.progress3)
  539.      elseif n-blueLeng<.6666 then
  540.       mon.setBackgroundColor(setColors.progress2)
  541.      else
  542.       mon.setBackgroundColor(setColors.progress1)
  543.      end
  544.     end
  545.     mon.write(string.sub(v,n,n))
  546.    end
  547.   else
  548.    mon.setBackgroundColor(setColors.background)
  549.    mon.write(v)
  550.   end
  551.  end
  552.  
  553.  mon.setCursorPos(18,1)
  554.  mon.setBackgroundColor(setColors.prefBack)
  555.  mon.setTextColor(setColors.prefText)
  556.  mon.write("p")
  557.  
  558.  
  559.  --wait for event
  560.  local eve,id,cx,cy
  561.  repeat
  562.   eve,id,cx,cy=os.pullEvent()
  563.  until eve=="timer" or eve=="monitor_touch"
  564.  
  565.  
  566.  --test event
  567.  if eve=="timer" then --the timer ended
  568.   if id==timer then
  569.    skip()
  570.   elseif id==tickTimer then
  571.    tickTimer=os.startTimer(0.25)
  572.    elapsed=elapsed+0.25
  573.   end
  574.  
  575.  else --the monitor was pressed
  576.   if cy>1 then --a track was pressed
  577.    if cy>2 and cy<#disks+3 then
  578.     skipto(cy-2)
  579.    end
  580.   elseif cx<=2 then --back was pressed
  581.    back()
  582.   elseif cx==4 then --stop/play was pressed
  583.    if playing then
  584.     stop()
  585.    else
  586.     play()
  587.    end
  588.   elseif cx==6 or cx==7 then --skip was pressed
  589.    skip()
  590.   elseif cx>=9 and cx<=15 then --shuffle was pressed
  591.    shuffle=not shuffle
  592.   elseif cx==18 then --preferences button was pressed
  593.    stop()
  594.    if changePref() then
  595.     return
  596.    end
  597.   end
  598.  end
  599.  
  600. until false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement