Skip to content

Commit 5838556

Browse files
committed
Fix stopwatch not being properly started when the stream is started & setTime not working for certain streams
* Fix webaudio not properly calling into the stopwatch when started / stopped: Have no idea what I was thinking when I didn't do this, maybe just forgot or something. Was half a year ago so.. * Fixed setTime not working for some streams: Turns out dont_decode actually breaks a lot of stuff :l, either way there's a networking cooldown so it shouldn't be an issue. There also seems to be problems with SetTime, but chances are it's a garrysmod issue. Hate how horrible bass streams are in gmod..
1 parent 6b7d1f0 commit 5838556

File tree

4 files changed

+78
-46
lines changed

4 files changed

+78
-46
lines changed

lua/autorun/stopwatch.lua

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,25 @@
1313

1414
STOPWATCH_STOPPED, STOPWATCH_PLAYING, STOPWATCH_PAUSED = 0, 1, 2
1515

16-
local StopWatch = {}
17-
StopWatch.__index = StopWatch
16+
---@class Stopwatch
17+
---@field playback_rate number
18+
---@field playback_now number
19+
---@field playback_elapsed number
20+
---@field playback_duration number
21+
---@field state number # STOPWATCH_STOPPED, STOPWATCH_PLAYING, STOPWATCH_PAUSED
22+
---@field delay number
23+
---@field looping boolean
24+
local Stopwatch = {}
25+
Stopwatch.__index = Stopwatch
1826

1927
local timer_now = RealTime
2028

2129
--- Creates a StopWatch.
22-
-- @param number duration How long the stopwatch will last
23-
-- @param function callback What to run when the stopwatch finishes.
24-
local function Initialize(_, duration, fn)
25-
local self = setmetatable({}, StopWatch)
30+
---@param duration number # How long the stopwatch will last
31+
---@param callback fun(self: Stopwatch) # What to run when the stopwatch finishes.
32+
---@return Stopwatch
33+
local function Initialize(_, duration, callback)
34+
local self = setmetatable({}, Stopwatch)
2635
self.playback_rate = 1
2736
self.playback_now = timer_now()
2837
self.playback_elapsed = 0
@@ -45,7 +54,7 @@ local function Initialize(_, duration, fn)
4554
self.playback_elapsed = self.playback_duration
4655
self.state = STOPWATCH_STOPPED
4756
end
48-
fn(self)
57+
callback(self)
4958
end)
5059
timer.Stop(mangled)
5160

@@ -54,20 +63,20 @@ local function Initialize(_, duration, fn)
5463
return self
5564
end
5665

57-
setmetatable(StopWatch, {
66+
setmetatable(Stopwatch, {
5867
__call = Initialize
5968
})
6069

6170
--- Pauses a stopwatch at the current time to be resumed with :Play
62-
function StopWatch:Pause()
71+
function Stopwatch:Pause()
6372
if self.state == STOPWATCH_PLAYING then
6473
self.state = STOPWATCH_PAUSED
6574
timer.Pause(self.timerid)
6675
end
6776
end
6877

6978
--- Resumes the stopwatch after it was paused. You can't :Play a :Stop(ped) timer, use :Start for that.
70-
function StopWatch:Play()
79+
function Stopwatch:Play()
7180
if self.state == STOPWATCH_PAUSED then
7281
self.playback_now = timer_now()
7382
self.state = STOPWATCH_PLAYING
@@ -76,7 +85,7 @@ function StopWatch:Play()
7685
end
7786

7887
--- Used internally by GetTime, don't use.
79-
function StopWatch:UpdateTime()
88+
function Stopwatch:UpdateTime()
8089
if self.state == STOPWATCH_PLAYING then
8190
local now = timer_now()
8291
local elapsed = (now - self.playback_now) * self.playback_rate
@@ -88,7 +97,7 @@ end
8897

8998
--- Stops the timer with the stored elapsed time.
9099
-- Continue from here with :Start()
91-
function StopWatch:Stop()
100+
function Stopwatch:Stop()
92101
if self.state ~= STOPWATCH_STOPPED then
93102
self:UpdateTime()
94103
self.state = STOPWATCH_STOPPED
@@ -97,7 +106,7 @@ function StopWatch:Stop()
97106
end
98107

99108
--- (Re)starts the stopwatch.
100-
function StopWatch:Start()
109+
function Stopwatch:Start()
101110
if self.state == STOPWATCH_STOPPED then
102111
self.playback_now = timer_now()
103112
self.state = STOPWATCH_PLAYING
@@ -107,15 +116,15 @@ function StopWatch:Start()
107116
end
108117

109118
--- Returns the playback duration of the stopwatch.
110-
-- @return number Length
111-
function StopWatch:GetDuration()
119+
---@return number length
120+
function Stopwatch:GetDuration()
112121
return self.playback_duration
113122
end
114123

115124
--- Returns the playback duration of the stopwatch.
116-
-- @param number duration
117-
-- @return StopWatch self
118-
function StopWatch:SetDuration(duration)
125+
---@param duration number
126+
---@return Stopwatch self
127+
function Stopwatch:SetDuration(duration)
119128
self.playback_duration = duration
120129
self.delay = duration
121130
timer.Adjust( self.timerid, duration )
@@ -126,27 +135,27 @@ function StopWatch:SetDuration(duration)
126135
end
127136

128137
--- Sets the playback rate / speed of the stopwatch. 2 is twice as fast, etc.
129-
-- @param number n Speed
130-
-- @return StopWatch self
131-
function StopWatch:SetRate(n)
138+
---@param speed number
139+
---@return Stopwatch self
140+
function Stopwatch:SetRate(speed)
132141
self:UpdateTime()
133-
self.playback_rate = n
142+
self.playback_rate = speed
134143
-- New Duration - Elapsed
135-
self.delay = (self.playback_duration / n) - self.playback_elapsed
144+
self.delay = (self.playback_duration / speed) - self.playback_elapsed
136145
timer.Adjust( self.timerid, self.delay )
137146
return self
138147
end
139148

140149
--- Returns the playback rate of the stopwatch. Default 1
141-
-- @return number Playback rate
142-
function StopWatch:GetRate()
150+
---@return number rate # Playback rate
151+
function Stopwatch:GetRate()
143152
return self.playback_rate
144153
end
145154

146155
--- Sets the playback time of the stopwatch.
147-
-- @param number n Time
148-
-- @return StopWatch self
149-
function StopWatch:SetTime(n)
156+
---@param n number # Time
157+
---@return Stopwatch self
158+
function Stopwatch:SetTime(n)
150159
self.playback_now = timer_now()
151160
self.playback_elapsed = n
152161
self.delay = (self.playback_duration - n) / self.playback_rate
@@ -156,22 +165,22 @@ function StopWatch:SetTime(n)
156165
end
157166

158167
--- Returns the current playback time in seconds of the stopwatch
159-
-- @return number Playback time
160-
function StopWatch:GetTime()
168+
---@return number time # Playback time
169+
function Stopwatch:GetTime()
161170
self:UpdateTime()
162171
return self.playback_elapsed
163172
end
164173

165174
--- Returns the current playback state of the stopwatch. 0 for STOPWATCH_STOPPED, 1 for STOPWATCH_PLAYING, 2 for STOPWATCH_PAUSED
166-
-- @return number Playback state
167-
function StopWatch:GetState()
175+
---@return number state Playback state
176+
function Stopwatch:GetState()
168177
return self.state
169178
end
170179

171180
--- Sets the stopwatch to loop. Won't call the callback if it is looping.
172-
-- @param boolean loop Whether it's looping
173-
-- @return StopWatch self
174-
function StopWatch:SetLooping(loop)
181+
---@param loop boolean Whether it's looping
182+
---@return Stopwatch self
183+
function Stopwatch:SetLooping(loop)
175184
if self.looping ~= loop then
176185
self.looping = loop
177186
timer.Adjust( self.timerid, self.delay, 0, nil )
@@ -180,11 +189,11 @@ function StopWatch:SetLooping(loop)
180189
end
181190

182191
--- Returns if the stopwatch is looping
183-
-- @return boolean Looping
184-
function StopWatch:GetLooping()
192+
---@return boolean looping
193+
function Stopwatch:GetLooping()
185194
return self.looping
186195
end
187196

188-
_G.StopWatch = StopWatch
197+
_G.StopWatch = Stopwatch
189198

190-
return StopWatch
199+
return Stopwatch

lua/autorun/webaudio.lua

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ end
7979
]]
8080

8181
--- Initiate WebAudio struct for both realms
82+
---@class WebAudio
83+
---@field stopwatch Stopwatch
84+
---@field radius number
85+
---@field looping boolean
86+
---@field parented boolean
87+
---@field volume number # 0-1
8288
_G.WebAudio = {}
8389
WebAudio.__index = WebAudio
8490

@@ -175,7 +181,7 @@ function WebAudio:Destroy(transmit)
175181
return true
176182
end
177183

178-
--- Returns time elapsed in URL stream.
184+
--- Returns current time in URL stream.
179185
-- Time elapsed is calculated on the server using playback rate and playback time.
180186
-- Not perfect for the clients and there will be desync if you pause and unpause constantly.
181187
-- @return number Elapsed time
@@ -378,6 +384,7 @@ local function createWebAudio(_, url, owner, bassobj, id)
378384
self.bass = bassobj
379385
self.parent_pos = Vector() -- Parent pos being nil means we will go directly to the parent's position w/o calculating local pos.
380386
else
387+
-- Stream will be set to 100 second length until the length of the audio stream is determined by the client.
381388
self.stopwatch = StopWatch(100, function(watch)
382389
if not watch:GetLooping() then
383390
self:Pause()

lua/webaudio/interface.lua

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ function WebAudio:Play()
8888

8989
if self.playing == false then
9090
self:AddModify(Modify.playing)
91+
92+
if self.stopwatch.state == STOPWATCH_STOPPED then
93+
self.stopwatch:Start()
94+
else
95+
self.stopwatch:Play()
96+
end
97+
9198
self.playing = true
9299
self:Transmit()
93100
return true
@@ -101,6 +108,7 @@ function WebAudio:Pause()
101108

102109
if self.playing then
103110
self:AddModify(Modify.playing)
111+
self.stopwatch:Pause()
104112
self.playing = false
105113
self:Transmit()
106114

@@ -293,17 +301,21 @@ net.Receive("wa_info", function(len, ply)
293301
-- Failed to create. Doesn't support 3d, or is block streamed.
294302
stream:Destroy()
295303
else
296-
local length = net.ReadUInt(16)
304+
local continuous = net.ReadBool()
305+
local length = -2
306+
if not continuous then
307+
length = net.ReadUInt(16)
308+
end
309+
297310
local file_name = net.ReadString()
298311
stream.length = length
299312
stream.filename = file_name
300313
stream.needs_info = false
301314

302315
local watch = stream.stopwatch
303316
watch:SetDuration(length)
304-
watch:SetRate(stream.playback_rate)
305-
watch:SetTime(stream.time)
306-
watch:Start()
317+
-- watch:SetRate(stream.playback_rate)
318+
-- watch:SetTime(stream.time)
307319
end
308320
end
309321
end)

lua/webaudio/receiver.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,11 @@ net.Receive("wa_create", function(len)
162162
net.Start("wa_info", true)
163163
WebAudio.writeID(id)
164164
net.WriteBool(false)
165-
net.WriteUInt(self.length, 16)
165+
local continuous = self.length < 0
166+
net.WriteBool(continuous) -- If the stream is continuous, it should return something less than 0.
167+
if not continuous then
168+
net.WriteUInt(self.length, 16)
169+
end
166170
net.WriteString(self.filename)
167171
net.SendToServer()
168172
end
@@ -206,7 +210,7 @@ function updateObject(id, modify_enum, handle_bass, inside_net)
206210
if hasModifyFlag(modify_enum, Modify.time) then
207211
if inside_net then self.time = net.ReadUInt(16) end
208212
if handle_bass then
209-
bass:SetTime(self.time, true) -- 18 hours max, if you need more, wtf..
213+
bass:SetTime(self.time) -- 18 hours max, if you need more, wtf..
210214
end
211215
end
212216

0 commit comments

Comments
 (0)