@rbxts/sound-manager 2.3.1 → 2.3.4

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
 
@@ -83,4 +83,4 @@ See the [Roadmap](./Roadmap.md) for planned features and current progress.
83
83
   
84
84
 
85
85
  For more Details:
86
- https://dev-lukas0.github.io/Sound-Manager-Docs/
86
+ https://dev-lukas0.github.io/Sound-Manager/
@@ -86,7 +86,7 @@ local function createSoundCategoryRegistry(definitions)
86
86
  old:stop()
87
87
  old:destroy()
88
88
  end
89
- local handle = createSpatialHandle(instance.SoundId, spatial.emitters, instance.Volume)
89
+ local handle = createSpatialHandle(instance.SoundId, spatial.emitters, instance.Volume, false, 1)
90
90
  local _categoryHandles_1 = categoryHandles
91
91
  local _name_1 = instance.Name
92
92
  _categoryHandles_1[_name_1] = handle
@@ -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,50 @@ 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 _condition_1 = config.loop
55
+ if _condition_1 == nil then
56
+ _condition_1 = false
57
+ end
58
+ local _condition_2 = config.playbackSpeed
59
+ if _condition_2 == nil then
60
+ _condition_2 = 1
61
+ end
62
+ local handle = createSpatialHandle(_exp, emittersArray, _condition, _condition_1, _condition_2)
63
+ local _name = name
64
+ spatialHandles[_name] = handle
65
+ return handle
41
66
  end
42
- sound.Looped = _condition_1
43
- sound.Parent = folder
44
67
  end
45
68
  --[[
46
69
  *
@@ -66,7 +89,15 @@ local function createSoundRegistry(definitions)
66
89
  if _condition == nil then
67
90
  _condition = 1
68
91
  end
69
- local handle = createSpatialHandle(_exp, emittersArray, _condition)
92
+ local _condition_1 = config.loop
93
+ if _condition_1 == nil then
94
+ _condition_1 = false
95
+ end
96
+ local _condition_2 = config.playbackSpeed
97
+ if _condition_2 == nil then
98
+ _condition_2 = 1
99
+ end
100
+ local handle = createSpatialHandle(_exp, emittersArray, _condition, _condition_1, _condition_2)
70
101
  local _name = name
71
102
  spatialHandles[_name] = handle
72
103
  handle:play()
@@ -113,6 +144,75 @@ local function createSoundRegistry(definitions)
113
144
  load(name)
114
145
  end
115
146
  end
147
+ --[[
148
+ *
149
+ * Preloads all spatial Sounds
150
+ * @param emittersMap
151
+
152
+ ]]
153
+ local function preloadAllSpatial(emittersMap)
154
+ for name, emitters in pairs(emittersMap) do
155
+ local emitterArray = emitters
156
+ if not emitterArray then
157
+ continue
158
+ end
159
+ local soundName = name
160
+ if spatialHandles[soundName] ~= nil then
161
+ continue
162
+ end
163
+ local config = definitions[soundName]
164
+ local _exp = config.id
165
+ local _condition = config.volume
166
+ if _condition == nil then
167
+ _condition = 1
168
+ end
169
+ local _condition_1 = config.loop
170
+ if _condition_1 == nil then
171
+ _condition_1 = false
172
+ end
173
+ local _condition_2 = config.playbackSpeed
174
+ if _condition_2 == nil then
175
+ _condition_2 = 1
176
+ end
177
+ local handle = createSpatialHandle(_exp, emitterArray, _condition, _condition_1, _condition_2)
178
+ spatialHandles[soundName] = handle
179
+ end
180
+ end
181
+ --[[
182
+ *
183
+ * Preloads a spatial sound
184
+ * @param name Sound Name
185
+ * @param emitters Emitters
186
+
187
+ ]]
188
+ local function preloadSpatial(name, emitters)
189
+ local _name = name
190
+ if spatialHandles[_name] ~= nil then
191
+ return nil
192
+ end
193
+ local config = definitions[name]
194
+ if not config then
195
+ warn(`{name} is not defined in the registry.`)
196
+ return nil
197
+ end
198
+ local _exp = config.id
199
+ local _exp_1 = emitters
200
+ local _condition = config.volume
201
+ if _condition == nil then
202
+ _condition = 1
203
+ end
204
+ local _condition_1 = config.loop
205
+ if _condition_1 == nil then
206
+ _condition_1 = false
207
+ end
208
+ local _condition_2 = config.playbackSpeed
209
+ if _condition_2 == nil then
210
+ _condition_2 = 1
211
+ end
212
+ local handle = createSpatialHandle(_exp, _exp_1, _condition, _condition_1, _condition_2)
213
+ local _name_1 = name
214
+ spatialHandles[_name_1] = handle
215
+ end
116
216
  --[[
117
217
  *
118
218
  * Smoothly fade in a sound
@@ -149,7 +249,15 @@ local function createSoundRegistry(definitions)
149
249
  if _condition == nil then
150
250
  _condition = 1
151
251
  end
152
- local handle = createSpatialHandle(_exp, emittersArray, _condition)
252
+ local _condition_1 = config.loop
253
+ if _condition_1 == nil then
254
+ _condition_1 = false
255
+ end
256
+ local _condition_2 = config.playbackSpeed
257
+ if _condition_2 == nil then
258
+ _condition_2 = 1
259
+ end
260
+ local handle = createSpatialHandle(_exp, emittersArray, _condition, _condition_1, _condition_2)
153
261
  local _self = handle
154
262
  local _result = _self.fadeIn
155
263
  if _result ~= nil then
@@ -206,7 +314,15 @@ local function createSoundRegistry(definitions)
206
314
  if _condition == nil then
207
315
  _condition = 1
208
316
  end
209
- local handle = createSpatialHandle(_exp, emittersArray, _condition)
317
+ local _condition_1 = config.loop
318
+ if _condition_1 == nil then
319
+ _condition_1 = false
320
+ end
321
+ local _condition_2 = config.playbackSpeed
322
+ if _condition_2 == nil then
323
+ _condition_2 = 1
324
+ end
325
+ local handle = createSpatialHandle(_exp, emittersArray, _condition, _condition_1, _condition_2)
210
326
  local _self = handle
211
327
  local _result = _self.fadeOut
212
328
  if _result ~= nil then
@@ -223,26 +339,43 @@ local function createSoundRegistry(definitions)
223
339
  *
224
340
  * Reset a Sound
225
341
  * @param sound Sound Instance
342
+ * @param spatial Define whether this sound is a spatial sound or not
226
343
 
227
344
  ]]
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
345
+ local function reset(sound, spatial)
346
+ if not spatial then
347
+ local _sound = folder:FindFirstChild(sound)
348
+ if not _sound then
349
+ warn(`{sound} not found! Tip: Preload the sound first before using it.`)
350
+ return nil
351
+ end
352
+ _sound.TimePosition = 0
353
+ end
354
+ local _sound_1 = sound
355
+ local _sound = spatialHandles[_sound_1]
356
+ local _result = _sound
357
+ if _result ~= nil then
358
+ _result:setTimePosition(0)
233
359
  end
234
- _sound.TimePosition = 0
235
360
  end
236
361
  --[[
237
362
  *
238
363
  * Reset every Sound
239
364
  * @param sound Sound Instance
365
+ * @param spatial Define whether this sound is a spatial sound or not
240
366
 
241
367
  ]]
242
- local function resetAll(sound)
243
- for _, sound in folder:GetChildren() do
244
- if sound:IsA("Sound") then
245
- sound.TimePosition = 0
368
+ local function resetAll(sound, spatial)
369
+ if not spatial then
370
+ for _, sound in folder:GetChildren() do
371
+ if sound:IsA("Sound") then
372
+ sound.TimePosition = 0
373
+ end
374
+ end
375
+ end
376
+ for _, instance in folder:GetChildren() do
377
+ if instance:IsA("AudioPlayer") then
378
+ instance.TimePosition = 0
246
379
  end
247
380
  end
248
381
  end
@@ -251,25 +384,46 @@ local function createSoundRegistry(definitions)
251
384
  * Set Time Position
252
385
  * @param sound Sound Instance
253
386
  * @param timePosition Time Position
387
+ * @param spatial Define whether this sound is a spatial sound or not
254
388
 
255
389
  ]]
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
390
+ local function setTimePosition(sound, timePosition, spatial)
391
+ if not spatial then
392
+ local _sound = folder:FindFirstChild(sound)
393
+ if not _sound then
394
+ warn(`{sound} not found! Tip: Preload the sound first before using it.`)
395
+ return nil
396
+ end
397
+ _sound.TimePosition = timePosition
398
+ end
399
+ local _sound_1 = sound
400
+ local _sound = spatialHandles[_sound_1]
401
+ local _result = _sound
402
+ if _result ~= nil then
403
+ _result:setTimePosition(timePosition)
261
404
  end
262
- _sound.TimePosition = timePosition
263
405
  end
264
406
  --[[
265
407
  *
266
408
  * Stop every Sound
267
409
  * @param reset Define whether every Sound should also be reset?
410
+ * @param spatial Define whether this sound is a spatial sound or not
268
411
 
269
412
  ]]
270
- local function stopAll(reset)
413
+ local function stopAll(reset, spatial)
414
+ if not spatial then
415
+ for _, instance in folder:GetChildren() do
416
+ if not instance:IsA("Sound") then
417
+ continue
418
+ end
419
+ instance:Stop()
420
+ if reset then
421
+ instance.TimePosition = 0
422
+ end
423
+ end
424
+ end
271
425
  for _, instance in folder:GetChildren() do
272
- if not instance:IsA("Sound") then
426
+ if not instance:IsA("AudioPlayer") then
273
427
  continue
274
428
  end
275
429
  instance:Stop()
@@ -297,21 +451,42 @@ local function createSoundRegistry(definitions)
297
451
  _sound.Volume = volume
298
452
  else
299
453
  local emittersArray = spatial.emitters
300
- local handle = createSpatialHandle(config.id, emittersArray, volume)
454
+ local _exp = config.id
455
+ local _exp_1 = volume
456
+ local _condition = config.loop
457
+ if _condition == nil then
458
+ _condition = false
459
+ end
460
+ local _condition_1 = config.playbackSpeed
461
+ if _condition_1 == nil then
462
+ _condition_1 = 1
463
+ end
464
+ local handle = createSpatialHandle(_exp, emittersArray, _exp_1, _condition, _condition_1)
301
465
  end
302
466
  end
303
467
  --[[
304
468
  *
305
469
  * Set the global Sound Volume
306
470
  * @param volume Sound Volume
471
+ * @param spatial Define whether this sound is a spatial sound or not
307
472
 
308
473
  ]]
309
- local function setGlobalVolume(volume)
474
+ local function setGlobalVolume(volume, spatial)
475
+ if not spatial then
476
+ for _, instance in folder:GetChildren() do
477
+ if not instance:IsA("Sound") then
478
+ continue
479
+ end
480
+ if instance:IsA("Sound") then
481
+ instance.Volume = volume
482
+ end
483
+ end
484
+ end
310
485
  for _, instance in folder:GetChildren() do
311
- if not instance:IsA("Sound") then
486
+ if not instance:IsA("AudioPlayer") then
312
487
  continue
313
488
  end
314
- if instance:IsA("Sound") then
489
+ if instance:IsA("AudioPlayer") then
315
490
  instance.Volume = volume
316
491
  end
317
492
  end
@@ -321,6 +496,7 @@ local function createSoundRegistry(definitions)
321
496
  * Does something on Sound end
322
497
  * @param name Sound Name
323
498
  * @param callback Callback
499
+ * @param spatial Define whether this sound is a spatial sound or not
324
500
 
325
501
  ]]
326
502
  local function onEnd(sound, callback, spatial)
@@ -354,18 +530,59 @@ local function createSoundRegistry(definitions)
354
530
  * Check whether a Sound is playing
355
531
  * @param sound Sound Instance
356
532
  * @returns Boolean
533
+ * @param spatial Define whether this sound is a spatial sound or not
357
534
 
358
535
  ]]
359
- local function isPlaying(sound)
360
- local _sound = folder:FindFirstChild(sound)
536
+ local function isPlaying(sound, spatial)
537
+ if not spatial then
538
+ local _sound = folder:FindFirstChild(sound)
539
+ if not _sound then
540
+ warn(`{sound} not found! Tip: Preload the sound first before using it.`)
541
+ end
542
+ if _sound.IsPlaying == true then
543
+ return true
544
+ else
545
+ return false
546
+ end
547
+ end
548
+ local _sound_1 = sound
549
+ local _sound = spatialHandles[_sound_1]
361
550
  if not _sound then
362
551
  warn(`{sound} not found! Tip: Preload the sound first before using it.`)
363
552
  end
364
- if _sound.IsPlaying == true then
553
+ local _result = _sound
554
+ if _result ~= nil then
555
+ _result = _result:playing()
556
+ end
557
+ if _result == true then
365
558
  return true
366
- else
367
- return false
368
559
  end
560
+ return false
561
+ end
562
+ --[[
563
+ *
564
+ * Delete a sound instance
565
+ * @param sound Sound Instance
566
+ * @param spatial Define whether this sound is a spatial sound or not
567
+
568
+ ]]
569
+ local function destroy(sound, spatial)
570
+ if not spatial then
571
+ local instance = folder:FindFirstChild(sound)
572
+ if not instance then
573
+ warn(`{sound} not found! Tip: Preload the sound first before using it.`)
574
+ return nil
575
+ end
576
+ instance:Destroy()
577
+ return nil
578
+ end
579
+ local _sound = sound
580
+ local handle = spatialHandles[_sound]
581
+ if not handle then
582
+ warn(`{sound} not found! Tip: Preload the sound first before using it.`)
583
+ return nil
584
+ end
585
+ handle:destroy()
369
586
  end
370
587
  return {
371
588
  play = play,
@@ -383,6 +600,9 @@ local function createSoundRegistry(definitions)
383
600
  resetAll = resetAll,
384
601
  onEnd = onEnd,
385
602
  isPlaying = isPlaying,
603
+ preloadAllSpatial = preloadAllSpatial,
604
+ preloadSpatial = preloadSpatial,
605
+ destroy = destroy,
386
606
  }
387
607
  end
388
608
  return {
@@ -4,4 +4,4 @@ import { SoundHandle } from "./options";
4
4
  * @param assetId Asset ID
5
5
  * @param emitters Emitter
6
6
  */
7
- export declare function createSpatialHandle(assetId: string, emitters: BasePart[], volume: number): SoundHandle;
7
+ export declare function createSpatialHandle(assetId: string, emitters: BasePart[], volume: number, looped: boolean, playBackSpeed: number): SoundHandle;
@@ -10,11 +10,13 @@ local fadeOut = _functions.fadeOut
10
10
  * @param emitters Emitter
11
11
 
12
12
  ]]
13
- local function createSpatialHandle(assetId, emitters, volume)
13
+ local function createSpatialHandle(assetId, emitters, volume, looped, playBackSpeed)
14
14
  local player = Instance.new("AudioPlayer")
15
15
  player.Asset = assetId
16
16
  player.Volume = volume
17
17
  player.Parent = game:GetService("ReplicatedStorage")
18
+ player.Looping = looped
19
+ player.PlaybackSpeed = playBackSpeed
18
20
  local emitterInstances = {}
19
21
  for _, part in emitters do
20
22
  local emitter = Instance.new("AudioEmitter")
@@ -52,6 +54,15 @@ local function createSpatialHandle(assetId, emitters, volume)
52
54
  played = function(self, callback)
53
55
  player.Ended:Connect(callback)
54
56
  end,
57
+ playing = function(self)
58
+ return player.IsPlaying
59
+ end,
60
+ setTimePosition = function(self, timeposition)
61
+ player.TimePosition = timeposition
62
+ end,
63
+ setPlayBackSpeed = function(self, playbackspeed)
64
+ player.PlaybackSpeed = playbackspeed
65
+ end,
55
66
  }
56
67
  end
57
68
  return {
@@ -1,6 +1,7 @@
1
1
  export interface SoundOptions {
2
2
  volume?: number;
3
3
  loop?: boolean;
4
+ playbackSpeed?: number;
4
5
  id: string;
5
6
  spatial?: {
6
7
  attenuation?: number;
@@ -14,14 +15,24 @@ export interface SoundHandle {
14
15
  fadeOut?(duration: number): void;
15
16
  destroy(): void;
16
17
  played(callback: () => void): void;
18
+ playing(): boolean;
19
+ setTimePosition(timeposition: number): void;
20
+ setPlayBackSpeed(playbackspeed: number): void;
17
21
  }
18
22
  interface SoundDefinition {
19
23
  id: string;
20
24
  volume?: number;
21
25
  loop?: boolean;
26
+ playBackSpeed?: number;
22
27
  }
23
28
  export interface CategoryOptions {
24
29
  category: string;
25
30
  sounds: Record<string, SoundDefinition>;
26
31
  }
32
+ export interface AudioListenerHandle {
33
+ listener: AudioListener;
34
+ output: AudioDeviceOutput;
35
+ wire: Wire;
36
+ destroy(): void;
37
+ }
27
38
  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.4",
4
4
  "description": "A sound manager for Roblox-Typescript projects.",
5
5
  "main": "out/init.lua",
6
6
  "scripts": {