Advertisement
MrHG

playsong

Nov 1st, 2022 (edited)
646
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.07 KB | None | 0 0
  1. local args = { ... }
  2. local speaker = peripheral.find("speaker")
  3. local instrument = "pling"
  4. local volume = 1
  5. local silent = true
  6.  
  7. local function setColor(color)
  8.     if term.isColor() then
  9.         term.setTextColor(color)
  10.     end
  11. end
  12.  
  13. local function errorMsg(message)
  14.     setColor(colors.red)
  15.     print(message)
  16.     setColor(colors.white)
  17. end
  18.  
  19. if #args == 0 then
  20.     errorMsg("Usage: playsong (songfile.sng)")
  21.     return
  22. end
  23. if args[2] == "v" then
  24.     silent = false
  25. end
  26. local fileName = args[1]
  27.  
  28. songFile = fs.open(fileName,"r")
  29. songList = textutils.unserialize(songFile.readAll())
  30. songFile.close()
  31.  
  32. for i=1,#songList,1 do
  33.     local songdata = songList[i]
  34.     if type(songdata[1]) == "string" then
  35.         if not silent then
  36.             print("setting instrument to "..songdata[1])
  37.         end
  38.         instrument = songdata[1]
  39.         volume = songdata[2]
  40.     else
  41.         if not silent then
  42.             print("playing note at pitch "..songdata[1])
  43.         end
  44.         speaker.playNote(instrument,volume,songdata[1])
  45.         sleep(songdata[2])
  46.     end
  47. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement