`speaker sound` command (#1747)

This commit is contained in:
Matthew Wilbern 2024-03-29 04:24:11 -06:00 committed by GitHub
parent c764981a40
commit b9ba2534a4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 1 deletions

View File

@ -132,9 +132,47 @@ elseif cmd == "play" then
end
handle.close()
elseif cmd == "sound" then
local _, sound, volume, pitch, name = ...
if not sound then
error("Usage: speaker sound <sound> [volume] [pitch] [speaker]", 0)
return
end
if volume then
volume = tonumber(volume)
if not volume then
error("Volume must be a number", 0)
end
if volume < 0 or volume > 3 then
error("Volume must be between 0 and 3", 0)
end
end
if pitch then
pitch = tonumber(pitch)
if not pitch then
error("Pitch must be a number", 0)
end
if pitch < 0 or pitch > 2 then
error("Pitch must be between 0 and 2", 0)
end
end
local speaker = get_speakers(name)[1]
if speaker.playSound(sound, volume, pitch) then
print(("Played sound %q on speaker %q with volume %s and pitch %s."):format(
sound, peripheral.getName(speaker), volume or 1, pitch or 1
))
else
error(("Could not play sound %q"):format(sound), 0)
end
else
local programName = arg[0] or fs.getName(shell.getRunningProgram())
print("Usage:")
print(programName .. " play <file or url> [speaker]")
print(programName .. " sound <sound> [volume] [pitch] [speaker]")
print(programName .. " stop [speaker]")
end

View File

@ -117,7 +117,7 @@ shell.setCompletionFunction("rom/programs/fun/dj.lua", completion.build(
completion.peripheral
))
shell.setCompletionFunction("rom/programs/fun/speaker.lua", completion.build(
{ completion.choice, { "play ", "stop " } },
{ completion.choice, { "play ", "sound ", "stop " } },
function(shell, text, previous)
if previous[2] == "play" then return completion.file(shell, text, previous, true)
elseif previous[2] == "stop" then return completion.peripheral(shell, text, previous, false)