diff --git a/elevator/elevator-controller.lua b/elevator/elevator-controller.lua index 337b6ef..a895568 100644 --- a/elevator/elevator-controller.lua +++ b/elevator/elevator-controller.lua @@ -3,8 +3,12 @@ A script for an all-in-one elevator with floor selection, sounds, doors, and more. + +Requires `pcm-reader.lua` installed as to be required with `require("pcm")` ]] +local pcm = require("pcm") + -- Load floors from elevator settings. local FLOORS = {} local floorsFile = io.open("floors.tbl", "r") or error("Missing floors.tbl") @@ -200,6 +204,8 @@ local function goToFloor(floorLabel) local motionKeyframes = computeLinearMotion(distance) playChime(currentFloor) closeDoor(currentFloor) + local audioFile = "audio/going-up.pcm" + if rpmDir == -1 then audioFile = "audio/going-down.pcm" end for _, frame in pairs(motionKeyframes) do local sleepTime = math.floor((frame.duration - 0.05) * 20) / 20 -- Make sure we round down to safely arrive before the detector. if frame.rpm == CONTROL_MAX_RPM then @@ -366,8 +372,6 @@ end Main Script Area. ]] - - initControls() initUserInterface() while true do diff --git a/elevator/going-down.pcm b/elevator/going-down.pcm new file mode 100644 index 0000000..4c26589 Binary files /dev/null and b/elevator/going-down.pcm differ diff --git a/pcm-reader.lua b/pcm-reader.lua new file mode 100644 index 0000000..8ab4217 --- /dev/null +++ b/pcm-reader.lua @@ -0,0 +1,43 @@ +local pcm = {} + +-- Reads a file containing signed 8-bit PCM audio data, and converts it into a list of buffers to play. +function pcm.readFile(filename) + if not fs.exists(filename) then error("Audio file " .. filename .. " doesn't exist.") end + if fs.isDir(filename) then error("Cannot read audio from directory: " .. filename) end + local frames = {} + local currentFrame = {} + local currentFrameSampleCount = 0 + local file = fs.open(filename, "rb") + while true do + local n = file.read() + if n == nil then break end + if n > 127 then n = n - 256 end + table.insert(currentFrame, n) + currentFrameSampleCount = currentFrameSampleCount + 1 + if currentFrameSampleCount == (128 * 1024) then + table.insert(frames, currentFrame) + currentFrame = {} + currentFrameSampleCount = 0 + end + end + file.close() + if currentFrameSampleCount > 0 then + table.insert(frames, currentFrame) + end + return frames +end + +function pcm.playFrames(speaker, frames) + for i = 1, #frames do + while not speaker.playAudio(frames[i]) do + os.pullEvent("speaker_audio_empty") + end + end +end + +function pcm.playFile(speaker, filename) + local frames = pcm.readFile(filename) + pcm.playFrames(frames) +end + +return pcm \ No newline at end of file diff --git a/pcmToLua.d b/pcmToLua.d index 8f66a65..04313fc 100755 --- a/pcmToLua.d +++ b/pcmToLua.d @@ -7,6 +7,7 @@ module pcm_to_lua; import std.stdio; import std.file; +import std.array; const frameSize = 128 * 1024; @@ -23,6 +24,7 @@ int main(string[] args) { byte[] contents = cast(byte[]) std.file.read(audioFilename); ulong sampleIndex = 0; + auto app = appender!string; stdout.writeln("local audio = {"); foreach (byte sample; contents) { stdout.writefln!" %d,"(sample);