Guest User

Chime

a guest
Feb 22nd, 2014
527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.29 KB | None | 0 0
  1. --Configuration
  2. local speakerSide = "top"
  3. local playSpeed = 0.2
  4. local playVolume = 20
  5.  
  6. --Argument Definition
  7. local filePath, playNotes = ...
  8.  
  9. --Note Dictionary
  10. local note = {
  11.     ["a"] = 220,
  12.     ["b"] = 248.942,
  13.     ["c"] = 261.626,
  14.     ["d"] = 293.665,
  15.     ["e"] = 329.628,
  16.     ["f"] = 349.228,
  17.     ["g"] = 391.995
  18. }
  19.  
  20. --Usage Label
  21. local function usage()
  22.     print("This program is designed to play music in notation 'aabbcc'.")
  23.     print("Usage: chime [File Path] [true to show notes, or false to not]")
  24. end
  25.  
  26. --File Loader and Parser
  27. local function fileParse()
  28.     return fs.open(filePath,"r").readLine()
  29. end
  30.  
  31. --Player
  32. local function play()
  33.     peripheral.wrap(speakerSide).setAttenuation(playVolume)
  34.     if playNotes == "false" then
  35.             if c ~= " " then
  36.                 peripheral.wrap(speakerSide).start(1,note[c])
  37.                 sleep(playSpeed)
  38.                 peripheral.wrap(speakerSide).stop(1)
  39.             else
  40.                 sleep(playSpeed)
  41.             end
  42.     elseif playNotes == "true" then
  43.         for c in fileParse():gmatch"." do
  44.             if c ~= " " then
  45.                 peripheral.wrap(speakerSide).start(1,note[c])
  46.                 sleep(playSpeed)
  47.                 peripheral.wrap(speakerSide).stop(1)
  48.                 print(c)
  49.             else
  50.                 sleep(playSpeed)
  51.             end
  52.         end
  53.     else
  54.         usage()
  55.     end
  56. end
  57.  
  58. --Main
  59. local function main()
  60.     term.clear()
  61.     term.setCursorPos(1,1)
  62.     play()
  63.     print("Song Finished")
  64. end
  65.  
  66. main()
Advertisement
Add Comment
Please, Sign In to add comment