1- -- !nonstrict
1+ -- !strict
22--[=[
33 Handles applying global volume and effects to specific sounds in a group based upon a path.
44
@@ -11,25 +11,46 @@ local RunService = game:GetService("RunService")
1111local SoundService = game :GetService (" SoundService" )
1212
1313local Maid = require (" Maid" )
14+ local Promise = require (" Promise" )
1415local ServiceBag = require (" ServiceBag" )
16+ local SoundEffectsList = require (" SoundEffectsList" )
1517local SoundEffectsRegistry = require (" SoundEffectsRegistry" )
1618local SoundGroupPathUtils = require (" SoundGroupPathUtils" )
1719local SoundGroupTracker = require (" SoundGroupTracker" )
20+ local SoundGroupVolume = require (" SoundGroupVolume" )
1821local WellKnownSoundGroups = require (" WellKnownSoundGroups" )
1922
2023local SoundEffectService = {}
2124SoundEffectService .ServiceName = " SoundEffectService"
2225
23- function SoundEffectService :Init (serviceBag : ServiceBag.ServiceBag )
24- assert (not self ._serviceBag , " Already initialized" )
26+ export type SoundEffectService = typeof (setmetatable (
27+ {} :: {
28+ _serviceBag : ServiceBag .ServiceBag ,
29+ _maid : Maid .Maid ,
30+ _soundEffectsRegister : any , -- SoundEffectsRegistry.SoundEffectsRegistry,
31+ _tracker : any , -- SoundGroupTracker.SoundGroupTracker,
32+ },
33+ {} :: typeof ({ __index = SoundEffectService })
34+ ))
35+
36+ function SoundEffectService .Init (self : SoundEffectService , serviceBag : ServiceBag.ServiceBag )
37+ assert (not (self :: any )._serviceBag , " Already initialized" )
2538 self ._serviceBag = assert (serviceBag , " No serviceBag" )
2639 self ._maid = Maid .new ()
2740
41+ -- External
42+ self ._serviceBag :GetService (require (" TieRealmService" ))
43+ self ._serviceBag :GetService (require (" RoguePropertyService" ))
44+
45+ -- State
2846 self ._soundEffectsRegister = self ._maid :Add (SoundEffectsRegistry .new ())
2947 self ._tracker = self ._maid :Add (SoundGroupTracker .new (SoundService ))
48+
49+ -- Binders
50+ self ._serviceBag :GetService (require (" SoundGroupVolume" ))
3051end
3152
32- function SoundEffectService : Start ()
53+ function SoundEffectService . Start ( self : SoundEffectService ): ()
3354 self :_setupEffectApplication ()
3455end
3556
3859 @param sound Sound
3960 @param soundGroupPath string? -- Optional
4061]=]
41- function SoundEffectService : RegisterSFX (sound : Sound , soundGroupPath : string ? )
62+ function SoundEffectService . RegisterSFX (self : SoundEffectService , sound : Sound , soundGroupPath : string ): ( )
4263 assert (typeof (sound ) == " Instance" and sound :IsA (" Sound" ), " Bad sound" )
4364 assert (SoundGroupPathUtils .isSoundGroupPath (soundGroupPath ) or soundGroupPath == nil , " Bad soundGroupPath" )
4465
4566 sound .SoundGroup = self :GetOrCreateSoundGroup (soundGroupPath or WellKnownSoundGroups .SFX )
4667end
4768
48- function SoundEffectService :GetOrCreateSoundGroup (soundGroupPath : string ): SoundGroup
69+ --[=[
70+ Creates a NumberValue multiplier for the given sound group path.
71+
72+ Destroy (or unparent) the returned NumberValue to remove the multiplier.
73+
74+ @param soundGroupPath string
75+ @return Promise<NumberValue>
76+ ]=]
77+ function SoundEffectService :PromiseCreateVolumeMultiplier (soundGroupPath : string ): Promise .Promise <NumberValue>
78+ local soundGroup = self :GetOrCreateSoundGroup (soundGroupPath )
79+ if soundGroup == nil then
80+ return Promise .rejected (" Failed to get or create sound group for path: " .. soundGroupPath )
81+ end
82+
83+ local soundGroupVolumeBinder = self ._serviceBag :GetService (require (" SoundGroupVolume" ))
84+ soundGroupVolumeBinder :Tag (soundGroup )
85+
86+ return soundGroupVolumeBinder :Promise (soundGroup ):Then (function (soundGroupVolume )
87+ return soundGroupVolume :CreateMultiplier ()
88+ end )
89+ end
90+
91+ --[=[
92+ Returns the SoundGroup for the given path, creating it if it does not exist.
93+
94+ @param soundGroupPath string
95+ @return SoundGroup
96+ ]=]
97+ function SoundEffectService .GetOrCreateSoundGroup (self : SoundEffectService , soundGroupPath : string ): SoundGroup
4998 assert (SoundGroupPathUtils .isSoundGroupPath (soundGroupPath ), " Bad soundGroupPath" )
5099
51100 local found = self :GetSoundGroup (soundGroupPath )
@@ -54,10 +103,21 @@ function SoundEffectService:GetOrCreateSoundGroup(soundGroupPath: string): Sound
54103 end
55104
56105 -- Handle deferred mode
57- return SoundGroupPathUtils .findOrCreateSoundGroup (soundGroupPath )
106+ found = SoundGroupPathUtils .findOrCreateSoundGroup (soundGroupPath )
107+ assert (found , " Failed to create sound group for path" )
108+
109+ SoundGroupVolume :Tag (found )
110+
111+ return found
58112end
59113
60- function SoundEffectService :GetSoundGroup (soundGroupPath : string ): SoundGroup
114+ --[=[
115+ Returns the SoundGroup for the given path, or nil if it does not exist.
116+
117+ @param soundGroupPath string
118+ @return SoundGroup
119+ ]=]
120+ function SoundEffectService .GetSoundGroup (self : SoundEffectService , soundGroupPath : string ): SoundGroup ?
61121 assert (SoundGroupPathUtils .isSoundGroupPath (soundGroupPath ), " Bad soundGroupPath" )
62122
63123 if not self ._tracker then
@@ -71,27 +131,41 @@ function SoundEffectService:GetSoundGroup(soundGroupPath: string): SoundGroup
71131
72132 local found = self ._tracker :GetFirstSoundGroup (soundGroupPath )
73133 if found then
134+ SoundGroupVolume :Tag (found )
74135 return found
75136 end
76137
77- return SoundGroupPathUtils .findOrCreateSoundGroup (soundGroupPath )
138+ found = SoundGroupPathUtils .findSoundGroup (soundGroupPath )
139+ if found then
140+ SoundGroupVolume :Tag (found )
141+ end
142+
143+ return found
78144end
79145
80- function SoundEffectService :PushEffect (soundGroupPath : string , effect )
146+ function SoundEffectService .PushEffect (
147+ self : SoundEffectService ,
148+ soundGroupPath : string ,
149+ effect : SoundEffectsList.SoundEffectApplier
150+ ): ()
81151 assert (SoundGroupPathUtils .isSoundGroupPath (soundGroupPath ), " Bad soundGroupPath" )
82152 assert (type (effect ) == " function" , " Bad effect" )
83153
84154 return self ._soundEffectsRegister :PushEffect (soundGroupPath , effect )
85155end
86156
87- function SoundEffectService :ApplyEffects (soundGroupPath , instance )
157+ function SoundEffectService .ApplyEffects (
158+ self : SoundEffectService ,
159+ soundGroupPath : string ,
160+ instance : SoundGroup | Sound
161+ ): () -> ()
88162 assert (SoundGroupPathUtils .isSoundGroupPath (soundGroupPath ), " Bad soundGroupPath" )
89163 assert (typeof (instance ) == " Instance" and (instance :IsA (" SoundGroup" ) or instance :IsA (" Sound" )), " Bad instance" )
90164
91165 return self ._soundEffectsRegister :ApplyEffects (soundGroupPath , instance )
92166end
93167
94- function SoundEffectService : _setupEffectApplication ()
168+ function SoundEffectService . _setupEffectApplication ( self : SoundEffectService ): ()
95169 self ._maid :GiveTask (self ._tracker :ObserveSoundGroupsBrio ():Subscribe (function (brio )
96170 if brio :IsDead () then
97171 return
@@ -102,7 +176,7 @@ function SoundEffectService:_setupEffectApplication()
102176 if soundGroupPath then
103177 maid ._currentEffects = self ._soundEffectsRegister :ApplyEffects (soundGroupPath , soundGroup )
104178 else
105- maid ._currentEffects = nil
179+ maid ._currentEffects = nil :: any
106180 end
107181 end ))
108182 end ))
@@ -122,7 +196,7 @@ function SoundEffectService:_setupEffectApplication()
122196 end ))
123197end
124198
125- function SoundEffectService : Destroy ()
199+ function SoundEffectService . Destroy ( self : SoundEffectService ): ()
126200 self ._maid :DoCleaning ()
127201end
128202
0 commit comments