File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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}
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments