@rbxts/sound-manager 1.2.0 → 2.0.0

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
@@ -24,3 +24,6 @@ Sounds.preloadAll();
24
24
 
25
25
  Sounds.play("SCP096");
26
26
  ```
27
+
28
+ For more Details:
29
+ https://dev-lukas0.github.io/Sound-Manager-Docs/
@@ -0,0 +1,9 @@
1
+ import { CategoryOptions } from "./options";
2
+ /**
3
+ * Create a sound category Registry
4
+ * @param definitions Define the Categorys
5
+ */
6
+ export declare function createSoundCategoryRegistry<T extends Record<string, CategoryOptions>>(definitions: T): {
7
+ loadCategory: (name: keyof T) => void;
8
+ playCategory: <C extends keyof T>(name: C) => void;
9
+ };
@@ -0,0 +1,85 @@
1
+ -- Compiled with roblox-ts v3.0.0
2
+ --[[
3
+ *
4
+ * Create a sound category Registry
5
+ * @param definitions Define the Categorys
6
+
7
+ ]]
8
+ local function createSoundCategoryRegistry(definitions)
9
+ local ReplicatedStorage = game:GetService("ReplicatedStorage")
10
+ local folder = ReplicatedStorage:FindFirstChild("Sounds")
11
+ if not folder then
12
+ folder = Instance.new("Folder")
13
+ folder.Name = "Sounds"
14
+ folder.Parent = ReplicatedStorage
15
+ end
16
+ --[[
17
+ *
18
+ * Loads a Category
19
+ * @param name Define which Sound Category should be loaded
20
+
21
+ ]]
22
+ local function loadCategory(name)
23
+ if folder:FindFirstChild(name) then
24
+ return nil
25
+ end
26
+ local config = definitions[name]
27
+ local category = folder:FindFirstChild(config.category)
28
+ if not category then
29
+ category = Instance.new("Folder")
30
+ category.Name = config.category
31
+ category.Parent = folder
32
+ end
33
+ for sound, soundConfig in pairs(config.sounds) do
34
+ if category:FindFirstChild(sound) then
35
+ continue
36
+ end
37
+ local newSound = Instance.new("Sound")
38
+ newSound.Name = sound
39
+ newSound.SoundId = soundConfig.id
40
+ local _condition = soundConfig.volume
41
+ if _condition == nil then
42
+ _condition = 1
43
+ end
44
+ newSound.Volume = _condition
45
+ local _condition_1 = soundConfig.loop
46
+ if _condition_1 == nil then
47
+ _condition_1 = false
48
+ end
49
+ newSound.Looped = _condition_1
50
+ newSound.Parent = category
51
+ end
52
+ end
53
+ --[[
54
+ *
55
+ * Play every Sound from a Sound Category
56
+ * @param name Sound Category
57
+
58
+ ]]
59
+ local function playCategory(name)
60
+ loadCategory(name)
61
+ local config = definitions[name]
62
+ local ReplicatedStorage = game:GetService("ReplicatedStorage")
63
+ local folder = ReplicatedStorage:FindFirstChild(config.category)
64
+ for sound in pairs(config.sounds) do
65
+ local _sound = folder:FindFirstChild(sound)
66
+ if not _sound then
67
+ continue
68
+ end
69
+ local _result = _sound
70
+ if _result ~= nil then
71
+ _result = _result:IsA("Sound")
72
+ end
73
+ if _result then
74
+ _sound:Play()
75
+ end
76
+ end
77
+ end
78
+ return {
79
+ loadCategory = loadCategory,
80
+ playCategory = playCategory,
81
+ }
82
+ end
83
+ return {
84
+ createSoundCategoryRegistry = createSoundCategoryRegistry,
85
+ }
@@ -1,12 +1,21 @@
1
1
  import { SoundOptions } from "./options";
2
2
  /**
3
- *
3
+ * Create a sound Registry
4
4
  * @param definitions Define the Sounds
5
- * @returns
6
5
  */
7
6
  export declare function createSoundRegistry<T extends Record<string, SoundOptions>>(definitions: T): {
8
7
  play: (name: keyof T) => void;
9
8
  stop: (name: keyof T) => void;
10
9
  preloadAll: () => void;
11
10
  load: (name: keyof T) => void;
11
+ fadeIn: (soundName: keyof T, duration: number, volume: number) => void;
12
+ fadeOut: (soundName: keyof T, duration: number, targetVolume?: number) => void;
13
+ reset: (sound: keyof T) => void;
14
+ setTimePosition: (sound: keyof T, timePosition: number) => void;
15
+ stopAll: (reset?: true) => void;
16
+ preload: (sound: keyof T) => void;
17
+ setGlobalVolume: (volume: number) => void;
18
+ setVolume: (sound: keyof T, volume: number) => void;
19
+ resetAll: (sound: keyof T) => void;
20
+ onEnd: (sound: keyof T, callback: () => void) => void;
12
21
  };
@@ -1,9 +1,8 @@
1
1
  -- Compiled with roblox-ts v3.0.0
2
2
  --[[
3
3
  *
4
- *
4
+ * Create a sound Registry
5
5
  * @param definitions Define the Sounds
6
- * @returns
7
6
 
8
7
  ]]
9
8
  local function createSoundRegistry(definitions)
@@ -16,9 +15,8 @@ local function createSoundRegistry(definitions)
16
15
  end
17
16
  --[[
18
17
  *
19
- *
18
+ * Loads a Sound
20
19
  * @param name Define which Sound should be loaded
21
- * @returns
22
20
 
23
21
  ]]
24
22
  local function load(name)
@@ -43,7 +41,7 @@ local function createSoundRegistry(definitions)
43
41
  end
44
42
  --[[
45
43
  *
46
- *
44
+ * Plays a Sound
47
45
  * @param name Define which Sound should be played
48
46
 
49
47
  ]]
@@ -57,7 +55,7 @@ local function createSoundRegistry(definitions)
57
55
  end
58
56
  --[[
59
57
  *
60
- *
58
+ * Stops a Sound
61
59
  * @param name Define which Sound should be stopped
62
60
 
63
61
  ]]
@@ -78,11 +76,188 @@ local function createSoundRegistry(definitions)
78
76
  load(name)
79
77
  end
80
78
  end
79
+ --[[
80
+ *
81
+ * Smoothly fad in a sound
82
+ * @param soundName Sound Instance
83
+ * @param duration Time in Seconds
84
+ * @param volume Volume
85
+
86
+ ]]
87
+ local function fadeIn(soundName, duration, volume)
88
+ local sound = folder:FindFirstChild(soundName)
89
+ sound.Volume = 0
90
+ sound:Play()
91
+ local step = 0.05
92
+ local interval = duration * step
93
+ task.spawn(function()
94
+ local vol = 0
95
+ while vol < volume do
96
+ vol += step
97
+ sound.Volume = math.clamp(vol, 0, volume)
98
+ task.wait(interval)
99
+ end
100
+ end)
101
+ end
102
+ --[[
103
+ *
104
+ * Smoothly fade out a sound
105
+ * @param soundName Sound name from Registry
106
+ * @param duration Time in seconds
107
+ * @param targetVolume Optional target volume (default 0)
108
+
109
+ ]]
110
+ local function fadeOut(soundName, duration, targetVolume)
111
+ local sound = folder:FindFirstChild(soundName)
112
+ if not sound then
113
+ return nil
114
+ end
115
+ local startVolume = sound.Volume
116
+ local _condition = targetVolume
117
+ if _condition == nil then
118
+ _condition = 0
119
+ end
120
+ local endVolume = _condition
121
+ local step = 0.05
122
+ local interval = duration * step
123
+ task.spawn(function()
124
+ local vol = startVolume
125
+ while vol > endVolume do
126
+ vol = math.clamp(vol - step, endVolume, startVolume)
127
+ sound.Volume = vol
128
+ task.wait(interval)
129
+ end
130
+ sound.Volume = endVolume
131
+ if endVolume == 0 then
132
+ sound:Stop()
133
+ end
134
+ end)
135
+ end
136
+ --[[
137
+ *
138
+ * Reset a Sound
139
+ * @param sound Sound Instance
140
+
141
+ ]]
142
+ local function reset(sound)
143
+ local _sound = folder:FindFirstChild(sound)
144
+ if not (sound ~= 0 and sound == sound and sound ~= "" and sound) then
145
+ return nil
146
+ end
147
+ _sound.TimePosition = 0
148
+ end
149
+ --[[
150
+ *
151
+ * Reset every Sound
152
+ * @param sound Sound Instance
153
+
154
+ ]]
155
+ local function resetAll(sound)
156
+ for _, sound in folder:GetChildren() do
157
+ if sound:IsA("Sound") then
158
+ sound.TimePosition = 0
159
+ end
160
+ end
161
+ end
162
+ --[[
163
+ *
164
+ * Set Time Position
165
+ * @param sound Sound Instance
166
+ * @param timePosition Time Position
167
+
168
+ ]]
169
+ local function setTimePosition(sound, timePosition)
170
+ local _sound = folder:FindFirstChild(sound)
171
+ if not (sound ~= 0 and sound == sound and sound ~= "" and sound) then
172
+ return nil
173
+ end
174
+ _sound.TimePosition = timePosition
175
+ end
176
+ --[[
177
+ *
178
+ * Stop every Sound
179
+ * @param reset Define whether every Sound should also be reset?
180
+
181
+ ]]
182
+ local function stopAll(reset)
183
+ for _, instance in folder:GetChildren() do
184
+ if not instance:IsA("Sound") then
185
+ continue
186
+ end
187
+ instance:Stop()
188
+ if reset then
189
+ instance.TimePosition = 0
190
+ end
191
+ end
192
+ end
193
+ --[[
194
+ *
195
+ * Set Sound Volume
196
+ * @param sound Sound Instance
197
+ * @param volume Sound Volume
198
+
199
+ ]]
200
+ local function setVolume(sound, volume)
201
+ local _sound = folder:FindFirstChild(sound)
202
+ if not (sound ~= 0 and sound == sound and sound ~= "" and sound) then
203
+ return nil
204
+ end
205
+ _sound.Volume = volume
206
+ end
207
+ --[[
208
+ *
209
+ * Set the global Sound Volume
210
+ * @param volume Sound Volume
211
+
212
+ ]]
213
+ local function setGlobalVolume(volume)
214
+ for _, instance in folder:GetChildren() do
215
+ if not instance:IsA("Sound") then
216
+ continue
217
+ end
218
+ if instance:IsA("Sound") then
219
+ instance.Volume = volume
220
+ end
221
+ end
222
+ end
223
+ --[[
224
+ *
225
+ * Plays Sound on Event Callback
226
+ * @param name Sound Name
227
+ * @param callback Callback
228
+
229
+ ]]
230
+ local function onEnd(sound, callback)
231
+ local _sound = folder:WaitForChild(sound)
232
+ if not _sound then
233
+ return nil
234
+ end
235
+ _sound.Ended:Connect(callback)
236
+ end
237
+ --[[
238
+ *
239
+ * Preloads a Sound
240
+ * @param sound Sound Instance
241
+
242
+ ]]
243
+ local function preload(sound)
244
+ load(sound)
245
+ end
81
246
  return {
82
247
  play = play,
83
248
  stop = stop,
84
249
  preloadAll = preloadAll,
85
250
  load = load,
251
+ fadeIn = fadeIn,
252
+ fadeOut = fadeOut,
253
+ reset = reset,
254
+ setTimePosition = setTimePosition,
255
+ stopAll = stopAll,
256
+ preload = preload,
257
+ setGlobalVolume = setGlobalVolume,
258
+ setVolume = setVolume,
259
+ resetAll = resetAll,
260
+ onEnd = onEnd,
86
261
  }
87
262
  end
88
263
  return {
@@ -3,3 +3,13 @@ export interface SoundOptions {
3
3
  loop?: boolean;
4
4
  id: string;
5
5
  }
6
+ interface SoundDefinition {
7
+ id: string;
8
+ volume?: number;
9
+ loop?: boolean;
10
+ }
11
+ export interface CategoryOptions {
12
+ category: string;
13
+ sounds: Record<string, SoundDefinition>;
14
+ }
15
+ export {};
package/out/index.d.ts CHANGED
@@ -1 +1,2 @@
1
1
  export * from "./core/createSoundRegistry";
2
+ export * from "./core/createSoundCategoryRegistry";
package/out/init.luau CHANGED
@@ -4,4 +4,7 @@ local exports = {}
4
4
  for _k, _v in TS.import(script, script, "core", "createSoundRegistry") or {} do
5
5
  exports[_k] = _v
6
6
  end
7
+ for _k, _v in TS.import(script, script, "core", "createSoundCategoryRegistry") or {} do
8
+ exports[_k] = _v
9
+ end
7
10
  return exports
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rbxts/sound-manager",
3
- "version": "1.2.0",
3
+ "version": "2.0.0",
4
4
  "description": "A sound manager for Roblox-Typescript projects.",
5
5
  "main": "out/init.lua",
6
6
  "scripts": {
@@ -1 +0,0 @@
1
- export {};
@@ -1,11 +0,0 @@
1
- -- Compiled with roblox-ts v3.0.0
2
- local TS = _G[script]
3
- local createSoundRegistry = TS.import(script, script.Parent, "createSoundRegistry").createSoundRegistry
4
- local Sounds = createSoundRegistry({
5
- Exlposion = {
6
- id = "rbxassetid://1",
7
- volume = 0.5,
8
- loop = false,
9
- },
10
- })
11
- return nil