Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- activeColors = {["bottom"] = 0, ["left"] = 0, ["right"] = 0, ["top"] = 0, ["back"] = 0}
- function parseNote(iNote, iInstrument) --> iColor, sSide
- bExtra = false
- sSide = ""
- if iNote >= 16 then bExtra = true end
- if not bExtra then
- if iInstrument == 1 then sSide = "bottom" end
- if iInstrument == 2 then sSide = "back" end
- if iInstrument >= 3 then sSide = "right" end
- iColor = 2 ^ iNote
- else
- if iInstrument == 1 then sSide = "top" end
- if iInstrument == 2 then sSide = "left" end
- if iInstrument >= 3 then sSide = "right" end
- iColor = 2 ^ (iNote - 16)
- end
- return iColor, sSide
- end
- function setNoteState(iNote, iInstrument, bState)
- iC, sS = parseNote(iNote, iInstrument)
- if not bState then iC = -iC end
- activeColors[sS] = activeColors[sS] + iC
- rs.setBundledOutput(sS, activeColors[sS])
- end
- function playNotes(iLen, tNotes)
- for k, v in pairs(tNotes) do
- if v[1] ~= nil and v[2] ~= nil then
- print("Playing " .. v[1] .. " " .. v[2])
- setNoteState(v[1], v[2], true)
- end
- end
- sleep(tonumber(iLen)/2)
- for k, v in pairs(tNotes) do
- if v[1] ~= nil and v[2] ~= nil then
- setNoteState(v[1], v[2], false)
- end
- end
- sleep(tonumber(iLen)/2)
- end
- function interpretLine(sLine) -->iLen, tNotes
- local sLineN, iFind, iSec1, iSec2, iTaken, tRet
- tRet = {}
- iLen1 = sLine:find(";")
- if iLen1 == nil then error("no ; found. Maybe : used instead in file?") end
- iLen = sLine:sub(1,iLen1 - 1)
- sLineN = sLine:sub(iLen1 + 1)
- iTaken = iLen1
- --print("sLine: " .. sLine)
- --print("iTaken: " .. iTaken)
- while sLineN ~= "" do
- iFind = sLineN:find(":")
- iFindk = sLineN:find(",")
- if iFind == nil or iFindk == nil then break end
- iFind = iFind - iTaken
- iFindk = iFindk - iTaken
- iSec1 = sLineN:sub(1, iFindk - 1)
- iSec2 = sLineN:sub(iFindk + 1, iFind - 1)
- --print("sLineN: " .. sLineN)
- --print("sFind: " .. iFind)
- --print("sFindk: " .. iFindk)
- --print("iSec1: " .. iSec1)
- --print("iSec2: " .. iSec2)
- tTemp = {tonumber(iSec1), tonumber(iSec2)}
- table.insert(tRet, tTemp)
- sLineN = sLineN:sub(iFind + 1)
- iTaken = iTaken + iFind
- end
- return iLen, tRet
- end
- function interpretFile(sFile)
- file = fs.open(sFile, "r")
- if file == nil then return error("invalid file") end
- repeat
- sLine = file.readLine()
- if sLine ~= nil then
- iLen, tNotes = interpretLine(sLine)
- print(textutils.serialize(tNotes))
- playNotes(iLen, tNotes)
- end
- until sLine == nil
- end
- interpretFile("testsong")
- length, notes = interpretLine("1.2;3,4:5,6:7,8:9,0")
- print(length)
- print(textutils.serialize(notes))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement