sava-telephony / scripts /channels.py
matthiastl's picture
SAVA fine-tuned for phone lines: PyTorch, ONNX and 8-bit ONNX, with tests and scripts
547c405
Raw History Blame Contribute Delete
3.13 kB
"""Phone lines to hear audio through: fixed ones to test on, a random family to train on.
Every line takes 16 kHz float audio and returns 16 kHz float32 audio of the same length.
clean the audio as it is
phone a landline: 300-3400 Hz, G.711 mu-law (``telephony.py``, the published test condition)
gsm a 2G mobile call: 300-3400 Hz, GSM 06.10 full rate; never used in training
"""
from __future__ import annotations
import io
import numpy as np
import soundfile
from scipy.signal import butter, resample_poly, sosfilt
from telephony import telephone_channel
NARROW_RATE = 8000
def through_codec(narrow: np.ndarray, codec: str) -> np.ndarray:
"""8 kHz audio encoded and decoded by one of libsndfile's WAV codecs; same length back."""
buffer = io.BytesIO()
soundfile.write(buffer, np.clip(narrow, -1.0, 32767 / 32768), NARROW_RATE, format="WAV", subtype=codec)
buffer.seek(0)
decoded = soundfile.read(buffer, dtype="float64")[0]
if decoded.size < narrow.size:
decoded = np.pad(decoded, (0, narrow.size - decoded.size))
return decoded[: narrow.size]
def narrowband(
samples: np.ndarray,
*,
low: float = 300.0,
high: float = 3400.0,
order: int = 4,
codec: str | None = "ULAW",
level_db: float = 0.0,
noise_db: float | None = None,
rng: np.random.Generator | None = None,
) -> np.ndarray:
"""Audio through an 8 kHz line: band-pass, optional line noise, a codec, back to 16 kHz.
``level_db`` moves the level going into the codec and is undone afterwards, so it changes how
coarse the codec's steps are relative to the speech, not how loud the result is. ``noise_db``
is white noise in dB relative to full scale, added before the codec.
"""
narrow = resample_poly(samples.astype(np.float64), 1, 2)
narrow = sosfilt(butter(order, [low, high], btype="bandpass", fs=NARROW_RATE, output="sos"), narrow)
gain = 10.0 ** (level_db / 20.0)
narrow = narrow * gain
if noise_db is not None:
narrow = narrow + rng.standard_normal(narrow.size) * 10.0 ** (noise_db / 20.0)
if codec is not None:
narrow = through_codec(narrow, codec)
return resample_poly(narrow / gain, 2, 1)[: samples.size].astype(np.float32)
def clean(samples: np.ndarray) -> np.ndarray:
return samples
def gsm(samples: np.ndarray) -> np.ndarray:
"""A 2G mobile call. Held out: training never uses this codec."""
return narrowband(samples, codec="GSM610")
def random_line(samples: np.ndarray, rng: np.random.Generator) -> np.ndarray:
"""A landline drawn at random, for training: G.711 mu-law or A-law or no codec, varied band and level."""
return narrowband(
samples,
low=rng.uniform(150.0, 400.0),
high=rng.uniform(3000.0, 3700.0),
order=int(rng.choice([2, 4, 6])),
codec=("ULAW", "ALAW", None)[rng.choice(3, p=[0.45, 0.45, 0.10])],
level_db=rng.uniform(-20.0, 0.0),
noise_db=rng.uniform(-70.0, -50.0) if rng.random() < 0.3 else None,
rng=rng,
)
TEST_CONDITIONS = {"clean": clean, "phone": telephone_channel, "gsm": gsm}