@rbxts/sound-manager 2.3.1 → 2.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -36,7 +36,7 @@ npm install @rbxts/sound-manager
36
36
 
37
37
  ## 🚀 Quick Start
38
38
 
39
- [See the Documentation ->](https://dev-lukas0.github.io/Sound-Manager-Docs/)
39
+ [See the Documentation ->](https://dev-lukas0.github.io/Sound-Manager/)
40
40
 
41
41
  ### ⚡ Starting with Sound Manager
42
42
 
@@ -11,24 +11,29 @@ export declare function createSoundRegistry<T extends Record<string, SoundOption
11
11
  emitters: BasePart[];
12
12
  }) => void;
13
13
  preloadAll: () => void;
14
- load: (name: keyof T) => void;
14
+ load: (name: keyof T, spatial?: {
15
+ emitters: BasePart[];
16
+ }) => SoundHandle | undefined;
15
17
  fadeIn: (soundName: keyof T, duration: number, volume: number, spatial?: {
16
18
  emitters: BasePart[];
17
19
  }) => void;
18
20
  fadeOut: (soundName: keyof T, duration: number, targetVolume?: number, spatial?: {
19
21
  emitters: BasePart[];
20
22
  }) => void;
21
- reset: (sound: keyof T) => void;
22
- setTimePosition: (sound: keyof T, timePosition: number) => void;
23
- stopAll: (reset?: true) => void;
23
+ reset: (sound: keyof T, spatial?: boolean) => void;
24
+ setTimePosition: (sound: keyof T, timePosition: number, spatial?: boolean) => void;
25
+ stopAll: (reset?: true, spatial?: boolean) => void;
24
26
  preload: (sound: keyof T) => void;
25
- setGlobalVolume: (volume: number) => void;
27
+ setGlobalVolume: (volume: number, spatial?: boolean) => void;
26
28
  setVolume: (sound: keyof T, volume: number, spatial?: {
27
29
  emitters: BasePart[];
28
30
  }) => void;
29
- resetAll: (sound: keyof T) => void;
31
+ resetAll: (sound: keyof T, spatial?: boolean) => void;
30
32
  onEnd: (sound: keyof T, callback: () => void, spatial?: {
31
33
  emitters: BasePart[];
32
34
  }) => void;
33
- isPlaying: (sound: keyof T) => boolean;
35
+ isPlaying: (sound: keyof T, spatial?: boolean) => boolean;
36
+ preloadAllSpatial: (emittersMap: Partial<Record<keyof T, BasePart[]>>) => void;
37
+ preloadSpatial: (name: keyof T, emitters: BasePart[]) => void;
38
+ destroy: (sound: keyof T, spatial?: boolean) => void;
34
39
  };
@@ -20,27 +20,42 @@ local function createSoundRegistry(definitions)
20
20
  *
21
21
  * Loads a Sound
22
22
  * @param name Define which Sound should be loaded
23
+ * @param spatial Array of BaseParts
23
24
 
24
25
  ]]
25
- local function load(name)
26
- if folder:FindFirstChild(name) then
27
- return nil
28
- end
26
+ local function load(name, spatial)
29
27
  local config = definitions[name]
30
- local sound = Instance.new("Sound")
31
- sound.Name = name
32
- sound.SoundId = config.id
33
- local _condition = config.volume
34
- if _condition == nil then
35
- _condition = 1
36
- end
37
- sound.Volume = _condition
38
- local _condition_1 = config.loop
39
- if _condition_1 == nil then
40
- _condition_1 = false
28
+ if not spatial or not spatial.emitters then
29
+ if folder:FindFirstChild(name) then
30
+ return nil
31
+ end
32
+ local config = definitions[name]
33
+ local sound = Instance.new("Sound")
34
+ sound.Name = name
35
+ sound.SoundId = config.id
36
+ local _condition = config.volume
37
+ if _condition == nil then
38
+ _condition = 1
39
+ end
40
+ sound.Volume = _condition
41
+ local _condition_1 = config.loop
42
+ if _condition_1 == nil then
43
+ _condition_1 = false
44
+ end
45
+ sound.Looped = _condition_1
46
+ sound.Parent = folder
47
+ else
48
+ local emittersArray = spatial.emitters
49
+ local _exp = config.id
50
+ local _condition = config.volume
51
+ if _condition == nil then
52
+ _condition = 1
53
+ end
54
+ local handle = createSpatialHandle(_exp, emittersArray, _condition)
55
+ local _name = name
56
+ spatialHandles[_name] = handle
57
+ return handle
41
58
  end
42
- sound.Looped = _condition_1
43
- sound.Parent = folder
44
59
  end
45
60
  --[[
46
61
  *
@@ -113,6 +128,59 @@ local function createSoundRegistry(definitions)
113
128
  load(name)
114
129
  end
115
130
  end
131
+ --[[
132
+ *
133
+ * Preloads all spatial Sounds
134
+ * @param emittersMap
135
+
136
+ ]]
137
+ local function preloadAllSpatial(emittersMap)
138
+ for name, emitters in pairs(emittersMap) do
139
+ local emitterArray = emitters
140
+ if not emitterArray then
141
+ continue
142
+ end
143
+ local soundName = name
144
+ if spatialHandles[soundName] ~= nil then
145
+ continue
146
+ end
147
+ local config = definitions[soundName]
148
+ local _exp = config.id
149
+ local _condition = config.volume
150
+ if _condition == nil then
151
+ _condition = 1
152
+ end
153
+ local handle = createSpatialHandle(_exp, emitterArray, _condition)
154
+ spatialHandles[soundName] = handle
155
+ end
156
+ end
157
+ --[[
158
+ *
159
+ * Preloads a spatial sound
160
+ * @param name Sound Name
161
+ * @param emitters Emitters
162
+
163
+ ]]
164
+ local function preloadSpatial(name, emitters)
165
+ local _name = name
166
+ if spatialHandles[_name] ~= nil then
167
+ return nil
168
+ end
169
+ local config = definitions[name]
170
+ if not config then
171
+ warn(`{name} is not defined in the registry.`)
172
+ return nil
173
+ end
174
+ local _exp = config.id
175
+ local _exp_1 = emitters
176
+ local _condition = config.volume
177
+ if _condition == nil then
178
+ _condition = 1
179
+ end
180
+ local handle = createSpatialHandle(_exp, _exp_1, _condition)
181
+ local _name_1 = name
182
+ spatialHandles[_name_1] = handle
183
+ end
116
184
  --[[
117
185
  *
118
186
  * Smoothly fade in a sound
@@ -223,26 +291,43 @@ local function createSoundRegistry(definitions)
223
291
  *
224
292
  * Reset a Sound
225
293
  * @param sound Sound Instance
294
+ * @param spatial Define whether this sound is a spatial sound or not
226
295
 
227
296
  ]]
228
- local function reset(sound)
229
- local _sound = folder:FindFirstChild(sound)
230
- if not _sound then
231
- warn(`{sound} not found! Tip: Preload the sound first before using it.`)
232
- return nil
297
+ local function reset(sound, spatial)
298
+ if not spatial then
299
+ local _sound = folder:FindFirstChild(sound)
300
+ if not _sound then
301
+ warn(`{sound} not found! Tip: Preload the sound first before using it.`)
302
+ return nil
303
+ end
304
+ _sound.TimePosition = 0
305
+ end
306
+ local _sound_1 = sound
307
+ local _sound = spatialHandles[_sound_1]
308
+ local _result = _sound
309
+ if _result ~= nil then
310
+ _result:setTimePosition(0)
233
311
  end
234
- _sound.TimePosition = 0
235
312
  end
236
313
  --[[
237
314
  *
238
315
  * Reset every Sound
239
316
  * @param sound Sound Instance
317
+ * @param spatial Define whether this sound is a spatial sound or not
240
318
 
241
319
  ]]
242
- local function resetAll(sound)
243
- for _, sound in folder:GetChildren() do
244
- if sound:IsA("Sound") then
245
- sound.TimePosition = 0
320
+ local function resetAll(sound, spatial)
321
+ if not spatial then
322
+ for _, sound in folder:GetChildren() do
323
+ if sound:IsA("Sound") then
324
+ sound.TimePosition = 0
325
+ end
326
+ end
327
+ end
328
+ for _, instance in folder:GetChildren() do
329
+ if instance:IsA("AudioPlayer") then
330
+ instance.TimePosition = 0
246
331
  end
247
332
  end
248
333
  end
@@ -251,25 +336,46 @@ local function createSoundRegistry(definitions)
251
336
  * Set Time Position
252
337
  * @param sound Sound Instance
253
338
  * @param timePosition Time Position
339
+ * @param spatial Define whether this sound is a spatial sound or not
254
340
 
255
341
  ]]
256
- local function setTimePosition(sound, timePosition)
257
- local _sound = folder:FindFirstChild(sound)
258
- if not _sound then
259
- warn(`{sound} not found! Tip: Preload the sound first before using it.`)
260
- return nil
342
+ local function setTimePosition(sound, timePosition, spatial)
343
+ if not spatial then
344
+ local _sound = folder:FindFirstChild(sound)
345
+ if not _sound then
346
+ warn(`{sound} not found! Tip: Preload the sound first before using it.`)
347
+ return nil
348
+ end
349
+ _sound.TimePosition = timePosition
350
+ end
351
+ local _sound_1 = sound
352
+ local _sound = spatialHandles[_sound_1]
353
+ local _result = _sound
354
+ if _result ~= nil then
355
+ _result:setTimePosition(timePosition)
261
356
  end
262
- _sound.TimePosition = timePosition
263
357
  end
264
358
  --[[
265
359
  *
266
360
  * Stop every Sound
267
361
  * @param reset Define whether every Sound should also be reset?
362
+ * @param spatial Define whether this sound is a spatial sound or not
268
363
 
269
364
  ]]
270
- local function stopAll(reset)
365
+ local function stopAll(reset, spatial)
366
+ if not spatial then
367
+ for _, instance in folder:GetChildren() do
368
+ if not instance:IsA("Sound") then
369
+ continue
370
+ end
371
+ instance:Stop()
372
+ if reset then
373
+ instance.TimePosition = 0
374
+ end
375
+ end
376
+ end
271
377
  for _, instance in folder:GetChildren() do
272
- if not instance:IsA("Sound") then
378
+ if not instance:IsA("AudioPlayer") then
273
379
  continue
274
380
  end
275
381
  instance:Stop()
@@ -304,14 +410,25 @@ local function createSoundRegistry(definitions)
304
410
  *
305
411
  * Set the global Sound Volume
306
412
  * @param volume Sound Volume
413
+ * @param spatial Define whether this sound is a spatial sound or not
307
414
 
308
415
  ]]
309
- local function setGlobalVolume(volume)
416
+ local function setGlobalVolume(volume, spatial)
417
+ if not spatial then
418
+ for _, instance in folder:GetChildren() do
419
+ if not instance:IsA("Sound") then
420
+ continue
421
+ end
422
+ if instance:IsA("Sound") then
423
+ instance.Volume = volume
424
+ end
425
+ end
426
+ end
310
427
  for _, instance in folder:GetChildren() do
311
- if not instance:IsA("Sound") then
428
+ if not instance:IsA("AudioPlayer") then
312
429
  continue
313
430
  end
314
- if instance:IsA("Sound") then
431
+ if instance:IsA("AudioPlayer") then
315
432
  instance.Volume = volume
316
433
  end
317
434
  end
@@ -321,6 +438,7 @@ local function createSoundRegistry(definitions)
321
438
  * Does something on Sound end
322
439
  * @param name Sound Name
323
440
  * @param callback Callback
441
+ * @param spatial Define whether this sound is a spatial sound or not
324
442
 
325
443
  ]]
326
444
  local function onEnd(sound, callback, spatial)
@@ -354,18 +472,59 @@ local function createSoundRegistry(definitions)
354
472
  * Check whether a Sound is playing
355
473
  * @param sound Sound Instance
356
474
  * @returns Boolean
475
+ * @param spatial Define whether this sound is a spatial sound or not
357
476
 
358
477
  ]]
359
- local function isPlaying(sound)
360
- local _sound = folder:FindFirstChild(sound)
478
+ local function isPlaying(sound, spatial)
479
+ if not spatial then
480
+ local _sound = folder:FindFirstChild(sound)
481
+ if not _sound then
482
+ warn(`{sound} not found! Tip: Preload the sound first before using it.`)
483
+ end
484
+ if _sound.IsPlaying == true then
485
+ return true
486
+ else
487
+ return false
488
+ end
489
+ end
490
+ local _sound_1 = sound
491
+ local _sound = spatialHandles[_sound_1]
361
492
  if not _sound then
362
493
  warn(`{sound} not found! Tip: Preload the sound first before using it.`)
363
494
  end
364
- if _sound.IsPlaying == true then
495
+ local _result = _sound
496
+ if _result ~= nil then
497
+ _result = _result:playing()
498
+ end
499
+ if _result == true then
365
500
  return true
366
- else
367
- return false
368
501
  end
502
+ return false
503
+ end
504
+ --[[
505
+ *
506
+ * Delete a sound instance
507
+ * @param sound Sound Instance
508
+ * @param spatial Define whether this sound is a spatial sound or not
509
+
510
+ ]]
511
+ local function destroy(sound, spatial)
512
+ if not spatial then
513
+ local instance = folder:FindFirstChild(sound)
514
+ if not instance then
515
+ warn(`{sound} not found! Tip: Preload the sound first before using it.`)
516
+ return nil
517
+ end
518
+ instance:Destroy()
519
+ return nil
520
+ end
521
+ local _sound = sound
522
+ local handle = spatialHandles[_sound]
523
+ if not handle then
524
+ warn(`{sound} not found! Tip: Preload the sound first before using it.`)
525
+ return nil
526
+ end
527
+ handle:destroy()
369
528
  end
370
529
  return {
371
530
  play = play,
@@ -383,6 +542,9 @@ local function createSoundRegistry(definitions)
383
542
  resetAll = resetAll,
384
543
  onEnd = onEnd,
385
544
  isPlaying = isPlaying,
545
+ preloadAllSpatial = preloadAllSpatial,
546
+ preloadSpatial = preloadSpatial,
547
+ destroy = destroy,
386
548
  }
387
549
  end
388
550
  return {
@@ -52,6 +52,12 @@ local function createSpatialHandle(assetId, emitters, volume)
52
52
  played = function(self, callback)
53
53
  player.Ended:Connect(callback)
54
54
  end,
55
+ playing = function(self)
56
+ return player.IsPlaying
57
+ end,
58
+ setTimePosition = function(self, timeposition)
59
+ player.TimePosition = timeposition
60
+ end,
55
61
  }
56
62
  end
57
63
  return {
@@ -14,6 +14,8 @@ export interface SoundHandle {
14
14
  fadeOut?(duration: number): void;
15
15
  destroy(): void;
16
16
  played(callback: () => void): void;
17
+ playing(): boolean;
18
+ setTimePosition(timeposition: number): void;
17
19
  }
18
20
  interface SoundDefinition {
19
21
  id: string;
@@ -24,4 +26,10 @@ export interface CategoryOptions {
24
26
  category: string;
25
27
  sounds: Record<string, SoundDefinition>;
26
28
  }
29
+ export interface AudioListenerHandle {
30
+ listener: AudioListener;
31
+ output: AudioDeviceOutput;
32
+ wire: Wire;
33
+ destroy(): void;
34
+ }
27
35
  export {};
@@ -0,0 +1,8 @@
1
+ import { AudioListenerHandle } from "./core/options";
2
+ type ListenerParent = BasePart | Camera | Model;
3
+ /**
4
+ * Creates an Audio Listener
5
+ * @param parent BasePart | Camera | Model
6
+ */
7
+ export declare function createAudioListener(parent: ListenerParent, name?: string): AudioListenerHandle | undefined;
8
+ export {};
@@ -0,0 +1,39 @@
1
+ -- Compiled with roblox-ts v3.0.0
2
+ --[[
3
+ *
4
+ * Creates an Audio Listener
5
+ * @param parent BasePart | Camera | Model
6
+
7
+ ]]
8
+ local function createAudioListener(parent, name)
9
+ if parent:FindFirstChild("Sound-Manager-AudioListener") then
10
+ warn("AudioListener already exists on this parent")
11
+ return nil
12
+ end
13
+ local listener = Instance.new("AudioListener")
14
+ local _condition = name
15
+ if _condition == nil then
16
+ _condition = "Sound-Manager-AudioListener"
17
+ end
18
+ listener.Name = _condition
19
+ listener.Parent = parent
20
+ local output = Instance.new("AudioDeviceOutput")
21
+ output.Name = "Sound-Manager-AudioOutput"
22
+ local wire = Instance.new("Wire")
23
+ wire.Name = "ListenerToOutput"
24
+ wire.SourceInstance = listener
25
+ wire.TargetInstance = output
26
+ output.Parent = wire
27
+ wire.Parent = listener
28
+ return {
29
+ listener = listener,
30
+ output = output,
31
+ wire = wire,
32
+ destroy = function(self)
33
+ listener:Destroy()
34
+ end,
35
+ }
36
+ end
37
+ return {
38
+ createAudioListener = createAudioListener,
39
+ }
package/out/index.d.ts CHANGED
@@ -3,6 +3,7 @@ import { currentPlayingSounds } from "./developer-tools/currentPlayingSounds";
3
3
  import { soundProperties } from "./developer-tools/soundProperties";
4
4
  export * from "./core/createSoundRegistry";
5
5
  export * from "./core/createSoundCategoryRegistry";
6
+ export * from "./createAudioListener";
6
7
  export declare namespace Developer_Tools {
7
8
  const getTotalSoundCount: typeof TotalSoundCount;
8
9
  const getCurrentPlayingSounds: typeof currentPlayingSounds;
package/out/init.luau CHANGED
@@ -10,6 +10,9 @@ end
10
10
  for _k, _v in TS.import(script, script, "core", "createSoundCategoryRegistry") or {} do
11
11
  exports[_k] = _v
12
12
  end
13
+ for _k, _v in TS.import(script, script, "createAudioListener") or {} do
14
+ exports[_k] = _v
15
+ end
13
16
  local Developer_Tools = {}
14
17
  do
15
18
  local _container = Developer_Tools
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rbxts/sound-manager",
3
- "version": "2.3.1",
3
+ "version": "2.3.3",
4
4
  "description": "A sound manager for Roblox-Typescript projects.",
5
5
  "main": "out/init.lua",
6
6
  "scripts": {