/ proof-of-concept / audio-spike-sorting
Sign in
proof-of-concept / audio-spike-sorting
audio-spike-sorting / scripts / synthetic-lib.ts
57 lines · 2.1 KBCodeBlameHistory
2 * Synthetic snippet library for the node tests: three event types (low
3 * ringing tone, high short tone, repeatable noise burst) standing in for
4 * recorded taps. A tap excites the struck object's impulse response, so
5 * instances of one type are highly repeatable up to gain and small
6 * decay/pitch drift; the burst type reuses one fixed noise pattern (the
7 * "object") rather than fresh noise per instance.
8 */
9import { mulberry32 } from '../src/bench/rng.ts';
10import { FS, type SnippetLibrary } from '../src/bench/types.ts';
12export const PRE_S = 0.02, POST_S = 0.18;
14export function makeSyntheticLibrary(perType = 120, seed = 42): SnippetLibrary {
15 const pre = Math.round(PRE_S * FS), post = Math.round(POST_S * FS);
16 const snipLen = pre + post;
17 const rng = mulberry32(seed);
19 const burstBase = new Float32Array(post);
20 {
21 const brng = mulberry32(7);
22 for (let i = 0; i < post; i++) burstBase[i] = brng() * 2 - 1;
23 }
25 function makeSnippet(kind: number): Float32Array {
26 const s = new Float32Array(snipLen);
27 const jitF = 1 + 0.01 * (rng() - 0.5);
28 const jitT = 1 + 0.2 * (rng() - 0.5);
29 if (kind === 0) {
30 const f = 300 * jitF, tau = 0.06 * jitT;
31 for (let i = 0; i < post; i++) {
32 const t = i / FS;
33 s[pre + i] = Math.exp(-t / tau) * Math.sin(2 * Math.PI * f * t) * 0.8;
34 }
35 } else if (kind === 1) {
36 const f = 1400 * jitF, tau = 0.025 * jitT;
37 for (let i = 0; i < post; i++) {
38 const t = i / FS;
39 s[pre + i] = Math.exp(-t / tau) * Math.sin(2 * Math.PI * f * t) * 0.8;
40 }
41 } else {
42 const tau = 0.012 * jitT;
43 for (let i = 0; i < post; i++) {
44 const t = i / FS;
45 s[pre + i] = Math.exp(-t / tau) * (burstBase[i] + 0.15 * (rng() * 2 - 1)) * 0.7;
46 }
47 }
48 for (let i = 0; i < snipLen; i++) s[i] += (rng() * 2 - 1) * 0.002;
49 return s;
50 }
52 const lib: SnippetLibrary = { names: ['low', 'high', 'burst'], pre, post, snippets: [] };
53 for (let k = 0; k < 3; k++) {
54 for (let e = 0; e < perType; e++) lib.snippets.push({ typeIndex: k, samples: makeSnippet(k) });
55 }
56 return lib;
↑↓ move↵openescclose