@flare-engine/audio 0.2.1 → 0.3.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/dist/index.js CHANGED
@@ -1,307 +1,273 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/index.ts
21
- var index_exports = {};
22
- __export(index_exports, {
23
- AudioManager: () => AudioManager,
24
- Music: () => Music,
25
- SFXPool: () => SFXPool,
26
- createChannels: () => createChannels
27
- });
28
- module.exports = __toCommonJS(index_exports);
29
-
30
- // src/audio-manager.ts
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region src/audio-manager.ts
3
+ /**
4
+ * AudioManager — manages the AudioContext lifecycle.
5
+ * Wraps react-native-audio-api's AudioContext.
6
+ */
31
7
  var AudioManager = class {
32
- ctx = null;
33
- factory = null;
34
- /** Current audio state. */
35
- get state() {
36
- if (!this.ctx) return "uninitialized";
37
- return this.ctx.state === "closed" ? "closed" : this.ctx.state;
38
- }
39
- /** The underlying AudioContext, or null if not initialized. */
40
- get context() {
41
- return this.ctx;
42
- }
43
- /** Current time from the AudioContext. */
44
- get currentTime() {
45
- return this.ctx?.currentTime ?? 0;
46
- }
47
- /** Initialize with a factory that creates the native AudioContext. */
48
- init(factory) {
49
- if (this.ctx) return;
50
- this.factory = factory;
51
- this.ctx = factory();
52
- }
53
- /** Suspend audio (e.g. on app background). */
54
- async suspend() {
55
- if (this.ctx?.state === "running") {
56
- await this.ctx.suspend();
57
- }
58
- }
59
- /** Resume audio (e.g. on app foreground). */
60
- async resume() {
61
- if (this.ctx?.state === "suspended") {
62
- await this.ctx.resume();
63
- }
64
- }
65
- /** Close and dispose the AudioContext. */
66
- async close() {
67
- if (this.ctx) {
68
- await this.ctx.close();
69
- this.ctx = null;
70
- }
71
- }
8
+ ctx = null;
9
+ /** Current audio state. */
10
+ get state() {
11
+ if (!this.ctx) return "uninitialized";
12
+ return this.ctx.state === "closed" ? "closed" : this.ctx.state;
13
+ }
14
+ /** The underlying AudioContext, or null if not initialized. */
15
+ get context() {
16
+ return this.ctx;
17
+ }
18
+ /** Current time from the AudioContext. */
19
+ get currentTime() {
20
+ return this.ctx?.currentTime ?? 0;
21
+ }
22
+ /** Initialize with a factory that creates the native AudioContext. */
23
+ init(factory) {
24
+ if (this.ctx) return;
25
+ this.ctx = factory();
26
+ }
27
+ /** Suspend audio. Call from your own background handler — no automatic wiring. */
28
+ async suspend() {
29
+ if (this.ctx?.state === "running") await this.ctx.suspend();
30
+ }
31
+ /** Resume audio. Call from your own foreground handler — no automatic wiring. */
32
+ async resume() {
33
+ if (this.ctx?.state === "suspended") await this.ctx.resume();
34
+ }
35
+ /** Close and dispose the AudioContext. */
36
+ async close() {
37
+ if (this.ctx) {
38
+ await this.ctx.close();
39
+ this.ctx = null;
40
+ }
41
+ }
72
42
  };
73
-
74
- // src/channels.ts
43
+ //#endregion
44
+ //#region src/channels.ts
45
+ /**
46
+ * Creates volume channels connected: sfx → master, music → master, master → destination.
47
+ * Returns a map of channels by name.
48
+ */
75
49
  function createChannels(ctx) {
76
- const masterGain = ctx.createGain();
77
- masterGain.gain.value = 1;
78
- masterGain.connect(ctx.destination);
79
- const sfxGain = ctx.createGain();
80
- sfxGain.gain.value = 1;
81
- sfxGain.connect(masterGain);
82
- const musicGain = ctx.createGain();
83
- musicGain.gain.value = 1;
84
- musicGain.connect(masterGain);
85
- const channels = /* @__PURE__ */ new Map();
86
- channels.set("master", createChannel("master", masterGain));
87
- channels.set("sfx", createChannel("sfx", sfxGain));
88
- channels.set("music", createChannel("music", musicGain));
89
- return channels;
50
+ const masterGain = ctx.createGain();
51
+ masterGain.gain.value = 1;
52
+ masterGain.connect(ctx.destination);
53
+ const sfxGain = ctx.createGain();
54
+ sfxGain.gain.value = 1;
55
+ sfxGain.connect(masterGain);
56
+ const musicGain = ctx.createGain();
57
+ musicGain.gain.value = 1;
58
+ musicGain.connect(masterGain);
59
+ const channels = /* @__PURE__ */ new Map();
60
+ channels.set("master", createChannel("master", masterGain));
61
+ channels.set("sfx", createChannel("sfx", sfxGain));
62
+ channels.set("music", createChannel("music", musicGain));
63
+ return channels;
90
64
  }
91
65
  function createChannel(name, node) {
92
- let vol = 1;
93
- return {
94
- name,
95
- get volume() {
96
- return vol;
97
- },
98
- set volume(v) {
99
- vol = Math.max(0, Math.min(1, v));
100
- node.gain.value = vol;
101
- },
102
- node
103
- };
66
+ let vol = 1;
67
+ return {
68
+ name,
69
+ get volume() {
70
+ return vol;
71
+ },
72
+ set volume(v) {
73
+ vol = Math.max(0, Math.min(1, v));
74
+ node.gain.value = vol;
75
+ },
76
+ node
77
+ };
104
78
  }
105
-
106
- // src/sfx-pool.ts
79
+ //#endregion
80
+ //#region src/music.ts
81
+ /**
82
+ * Music player — one track at a time, with crossfade support.
83
+ * Loops by default.
84
+ */
85
+ var Music = class {
86
+ ctx = null;
87
+ output = null;
88
+ buffers = /* @__PURE__ */ new Map();
89
+ currentSource = null;
90
+ currentGain = null;
91
+ currentName = null;
92
+ /** Name of the currently playing track, or null. */
93
+ get playing() {
94
+ return this.currentName;
95
+ }
96
+ /** Connect to an AudioContext and output gain node. */
97
+ connect(ctx, output) {
98
+ this.ctx = ctx;
99
+ this.output = output;
100
+ }
101
+ /** Register a music track buffer. */
102
+ register(name, buffer) {
103
+ this.buffers.set(name, buffer);
104
+ }
105
+ /** Play a track. Stops any currently playing track. Loops by default. */
106
+ play(name, loop = true) {
107
+ if (!this.ctx || !this.output) return;
108
+ const buffer = this.buffers.get(name);
109
+ if (!buffer) return;
110
+ this.stopInternal();
111
+ const source = this.ctx.createBufferSource();
112
+ source.buffer = buffer;
113
+ source.loop = loop;
114
+ const gain = this.ctx.createGain();
115
+ gain.gain.value = 1;
116
+ source.connect(gain);
117
+ gain.connect(this.output);
118
+ source.start();
119
+ this.currentSource = source;
120
+ this.currentGain = gain;
121
+ this.currentName = name;
122
+ source.onended = () => {
123
+ if (this.currentSource === source) {
124
+ this.currentSource = null;
125
+ this.currentGain = null;
126
+ this.currentName = null;
127
+ }
128
+ gain.disconnect();
129
+ };
130
+ }
131
+ /** Stop the current track. */
132
+ stop() {
133
+ this.stopInternal();
134
+ }
135
+ /**
136
+ * Crossfade to another track over duration seconds.
137
+ * Fades out current and fades in the new track simultaneously.
138
+ */
139
+ crossfadeTo(name, duration, loop = true) {
140
+ if (!this.ctx || !this.output) return;
141
+ const buffer = this.buffers.get(name);
142
+ if (!buffer) return;
143
+ const now = this.ctx.currentTime;
144
+ if (this.currentGain && this.currentSource) {
145
+ const oldGain = this.currentGain;
146
+ const oldSource = this.currentSource;
147
+ oldGain.gain.setValueAtTime(oldGain.gain.value, now);
148
+ oldGain.gain.linearRampToValueAtTime(0, now + duration);
149
+ try {
150
+ oldSource.stop(now + duration);
151
+ } catch {}
152
+ }
153
+ const source = this.ctx.createBufferSource();
154
+ source.buffer = buffer;
155
+ source.loop = loop;
156
+ const gain = this.ctx.createGain();
157
+ gain.gain.setValueAtTime(0, now);
158
+ gain.gain.linearRampToValueAtTime(1, now + duration);
159
+ source.connect(gain);
160
+ gain.connect(this.output);
161
+ source.start();
162
+ this.currentSource = source;
163
+ this.currentGain = gain;
164
+ this.currentName = name;
165
+ source.onended = () => {
166
+ if (this.currentSource === source) {
167
+ this.currentSource = null;
168
+ this.currentGain = null;
169
+ this.currentName = null;
170
+ }
171
+ gain.disconnect();
172
+ };
173
+ }
174
+ stopInternal() {
175
+ if (this.currentSource) try {
176
+ this.currentSource.stop();
177
+ } catch {}
178
+ if (this.currentGain) this.currentGain.disconnect();
179
+ this.currentSource = null;
180
+ this.currentGain = null;
181
+ this.currentName = null;
182
+ }
183
+ };
184
+ //#endregion
185
+ //#region src/sfx-pool.ts
186
+ /**
187
+ * SFX pool — pre-decoded audio buffers with polyphony management.
188
+ * When max polyphony is reached, the oldest sound is killed.
189
+ */
107
190
  var SFXPool = class {
108
- buffers = /* @__PURE__ */ new Map();
109
- active = [];
110
- ctx = null;
111
- output = null;
112
- maxPolyphony;
113
- constructor(maxPolyphony = 16) {
114
- this.maxPolyphony = maxPolyphony;
115
- }
116
- /** Connect to an AudioContext and output GainNode. */
117
- connect(ctx, output) {
118
- this.ctx = ctx;
119
- this.output = output;
120
- }
121
- /** Register a pre-decoded audio buffer. */
122
- register(name, buffer) {
123
- this.buffers.set(name, buffer);
124
- }
125
- /** Unregister a buffer. */
126
- unregister(name) {
127
- this.buffers.delete(name);
128
- }
129
- /** Check if a buffer is registered. */
130
- has(name) {
131
- return this.buffers.has(name);
132
- }
133
- /** Number of registered buffers. */
134
- get size() {
135
- return this.buffers.size;
136
- }
137
- /** Number of currently active sounds. */
138
- get activeCount() {
139
- return this.active.length;
140
- }
141
- /** Play a registered sound. Returns a handle, or null if not registered/no context. */
142
- play(name, opts) {
143
- const buffer = this.buffers.get(name);
144
- if (!buffer || !this.ctx || !this.output) return null;
145
- while (this.active.length >= this.maxPolyphony) {
146
- const oldest = this.active.shift();
147
- oldest?.stop();
148
- }
149
- const source = this.ctx.createBufferSource();
150
- source.buffer = buffer;
151
- source.loop = opts?.loop ?? false;
152
- const gain = this.ctx.createGain();
153
- gain.gain.value = opts?.volume ?? 1;
154
- source.connect(gain);
155
- gain.connect(this.output);
156
- const entry = {
157
- name,
158
- stop() {
159
- try {
160
- source.stop();
161
- } catch {
162
- }
163
- gain.disconnect();
164
- }
165
- };
166
- this.active.push(entry);
167
- source.onended = () => {
168
- const idx = this.active.indexOf(entry);
169
- if (idx !== -1) {
170
- this.active.splice(idx, 1);
171
- }
172
- gain.disconnect();
173
- };
174
- source.start();
175
- return {
176
- stop: () => {
177
- entry.stop();
178
- const idx = this.active.indexOf(entry);
179
- if (idx !== -1) {
180
- this.active.splice(idx, 1);
181
- }
182
- }
183
- };
184
- }
185
- /** Stop all active sounds. */
186
- stopAll() {
187
- for (const entry of this.active) {
188
- entry.stop();
189
- }
190
- this.active.length = 0;
191
- }
191
+ buffers = /* @__PURE__ */ new Map();
192
+ active = [];
193
+ ctx = null;
194
+ output = null;
195
+ maxPolyphony;
196
+ constructor(maxPolyphony = 16) {
197
+ if (maxPolyphony <= 0) throw new Error("SFXPool: maxPolyphony must be > 0");
198
+ this.maxPolyphony = maxPolyphony;
199
+ }
200
+ /** Connect to an AudioContext and output GainNode. */
201
+ connect(ctx, output) {
202
+ this.ctx = ctx;
203
+ this.output = output;
204
+ }
205
+ /** Register a pre-decoded audio buffer. */
206
+ register(name, buffer) {
207
+ this.buffers.set(name, buffer);
208
+ }
209
+ /** Unregister a buffer. */
210
+ unregister(name) {
211
+ this.buffers.delete(name);
212
+ }
213
+ /** Check if a buffer is registered. */
214
+ has(name) {
215
+ return this.buffers.has(name);
216
+ }
217
+ /** Number of registered buffers. */
218
+ get size() {
219
+ return this.buffers.size;
220
+ }
221
+ /** Number of currently active sounds. */
222
+ get activeCount() {
223
+ return this.active.length;
224
+ }
225
+ /** Play a registered sound. Returns a handle, or null if not registered/no context. */
226
+ play(name, opts) {
227
+ const buffer = this.buffers.get(name);
228
+ if (!buffer || !this.ctx || !this.output) return null;
229
+ while (this.active.length >= this.maxPolyphony) this.active.shift()?.stop();
230
+ const source = this.ctx.createBufferSource();
231
+ source.buffer = buffer;
232
+ source.loop = opts?.loop ?? false;
233
+ const rateParam = source.playbackRate;
234
+ if (rateParam && opts?.rate !== void 0) rateParam.value = opts.rate;
235
+ const gain = this.ctx.createGain();
236
+ gain.gain.value = opts?.volume ?? 1;
237
+ source.connect(gain);
238
+ gain.connect(this.output);
239
+ const entry = {
240
+ name,
241
+ stop() {
242
+ try {
243
+ source.stop();
244
+ } catch {}
245
+ gain.disconnect();
246
+ }
247
+ };
248
+ this.active.push(entry);
249
+ source.onended = () => {
250
+ const idx = this.active.indexOf(entry);
251
+ if (idx !== -1) this.active.splice(idx, 1);
252
+ gain.disconnect();
253
+ };
254
+ source.start();
255
+ return { stop: () => {
256
+ entry.stop();
257
+ const idx = this.active.indexOf(entry);
258
+ if (idx !== -1) this.active.splice(idx, 1);
259
+ } };
260
+ }
261
+ /** Stop all active sounds. */
262
+ stopAll() {
263
+ for (const entry of this.active) entry.stop();
264
+ this.active.length = 0;
265
+ }
192
266
  };
267
+ //#endregion
268
+ exports.AudioManager = AudioManager;
269
+ exports.Music = Music;
270
+ exports.SFXPool = SFXPool;
271
+ exports.createChannels = createChannels;
193
272
 
194
- // src/music.ts
195
- var Music = class {
196
- ctx = null;
197
- output = null;
198
- buffers = /* @__PURE__ */ new Map();
199
- currentSource = null;
200
- currentGain = null;
201
- currentName = null;
202
- /** Name of the currently playing track, or null. */
203
- get playing() {
204
- return this.currentName;
205
- }
206
- /** Connect to an AudioContext and output gain node. */
207
- connect(ctx, output) {
208
- this.ctx = ctx;
209
- this.output = output;
210
- }
211
- /** Register a music track buffer. */
212
- register(name, buffer) {
213
- this.buffers.set(name, buffer);
214
- }
215
- /** Play a track. Stops any currently playing track. Loops by default. */
216
- play(name, loop = true) {
217
- if (!this.ctx || !this.output) return;
218
- const buffer = this.buffers.get(name);
219
- if (!buffer) return;
220
- this.stopInternal();
221
- const source = this.ctx.createBufferSource();
222
- source.buffer = buffer;
223
- source.loop = loop;
224
- const gain = this.ctx.createGain();
225
- gain.gain.value = 1;
226
- source.connect(gain);
227
- gain.connect(this.output);
228
- source.start();
229
- this.currentSource = source;
230
- this.currentGain = gain;
231
- this.currentName = name;
232
- source.onended = () => {
233
- if (this.currentSource === source) {
234
- this.currentSource = null;
235
- this.currentGain = null;
236
- this.currentName = null;
237
- }
238
- gain.disconnect();
239
- };
240
- }
241
- /** Stop the current track. */
242
- stop() {
243
- this.stopInternal();
244
- }
245
- /**
246
- * Crossfade to another track over duration seconds.
247
- * Fades out current and fades in the new track simultaneously.
248
- */
249
- crossfadeTo(name, duration, loop = true) {
250
- if (!this.ctx || !this.output) return;
251
- const buffer = this.buffers.get(name);
252
- if (!buffer) return;
253
- const now = this.ctx.currentTime;
254
- if (this.currentGain && this.currentSource) {
255
- const oldGain = this.currentGain;
256
- const oldSource = this.currentSource;
257
- oldGain.gain.setValueAtTime(oldGain.gain.value, now);
258
- oldGain.gain.linearRampToValueAtTime(0, now + duration);
259
- try {
260
- oldSource.stop(now + duration);
261
- } catch {
262
- }
263
- }
264
- const source = this.ctx.createBufferSource();
265
- source.buffer = buffer;
266
- source.loop = loop;
267
- const gain = this.ctx.createGain();
268
- gain.gain.setValueAtTime(0, now);
269
- gain.gain.linearRampToValueAtTime(1, now + duration);
270
- source.connect(gain);
271
- gain.connect(this.output);
272
- source.start();
273
- this.currentSource = source;
274
- this.currentGain = gain;
275
- this.currentName = name;
276
- source.onended = () => {
277
- if (this.currentSource === source) {
278
- this.currentSource = null;
279
- this.currentGain = null;
280
- this.currentName = null;
281
- }
282
- gain.disconnect();
283
- };
284
- }
285
- stopInternal() {
286
- if (this.currentSource) {
287
- try {
288
- this.currentSource.stop();
289
- } catch {
290
- }
291
- }
292
- if (this.currentGain) {
293
- this.currentGain.disconnect();
294
- }
295
- this.currentSource = null;
296
- this.currentGain = null;
297
- this.currentName = null;
298
- }
299
- };
300
- // Annotate the CommonJS export names for ESM import in node:
301
- 0 && (module.exports = {
302
- AudioManager,
303
- Music,
304
- SFXPool,
305
- createChannels
306
- });
307
273
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/audio-manager.ts","../src/channels.ts","../src/sfx-pool.ts","../src/music.ts"],"sourcesContent":["export {\n type AudioContextLike,\n type AudioBufferLike,\n type AudioBufferSourceNodeLike,\n type AudioDestinationNodeLike,\n type AudioParamLike,\n type GainNodeLike,\n type AudioState,\n AudioManager,\n} from './audio-manager.js';\n\nexport { type Channel, type ChannelName, createChannels } from './channels.js';\n\nexport { type PlaySFXOptions, type SFXHandle, SFXPool } from './sfx-pool.js';\n\nexport { Music } from './music.js';\n","/**\n * Minimal interfaces for the Web Audio API subset we use.\n * At runtime these are provided by react-native-audio-api.\n */\n\nexport interface AudioDestinationNodeLike {\n readonly channelCount: number;\n}\n\nexport interface GainNodeLike {\n readonly gain: AudioParamLike;\n connect(dest: AudioDestinationNodeLike | GainNodeLike): void;\n disconnect(): void;\n}\n\nexport interface AudioParamLike {\n value: number;\n setValueAtTime(value: number, time: number): void;\n linearRampToValueAtTime(value: number, time: number): void;\n}\n\nexport interface AudioBufferLike {\n readonly duration: number;\n readonly numberOfChannels: number;\n readonly sampleRate: number;\n readonly length: number;\n}\n\nexport interface AudioBufferSourceNodeLike {\n buffer: AudioBufferLike | null;\n loop: boolean;\n connect(dest: GainNodeLike | AudioDestinationNodeLike): void;\n disconnect(): void;\n start(when?: number): void;\n stop(when?: number): void;\n readonly onended: ((this: AudioBufferSourceNodeLike, ev: unknown) => void) | null;\n}\n\nexport interface AudioContextLike {\n readonly state: 'suspended' | 'running' | 'closed';\n readonly currentTime: number;\n readonly destination: AudioDestinationNodeLike;\n createGain(): GainNodeLike;\n createBufferSource(): AudioBufferSourceNodeLike;\n resume(): Promise<void>;\n suspend(): Promise<void>;\n close(): Promise<void>;\n}\n\n/** Audio context state. */\nexport type AudioState = 'uninitialized' | 'running' | 'suspended' | 'closed';\n\n/**\n * AudioManager — manages the AudioContext lifecycle.\n * Wraps react-native-audio-api's AudioContext.\n */\nexport class AudioManager {\n private ctx: AudioContextLike | null = null;\n private factory: (() => AudioContextLike) | null = null;\n\n /** Current audio state. */\n get state(): AudioState {\n if (!this.ctx) return 'uninitialized';\n return this.ctx.state === 'closed' ? 'closed' : this.ctx.state;\n }\n\n /** The underlying AudioContext, or null if not initialized. */\n get context(): AudioContextLike | null {\n return this.ctx;\n }\n\n /** Current time from the AudioContext. */\n get currentTime(): number {\n return this.ctx?.currentTime ?? 0;\n }\n\n /** Initialize with a factory that creates the native AudioContext. */\n init(factory: () => AudioContextLike): void {\n if (this.ctx) return;\n this.factory = factory;\n this.ctx = factory();\n }\n\n /** Suspend audio (e.g. on app background). */\n async suspend(): Promise<void> {\n if (this.ctx?.state === 'running') {\n await this.ctx.suspend();\n }\n }\n\n /** Resume audio (e.g. on app foreground). */\n async resume(): Promise<void> {\n if (this.ctx?.state === 'suspended') {\n await this.ctx.resume();\n }\n }\n\n /** Close and dispose the AudioContext. */\n async close(): Promise<void> {\n if (this.ctx) {\n await this.ctx.close();\n this.ctx = null;\n }\n }\n}\n","import type { AudioContextLike, GainNodeLike } from './audio-manager.js';\n\n/** A volume channel that wraps a GainNode. */\nexport interface Channel {\n readonly name: string;\n /** Current volume (0.0 - 1.0). */\n volume: number;\n /** The underlying GainNode. */\n readonly node: GainNodeLike;\n}\n\n/** Standard channel names. */\nexport type ChannelName = 'master' | 'sfx' | 'music';\n\n/**\n * Creates volume channels connected: sfx → master, music → master, master → destination.\n * Returns a map of channels by name.\n */\nexport function createChannels(ctx: AudioContextLike): Map<ChannelName, Channel> {\n const masterGain = ctx.createGain();\n masterGain.gain.value = 1;\n masterGain.connect(ctx.destination);\n\n const sfxGain = ctx.createGain();\n sfxGain.gain.value = 1;\n sfxGain.connect(masterGain);\n\n const musicGain = ctx.createGain();\n musicGain.gain.value = 1;\n musicGain.connect(masterGain);\n\n const channels = new Map<ChannelName, Channel>();\n\n channels.set('master', createChannel('master', masterGain));\n channels.set('sfx', createChannel('sfx', sfxGain));\n channels.set('music', createChannel('music', musicGain));\n\n return channels;\n}\n\nfunction createChannel(name: string, node: GainNodeLike): Channel {\n let vol = 1;\n return {\n name,\n get volume() {\n return vol;\n },\n set volume(v: number) {\n vol = Math.max(0, Math.min(1, v));\n node.gain.value = vol;\n },\n node,\n };\n}\n","import type { AudioBufferLike, AudioContextLike, GainNodeLike } from './audio-manager.js';\n\n/** Options for playing a sound effect. */\nexport interface PlaySFXOptions {\n /** Playback volume (0-1). Default: 1. */\n volume?: number;\n /** Playback rate. Default: 1. */\n rate?: number;\n /** Whether to loop. Default: false. */\n loop?: boolean;\n}\n\n/** A handle to a playing SFX instance. */\nexport interface SFXHandle {\n /** Stop playback. */\n stop(): void;\n}\n\n/** Tracks an active playing source. */\ninterface ActiveSource {\n name: string;\n stop(): void;\n}\n\n/**\n * SFX pool — pre-decoded audio buffers with polyphony management.\n * When max polyphony is reached, the oldest sound is killed.\n */\nexport class SFXPool {\n private buffers = new Map<string, AudioBufferLike>();\n private active: ActiveSource[] = [];\n private ctx: AudioContextLike | null = null;\n private output: GainNodeLike | null = null;\n private maxPolyphony: number;\n\n constructor(maxPolyphony = 16) {\n this.maxPolyphony = maxPolyphony;\n }\n\n /** Connect to an AudioContext and output GainNode. */\n connect(ctx: AudioContextLike, output: GainNodeLike): void {\n this.ctx = ctx;\n this.output = output;\n }\n\n /** Register a pre-decoded audio buffer. */\n register(name: string, buffer: AudioBufferLike): void {\n this.buffers.set(name, buffer);\n }\n\n /** Unregister a buffer. */\n unregister(name: string): void {\n this.buffers.delete(name);\n }\n\n /** Check if a buffer is registered. */\n has(name: string): boolean {\n return this.buffers.has(name);\n }\n\n /** Number of registered buffers. */\n get size(): number {\n return this.buffers.size;\n }\n\n /** Number of currently active sounds. */\n get activeCount(): number {\n return this.active.length;\n }\n\n /** Play a registered sound. Returns a handle, or null if not registered/no context. */\n play(name: string, opts?: PlaySFXOptions): SFXHandle | null {\n const buffer = this.buffers.get(name);\n if (!buffer || !this.ctx || !this.output) return null;\n\n // Kill oldest if at max polyphony\n while (this.active.length >= this.maxPolyphony) {\n const oldest = this.active.shift();\n oldest?.stop();\n }\n\n const source = this.ctx.createBufferSource();\n source.buffer = buffer;\n source.loop = opts?.loop ?? false;\n\n // Create a per-source gain for volume control\n const gain = this.ctx.createGain();\n gain.gain.value = opts?.volume ?? 1;\n source.connect(gain);\n gain.connect(this.output);\n\n const entry: ActiveSource = {\n name,\n stop() {\n try {\n source.stop();\n } catch {\n // Already stopped\n }\n gain.disconnect();\n },\n };\n\n this.active.push(entry);\n\n // Clean up when playback ends\n (source as { onended: (() => void) | null }).onended = () => {\n const idx = this.active.indexOf(entry);\n if (idx !== -1) {\n this.active.splice(idx, 1);\n }\n gain.disconnect();\n };\n\n source.start();\n\n return {\n stop: () => {\n entry.stop();\n const idx = this.active.indexOf(entry);\n if (idx !== -1) {\n this.active.splice(idx, 1);\n }\n },\n };\n }\n\n /** Stop all active sounds. */\n stopAll(): void {\n for (const entry of this.active) {\n entry.stop();\n }\n this.active.length = 0;\n }\n}\n","import type {\n AudioBufferLike,\n AudioBufferSourceNodeLike,\n AudioContextLike,\n GainNodeLike,\n} from './audio-manager.js';\n\n/**\n * Music player — one track at a time, with crossfade support.\n * Loops by default.\n */\nexport class Music {\n private ctx: AudioContextLike | null = null;\n private output: GainNodeLike | null = null;\n private buffers = new Map<string, AudioBufferLike>();\n private currentSource: AudioBufferSourceNodeLike | null = null;\n private currentGain: GainNodeLike | null = null;\n private currentName: string | null = null;\n\n /** Name of the currently playing track, or null. */\n get playing(): string | null {\n return this.currentName;\n }\n\n /** Connect to an AudioContext and output gain node. */\n connect(ctx: AudioContextLike, output: GainNodeLike): void {\n this.ctx = ctx;\n this.output = output;\n }\n\n /** Register a music track buffer. */\n register(name: string, buffer: AudioBufferLike): void {\n this.buffers.set(name, buffer);\n }\n\n /** Play a track. Stops any currently playing track. Loops by default. */\n play(name: string, loop = true): void {\n if (!this.ctx || !this.output) return;\n const buffer = this.buffers.get(name);\n if (!buffer) return;\n\n this.stopInternal();\n\n const source = this.ctx.createBufferSource();\n source.buffer = buffer;\n source.loop = loop;\n\n const gain = this.ctx.createGain();\n gain.gain.value = 1;\n source.connect(gain);\n gain.connect(this.output);\n\n source.start();\n this.currentSource = source;\n this.currentGain = gain;\n this.currentName = name;\n\n (source as { onended: (() => void) | null }).onended = () => {\n if (this.currentSource === source) {\n this.currentSource = null;\n this.currentGain = null;\n this.currentName = null;\n }\n gain.disconnect();\n };\n }\n\n /** Stop the current track. */\n stop(): void {\n this.stopInternal();\n }\n\n /**\n * Crossfade to another track over duration seconds.\n * Fades out current and fades in the new track simultaneously.\n */\n crossfadeTo(name: string, duration: number, loop = true): void {\n if (!this.ctx || !this.output) return;\n const buffer = this.buffers.get(name);\n if (!buffer) return;\n\n const now = this.ctx.currentTime;\n\n // Fade out current\n if (this.currentGain && this.currentSource) {\n const oldGain = this.currentGain;\n const oldSource = this.currentSource;\n oldGain.gain.setValueAtTime(oldGain.gain.value, now);\n oldGain.gain.linearRampToValueAtTime(0, now + duration);\n // Schedule stop after fade\n try {\n oldSource.stop(now + duration);\n } catch {\n // Already stopped\n }\n }\n\n // Start new track fading in\n const source = this.ctx.createBufferSource();\n source.buffer = buffer;\n source.loop = loop;\n\n const gain = this.ctx.createGain();\n gain.gain.setValueAtTime(0, now);\n gain.gain.linearRampToValueAtTime(1, now + duration);\n source.connect(gain);\n gain.connect(this.output);\n\n source.start();\n this.currentSource = source;\n this.currentGain = gain;\n this.currentName = name;\n\n (source as { onended: (() => void) | null }).onended = () => {\n if (this.currentSource === source) {\n this.currentSource = null;\n this.currentGain = null;\n this.currentName = null;\n }\n gain.disconnect();\n };\n }\n\n private stopInternal(): void {\n if (this.currentSource) {\n try {\n this.currentSource.stop();\n } catch {\n // Already stopped\n }\n }\n if (this.currentGain) {\n this.currentGain.disconnect();\n }\n this.currentSource = null;\n this.currentGain = null;\n this.currentName = null;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACwDO,IAAM,eA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C/B,QAAQ;AAAA,MAER;AAAA,IACF;AAGA,UAAM,SAAS,KAAK,IAAI,mBAAmB;AAC3C,WAAO,SAAS;AAChB,WAAO,OAAO;AAEd,UAAM,OAAO,KAAK,IAAI,WAAW;AACjC,SAAK,KAAK,eAAe,GAAG,GAAG;AAC/B,SAAK,KAAK,wBAAwB,GAAG,MAAM,QAAQ;AACnD,WAAO,QAAQ,IAAI;AACnB,SAAK,QAAQ,KAAK,MAAM;AAExB,WAAO,MAAM;AACb,SAAK,gBAAgB;AACrB,SAAK,cAAc;AACnB,SAAK,cAAc;AAEnB,IAAC,OAA4C,UAAU,MAAM;AAC3D,UAAI,KAAK,kBAAkB,QAAQ;AACjC,aAAK,gBAAgB;AACrB,aAAK,cAAc;AACnB,aAAK,cAAc;AAAA,MACrB;AACA,WAAK,WAAW;AAAA,IAClB;AAAA,EACF;AAAA,EAEQ,eAAqB;AAC3B,QAAI,KAAK,eAAe;AACtB,UAAI;AACF,aAAK,cAAc,KAAK;AAAA,MAC1B,QAAQ;AAAA,MAER;AAAA,IACF;AACA,QAAI,KAAK,aAAa;AACpB,WAAK,YAAY,WAAW;AAAA,IAC9B;AACA,SAAK,gBAAgB;AACrB,SAAK,cAAc;AACnB,SAAK,cAAc;AAAA,EACrB;AACF;","names":[]}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/audio-manager.ts","../src/channels.ts","../src/music.ts","../src/sfx-pool.ts"],"sourcesContent":["/**\n * Minimal interfaces for the Web Audio API subset we use.\n * At runtime these are provided by react-native-audio-api.\n */\n\nexport interface AudioDestinationNodeLike {\n readonly channelCount: number;\n}\n\nexport interface GainNodeLike {\n readonly gain: AudioParamLike;\n connect(dest: AudioDestinationNodeLike | GainNodeLike): void;\n disconnect(): void;\n}\n\nexport interface AudioParamLike {\n value: number;\n setValueAtTime(value: number, time: number): void;\n linearRampToValueAtTime(value: number, time: number): void;\n}\n\nexport interface AudioBufferLike {\n readonly duration: number;\n readonly numberOfChannels: number;\n readonly sampleRate: number;\n readonly length: number;\n}\n\nexport interface AudioBufferSourceNodeLike {\n buffer: AudioBufferLike | null;\n loop: boolean;\n /**\n * Playback-rate param (Web Audio shape). Optional — not every injected node\n * exposes it; `SFXPool.play`'s `rate` option is a no-op when this is absent.\n */\n readonly playbackRate?: { value: number };\n connect(dest: GainNodeLike | AudioDestinationNodeLike): void;\n disconnect(): void;\n start(when?: number): void;\n stop(when?: number): void;\n onended: ((this: AudioBufferSourceNodeLike, ev: unknown) => void) | null;\n}\n\nexport interface AudioContextLike {\n readonly state: 'suspended' | 'running' | 'closed';\n readonly currentTime: number;\n readonly destination: AudioDestinationNodeLike;\n createGain(): GainNodeLike;\n createBufferSource(): AudioBufferSourceNodeLike;\n resume(): Promise<void>;\n suspend(): Promise<void>;\n close(): Promise<void>;\n}\n\n/** Audio context state. */\nexport type AudioState = 'uninitialized' | 'running' | 'suspended' | 'closed';\n\n/**\n * AudioManager — manages the AudioContext lifecycle.\n * Wraps react-native-audio-api's AudioContext.\n */\nexport class AudioManager {\n private ctx: AudioContextLike | null = null;\n\n /** Current audio state. */\n get state(): AudioState {\n if (!this.ctx) return 'uninitialized';\n return this.ctx.state === 'closed' ? 'closed' : this.ctx.state;\n }\n\n /** The underlying AudioContext, or null if not initialized. */\n get context(): AudioContextLike | null {\n return this.ctx;\n }\n\n /** Current time from the AudioContext. */\n get currentTime(): number {\n return this.ctx?.currentTime ?? 0;\n }\n\n /** Initialize with a factory that creates the native AudioContext. */\n init(factory: () => AudioContextLike): void {\n if (this.ctx) return;\n this.ctx = factory();\n }\n\n /** Suspend audio. Call from your own background handler — no automatic wiring. */\n async suspend(): Promise<void> {\n if (this.ctx?.state === 'running') {\n await this.ctx.suspend();\n }\n }\n\n /** Resume audio. Call from your own foreground handler — no automatic wiring. */\n async resume(): Promise<void> {\n if (this.ctx?.state === 'suspended') {\n await this.ctx.resume();\n }\n }\n\n /** Close and dispose the AudioContext. */\n async close(): Promise<void> {\n if (this.ctx) {\n await this.ctx.close();\n this.ctx = null;\n }\n }\n}\n","import type { AudioContextLike, GainNodeLike } from './audio-manager.js';\n\n/** A volume channel that wraps a GainNode. */\nexport interface Channel {\n readonly name: string;\n /** Current volume (0.0 - 1.0). */\n volume: number;\n /** The underlying GainNode. */\n readonly node: GainNodeLike;\n}\n\n/** Standard channel names. */\nexport type ChannelName = 'master' | 'sfx' | 'music';\n\n/**\n * Creates volume channels connected: sfx → master, music → master, master → destination.\n * Returns a map of channels by name.\n */\nexport function createChannels(ctx: AudioContextLike): Map<ChannelName, Channel> {\n const masterGain = ctx.createGain();\n masterGain.gain.value = 1;\n masterGain.connect(ctx.destination);\n\n const sfxGain = ctx.createGain();\n sfxGain.gain.value = 1;\n sfxGain.connect(masterGain);\n\n const musicGain = ctx.createGain();\n musicGain.gain.value = 1;\n musicGain.connect(masterGain);\n\n const channels = new Map<ChannelName, Channel>();\n\n channels.set('master', createChannel('master', masterGain));\n channels.set('sfx', createChannel('sfx', sfxGain));\n channels.set('music', createChannel('music', musicGain));\n\n return channels;\n}\n\nfunction createChannel(name: string, node: GainNodeLike): Channel {\n let vol = 1;\n return {\n name,\n get volume() {\n return vol;\n },\n set volume(v: number) {\n vol = Math.max(0, Math.min(1, v));\n node.gain.value = vol;\n },\n node,\n };\n}\n","import type {\n AudioBufferLike,\n AudioBufferSourceNodeLike,\n AudioContextLike,\n GainNodeLike,\n} from './audio-manager.js';\n\n/**\n * Music player — one track at a time, with crossfade support.\n * Loops by default.\n */\nexport class Music {\n private ctx: AudioContextLike | null = null;\n private output: GainNodeLike | null = null;\n private buffers = new Map<string, AudioBufferLike>();\n private currentSource: AudioBufferSourceNodeLike | null = null;\n private currentGain: GainNodeLike | null = null;\n private currentName: string | null = null;\n\n /** Name of the currently playing track, or null. */\n get playing(): string | null {\n return this.currentName;\n }\n\n /** Connect to an AudioContext and output gain node. */\n connect(ctx: AudioContextLike, output: GainNodeLike): void {\n this.ctx = ctx;\n this.output = output;\n }\n\n /** Register a music track buffer. */\n register(name: string, buffer: AudioBufferLike): void {\n this.buffers.set(name, buffer);\n }\n\n /** Play a track. Stops any currently playing track. Loops by default. */\n play(name: string, loop = true): void {\n if (!this.ctx || !this.output) return;\n const buffer = this.buffers.get(name);\n if (!buffer) return;\n\n this.stopInternal();\n\n const source = this.ctx.createBufferSource();\n source.buffer = buffer;\n source.loop = loop;\n\n const gain = this.ctx.createGain();\n gain.gain.value = 1;\n source.connect(gain);\n gain.connect(this.output);\n\n source.start();\n this.currentSource = source;\n this.currentGain = gain;\n this.currentName = name;\n\n source.onended = () => {\n if (this.currentSource === source) {\n this.currentSource = null;\n this.currentGain = null;\n this.currentName = null;\n }\n gain.disconnect();\n };\n }\n\n /** Stop the current track. */\n stop(): void {\n this.stopInternal();\n }\n\n /**\n * Crossfade to another track over duration seconds.\n * Fades out current and fades in the new track simultaneously.\n */\n crossfadeTo(name: string, duration: number, loop = true): void {\n if (!this.ctx || !this.output) return;\n const buffer = this.buffers.get(name);\n if (!buffer) return;\n\n const now = this.ctx.currentTime;\n\n // Fade out current\n if (this.currentGain && this.currentSource) {\n const oldGain = this.currentGain;\n const oldSource = this.currentSource;\n oldGain.gain.setValueAtTime(oldGain.gain.value, now);\n oldGain.gain.linearRampToValueAtTime(0, now + duration);\n // Schedule stop after fade\n try {\n oldSource.stop(now + duration);\n } catch {\n // Already stopped\n }\n }\n\n // Start new track fading in\n const source = this.ctx.createBufferSource();\n source.buffer = buffer;\n source.loop = loop;\n\n const gain = this.ctx.createGain();\n gain.gain.setValueAtTime(0, now);\n gain.gain.linearRampToValueAtTime(1, now + duration);\n source.connect(gain);\n gain.connect(this.output);\n\n source.start();\n this.currentSource = source;\n this.currentGain = gain;\n this.currentName = name;\n\n source.onended = () => {\n if (this.currentSource === source) {\n this.currentSource = null;\n this.currentGain = null;\n this.currentName = null;\n }\n gain.disconnect();\n };\n }\n\n private stopInternal(): void {\n if (this.currentSource) {\n try {\n this.currentSource.stop();\n } catch {\n // Already stopped\n }\n }\n if (this.currentGain) {\n this.currentGain.disconnect();\n }\n this.currentSource = null;\n this.currentGain = null;\n this.currentName = null;\n }\n}\n","import type { AudioBufferLike, AudioContextLike, GainNodeLike } from './audio-manager.js';\n\n/** Options for playing a sound effect. */\nexport interface PlaySFXOptions {\n /** Playback volume (0-1). Default: 1. */\n volume?: number;\n /** Playback rate multiplier. Default: 1. Applied only when the audio node exposes a `playbackRate` param. */\n rate?: number;\n /** Whether to loop. Default: false. */\n loop?: boolean;\n}\n\n/** A handle to a playing SFX instance. */\nexport interface SFXHandle {\n /** Stop playback. */\n stop(): void;\n}\n\n/** Tracks an active playing source. */\ninterface ActiveSource {\n name: string;\n stop(): void;\n}\n\n/**\n * SFX pool — pre-decoded audio buffers with polyphony management.\n * When max polyphony is reached, the oldest sound is killed.\n */\nexport class SFXPool {\n private buffers = new Map<string, AudioBufferLike>();\n private active: ActiveSource[] = [];\n private ctx: AudioContextLike | null = null;\n private output: GainNodeLike | null = null;\n private maxPolyphony: number;\n\n constructor(maxPolyphony = 16) {\n if (maxPolyphony <= 0) throw new Error('SFXPool: maxPolyphony must be > 0');\n this.maxPolyphony = maxPolyphony;\n }\n\n /** Connect to an AudioContext and output GainNode. */\n connect(ctx: AudioContextLike, output: GainNodeLike): void {\n this.ctx = ctx;\n this.output = output;\n }\n\n /** Register a pre-decoded audio buffer. */\n register(name: string, buffer: AudioBufferLike): void {\n this.buffers.set(name, buffer);\n }\n\n /** Unregister a buffer. */\n unregister(name: string): void {\n this.buffers.delete(name);\n }\n\n /** Check if a buffer is registered. */\n has(name: string): boolean {\n return this.buffers.has(name);\n }\n\n /** Number of registered buffers. */\n get size(): number {\n return this.buffers.size;\n }\n\n /** Number of currently active sounds. */\n get activeCount(): number {\n return this.active.length;\n }\n\n /** Play a registered sound. Returns a handle, or null if not registered/no context. */\n play(name: string, opts?: PlaySFXOptions): SFXHandle | null {\n const buffer = this.buffers.get(name);\n if (!buffer || !this.ctx || !this.output) return null;\n\n // Kill oldest if at max polyphony\n while (this.active.length >= this.maxPolyphony) {\n const oldest = this.active.shift();\n oldest?.stop();\n }\n\n const source = this.ctx.createBufferSource();\n source.buffer = buffer;\n source.loop = opts?.loop ?? false;\n\n // Apply playback rate when the injected node exposes the param (Web Audio shape).\n const rateParam = source.playbackRate;\n if (rateParam && opts?.rate !== undefined) {\n rateParam.value = opts.rate;\n }\n\n // Create a per-source gain for volume control\n const gain = this.ctx.createGain();\n gain.gain.value = opts?.volume ?? 1;\n source.connect(gain);\n gain.connect(this.output);\n\n const entry: ActiveSource = {\n name,\n stop() {\n try {\n source.stop();\n } catch {\n // Already stopped\n }\n gain.disconnect();\n },\n };\n\n this.active.push(entry);\n\n // Clean up when playback ends\n source.onended = () => {\n const idx = this.active.indexOf(entry);\n if (idx !== -1) {\n this.active.splice(idx, 1);\n }\n gain.disconnect();\n };\n\n source.start();\n\n return {\n stop: () => {\n entry.stop();\n const idx = this.active.indexOf(entry);\n if (idx !== -1) {\n this.active.splice(idx, 1);\n }\n },\n };\n }\n\n /** Stop all active sounds. */\n stopAll(): void {\n for (const entry of this.active) {\n entry.stop();\n }\n this.active.length = 0;\n }\n}\n"],"mappings":";;;;;;AA6DA,IAAa,eAAb,MAA0B;CACxB,MAAuC;;CAGvC,IAAI,QAAoB;EACtB,IAAI,CAAC,KAAK,KAAK,OAAO;EACtB,OAAO,KAAK,IAAI,UAAU,WAAW,WAAW,KAAK,IAAI;CAC3D;;CAGA,IAAI,UAAmC;EACrC,OAAO,KAAK;CACd;;CAGA,IAAI,cAAsB;EACxB,OAAO,KAAK,KAAK,eAAe;CAClC;;CAGA,KAAK,SAAuC;EAC1C,IAAI,KAAK,KAAK;EACd,KAAK,MAAM,QAAQ;CACrB;;CAGA,MAAM,UAAyB;EAC7B,IAAI,KAAK,KAAK,UAAU,WACtB,MAAM,KAAK,IAAI,QAAQ;CAE3B;;CAGA,MAAM,SAAwB;EAC5B,IAAI,KAAK,KAAK,UAAU,aACtB,MAAM,KAAK,IAAI,OAAO;CAE1B;;CAGA,MAAM,QAA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