Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. local args = {...}
  2.  
  3. local sound = args[1]
  4.  
  5. if sound:sub(1,1) == "{" and sound:sub(-1, -1) == "}" then
  6. sound = sound:gsub(" ", "")
  7. local t = {}
  8. for token in string.gmatch(sound:sub(2, -2), "[^,]+") do
  9. table.insert(t, token)
  10. -- print(token)
  11. end
  12. sound = t
  13. else
  14. sound = { sound }
  15. end
  16.  
  17. function printUsage()
  18. print("Usage: play <sound> [pitch or random] [repeats [delay]]\n")
  19. end
  20.  
  21. if not sound then
  22. return printUsage()
  23. end
  24.  
  25. local pitch = args[2] or "1"
  26. local repeats = tonumber(args[3] or "1") or 1
  27. local delay = tonumber(args[4] or "0.5") or .5
  28.  
  29. local sides = {
  30. "right"
  31. , "left"
  32. , "top"
  33. , "bottom"
  34. , "front"
  35. , "back"
  36. }
  37.  
  38. local playSound
  39.  
  40. for k, v in pairs(sides) do
  41. playSound = playSound or
  42. (peripheral.wrap(v) or {}).playSound
  43. end
  44.  
  45. local randomPitch = false
  46. if tonumber(pitch) then
  47. pitch = tonumber(pitch)
  48. elseif pitch == "random" then
  49. randomPitch = true
  50. else
  51. return printUsage()
  52. end
  53.  
  54. for i = 1, repeats do
  55. local toPlay = sound[math.random(1, #sound)]
  56. if randomPitch then
  57. pitch = math.random(0, 20000) / 10000
  58. end
  59. if playSound then
  60. playSound(toPlay, 1, pitch)
  61. print(string.format("Playing %s with pitch %.2f", toPlay, pitch))
  62. end
  63. if delay > 0 and repeats > 1 then
  64. sleep(delay)
  65. end
  66. end
  67. print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement