MysticT

NoteBlock Player

Jul 3rd, 2012
1,215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.24 KB | None | 0 0
  1. -- NoteBlock Player
  2. -- by MysticT
  3.  
  4. -- Config
  5.  
  6. -- id of the controller computer
  7. local nControllerID = 0
  8. -- sides corresponding to each instrument
  9. -- order: piano, bass, bass drum, snare drum, click
  10. local tSides = {}
  11. -- colors corresponding to each note
  12. local tColors = {}
  13. -- pulse sleep time
  14. local nPulse = 0.1
  15.  
  16. -- Functions
  17.  
  18. -- clear the screen
  19. local function clear()
  20.     term.clear()
  21.     term.setCursorPos(1, 1)
  22. end
  23.  
  24. -- detect modem and connect
  25. local function connect()
  26.     for _,s in ipairs(rs.getSides()) do
  27.         if peripheral.isPresent(s) and peripheral.getType(s) == "modem" then
  28.             rednet.open(s)
  29.             return true
  30.         end
  31.     end
  32.     return false
  33. end
  34.  
  35. -- send a pulse
  36. --[[
  37. table format:
  38. t = {
  39. [side1] = colors,
  40. [side2] = colors,
  41. ...
  42. [sideN] = colors,
  43. }
  44. --]]
  45. local function pulse(t)
  46.     for side, c in pairs(t) do
  47.         rs.setBundledOutput(side, c)
  48.     end
  49.     sleep(nPulse)
  50.     for side,_ in pairs(t) do
  51.         rs.setBundledOutput(side, 0)
  52.     end
  53.     sleep(nPulse)
  54. end
  55.  
  56. -- play the given notes on the given instruments
  57. --[[
  58. table format:
  59. t = {
  60. [i1] = { n1, n2, ..., nN },
  61. [i2] = { n1, n2, ..., nN },
  62. ...
  63. [iN] = { n1, n2, ..., nN }
  64. }
  65.  
  66. i: instrument number (range defined by the sides table)
  67. n: notes (range defined by the colors table)
  68. --]]
  69. local function play(t)
  70.     print("Playing...")
  71.     local tPulse = {}
  72.     for i, notes in pairs(t) do
  73.         print("Instrument: ", i)
  74.         local side = tSides[i]
  75.         if side then
  76.             if type(notes) == "table" then
  77.                 print("Notes: ", unpack(notes))
  78.                 local c = 0
  79.                 for _,n in ipairs(notes) do
  80.                     local k = tColors[n]
  81.                     if k then
  82.                         c = colors.combine(c, k)
  83.                     else
  84.                         print("Unknown note: ", n)
  85.                     end
  86.                 end
  87.                 tPulse[side] = c
  88.             else
  89.                 print("Wrong format. table expected, got ", type(notes))
  90.             end
  91.         else
  92.             print("Unknown Instrument")
  93.         end
  94.     end
  95.     pulse(tPulse)
  96. end
  97.  
  98. -- Start Program
  99.  
  100. -- try to connect to rednet
  101. if not connect() then
  102.     print("No modem found")
  103.     return
  104. end
  105.  
  106. clear()
  107. print("Waiting for messages...")
  108.  
  109. -- start receiving
  110. while true do
  111.     local id, msg = rednet.receive()
  112.     if id == nControllerID then
  113.         local t = string.match(msg, "<PLAY> (.+)")
  114.         if t then
  115.             local notes = textutils.unserialize(t)
  116.             if notes then
  117.                 play(notes)
  118.             end
  119.         end
  120.     end
  121. end
Advertisement
Add Comment
Please, Sign In to add comment