@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.
@@ -0,0 +1,70 @@
1
+ import { Howl, Howler } from 'howler';
2
+
3
+ interface AudioManagerOptions {
4
+ /** Default global volume (0.0 to 1.0), defaults to 1.0 */
5
+ defaultVolume?: number;
6
+ }
7
+ interface PlayOptions {
8
+ /** Play volume override (0.0 to 1.0) */
9
+ volume?: number;
10
+ /** Callback triggered when playback starts */
11
+ onStart?: (soundId: number) => void;
12
+ /** Callback triggered when playback ends */
13
+ onEnd?: (soundId: number) => void;
14
+ /** Callback triggered when playback encounters an error */
15
+ onError?: (soundId: number, error: unknown) => void;
16
+ /** Callback triggered when playback is manually stopped */
17
+ onStop?: (soundId: number) => void;
18
+ }
19
+ 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
+ type AudioEventType = 'play' | 'end' | 'stop' | 'pause' | 'error' | 'load';
26
+ interface AudioEventPayload<K = string> {
27
+ type: 'effect' | 'url';
28
+ key?: K;
29
+ url?: string;
30
+ soundId?: number;
31
+ error?: unknown;
32
+ }
33
+ interface EventSubscription {
34
+ destroy: () => void;
35
+ }
36
+ type SoundResourceMap<K extends string | number> = Record<K, string>;
37
+
38
+ declare class AudioManager<K extends string | number = string> {
39
+ private effectCache;
40
+ private urlAudioCache;
41
+ private preloadPromises;
42
+ private activeUrlHowl;
43
+ private globalVolume;
44
+ private listeners;
45
+ private HowlConstructor;
46
+ private HowlerInstance;
47
+ constructor(options?: AudioManagerOptions, inject?: {
48
+ HowlClass?: typeof Howl;
49
+ HowlerObj?: typeof Howler;
50
+ });
51
+ hasEffect(key: K): boolean;
52
+ loadEffect(key: K, url: string): void;
53
+ loadEffects(map: SoundResourceMap<K>): void;
54
+ playEffect(key: K, options?: PlayOptions): number | undefined;
55
+ stopEffect(key: K): void;
56
+ removeEffect(key: K): void;
57
+ preloadUrl(url: string): Promise<void>;
58
+ preloadUrls(urls: string[]): Promise<void[]>;
59
+ private createUrlHowl;
60
+ playUrl(url: string, options?: PlayUrlOptions): number;
61
+ stopUrl(url: string): void;
62
+ on(event: AudioEventType, handler: (payload: AudioEventPayload<K>) => void): EventSubscription;
63
+ protected emit(event: AudioEventType, payload: AudioEventPayload<K>): void;
64
+ setGlobalVolume(volume: number): void;
65
+ mute(muted: boolean): void;
66
+ stopAll(): void;
67
+ destroy(): void;
68
+ }
69
+
70
+ export { type AudioEventPayload, type AudioEventType, AudioManager, type AudioManagerOptions, type EventSubscription, type PlayOptions, type PlayUrlOptions, type SoundResourceMap, AudioManager as default };
@@ -0,0 +1,72 @@
1
+ import { Howl, Howler } from 'howler';
2
+
3
+ interface AudioManagerOptions {
4
+ /** Default global volume (0.0 to 1.0), defaults to 1.0 */
5
+ defaultVolume?: number;
6
+ }
7
+ interface PlayOptions {
8
+ /** Play volume override (0.0 to 1.0) */
9
+ volume?: number;
10
+ /** Callback triggered when playback starts */
11
+ onStart?: (soundId: number) => void;
12
+ /** Callback triggered when playback ends */
13
+ onEnd?: (soundId: number) => void;
14
+ /** Callback triggered when playback encounters an error */
15
+ onError?: (soundId: number, error: unknown) => void;
16
+ /** Callback triggered when playback is manually stopped */
17
+ onStop?: (soundId: number) => void;
18
+ }
19
+ 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
+ type AudioEventType = 'play' | 'end' | 'stop' | 'pause' | 'error' | 'load';
26
+ interface AudioEventPayload<K = string> {
27
+ type: 'effect' | 'url';
28
+ key?: K;
29
+ url?: string;
30
+ soundId?: number;
31
+ error?: unknown;
32
+ }
33
+ interface EventSubscription {
34
+ destroy: () => void;
35
+ }
36
+ type SoundResourceMap<K extends string | number> = Record<K, string>;
37
+
38
+ declare class AudioManager<K extends string | number = string> {
39
+ private effectCache;
40
+ private urlAudioCache;
41
+ private preloadPromises;
42
+ private activeUrlHowl;
43
+ private globalVolume;
44
+ private listeners;
45
+ private HowlConstructor;
46
+ private HowlerInstance;
47
+ constructor(options?: AudioManagerOptions, inject?: {
48
+ HowlClass?: typeof Howl;
49
+ HowlerObj?: typeof Howler;
50
+ });
51
+ hasEffect(key: K): boolean;
52
+ loadEffect(key: K, url: string): void;
53
+ loadEffects(map: SoundResourceMap<K>): void;
54
+ playEffect(key: K, options?: PlayOptions): number | undefined;
55
+ stopEffect(key: K): void;
56
+ removeEffect(key: K): void;
57
+ preloadUrl(url: string): Promise<void>;
58
+ preloadUrls(urls: string[]): Promise<void[]>;
59
+ private createUrlHowl;
60
+ playUrl(url: string, options?: PlayUrlOptions): number;
61
+ stopUrl(url: string): void;
62
+ on(event: AudioEventType, handler: (payload: AudioEventPayload<K>) => void): EventSubscription;
63
+ protected emit(event: AudioEventType, payload: AudioEventPayload<K>): void;
64
+ setGlobalVolume(volume: number): void;
65
+ mute(muted: boolean): void;
66
+ stopAll(): void;
67
+ destroy(): void;
68
+ }
69
+
70
+ // @ts-ignore
71
+ export = AudioManager;
72
+ export { type AudioEventPayload, type AudioEventType, AudioManager, type AudioManagerOptions, type EventSubscription, type PlayOptions, type PlayUrlOptions, type SoundResourceMap };
@@ -0,0 +1,403 @@
1
+ function _class_call_check(instance, Constructor) {
2
+ if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function");
3
+ }
4
+ function _defineProperties(target, props) {
5
+ for(var i = 0; i < props.length; i++){
6
+ var descriptor = props[i];
7
+ descriptor.enumerable = descriptor.enumerable || false;
8
+ descriptor.configurable = true;
9
+ if ("value" in descriptor) descriptor.writable = true;
10
+ Object.defineProperty(target, descriptor.key, descriptor);
11
+ }
12
+ }
13
+ function _create_class(Constructor, protoProps, staticProps) {
14
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
15
+ if (staticProps) _defineProperties(Constructor, staticProps);
16
+ return Constructor;
17
+ }
18
+ /*!
19
+ * name: @jswork/howler-audio-manager
20
+ * description: Howler.js audio manager, enum-driven sounds for H5.
21
+ * homepage: https://js.work
22
+ * version: 1.0.1
23
+ * date: 2026-10-20 18:54:06
24
+ * license: MIT
25
+ */ // src/manager.ts
26
+ import { Howl, Howler } from "howler";
27
+ var AudioManager = /*#__PURE__*/ function() {
28
+ "use strict";
29
+ function AudioManager(options, inject) {
30
+ _class_call_check(this, AudioManager);
31
+ var _ref, _ref1;
32
+ this.effectCache = /* @__PURE__ */ new Map();
33
+ this.urlAudioCache = /* @__PURE__ */ new Map();
34
+ this.preloadPromises = /* @__PURE__ */ new Map();
35
+ this.activeUrlHowl = null;
36
+ this.globalVolume = 1;
37
+ this.listeners = /* @__PURE__ */ new Map();
38
+ this.HowlConstructor = (_ref = inject === null || inject === void 0 ? void 0 : inject.HowlClass) !== null && _ref !== void 0 ? _ref : Howl;
39
+ this.HowlerInstance = (_ref1 = inject === null || inject === void 0 ? void 0 : inject.HowlerObj) !== null && _ref1 !== void 0 ? _ref1 : Howler;
40
+ if (typeof (options === null || options === void 0 ? void 0 : options.defaultVolume) === "number") {
41
+ this.setGlobalVolume(options.defaultVolume);
42
+ }
43
+ }
44
+ _create_class(AudioManager, [
45
+ {
46
+ key: "hasEffect",
47
+ value: function hasEffect(key) {
48
+ return this.effectCache.has(key);
49
+ }
50
+ },
51
+ {
52
+ key: "loadEffect",
53
+ value: function loadEffect(key, url) {
54
+ var _this = this;
55
+ if (this.effectCache.has(key)) {
56
+ this.effectCache.get(key).unload();
57
+ }
58
+ var howl = new this.HowlConstructor({
59
+ src: [
60
+ url
61
+ ],
62
+ preload: true
63
+ });
64
+ howl.on("load", function() {
65
+ _this.emit("load", {
66
+ type: "effect",
67
+ key: key,
68
+ url: url
69
+ });
70
+ });
71
+ howl.on("loaderror", function(_id, error) {
72
+ _this.emit("error", {
73
+ type: "effect",
74
+ key: key,
75
+ url: url,
76
+ error: error
77
+ });
78
+ });
79
+ this.effectCache.set(key, howl);
80
+ }
81
+ },
82
+ {
83
+ key: "loadEffects",
84
+ value: function loadEffects(map) {
85
+ var _this = this;
86
+ Object.keys(map).forEach(function(key) {
87
+ var typedKey = isNaN(Number(key)) ? key : Number(key);
88
+ _this.loadEffect(typedKey, map[key]);
89
+ });
90
+ }
91
+ },
92
+ {
93
+ key: "playEffect",
94
+ value: function playEffect(key, options) {
95
+ var _this = this;
96
+ var howl = this.effectCache.get(key);
97
+ if (!howl) {
98
+ console.warn("[AudioManager] Effect not found for key: ".concat(String(key), "."));
99
+ return void 0;
100
+ }
101
+ if (howl.playing()) {
102
+ howl.stop();
103
+ }
104
+ if (typeof (options === null || options === void 0 ? void 0 : options.volume) === "number") {
105
+ howl.volume(options.volume);
106
+ }
107
+ howl.off("play");
108
+ howl.off("end");
109
+ howl.off("stop");
110
+ howl.off("playerror");
111
+ howl.once("play", function(soundId) {
112
+ var _options_onStart;
113
+ options === null || options === void 0 ? void 0 : (_options_onStart = options.onStart) === null || _options_onStart === void 0 ? void 0 : _options_onStart.call(options, soundId);
114
+ _this.emit("play", {
115
+ type: "effect",
116
+ key: key,
117
+ soundId: soundId
118
+ });
119
+ });
120
+ howl.once("end", function(soundId) {
121
+ var _options_onEnd;
122
+ if (typeof (options === null || options === void 0 ? void 0 : options.volume) === "number") {
123
+ howl.volume(1);
124
+ }
125
+ options === null || options === void 0 ? void 0 : (_options_onEnd = options.onEnd) === null || _options_onEnd === void 0 ? void 0 : _options_onEnd.call(options, soundId);
126
+ _this.emit("end", {
127
+ type: "effect",
128
+ key: key,
129
+ soundId: soundId
130
+ });
131
+ });
132
+ howl.once("stop", function(soundId) {
133
+ var _options_onStop;
134
+ if (typeof (options === null || options === void 0 ? void 0 : options.volume) === "number") {
135
+ howl.volume(1);
136
+ }
137
+ options === null || options === void 0 ? void 0 : (_options_onStop = options.onStop) === null || _options_onStop === void 0 ? void 0 : _options_onStop.call(options, soundId);
138
+ _this.emit("stop", {
139
+ type: "effect",
140
+ key: key,
141
+ soundId: soundId
142
+ });
143
+ });
144
+ howl.once("playerror", function(soundId, error) {
145
+ var _options_onError;
146
+ options === null || options === void 0 ? void 0 : (_options_onError = options.onError) === null || _options_onError === void 0 ? void 0 : _options_onError.call(options, soundId, error);
147
+ _this.emit("error", {
148
+ type: "effect",
149
+ key: key,
150
+ soundId: soundId,
151
+ error: error
152
+ });
153
+ });
154
+ return howl.play();
155
+ }
156
+ },
157
+ {
158
+ key: "stopEffect",
159
+ value: function stopEffect(key) {
160
+ var howl = this.effectCache.get(key);
161
+ if (howl) {
162
+ howl.stop();
163
+ }
164
+ }
165
+ },
166
+ {
167
+ key: "removeEffect",
168
+ value: function removeEffect(key) {
169
+ var howl = this.effectCache.get(key);
170
+ if (howl) {
171
+ howl.unload();
172
+ this.effectCache.delete(key);
173
+ }
174
+ }
175
+ },
176
+ {
177
+ key: "preloadUrl",
178
+ value: function preloadUrl(url) {
179
+ var _this = this;
180
+ var existing = this.urlAudioCache.get(url);
181
+ if (existing && existing.state() === "loaded") {
182
+ return Promise.resolve();
183
+ }
184
+ var pending = this.preloadPromises.get(url);
185
+ if (pending) {
186
+ return pending;
187
+ }
188
+ var howl = existing && existing.state() !== "unloaded" ? existing : this.createUrlHowl(url);
189
+ var promise = new Promise(function(resolve, reject) {
190
+ howl.once("load", function() {
191
+ resolve();
192
+ });
193
+ howl.once("loaderror", function(_id, error) {
194
+ _this.urlAudioCache.delete(url);
195
+ reject(error);
196
+ });
197
+ });
198
+ this.preloadPromises.set(url, promise);
199
+ var settle = function settle() {
200
+ if (_this.preloadPromises.get(url) === promise) {
201
+ _this.preloadPromises.delete(url);
202
+ }
203
+ };
204
+ promise.then(settle, settle);
205
+ return promise;
206
+ }
207
+ },
208
+ {
209
+ key: "preloadUrls",
210
+ value: function preloadUrls(urls) {
211
+ var _this = this;
212
+ return Promise.all(urls.map(function(url) {
213
+ return _this.preloadUrl(url);
214
+ }));
215
+ }
216
+ },
217
+ {
218
+ key: "createUrlHowl",
219
+ value: function createUrlHowl(url) {
220
+ var _this = this;
221
+ var howl = new this.HowlConstructor({
222
+ src: [
223
+ url
224
+ ],
225
+ preload: true
226
+ });
227
+ howl.on("load", function() {
228
+ _this.emit("load", {
229
+ type: "url",
230
+ url: url
231
+ });
232
+ });
233
+ howl.on("loaderror", function(_id, error) {
234
+ _this.urlAudioCache.delete(url);
235
+ _this.emit("error", {
236
+ type: "url",
237
+ url: url,
238
+ error: error
239
+ });
240
+ });
241
+ this.urlAudioCache.set(url, howl);
242
+ return howl;
243
+ }
244
+ },
245
+ {
246
+ key: "playUrl",
247
+ value: function playUrl(url, options) {
248
+ var _this = this;
249
+ var howl = this.urlAudioCache.get(url) || this.createUrlHowl(url);
250
+ var isSingleton = (options === null || options === void 0 ? void 0 : options.singleton) !== false;
251
+ if (isSingleton && this.activeUrlHowl && this.activeUrlHowl !== howl && this.activeUrlHowl.playing()) {
252
+ this.activeUrlHowl.stop();
253
+ }
254
+ if (howl.playing()) {
255
+ howl.stop();
256
+ }
257
+ if ((options === null || options === void 0 ? void 0 : options.loop) !== void 0) {
258
+ howl.loop(options.loop);
259
+ }
260
+ if (typeof (options === null || options === void 0 ? void 0 : options.volume) === "number") {
261
+ howl.volume(options.volume);
262
+ }
263
+ if (isSingleton) {
264
+ this.activeUrlHowl = howl;
265
+ }
266
+ howl.off("play");
267
+ howl.off("end");
268
+ howl.off("stop");
269
+ howl.off("playerror");
270
+ howl.once("play", function(soundId) {
271
+ var _options_onStart;
272
+ options === null || options === void 0 ? void 0 : (_options_onStart = options.onStart) === null || _options_onStart === void 0 ? void 0 : _options_onStart.call(options, soundId);
273
+ _this.emit("play", {
274
+ type: "url",
275
+ url: url,
276
+ soundId: soundId
277
+ });
278
+ });
279
+ howl.once("end", function(soundId) {
280
+ var _options_onEnd;
281
+ if (typeof (options === null || options === void 0 ? void 0 : options.volume) === "number") {
282
+ howl.volume(1);
283
+ }
284
+ options === null || options === void 0 ? void 0 : (_options_onEnd = options.onEnd) === null || _options_onEnd === void 0 ? void 0 : _options_onEnd.call(options, soundId);
285
+ _this.emit("end", {
286
+ type: "url",
287
+ url: url,
288
+ soundId: soundId
289
+ });
290
+ if (_this.activeUrlHowl === howl && !howl.loop()) {
291
+ _this.activeUrlHowl = null;
292
+ }
293
+ });
294
+ howl.once("stop", function(soundId) {
295
+ var _options_onStop;
296
+ if (typeof (options === null || options === void 0 ? void 0 : options.volume) === "number") {
297
+ howl.volume(1);
298
+ }
299
+ options === null || options === void 0 ? void 0 : (_options_onStop = options.onStop) === null || _options_onStop === void 0 ? void 0 : _options_onStop.call(options, soundId);
300
+ _this.emit("stop", {
301
+ type: "url",
302
+ url: url,
303
+ soundId: soundId
304
+ });
305
+ if (_this.activeUrlHowl === howl) {
306
+ _this.activeUrlHowl = null;
307
+ }
308
+ });
309
+ howl.once("playerror", function(soundId, error) {
310
+ var _options_onError;
311
+ options === null || options === void 0 ? void 0 : (_options_onError = options.onError) === null || _options_onError === void 0 ? void 0 : _options_onError.call(options, soundId, error);
312
+ _this.emit("error", {
313
+ type: "url",
314
+ url: url,
315
+ soundId: soundId,
316
+ error: error
317
+ });
318
+ if (_this.activeUrlHowl === howl) {
319
+ _this.activeUrlHowl = null;
320
+ }
321
+ });
322
+ return howl.play();
323
+ }
324
+ },
325
+ {
326
+ key: "stopUrl",
327
+ value: function stopUrl(url) {
328
+ var howl = this.urlAudioCache.get(url);
329
+ if (howl) {
330
+ howl.stop();
331
+ }
332
+ }
333
+ },
334
+ {
335
+ key: "on",
336
+ value: function on(event, handler) {
337
+ var _this_listeners_get;
338
+ var set = (_this_listeners_get = this.listeners.get(event)) !== null && _this_listeners_get !== void 0 ? _this_listeners_get : /* @__PURE__ */ new Set();
339
+ this.listeners.set(event, set);
340
+ set.add(handler);
341
+ return {
342
+ destroy: function destroy() {
343
+ set.delete(handler);
344
+ }
345
+ };
346
+ }
347
+ },
348
+ {
349
+ key: "emit",
350
+ value: function emit(event, payload) {
351
+ var set = this.listeners.get(event);
352
+ if (set) {
353
+ set.forEach(function(handler) {
354
+ try {
355
+ handler(payload);
356
+ } catch (err) {
357
+ console.error("[AudioManager] Error in event listener for ".concat(event, ":"), err);
358
+ }
359
+ });
360
+ }
361
+ }
362
+ },
363
+ {
364
+ key: "setGlobalVolume",
365
+ value: function setGlobalVolume(volume) {
366
+ this.globalVolume = volume;
367
+ this.HowlerInstance.volume(volume);
368
+ }
369
+ },
370
+ {
371
+ key: "mute",
372
+ value: function mute(muted) {
373
+ this.HowlerInstance.mute(muted);
374
+ }
375
+ },
376
+ {
377
+ key: "stopAll",
378
+ value: function stopAll() {
379
+ this.HowlerInstance.stop();
380
+ this.activeUrlHowl = null;
381
+ }
382
+ },
383
+ {
384
+ key: "destroy",
385
+ value: function destroy() {
386
+ this.effectCache.forEach(function(howl) {
387
+ return howl.unload();
388
+ });
389
+ this.urlAudioCache.forEach(function(howl) {
390
+ return howl.unload();
391
+ });
392
+ this.effectCache.clear();
393
+ this.urlAudioCache.clear();
394
+ this.preloadPromises.clear();
395
+ this.listeners.clear();
396
+ this.activeUrlHowl = null;
397
+ }
398
+ }
399
+ ]);
400
+ return AudioManager;
401
+ }();
402
+ export { AudioManager, AudioManager as default };
403
+ //# sourceMappingURL=index.esm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["/Users/afei/github/howler-audio-manager/dist/index.esm.js","../src/manager.ts"],"names":["Howl","Howler","AudioManager","options","inject","effectCache","Map","urlAudioCache","preloadPromises","activeUrlHowl","globalVolume","listeners","HowlConstructor","HowlClass","HowlerInstance","HowlerObj","defaultVolume","setGlobalVolume","hasEffect","key","has","loadEffect","url","get","unload","howl","src","preload","on","emit","type","_id","error","set","loadEffects","map","Object","keys","forEach","typedKey","isNaN","Number","playEffect","console","warn","String","playing","stop","volume","off","once","soundId","onStart","onEnd","onStop","onError","play","stopEffect","removeEffect","delete","preloadUrl","existing","state","Promise","resolve","pending","createUrlHowl","promise","reject","settle","then","preloadUrls","urls","all","playUrl","isSingleton","singleton","loop","stopUrl","event","handler","Set","add","destroy","payload","err","mute","muted","stopAll","clear","default"],"mappings":";;;;;;;;;;;;;;;;;AAAA;;;;;;;CAOC,GAGD,iBAAiB;ACVjB,SAASA,IAAA,EAAMC,MAAA,QAAc,SAAA;AAWtB,IAAMC,6BAAN;;aAAMA,aAYTC,OAAA,EACAC,MAAA;gCAbSF;;QACX,IAAA,CAAQG,WAAA,GAAc,aAAA,GAAA,IAAIC;QAC1B,IAAA,CAAQC,aAAA,GAAgB,aAAA,GAAA,IAAID;QAC5B,IAAA,CAAQE,eAAA,GAAkB,aAAA,GAAA,IAAIF;QAC9B,IAAA,CAAQG,aAAA,GAA6B;QACrC,IAAA,CAAQC,YAAA,GAAe;QACvB,IAAA,CAAQC,SAAA,GAAY,aAAA,GAAA,IAAIL;QAStB,IAAA,CAAKM,eAAA,WAAkBR,mBAAAA,6BAAAA,OAAQS,SAAA,uCAAab;QAC5C,IAAA,CAAKc,cAAA,YAAiBV,mBAAAA,6BAAAA,OAAQW,SAAA,yCAAad;QAC3C,IAAI,QAAOE,oBAAAA,8BAAAA,QAASa,aAAA,MAAkB,UAAU;YAC9C,IAAA,CAAKC,eAAA,CAAgBd,QAAQa,aAAa;QAC5C;;;;YAGFE,KAAAA;mBAAAA,SAAAA,UAAUC,GAAA;gBACR,OAAO,IAAA,CAAKd,WAAA,CAAYe,GAAA,CAAID;YAC9B;;;YAEAE,KAAAA;mBAAAA,SAAAA,WAAWF,GAAA,EAAQG,GAAA;;gBACjB,IAAI,IAAA,CAAKjB,WAAA,CAAYe,GAAA,CAAID,MAAM;oBAC7B,IAAA,CAAKd,WAAA,CAAYkB,GAAA,CAAIJ,KAAMK,MAAA;gBAC7B;gBACA,IAAMC,OAAO,IAAI,IAAA,CAAKb,eAAA,CAAgB;oBACpCc,KAAK;wBAACJ;qBAAG;oBACTK,SAAS;gBACX;gBACAF,KAAKG,EAAA,CAAG,QAAQ;oBACd,MA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,gBAAgB+B,MAAA;gBACd,IAAA,CAAKtC,YAAA,GAAesC;gBACpB,IAAA,CAAKlC,cAAA,CAAekC,MAAA,CAAOA;YAC7B;;;YAEAsC,KAAAA;mBAAAA,SAAAA,KAAKC,KAAA;gBACH,IAAA,CAAKzE,cAAA,CAAewE,IAAA,CAAKC;YAC3B;;;YAEAC,KAAAA;mBAAAA,SAAAA;gBACE,IAAA,CAAK1E,cAAA,CAAeiC,IAAA;gBACpB,IAAA,CAAKtC,aAAA,GAAgB;YACvB;;;YAEA0E,KAAAA;mBAAAA,SAAAA;gBACE,IAAA,CAAK9E,WAAA,CAAYiC,OAAA,CAAQ,SAACb;2BAASA,KAAKD,MAAA;;gBACxC,IAAA,CAAKjB,aAAA,CAAc+B,OAAA,CAAQ,SAACb;2BAASA,KAAKD,MAAA;;gBAC1C,IAAA,CAAKnB,WAAA,CAAYoF,KAAA;gBACjB,IAAA,CAAKlF,aAAA,CAAckF,KAAA;gBACnB,IAAA,CAAKjF,eAAA,CAAgBiF,KAAA;gBACrB,IAAA,CAAK9E,SAAA,CAAU8E,KAAA;gBACf,IAAA,CAAKhF,aAAA,GAAgB;YACvB;;;;;ADzDF,SACEP,YAAY,EACZA,gBAAgBwF,OAAO,GACvB","sourcesContent":["/*!\n * name: @jswork/howler-audio-manager\n * description: Howler.js audio manager, enum-driven sounds for H5.\n * homepage: https://js.work\n * version: 1.0.1\n * date: 2026-10-20 18:54:06\n * license: MIT\n */\n\n\n// src/manager.ts\nimport { Howl, Howler } from \"howler\";\nvar AudioManager = class {\n constructor(options, inject) {\n this.effectCache = /* @__PURE__ */ new Map();\n this.urlAudioCache = /* @__PURE__ */ new Map();\n this.preloadPromises = /* @__PURE__ */ new Map();\n this.activeUrlHowl = null;\n this.globalVolume = 1;\n this.listeners = /* @__PURE__ */ new Map();\n this.HowlConstructor = inject?.HowlClass ?? Howl;\n this.HowlerInstance = inject?.HowlerObj ?? Howler;\n if (typeof options?.defaultVolume === \"number\") {\n this.setGlobalVolume(options.defaultVolume);\n }\n }\n hasEffect(key) {\n return this.effectCache.has(key);\n }\n loadEffect(key, url) {\n if (this.effectCache.has(key)) {\n this.effectCache.get(key).unload();\n }\n const howl = new this.HowlConstructor({\n src: [url],\n preload: true\n });\n howl.on(\"load\", () => {\n this.emit(\"load\", { type: \"effect\", key, url });\n });\n howl.on(\"loaderror\", (_id, error) => {\n this.emit(\"error\", { type: \"effect\", key, url, error });\n });\n this.effectCache.set(key, howl);\n }\n loadEffects(map) {\n Object.keys(map).forEach((key) => {\n const typedKey = isNaN(Number(key)) ? key : Number(key);\n this.loadEffect(typedKey, map[key]);\n });\n }\n playEffect(key, options) {\n const howl = this.effectCache.get(key);\n if (!howl) {\n console.warn(`[AudioManager] Effect not found for key: ${String(key)}.`);\n return void 0;\n }\n if (howl.playing()) {\n howl.stop();\n }\n if (typeof options?.volume === \"number\") {\n howl.volume(options.volume);\n }\n howl.off(\"play\");\n howl.off(\"end\");\n howl.off(\"stop\");\n howl.off(\"playerror\");\n howl.once(\"play\", (soundId) => {\n options?.onStart?.(soundId);\n this.emit(\"play\", { type: \"effect\", key, soundId });\n });\n howl.once(\"end\", (soundId) => {\n if (typeof options?.volume === \"number\") {\n howl.volume(1);\n }\n options?.onEnd?.(soundId);\n this.emit(\"end\", { type: \"effect\", key, soundId });\n });\n howl.once(\"stop\", (soundId) => {\n if (typeof options?.volume === \"number\") {\n howl.volume(1);\n }\n options?.onStop?.(soundId);\n this.emit(\"stop\", { type: \"effect\", key, soundId });\n });\n howl.once(\"playerror\", (soundId, error) => {\n options?.onError?.(soundId, error);\n this.emit(\"error\", { type: \"effect\", key, soundId, error });\n });\n return howl.play();\n }\n stopEffect(key) {\n const howl = this.effectCache.get(key);\n if (howl) {\n howl.stop();\n }\n }\n removeEffect(key) {\n const howl = this.effectCache.get(key);\n if (howl) {\n howl.unload();\n this.effectCache.delete(key);\n }\n }\n preloadUrl(url) {\n const existing = this.urlAudioCache.get(url);\n if (existing && existing.state() === \"loaded\") {\n return Promise.resolve();\n }\n const pending = this.preloadPromises.get(url);\n if (pending) {\n return pending;\n }\n const howl = existing && existing.state() !== \"unloaded\" ? existing : this.createUrlHowl(url);\n const promise = new Promise((resolve, reject) => {\n howl.once(\"load\", () => {\n resolve();\n });\n howl.once(\"loaderror\", (_id, error) => {\n this.urlAudioCache.delete(url);\n reject(error);\n });\n });\n this.preloadPromises.set(url, promise);\n const settle = () => {\n if (this.preloadPromises.get(url) === promise) {\n this.preloadPromises.delete(url);\n }\n };\n promise.then(settle, settle);\n return promise;\n }\n preloadUrls(urls) {\n return Promise.all(urls.map((url) => this.preloadUrl(url)));\n }\n createUrlHowl(url) {\n const howl = new this.HowlConstructor({\n src: [url],\n preload: true\n });\n howl.on(\"load\", () => {\n this.emit(\"load\", { type: \"url\", url });\n });\n howl.on(\"loaderror\", (_id, error) => {\n this.urlAudioCache.delete(url);\n this.emit(\"error\", { type: \"url\", url, error });\n });\n this.urlAudioCache.set(url, howl);\n return howl;\n }\n playUrl(url, options) {\n const howl = this.urlAudioCache.get(url) || this.createUrlHowl(url);\n const isSingleton = options?.singleton !== false;\n if (isSingleton && this.activeUrlHowl && this.activeUrlHowl !== howl && this.activeUrlHowl.playing()) {\n this.activeUrlHowl.stop();\n }\n if (howl.playing()) {\n howl.stop();\n }\n if (options?.loop !== void 0) {\n howl.loop(options.loop);\n }\n if (typeof options?.volume === \"number\") {\n howl.volume(options.volume);\n }\n if (isSingleton) {\n this.activeUrlHowl = howl;\n }\n howl.off(\"play\");\n howl.off(\"end\");\n howl.off(\"stop\");\n howl.off(\"playerror\");\n howl.once(\"play\", (soundId) => {\n options?.onStart?.(soundId);\n this.emit(\"play\", { type: \"url\", url, soundId });\n });\n howl.once(\"end\", (soundId) => {\n if (typeof options?.volume === \"number\") {\n howl.volume(1);\n }\n options?.onEnd?.(soundId);\n this.emit(\"end\", { type: \"url\", url, soundId });\n if (this.activeUrlHowl === howl && !howl.loop()) {\n this.activeUrlHowl = null;\n }\n });\n howl.once(\"stop\", (soundId) => {\n if (typeof options?.volume === \"number\") {\n howl.volume(1);\n }\n options?.onStop?.(soundId);\n this.emit(\"stop\", { type: \"url\", url, soundId });\n if (this.activeUrlHowl === howl) {\n this.activeUrlHowl = null;\n }\n });\n howl.once(\"playerror\", (soundId, error) => {\n options?.onError?.(soundId, error);\n this.emit(\"error\", { type: \"url\", url, soundId, error });\n if (this.activeUrlHowl === howl) {\n this.activeUrlHowl = null;\n }\n });\n return howl.play();\n }\n stopUrl(url) {\n const howl = this.urlAudioCache.get(url);\n if (howl) {\n howl.stop();\n }\n }\n on(event, handler) {\n const set = this.listeners.get(event) ?? /* @__PURE__ */ new Set();\n this.listeners.set(event, set);\n set.add(handler);\n return {\n destroy: () => {\n set.delete(handler);\n }\n };\n }\n emit(event, payload) {\n const set = this.listeners.get(event);\n if (set) {\n set.forEach((handler) => {\n try {\n handler(payload);\n } catch (err) {\n console.error(`[AudioManager] Error in event listener for ${event}:`, err);\n }\n });\n }\n }\n setGlobalVolume(volume) {\n this.globalVolume = volume;\n this.HowlerInstance.volume(volume);\n }\n mute(muted) {\n this.HowlerInstance.mute(muted);\n }\n stopAll() {\n this.HowlerInstance.stop();\n this.activeUrlHowl = null;\n }\n destroy() {\n this.effectCache.forEach((howl) => howl.unload());\n this.urlAudioCache.forEach((howl) => howl.unload());\n this.effectCache.clear();\n this.urlAudioCache.clear();\n this.preloadPromises.clear();\n this.listeners.clear();\n this.activeUrlHowl = null;\n }\n};\nexport {\n AudioManager,\n AudioManager as default\n};\n","import { Howl, Howler } from 'howler';\nimport type {\n AudioManagerOptions,\n PlayOptions,\n PlayUrlOptions,\n AudioEventType,\n AudioEventPayload,\n EventSubscription,\n SoundResourceMap\n} from './types';\n\nexport class AudioManager<K extends string | number = string> {\n private effectCache = new Map<K, Howl>();\n private urlAudioCache = new Map<string, Howl>();\n private preloadPromises = new Map<string, Promise<void>>();\n private activeUrlHowl: Howl | null = null;\n private globalVolume = 1.0;\n private listeners = new Map<AudioEventType, Set<(payload: AudioEventPayload<K>) => void>>();\n\n private HowlConstructor: typeof Howl;\n private HowlerInstance: typeof Howler;\n\n constructor(\n options?: AudioManagerOptions,\n inject?: { HowlClass?: typeof Howl; HowlerObj?: typeof Howler }\n ) {\n this.HowlConstructor = inject?.HowlClass ?? Howl;\n this.HowlerInstance = inject?.HowlerObj ?? Howler;\n if (typeof options?.defaultVolume === 'number') {\n this.setGlobalVolume(options.defaultVolume);\n }\n }\n\n hasEffect(key: K): boolean {\n return this.effectCache.has(key);\n }\n\n loadEffect(key: K, url: string): void {\n if (this.effectCache.has(key)) {\n this.effectCache.get(key)!.unload();\n }\n const howl = new this.HowlConstructor({\n src: [url],\n preload: true\n });\n howl.on('load', () => {\n this.emit('load', { type: 'effect', key, url });\n });\n howl.on('loaderror', (_id: number, error: unknown) => {\n this.emit('error', { type: 'effect', key, url, error });\n });\n this.effectCache.set(key, howl);\n }\n\n loadEffects(map: SoundResourceMap<K>): void {\n Object.keys(map).forEach((key) => {\n const typedKey = (isNaN(Number(key)) ? key : Number(key)) as K;\n this.loadEffect(typedKey, map[key as any]);\n });\n }\n\n playEffect(key: K, options?: PlayOptions): number | undefined {\n const howl = this.effectCache.get(key);\n if (!howl) {\n console.warn(`[AudioManager] Effect not found for key: ${String(key)}.`);\n return undefined;\n }\n\n if (howl.playing()) {\n howl.stop();\n }\n\n if (typeof options?.volume === 'number') {\n howl.volume(options.volume);\n }\n\n // Clear handlers from previous plays to avoid stale/duplicate callbacks.\n howl.off('play');\n howl.off('end');\n howl.off('stop');\n howl.off('playerror');\n\n howl.once('play', (soundId: number) => {\n options?.onStart?.(soundId);\n this.emit('play', { type: 'effect', key, soundId });\n });\n\n howl.once('end', (soundId: number) => {\n if (typeof options?.volume === 'number') {\n howl.volume(1);\n }\n options?.onEnd?.(soundId);\n this.emit('end', { type: 'effect', key, soundId });\n });\n\n howl.once('stop', (soundId: number) => {\n if (typeof options?.volume === 'number') {\n howl.volume(1);\n }\n options?.onStop?.(soundId);\n this.emit('stop', { type: 'effect', key, soundId });\n });\n\n howl.once('playerror', (soundId: number, error: unknown) => {\n options?.onError?.(soundId, error);\n this.emit('error', { type: 'effect', key, soundId, error });\n });\n\n return howl.play();\n }\n\n stopEffect(key: K): void {\n const howl = this.effectCache.get(key);\n if (howl) {\n howl.stop();\n }\n }\n\n removeEffect(key: K): void {\n const howl = this.effectCache.get(key);\n if (howl) {\n howl.unload();\n this.effectCache.delete(key);\n }\n }\n\n preloadUrl(url: string): Promise<void> {\n const existing = this.urlAudioCache.get(url);\n if (existing && existing.state() === 'loaded') {\n return Promise.resolve();\n }\n\n const pending = this.preloadPromises.get(url);\n if (pending) {\n return pending;\n }\n\n // A howl in a terminal failed state (unloaded after error) must be replaced\n // so a fresh instance can retry the load.\n const howl = existing && existing.state() !== 'unloaded' ? existing : this.createUrlHowl(url);\n\n const promise = new Promise<void>((resolve, reject) => {\n howl.once('load', () => {\n resolve();\n });\n\n howl.once('loaderror', (_id: number, error: unknown) => {\n this.urlAudioCache.delete(url);\n reject(error);\n });\n });\n\n this.preloadPromises.set(url, promise);\n const settle = () => {\n if (this.preloadPromises.get(url) === promise) {\n this.preloadPromises.delete(url);\n }\n };\n promise.then(settle, settle);\n\n return promise;\n }\n\n preloadUrls(urls: string[]): Promise<void[]> {\n return Promise.all(urls.map((url) => this.preloadUrl(url)));\n }\n\n private createUrlHowl(url: string): Howl {\n const howl = new this.HowlConstructor({\n src: [url],\n preload: true\n });\n howl.on('load', () => {\n this.emit('load', { type: 'url', url });\n });\n howl.on('loaderror', (_id: number, error: unknown) => {\n this.urlAudioCache.delete(url);\n this.emit('error', { type: 'url', url, error });\n });\n this.urlAudioCache.set(url, howl);\n return howl;\n }\n\n playUrl(url: string, options?: PlayUrlOptions): number {\n const howl = this.urlAudioCache.get(url) || this.createUrlHowl(url);\n\n // Singleton mutex: stop currently active URL audio if singleton !== false\n const isSingleton = options?.singleton !== false;\n if (\n isSingleton &&\n this.activeUrlHowl &&\n this.activeUrlHowl !== howl &&\n this.activeUrlHowl.playing()\n ) {\n this.activeUrlHowl.stop();\n }\n\n // Overlap prevention on the same audio instance\n if (howl.playing()) {\n howl.stop();\n }\n\n if (options?.loop !== undefined) {\n howl.loop(options.loop);\n }\n\n if (typeof options?.volume === 'number') {\n howl.volume(options.volume);\n }\n\n if (isSingleton) {\n this.activeUrlHowl = howl;\n }\n\n // Clear handlers from previous plays to avoid stale/duplicate callbacks.\n howl.off('play');\n howl.off('end');\n howl.off('stop');\n howl.off('playerror');\n\n howl.once('play', (soundId: number) => {\n options?.onStart?.(soundId);\n this.emit('play', { type: 'url', url, soundId });\n });\n\n howl.once('end', (soundId: number) => {\n if (typeof options?.volume === 'number') {\n howl.volume(1);\n }\n options?.onEnd?.(soundId);\n this.emit('end', { type: 'url', url, soundId });\n if (this.activeUrlHowl === howl && !howl.loop()) {\n this.activeUrlHowl = null;\n }\n });\n\n howl.once('stop', (soundId: number) => {\n if (typeof options?.volume === 'number') {\n howl.volume(1);\n }\n options?.onStop?.(soundId);\n this.emit('stop', { type: 'url', url, soundId });\n if (this.activeUrlHowl === howl) {\n this.activeUrlHowl = null;\n }\n });\n\n howl.once('playerror', (soundId: number, error: unknown) => {\n options?.onError?.(soundId, error);\n this.emit('error', { type: 'url', url, soundId, error });\n if (this.activeUrlHowl === howl) {\n this.activeUrlHowl = null;\n }\n });\n\n return howl.play();\n }\n\n stopUrl(url: string): void {\n const howl = this.urlAudioCache.get(url);\n if (howl) {\n howl.stop();\n }\n }\n\n on(event: AudioEventType, handler: (payload: AudioEventPayload<K>) => void): EventSubscription {\n const set = this.listeners.get(event) ?? new Set<(payload: AudioEventPayload<K>) => void>();\n this.listeners.set(event, set);\n set.add(handler);\n return {\n destroy: () => {\n set.delete(handler);\n }\n };\n }\n\n protected emit(event: AudioEventType, payload: AudioEventPayload<K>): void {\n const set = this.listeners.get(event);\n if (set) {\n set.forEach((handler) => {\n try {\n handler(payload);\n } catch (err) {\n console.error(`[AudioManager] Error in event listener for ${event}:`, err);\n }\n });\n }\n }\n\n setGlobalVolume(volume: number): void {\n this.globalVolume = volume;\n this.HowlerInstance.volume(volume);\n }\n\n mute(muted: boolean): void {\n this.HowlerInstance.mute(muted);\n }\n\n stopAll(): void {\n this.HowlerInstance.stop();\n this.activeUrlHowl = null;\n }\n\n destroy(): void {\n this.effectCache.forEach((howl) => howl.unload());\n this.urlAudioCache.forEach((howl) => howl.unload());\n this.effectCache.clear();\n this.urlAudioCache.clear();\n this.preloadPromises.clear();\n this.listeners.clear();\n this.activeUrlHowl = null;\n }\n}\n"]}