@margoengine/audio 0.1.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -201
- package/NOTICE +4 -4
- package/README.md +988 -748
- package/dist/index.js +4 -4
- package/dist/outdoor.js +24 -18
- package/dist/resample.js +34 -59
- package/dist/sinc-CbjImCBh.js +36 -0
- package/dist/sinc.d.ts +5 -0
- package/dist/spectrogram-fft-CRLetuHA.js +59 -0
- package/dist/spectrogram.js +102 -158
- package/dist/vocoder-envelope.d.ts +5 -0
- package/dist/vocoder-phase.d.ts +7 -0
- package/dist/vocoder-split.d.ts +10 -0
- package/dist/vocoder.d.ts +59 -0
- package/dist/vocoder.js +291 -0
- package/dist/voices-CdfPmzIo.js +2 -2
- package/package.json +132 -127
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
// @margo/core-built
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
2
|
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
-
const c=(engine,version="
|
|
3
|
+
const c=(engine,version="1.0.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
4
|
import { n as e } from "./errors-D8rc8kzg.js";
|
|
5
5
|
import { i as t, n, r, t as i } from "./math-C82bLsED.js";
|
|
6
6
|
import { n as a, r as o, t as s } from "./voices-CdfPmzIo.js";
|
|
@@ -83,8 +83,8 @@ var b = () => {
|
|
|
83
83
|
let t = globalThis.AudioContext;
|
|
84
84
|
if (!t) throw e("unsupported", "Web Audio is unavailable in this environment; run in a browser, or pass options.context (for example an OfflineAudioContext)");
|
|
85
85
|
return t;
|
|
86
|
-
}, x = class e {coreRequirement=__margoCoreRequirement("
|
|
87
|
-
static attach(t, n) {__margoAssertCore(t,__margoCoreRequirement("
|
|
86
|
+
}, x = class e {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
|
|
87
|
+
static attach(t, n) {__margoAssertCore(t,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
88
88
|
let r = new e(n);
|
|
89
89
|
return t.use(r), r;
|
|
90
90
|
}
|
package/dist/outdoor.js
CHANGED
|
@@ -1,22 +1,24 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { i as
|
|
1
|
+
import { t as e } from "./errors-D8rc8kzg.js";
|
|
2
|
+
import { i as t } from "./math-C82bLsED.js";
|
|
3
3
|
//#region src/outdoor.ts
|
|
4
|
-
var
|
|
4
|
+
var n = class n {
|
|
5
5
|
static {
|
|
6
6
|
this.owners = /* @__PURE__ */ new WeakSet();
|
|
7
7
|
}
|
|
8
|
-
constructor(
|
|
9
|
-
if (this.sound =
|
|
10
|
-
if (
|
|
11
|
-
if (
|
|
12
|
-
this.
|
|
13
|
-
|
|
14
|
-
this.
|
|
8
|
+
constructor(t, r, i = {}) {
|
|
9
|
+
if (this.sound = t, this.path = r, this.initial = !0, this.disposed = !1, !t || t.isDisposed || n.owners.has(t)) throw e("attached", "Outdoor audio needs a live, uncontrolled sound. Dispose its old controller or use a fresh sound", "outdoor-sound");
|
|
10
|
+
if (typeof r != "function") throw e("unsupported", "Outdoor audio needs a callback that returns a solved path. Pass a function", "outdoor-sound");
|
|
11
|
+
if (!i || typeof i != "object") throw e("unsupported", "Outdoor audio settings must be an object. Supply settings or omit the argument", "outdoor-sound");
|
|
12
|
+
if (this.upperDelay = i.maxDelay ?? 30, !(this.upperDelay > 0 && this.upperDelay <= 180)) throw e("unsupported", "Outdoor maxDelay must be above 0 and at most 180 seconds. Pass a sufficient delay", "outdoor-sound");
|
|
13
|
+
if (this.smoothing = i.smoothing ?? t.listener.smoothing, !(this.smoothing >= 0 && Number.isFinite(this.smoothing))) throw e("unsupported", "Outdoor smoothing must be finite and nonnegative. Pass seconds or use the listener value", "outdoor-sound");
|
|
14
|
+
this.previousRolloff = t.rolloffFactor;
|
|
15
|
+
let a = t.context;
|
|
16
|
+
this.delay = a.createDelay(this.upperDelay), this.level = a.createGain(), this.bass = a.createBiquadFilter(), this.treble = a.createBiquadFilter(), this.bass.type = "lowshelf", this.treble.type = "highshelf", this.bass.frequency.value = Math.min(250, a.sampleRate / 2), this.treble.frequency.value = Math.min(2800, a.sampleRate / 2), this.filters = [
|
|
15
17
|
this.delay,
|
|
16
18
|
this.bass,
|
|
17
19
|
this.treble,
|
|
18
20
|
this.level
|
|
19
|
-
], this.update(),
|
|
21
|
+
], this.update(), t.setFilters([...t.getFilters(), ...this.filters]), t.rolloffFactor = 0, t.listener.addSource(this), n.owners.add(t);
|
|
20
22
|
}
|
|
21
23
|
update() {
|
|
22
24
|
if (this.disposed) return;
|
|
@@ -24,11 +26,15 @@ var r = class r {
|
|
|
24
26
|
this.dispose();
|
|
25
27
|
return;
|
|
26
28
|
}
|
|
27
|
-
let
|
|
28
|
-
if (!(
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
let n = this.path();
|
|
30
|
+
if (!n || !(n.delaySeconds >= 0 && n.delaySeconds <= this.upperDelay)) throw e("unsupported", "Outdoor travel time exceeds the delay line. Increase maxDelay or pass a shorter path", "outdoor-sound");
|
|
31
|
+
let { delaySeconds: r, gainDb: i } = n;
|
|
32
|
+
if (!i || i.length !== 8) throw e("unsupported", "Outdoor audio needs finite octave gains. Pass a solved eight-band path", "outdoor-sound");
|
|
33
|
+
for (let t = 0; t < 8; t++) if (!Number.isFinite(i[t])) throw e("unsupported", "Outdoor audio needs finite octave gains. Pass a solved eight-band path", "outdoor-sound");
|
|
34
|
+
let a = i[4], o = 10 ** (a / 20), s = i[0] - a, c = i[7] - a;
|
|
35
|
+
if (!(Number.isFinite(Math.fround(o)) && Number.isFinite(Math.fround(s)) && Number.isFinite(Math.fround(c)))) throw e("failed", "Outdoor audio gains exceed the representable filter range. Reduce the path gain or equalizer difference", "outdoor-sound");
|
|
36
|
+
let l = this.sound.context, u = this.initial ? 0 : this.smoothing;
|
|
37
|
+
t(this.delay.delayTime, r, l, u), t(this.level.gain, o, l, u), t(this.bass.gain, s, l, u), t(this.treble.gain, c, l, u), this.initial = !1;
|
|
32
38
|
}
|
|
33
39
|
dispose() {
|
|
34
40
|
if (this.disposed) return;
|
|
@@ -36,11 +42,11 @@ var r = class r {
|
|
|
36
42
|
let e = this.sound;
|
|
37
43
|
e.listener.removeSource(this), e.isDisposed || (e.setFilters(e.getFilters().filter((e) => !this.filters.includes(e))), e.rolloffFactor === 0 && (e.rolloffFactor = this.previousRolloff));
|
|
38
44
|
for (let e of this.filters) e.disconnect();
|
|
39
|
-
|
|
45
|
+
n.owners.delete(e);
|
|
40
46
|
}
|
|
41
47
|
[Symbol.dispose]() {
|
|
42
48
|
this.dispose();
|
|
43
49
|
}
|
|
44
50
|
};
|
|
45
51
|
//#endregion
|
|
46
|
-
export {
|
|
52
|
+
export { n as OutdoorAudio };
|
package/dist/resample.js
CHANGED
|
@@ -1,28 +1,15 @@
|
|
|
1
1
|
import { i as e, r as t } from "./errors-D8rc8kzg.js";
|
|
2
|
+
import { t as n } from "./sinc-CbjImCBh.js";
|
|
2
3
|
//#region src/resample.ts
|
|
3
|
-
var
|
|
4
|
-
function i(
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
9
|
-
function a(e, t) {
|
|
10
|
-
let n = e * r, a = new Float64Array(n + 2), o = i(t);
|
|
11
|
-
a[0] = 1;
|
|
12
|
-
for (let s = 1; s <= n; s++) {
|
|
13
|
-
let n = s / r, c = n / e, l = c < 1 ? i(t * Math.sqrt(1 - c * c)) / o : 0;
|
|
14
|
-
a[s] = Math.sin(Math.PI * n) / (Math.PI * n) * l;
|
|
15
|
-
}
|
|
16
|
-
return a;
|
|
17
|
-
}
|
|
18
|
-
function o(r, i) {
|
|
19
|
-
if (!Array.isArray(r) || !r.length || !r.every((e) => e instanceof Float32Array)) throw e("Pass the channels as an array of Float32Array", "audio-resample");
|
|
20
|
-
let a = r[0].length;
|
|
21
|
-
if (!r.every((e) => e.length === a)) throw t("Every channel must hold the same number of samples", "audio-resample");
|
|
4
|
+
var r = Math.SQRT1_2;
|
|
5
|
+
function i(n, i) {
|
|
6
|
+
if (!Array.isArray(n) || !n.length || !n.every((e) => e instanceof Float32Array)) throw e("Pass the channels as an array of Float32Array", "audio-resample");
|
|
7
|
+
let a = n[0].length;
|
|
8
|
+
if (!n.every((e) => e.length === a)) throw t("Every channel must hold the same number of samples", "audio-resample");
|
|
22
9
|
if (!Number.isInteger(i) || i < 1 || i > 32) throw t("Pass an output channel count from 1 to 32", "audio-resample");
|
|
23
|
-
let o =
|
|
24
|
-
let i = s[e], o =
|
|
25
|
-
for (let e = 0; e < a; e++) i[e] +=
|
|
10
|
+
let o = n.length, s = Array.from({ length: i }, () => new Float32Array(a)), c = (e, t, r) => {
|
|
11
|
+
let i = s[e], o = n[t];
|
|
12
|
+
for (let e = 0; e < a; e++) i[e] += r * o[e];
|
|
26
13
|
}, l = o === i ? void 0 : {
|
|
27
14
|
"1>2": [[
|
|
28
15
|
0,
|
|
@@ -122,12 +109,12 @@ function o(r, i) {
|
|
|
122
109
|
[
|
|
123
110
|
0,
|
|
124
111
|
0,
|
|
125
|
-
|
|
112
|
+
r
|
|
126
113
|
],
|
|
127
114
|
[
|
|
128
115
|
0,
|
|
129
116
|
1,
|
|
130
|
-
|
|
117
|
+
r
|
|
131
118
|
],
|
|
132
119
|
[
|
|
133
120
|
0,
|
|
@@ -176,12 +163,12 @@ function o(r, i) {
|
|
|
176
163
|
[
|
|
177
164
|
0,
|
|
178
165
|
2,
|
|
179
|
-
|
|
166
|
+
r
|
|
180
167
|
],
|
|
181
168
|
[
|
|
182
169
|
0,
|
|
183
170
|
4,
|
|
184
|
-
|
|
171
|
+
r
|
|
185
172
|
],
|
|
186
173
|
[
|
|
187
174
|
1,
|
|
@@ -191,12 +178,12 @@ function o(r, i) {
|
|
|
191
178
|
[
|
|
192
179
|
1,
|
|
193
180
|
2,
|
|
194
|
-
|
|
181
|
+
r
|
|
195
182
|
],
|
|
196
183
|
[
|
|
197
184
|
1,
|
|
198
185
|
5,
|
|
199
|
-
|
|
186
|
+
r
|
|
200
187
|
]
|
|
201
188
|
],
|
|
202
189
|
"6>4": [
|
|
@@ -208,7 +195,7 @@ function o(r, i) {
|
|
|
208
195
|
[
|
|
209
196
|
0,
|
|
210
197
|
2,
|
|
211
|
-
|
|
198
|
+
r
|
|
212
199
|
],
|
|
213
200
|
[
|
|
214
201
|
1,
|
|
@@ -218,7 +205,7 @@ function o(r, i) {
|
|
|
218
205
|
[
|
|
219
206
|
1,
|
|
220
207
|
2,
|
|
221
|
-
|
|
208
|
+
r
|
|
222
209
|
],
|
|
223
210
|
[
|
|
224
211
|
2,
|
|
@@ -233,38 +220,26 @@ function o(r, i) {
|
|
|
233
220
|
]
|
|
234
221
|
}[`${o}>${i}`];
|
|
235
222
|
if (l) for (let [e, t, n] of l) c(e, t, n);
|
|
236
|
-
else for (let e = 0; e < Math.min(o, i); e++) s[e].set(
|
|
223
|
+
else for (let e = 0; e < Math.min(o, i); e++) s[e].set(n[e]);
|
|
237
224
|
return s;
|
|
238
225
|
}
|
|
239
|
-
function
|
|
240
|
-
if (!
|
|
241
|
-
let { zeroCrossings:
|
|
242
|
-
for (let e of [
|
|
243
|
-
if (!Number.isInteger(
|
|
244
|
-
if (!(
|
|
245
|
-
if (!(
|
|
246
|
-
let
|
|
247
|
-
if (
|
|
248
|
-
sampleRate:
|
|
249
|
-
channels:
|
|
226
|
+
function a(r, a, o = {}) {
|
|
227
|
+
if (!r || typeof r != "object") throw e("Pass {sampleRate, channels} audio", "audio-resample");
|
|
228
|
+
let { zeroCrossings: s = 32, rolloff: c = .92, attenuation: l = 90 } = o;
|
|
229
|
+
for (let e of [r.sampleRate, a]) if (!Number.isInteger(e) || e < 1 || e > 768e3) throw t("Sample rates must be whole numbers from 1 to 768000", "audio-resample");
|
|
230
|
+
if (!Number.isInteger(s) || s < 4 || s > 256) throw t("zeroCrossings must be a whole number from 4 to 256", "audio-resample");
|
|
231
|
+
if (!(c >= .5 && c <= 1)) throw t("rolloff must be from 0.5 to 1", "audio-resample");
|
|
232
|
+
if (!(l >= 40 && l <= 160)) throw t("attenuation must be from 40 to 160 dB", "audio-resample");
|
|
233
|
+
let u = i(r.channels, o.channels ?? r.channels.length), d = r.sampleRate, f = u[0].length;
|
|
234
|
+
if (d === a) return {
|
|
235
|
+
sampleRate: a,
|
|
236
|
+
channels: u
|
|
250
237
|
};
|
|
251
|
-
let
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
let n = Math.abs(t - e) * h * r, i = Math.floor(n), a = n - i;
|
|
256
|
-
v[o++] = i >= _.length - 1 ? 0 : h * (_[i] + (_[i + 1] - _[i]) * a);
|
|
257
|
-
}
|
|
258
|
-
for (let t = 0; t < d.length; t++) {
|
|
259
|
-
let r = d[t], i = 0;
|
|
260
|
-
for (let e = 0; e < o; e++) i += v[e] * r[n + e];
|
|
261
|
-
y[t][e] = i;
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
return {
|
|
265
|
-
sampleRate: i,
|
|
266
|
-
channels: y
|
|
238
|
+
let p = Math.floor((f * a + d - 1) / d), m = u.map(() => new Float32Array(p));
|
|
239
|
+
return n(d, a, s, c, l)(u, m, 0, p), {
|
|
240
|
+
sampleRate: a,
|
|
241
|
+
channels: m
|
|
267
242
|
};
|
|
268
243
|
}
|
|
269
244
|
//#endregion
|
|
270
|
-
export {
|
|
245
|
+
export { i as mixChannels, a as resample };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
//#region src/sinc.ts
|
|
2
|
+
var e = 1024;
|
|
3
|
+
function t(e) {
|
|
4
|
+
let t = 1, n = 1, r = e * e / 4;
|
|
5
|
+
for (let e = 1; e < 64 && (n *= r / (e * e), t += n, !(n < t * 1e-17)); e++);
|
|
6
|
+
return t;
|
|
7
|
+
}
|
|
8
|
+
function n(n, r) {
|
|
9
|
+
let i = n * e, a = new Float64Array(i + 2), o = t(r);
|
|
10
|
+
a[0] = 1;
|
|
11
|
+
for (let s = 1; s <= i; s++) {
|
|
12
|
+
let i = s / e, c = i / n, l = c < 1 ? t(r * Math.sqrt(1 - c * c)) / o : 0;
|
|
13
|
+
a[s] = Math.sin(Math.PI * i) / (Math.PI * i) * l;
|
|
14
|
+
}
|
|
15
|
+
return a;
|
|
16
|
+
}
|
|
17
|
+
function r(t, r, i = 32, a = .92, o = 90) {
|
|
18
|
+
let s = a * Math.min(1, r / t), c = i / s, l = n(i, o > 50 ? .1102 * (o - 8.7) : .5842 * (o - 21) ** .4 + .07886 * (o - 21)), u = new Float64Array(Math.ceil(2 * c) + 2);
|
|
19
|
+
return (n, i, a, o) => {
|
|
20
|
+
let d = n[0].length;
|
|
21
|
+
for (let f = a; f < o; f++) {
|
|
22
|
+
let a = f * t / r, o = Math.max(0, Math.ceil(a - c)), p = Math.min(d - 1, Math.floor(a + c)), m = 0;
|
|
23
|
+
for (let t = o; t <= p; t++) {
|
|
24
|
+
let n = Math.abs(a - t) * s * e, r = Math.floor(n), i = n - r;
|
|
25
|
+
u[m++] = r >= l.length - 1 ? 0 : s * (l[r] + (l[r + 1] - l[r]) * i);
|
|
26
|
+
}
|
|
27
|
+
for (let e = 0; e < n.length; e++) {
|
|
28
|
+
let t = n[e], r = 0;
|
|
29
|
+
for (let e = 0; e < m; e++) r += u[e] * t[o + e];
|
|
30
|
+
i[e][f] = r;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
//#endregion
|
|
36
|
+
export { r as t };
|
package/dist/sinc.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A renderer whose output sample `n` is the input read at time `n * step / per` samples. `render` fills output samples
|
|
3
|
+
* `from` to `to` (exclusive) of every channel, so a long signal can be converted in blocks with the same result.
|
|
4
|
+
*/
|
|
5
|
+
export declare function sincRenderer(step: number, per: number, zeroCrossings?: number, rolloff?: number, attenuation?: number): (channels: readonly Float32Array[], out: readonly Float32Array[], from: number, to: number) => void;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { r as e } from "./errors-D8rc8kzg.js";
|
|
2
|
+
//#region src/spectrogram-fft.ts
|
|
3
|
+
var t = /* @__PURE__ */ new Map();
|
|
4
|
+
function n(n) {
|
|
5
|
+
let r = t.get(n);
|
|
6
|
+
if (r) return r;
|
|
7
|
+
if (!Number.isInteger(n) || n < 2 || n > 65536 || n & n - 1) throw e(`FFT size must be a power of two from 2 to 65536, got ${n}`, "state");
|
|
8
|
+
let i = new Float64Array(n / 2), a = new Float64Array(n / 2), o = new Uint32Array(n), s = Math.log2(n);
|
|
9
|
+
for (let e = 0; e < n / 2; e++) i[e] = Math.cos(2 * Math.PI * e / n), a[e] = -Math.sin(2 * Math.PI * e / n);
|
|
10
|
+
for (let e = 0; e < n; e++) {
|
|
11
|
+
let t = 0;
|
|
12
|
+
for (let n = 0; n < s; n++) t |= (e >> n & 1) << s - 1 - n;
|
|
13
|
+
o[e] = t;
|
|
14
|
+
}
|
|
15
|
+
return t.size >= 8 && t.delete(t.keys().next().value), t.set(n, r = {
|
|
16
|
+
size: n,
|
|
17
|
+
cos: i,
|
|
18
|
+
sin: a,
|
|
19
|
+
reverse: o
|
|
20
|
+
}), r;
|
|
21
|
+
}
|
|
22
|
+
function r(e, t, n, r = !1) {
|
|
23
|
+
let { size: i, cos: a, sin: o, reverse: s } = e;
|
|
24
|
+
for (let e = 0; e < i; e++) {
|
|
25
|
+
let r = s[e];
|
|
26
|
+
if (r > e) {
|
|
27
|
+
let i = t[e];
|
|
28
|
+
t[e] = t[r], t[r] = i, i = n[e], n[e] = n[r], n[r] = i;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
let c = r ? -1 : 1;
|
|
32
|
+
for (let e = 2; e <= i; e *= 2) {
|
|
33
|
+
let r = e / 2, s = i / e;
|
|
34
|
+
for (let l = 0; l < i; l += e) for (let e = 0, i = 0; e < r; e++, i += s) {
|
|
35
|
+
let s = a[i], u = c * o[i], d = l + e, f = d + r, p = t[f] * s - n[f] * u, m = t[f] * u + n[f] * s;
|
|
36
|
+
t[f] = t[d] - p, n[f] = n[d] - m, t[d] += p, n[d] += m;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function i(e, t, n, i, a, o, s, c, l) {
|
|
41
|
+
let u = e.size;
|
|
42
|
+
i.set(t), a.set(n), r(e, i, a);
|
|
43
|
+
for (let e = 0; e <= u / 2; e++) {
|
|
44
|
+
let t = u - e & u - 1;
|
|
45
|
+
o[e] = (i[e] + i[t]) / 2, s[e] = (a[e] - a[t]) / 2, c[e] = (a[e] + a[t]) / 2, l[e] = (i[t] - i[e]) / 2;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
function a(e, t, n, i, a, o, s, c, l) {
|
|
49
|
+
let u = e.size;
|
|
50
|
+
for (let e = 0; e <= u / 2; e++) {
|
|
51
|
+
let r = u - e & u - 1, c = e === 0 || e === u / 2 ? 0 : n[e], l = e === 0 || e === u / 2 ? 0 : a[e];
|
|
52
|
+
o[e] = t[e] - l, s[e] = c + i[e], o[r] = t[e] + l, s[r] = -c + i[e];
|
|
53
|
+
}
|
|
54
|
+
r(e, o, s, !0);
|
|
55
|
+
let d = 1 / u;
|
|
56
|
+
for (let e = 0; e < u; e++) c[e] = o[e] * d, l[e] = s[e] * d;
|
|
57
|
+
}
|
|
58
|
+
//#endregion
|
|
59
|
+
export { i, n, a as r, r as t };
|