Advertisement
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 = 8
- -- sides corresponding to each instrument
- -- order: low-high
- local tSides = {"front","left","back","right","top"}
- -- pulse sleep time
- local nPulse = 0.01
- -- 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)
- --print(textutils.serialize(t))
- if t[1] then
- rs.setOutput("front",true)
- --print("f")
- end
- if t[2] then
- rs.setOutput("left",true)
- --print("l")
- end
- if t[3] then
- rs.setOutput("back",true)
- --print("b")
- end
- if t[4] then
- rs.setOutput("right",true)
- --print("r")
- end
- if t[5] then
- rs.setOutput("top",true)
- --print("t")
- end
- sleep(nPulse)
- rs.setOutput("front",false)
- rs.setOutput("left",false)
- rs.setOutput("back",false)
- rs.setOutput("right",false)
- rs.setOutput("top",false)
- 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
- --]]
- local function play(t)
- print("Playing...")
- local c={}
- --local c[i]
- for i,notes in pairs(t) do
- --if type(notes) == "table" then
- --table.insert(c,i{})
- local r = notes%5
- if r==0 then
- r=r+5
- end
- c[r]=true
- end
- --print("c",textutils.serialize(c))
- --else
- -- print("Wrong format. table expected, got ", type(notes))
- --end
- pulse(c)
- 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 msg then
- --print ("recieved")
- --print(id)
- end
- if id == nControllerID then
- --print("matched")
- local t = string.match(msg, "<PLAY> (.+)")
- if t then
- local notes = textutils.unserialize(t)
- if notes then
- play(notes)
- --print(";;",unpack(notes),";;")
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement