-
Notifications
You must be signed in to change notification settings - Fork 0
/
easy_bass.txt
309 lines (238 loc) · 9.27 KB
/
easy_bass.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
--@name Easy Bass
--@author legokidlogan
--@client
easyBass = easyBass or {}
if easyBass._alreadyLoaded then return end
easyBass._alreadyLoaded = true
local allBassObjs = {}
local soundIDIncrement = 0
local realtime = timer.realtime
local handleFollowTarget
local handleDestroyWhenDone
local handleFadeInOut
local loadBass
--[[
- Creates a bassObj from a clientside game sound file.
- callback behaves identically to the callback in bass.loadFile.
- failCallback receives errNum, errName, nonBassErr as arguments if the sound fails to load.
- Return true to automatically retry the sound.
- If failCallback is not provided, the sound will always be retried.
- settings: A table of settings for the sound. All settings are optional, and will default to the following if not specified:
{
Flags = "3d stereo noblock noplay", -- Flags to pass to bass.loadFile.
Volume = 1, -- Volume to play the sound at.
Pitch = 1, -- Pitch to play the sound at.
FadeStart = 700, -- Distance at which the sound starts to fade.
FadeEnd = 2000, -- Distance at which the sound is completely silent.
UseSimpleFading = true, -- Whether or not to use simple distance fading. See Bass:setFade() in the starfall docs for more info.
FadeIn = 0, -- Time in seconds to fade in the sound.
FadeOut = 0, -- Time in seconds from the end to fade out the sound. Disabled if Duration is not given.
Duration = nil, -- Duration of the sound in seconds. If nil, the duration will be automatically calculated.
Target = chip(), -- The entity or vector to play the sound at.
Loop = false, -- Whether or not to loop the sound.
DestroyWhenDone = true, -- Whether or not to destroy the sound when it finishes playing. Disabled if Duration is provided.
StaleThreshold = 30, -- How many ticks of silence must occur in a row for a sound to be considered done, for use with DestroyWhenDone.
}
--]]
function easyBass.loadFile( path, settings, callback, failCallback )
if not string.startWith( path, "sound/" ) then
path = "sound/" .. path
end
loadBass( bass.loadFile, path, settings, callback, failCallback )
end
-- Behaves identically to easyBass.loadFile, but loads a sound from a URL instead.
function easyBass.loadURL( link, settings, callback, failCallback )
loadBass( bass.loadURL, link, settings, callback, failCallback )
end
----- PRIVATE FUNCTIONS -----
handleFollowTarget = function( bassObj )
if bassObj._dontFollowTarget then return end
local target = bassObj._target
if not isValid( target ) then
bassObj:stop()
return true
end
if target:isPlayer() then
bassObj:setPos( target:obbCenterW() )
else
bassObj:setPos( target:getPos() )
end
end
handleDestroyWhenDone = function( bassObj )
if bassObj._dontDestroyWhenDone then return end
if bassObj:isPaused() or bassObj:isStalled() then return end
local volL, volR = bassObj:getLevels() -- :getLevels() is unaffected by :setVolume(), so additional checks aren't needed.
if volL ~= 0 or volR ~= 0 then
bassObj._staleStacks = 0
return
end
local staleStacks = ( bassObj._staleStacks or 0 ) + 1
bassObj._staleStacks = staleStacks
if staleStacks > bassObj._staleThreshold then
bassObj:stop()
return true
end
end
handleFadeInOut = function( bassObj, now )
local volume = bassObj._volume
local fadeInDuration = bassObj._fadeInDuration
local fadeOutDuration = bassObj._fadeOutDuration
if fadeOutDuration then
local endTime = bassObj._endTime
local timeUntilEnd = endTime - now
if timeUntilEnd < fadeOutDuration then
bassObj:setVolumeEffective( volume * ( timeUntilEnd / fadeOutDuration ) )
return
end
end
if fadeInDuration then
local startTime = bassObj._startTime
local timeSinceStart = now - startTime
if timeSinceStart < fadeInDuration then
bassObj:setVolumeEffective( volume * ( timeSinceStart / fadeInDuration ) )
return
end
end
return true
end
loadBass = function( loadFunc, path, settings, callback, failCallback, soundID )
if type( path ) ~= "string" then
error( "Expected path to be a string" )
end
settings = settings or {}
local flags = settings.Flags or "3d stereo noblock noplay"
local volume = settings.Volume or 1
local pitch = settings.Pitch or 1
local fadeStart = settings.FadeStart or 700
local fadeEnd = settings.FadeEnd or 2000
local useSimpleFading = settings.UseSimpleFading
local fadeIn = settings.FadeIn or 0
local fadeOut = settings.FadeOut or 0
local duration = settings.Duration or 0
local target = settings.Target or chip()
local loop = settings.Loop or false
local destroyWhenDone = false
local staleThreshold = settings.StaleThreshold or 30
duration = math.max( duration, 0 )
if duration == 0 then
duration = false
fadeOut = 0
if settings.DestroyWhenDone == nil then
destroyWhenDone = true
else
destroyWhenDone = settings.DestroyWhenDone
end
end
if useSimpleFading == nil then
useSimpleFading = true
end
if fadeIn <= 0 then
fadeIn = false
end
if fadeOut <= 0 then
fadeOut = false
end
if soundID then
timer.remove( "LKL_EasyBass_RetrySound_" .. soundID )
else
soundIDIncrement = soundIDIncrement + 1
soundID = soundIDIncrement
end
loadFunc( path, flags, function( bassObj, errNum, errName )
local success, nonBassErr
success, nonBassErr = pcall( function()
local now = realtime()
if not bassObj then
error( "Invalid bassObj" )
end
table.insert( allBassObjs, bassObj )
bassObj._easyBass_setVolume = bassObj.setVolume
bassObj._easyBass_stop = bassObj.stop
function bassObj:setVolume( vol )
if self._volume == vol then return end
self._volume = vol
if handleFadeInOut( self, realtime() ) then
self:setVolumeEffective( vol )
end
end
function bassObj:getVolume()
return self._volume
end
-- Sets the effective volume without changing the desired volume. Used by in/ou fading.
function bassObj:setVolumeEffective( vol )
if self._volumeEffective == vol then return end
self._volumeEffective = vol
self:_easyBass_setVolume( vol )
end
function bassObj:getVolumeEffective()
return self._volumeEffective
end
function bassObj:stop()
table.removeByValue( allBassObjs, self )
self:pause()
self:_easyBass_stop()
function self:stop()
end
end
bassObj._soundID = soundID
bassObj._staleThreshold = staleThreshold
bassObj._startTime = now
bassObj._dontDestroyWhenDone = not destroyWhenDone
bassObj._fadeInDuration = fadeIn
bassObj._fadeOutDuration = fadeOut
bassObj._volume = 1
bassObj._volumeEffective = 1
bassObj:setVolume( volume )
bassObj:setPitch( pitch )
bassObj:setLooping( loop )
bassObj:setFade( fadeStart, fadeEnd, useSimpleFading )
bassObj:play()
if duration then
bassObj._endTime = now + duration
timer.simple( duration, function()
if not isValid( bassObj ) then return end
bassObj:stop()
end )
end
if type( target ) == "Vector" then
bassObj:setPos( target )
bassObj._dontFollowTarget = true
elseif isValid( target ) then
bassObj._target = target
bassObj._dontFollowTarget = false
end
if callback then
callback( bassObj, errNum, errName )
end
end )
if not success then
if isValid( bassObj ) then
bassObj:pause()
bassObj:stop()
end
local retry = true
if failCallback then
retry = failCallback( errNum, errName, nonBassErr )
end
if retry then
timer.create( "LKL_EasyBass_RetrySound_" .. soundID, 1, 1, function()
loadBass( loadFunc, path, settings, callback, failCallback, soundID )
end )
end
end
end )
end
----- SETUP -----
hook.add( "Think", "LKL_EasyBass_Think", function()
local now = realtime()
for i = #allBassObjs, 1, -1 do
local bassObj = allBassObjs[i]
if isValid( bassObj ) then
if handleFollowTarget( bassObj ) then continue end
if handleDestroyWhenDone( bassObj ) then continue end
handleFadeInOut( bassObj, now )
else
table.remove( allBassObjs, i )
end
end
end )