Download worklet.js from OpenVoiceOS/wakehubert-wakewords-space: direct link, hf CLI and curl.
- Browser
- Download file 2.66 kB
-
https://huggingface.co/spaces/OpenVoiceOS/wakehubert-wakewords-space/resolve/main/worklet.js
- Command line
-
hf download hf://spaces/OpenVoiceOS/wakehubert-wakewords-space/worklet.js
-
curl -L -o worklet.js https://huggingface.co/spaces/OpenVoiceOS/wakehubert-wakewords-space/resolve/main/worklet.js
2.66 kB
| // Microphone capture: mono 16 kHz audio posted to the page in 1280-sample (80 ms) blocks. | |
| // When the context does not run at 16 kHz the audio is resampled here with a | |
| // Blackman-windowed sinc low-pass at 7.6 kHz. | |
| const TARGET = 16000; | |
| const BLOCK = 1280; | |
| class Capture extends AudioWorkletProcessor { | |
| constructor() { | |
| super(); | |
| this.ratio = sampleRate / TARGET; | |
| this.block = new Float32Array(BLOCK); | |
| this.fill = 0; | |
| if (this.ratio !== 1) { | |
| this.half = Math.ceil(16 * this.ratio); // taps each side, in input samples | |
| this.cut = (0.475 * TARGET) / sampleRate; // cutoff in cycles per input sample | |
| this.hist = new Float32Array(0); // pending input, hist[0] is input index this.base | |
| this.base = 0; | |
| this.next = 0; // next output position, in input samples | |
| } | |
| this.port.postMessage({type: "rate", sampleRate}); | |
| } | |
| emit(x) { | |
| this.block[this.fill++] = x; | |
| if (this.fill === BLOCK) { | |
| this.port.postMessage({type: "block", samples: this.block}, [this.block.buffer]); | |
| this.block = new Float32Array(BLOCK); | |
| this.fill = 0; | |
| } | |
| } | |
| kernel(d) { | |
| if (d <= -this.half || d >= this.half) return 0; | |
| const x = 2 * this.cut * d; | |
| const sinc = d === 0 ? 1 : Math.sin(Math.PI * x) / (Math.PI * x); | |
| const w = 0.42 + 0.5 * Math.cos(Math.PI * d / this.half) + 0.08 * Math.cos(2 * Math.PI * d / this.half); | |
| return 2 * this.cut * sinc * w; | |
| } | |
| resample(input) { | |
| const merged = new Float32Array(this.hist.length + input.length); | |
| merged.set(this.hist); | |
| merged.set(input, this.hist.length); | |
| const end = this.base + merged.length; | |
| while (this.next + this.half < end) { | |
| const c = Math.floor(this.next); | |
| let acc = 0; | |
| for (let k = c - this.half + 1; k <= c + this.half; k++) { | |
| const i = k - this.base; | |
| if (i >= 0) acc += merged[i] * this.kernel(this.next - k); | |
| } | |
| this.emit(acc); | |
| this.next += this.ratio; | |
| } | |
| const keepFrom = Math.max(0, Math.floor(this.next) - this.half - this.base); | |
| this.hist = merged.slice(keepFrom); | |
| this.base += keepFrom; | |
| } | |
| process(inputs) { | |
| const input = inputs[0]; | |
| if (input && input.length) { | |
| let mono = input[0]; | |
| if (input.length > 1) { | |
| mono = new Float32Array(input[0].length); | |
| for (const ch of input) for (let i = 0; i < ch.length; i++) mono[i] += ch[i] / input.length; | |
| } | |
| if (this.ratio === 1) for (let i = 0; i < mono.length; i++) this.emit(mono[i]); | |
| else this.resample(mono); | |
| } | |
| return true; | |
| } | |
| } | |
| registerProcessor("wake-capture", Capture); | |