@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 84 lines 3.06 KB
Raw
1 import { PcmResampler } from "./pcmResampler";
2
3 declare const sampleRate: number;
4 declare class AudioWorkletProcessor {
5 readonly port: MessagePort;
6 }
7 declare function registerProcessor(name: string, processor: typeof AudioWorkletProcessor): void;
8
9 class PcmCaptureProcessor extends AudioWorkletProcessor {
10 private readonly resampler = new PcmResampler(sampleRate);
11 private chunk = new Float32Array(8_000);
12 private length = 0;
13 private recording = false;
14 private flushed = false;
15 private samples = 0;
16 private levelSamples = 0;
17 private levelEnergy = 0;
18 private level = 0;
19 private readonly meterOnly: boolean;
20
21 constructor(options?: { readonly processorOptions?: { readonly meterOnly?: boolean } }) {
22 super();
23 this.meterOnly = options?.processorOptions?.meterOnly === true;
24 this.port.onmessage = ({ data }) => {
25 if (data === "start") this.recording = true;
26 if (data === "stop") {
27 this.recording = false;
28 if (!this.meterOnly && !this.flushed) this.append(this.resampler.flush());
29 this.flushed = true;
30 if (this.length) this.publish();
31 // oxlint-disable-next-line unicorn/require-post-message-target-origin -- AudioWorklet MessagePorts do not accept a target origin
32 this.port.postMessage("stopped");
33 }
34 };
35 }
36
37 private publish() {
38 const pcm = this.chunk.slice(0, this.length);
39 this.port.postMessage(pcm, [pcm.buffer]);
40 this.length = 0;
41 }
42
43 private append(pcm: Float32Array) {
44 for (const sample of pcm) {
45 if (this.samples === 16_000 * 300) {
46 this.recording = false;
47 // oxlint-disable-next-line unicorn/require-post-message-target-origin -- AudioWorklet MessagePorts do not accept a target origin
48 this.port.postMessage("limit");
49 break;
50 }
51 this.chunk[this.length++] = sample;
52 this.samples++;
53 if (this.length === this.chunk.length) this.publish();
54 }
55 }
56
57 process(inputs: Float32Array[][]): boolean {
58 if (!this.recording) return true;
59 const channels = inputs[0];
60 if (!channels?.[0]) return true;
61 const mono = new Float32Array(channels[0].length);
62 for (const channel of channels)
63 for (let i = 0; i < mono.length; i++) mono[i]! += channel[i]! / channels.length;
64 // Metering must not wait for the half-second transcription chunks.
65 for (const sample of mono) {
66 this.levelEnergy += sample * sample;
67 this.levelSamples++;
68 if (this.levelSamples >= sampleRate / 20) {
69 const target = Math.min(1, Math.sqrt(this.levelEnergy / this.levelSamples) * 4);
70 // Soften brief peaks without delaying meter updates or transcription.
71 const timeConstant = target > this.level ? 0.075 : 0.2;
72 const decay = Math.exp(-this.levelSamples / sampleRate / timeConstant);
73 this.level = target + (this.level - target) * decay;
74 this.port.postMessage(this.level, []);
75 this.levelEnergy = 0;
76 this.levelSamples = 0;
77 }
78 }
79 if (!this.meterOnly) this.append(this.resampler.push(mono));
80 return true;
81 }
82 }
83
84 registerProcessor("t3-pcm-capture", PcmCaptureProcessor);