Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

2.4 KB
Raw Blame
const SILENCE_RMS_THRESHOLD = 0.002;
const SPEECH_WINDOW_RMS_THRESHOLD = 0.003;
const SPEECH_WINDOW_PEAK_THRESHOLD = 0.02;
const STRONG_SPEECH_RMS_THRESHOLD = 0.006;

export const createLocalSpeechGateState = () => ({
  peakRms: 0,
  peakAmplitude: 0,
  windowCount: 0,
  speechWindowCount: 0,
  consecutiveSpeechWindows: 0,
  maxConsecutiveSpeechWindows: 0,
});

export const recordLocalSpeechWindow = (state, rms, peak) => {
  if (!state) {
    return null;
  }

  state.windowCount += 1;
  state.peakRms = Math.max(state.peakRms, rms);
  state.peakAmplitude = Math.max(state.peakAmplitude, peak);

  const isSpeechWindow = rms >= SPEECH_WINDOW_RMS_THRESHOLD && peak >= SPEECH_WINDOW_PEAK_THRESHOLD;
  if (!isSpeechWindow) {
    state.consecutiveSpeechWindows = 0;
    return state;
  }

  state.speechWindowCount += 1;
  state.consecutiveSpeechWindows += 1;
  state.maxConsecutiveSpeechWindows = Math.max(
    state.maxConsecutiveSpeechWindows,
    state.consecutiveSpeechWindows
  );
  return state;
};

// The streaming worklet's PCM16 chunks, quantized to the batch analyser's
// getByteTimeDomainData bytes (floor(128·(1 + x))) so a failed-over recording
// is skipped exactly when batch would skip it. At full resolution a quiet mic
// reads as silence that batch never sees.
export const recordPcm16SpeechWindow = (state, buffer) => {
  const samples = new Int16Array(buffer);
  if (!samples.length) return state;
  let sum = 0;
  let peak = 0;
  for (const sample of samples) {
    const byte = Math.floor(128 * (1 + sample / 0x8000));
    const value = (byte - 128) / 128;
    sum += value * value;
    peak = Math.max(peak, Math.abs(value));
  }
  return recordLocalSpeechWindow(state, Math.sqrt(sum / samples.length), peak);
};

export const getLocalSpeechGateDecision = (state) => {
  if (!state?.windowCount) {
    return { skip: false, reason: "unavailable" };
  }

  const metrics = {
    peakRms: state.peakRms,
    peakAmplitude: state.peakAmplitude,
    windowCount: state.windowCount,
    speechWindowCount: state.speechWindowCount,
    maxConsecutiveSpeechWindows: state.maxConsecutiveSpeechWindows,
  };

  if (state.peakRms < SILENCE_RMS_THRESHOLD) {
    return { skip: true, reason: "silence", ...metrics };
  }

  const hasSpeech = state.speechWindowCount >= 1 || state.peakRms >= STRONG_SPEECH_RMS_THRESHOLD;

  if (!hasSpeech) {
    return { skip: true, reason: "insufficient_speech", ...metrics };
  }

  return { skip: false, reason: "speech_detected", ...metrics };
};