sailfork / t3code-github / apps/ web/ src/ speech/ pcmWorklet.ts

import { PcmResampler } from "./pcmResampler";

declare const sampleRate: number;
declare class AudioWorkletProcessor {
  readonly port: MessagePort;
}
declare function registerProcessor(name: string, processor: typeof AudioWorkletProcessor): void;

class PcmCaptureProcessor extends AudioWorkletProcessor {
  private readonly resampler = new PcmResampler(sampleRate);
  private chunk = new Float32Array(8_000);
  private length = 0;
  private recording = false;
  private flushed = false;
  private samples = 0;
  private levelSamples = 0;
  private levelEnergy = 0;
  private level = 0;
  private readonly meterOnly: boolean;

  constructor(options?: { readonly processorOptions?: { readonly meterOnly?: boolean } }) {
    super();
    this.meterOnly = options?.processorOptions?.meterOnly === true;
    this.port.onmessage = ({ data }) => {
      if (data === "start") this.recording = true;
      if (data === "stop") {
        this.recording = false;
        if (!this.meterOnly && !this.flushed) this.append(this.resampler.flush());
        this.flushed = true;
        if (this.length) this.publish();
        // oxlint-disable-next-line unicorn/require-post-message-target-origin -- AudioWorklet MessagePorts do not accept a target origin
        this.port.postMessage("stopped");
      }
    };
  }

  private publish() {
    const pcm = this.chunk.slice(0, this.length);
    this.port.postMessage(pcm, [pcm.buffer]);
    this.length = 0;
  }

  private append(pcm: Float32Array) {
    for (const sample of pcm) {
      if (this.samples === 16_000 * 300) {
        this.recording = false;
        // oxlint-disable-next-line unicorn/require-post-message-target-origin -- AudioWorklet MessagePorts do not accept a target origin
        this.port.postMessage("limit");
        break;
      }
      this.chunk[this.length++] = sample;
      this.samples++;
      if (this.length === this.chunk.length) this.publish();
    }
  }

  process(inputs: Float32Array[][]): boolean {
    if (!this.recording) return true;
    const channels = inputs[0];
    if (!channels?.[0]) return true;
    const mono = new Float32Array(channels[0].length);
    for (const channel of channels)
      for (let i = 0; i < mono.length; i++) mono[i]! += channel[i]! / channels.length;
    // Metering must not wait for the half-second transcription chunks.
    for (const sample of mono) {
      this.levelEnergy += sample * sample;
      this.levelSamples++;
      if (this.levelSamples >= sampleRate / 20) {
        const target = Math.min(1, Math.sqrt(this.levelEnergy / this.levelSamples) * 4);
        // Soften brief peaks without delaying meter updates or transcription.
        const timeConstant = target > this.level ? 0.075 : 0.2;
        const decay = Math.exp(-this.levelSamples / sampleRate / timeConstant);
        this.level = target + (this.level - target) * decay;
        this.port.postMessage(this.level, []);
        this.levelEnergy = 0;
        this.levelSamples = 0;
      }
    }
    if (!this.meterOnly) this.append(this.resampler.push(mono));
    return true;
  }
}

registerProcessor("t3-pcm-capture", PcmCaptureProcessor);