Skip to content

Commit 08ea5e5

Browse files
committed
Add soundcard wrapper library
1 parent 16204b1 commit 08ea5e5

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

programs.cfg

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,13 @@
215215
authors = "gamax92, GreaseMonkey (for Java implementation)",
216216
repo = "tree/master/dfpwm"
217217
},
218+
["soundcard"] = {
219+
files = {
220+
["master/soundcard.lua"] = "/lib",
221+
},
222+
name = "soundcard wrapper",
223+
description = "automatic processing of soundcard queues and delays",
224+
authors = "gamax92",
225+
repo = "blob/master/soundcard.lua",
226+
}
218227
}

soundcard.lua

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
local component=require("component")
2+
3+
local soundcard={}
4+
5+
function soundcard.wrap(proxy, maxms)
6+
if maxms == nil then maxms = math.huge end
7+
if type(proxy) == "string" then
8+
proxy=component.proxy(component.get(proxy))
9+
end
10+
11+
local sound={
12+
process=proxy.process,
13+
clear=proxy.clear,
14+
setTotalVolume=proxy.setTotalVolume
15+
}
16+
for k, v in pairs(proxy) do
17+
if sound[k] then
18+
-- don't replace it
19+
elseif type(v) == "function" or (type(v) == "table" and getmetatable(v) ~= nil) then
20+
sound[k]=function(...)
21+
local ok=v(...)
22+
if not ok then
23+
os.sleep(0)
24+
while not proxy.process() do os.sleep(0) end
25+
assert(v(...))
26+
end
27+
return true
28+
end
29+
else
30+
sound[k]=proxy[k]
31+
end
32+
end
33+
34+
local chcount=proxy.channel_count
35+
36+
local time=0
37+
function sound.delay(ms)
38+
if ms < 1 then return end
39+
if time+ms >= maxms then
40+
proxy.delay(maxms-time)
41+
while not proxy.process() do os.sleep(0) end
42+
time=time+ms-maxms
43+
while time >= maxms do
44+
time=time-maxms
45+
proxy.delay(maxms)
46+
while not proxy.process() do os.sleep(0) end
47+
end
48+
if time > 0 then
49+
proxy.delay(time)
50+
end
51+
time=0
52+
else
53+
proxy.delay(ms)
54+
time=time+ms
55+
end
56+
end
57+
58+
-- Reset Sound Card to known state.
59+
sound.clear()
60+
sound.setTotalVolume(1)
61+
for i = 1, chcount do
62+
sound.resetEnvelope(i)
63+
sound.resetAM(i)
64+
sound.resetFM(i)
65+
sound.setVolume(i, 1)
66+
sound.close(i)
67+
sound.setWave(i, sound.modes.square)
68+
end
69+
70+
while not proxy.process() do os.sleep(0) end
71+
72+
return sound
73+
end
74+
75+
return soundcard

0 commit comments

Comments
 (0)