Advertisement
Guest User

tape

a guest
Jan 3rd, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.14 KB | None | 0 0
  1. --[[ tape program, provides basic tape modification and access tools
  2. Authors: gamax92, Vexatos
  3. ]]
  4. local args = { ... }
  5. local tape = peripheral.find("tape_drive")
  6. if not tape then
  7.   print("This program requires a tape drive to run.")
  8.   return
  9. end
  10.  
  11. local function printUsage()
  12.   print("Usage:")
  13.   print(" - 'tape play' to start playing a tape")
  14.   print(" - 'tape pause' to pause playing the tape")
  15.   print(" - 'tape stop' to stop playing and rewind the tape")
  16.   print(" - 'tape rewind' to rewind the tape")
  17.   print(" - 'tape label [name]' to label the tape, leave 'name' empty to get current label")
  18.   print(" - 'tape speed <speed>' to set the playback speed. Needs to be between 0.25 and 2.0")
  19.   print(" - 'tape volume <volume>' to set the volume of the tape. Needs to be between 0.0 and 1.0")
  20.   print(" - 'tape write <path/of/audio/file>' to write to the tape from a file")
  21.   return
  22. end
  23.  
  24. if not tape.isReady() then
  25.   printError("The tape drive does not contain a tape.")
  26.   return
  27. end
  28.  
  29. local function label(name)
  30.   if not name then
  31.     if tape.getLabel() == "" then
  32.       print("Tape is currently not labeled.")
  33.       return
  34.     end
  35.     print("Tape is currently labeled: " .. tape.getLabel())
  36.     return
  37.   end
  38.   tape.setLabel(name)
  39.   print("Tape label set to " .. name)
  40. end
  41.  
  42. local function rewind()
  43.   print("Rewound tape")
  44.   tape.seek(-tape.getSize())
  45. end
  46.  
  47. local function play()
  48.   if tape.getState() == "PLAYING" then
  49.     print("Tape is already playing")
  50.   else
  51.     tape.play()
  52.     print("Tape started")
  53.   end
  54. end
  55.  
  56. local function stop()
  57.   if tape.getState() == "STOPPED" then
  58.     print("Tape is already stopped")
  59.   else
  60.     tape.stop()
  61.     tape.seek(-tape.getSize())
  62.     print("Tape stopped")
  63.   end
  64. end
  65.  
  66. local function pause()
  67.   if tape.getState() == "STOPPED" then
  68.     print("Tape is already paused")
  69.   else
  70.     tape.stop()
  71.     print("Tape paused")
  72.   end
  73. end
  74.  
  75. local function speed(sp)
  76.   local s = tonumber(sp)
  77.   if not s or s < 0.25 or s > 2 then
  78.     printError("Speed needs to be a number between 0.25 and 2.0")
  79.     return
  80.   end
  81.   tape.setSpeed(s)
  82.   print("Playback speed set to " .. sp)
  83. end
  84.  
  85. local function volume(vol)
  86.   local v = tonumber(vol)
  87.   if not v or v < 0 or v > 1 then
  88.     printError("Volume needs to be a number between 0.0 and 1.0")
  89.     return
  90.   end
  91.   tape.setVolume(v)
  92.   print("Volume set to " .. vol)
  93. end
  94.  
  95. local function writeTape(relPath)
  96.   tape.stop()
  97.   tape.seek(-tape.getSize())
  98.   tape.stop() --Just making sure
  99.  
  100.   local file, msg, _, y, success
  101.   local block = 8192 --How much to read at a time
  102.   local bytery = 0 --For the progress indicator
  103.  
  104.   local path = shell.resolve(relPath)
  105.   local filesize = fs.getSize(path)
  106.   print("Path: " .. path)
  107.   file, msg = fs.open(path, "rb")
  108.   if not fs.exists(path) then msg = "file not found" end
  109.   if not file then
  110.     printError("Failed to open file " .. relPath .. (msg and ": " .. tostring(msg)) or "")
  111.     return
  112.   end
  113.  
  114.   print("Writing...")
  115.  
  116.   _, y = term.getCursorPos()
  117.  
  118.   if filesize > tape.getSize() then
  119.     term.setCursorPos(1, y)
  120.     printError("Error: File is too large for tape, shortening file")
  121.     y = y + 1
  122.     filesize = tape.getSize()
  123.   end
  124.  
  125.   repeat
  126.     local bytes = {}
  127.     for i = 1, block do
  128.       local byte = file.read()
  129.       if not byte then break end
  130.       bytes[#bytes + 1] = byte
  131.     end
  132.     if #bytes > 0 then
  133.       term.setCursorPos(1, y)
  134.       bytery = bytery + #bytes
  135.       term.write("Read " .. tostring(bytery) .. " of " .. tostring(filesize) .. " bytes...")
  136.       for i = 1, #bytes do
  137.         tape.write(bytes[i])
  138.       end
  139.       sleep(0)
  140.     end
  141.   until not bytes or #bytes <= 0
  142.   file.close()
  143.   tape.stop()
  144.   tape.seek(-tape.getSize())
  145.   tape.stop() --Just making sure
  146.   print("\nDone.")
  147. end
  148.  
  149. if args[1] == "play" then
  150.   play()
  151. elseif args[1] == "stop" then
  152.   stop()
  153. elseif args[1] == "pause" then
  154.   pause()
  155. elseif args[1] == "rewind" then
  156.   rewind()
  157. elseif args[1] == "label" then
  158.   label(args[2])
  159. elseif args[1] == "speed" then
  160.   speed(args[2])
  161. elseif args[1] == "volume" then
  162.   volume(args[2])
  163. elseif args[1] == "write" then
  164.   writeTape(args[2])
  165. else
  166.   printUsage()
  167. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement