@jswork/howler-audio-manager 0.0.0-stage → 1.0.1
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/LICENSE.txt +21 -0
- package/README.md +193 -2
- package/dist/index.cjs.js +612 -0
- package/dist/index.cjs.js.map +1 -0
- package/dist/index.d.mts +70 -0
- package/dist/index.d.ts +72 -0
- package/dist/index.esm.js +403 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.umd.js +485 -0
- package/dist/index.umd.js.map +1 -0
- package/package.json +51 -4
- package/src/index.ts +3 -0
- package/src/manager.ts +313 -0
- package/src/types.ts +40 -0
package/src/manager.ts
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
import { Howl, Howler } from 'howler';
|
|
2
|
+
import type {
|
|
3
|
+
AudioManagerOptions,
|
|
4
|
+
PlayOptions,
|
|
5
|
+
PlayUrlOptions,
|
|
6
|
+
AudioEventType,
|
|
7
|
+
AudioEventPayload,
|
|
8
|
+
EventSubscription,
|
|
9
|
+
SoundResourceMap
|
|
10
|
+
} from './types';
|
|
11
|
+
|
|
12
|
+
export class AudioManager<K extends string | number = string> {
|
|
13
|
+
private effectCache = new Map<K, Howl>();
|
|
14
|
+
private urlAudioCache = new Map<string, Howl>();
|
|
15
|
+
private preloadPromises = new Map<string, Promise<void>>();
|
|
16
|
+
private activeUrlHowl: Howl | null = null;
|
|
17
|
+
private globalVolume = 1.0;
|
|
18
|
+
private listeners = new Map<AudioEventType, Set<(payload: AudioEventPayload<K>) => void>>();
|
|
19
|
+
|
|
20
|
+
private HowlConstructor: typeof Howl;
|
|
21
|
+
private HowlerInstance: typeof Howler;
|
|
22
|
+
|
|
23
|
+
constructor(
|
|
24
|
+
options?: AudioManagerOptions,
|
|
25
|
+
inject?: { HowlClass?: typeof Howl; HowlerObj?: typeof Howler }
|
|
26
|
+
) {
|
|
27
|
+
this.HowlConstructor = inject?.HowlClass ?? Howl;
|
|
28
|
+
this.HowlerInstance = inject?.HowlerObj ?? Howler;
|
|
29
|
+
if (typeof options?.defaultVolume === 'number') {
|
|
30
|
+
this.setGlobalVolume(options.defaultVolume);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
hasEffect(key: K): boolean {
|
|
35
|
+
return this.effectCache.has(key);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
loadEffect(key: K, url: string): void {
|
|
39
|
+
if (this.effectCache.has(key)) {
|
|
40
|
+
this.effectCache.get(key)!.unload();
|
|
41
|
+
}
|
|
42
|
+
const howl = new this.HowlConstructor({
|
|
43
|
+
src: [url],
|
|
44
|
+
preload: true
|
|
45
|
+
});
|
|
46
|
+
howl.on('load', () => {
|
|
47
|
+
this.emit('load', { type: 'effect', key, url });
|
|
48
|
+
});
|
|
49
|
+
howl.on('loaderror', (_id: number, error: unknown) => {
|
|
50
|
+
this.emit('error', { type: 'effect', key, url, error });
|
|
51
|
+
});
|
|
52
|
+
this.effectCache.set(key, howl);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
loadEffects(map: SoundResourceMap<K>): void {
|
|
56
|
+
Object.keys(map).forEach((key) => {
|
|
57
|
+
const typedKey = (isNaN(Number(key)) ? key : Number(key)) as K;
|
|
58
|
+
this.loadEffect(typedKey, map[key as any]);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
playEffect(key: K, options?: PlayOptions): number | undefined {
|
|
63
|
+
const howl = this.effectCache.get(key);
|
|
64
|
+
if (!howl) {
|
|
65
|
+
console.warn(`[AudioManager] Effect not found for key: ${String(key)}.`);
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (howl.playing()) {
|
|
70
|
+
howl.stop();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (typeof options?.volume === 'number') {
|
|
74
|
+
howl.volume(options.volume);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Clear handlers from previous plays to avoid stale/duplicate callbacks.
|
|
78
|
+
howl.off('play');
|
|
79
|
+
howl.off('end');
|
|
80
|
+
howl.off('stop');
|
|
81
|
+
howl.off('playerror');
|
|
82
|
+
|
|
83
|
+
howl.once('play', (soundId: number) => {
|
|
84
|
+
options?.onStart?.(soundId);
|
|
85
|
+
this.emit('play', { type: 'effect', key, soundId });
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
howl.once('end', (soundId: number) => {
|
|
89
|
+
if (typeof options?.volume === 'number') {
|
|
90
|
+
howl.volume(1);
|
|
91
|
+
}
|
|
92
|
+
options?.onEnd?.(soundId);
|
|
93
|
+
this.emit('end', { type: 'effect', key, soundId });
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
howl.once('stop', (soundId: number) => {
|
|
97
|
+
if (typeof options?.volume === 'number') {
|
|
98
|
+
howl.volume(1);
|
|
99
|
+
}
|
|
100
|
+
options?.onStop?.(soundId);
|
|
101
|
+
this.emit('stop', { type: 'effect', key, soundId });
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
howl.once('playerror', (soundId: number, error: unknown) => {
|
|
105
|
+
options?.onError?.(soundId, error);
|
|
106
|
+
this.emit('error', { type: 'effect', key, soundId, error });
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
return howl.play();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
stopEffect(key: K): void {
|
|
113
|
+
const howl = this.effectCache.get(key);
|
|
114
|
+
if (howl) {
|
|
115
|
+
howl.stop();
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
removeEffect(key: K): void {
|
|
120
|
+
const howl = this.effectCache.get(key);
|
|
121
|
+
if (howl) {
|
|
122
|
+
howl.unload();
|
|
123
|
+
this.effectCache.delete(key);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
preloadUrl(url: string): Promise<void> {
|
|
128
|
+
const existing = this.urlAudioCache.get(url);
|
|
129
|
+
if (existing && existing.state() === 'loaded') {
|
|
130
|
+
return Promise.resolve();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const pending = this.preloadPromises.get(url);
|
|
134
|
+
if (pending) {
|
|
135
|
+
return pending;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// A howl in a terminal failed state (unloaded after error) must be replaced
|
|
139
|
+
// so a fresh instance can retry the load.
|
|
140
|
+
const howl = existing && existing.state() !== 'unloaded' ? existing : this.createUrlHowl(url);
|
|
141
|
+
|
|
142
|
+
const promise = new Promise<void>((resolve, reject) => {
|
|
143
|
+
howl.once('load', () => {
|
|
144
|
+
resolve();
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
howl.once('loaderror', (_id: number, error: unknown) => {
|
|
148
|
+
this.urlAudioCache.delete(url);
|
|
149
|
+
reject(error);
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
this.preloadPromises.set(url, promise);
|
|
154
|
+
const settle = () => {
|
|
155
|
+
if (this.preloadPromises.get(url) === promise) {
|
|
156
|
+
this.preloadPromises.delete(url);
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
promise.then(settle, settle);
|
|
160
|
+
|
|
161
|
+
return promise;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
preloadUrls(urls: string[]): Promise<void[]> {
|
|
165
|
+
return Promise.all(urls.map((url) => this.preloadUrl(url)));
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
private createUrlHowl(url: string): Howl {
|
|
169
|
+
const howl = new this.HowlConstructor({
|
|
170
|
+
src: [url],
|
|
171
|
+
preload: true
|
|
172
|
+
});
|
|
173
|
+
howl.on('load', () => {
|
|
174
|
+
this.emit('load', { type: 'url', url });
|
|
175
|
+
});
|
|
176
|
+
howl.on('loaderror', (_id: number, error: unknown) => {
|
|
177
|
+
this.urlAudioCache.delete(url);
|
|
178
|
+
this.emit('error', { type: 'url', url, error });
|
|
179
|
+
});
|
|
180
|
+
this.urlAudioCache.set(url, howl);
|
|
181
|
+
return howl;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
playUrl(url: string, options?: PlayUrlOptions): number {
|
|
185
|
+
const howl = this.urlAudioCache.get(url) || this.createUrlHowl(url);
|
|
186
|
+
|
|
187
|
+
// Singleton mutex: stop currently active URL audio if singleton !== false
|
|
188
|
+
const isSingleton = options?.singleton !== false;
|
|
189
|
+
if (
|
|
190
|
+
isSingleton &&
|
|
191
|
+
this.activeUrlHowl &&
|
|
192
|
+
this.activeUrlHowl !== howl &&
|
|
193
|
+
this.activeUrlHowl.playing()
|
|
194
|
+
) {
|
|
195
|
+
this.activeUrlHowl.stop();
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// Overlap prevention on the same audio instance
|
|
199
|
+
if (howl.playing()) {
|
|
200
|
+
howl.stop();
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (options?.loop !== undefined) {
|
|
204
|
+
howl.loop(options.loop);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (typeof options?.volume === 'number') {
|
|
208
|
+
howl.volume(options.volume);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (isSingleton) {
|
|
212
|
+
this.activeUrlHowl = howl;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// Clear handlers from previous plays to avoid stale/duplicate callbacks.
|
|
216
|
+
howl.off('play');
|
|
217
|
+
howl.off('end');
|
|
218
|
+
howl.off('stop');
|
|
219
|
+
howl.off('playerror');
|
|
220
|
+
|
|
221
|
+
howl.once('play', (soundId: number) => {
|
|
222
|
+
options?.onStart?.(soundId);
|
|
223
|
+
this.emit('play', { type: 'url', url, soundId });
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
howl.once('end', (soundId: number) => {
|
|
227
|
+
if (typeof options?.volume === 'number') {
|
|
228
|
+
howl.volume(1);
|
|
229
|
+
}
|
|
230
|
+
options?.onEnd?.(soundId);
|
|
231
|
+
this.emit('end', { type: 'url', url, soundId });
|
|
232
|
+
if (this.activeUrlHowl === howl && !howl.loop()) {
|
|
233
|
+
this.activeUrlHowl = null;
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
howl.once('stop', (soundId: number) => {
|
|
238
|
+
if (typeof options?.volume === 'number') {
|
|
239
|
+
howl.volume(1);
|
|
240
|
+
}
|
|
241
|
+
options?.onStop?.(soundId);
|
|
242
|
+
this.emit('stop', { type: 'url', url, soundId });
|
|
243
|
+
if (this.activeUrlHowl === howl) {
|
|
244
|
+
this.activeUrlHowl = null;
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
howl.once('playerror', (soundId: number, error: unknown) => {
|
|
249
|
+
options?.onError?.(soundId, error);
|
|
250
|
+
this.emit('error', { type: 'url', url, soundId, error });
|
|
251
|
+
if (this.activeUrlHowl === howl) {
|
|
252
|
+
this.activeUrlHowl = null;
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
return howl.play();
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
stopUrl(url: string): void {
|
|
260
|
+
const howl = this.urlAudioCache.get(url);
|
|
261
|
+
if (howl) {
|
|
262
|
+
howl.stop();
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
on(event: AudioEventType, handler: (payload: AudioEventPayload<K>) => void): EventSubscription {
|
|
267
|
+
const set = this.listeners.get(event) ?? new Set<(payload: AudioEventPayload<K>) => void>();
|
|
268
|
+
this.listeners.set(event, set);
|
|
269
|
+
set.add(handler);
|
|
270
|
+
return {
|
|
271
|
+
destroy: () => {
|
|
272
|
+
set.delete(handler);
|
|
273
|
+
}
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
protected emit(event: AudioEventType, payload: AudioEventPayload<K>): void {
|
|
278
|
+
const set = this.listeners.get(event);
|
|
279
|
+
if (set) {
|
|
280
|
+
set.forEach((handler) => {
|
|
281
|
+
try {
|
|
282
|
+
handler(payload);
|
|
283
|
+
} catch (err) {
|
|
284
|
+
console.error(`[AudioManager] Error in event listener for ${event}:`, err);
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
setGlobalVolume(volume: number): void {
|
|
291
|
+
this.globalVolume = volume;
|
|
292
|
+
this.HowlerInstance.volume(volume);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
mute(muted: boolean): void {
|
|
296
|
+
this.HowlerInstance.mute(muted);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
stopAll(): void {
|
|
300
|
+
this.HowlerInstance.stop();
|
|
301
|
+
this.activeUrlHowl = null;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
destroy(): void {
|
|
305
|
+
this.effectCache.forEach((howl) => howl.unload());
|
|
306
|
+
this.urlAudioCache.forEach((howl) => howl.unload());
|
|
307
|
+
this.effectCache.clear();
|
|
308
|
+
this.urlAudioCache.clear();
|
|
309
|
+
this.preloadPromises.clear();
|
|
310
|
+
this.listeners.clear();
|
|
311
|
+
this.activeUrlHowl = null;
|
|
312
|
+
}
|
|
313
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export interface AudioManagerOptions {
|
|
2
|
+
/** Default global volume (0.0 to 1.0), defaults to 1.0 */
|
|
3
|
+
defaultVolume?: number;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export interface PlayOptions {
|
|
7
|
+
/** Play volume override (0.0 to 1.0) */
|
|
8
|
+
volume?: number;
|
|
9
|
+
/** Callback triggered when playback starts */
|
|
10
|
+
onStart?: (soundId: number) => void;
|
|
11
|
+
/** Callback triggered when playback ends */
|
|
12
|
+
onEnd?: (soundId: number) => void;
|
|
13
|
+
/** Callback triggered when playback encounters an error */
|
|
14
|
+
onError?: (soundId: number, error: unknown) => void;
|
|
15
|
+
/** Callback triggered when playback is manually stopped */
|
|
16
|
+
onStop?: (soundId: number) => void;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface PlayUrlOptions extends PlayOptions {
|
|
20
|
+
/** Whether to loop the audio, defaults to false */
|
|
21
|
+
loop?: boolean;
|
|
22
|
+
/** Whether to stop currently playing URL audio, defaults to true */
|
|
23
|
+
singleton?: boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type AudioEventType = 'play' | 'end' | 'stop' | 'pause' | 'error' | 'load';
|
|
27
|
+
|
|
28
|
+
export interface AudioEventPayload<K = string> {
|
|
29
|
+
type: 'effect' | 'url';
|
|
30
|
+
key?: K;
|
|
31
|
+
url?: string;
|
|
32
|
+
soundId?: number;
|
|
33
|
+
error?: unknown;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface EventSubscription {
|
|
37
|
+
destroy: () => void;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export type SoundResourceMap<K extends string | number> = Record<K, string>;
|