Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- NoteBlock Player
- -- by MysticT
- -- Config
- -- id of the controller computer
- local nControllerID = 0
- -- sides corresponding to each instrument
- -- order: piano, bass, bass drum, snare drum, click
- local tSides = {}
- -- colors corresponding to each note
- local tColors = {}
- -- pulse sleep time
- local nPulse = 0.1
- -- Functions
- -- clear the screen
- local function clear()
- term.clear()
- term.setCursorPos(1, 1)
- end
- -- detect modem and connect
- local function connect()
- for _,s in ipairs(rs.getSides()) do
- if peripheral.isPresent(s) and peripheral.getType(s) == "modem" then
- rednet.open(s)
- return true
- end
- end
- return false
- end
- -- send a pulse
- --[[
- table format:
- t = {
- [side1] = colors,
- [side2] = colors,
- ...
- [sideN] = colors,
- }
- --]]
- local function pulse(t)
- for side, c in pairs(t) do
- rs.setBundledOutput(side, c)
- end
- sleep(nPulse)
- for side,_ in pairs(t) do
- rs.setBundledOutput(side, 0)
- end
- sleep(nPulse)
- end
- -- play the given notes on the given instruments
- --[[
- table format:
- t = {
- [i1] = { n1, n2, ..., nN },
- [i2] = { n1, n2, ..., nN },
- ...
- [iN] = { n1, n2, ..., nN }
- }
- i: instrument number (range defined by the sides table)
- n: notes (range defined by the colors table)
- --]]
- local function play(t)
- print("Playing...")
- local tPulse = {}
- for i, notes in pairs(t) do
- print("Instrument: ", i)
- local side = tSides[i]
- if side then
- if type(notes) == "table" then
- print("Notes: ", unpack(notes))
- local c = 0
- for _,n in ipairs(notes) do
- local k = tColors[n]
- if k then
- c = colors.combine(c, k)
- else
- print("Unknown note: ", n)
- end
- end
- tPulse[side] = c
- else
- print("Wrong format. table expected, got ", type(notes))
- end
- else
- print("Unknown Instrument")
- end
- end
- pulse(tPulse)
- end
- -- Start Program
- -- try to connect to rednet
- if not connect() then
- print("No modem found")
- return
- end
- clear()
- print("Waiting for messages...")
- -- start receiving
- while true do
- local id, msg = rednet.receive()
- if id == nControllerID then
- local t = string.match(msg, "<PLAY> (.+)")
- if t then
- local notes = textutils.unserialize(t)
- if notes then
- play(notes)
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment