b11b069Classify takes with numbl algorithms by default (silence_gap_knn from audio-spike-sorting, noise-floor adapted), with a custom-algorithm editor and the feature kNN kept as an optionJeremy Magland 1/// <reference lib="webworker" />
2/** Runs the classifier off the main thread so a slow or broken custom
3 * algorithm cannot freeze the page. One request, one response. */
4import { runAlgorithm } from './execute.ts';
5import type { Detection, TrainingArrays } from './types.ts';
7export interface ClfRequest {
8 id: number;
9 source: string;
10 x: Float32Array;
11 fs: number;
12 training: TrainingArrays;
13}
15export interface ClfResponse {
16 id: number;
17 ok: boolean;
18 detections?: Detection[];
19 logs: string[];
20 error?: string;
21}
23self.onmessage = (e: MessageEvent<ClfRequest>) => {
24 const { id, source, x, fs, training } = e.data;
25 try {
26 const out = runAlgorithm(source, x, fs, training);
27 const resp: ClfResponse = { id, ok: true, ...out };
28 self.postMessage(resp);
29 } catch (err) {
30 const resp: ClfResponse = {
31 id, ok: false, logs: [],
32 error: err instanceof Error ? err.message : String(err),
33 };
34 self.postMessage(resp);
35 }
36};