Added pcm-reader and more sounds.
This commit is contained in:
		
							parent
							
								
									a0e8726ce6
								
							
						
					
					
						commit
						0f204f5fbf
					
				| 
						 | 
					@ -3,8 +3,12 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
A script for an all-in-one elevator with floor selection, sounds, doors, and
 | 
					A script for an all-in-one elevator with floor selection, sounds, doors, and
 | 
				
			||||||
more.
 | 
					more.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Requires `pcm-reader.lua` installed as to be required with `require("pcm")`
 | 
				
			||||||
]]
 | 
					]]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					local pcm = require("pcm")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
-- Load floors from elevator settings.
 | 
					-- Load floors from elevator settings.
 | 
				
			||||||
local FLOORS = {}
 | 
					local FLOORS = {}
 | 
				
			||||||
local floorsFile = io.open("floors.tbl", "r") or error("Missing floors.tbl")
 | 
					local floorsFile = io.open("floors.tbl", "r") or error("Missing floors.tbl")
 | 
				
			||||||
| 
						 | 
					@ -200,6 +204,8 @@ local function goToFloor(floorLabel)
 | 
				
			||||||
    local motionKeyframes = computeLinearMotion(distance)
 | 
					    local motionKeyframes = computeLinearMotion(distance)
 | 
				
			||||||
    playChime(currentFloor)
 | 
					    playChime(currentFloor)
 | 
				
			||||||
    closeDoor(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
 | 
					    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.
 | 
					        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
 | 
					        if frame.rpm == CONTROL_MAX_RPM then
 | 
				
			||||||
| 
						 | 
					@ -366,8 +372,6 @@ end
 | 
				
			||||||
    Main Script Area.
 | 
					    Main Script Area.
 | 
				
			||||||
]]
 | 
					]]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
initControls()
 | 
					initControls()
 | 
				
			||||||
initUserInterface()
 | 
					initUserInterface()
 | 
				
			||||||
while true do
 | 
					while true do
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
										
											Binary file not shown.
										
									
								
							| 
						 | 
					@ -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
 | 
				
			||||||
| 
						 | 
					@ -7,6 +7,7 @@ module pcm_to_lua;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import std.stdio;
 | 
					import std.stdio;
 | 
				
			||||||
import std.file;
 | 
					import std.file;
 | 
				
			||||||
 | 
					import std.array;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const frameSize = 128 * 1024;
 | 
					const frameSize = 128 * 1024;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -23,6 +24,7 @@ int main(string[] args) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    byte[] contents = cast(byte[]) std.file.read(audioFilename);
 | 
					    byte[] contents = cast(byte[]) std.file.read(audioFilename);
 | 
				
			||||||
    ulong sampleIndex = 0;
 | 
					    ulong sampleIndex = 0;
 | 
				
			||||||
 | 
					    auto app = appender!string;
 | 
				
			||||||
    stdout.writeln("local audio = {");
 | 
					    stdout.writeln("local audio = {");
 | 
				
			||||||
    foreach (byte sample; contents) {
 | 
					    foreach (byte sample; contents) {
 | 
				
			||||||
        stdout.writefln!"    %d,"(sample);
 | 
					        stdout.writefln!"    %d,"(sample);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue