Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local sp = peripheral.find("speaker")
- if not sp then error("Speaker not found",0) end
- local mrs = {}
- function mrs.playNote(sInstr,nPitch)
- sp.playNote(sInstr,100,nPitch)
- end
- function mrs.playChord(sInstr,tPitches)
- for i=1,#tPitches do
- mrs.playNote(sInstr,tPitches[i])
- end
- end
- function mrs.playSong(tSong)
- local tNotes = tSong.tNotes
- for i=1,#tNotes do
- local tNote = tNotes[i]
- if tNote.sType == "chord" then mrs.playChord(tNote.sInstr,tNote.vArg) end
- if tNote.sType == "note" then mrs.playNote(tNote.sInstr,tNote.vArg) end
- sleep(tSong.nTempo)
- end
- end
- local function getLines(sInput)
- if not sInput:find("\n") then return {sInput} end
- local tLines = {}
- for i in string.gmatch((sInput.."\n"),"(.-)\n") do
- table.insert(tLines,i)
- end
- return tLines
- end
- local function getParts(sLine)
- local tRet = {}
- for i in string.gmatch(sLine,"%S+") do
- table.insert(tRet,i)
- end
- return tRet
- end
- function mrs.parseSong(sSong)
- local tLines = getLines(sSong)
- if #tLines==1 then
- local tSong = {}
- tSong.sName = "Malformed File"
- tSong.sMusician = "MRSAPI"
- local tNote = {}
- tNote.sType = "note"
- tNote.sInstr = "harp"
- tNote.vArg = 6
- tSong.tNotes = {tNote}
- return tSong
- end
- local tSong = {}
- tSong.sName = table.remove(tLines,1)
- tSong.sMusician = table.remove(tLines,1)
- tSong.nTempo = tonumber(table.remove(tLines,1))
- tSong.tNotes = {}
- for i=1,#tLines do
- local tNote = {}
- local tParts = getParts(tLines[i])
- tNote.sType = table.remove(tParts,1)
- tNote.sInstr = table.remove(tParts,1)
- if tNote.sType=="note" then
- tNote.vArg = tonumber(tParts[1])
- else
- tNote.vArg = {}
- for i=1,#tParts do
- table.insert(tNote.vArg,tonumber(tParts[i]))
- end
- end
- table.insert(tSong.tNotes,tNote)
- end
- return tSong
- end
- function mrs.loadSong(sFilename)
- if not fs.exists(sFilename) then error("File not found",0) end
- local fh = fs.open(sFilename,"r")
- local tSong = mrs.parseSong(fh.readAll())
- fh.close()
- return tSong
- end
- return mrs
Add Comment
Please, Sign In to add comment