kd2bwzgen

[dofile] MineRobberSong format

Jul 26th, 2017
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.27 KB | None | 0 0
  1. local sp = peripheral.find("speaker")
  2. if not sp then error("Speaker not found",0) end
  3. local mrs = {}
  4. function mrs.playNote(sInstr,nPitch)
  5.     sp.playNote(sInstr,100,nPitch)
  6. end
  7. function mrs.playChord(sInstr,tPitches)
  8.     for i=1,#tPitches do
  9.         mrs.playNote(sInstr,tPitches[i])
  10.     end
  11. end
  12. function mrs.playSong(tSong)
  13.     local tNotes = tSong.tNotes
  14.     for i=1,#tNotes do
  15.         local tNote = tNotes[i]
  16.         if tNote.sType == "chord" then mrs.playChord(tNote.sInstr,tNote.vArg) end
  17.         if tNote.sType == "note" then mrs.playNote(tNote.sInstr,tNote.vArg) end
  18.         sleep(tSong.nTempo)
  19.     end
  20. end
  21.  
  22. local function getLines(sInput)
  23.     if not sInput:find("\n") then return {sInput} end
  24.     local tLines = {}
  25.     for i in string.gmatch((sInput.."\n"),"(.-)\n") do
  26.         table.insert(tLines,i)
  27.     end
  28.     return tLines
  29. end
  30.  
  31. local function getParts(sLine)
  32.     local tRet = {}
  33.     for i in string.gmatch(sLine,"%S+") do
  34.         table.insert(tRet,i)
  35.     end
  36.     return tRet
  37. end
  38.  
  39. function mrs.parseSong(sSong)
  40.     local tLines = getLines(sSong)
  41.     if #tLines==1 then
  42.         local tSong = {}
  43.         tSong.sName = "Malformed File"
  44.         tSong.sMusician = "MRSAPI"
  45.         local tNote = {}
  46.         tNote.sType = "note"
  47.         tNote.sInstr = "harp"
  48.         tNote.vArg = 6
  49.         tSong.tNotes = {tNote}
  50.         return tSong
  51.     end
  52.     local tSong = {}
  53.     tSong.sName = table.remove(tLines,1)
  54.     tSong.sMusician = table.remove(tLines,1)
  55.     tSong.nTempo = tonumber(table.remove(tLines,1))
  56.     tSong.tNotes = {}
  57.     for i=1,#tLines do
  58.         local tNote = {}
  59.         local tParts = getParts(tLines[i])
  60.         tNote.sType = table.remove(tParts,1)
  61.         tNote.sInstr = table.remove(tParts,1)
  62.         if tNote.sType=="note" then
  63.             tNote.vArg = tonumber(tParts[1])
  64.         else
  65.             tNote.vArg = {}
  66.             for i=1,#tParts do
  67.                 table.insert(tNote.vArg,tonumber(tParts[i]))
  68.             end
  69.         end
  70.         table.insert(tSong.tNotes,tNote)
  71.     end
  72.     return tSong
  73. end
  74. function mrs.loadSong(sFilename)
  75.     if not fs.exists(sFilename) then error("File not found",0) end
  76.     local fh = fs.open(sFilename,"r")
  77.     local tSong = mrs.parseSong(fh.readAll())
  78.     fh.close()
  79.     return tSong
  80. end
  81. return mrs
Advertisement
Add Comment
Please, Sign In to add comment