-- SPDX-FileCopyrightText: 2021 The CC: Tweaked Developers -- -- SPDX-License-Identifier: MPL-2.0 local function get_speakers(name) if name then local speaker = peripheral.wrap(name) if speaker == nil then error(("Speaker %q does not exist"):format(name), 0) return elseif not peripheral.hasType(name, "speaker") then error(("%q is not a speaker"):format(name), 0) end return { speaker } else local speakers = { peripheral.find("speaker") } if #speakers == 0 then error("No speakers attached", 0) end return speakers end end local function pcm_decoder(chunk) local buffer = {} for i = 1, #chunk do buffer[i] = chunk:byte(i) - 128 end return buffer end local function report_invalid_format(format) printError(("speaker cannot play %s files."):format(format)) local pp = require "cc.pretty" pp.print("Run '" .. pp.text("help speaker", colours.lightGrey) .. "' for information on supported formats.") end local cmd = ... if cmd == "stop" then local _, name = ... for _, speaker in pairs(get_speakers(name)) do speaker.stop() end elseif cmd == "play" then local _, file, name = ... local speaker = get_speakers(name)[1] local handle, err if http and file:match("^https?://") then print("Downloading...") handle, err = http.get(file) else handle, err = fs.open(file, "r") end if not handle then printError("Could not play audio:") error(err, 0) end local start = handle.read(4) local pcm = false local size = 16 * 1024 - 4 if start == "RIFF" then handle.read(4) if handle.read(8) ~= "WAVEfmt " then handle.close() error("Could not play audio: Unsupported WAV file", 0) end local fmtsize = (" [speaker]") print(programName .. " stop [speaker]") end