Advertisement
FluttyProger

nbpPlayerVanila

Sep 3rd, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local com = require("component")
  2. local thread = require("threads")
  3. local fs = require("filesystem")
  4. local NBlocks = com.list("note_block")
  5. local NBlockList = {}
  6. _G.CalibrationNBlocks = {}
  7.  
  8. print("Введите путь к файлу (*.nbp)")
  9. local nepath=io.read()
  10.  
  11. if fs.exists(nepath) then
  12.   local path=nepath
  13. else
  14.   print("Такого файла нет")
  15.   os.exit()
  16. end
  17.  
  18. if _G.da==nil then
  19.  
  20. for k,v in pairs(NoteBlock) do
  21.   table.insert(NoteBlockList,k)
  22. end
  23.  
  24. -- 1 - piano
  25. -- 2 - Double Bass
  26. -- 3 - Bass Drum
  27. -- 4 - Snare Drum
  28. -- 5 - Click
  29.  
  30. print("в какой последовательность проигрываются нотные блоки?")
  31.  
  32.  
  33. local function posl()
  34.     for i=1,5 do
  35.         com.invoke(NoteBlockList[i],"trigger")
  36.         os.sleep(1)
  37.     end  
  38. end
  39.  
  40.  
  41. for o=1,5 do
  42.     posl()
  43.     io.write("на каком месте "..o.." блок: ")
  44.     table.insert(_G.CalibrationNBlocks,NoteBlockList[tonumber(io.read())])
  45. end
  46. _G.da=true
  47. end
  48.  
  49. thread.init()
  50.  
  51. local tbl = {
  52.     [0] = 1,
  53.     [1] = 3,
  54.     [2] = 4,
  55.     [3] = 5,
  56.     [4] = 2,
  57. }
  58.  
  59. local function countnotes(test)
  60.     for i = 1, math.huge do
  61.         local subs = string.sub(test, 1, 3*i)
  62.         local lens = string.len(subs)
  63.         local subs2 = string.sub(test, 1, 3*i+1)
  64.         local lens2 = string.len(subs2)
  65.         if lens==lens2 then
  66.             return i
  67.         end
  68.     end
  69. end
  70.  
  71. local function thirding(linen,mi)
  72.     return string.sub(linen, 3*mi - 2, 3*mi)
  73. end
  74.  
  75. local function max()
  76.     local fil = io.open(path)
  77.     local maxn=0
  78.     for line in fil:lines() do
  79.         local v=countnotes(line)
  80.         if v>maxn then
  81.             maxn=v
  82.         end
  83.     end
  84.     fil:close()
  85.     return maxn
  86. end
  87.  
  88. local function parse(i)
  89.     local file = io.open(path)
  90.     for line in file:lines() do
  91.         local line = thirding(line,i)
  92.         if line ~= "" and line ~= " " and line ~= nil then
  93.             local instrum = tonumber(string.sub(line,1,1))
  94.             local Notes = tonumber(string.sub(line,2,3))
  95.             com.invoke(_G.CalibrationNBlocks[tbl[instrum]],"setPitch",Notes)
  96.             com.invoke(_G.CalibrationNBlocks[tbl[instrum]],"trigger")
  97.         end
  98.         os.sleep(0.05)
  99.     end
  100.     file:close()
  101. end
  102.  
  103. local function check()
  104.     local file = io.open(path)
  105.     local line3 = file:read("*l")
  106.     local maxn=max()
  107.     if line3 ~= nil then
  108.         for hg = 1, maxn do
  109.             thread.create(parse, hg)
  110.         end
  111.     end
  112.     file:close()
  113. end
  114.  
  115. thread.create(check)
  116. thread.waitForAll()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement