Advertisement
incinirate

Note Player

Jul 13th, 2014
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.38 KB | None | 0 0
  1. local tArgs = {...}
  2.  
  3. local nb = "right"
  4. local nb2 = "left"
  5.  
  6. block = peripheral.wrap(nb)
  7. fallback = peripheral.wrap(nb2)
  8.  
  9. if #tArgs<1 then
  10.   print("Usage: "..shell.getRunningProgram().." <file>")
  11.   error()
  12. end
  13.  
  14. function iter( tTable, nIndex )
  15.   local tVals = {}
  16.   for k,v in pairs(tTable) do
  17.     if type(k)=="number" and k>nIndex then
  18.       table.insert(tVals,k)
  19.     end
  20.   end
  21.   if #tVals>0 then
  22.     return math.min(unpack(tVals)), tTable[math.min(unpack(tVals))]
  23.   end
  24. end
  25.  
  26. function ipairs2( tTable )
  27.   return iter, tTable, 0
  28. end
  29.  
  30. local itn = {
  31.   ["p"]=0,
  32.   ["d"]=1,
  33.   ["b"]=2,
  34.   ["s"]=3,
  35.   ["c"]=4,
  36.   ["x"]=5
  37. }
  38.  
  39. function format(sStr)
  40.   local tbl={}
  41.   for n in string.gmatch(sStr,"[^:]+") do
  42.    -- write(n:sub(1,1))
  43.     table.insert(tbl,{itn[n:sub(1,1)],n:sub(2,#n)})
  44.   end
  45.   return tbl
  46. end
  47.  
  48. file = fs.open(tArgs[1],"r")
  49. --piano = file.readLine()
  50. --doublebass = file.readLine()
  51. --bassdrum = file.readLine()
  52. --snaredrum = file.readLine()
  53. --click = file.readLine()
  54. if not file then error("Invalid File") end
  55.  
  56. lines = {}
  57. done = {}
  58. --lnn = {}
  59. tempo=1/tonumber(file.readLine())
  60. repeat
  61.   ln=file.readLine()
  62.   if ln then
  63.     table.insert(lines,format(ln))
  64.     table.insert(done,false)
  65.   end
  66. until not ln
  67.  
  68. function dne()
  69.   for i=1,#done do
  70.     if done[i]==false then return false end
  71.   end
  72.   return true
  73. end
  74.  
  75. function len(t)
  76.   local tbl = {}
  77.   for i=1,#t do
  78.     table.insert(tbl,#t[i])
  79.   end
  80.   return tbl
  81. end
  82.  
  83. if lines[1] then
  84.   print("Playing: "..tArgs[1])
  85.   print("Length: "..math.max(unpack(len(lines)))*tempo.." seconds.")
  86.   _,py = term.getCursorPos()
  87.   index=1
  88.   while not dne() do
  89.     term.setCursorPos(1,py)
  90.     term.clearLine()
  91.     write(math.floor((index-1)*tempo).."/"..math.floor(math.max(unpack(len(lines)))*tempo))
  92.     local npt = 0
  93.     for i=1,#lines do
  94.       if #lines[i]>=index then
  95.         if lines[i][index][1]~=5 then
  96.           if npt < 5 then
  97.             block.playNote(tonumber(lines[i][index][1]),tonumber(lines[i][index][2]))
  98.             npt = npt+1
  99.           else
  100.             fallback.playNote(tonumber(lines[i][index][1]),tonumber(lines[i][index][2]))
  101.             npt = npt+1
  102.           end
  103.         end
  104. --        print(lines[i][index][1])
  105. --        print(lines[i][index][2])
  106.       else
  107.         done[i]=true
  108.       end
  109.     end
  110.     index=index+1
  111.     sleep(tempo)
  112.   end
  113. else
  114.   error("File is blank... -.-")
  115. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement