zefie icon

Minecraft Radio Thing (OpenComputers + Computronics Tapes)

zefie | PRO | 02/17/21 05:29:56 PM UTC (Edited) | 0 ⭐ | 1786 👁️ | Never ⏰ | []
Lua |

3.61 KB

|

None

|

0 👍

/

0 👎

local component = require("component")
local shell = require("shell")
local event = require("event")
local args, options = shell.parse(...)
 
-- 'Smart' Computronics Tape Player Script
-- Automatically rewinds and loops tape
 
 
if not component.isAvailable("tape_drive") then
  io.stderr:wrte("This program requires a tape drive to run.")
  return
end
 
local function tapePosToTime(pos,vers)
  local multi
  if vers == 1 then
     multi = 4096 --32768hz
  else
     multi = 6000 --48000hz
  end
 
  local seconds = tonumber(pos) / multi
  
  if seconds <= 0 then
    return "00:00:00";
  else
    hours = string.format("%02.f", math.floor(seconds/3600));
    mins = string.format("%02.f", math.floor(seconds/60 - (hours*60)));
    secs = string.format("%02.f", math.floor(seconds - hours*3600 - mins*60));
    return hours..":"..mins..":"..secs
  end
end
 
local function getTapeDrive()
  --Credits to gamax92 for this
  --Taken from Computronics tape.lua
  local tape
  if options.address then
    if type(options.address) ~= "string" then
      io.stderr:write("'address' may only be a string.")
      return
    end
    local fulladdr = component.get(options.address)
    if fulladdr == nil then
      io.stderr:write("No component at this address.")
      return
    end
    if component.type(fulladdr) ~= "tape_drive" then
      io.stderr:write("No tape drive at this address")
      return
    end
    tape = component.proxy(fulladdr)
  else
    tape = component.tape_drive
  end
  return tape
end
 
local function handleTape(addr) 
  os.execute("cls")
  local tape
  if addr ~= nil then
    tape = getTapeDrive(addr)
  else
    tape = getTapeDrive()
  end
 
  if not tape.isReady() then
    io.stderr:write("The tape drive does not contain a tape.")
    return
  end
 
  local tape_label = tape.getLabel()
  local tape_length = tape.getSize()
  
  local tape_type
 
  --DFPWM Vers for calculating time
  if tape_label == "Minecraft Radio" then
    tape_tape = 1; --32768hz
  else
    tape_type = 2; --48000hz
  end
 
  print("Playing Tape '"..tape_label.."' until silence is detected...")
  print("Tape Length: "..tapePosToTime(tape_length,tape_type).." - Tape Size: "..string.format("%02.f",math.floor(tape_length / 1024)).."KB")
  print(" ")
  print("Press [SPACEBAR] to exit script...")
  
  if tape.getState() ~= "PLAYING" then
    tape.play()
  end
 
  while true do
    local evt, addr, char  = event.pull(1, "key_up")
    if char == 32 then    -- space
      io.write("\nSpacebar pressed, exiting script...")
      break
    end
 
    if not tape.isReady() then
      io.stderr:write("\nThe tape was removed! Waiting for tape...")
      while not tape.isReady() do
        os.sleep(1);
      end
      io.write("\nTape detected. Rewinding and restarting tape...\n")
      tape.seek(-tape.getSize())
      os.sleep(1)
      handleTape(addr)
      break
    end
 
    local tapepos = tape.getPosition()
    io.write("["..tape.getLabel().."] "..tape.getState()..": "..tapePosToTime(tapepos).." ("..string.format("%02.f",math.floor(tapepos / 1024)).."KB)     \r")
 
    --if we find three nil bytes in a row, assume silence
    if tape.read(false) == 0 then
      tape.seek(1)
      if tape.read(true) == 0 then
        if tape.read(true) == 0 then
          io.write("\nRewinding and restarting tape...\n")
          tape.stop();
          tape.seek(-tape.getSize())
          tape.play();
        end
      end
    end
  end
end
 
if args[1] == nill or args[1] == "tape" then
  handleTape(args[2])
else
  print("Usage: radio [tape] [tapedeck_addr]")
  return
end

Comments