Skip to content

Commit d4063be

Browse files
committed
Add Input/Output for webaudio streams
Fixes #40 * Also added toString(wa) and wa:toString() * Added debugger format for webaudio streams that * Fix internal errors in GetTimeElapsed and GetState if the stream is invalid
1 parent 54c1bf9 commit d4063be

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

lua/autorun/webaudio.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ end
180180
-- Not perfect for the clients and there will be desync if you pause and unpause constantly.
181181
-- @return number Elapsed time
182182
function WebAudio:GetTimeElapsed()
183+
if self:IsDestroyed() then return -1 end
183184
return self.stopwatch:GetTime()
184185
end
185186

@@ -216,6 +217,7 @@ end
216217
--- Returns the state of the WebAudio object
217218
-- @return number State, See STOPWATCH_* Enums
218219
function WebAudio:GetState()
220+
if self:IsDestroyed() then return STOPWATCH_STOPPED end
219221
return self.stopwatch:GetState()
220222
end
221223

lua/entities/gmod_wire_expression2/core/custom/webaudio.lua

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ E2Lib.registerConstant( "WA_FFT_DELAY", 80 ) -- Delay in ms
2626

2727

2828
registerType("webaudio", "xwa", WebAudio.getNULL(),
29-
nil, -- TODO: webaudios in io?
30-
nil,
29+
function(self, input)
30+
return input
31+
end,
32+
function(self, output)
33+
return output
34+
end,
3135
function(ret)
3236
-- For some reason we don't throw an error here.
3337
-- See https://github.com/wiremod/wire/blob/501dd9875ab1f6db37a795e1f9a946d382db4f1f/lua/entities/gmod_wire_expression2/core/entity.lua#L10
@@ -46,6 +50,24 @@ local function registerStream(self, url, owner)
4650
return stream
4751
end
4852

53+
e2function string toString(webaudio stream)
54+
if IsValid(stream) then
55+
return string.format("WebAudio [%d]", stream.id)
56+
else
57+
return "WebAudio [null]"
58+
end
59+
end
60+
61+
e2function string webaudio:toString() = e2function string toString(webaudio stream)
62+
63+
WireLib.registerDebuggerFormat("WEBAUDIO", function(stream)
64+
if IsValid(stream) then
65+
return string.format( "WebAudio [Id: %d, volume: %d%%, time: %.02f/%d]", stream.id, stream.volume * 100, stream.stopwatch:GetTime(), stream.length )
66+
else
67+
return "WebAudio [null]"
68+
end
69+
end)
70+
4971
__e2setcost(1)
5072
e2function webaudio operator=(webaudio lhs, webaudio rhs) -- Wa = webAudio("...") (Rip Coroutine Core comments)
5173
local scope = self.Scopes[ args[4] ]
@@ -294,9 +316,10 @@ end
294316
__e2setcost(15)
295317
e2function void webaudio:destroy()
296318
-- No limit here because they'd already have been limited by the creation burst.
319+
local owner = this.owner or self.player
320+
-- Prevent people destroying others streams and getting more slots? Would be weird.
297321
if this:Destroy() then
298-
local ply = self.player
299-
StreamCounter[ply] = StreamCounter[ply] - 1
322+
StreamCounter[owner] = StreamCounter[owner] - 1
300323
end
301324
end
302325

0 commit comments

Comments
 (0)