Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = {...}
- local sound = args[1]
- if sound:sub(1,1) == "{" and sound:sub(-1, -1) == "}" then
- sound = sound:gsub(" ", "")
- local t = {}
- for token in string.gmatch(sound:sub(2, -2), "[^,]+") do
- table.insert(t, token)
- -- print(token)
- end
- sound = t
- else
- sound = { sound }
- end
- function printUsage()
- print("Usage: play <sound> [pitch or random] [repeats [delay]]\n")
- end
- if not sound then
- return printUsage()
- end
- local pitch = args[2] or "1"
- local repeats = tonumber(args[3] or "1") or 1
- local delay = tonumber(args[4] or "0.5") or .5
- local sides = {
- "right"
- , "left"
- , "top"
- , "bottom"
- , "front"
- , "back"
- }
- local playSound
- for k, v in pairs(sides) do
- playSound = playSound or
- (peripheral.wrap(v) or {}).playSound
- end
- local randomPitch = false
- if tonumber(pitch) then
- pitch = tonumber(pitch)
- elseif pitch == "random" then
- randomPitch = true
- else
- return printUsage()
- end
- for i = 1, repeats do
- local toPlay = sound[math.random(1, #sound)]
- if randomPitch then
- pitch = math.random(0, 20000) / 10000
- end
- if playSound then
- playSound(toPlay, 1, pitch)
- print(string.format("Playing %s with pitch %.2f", toPlay, pitch))
- end
- if delay > 0 and repeats > 1 then
- sleep(delay)
- end
- end
- print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement