@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/LICENSE +201 -21
- package/NOTICE +2 -0
- package/README.md +50 -8
- package/dist/index.d.mts +120 -109
- package/dist/index.d.mts.map +1 -0
- package/dist/index.d.ts +120 -109
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +263 -297
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +259 -267
- package/dist/index.mjs.map +1 -1
- package/package.json +15 -7
package/dist/index.mjs
CHANGED
|
@@ -1,277 +1,269 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/audio-manager.ts
|
|
2
|
+
/**
|
|
3
|
+
* AudioManager — manages the AudioContext lifecycle.
|
|
4
|
+
* Wraps react-native-audio-api's AudioContext.
|
|
5
|
+
*/
|
|
2
6
|
var AudioManager = class {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
async close() {
|
|
38
|
-
if (this.ctx) {
|
|
39
|
-
await this.ctx.close();
|
|
40
|
-
this.ctx = null;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
7
|
+
ctx = null;
|
|
8
|
+
/** Current audio state. */
|
|
9
|
+
get state() {
|
|
10
|
+
if (!this.ctx) return "uninitialized";
|
|
11
|
+
return this.ctx.state === "closed" ? "closed" : this.ctx.state;
|
|
12
|
+
}
|
|
13
|
+
/** The underlying AudioContext, or null if not initialized. */
|
|
14
|
+
get context() {
|
|
15
|
+
return this.ctx;
|
|
16
|
+
}
|
|
17
|
+
/** Current time from the AudioContext. */
|
|
18
|
+
get currentTime() {
|
|
19
|
+
return this.ctx?.currentTime ?? 0;
|
|
20
|
+
}
|
|
21
|
+
/** Initialize with a factory that creates the native AudioContext. */
|
|
22
|
+
init(factory) {
|
|
23
|
+
if (this.ctx) return;
|
|
24
|
+
this.ctx = factory();
|
|
25
|
+
}
|
|
26
|
+
/** Suspend audio. Call from your own background handler — no automatic wiring. */
|
|
27
|
+
async suspend() {
|
|
28
|
+
if (this.ctx?.state === "running") await this.ctx.suspend();
|
|
29
|
+
}
|
|
30
|
+
/** Resume audio. Call from your own foreground handler — no automatic wiring. */
|
|
31
|
+
async resume() {
|
|
32
|
+
if (this.ctx?.state === "suspended") await this.ctx.resume();
|
|
33
|
+
}
|
|
34
|
+
/** Close and dispose the AudioContext. */
|
|
35
|
+
async close() {
|
|
36
|
+
if (this.ctx) {
|
|
37
|
+
await this.ctx.close();
|
|
38
|
+
this.ctx = null;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
43
41
|
};
|
|
44
|
-
|
|
45
|
-
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/channels.ts
|
|
44
|
+
/**
|
|
45
|
+
* Creates volume channels connected: sfx → master, music → master, master → destination.
|
|
46
|
+
* Returns a map of channels by name.
|
|
47
|
+
*/
|
|
46
48
|
function createChannels(ctx) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
49
|
+
const masterGain = ctx.createGain();
|
|
50
|
+
masterGain.gain.value = 1;
|
|
51
|
+
masterGain.connect(ctx.destination);
|
|
52
|
+
const sfxGain = ctx.createGain();
|
|
53
|
+
sfxGain.gain.value = 1;
|
|
54
|
+
sfxGain.connect(masterGain);
|
|
55
|
+
const musicGain = ctx.createGain();
|
|
56
|
+
musicGain.gain.value = 1;
|
|
57
|
+
musicGain.connect(masterGain);
|
|
58
|
+
const channels = /* @__PURE__ */ new Map();
|
|
59
|
+
channels.set("master", createChannel("master", masterGain));
|
|
60
|
+
channels.set("sfx", createChannel("sfx", sfxGain));
|
|
61
|
+
channels.set("music", createChannel("music", musicGain));
|
|
62
|
+
return channels;
|
|
61
63
|
}
|
|
62
64
|
function createChannel(name, node) {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
65
|
+
let vol = 1;
|
|
66
|
+
return {
|
|
67
|
+
name,
|
|
68
|
+
get volume() {
|
|
69
|
+
return vol;
|
|
70
|
+
},
|
|
71
|
+
set volume(v) {
|
|
72
|
+
vol = Math.max(0, Math.min(1, v));
|
|
73
|
+
node.gain.value = vol;
|
|
74
|
+
},
|
|
75
|
+
node
|
|
76
|
+
};
|
|
75
77
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
output = null;
|
|
83
|
-
maxPolyphony;
|
|
84
|
-
constructor(maxPolyphony = 16) {
|
|
85
|
-
this.maxPolyphony = maxPolyphony;
|
|
86
|
-
}
|
|
87
|
-
/** Connect to an AudioContext and output GainNode. */
|
|
88
|
-
connect(ctx, output) {
|
|
89
|
-
this.ctx = ctx;
|
|
90
|
-
this.output = output;
|
|
91
|
-
}
|
|
92
|
-
/** Register a pre-decoded audio buffer. */
|
|
93
|
-
register(name, buffer) {
|
|
94
|
-
this.buffers.set(name, buffer);
|
|
95
|
-
}
|
|
96
|
-
/** Unregister a buffer. */
|
|
97
|
-
unregister(name) {
|
|
98
|
-
this.buffers.delete(name);
|
|
99
|
-
}
|
|
100
|
-
/** Check if a buffer is registered. */
|
|
101
|
-
has(name) {
|
|
102
|
-
return this.buffers.has(name);
|
|
103
|
-
}
|
|
104
|
-
/** Number of registered buffers. */
|
|
105
|
-
get size() {
|
|
106
|
-
return this.buffers.size;
|
|
107
|
-
}
|
|
108
|
-
/** Number of currently active sounds. */
|
|
109
|
-
get activeCount() {
|
|
110
|
-
return this.active.length;
|
|
111
|
-
}
|
|
112
|
-
/** Play a registered sound. Returns a handle, or null if not registered/no context. */
|
|
113
|
-
play(name, opts) {
|
|
114
|
-
const buffer = this.buffers.get(name);
|
|
115
|
-
if (!buffer || !this.ctx || !this.output) return null;
|
|
116
|
-
while (this.active.length >= this.maxPolyphony) {
|
|
117
|
-
const oldest = this.active.shift();
|
|
118
|
-
oldest?.stop();
|
|
119
|
-
}
|
|
120
|
-
const source = this.ctx.createBufferSource();
|
|
121
|
-
source.buffer = buffer;
|
|
122
|
-
source.loop = opts?.loop ?? false;
|
|
123
|
-
const gain = this.ctx.createGain();
|
|
124
|
-
gain.gain.value = opts?.volume ?? 1;
|
|
125
|
-
source.connect(gain);
|
|
126
|
-
gain.connect(this.output);
|
|
127
|
-
const entry = {
|
|
128
|
-
name,
|
|
129
|
-
stop() {
|
|
130
|
-
try {
|
|
131
|
-
source.stop();
|
|
132
|
-
} catch {
|
|
133
|
-
}
|
|
134
|
-
gain.disconnect();
|
|
135
|
-
}
|
|
136
|
-
};
|
|
137
|
-
this.active.push(entry);
|
|
138
|
-
source.onended = () => {
|
|
139
|
-
const idx = this.active.indexOf(entry);
|
|
140
|
-
if (idx !== -1) {
|
|
141
|
-
this.active.splice(idx, 1);
|
|
142
|
-
}
|
|
143
|
-
gain.disconnect();
|
|
144
|
-
};
|
|
145
|
-
source.start();
|
|
146
|
-
return {
|
|
147
|
-
stop: () => {
|
|
148
|
-
entry.stop();
|
|
149
|
-
const idx = this.active.indexOf(entry);
|
|
150
|
-
if (idx !== -1) {
|
|
151
|
-
this.active.splice(idx, 1);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
};
|
|
155
|
-
}
|
|
156
|
-
/** Stop all active sounds. */
|
|
157
|
-
stopAll() {
|
|
158
|
-
for (const entry of this.active) {
|
|
159
|
-
entry.stop();
|
|
160
|
-
}
|
|
161
|
-
this.active.length = 0;
|
|
162
|
-
}
|
|
163
|
-
};
|
|
164
|
-
|
|
165
|
-
// src/music.ts
|
|
78
|
+
//#endregion
|
|
79
|
+
//#region src/music.ts
|
|
80
|
+
/**
|
|
81
|
+
* Music player — one track at a time, with crossfade support.
|
|
82
|
+
* Loops by default.
|
|
83
|
+
*/
|
|
166
84
|
var Music = class {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
this.currentGain.disconnect();
|
|
265
|
-
}
|
|
266
|
-
this.currentSource = null;
|
|
267
|
-
this.currentGain = null;
|
|
268
|
-
this.currentName = null;
|
|
269
|
-
}
|
|
85
|
+
ctx = null;
|
|
86
|
+
output = null;
|
|
87
|
+
buffers = /* @__PURE__ */ new Map();
|
|
88
|
+
currentSource = null;
|
|
89
|
+
currentGain = null;
|
|
90
|
+
currentName = null;
|
|
91
|
+
/** Name of the currently playing track, or null. */
|
|
92
|
+
get playing() {
|
|
93
|
+
return this.currentName;
|
|
94
|
+
}
|
|
95
|
+
/** Connect to an AudioContext and output gain node. */
|
|
96
|
+
connect(ctx, output) {
|
|
97
|
+
this.ctx = ctx;
|
|
98
|
+
this.output = output;
|
|
99
|
+
}
|
|
100
|
+
/** Register a music track buffer. */
|
|
101
|
+
register(name, buffer) {
|
|
102
|
+
this.buffers.set(name, buffer);
|
|
103
|
+
}
|
|
104
|
+
/** Play a track. Stops any currently playing track. Loops by default. */
|
|
105
|
+
play(name, loop = true) {
|
|
106
|
+
if (!this.ctx || !this.output) return;
|
|
107
|
+
const buffer = this.buffers.get(name);
|
|
108
|
+
if (!buffer) return;
|
|
109
|
+
this.stopInternal();
|
|
110
|
+
const source = this.ctx.createBufferSource();
|
|
111
|
+
source.buffer = buffer;
|
|
112
|
+
source.loop = loop;
|
|
113
|
+
const gain = this.ctx.createGain();
|
|
114
|
+
gain.gain.value = 1;
|
|
115
|
+
source.connect(gain);
|
|
116
|
+
gain.connect(this.output);
|
|
117
|
+
source.start();
|
|
118
|
+
this.currentSource = source;
|
|
119
|
+
this.currentGain = gain;
|
|
120
|
+
this.currentName = name;
|
|
121
|
+
source.onended = () => {
|
|
122
|
+
if (this.currentSource === source) {
|
|
123
|
+
this.currentSource = null;
|
|
124
|
+
this.currentGain = null;
|
|
125
|
+
this.currentName = null;
|
|
126
|
+
}
|
|
127
|
+
gain.disconnect();
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
/** Stop the current track. */
|
|
131
|
+
stop() {
|
|
132
|
+
this.stopInternal();
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Crossfade to another track over duration seconds.
|
|
136
|
+
* Fades out current and fades in the new track simultaneously.
|
|
137
|
+
*/
|
|
138
|
+
crossfadeTo(name, duration, loop = true) {
|
|
139
|
+
if (!this.ctx || !this.output) return;
|
|
140
|
+
const buffer = this.buffers.get(name);
|
|
141
|
+
if (!buffer) return;
|
|
142
|
+
const now = this.ctx.currentTime;
|
|
143
|
+
if (this.currentGain && this.currentSource) {
|
|
144
|
+
const oldGain = this.currentGain;
|
|
145
|
+
const oldSource = this.currentSource;
|
|
146
|
+
oldGain.gain.setValueAtTime(oldGain.gain.value, now);
|
|
147
|
+
oldGain.gain.linearRampToValueAtTime(0, now + duration);
|
|
148
|
+
try {
|
|
149
|
+
oldSource.stop(now + duration);
|
|
150
|
+
} catch {}
|
|
151
|
+
}
|
|
152
|
+
const source = this.ctx.createBufferSource();
|
|
153
|
+
source.buffer = buffer;
|
|
154
|
+
source.loop = loop;
|
|
155
|
+
const gain = this.ctx.createGain();
|
|
156
|
+
gain.gain.setValueAtTime(0, now);
|
|
157
|
+
gain.gain.linearRampToValueAtTime(1, now + duration);
|
|
158
|
+
source.connect(gain);
|
|
159
|
+
gain.connect(this.output);
|
|
160
|
+
source.start();
|
|
161
|
+
this.currentSource = source;
|
|
162
|
+
this.currentGain = gain;
|
|
163
|
+
this.currentName = name;
|
|
164
|
+
source.onended = () => {
|
|
165
|
+
if (this.currentSource === source) {
|
|
166
|
+
this.currentSource = null;
|
|
167
|
+
this.currentGain = null;
|
|
168
|
+
this.currentName = null;
|
|
169
|
+
}
|
|
170
|
+
gain.disconnect();
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
stopInternal() {
|
|
174
|
+
if (this.currentSource) try {
|
|
175
|
+
this.currentSource.stop();
|
|
176
|
+
} catch {}
|
|
177
|
+
if (this.currentGain) this.currentGain.disconnect();
|
|
178
|
+
this.currentSource = null;
|
|
179
|
+
this.currentGain = null;
|
|
180
|
+
this.currentName = null;
|
|
181
|
+
}
|
|
270
182
|
};
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
183
|
+
//#endregion
|
|
184
|
+
//#region src/sfx-pool.ts
|
|
185
|
+
/**
|
|
186
|
+
* SFX pool — pre-decoded audio buffers with polyphony management.
|
|
187
|
+
* When max polyphony is reached, the oldest sound is killed.
|
|
188
|
+
*/
|
|
189
|
+
var SFXPool = class {
|
|
190
|
+
buffers = /* @__PURE__ */ new Map();
|
|
191
|
+
active = [];
|
|
192
|
+
ctx = null;
|
|
193
|
+
output = null;
|
|
194
|
+
maxPolyphony;
|
|
195
|
+
constructor(maxPolyphony = 16) {
|
|
196
|
+
if (maxPolyphony <= 0) throw new Error("SFXPool: maxPolyphony must be > 0");
|
|
197
|
+
this.maxPolyphony = maxPolyphony;
|
|
198
|
+
}
|
|
199
|
+
/** Connect to an AudioContext and output GainNode. */
|
|
200
|
+
connect(ctx, output) {
|
|
201
|
+
this.ctx = ctx;
|
|
202
|
+
this.output = output;
|
|
203
|
+
}
|
|
204
|
+
/** Register a pre-decoded audio buffer. */
|
|
205
|
+
register(name, buffer) {
|
|
206
|
+
this.buffers.set(name, buffer);
|
|
207
|
+
}
|
|
208
|
+
/** Unregister a buffer. */
|
|
209
|
+
unregister(name) {
|
|
210
|
+
this.buffers.delete(name);
|
|
211
|
+
}
|
|
212
|
+
/** Check if a buffer is registered. */
|
|
213
|
+
has(name) {
|
|
214
|
+
return this.buffers.has(name);
|
|
215
|
+
}
|
|
216
|
+
/** Number of registered buffers. */
|
|
217
|
+
get size() {
|
|
218
|
+
return this.buffers.size;
|
|
219
|
+
}
|
|
220
|
+
/** Number of currently active sounds. */
|
|
221
|
+
get activeCount() {
|
|
222
|
+
return this.active.length;
|
|
223
|
+
}
|
|
224
|
+
/** Play a registered sound. Returns a handle, or null if not registered/no context. */
|
|
225
|
+
play(name, opts) {
|
|
226
|
+
const buffer = this.buffers.get(name);
|
|
227
|
+
if (!buffer || !this.ctx || !this.output) return null;
|
|
228
|
+
while (this.active.length >= this.maxPolyphony) this.active.shift()?.stop();
|
|
229
|
+
const source = this.ctx.createBufferSource();
|
|
230
|
+
source.buffer = buffer;
|
|
231
|
+
source.loop = opts?.loop ?? false;
|
|
232
|
+
const rateParam = source.playbackRate;
|
|
233
|
+
if (rateParam && opts?.rate !== void 0) rateParam.value = opts.rate;
|
|
234
|
+
const gain = this.ctx.createGain();
|
|
235
|
+
gain.gain.value = opts?.volume ?? 1;
|
|
236
|
+
source.connect(gain);
|
|
237
|
+
gain.connect(this.output);
|
|
238
|
+
const entry = {
|
|
239
|
+
name,
|
|
240
|
+
stop() {
|
|
241
|
+
try {
|
|
242
|
+
source.stop();
|
|
243
|
+
} catch {}
|
|
244
|
+
gain.disconnect();
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
this.active.push(entry);
|
|
248
|
+
source.onended = () => {
|
|
249
|
+
const idx = this.active.indexOf(entry);
|
|
250
|
+
if (idx !== -1) this.active.splice(idx, 1);
|
|
251
|
+
gain.disconnect();
|
|
252
|
+
};
|
|
253
|
+
source.start();
|
|
254
|
+
return { stop: () => {
|
|
255
|
+
entry.stop();
|
|
256
|
+
const idx = this.active.indexOf(entry);
|
|
257
|
+
if (idx !== -1) this.active.splice(idx, 1);
|
|
258
|
+
} };
|
|
259
|
+
}
|
|
260
|
+
/** Stop all active sounds. */
|
|
261
|
+
stopAll() {
|
|
262
|
+
for (const entry of this.active) entry.stop();
|
|
263
|
+
this.active.length = 0;
|
|
264
|
+
}
|
|
276
265
|
};
|
|
266
|
+
//#endregion
|
|
267
|
+
export { AudioManager, Music, SFXPool, createChannels };
|
|
268
|
+
|
|
277
269
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/audio-manager.ts","../src/channels.ts","../src/sfx-pool.ts","../src/music.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 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":";AAwDO,IA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AA,MAC/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.mjs","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;;C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mBAAmB;EAC3C,OAAO,SAAS;EAChB,OAAO,OAAO,MAAM,QAAQ;EAG5B,MAAM,YAAY,OAAO;EACzB,IAAI,aAAa,MAAM,SAAS,KAAA,GAC9B,UAAU,QAAQ,KAAK;EAIzB,MAAM,OAAO,KAAK,IAAI,WAAW;EACjC,KAAK,KAAK,QAAQ,MAAM,UAAU;EAClC,OAAO,QAAQ,IAAI;EACnB,KAAK,QAAQ,KAAK,MAAM;EAExB,MAAM,QAAsB;GAC1B;GACA,OAAO;IACL,IAAI;KACF,OAAO,KAAK;IACd,QAAQ,CAER;IACA,KAAK,WAAW;GAClB;EACF;EAEA,KAAK,OAAO,KAAK,KAAK;EAGtB,OAAO,gBAAgB;GACrB,MAAM,MAAM,KAAK,OAAO,QAAQ,KAAK;GACrC,IAAI,QAAQ,IACV,KAAK,OAAO,OAAO,KAAK,CAAC;GAE3B,KAAK,WAAW;EAClB;EAEA,OAAO,MAAM;EAEb,OAAO,EACL,YAAY;GACV,MAAM,KAAK;GACX,MAAM,MAAM,KAAK,OAAO,QAAQ,KAAK;GACrC,IAAI,QAAQ,IACV,KAAK,OAAO,OAAO,KAAK,CAAC;EAE7B,EACF;CACF;;CAGA,UAAgB;EACd,KAAK,MAAM,SAAS,KAAK,QACvB,MAAM,KAAK;EAEb,KAAK,OAAO,SAAS;CACvB;AACF"}
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flare-engine/audio",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "AudioManager, SFX pool, music crossfade",
|
|
5
5
|
"keywords": ["flare-engine", "audio", "sfx", "music"],
|
|
6
|
-
"license": "
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
7
|
"author": "Grzegorz Otto",
|
|
8
8
|
"homepage": "https://github.com/grzott/flare-engine#readme",
|
|
9
9
|
"bugs": {
|
|
@@ -19,14 +19,19 @@
|
|
|
19
19
|
"types": "dist/index.d.ts",
|
|
20
20
|
"exports": {
|
|
21
21
|
".": {
|
|
22
|
-
"
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
"import": {
|
|
23
|
+
"types": "./dist/index.d.mts",
|
|
24
|
+
"default": "./dist/index.mjs"
|
|
25
|
+
},
|
|
26
|
+
"require": {
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"default": "./dist/index.js"
|
|
29
|
+
}
|
|
25
30
|
}
|
|
26
31
|
},
|
|
27
|
-
"files": ["dist", "README.md", "LICENSE"],
|
|
32
|
+
"files": ["dist", "README.md", "LICENSE", "NOTICE"],
|
|
28
33
|
"scripts": {
|
|
29
|
-
"build": "
|
|
34
|
+
"build": "tsdown",
|
|
30
35
|
"test": "bun test",
|
|
31
36
|
"lint": "biome check src/",
|
|
32
37
|
"typecheck": "tsc --noEmit"
|
|
@@ -34,6 +39,9 @@
|
|
|
34
39
|
"peerDependencies": {
|
|
35
40
|
"react-native-audio-api": ">=0.3.0"
|
|
36
41
|
},
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": ">=20"
|
|
44
|
+
},
|
|
37
45
|
"publishConfig": {
|
|
38
46
|
"access": "restricted",
|
|
39
47
|
"registry": "https://registry.npmjs.org/"
|