Guest User

Untitled

a guest
Oct 1st, 2017
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. minecraft@univac2:/srv/minecraft/forge$ cat world/computer/0/usr/playsound.lua
  2. args = {...}
  3. if #args < 2 then
  4. print("usage: playsound side soundfile")
  5. return
  6. end
  7.  
  8. local side = args[1]
  9. if peripheral.getType(side) ~= "speaker" then
  10. print("no speaker connected at side "..side)
  11. return
  12. end
  13.  
  14. local fileName = shell.resolve(args[2])
  15. if not fs.exists(fileName) then
  16. print("file "..fileName.." does not exist")
  17. return
  18. end
  19.  
  20. notes = "cCdDefFgGaAb"
  21. length = 4
  22. tempo = 125
  23. octave = 0
  24. instrument = "flute"
  25. function playString(str)
  26. if string.len(str) < 1 then return end
  27. for i=1,string.len(str) do
  28. chr = string.match(str,".",i)
  29. if chr == "<" then
  30. octave = octave - 1
  31. elseif chr == ">" then
  32. octave = octave + 1
  33. elseif chr == "l" then
  34. i = i + 1
  35. length = string.match(str,"(%d+)",i)
  36. elseif chr == "t" then
  37. i = i + 1
  38. tempo = string.match(str,"(%d+)",i)
  39. elseif string.find(notes,chr) then
  40. local wait = 60 / tempo / (length/4)
  41. local note = string.find(notes,chr) + 5 + octave * 12
  42. print(chr.." "..note)
  43. peripheral.call(side,"playNote",instrument,1,note)
  44. os.sleep(wait)
  45. end
  46. end
  47. end
  48.  
  49. local f = fs.open(fileName, "r")
  50. while true do
  51. local str = f.readLine()
  52. if not str then break end
  53. print(str)
  54. playString(str)
  55. end
  56. f.close()
Advertisement
Add Comment
Please, Sign In to add comment