Added pcm-reader and more sounds.

This commit is contained in:
Andrew Lalis 2023-01-02 21:52:21 +01:00
parent a0e8726ce6
commit 0f204f5fbf
4 changed files with 51 additions and 2 deletions

View File

@ -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

BIN
elevator/going-down.pcm Normal file

Binary file not shown.

43
pcm-reader.lua Normal file
View File

@ -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

View File

@ -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);