Yobi

Fixedplayer

Apr 21st, 2013
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 playNote2(i, n)
  27.     m = peripheral.wrap("left")
  28.     local t = {}
  29.     t[i] = { n }
  30.     local id = tIds[math.floor(n / nNotes) + 1]
  31.     m.playNote(i-1,n)
  32. end
  33.  
  34. function playNote3(i, n)
  35.     u = peripheral.wrap("right")
  36.     local t = {}
  37.     t[i] = { n }
  38.     local id = tIds[math.floor(n / nNotes) + 1]
  39.     u.playNote(i-1,n)
  40. end
  41.  
  42. --[[
  43. i: instrument (1-5)
  44. notes: { n1, n2, n3, ..., nN}
  45.  
  46. n: note (0-24)
  47. --]]
  48. function playChord(i, notes)
  49.     local ts = {}
  50.     m = peripheral.wrap("left")
  51.     o = peripheral.wrap("right")
  52.     for g, n in ipairs(notes) do
  53.         if g/2 == 0 then
  54.         m.playnote(i-1, n)
  55.         else
  56.         o.playnote(i-1,n)
  57.         end
  58.     end
  59. end
  60.  
  61. --[[
  62. Table format:
  63. t = {
  64. [i1] = { n1, n2, n3, ..., nN },
  65. [i2] = { n1, n2, n3, ..., nN },
  66. ...
  67. [iN] = { n1, n2, n3, ..., nN }
  68. }
  69.  
  70. i: instrument (1-5)
  71. n: note (0-24)
  72. --]]
  73. function play(t)
  74.     local ts = {}
  75.     for i, notes in pairs(t) do
  76.         for i,n in ipairs(notes) do
  77.             if i/2 == 0 then
  78.             playNote2(i, n)
  79.             else
  80.             playNote3(i, n)
  81.             end
  82.         end
  83.     end
  84. end
  85. --[[
  86. Table format:
  87. t = {
  88. [1] = n,
  89. [2] = n,
  90. [3] = n,
  91. ...
  92. [N] = n,
  93. ["delay"] = d,
  94. }
  95.  
  96. d: delay (in seconds), default 0.5
  97. n: instrument-notes table (play() format, see above)
  98. --]]
  99. function playSong(t)
  100.     local nDelay = t.delay or 0.5
  101.     for _,n in ipairs(t) do
  102.         play(n)
  103.         sleep(nDelay)
  104.     end
  105. end
Advertisement
Add Comment
Please, Sign In to add comment