Advertisement
MysticT

NoteBlock API

Jul 3rd, 2012
2,283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.67 KB | None | 0 0
  1. -- NoteBlock Controller API
  2. -- by MysticT
  3.  
  4. -- Config
  5.  
  6. -- id of the players
  7. local tIds = {}
  8. -- number of notes per player
  9. local nNotes = 0
  10.  
  11. -- Instruments
  12. piano = 1
  13. bass = 2
  14. bass_drum = 3
  15. snare_drum = 4
  16. click = 5
  17.  
  18. local function send(id, t)
  19.     rednet.send(id, "<PLAY> "..textutils.serialize(t))
  20. end
  21.  
  22. --[[
  23. i: instrument (1-5)
  24. n: note (0-24)
  25. --]]
  26. function playNote(i, n)
  27.     local t = {}
  28.     t[i] = { n }
  29.     local id = tIds[math.floor(n / nNotes) + 1]
  30.     send(id, t)
  31. end
  32.  
  33. --[[
  34. i: instrument (1-5)
  35. notes: { n1, n2, n3, ..., nN}
  36.  
  37. n: note (0-24)
  38. --]]
  39. function playChord(i, notes)
  40.     local ts = {}
  41.     for _,n in ipairs(notes) do
  42.         local id = tIds[math.floor(n / nNotes) + 1]
  43.         if not ts[id] then
  44.             ts[id] = {}
  45.         end
  46.         if not ts[id][i] then
  47.             ts[id][i] = {}
  48.         end
  49.         table.insert(ts[id][i], n)
  50.     end
  51.     for id, v in pairs(ts) do
  52.         send(id, v)
  53.     end
  54. end
  55.  
  56. --[[
  57. Table format:
  58. t = {
  59. [i1] = { n1, n2, n3, ..., nN },
  60. [i2] = { n1, n2, n3, ..., nN },
  61. ...
  62. [iN] = { n1, n2, n3, ..., nN }
  63. }
  64.  
  65. i: instrument (1-5)
  66. n: note (0-24)
  67. --]]
  68. function play(t)
  69.     local ts = {}
  70.     for i, notes in pairs(t) do
  71.         for _,n in ipairs(notes) do
  72.             local id = tIds[math.floor(n / nNotes) + 1]
  73.             if not ts[id] then
  74.                 ts[id] = {}
  75.             end
  76.             if not ts[id][i] then
  77.                 ts[id][i] = {}
  78.             end
  79.             table.insert(ts[id][i], n)
  80.         end
  81.     end
  82.     for id, v in pairs(ts) do
  83.         send(id, v)
  84.     end
  85. end
  86.  
  87. --[[
  88. Table format:
  89. t = {
  90. [1] = n,
  91. [2] = n,
  92. [3] = n,
  93. ...
  94. [N] = n,
  95. ["delay"] = d,
  96. }
  97.  
  98. d: delay (in seconds), default 0.5
  99. n: instrument-notes table (play() format, see above)
  100. --]]
  101. function playSong(t)
  102.     local nDelay = t.delay or 0.5
  103.     for _,n in ipairs(t) do
  104.         play(n)
  105.         sleep(nDelay)
  106.     end
  107. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement