-
Notifications
You must be signed in to change notification settings - Fork 7
/
base.sfx.channelpool.bmx
270 lines (210 loc) · 7.43 KB
/
base.sfx.channelpool.bmx
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
Rem
====================================================================
class providing a simple channel pool
====================================================================
With this class it is easy to allocate channels or reuse existing
ones in the case of a limitation regarding channel count.
You are able to protect channels so they do not get returned for
reuse (like background music channels).
====================================================================
If not otherwise stated, the following code is available under the
following licence:
LICENCE: zlib/libpng
Copyright (C) 2015 Ronny Otto, digidea.de
This software is provided 'as-is', without any express or
implied warranty. In no event will the authors be held liable
for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it
and redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you
must not claim that you wrote the original software. If you use
this software in a product, an acknowledgment in the product
documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
====================================================================
EndRem
SuperStrict
Import brl.Audio
Import brl.Map
Type TChannelPool
Global channels:TMap = CreateMap()
'a list of keys for channels you cannot "overwrite" when exceeding
'a limit
Global protectedChannels:string[]
'is there a limit of channels? As channels are "software mixed"
'by the audio-module, there shouldn't be some
Global channelLimit:int = -1
Global channelCount:int = -1
'returns the channel associated to the given key
'If no channel exists yet, a new one is created/added automatically
Function GetChannel:TChannel(key:string)
key = key.toLower()
local channel:TChannel = TChannel(channels.ValueForKey(key))
'if channel was not existing yet, create a new one
If not channel
channel = AllocChannel()
If not channel
Throw "TChannelPool: Failed to allocate new channel."
EndIf
AddChannel(key, channel)
EndIf
'if we do not have a channel now, we will surely have exceeded a
'limit. In that case we return a random one to override it
If not channel and GetChannelCount() >= 1
local channelKey:string = GetRandomChannelKey()
If not channelKey
Throw "TChannelPool: Limit exceeded. No existing and valid channel found to reuse."
EndIf
local channel:TChannel = TChannel(channels.ValueForKey(channelKey))
If not channel
Throw "TChannelPool: channelKey invalid."
EndIf
'remove the old channel, and add it back with the new
'name - so from now on, the newly requested key is stored
'instead of the old one
RemoveChannel(channelKey)
AddChannel(key, channel)
EndIf
return channel
End Function
'returns the channel of a random channel key
Function GetRandomChannel:TChannel()
return TChannel(channels.ValueForKey( GetRandomChannelKey() ))
End Function
'returns the key of a random channel
'currently unused channels are preferred so it is not truly "random"
Function GetRandomChannelKey:String()
'if no protected channels are existent ... we just could
'randomly access a channel
If protectedChannels.length = 0
local randNumber:int = Rnd(1, GetChannelCount())
local randPosition:int = 0
For local k:string = EachIn channels.Keys()
randPosition :+ 1
If randPosition >= randNumber and not isProtectedChannel(k)
return k
EndIf
Next
return ""
Else
'create an array containing just "unprotected" channels
'also store not playing channels in an extra array
'because we prefer an currently unused channel
local unprotectedChannelKeys:string[]
local unprotectedUnusedChannelKeys:string[]
local channel:TChannel
For local k:string = EachIn channels.Keys()
If isProtectedChannel(k) then Continue
channel = TChannel(channels.ValueForKey(k))
If not channel then continue
unprotectedChannelKeys :+ [k]
If not channel.Playing()
unprotectedUnusedChannelKeys :+ [k]
EndIf
Next
If not unprotectedChannelKeys
Throw "TChannelPool.GetRandomChannelKey(): No unprotected and valid channel found to return."
Else
If unprotectedUnusedChannelKeys
return unprotectedUnusedChannelKeys[ Rnd(0,unprotectedUnusedChannelKeys.length-1) ]
Else
return unprotectedChannelKeys[ Rnd(0,unprotectedChannelKeys.length-1) ]
EndIf
EndIf
EndIf
return ""
End Function
Function RemoveChannel:TChannel(key:string)
key = key.toLower()
local channel:TChannel = GetChannel(key)
if not channel then return Null
channels.Remove(key)
'invalidate channel count
channelCount = -1
'remove a previously set protection?
UnProtectChannel(key)
return channel
End Function
Function HasChannel:Int(key:string)
key = key.toLower()
For local k:string = EachIn channels.Keys()
if k = key then return True
Next
return False
End Function
'adds a channel - and overwrites potentially existing ones
Function AddChannel:TChannel(key:string, channel:TChannel)
'if there is a limit, skip adding a channel if limit is
'exceeded
If channelLimit >= 0 and GetChannelCount() > channelLimit
return Null
EndIf
key = key.toLower()
channels.insert(key, channel)
'invalidate channelCount
channelCount = -1
'remove a previously set protection?
'UnProtectChannel(key)
return channel
End Function
Function isProtectedChannel:Int(key:string)
if not protectedChannels then return False
key = key.ToLower()
For local k:string = EachIn protectedChannels
if k = key then return True
Next
return False
End Function
'mark a channel for protection so it cannot get overwritten
'when the limits exceed
Function ProtectChannel:int(key:string)
'already protected?
if isProtectedChannel(key) then return True
key = key.ToLower()
protectedChannels :+ [key]
End Function
Function UnProtectChannel:int(key:string)
if not protectedChannels then return False
key = key.ToLower()
local newProtectedChannels:string[]
For local k:string = EachIn protectedChannels
if k <> key then newProtectedChannels :+ [k]
Next
protectedChannels = protectedChannels
End Function
Function GetChannelCount:int(forceRecount:int = False)
'use cached var
if not forceRecount and channelCount >= 0 then return channelCount
channelCount = 0
For local channel:TChannel = EachIn channels.Values()
channelCount :+1
Next
return channelCount
End Function
End Type
'convenience accessors
Function GetPooledChannel:TChannel(key:string)
return TChannelPool.GetChannel(key)
End Function
Function GetRandomPooledChannel:TChannel()
return TChannelPool.GetRandomChannel()
End Function
Function AddPooledChannel:TChannel(key:string, channel:TChannel)
return TChannelPool.AddChannel(key, channel)
End Function
Function RemovePooledChannel:TChannel(key:string)
return TChannelPool.RemoveChannel(key)
End Function
Function PooledChannelExists:Int(key:string)
return TChannelPool.HasChannel(key)
End Function
Function ProtectPooledChannel(key:string)
TChannelPool.ProtectChannel(key)
End Function
Function UnProtectPooledChannel(key:string)
TChannelPool.UnProtectChannel(key)
End Function