"""Incoming PCM to 16 kHz mono float32, and clip normalisation before transcription.""" from __future__ import annotations import numpy as np TARGET_RATE = 16000 _TAPS = 97 # windowed-sinc low-pass for the 48 kHz -> 16 kHz decimation _CUTOFF_HZ = 7200 # below the new 8 kHz Nyquist def _lowpass(rate: int) -> np.ndarray: n = np.arange(_TAPS) - (_TAPS - 1) / 2 fc = _CUTOFF_HZ / rate h = 2 * fc * np.sinc(2 * fc * n) * np.blackman(_TAPS) return (h / h.sum()).astype(np.float64) class InputConverter: """Streaming converter for one speaker. Keeps filter history and odd bytes between frames.""" def __init__(self, rate: int, channels: int): if rate not in (16000, 48000): raise ValueError("audio rate must be 16000 or 48000") if channels not in (1, 2): raise ValueError("audio channels must be 1 or 2") self.channels = channels self.factor = rate // TARGET_RATE self._carry = b"" if self.factor > 1: self._h = _lowpass(rate) self._hist = np.zeros(_TAPS - 1, dtype=np.float64) self._phase = 0 # input samples to skip before the next kept output def push(self, pcm: bytes) -> np.ndarray: data = self._carry + pcm frame_bytes = 2 * self.channels usable = len(data) - len(data) % frame_bytes self._carry = data[usable:] x = np.frombuffer(data[:usable], dtype=" np.ndarray: """Bring a clip to the same loudness whoever spoke it, without clipping. ``voiced`` marks the 512-sample windows the VAD heard as speech; the level is measured on them so silence, a click or a knock does not decide the gain. Without it the louder half of the windows is used. Low rumble is removed first and peaks are soft-limited after the gain. """ x = np.asarray(x, dtype=np.float32) if len(x) == 0: return x spec = np.fft.rfft(x.astype(np.float64)) spec[np.fft.rfftfreq(len(x), 1 / TARGET_RATE) < HIGHPASS_HZ] = 0 y = np.fft.irfft(spec, len(x)) n_win = -(-len(y) // _WINDOW) frames = np.pad(y, (0, n_win * _WINDOW - len(y))).reshape(n_win, _WINDOW) energy = (frames ** 2).mean(axis=1) if voiced is not None and len(voiced) == n_win and voiced.any(): sel = energy[voiced] else: sel = energy[energy >= np.median(energy)] level_db = 10 * np.log10(sel.mean() + 1e-20) gain_db = float(np.clip(TARGET_DB - level_db, MIN_GAIN_DB, MAX_GAIN_DB)) y = y * 10 ** (gain_db / 20) over = np.abs(y) > _KNEE room = _CEILING - _KNEE y[over] = np.sign(y[over]) * (_KNEE + room * np.tanh((np.abs(y[over]) - _KNEE) / room)) return y.astype(np.float32)