Advertisement
zefie

Minecraft Radio Thing (OpenComputers + Computronics Tapes)

Feb 16th, 2021 (edited)
1,333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.61 KB | None | 0 0
  1. local component = require("component")
  2. local shell = require("shell")
  3. local event = require("event")
  4. local args, options = shell.parse(...)
  5.  
  6. -- 'Smart' Computronics Tape Player Script
  7. -- Automatically rewinds and loops tape
  8.  
  9.  
  10. if not component.isAvailable("tape_drive") then
  11.   io.stderr:wrte("This program requires a tape drive to run.")
  12.   return
  13. end
  14.  
  15. local function tapePosToTime(pos,vers)
  16.   local multi
  17.   if vers == 1 then
  18.      multi = 4096 --32768hz
  19.   else
  20.      multi = 6000 --48000hz
  21.   end
  22.  
  23.   local seconds = tonumber(pos) / multi
  24.  
  25.   if seconds <= 0 then
  26.     return "00:00:00";
  27.   else
  28.     hours = string.format("%02.f", math.floor(seconds/3600));
  29.     mins = string.format("%02.f", math.floor(seconds/60 - (hours*60)));
  30.     secs = string.format("%02.f", math.floor(seconds - hours*3600 - mins*60));
  31.     return hours..":"..mins..":"..secs
  32.   end
  33. end
  34.  
  35. local function getTapeDrive()
  36.   --Credits to gamax92 for this
  37.   --Taken from Computronics tape.lua
  38.   local tape
  39.   if options.address then
  40.     if type(options.address) ~= "string" then
  41.       io.stderr:write("'address' may only be a string.")
  42.       return
  43.     end
  44.     local fulladdr = component.get(options.address)
  45.     if fulladdr == nil then
  46.       io.stderr:write("No component at this address.")
  47.       return
  48.     end
  49.     if component.type(fulladdr) ~= "tape_drive" then
  50.       io.stderr:write("No tape drive at this address")
  51.       return
  52.     end
  53.     tape = component.proxy(fulladdr)
  54.   else
  55.     tape = component.tape_drive
  56.   end
  57.   return tape
  58. end
  59.  
  60. local function handleTape(addr)
  61.   os.execute("cls")
  62.   local tape
  63.   if addr ~= nil then
  64.     tape = getTapeDrive(addr)
  65.   else
  66.     tape = getTapeDrive()
  67.   end
  68.  
  69.   if not tape.isReady() then
  70.     io.stderr:write("The tape drive does not contain a tape.")
  71.     return
  72.   end
  73.  
  74.   local tape_label = tape.getLabel()
  75.   local tape_length = tape.getSize()
  76.  
  77.   local tape_type
  78.  
  79.   --DFPWM Vers for calculating time
  80.   if tape_label == "Minecraft Radio" then
  81.     tape_tape = 1; --32768hz
  82.   else
  83.     tape_type = 2; --48000hz
  84.   end
  85.  
  86.   print("Playing Tape '"..tape_label.."' until silence is detected...")
  87.   print("Tape Length: "..tapePosToTime(tape_length,tape_type).." - Tape Size: "..string.format("%02.f",math.floor(tape_length / 1024)).."KB")
  88.   print(" ")
  89.   print("Press [SPACEBAR] to exit script...")
  90.  
  91.   if tape.getState() ~= "PLAYING" then
  92.     tape.play()
  93.   end
  94.  
  95.   while true do
  96.     local evt, addr, char  = event.pull(1, "key_up")
  97.     if char == 32 then    -- space
  98.       io.write("\nSpacebar pressed, exiting script...")
  99.       break
  100.     end
  101.  
  102.     if not tape.isReady() then
  103.       io.stderr:write("\nThe tape was removed! Waiting for tape...")
  104.       while not tape.isReady() do
  105.         os.sleep(1);
  106.       end
  107.       io.write("\nTape detected. Rewinding and restarting tape...\n")
  108.       tape.seek(-tape.getSize())
  109.       os.sleep(1)
  110.       handleTape(addr)
  111.       break
  112.     end
  113.  
  114.     local tapepos = tape.getPosition()
  115.     io.write("["..tape.getLabel().."] "..tape.getState()..": "..tapePosToTime(tapepos).." ("..string.format("%02.f",math.floor(tapepos / 1024)).."KB)     \r")
  116.  
  117.     --if we find three nil bytes in a row, assume silence
  118.     if tape.read(false) == 0 then
  119.       tape.seek(1)
  120.       if tape.read(true) == 0 then
  121.         if tape.read(true) == 0 then
  122.           io.write("\nRewinding and restarting tape...\n")
  123.           tape.stop();
  124.           tape.seek(-tape.getSize())
  125.           tape.play();
  126.         end
  127.       end
  128.     end
  129.   end
  130. end
  131.  
  132. if args[1] == nill or args[1] == "tape" then
  133.   handleTape(args[2])
  134. else
  135.   print("Usage: radio [tape] [tapedeck_addr]")
  136.   return
  137. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement