@jgorset/beatnet 0.0.0-stage → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +149 -2
- package/THIRD_PARTY_NOTICES.md +21 -0
- package/dist/dbn.d.ts +18 -0
- package/dist/dbn.js +600 -0
- package/dist/dbn.json +1 -0
- package/dist/dsp.d.ts +4 -0
- package/dist/dsp.js +672 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +72 -0
- package/dist/inference.d.ts +5 -0
- package/dist/inference.js +77 -0
- package/dist/preprocessing.json +1 -0
- package/dist/wasm.d.ts +7 -0
- package/dist/wasm.js +495 -0
- package/licenses/BeatNet-CC-BY-4.0.txt +395 -0
- package/licenses/madmom-BSD-2-Clause.txt +24 -0
- package/model.json +18 -0
- package/package.json +50 -4
package/dist/wasm.js
ADDED
|
@@ -0,0 +1,495 @@
|
|
|
1
|
+
// Minimal zero-dependency WebAssembly kernel emitter for BeatNet's hot numerical loops:
|
|
2
|
+
// 1) Exact IEEE-754 Float64 radix-4 DIT Bluestein STFT (bit-identical to fft.js).
|
|
3
|
+
// 2) Contiguous-predecessor chain-compressed Viterbi DBN block advance.
|
|
4
|
+
// Uses WASM SIMD (f64x2) when supported by the host runtime, with scalar WASM 1.0 fallback.
|
|
5
|
+
const uleb = (n) => {
|
|
6
|
+
const out = [];
|
|
7
|
+
do {
|
|
8
|
+
let b = n & 0x7f;
|
|
9
|
+
n >>>= 7;
|
|
10
|
+
if (n)
|
|
11
|
+
b |= 0x80;
|
|
12
|
+
out.push(b);
|
|
13
|
+
} while (n);
|
|
14
|
+
return out;
|
|
15
|
+
};
|
|
16
|
+
const sleb = (n) => {
|
|
17
|
+
const out = [];
|
|
18
|
+
let more = true;
|
|
19
|
+
while (more) {
|
|
20
|
+
let b = n & 0x7f;
|
|
21
|
+
n >>= 7;
|
|
22
|
+
if ((n === 0 && (b & 0x40) === 0) || (n === -1 && (b & 0x40) !== 0))
|
|
23
|
+
more = false;
|
|
24
|
+
else
|
|
25
|
+
b |= 0x80;
|
|
26
|
+
out.push(b);
|
|
27
|
+
}
|
|
28
|
+
return out;
|
|
29
|
+
};
|
|
30
|
+
const section = (id, body) => [id, ...uleb(body.length), ...body];
|
|
31
|
+
const str = (s) => [...uleb(s.length), ...Array.from(s, c => c.charCodeAt(0))];
|
|
32
|
+
const f64Const = (v) => {
|
|
33
|
+
const b = new ArrayBuffer(8);
|
|
34
|
+
new DataView(b).setFloat64(0, v, true);
|
|
35
|
+
return [0x44, ...new Uint8Array(b)];
|
|
36
|
+
};
|
|
37
|
+
const I32 = 0x7f, F64 = 0x7c, V128 = 0x7b;
|
|
38
|
+
const OP = {
|
|
39
|
+
block: 0x02, loop: 0x03, if: 0x04, else: 0x05, end: 0x0b, br: 0x0c, br_if: 0x0d,
|
|
40
|
+
local_get: 0x20, local_set: 0x21, local_tee: 0x22,
|
|
41
|
+
i32_load: 0x28, i32_load8_u: 0x2d, i32_load16_u: 0x2f, f64_load: 0x2b,
|
|
42
|
+
i32_store16: 0x3b, f32_store: 0x38, f64_store: 0x39,
|
|
43
|
+
i32_const: 0x41,
|
|
44
|
+
i32_lt_s: 0x48, i32_ge_s: 0x4e,
|
|
45
|
+
f64_gt: 0x64,
|
|
46
|
+
i32_add: 0x6a, i32_sub: 0x6b, i32_mul: 0x6c, i32_and: 0x71, i32_shl: 0x74,
|
|
47
|
+
f64_neg: 0x9a, f64_sqrt: 0x9f, f64_add: 0xa0, f64_sub: 0xa1, f64_mul: 0xa2,
|
|
48
|
+
f32_demote_f64: 0xb6,
|
|
49
|
+
};
|
|
50
|
+
const SIMD = {
|
|
51
|
+
v128_load: [0xfd, 0x00, 3, 0],
|
|
52
|
+
v128_store: [0xfd, 0x0b, 3, 0],
|
|
53
|
+
f64x2_add: [0xfd, ...uleb(0xf0)],
|
|
54
|
+
f64x2_sub: [0xfd, ...uleb(0xf1)],
|
|
55
|
+
f64x2_mul: [0xfd, ...uleb(0xf2)],
|
|
56
|
+
};
|
|
57
|
+
const g = (i) => [OP.local_get, ...uleb(i)];
|
|
58
|
+
const s = (i) => [OP.local_set, ...uleb(i)];
|
|
59
|
+
const t = (i) => [OP.local_tee, ...uleb(i)];
|
|
60
|
+
const ld32 = (addr, off = 0) => [...addr, OP.i32_load, 2, ...uleb(off)];
|
|
61
|
+
const ld16u = (addr, off = 0) => [...addr, OP.i32_load16_u, 1, ...uleb(off)];
|
|
62
|
+
const ld8u = (addr, off = 0) => [...addr, OP.i32_load8_u, 0, ...uleb(off)];
|
|
63
|
+
const ld64 = (addr, off = 0) => [...addr, OP.f64_load, 3, ...uleb(off)];
|
|
64
|
+
const st64 = (addr, val, off = 0) => [...addr, ...val, OP.f64_store, 3, ...uleb(off)];
|
|
65
|
+
const st32f = (addr, val, off = 0) => [...addr, ...val, OP.f32_store, 2, ...uleb(off)];
|
|
66
|
+
const st16 = (addr, val, off = 0) => [...addr, ...val, OP.i32_store16, 1, ...uleb(off)];
|
|
67
|
+
const ld128 = (addr, off = 0) => [...addr, 0xfd, 0x00, 3, ...uleb(off)];
|
|
68
|
+
const st128 = (addr, val, off = 0) => [...addr, ...val, 0xfd, 0x0b, 3, ...uleb(off)];
|
|
69
|
+
const idx8 = (ptr, idx) => [...g(ptr), ...g(idx), OP.i32_const, 3, OP.i32_shl, OP.i32_add];
|
|
70
|
+
const idx4 = (ptr, idx) => [...g(ptr), ...g(idx), OP.i32_const, 2, OP.i32_shl, OP.i32_add];
|
|
71
|
+
const idx2 = (ptr, idx) => [...g(ptr), ...g(idx), OP.i32_const, 1, OP.i32_shl, OP.i32_add];
|
|
72
|
+
function supportsSimd() {
|
|
73
|
+
try {
|
|
74
|
+
// Minimal valid SIMD module with a single v128.const
|
|
75
|
+
const probe = new Uint8Array([
|
|
76
|
+
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00,
|
|
77
|
+
0x01, 0x04, 0x01, 0x60, 0x00, 0x00,
|
|
78
|
+
0x03, 0x02, 0x01, 0x00,
|
|
79
|
+
0x0a, 0x17, 0x01, 0x15, 0x00, 0xfd, 0x0c,
|
|
80
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x1a, 0x0b,
|
|
81
|
+
]);
|
|
82
|
+
return typeof WebAssembly !== 'undefined' && WebAssembly.validate(probe);
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
// Emit radix4Stage(rePtr, imPtr, twPtr, n, len, q, isInv) using f64x2 SIMD (2 adjacent butterflies at a time!).
|
|
89
|
+
// twPtr stores 6 contiguous f64 arrays per stage in pairs: [tBr0,tBr1, tBi0,tBi1, tCr0,tCr1, tCi0,tCi1, tDr0,tDr1, tDi0,tDi1] (96 bytes per pair).
|
|
90
|
+
function emitRadix4StageSimd() {
|
|
91
|
+
// Params: 0:rePtr, 1:imPtr, 2:twPtr, 3:n, 4:len, 5:q, 6:isInv
|
|
92
|
+
// Locals i32 (9): 7:outOff, 8:i, 9:o, 10:A, 11:B, 12:C, 13:D, 14:q2, 15:q3
|
|
93
|
+
// Locals v128 (24): 16:Ar, 17:Ai, 18:Br, 19:Bi, 20:Cr, 21:Ci, 22:Dr, 23:Di,
|
|
94
|
+
// 24:tR, 25:tI, 26:MBr, 27:MBi, 28:MCr, 29:MCi, 30:MDr, 31:MDi,
|
|
95
|
+
// 32:T0r, 33:T0i, 34:T1r, 35:T1i, 36:T2r, 37:T2i, 38:T3r, 39:T3i
|
|
96
|
+
const twAddr = () => [...g(2), ...g(9), OP.i32_add];
|
|
97
|
+
return [
|
|
98
|
+
2, ...uleb(9), I32, ...uleb(24), V128,
|
|
99
|
+
...g(5), OP.i32_const, 1, OP.i32_shl, ...t(14),
|
|
100
|
+
...g(5), OP.i32_add, ...s(15),
|
|
101
|
+
OP.i32_const, 0, ...s(7),
|
|
102
|
+
OP.block, 0x40,
|
|
103
|
+
OP.loop, 0x40,
|
|
104
|
+
...g(7), ...g(3), OP.i32_ge_s, OP.br_if, 1,
|
|
105
|
+
OP.i32_const, 0, ...s(8),
|
|
106
|
+
OP.i32_const, 0, ...s(9),
|
|
107
|
+
OP.block, 0x40,
|
|
108
|
+
OP.loop, 0x40,
|
|
109
|
+
...g(8), ...g(5), OP.i32_ge_s, OP.br_if, 1,
|
|
110
|
+
...g(7), ...g(8), OP.i32_add, ...t(10),
|
|
111
|
+
...g(5), OP.i32_add, ...s(11),
|
|
112
|
+
...g(10), ...g(14), OP.i32_add, ...s(12),
|
|
113
|
+
...g(10), ...g(15), OP.i32_add, ...s(13),
|
|
114
|
+
...ld128(idx8(0, 10)), ...s(16), ...ld128(idx8(1, 10)), ...s(17),
|
|
115
|
+
...ld128(idx8(0, 11)), ...s(18), ...ld128(idx8(1, 11)), ...s(19),
|
|
116
|
+
...ld128(idx8(0, 12)), ...s(20), ...ld128(idx8(1, 12)), ...s(21),
|
|
117
|
+
...ld128(idx8(0, 13)), ...s(22), ...ld128(idx8(1, 13)), ...s(23),
|
|
118
|
+
...ld128(twAddr(), 0), ...s(24), ...ld128(twAddr(), 16), ...s(25),
|
|
119
|
+
...g(18), ...g(24), ...SIMD.f64x2_mul, ...g(19), ...g(25), ...SIMD.f64x2_mul, ...SIMD.f64x2_sub, ...s(26),
|
|
120
|
+
...g(18), ...g(25), ...SIMD.f64x2_mul, ...g(19), ...g(24), ...SIMD.f64x2_mul, ...SIMD.f64x2_add, ...s(27),
|
|
121
|
+
...ld128(twAddr(), 32), ...s(24), ...ld128(twAddr(), 48), ...s(25),
|
|
122
|
+
...g(20), ...g(24), ...SIMD.f64x2_mul, ...g(21), ...g(25), ...SIMD.f64x2_mul, ...SIMD.f64x2_sub, ...s(28),
|
|
123
|
+
...g(20), ...g(25), ...SIMD.f64x2_mul, ...g(21), ...g(24), ...SIMD.f64x2_mul, ...SIMD.f64x2_add, ...s(29),
|
|
124
|
+
...ld128(twAddr(), 64), ...s(24), ...ld128(twAddr(), 80), ...s(25),
|
|
125
|
+
...g(22), ...g(24), ...SIMD.f64x2_mul, ...g(23), ...g(25), ...SIMD.f64x2_mul, ...SIMD.f64x2_sub, ...s(30),
|
|
126
|
+
...g(22), ...g(25), ...SIMD.f64x2_mul, ...g(23), ...g(24), ...SIMD.f64x2_mul, ...SIMD.f64x2_add, ...s(31),
|
|
127
|
+
...g(16), ...g(28), ...SIMD.f64x2_add, ...s(32),
|
|
128
|
+
...g(17), ...g(29), ...SIMD.f64x2_add, ...s(33),
|
|
129
|
+
...g(16), ...g(28), ...SIMD.f64x2_sub, ...s(34),
|
|
130
|
+
...g(17), ...g(29), ...SIMD.f64x2_sub, ...s(35),
|
|
131
|
+
...g(26), ...g(30), ...SIMD.f64x2_add, ...s(36),
|
|
132
|
+
...g(27), ...g(31), ...SIMD.f64x2_add, ...s(37),
|
|
133
|
+
...g(6), OP.if, 0x40,
|
|
134
|
+
...g(30), ...g(26), ...SIMD.f64x2_sub, ...s(38),
|
|
135
|
+
...g(31), ...g(27), ...SIMD.f64x2_sub, ...s(39),
|
|
136
|
+
OP.else,
|
|
137
|
+
...g(26), ...g(30), ...SIMD.f64x2_sub, ...s(38),
|
|
138
|
+
...g(27), ...g(31), ...SIMD.f64x2_sub, ...s(39),
|
|
139
|
+
OP.end,
|
|
140
|
+
...st128(idx8(0, 10), [...g(32), ...g(36), ...SIMD.f64x2_add]),
|
|
141
|
+
...st128(idx8(1, 10), [...g(33), ...g(37), ...SIMD.f64x2_add]),
|
|
142
|
+
...st128(idx8(0, 11), [...g(34), ...g(39), ...SIMD.f64x2_add]),
|
|
143
|
+
...st128(idx8(1, 11), [...g(35), ...g(38), ...SIMD.f64x2_sub]),
|
|
144
|
+
...st128(idx8(0, 12), [...g(32), ...g(36), ...SIMD.f64x2_sub]),
|
|
145
|
+
...st128(idx8(1, 12), [...g(33), ...g(37), ...SIMD.f64x2_sub]),
|
|
146
|
+
...st128(idx8(0, 13), [...g(34), ...g(39), ...SIMD.f64x2_sub]),
|
|
147
|
+
...st128(idx8(1, 13), [...g(35), ...g(38), ...SIMD.f64x2_add]),
|
|
148
|
+
...g(8), OP.i32_const, 2, OP.i32_add, ...s(8),
|
|
149
|
+
...g(9), OP.i32_const, ...sleb(96), OP.i32_add, ...s(9),
|
|
150
|
+
OP.br, 0,
|
|
151
|
+
OP.end, OP.end,
|
|
152
|
+
...g(7), ...g(4), OP.i32_add, ...s(7),
|
|
153
|
+
OP.br, 0,
|
|
154
|
+
OP.end, OP.end,
|
|
155
|
+
OP.end,
|
|
156
|
+
];
|
|
157
|
+
}
|
|
158
|
+
// Scalar fallback for radix4Stage (identical semantics, 48-byte twiddle stride per i).
|
|
159
|
+
function emitRadix4StageScalar() {
|
|
160
|
+
const twAddr = () => [...g(2), ...g(9), OP.i32_add];
|
|
161
|
+
return [
|
|
162
|
+
2, ...uleb(9), I32, ...uleb(24), F64,
|
|
163
|
+
...g(5), OP.i32_const, 1, OP.i32_shl, ...t(14),
|
|
164
|
+
...g(5), OP.i32_add, ...s(15),
|
|
165
|
+
OP.i32_const, 0, ...s(7),
|
|
166
|
+
OP.block, 0x40,
|
|
167
|
+
OP.loop, 0x40,
|
|
168
|
+
...g(7), ...g(3), OP.i32_ge_s, OP.br_if, 1,
|
|
169
|
+
OP.i32_const, 0, ...s(8),
|
|
170
|
+
OP.i32_const, 0, ...s(9),
|
|
171
|
+
OP.block, 0x40,
|
|
172
|
+
OP.loop, 0x40,
|
|
173
|
+
...g(8), ...g(5), OP.i32_ge_s, OP.br_if, 1,
|
|
174
|
+
...g(7), ...g(8), OP.i32_add, ...t(10),
|
|
175
|
+
...g(5), OP.i32_add, ...s(11),
|
|
176
|
+
...g(10), ...g(14), OP.i32_add, ...s(12),
|
|
177
|
+
...g(10), ...g(15), OP.i32_add, ...s(13),
|
|
178
|
+
...ld64(idx8(0, 10)), ...s(16), ...ld64(idx8(1, 10)), ...s(17),
|
|
179
|
+
...ld64(idx8(0, 11)), ...s(18), ...ld64(idx8(1, 11)), ...s(19),
|
|
180
|
+
...ld64(idx8(0, 12)), ...s(20), ...ld64(idx8(1, 12)), ...s(21),
|
|
181
|
+
...ld64(idx8(0, 13)), ...s(22), ...ld64(idx8(1, 13)), ...s(23),
|
|
182
|
+
...ld64(twAddr(), 0), ...s(24), ...ld64(twAddr(), 8), ...s(25),
|
|
183
|
+
...g(18), ...g(24), OP.f64_mul, ...g(19), ...g(25), OP.f64_mul, OP.f64_sub, ...s(26),
|
|
184
|
+
...g(18), ...g(25), OP.f64_mul, ...g(19), ...g(24), OP.f64_mul, OP.f64_add, ...s(27),
|
|
185
|
+
...ld64(twAddr(), 16), ...s(24), ...ld64(twAddr(), 24), ...s(25),
|
|
186
|
+
...g(20), ...g(24), OP.f64_mul, ...g(21), ...g(25), OP.f64_mul, OP.f64_sub, ...s(28),
|
|
187
|
+
...g(20), ...g(25), OP.f64_mul, ...g(21), ...g(24), OP.f64_mul, OP.f64_add, ...s(29),
|
|
188
|
+
...ld64(twAddr(), 32), ...s(24), ...ld64(twAddr(), 40), ...s(25),
|
|
189
|
+
...g(22), ...g(24), OP.f64_mul, ...g(23), ...g(25), OP.f64_mul, OP.f64_sub, ...s(30),
|
|
190
|
+
...g(22), ...g(25), OP.f64_mul, ...g(23), ...g(24), OP.f64_mul, OP.f64_add, ...s(31),
|
|
191
|
+
...g(16), ...g(28), OP.f64_add, ...s(32),
|
|
192
|
+
...g(17), ...g(29), OP.f64_add, ...s(33),
|
|
193
|
+
...g(16), ...g(28), OP.f64_sub, ...s(34),
|
|
194
|
+
...g(17), ...g(29), OP.f64_sub, ...s(35),
|
|
195
|
+
...g(26), ...g(30), OP.f64_add, ...s(36),
|
|
196
|
+
...g(27), ...g(31), OP.f64_add, ...s(37),
|
|
197
|
+
...g(6), OP.if, 0x40,
|
|
198
|
+
...g(30), ...g(26), OP.f64_sub, ...s(38),
|
|
199
|
+
...g(31), ...g(27), OP.f64_sub, ...s(39),
|
|
200
|
+
OP.else,
|
|
201
|
+
...g(26), ...g(30), OP.f64_sub, ...s(38),
|
|
202
|
+
...g(27), ...g(31), OP.f64_sub, ...s(39),
|
|
203
|
+
OP.end,
|
|
204
|
+
...st64(idx8(0, 10), [...g(32), ...g(36), OP.f64_add]),
|
|
205
|
+
...st64(idx8(1, 10), [...g(33), ...g(37), OP.f64_add]),
|
|
206
|
+
...st64(idx8(0, 11), [...g(34), ...g(39), OP.f64_add]),
|
|
207
|
+
...st64(idx8(1, 11), [...g(35), ...g(38), OP.f64_sub]),
|
|
208
|
+
...st64(idx8(0, 12), [...g(32), ...g(36), OP.f64_sub]),
|
|
209
|
+
...st64(idx8(1, 12), [...g(33), ...g(37), OP.f64_sub]),
|
|
210
|
+
...st64(idx8(0, 13), [...g(34), ...g(39), OP.f64_sub]),
|
|
211
|
+
...st64(idx8(1, 13), [...g(35), ...g(38), OP.f64_add]),
|
|
212
|
+
...g(8), OP.i32_const, 1, OP.i32_add, ...s(8),
|
|
213
|
+
...g(9), OP.i32_const, ...sleb(48), OP.i32_add, ...s(9),
|
|
214
|
+
OP.br, 0,
|
|
215
|
+
OP.end, OP.end,
|
|
216
|
+
...g(7), ...g(4), OP.i32_add, ...s(7),
|
|
217
|
+
OP.br, 0,
|
|
218
|
+
OP.end, OP.end,
|
|
219
|
+
OP.end,
|
|
220
|
+
];
|
|
221
|
+
}
|
|
222
|
+
// Function 1: bluesteinRun(rePtr, imPtr, inRePtr, inImPtr, specRePtr, specImPtr, bitrevPtr,
|
|
223
|
+
// twFwdBasePtr, twInvBasePtr, lastInvTwPtr, magOutPtr, outLen)
|
|
224
|
+
// Fixed n = 4096, quarter = 1024, stage twiddle tables laid out sequentially (q = 4, 16, 64, 256, 1024).
|
|
225
|
+
function emitBluestein4096(simd) {
|
|
226
|
+
// Params (12 i32):
|
|
227
|
+
// 0:re, 1:im, 2:inRe, 3:inIm, 4:specRe, 5:specIm, 6:bitrev,
|
|
228
|
+
// 7:twFwdBase, 8:twInvBase, 9:lastInvTw, 10:magOut, 11:outLen
|
|
229
|
+
// Locals: i32 (7): 12:i, 13:off, 14:outOff, 15:twOff, 16:q, 17:o, 18:bIdx
|
|
230
|
+
// f64 (16): 19:Ar, 20:Ai, 21:Br, 22:Bi, 23:Cr, 24:Ci, 25:Dr, 26:Di,
|
|
231
|
+
// 27:T0r, 28:T0i, 29:T1r, 30:T1i, 31:T2r, 32:T2i, 33:T3r, 34:T3i
|
|
232
|
+
const n = 4096, q0 = 1024, half = 2048, threeQ = 3072;
|
|
233
|
+
const stridePerStage = (q) => (simd ? (q >>> 1) * 96 : q * 48);
|
|
234
|
+
const callStage = (twBaseParam, twByteOffset, q, isInv) => [
|
|
235
|
+
...g(0), ...g(1),
|
|
236
|
+
...g(twBaseParam), OP.i32_const, ...sleb(twByteOffset), OP.i32_add,
|
|
237
|
+
OP.i32_const, ...sleb(n),
|
|
238
|
+
OP.i32_const, ...sleb(q << 2),
|
|
239
|
+
OP.i32_const, ...sleb(q),
|
|
240
|
+
OP.i32_const, isInv,
|
|
241
|
+
0x10, 0, // call func 0 (radix4Stage)
|
|
242
|
+
];
|
|
243
|
+
const off4 = 0;
|
|
244
|
+
const off16 = off4 + stridePerStage(4);
|
|
245
|
+
const off64 = off16 + stridePerStage(16);
|
|
246
|
+
const off256 = off64 + stridePerStage(64);
|
|
247
|
+
const off1024 = off256 + stridePerStage(256);
|
|
248
|
+
const lastTwAddr = () => [...g(9), ...g(17), OP.i32_add];
|
|
249
|
+
return [
|
|
250
|
+
2, ...uleb(7), I32, ...uleb(16), F64,
|
|
251
|
+
// 1. Forward stages 1..5 (re, im already hold stage-0 forward output)
|
|
252
|
+
...callStage(7, off4, 4, 0),
|
|
253
|
+
...callStage(7, off16, 16, 0),
|
|
254
|
+
...callStage(7, off64, 64, 0),
|
|
255
|
+
...callStage(7, off256, 256, 0),
|
|
256
|
+
...callStage(7, off1024, 1024, 0),
|
|
257
|
+
// 2. Pointwise complex multiply: in = re * spec
|
|
258
|
+
OP.i32_const, 0, ...s(12),
|
|
259
|
+
OP.block, 0x40,
|
|
260
|
+
OP.loop, 0x40,
|
|
261
|
+
...g(12), OP.i32_const, ...sleb(n), OP.i32_ge_s, OP.br_if, 1,
|
|
262
|
+
...ld64(idx8(0, 12)), ...s(19),
|
|
263
|
+
...ld64(idx8(1, 12)), ...s(20),
|
|
264
|
+
...ld64(idx8(4, 12)), ...s(21),
|
|
265
|
+
...ld64(idx8(5, 12)), ...s(22),
|
|
266
|
+
...st64(idx8(2, 12), [...g(19), ...g(21), OP.f64_mul, ...g(20), ...g(22), OP.f64_mul, OP.f64_sub]),
|
|
267
|
+
...st64(idx8(3, 12), [...g(19), ...g(22), OP.f64_mul, ...g(20), ...g(21), OP.f64_mul, OP.f64_add]),
|
|
268
|
+
...g(12), OP.i32_const, 1, OP.i32_add, ...s(12),
|
|
269
|
+
OP.br, 0,
|
|
270
|
+
OP.end, OP.end,
|
|
271
|
+
// 3. Stage 0 of inverse FFT (inv = -1) from (inRe, inIm) -> (re, im)
|
|
272
|
+
OP.i32_const, 0, ...s(12),
|
|
273
|
+
OP.i32_const, 0, ...s(14),
|
|
274
|
+
OP.block, 0x40,
|
|
275
|
+
OP.loop, 0x40,
|
|
276
|
+
...g(12), OP.i32_const, ...sleb(q0), OP.i32_ge_s, OP.br_if, 1,
|
|
277
|
+
...ld32(idx4(6, 12)), ...s(13),
|
|
278
|
+
...ld64(idx8(2, 13), 0), ...s(19), ...ld64(idx8(3, 13), 0), ...s(20),
|
|
279
|
+
...ld64(idx8(2, 13), q0 * 8), ...s(21), ...ld64(idx8(3, 13), q0 * 8), ...s(22),
|
|
280
|
+
...ld64(idx8(2, 13), half * 8), ...s(23), ...ld64(idx8(3, 13), half * 8), ...s(24),
|
|
281
|
+
...ld64(idx8(2, 13), threeQ * 8), ...s(25), ...ld64(idx8(3, 13), threeQ * 8), ...s(26),
|
|
282
|
+
...g(19), ...g(23), OP.f64_add, ...s(27),
|
|
283
|
+
...g(20), ...g(24), OP.f64_add, ...s(28),
|
|
284
|
+
...g(19), ...g(23), OP.f64_sub, ...s(29),
|
|
285
|
+
...g(20), ...g(24), OP.f64_sub, ...s(30),
|
|
286
|
+
...g(21), ...g(25), OP.f64_add, ...s(31),
|
|
287
|
+
...g(22), ...g(26), OP.f64_add, ...s(32),
|
|
288
|
+
...g(25), ...g(21), OP.f64_sub, ...s(33),
|
|
289
|
+
...g(26), ...g(22), OP.f64_sub, ...s(34),
|
|
290
|
+
...st64(idx8(0, 14), [...g(27), ...g(31), OP.f64_add], 0),
|
|
291
|
+
...st64(idx8(1, 14), [...g(28), ...g(32), OP.f64_add], 0),
|
|
292
|
+
...st64(idx8(0, 14), [...g(29), ...g(34), OP.f64_add], 8),
|
|
293
|
+
...st64(idx8(1, 14), [...g(30), ...g(33), OP.f64_sub], 8),
|
|
294
|
+
...st64(idx8(0, 14), [...g(27), ...g(31), OP.f64_sub], 16),
|
|
295
|
+
...st64(idx8(1, 14), [...g(28), ...g(32), OP.f64_sub], 16),
|
|
296
|
+
...st64(idx8(0, 14), [...g(29), ...g(34), OP.f64_sub], 24),
|
|
297
|
+
...st64(idx8(1, 14), [...g(30), ...g(33), OP.f64_add], 24),
|
|
298
|
+
...g(12), OP.i32_const, 1, OP.i32_add, ...s(12),
|
|
299
|
+
...g(14), OP.i32_const, 4, OP.i32_add, ...s(14),
|
|
300
|
+
OP.br, 0,
|
|
301
|
+
OP.end, OP.end,
|
|
302
|
+
// 4. Inverse stages 1..4 (q = 4, 16, 64, 256)
|
|
303
|
+
...callStage(8, off4, 4, 1),
|
|
304
|
+
...callStage(8, off16, 16, 1),
|
|
305
|
+
...callStage(8, off64, 64, 1),
|
|
306
|
+
...callStage(8, off256, 256, 1),
|
|
307
|
+
// 5. Truncated final inverse stage (q = 1024, only bins 0..outLen-1 <= 1024) + magnitude -> f32
|
|
308
|
+
// lastInvTw stores scalar 48-byte entries [tBr, tBi, tCr, tCi, tDr, tDi] for i = 0..1023
|
|
309
|
+
OP.i32_const, 0, ...s(12),
|
|
310
|
+
OP.i32_const, 0, ...s(17),
|
|
311
|
+
OP.block, 0x40,
|
|
312
|
+
OP.loop, 0x40,
|
|
313
|
+
...g(12), ...g(11), OP.i32_ge_s, OP.br_if, 1,
|
|
314
|
+
...ld64(idx8(0, 12), 0), ...s(19), ...ld64(idx8(1, 12), 0), ...s(20),
|
|
315
|
+
...ld64(idx8(0, 12), q0 * 8), ...s(21), ...ld64(idx8(1, 12), q0 * 8), ...s(22),
|
|
316
|
+
...ld64(idx8(0, 12), half * 8), ...s(23), ...ld64(idx8(1, 12), half * 8), ...s(24),
|
|
317
|
+
...ld64(idx8(0, 12), threeQ * 8), ...s(25), ...ld64(idx8(1, 12), threeQ * 8), ...s(26),
|
|
318
|
+
// MBr, MBi -> 27, 28
|
|
319
|
+
...ld64(lastTwAddr(), 0), ...s(33), ...ld64(lastTwAddr(), 8), ...s(34),
|
|
320
|
+
...g(21), ...g(33), OP.f64_mul, ...g(22), ...g(34), OP.f64_mul, OP.f64_sub, ...s(27),
|
|
321
|
+
...g(21), ...g(34), OP.f64_mul, ...g(22), ...g(33), OP.f64_mul, OP.f64_add, ...s(28),
|
|
322
|
+
// MCr, MCi -> 29, 30
|
|
323
|
+
...ld64(lastTwAddr(), 16), ...s(33), ...ld64(lastTwAddr(), 24), ...s(34),
|
|
324
|
+
...g(23), ...g(33), OP.f64_mul, ...g(24), ...g(34), OP.f64_mul, OP.f64_sub, ...s(29),
|
|
325
|
+
...g(23), ...g(34), OP.f64_mul, ...g(24), ...g(33), OP.f64_mul, OP.f64_add, ...s(30),
|
|
326
|
+
// MDr, MDi -> 31, 32
|
|
327
|
+
...ld64(lastTwAddr(), 32), ...s(33), ...ld64(lastTwAddr(), 40), ...s(34),
|
|
328
|
+
...g(25), ...g(33), OP.f64_mul, ...g(26), ...g(34), OP.f64_mul, OP.f64_sub, ...s(31),
|
|
329
|
+
...g(25), ...g(34), OP.f64_mul, ...g(26), ...g(33), OP.f64_mul, OP.f64_add, ...s(32),
|
|
330
|
+
// FAr = ((Ar + MCr) + (MBr + MDr)) * (1/4096) -> 33; FAi -> 34
|
|
331
|
+
...g(19), ...g(29), OP.f64_add, ...g(27), ...g(31), OP.f64_add, OP.f64_add, ...f64Const(1 / n), OP.f64_mul, ...s(33),
|
|
332
|
+
...g(20), ...g(30), OP.f64_add, ...g(28), ...g(32), OP.f64_add, OP.f64_add, ...f64Const(1 / n), OP.f64_mul, ...s(34),
|
|
333
|
+
// magOut[i] = f32(sqrt(FAr*FAr + FAi*FAi))
|
|
334
|
+
...st32f(idx4(10, 12), [
|
|
335
|
+
...g(33), ...g(33), OP.f64_mul, ...g(34), ...g(34), OP.f64_mul, OP.f64_add, OP.f64_sqrt, OP.f32_demote_f64,
|
|
336
|
+
]),
|
|
337
|
+
...g(12), OP.i32_const, 1, OP.i32_add, ...s(12),
|
|
338
|
+
...g(17), OP.i32_const, ...sleb(48), OP.i32_add, ...s(17),
|
|
339
|
+
OP.br, 0,
|
|
340
|
+
OP.end, OP.end,
|
|
341
|
+
OP.end,
|
|
342
|
+
];
|
|
343
|
+
}
|
|
344
|
+
// Function 2: dbnRunBlock(tStart, tStop, numChains, ringMask, predPtrs, predStart, predCount,
|
|
345
|
+
// predLogTrans, lens, chainBonusIdx, frameBonuses, bonusStride, ring, tailScores, tracePtr)
|
|
346
|
+
// Runs [tStart, tStop) of contiguous-predecessor chain-compressed Viterbi DBN.
|
|
347
|
+
// If tracePtr >= 0, writes u16 predecessor chain indices to tracePtr[(t - tStart) * numChains + c].
|
|
348
|
+
function emitDbnRunBlock() {
|
|
349
|
+
// Params (15 i32):
|
|
350
|
+
// 0:tStart, 1:tStop, 2:numChains, 3:ringMask, 4:predPtrs, 5:predStart, 6:predCount,
|
|
351
|
+
// 7:predLogTrans, 8:lens, 9:chainBonusIdx, 10:frameBonuses, 11:bonusStride, 12:ring, 13:tailScores, 14:tracePtr
|
|
352
|
+
// Locals i32 (10): 15:t, 16:c, 17:writeBase, 18:p, 19:pEnd, 20:pc, 21:pred, 22:bonusBase, 23:traceBase, 24:slot
|
|
353
|
+
// Locals f64 (2): 25:best, 26:cand
|
|
354
|
+
return [
|
|
355
|
+
2, ...uleb(10), I32, ...uleb(2), F64,
|
|
356
|
+
...g(0), ...s(15),
|
|
357
|
+
OP.block, 0x40,
|
|
358
|
+
OP.loop, 0x40,
|
|
359
|
+
...g(15), ...g(1), OP.i32_ge_s, OP.br_if, 1,
|
|
360
|
+
// writeBase = (t & ringMask) * numChains
|
|
361
|
+
...g(15), ...g(3), OP.i32_and, ...g(2), OP.i32_mul, ...s(17),
|
|
362
|
+
// bonusBase = (t - tStart) * bonusStride
|
|
363
|
+
...g(15), ...g(0), OP.i32_sub, ...g(11), OP.i32_mul, ...s(22),
|
|
364
|
+
// traceBase = (t - tStart) * numChains
|
|
365
|
+
...g(15), ...g(0), OP.i32_sub, ...g(2), OP.i32_mul, ...s(23),
|
|
366
|
+
// 1. Gather tailScores[c] = ring[((t - lens[c]) & ringMask) * numChains + c]
|
|
367
|
+
OP.i32_const, 0, ...s(16),
|
|
368
|
+
OP.block, 0x40,
|
|
369
|
+
OP.loop, 0x40,
|
|
370
|
+
...g(16), ...g(2), OP.i32_ge_s, OP.br_if, 1,
|
|
371
|
+
...g(15), ...ld16u(idx2(8, 16)), OP.i32_sub, ...g(3), OP.i32_and, ...g(2), OP.i32_mul, ...g(16), OP.i32_add, ...s(24),
|
|
372
|
+
...st64(idx8(13, 16), ld64(idx8(12, 24))),
|
|
373
|
+
...g(16), OP.i32_const, 1, OP.i32_add, ...s(16),
|
|
374
|
+
OP.br, 0,
|
|
375
|
+
OP.end, OP.end,
|
|
376
|
+
// 2. For each chain head c = 0..numChains-1, find max over contiguous preds
|
|
377
|
+
OP.i32_const, 0, ...s(16),
|
|
378
|
+
...g(14), OP.i32_const, 0, OP.i32_ge_s, OP.if, 0x40,
|
|
379
|
+
// Trace-enabled loop
|
|
380
|
+
OP.block, 0x40,
|
|
381
|
+
OP.loop, 0x40,
|
|
382
|
+
...g(16), ...g(2), OP.i32_ge_s, OP.br_if, 1,
|
|
383
|
+
...f64Const(-Infinity), ...s(25),
|
|
384
|
+
OP.i32_const, 0, ...s(21),
|
|
385
|
+
...ld32(idx4(4, 16)), ...t(18),
|
|
386
|
+
...ld16u(idx2(6, 16)), OP.i32_add, ...s(19),
|
|
387
|
+
...ld16u(idx2(5, 16)), ...s(20),
|
|
388
|
+
OP.block, 0x40,
|
|
389
|
+
OP.loop, 0x40,
|
|
390
|
+
...g(18), ...g(19), OP.i32_ge_s, OP.br_if, 1,
|
|
391
|
+
...ld64(idx8(13, 20)), ...ld64(idx8(7, 18)), OP.f64_add, ...t(26),
|
|
392
|
+
...g(25), OP.f64_gt, OP.if, 0x40,
|
|
393
|
+
...g(26), ...s(25),
|
|
394
|
+
...g(20), ...s(21),
|
|
395
|
+
OP.end,
|
|
396
|
+
...g(18), OP.i32_const, 1, OP.i32_add, ...s(18),
|
|
397
|
+
...g(20), OP.i32_const, 1, OP.i32_add, ...s(20),
|
|
398
|
+
OP.br, 0,
|
|
399
|
+
OP.end, OP.end,
|
|
400
|
+
// ring[writeBase + c] = best + frameBonuses[bonusBase + chainBonusIdx[c]]
|
|
401
|
+
...g(17), ...g(16), OP.i32_add, ...s(24),
|
|
402
|
+
...st64(idx8(12, 24), [
|
|
403
|
+
...g(25),
|
|
404
|
+
...ld64([...g(10), ...g(22), ...ld8u([...g(9), ...g(16), OP.i32_add]), OP.i32_add, OP.i32_const, 3, OP.i32_shl, OP.i32_add]),
|
|
405
|
+
OP.f64_add,
|
|
406
|
+
]),
|
|
407
|
+
...g(23), ...g(16), OP.i32_add, ...s(24),
|
|
408
|
+
...st16(idx2(14, 24), g(21)),
|
|
409
|
+
...g(16), OP.i32_const, 1, OP.i32_add, ...s(16),
|
|
410
|
+
OP.br, 0,
|
|
411
|
+
OP.end, OP.end,
|
|
412
|
+
OP.else,
|
|
413
|
+
// Forward-only loop (no trace writes)
|
|
414
|
+
OP.block, 0x40,
|
|
415
|
+
OP.loop, 0x40,
|
|
416
|
+
...g(16), ...g(2), OP.i32_ge_s, OP.br_if, 1,
|
|
417
|
+
...f64Const(-Infinity), ...s(25),
|
|
418
|
+
...ld32(idx4(4, 16)), ...t(18),
|
|
419
|
+
...ld16u(idx2(6, 16)), OP.i32_add, ...s(19),
|
|
420
|
+
...ld16u(idx2(5, 16)), ...s(20),
|
|
421
|
+
OP.block, 0x40,
|
|
422
|
+
OP.loop, 0x40,
|
|
423
|
+
...g(18), ...g(19), OP.i32_ge_s, OP.br_if, 1,
|
|
424
|
+
...ld64(idx8(13, 20)), ...ld64(idx8(7, 18)), OP.f64_add, ...t(26),
|
|
425
|
+
...g(25), OP.f64_gt, OP.if, 0x40,
|
|
426
|
+
...g(26), ...s(25),
|
|
427
|
+
OP.end,
|
|
428
|
+
...g(18), OP.i32_const, 1, OP.i32_add, ...s(18),
|
|
429
|
+
...g(20), OP.i32_const, 1, OP.i32_add, ...s(20),
|
|
430
|
+
OP.br, 0,
|
|
431
|
+
OP.end, OP.end,
|
|
432
|
+
...g(17), ...g(16), OP.i32_add, ...s(24),
|
|
433
|
+
...st64(idx8(12, 24), [
|
|
434
|
+
...g(25),
|
|
435
|
+
...ld64([...g(10), ...g(22), ...ld8u([...g(9), ...g(16), OP.i32_add]), OP.i32_add, OP.i32_const, 3, OP.i32_shl, OP.i32_add]),
|
|
436
|
+
OP.f64_add,
|
|
437
|
+
]),
|
|
438
|
+
...g(16), OP.i32_const, 1, OP.i32_add, ...s(16),
|
|
439
|
+
OP.br, 0,
|
|
440
|
+
OP.end, OP.end,
|
|
441
|
+
OP.end,
|
|
442
|
+
...g(15), OP.i32_const, 1, OP.i32_add, ...s(15),
|
|
443
|
+
OP.br, 0,
|
|
444
|
+
OP.end, OP.end,
|
|
445
|
+
OP.end,
|
|
446
|
+
];
|
|
447
|
+
}
|
|
448
|
+
let cachedWasm;
|
|
449
|
+
export function getBeatNetWasm() {
|
|
450
|
+
if (cachedWasm !== undefined)
|
|
451
|
+
return cachedWasm;
|
|
452
|
+
try {
|
|
453
|
+
if (typeof WebAssembly === 'undefined') {
|
|
454
|
+
cachedWasm = null;
|
|
455
|
+
return null;
|
|
456
|
+
}
|
|
457
|
+
const simd = supportsSimd();
|
|
458
|
+
const f0 = simd ? emitRadix4StageSimd() : emitRadix4StageScalar();
|
|
459
|
+
const f1 = emitBluestein4096(simd);
|
|
460
|
+
const f2 = emitDbnRunBlock();
|
|
461
|
+
const typeSec = section(1, [
|
|
462
|
+
3,
|
|
463
|
+
0x60, 7, I32, I32, I32, I32, I32, I32, I32, 0,
|
|
464
|
+
0x60, 12, I32, I32, I32, I32, I32, I32, I32, I32, I32, I32, I32, I32, 0,
|
|
465
|
+
0x60, 15, I32, I32, I32, I32, I32, I32, I32, I32, I32, I32, I32, I32, I32, I32, I32, 0,
|
|
466
|
+
]);
|
|
467
|
+
const funcSec = section(3, [3, 0, 1, 2]);
|
|
468
|
+
// 32 pages = 2 MB linear memory (plenty for both FFT tables and DBN working blocks)
|
|
469
|
+
const memSec = section(5, [1, 0, ...uleb(32)]);
|
|
470
|
+
const expSec = section(7, [
|
|
471
|
+
3,
|
|
472
|
+
...str('mem'), 2, 0,
|
|
473
|
+
...str('bluestein4096'), 0, 1,
|
|
474
|
+
...str('dbnRunBlock'), 0, 2,
|
|
475
|
+
]);
|
|
476
|
+
const codeSec = section(10, [
|
|
477
|
+
3,
|
|
478
|
+
...uleb(f0.length), ...f0,
|
|
479
|
+
...uleb(f1.length), ...f1,
|
|
480
|
+
...uleb(f2.length), ...f2,
|
|
481
|
+
]);
|
|
482
|
+
const bytes = new Uint8Array([
|
|
483
|
+
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00,
|
|
484
|
+
...typeSec, ...funcSec, ...memSec, ...expSec, ...codeSec,
|
|
485
|
+
]);
|
|
486
|
+
const instance = new WebAssembly.Instance(new WebAssembly.Module(bytes));
|
|
487
|
+
const exp = instance.exports;
|
|
488
|
+
cachedWasm = { simd, mem: exp.mem, bluestein4096: exp.bluestein4096, dbnRunBlock: exp.dbnRunBlock };
|
|
489
|
+
return cachedWasm;
|
|
490
|
+
}
|
|
491
|
+
catch {
|
|
492
|
+
cachedWasm = null;
|
|
493
|
+
return null;
|
|
494
|
+
}
|
|
495
|
+
}
|