From b9ba2534a4183ae47efb29ec0a6b29e3b7756457 Mon Sep 17 00:00:00 2001 From: Matthew Wilbern <39929480+fatboychummy@users.noreply.github.com> Date: Fri, 29 Mar 2024 04:24:11 -0600 Subject: [PATCH] `speaker sound` command (#1747) --- .../lua/rom/programs/fun/speaker.lua | 38 +++++++++++++++++++ .../data/computercraft/lua/rom/startup.lua | 2 +- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/projects/core/src/main/resources/data/computercraft/lua/rom/programs/fun/speaker.lua b/projects/core/src/main/resources/data/computercraft/lua/rom/programs/fun/speaker.lua index 928c403dd..cee07b714 100644 --- a/projects/core/src/main/resources/data/computercraft/lua/rom/programs/fun/speaker.lua +++ b/projects/core/src/main/resources/data/computercraft/lua/rom/programs/fun/speaker.lua @@ -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 [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 [speaker]") + print(programName .. " sound [volume] [pitch] [speaker]") print(programName .. " stop [speaker]") end diff --git a/projects/core/src/main/resources/data/computercraft/lua/rom/startup.lua b/projects/core/src/main/resources/data/computercraft/lua/rom/startup.lua index 2c1f147a3..f3b9f7a08 100644 --- a/projects/core/src/main/resources/data/computercraft/lua/rom/startup.lua +++ b/projects/core/src/main/resources/data/computercraft/lua/rom/startup.lua @@ -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)