Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

20.0 KB
Raw Blame
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

"""Measure a take: duration, integrated loudness (ITU-R BS.1770-4), true peak, clipping and the span that holds sound, plus a word match against the text it should say. numpy only, so no new dependency."""

from __future__ import annotations

import functools
import math
import re
import struct
import unicodedata
import wave
from pathlib import Path
from typing import Any, Optional

import numpy as np

# BS.1770 gating: 400 ms blocks with 75% overlap, an absolute gate at -70 LUFS and a relative gate 10 LU below the ungated mean.
_BLOCK_S = 0.4
_HOP_S = 0.1
_ABSOLUTE_GATE = -70.0
_RELATIVE_GATE = 10.0
# The K-weighting high-pass rings for well under a second; past this many seconds its impulse response is below float precision.
_IR_SECONDS = 1.0
# Walks audio in _CHUNK-frame slices, so a long take never needs full-length float64 copies.
_CHUNK = 1 << 18
# 3+ identical full-scale samples reached or left by a jump no smooth peak makes; step capped at
# _CLIP_STEP_MAX so 8-bit steps cannot hide overdrive; _CLIP_LEVEL 0.99 lets 127/128 count.
_CLIP_LEVEL = 0.99
_CLIP_RUN = 3
_CLIP_EDGE = 2
_CLIP_STEP_MAX = 2.0**-13
# Sound span: 20 ms frames louder than 40 dB under the loudest frame, never below -60 dBFS.
_FRAME_S = 0.02
_SPAN_RANGE_DB = 40.0
_SPAN_FLOOR_DB = -60.0


def read_wav(path: Path) -> tuple[np.ndarray, int]:
    """(frames x channels float32, sample rate) for an 8/16/24/32-bit PCM or 32/64-bit float WAV."""
    data, rate, _step = _read_pcm(path)
    return data, rate


def _float_layout(path: Path) -> Optional[tuple[int, int, str, int, int]]:
    """Layout (rate, channels, dtype, data offset, frames) of an IEEE float WAV, else None; wave
    reads PCM only, and a 16-bit decode would clamp peaks and invent clipping."""
    size = path.stat().st_size
    with open(path, "rb") as f:
        head = f.read(12)
        if len(head) < 12 or head[:4] != b"RIFF" or head[8:12] != b"WAVE":
            return None
        fmt = None
        while True:
            chunk = f.read(8)
            if len(chunk) < 8:
                return None
            name, length = chunk[:4], struct.unpack("<I", chunk[4:])[0]
            if name == b"fmt ":
                body = f.read(length)
                if len(body) < 16:
                    return None
                tag, channels, rate, _byte_rate, _align, bits = struct.unpack("<HHIIHH", body[:16])
                if tag == 0xFFFE and len(body) >= 26:
                    tag = struct.unpack("<H", body[24:26])[0]
                if tag != 3 or bits not in (32, 64) or not channels or not rate:
                    return None
                fmt = (rate, channels, "<f4" if bits == 32 else "<f8")
                f.seek(length % 2, 1)
            elif name == b"data":
                if fmt is None:
                    return None
                offset = f.tell()
                # A streamed writer leaves the size at 0 or 0xFFFFFFFF; the file length is the truth.
                if length == 0 or length > size - offset:
                    length = size - offset
                width = np.dtype(fmt[2]).itemsize * fmt[1]
                return fmt[0], fmt[1], fmt[2], offset, length // width
            else:
                f.seek(length + (length % 2), 1)


def wav_seconds(path: Path) -> float:
    """Duration of a PCM or IEEE float WAV; raises wave.Error, EOFError or ValueError otherwise."""
    layout = _float_layout(path)
    if layout is not None:
        return layout[4] / layout[0]
    with wave.open(str(path)) as w:
        rate = w.getframerate()
        return w.getnframes() / rate if rate else 0.0


def _read_pcm(path: Path) -> tuple[np.ndarray, int, float]:
    """``read_wav`` plus the size of one quantization step (0 for float samples)."""
    layout = _float_layout(path)
    if layout is not None:
        rate, channels, dtype, offset, frames = layout
        data = np.fromfile(path, dtype = dtype, count = frames * channels, offset = offset)
        return data.astype(np.float32).reshape(-1, channels), rate, 0.0
    with wave.open(str(path)) as w:
        rate = w.getframerate()
        channels = w.getnchannels()
        width = w.getsampwidth()
        raw = w.readframes(w.getnframes())
    if width == 1:
        data = (np.frombuffer(raw, dtype = np.uint8).astype(np.float32) - 128.0) / 128.0
    elif width == 2:
        data = np.frombuffer(raw, dtype = "<i2").astype(np.float32) / 32768.0
    elif width == 3:
        bytes3 = np.frombuffer(raw, dtype = np.uint8).reshape(-1, 3)
        ints = (
            bytes3[:, 0].astype(np.int32)
            | (bytes3[:, 1].astype(np.int32) << 8)
            | (bytes3[:, 2].astype(np.int32) << 16)
        )
        ints = np.where(ints >= 1 << 23, ints - (1 << 24), ints)
        data = ints.astype(np.float32) / float(1 << 23)
    elif width == 4:
        data = np.frombuffer(raw, dtype = "<i4").astype(np.float32) / float(1 << 31)
    else:
        raise ValueError(f"Unsupported WAV sample width: {width * 8} bits")
    # float32 holds samples near full scale 2**-24 apart, coarser than 32-bit PCM's own step.
    return data.reshape(-1, max(channels, 1)), rate, max(2.0 ** (1 - 8 * width), 2.0**-24)


def _biquad_coefficients(rate: int) -> list[tuple[np.ndarray, np.ndarray]]:
    """The two K-weighting stages (high shelf, then RLB high-pass) at any rate, from the BS.1770 analog prototypes."""
    stages = []
    # Stage 1: high shelf, +4 dB above about 1.7 kHz.
    f0, gain_db, q = 1681.974450955533, 3.999843853973347, 0.7071752369554196
    k = math.tan(math.pi * f0 / rate)
    vh = 10.0 ** (gain_db / 20.0)
    vb = vh**0.4996667741545416
    a0 = 1.0 + k / q + k * k
    b = np.array(
        [(vh + vb * k / q + k * k) / a0, 2.0 * (k * k - vh) / a0, (vh - vb * k / q + k * k) / a0]
    )
    a = np.array([1.0, 2.0 * (k * k - 1.0) / a0, (1.0 - k / q + k * k) / a0])
    stages.append((b, a))
    # Stage 2: second-order high-pass at about 38 Hz.
    f0, q = 38.13547087602444, 0.5003270373238773
    k = math.tan(math.pi * f0 / rate)
    a0 = 1.0 + k / q + k * k
    b = np.array([1.0, -2.0, 1.0])
    a = np.array([1.0, 2.0 * (k * k - 1.0) / a0, (1.0 - k / q + k * k) / a0])
    stages.append((b, a))
    return stages


@functools.lru_cache(maxsize = 8)
def _impulse_response(rate: int) -> np.ndarray:
    """The K-weighting filter's impulse response, truncated where it has died out. Cached per rate:
    the filter runs sample by sample in Python. Callers must not modify it."""
    n = max(int(rate * _IR_SECONDS), 64)
    x = np.zeros(n)
    x[0] = 1.0
    for b, a in _biquad_coefficients(rate):
        y = np.zeros(n)
        x1 = x2 = y1 = y2 = 0.0
        b0, b1, b2 = b
        a1, a2 = a[1], a[2]
        for i in range(n):
            xi = x[i]
            yi = b0 * xi + b1 * x1 + b2 * x2 - a1 * y1 - a2 * y2
            x2, x1 = x1, xi
            y2, y1 = y1, yi
            y[i] = yi
        x = y
    return x


def _fft_convolve(signal: np.ndarray, kernel: np.ndarray):
    """``signal`` (1-D) convolved with ``kernel`` by overlap-add, cut to the signal's length and
    yielded in order, one chunk at a time."""
    m = len(kernel)
    size = 1 << int(math.ceil(math.log2(_CHUNK + m - 1)))
    kernel_f = np.fft.rfft(kernel, size)
    tail = np.zeros(m - 1)
    for start in range(0, len(signal), _CHUNK):
        block = signal[start : start + _CHUNK].astype(np.float64)
        piece = np.fft.irfft(np.fft.rfft(block, size) * kernel_f, size)[: len(block) + m - 1]
        piece[: m - 1] += tail
        yield piece[: len(block)]
        tail = piece[len(block) :]


def _padded(x: np.ndarray, start: int, stop: int) -> np.ndarray:
    """``x[start:stop]`` as float64, zero where the range runs past either end."""
    out = np.zeros(stop - start)
    lo, hi = max(start, 0), min(stop, len(x))
    if hi > lo:
        out[lo - start : hi - start] = x[lo:hi]
    return out


def integrated_loudness(data: np.ndarray, rate: int) -> Optional[float]:
    """Gated integrated loudness in LUFS, or None when every block sits under the absolute gate (silence, or shorter than one block)."""
    block = int(round(_BLOCK_S * rate))
    hop = max(int(round(_HOP_S * rate)), 1)
    if data.shape[0] < block or block <= 0:
        return None
    kernel = _impulse_response(rate)
    count = 1 + (data.shape[0] - block) // hop
    starts = np.arange(count) * hop
    # The running sum of squares is only read at block edges, so only those points are kept.
    edges = np.unique(np.concatenate((starts, starts + block)))
    first, last = np.searchsorted(edges, starts), np.searchsorted(edges, starts + block)
    powers = np.zeros(count)
    for channel in range(data.shape[1]):
        sums = np.zeros(edges.size)  # sums[i]: energy of the first edges[i] filtered samples
        total, done = 0.0, 0
        for piece in _fft_convolve(data[:, channel], kernel):
            running = np.cumsum(piece * piece) + total
            lo = np.searchsorted(edges, done, side = "right")
            hi = np.searchsorted(edges, done + piece.size, side = "right")
            sums[lo:hi] = running[edges[lo:hi] - done - 1]
            total, done = float(running[-1]), done + piece.size
        # Channel weights are 1.0 for mono, L, R and C; surround layouts are not something audio runs produce.
        powers += (sums[last] - sums[first]) / block
    with np.errstate(divide = "ignore"):
        loudness = -0.691 + 10.0 * np.log10(powers)
    kept = powers[loudness > _ABSOLUTE_GATE]
    if kept.size == 0:
        return None
    relative = -0.691 + 10.0 * math.log10(kept.mean()) - _RELATIVE_GATE
    with np.errstate(divide = "ignore"):
        kept = kept[-0.691 + 10.0 * np.log10(kept) > relative]
    if kept.size == 0:
        return None
    return -0.691 + 10.0 * math.log10(kept.mean())


def _oversampler(factor: int = 4, taps_per_phase: int = 12) -> np.ndarray:
    """A windowed-sinc interpolation filter, shaped (factor, taps_per_phase) by phase."""
    length = factor * taps_per_phase
    n = np.arange(length) - (length - 1) / 2.0
    taps = np.sinc(n / factor) * np.kaiser(length, 8.0)
    taps *= factor / taps.sum()
    return taps.reshape(taps_per_phase, factor).T


# Past this many candidate spans per phase the whole chunk is convolved instead.
_PEAK_SPANS_MAX = 128


def _phase_peak(
    window: np.ndarray, taps: np.ndarray, peak: float, screen: Optional[np.ndarray], err: float
) -> float:
    """max(peak, max |np.convolve(window, taps, "valid")|), convolving only where ``screen`` (+-err) allows."""
    if screen is None:
        return max(peak, float(np.max(np.abs(np.convolve(window, taps, mode = "valid")))))
    top = float(screen.max())
    # |output j - screen[j]| <= err: below top - 2 err cannot be the max, below peak - err cannot raise it.
    floor = max(top - 2.0 * err, peak - err)
    if floor > top:
        return peak
    idx = np.flatnonzero(screen >= floor)
    length = len(taps)
    breaks = np.flatnonzero(np.diff(idx) > length) + 1
    if breaks.size >= _PEAK_SPANS_MAX:
        return _phase_peak(window, taps, peak, None, err)
    firsts = idx[np.concatenate(([0], breaks))].tolist()
    lasts = idx[np.concatenate((breaks - 1, [idx.size - 1]))].tolist()
    for first, last in zip(firsts, lasts):
        # Same memory, same dot product per output as the whole-chunk np.convolve.
        part = np.convolve(window[first : last + length], taps, mode = "valid")
        peak = max(peak, float(np.max(np.abs(part))))
    return peak


def true_peak(data: np.ndarray) -> float:
    """The largest absolute sample after 4x oversampling (BS.1770 Annex 2), linear."""
    phases = _oversampler()
    length = phases.shape[1]
    # np.convolve's "same" output i reads input i - before .. i + after.
    after = (length - 1) // 2
    before = length - 1 - after
    # np.convolve(window, taps, "valid")[j] is sum(taps[k] * window[j + length - 1 - k]).
    flipped = phases[:, ::-1].astype(np.float32)
    reach = float(np.abs(phases).sum(axis = 1).max())
    peak = 0.0
    for channel in range(data.shape[1]):
        x = data[:, channel]
        for start in range(0, len(x), _CHUNK):
            stop = min(start + _CHUNK, len(x))
            peak = max(peak, float(np.max(np.abs(x[start:stop]))))
            window = _padded(x, start - before, stop + after)
            scale = float(np.max(np.abs(window)))
            screens, err = [None] * len(phases), 0.0
            if 2.0**-60 <= scale <= 2.0**60:
                # Screen and np.convolve each lie within ~14 * 2**-24 * reach * scale of the exact
                # sum in any summation order; 2**-19 leaves 2x margin.
                err = reach * scale * 2.0**-19
                samples = window.astype(np.float32, copy = False)
                cols = len(window) - length + 1
                # Plain ufuncs, not BLAS: independent of the caller's BLAS thread setting.
                screens = flipped[:, :1] * samples[:cols]
                term = np.empty_like(screens)
                for k in range(1, length):
                    screens += np.multiply(flipped[:, k : k + 1], samples[k : k + cols], out = term)
                np.abs(screens, out = screens)
            for taps, screen in zip(phases, screens):
                peak = _phase_peak(window, taps, peak, screen, err)
    return peak


def _steep_samples(x: np.ndarray, first: np.ndarray, length: np.ndarray, step: float) -> int:
    """Samples in the runs (start ``first``, ``length`` samples) of ``x`` that are long enough and
    that the waveform reaches or leaves more steeply than a smooth peak could."""
    keep = length >= _CLIP_RUN
    first, length = first[keep], length[keep]
    if not first.size:
        return 0
    last = first + length - 1
    top = x[first].astype(np.float64)
    # The larger jump onto or off the run; -1 at an end of the audio, where there is none, and
    # a run that fills the whole channel counts.
    into = np.where(first > 0, np.abs(top - x[np.maximum(first - 1, 0)]), -1.0)
    out = np.where(last < len(x) - 1, np.abs(top - x[np.minimum(last + 1, len(x) - 1)]), -1.0)
    edge = np.maximum(into, out)
    limit = _CLIP_EDGE * min(step, _CLIP_STEP_MAX) * ((length + 1) / (length - 1)) ** 2
    return int(length[(edge > limit) | (edge < 0)].sum())


def clipped_samples(data: np.ndarray, step: float = 0.0) -> int:
    """Identical full-scale runs of at least _CLIP_RUN samples per channel, reached or left more
    steeply than a smooth peak could; step is the quantization step (0 for float)."""
    total = 0
    for channel in range(data.shape[1]):
        x = data[:, channel]
        open_start, open_length = 0, 0  # the run still open at the end of the last chunk
        for start in range(0, len(x), _CHUNK):
            # One sample of overlap, so a run that crosses chunks stays one run.
            lead = 1 if start else 0
            seg = x[start - lead : start + _CHUNK]
            rail = np.abs(seg) >= _CLIP_LEVEL
            same = np.zeros(len(seg), dtype = bool)
            same[1:] = rail[1:] & rail[:-1] & (seg[1:] == seg[:-1])
            rail, same = rail[lead:], same[lead:]
            # Each rail sample not continuing a run opens one; id 0 is the carried-in run.
            opens = rail & ~same
            ids = np.cumsum(opens)
            counts = np.bincount(ids[rail], minlength = int(ids[-1]) + 1)
            counts[0] += open_length
            firsts = np.concatenate(([open_start], np.flatnonzero(opens) + start))
            if rail[-1]:
                open_start, open_length = int(firsts[-1]), int(counts[-1])
                firsts, counts = firsts[:-1], counts[:-1]
            else:
                open_start, open_length = 0, 0
            total += _steep_samples(x, firsts, counts, step)
        total += _steep_samples(x, np.array([open_start]), np.array([open_length]), step)
    return total


def _db(value: float) -> Optional[float]:
    return round(20.0 * math.log10(value), 2) if value > 0 else None


def sound_span(data: np.ndarray, rate: int) -> Optional[tuple[float, float]]:
    """(start_s, end_s) of the part that holds sound, or None for silence."""
    if rate <= 0:
        return None
    frame = max(int(rate * _FRAME_S), 1)
    total = data.shape[0]
    count = -(-total // frame)  # the last, shorter frame counts too
    if count == 0:
        return None
    # Energy over every channel, so channels in opposite phase do not cancel out.
    rms = np.empty(count)
    full = total // frame
    step = max(_CHUNK // frame, 1)
    for lo in range(0, full, step):
        hi = min(lo + step, full)
        frames = data[lo * frame : hi * frame].astype(np.float64)
        frames = frames.reshape(hi - lo, frame, data.shape[1])
        rms[lo:hi] = np.sqrt(np.mean(frames * frames, axis = (1, 2)))
    if full < count:
        tail = data[full * frame :].astype(np.float64)
        rms[full] = np.sqrt(np.mean(tail * tail))
    loudest = float(rms.max())
    if loudest <= 0:
        return None
    floor = max(20.0 * math.log10(loudest) - _SPAN_RANGE_DB, _SPAN_FLOOR_DB)
    with np.errstate(divide = "ignore"):
        loud = np.nonzero(20.0 * np.log10(rms) > floor)[0]
    if loud.size == 0:
        return None
    end = min(int(loud[-1] + 1) * frame, total)
    return round(float(loud[0]) * frame / rate, 3), round(end / rate, 3)


def measure(
    data: np.ndarray,
    rate: int,
    step: float = 0.0,
) -> dict[str, Any]:
    """The metrics for ``data`` (frames x channels in [-1, 1]) at ``rate``; ``step`` is the PCM
    quantization step the samples were decoded from (0 for float audio)."""
    duration = data.shape[0] / rate if rate else 0.0
    lufs = integrated_loudness(data, rate)
    clipped = clipped_samples(data, step)
    span = sound_span(data, rate)
    return {
        "duration_s": round(duration, 3),
        "sample_rate": rate,
        "channels": int(data.shape[1]),
        "integrated_lufs": round(lufs, 2) if lufs is not None else None,
        "true_peak_dbtp": _db(true_peak(data)),
        "clipped_samples": clipped,
        "clipping_ratio": round(clipped / data.size, 6) if data.size else 0.0,
        "speech_start_s": span[0] if span else None,
        "speech_end_s": span[1] if span else None,
    }


def measure_file(path: Path) -> dict[str, Any]:
    """``measure`` for a PCM or IEEE float WAV; raises wave.Error, EOFError or ValueError for anything else."""
    data, rate, step = _read_pcm(path)
    return measure(data, rate, step)


# Scripts written without spaces between words: each character counts as a word.
_UNSPACED = re.compile(r"[\u0e00-\u0e7f\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff]")


def _words(text: str) -> list[str]:
    folded = unicodedata.normalize("NFKC", text or "").casefold()
    # "don't" and "dont" are one word; other punctuation splits words.
    folded = re.sub(r"(?<=\w)['\u2019](?=\w)", "", folded)
    folded = "".join(" " if unicodedata.category(ch)[0] in "PS" else ch for ch in folded)
    return _UNSPACED.sub(lambda m: f" {m.group(0)} ", folded).split()


# Alignment compares every expected word with every heard word; past this many pairs, no score.
_MATCH_MAX_PAIRS = 50_000_000


def match_score(expected: str, heard: str) -> Optional[float]:
    """1 minus word error rate after normalisation, floored at 0; None when too long to align."""
    ref = _words(expected)
    hyp = _words(heard)
    if not ref:
        return 1.0 if not hyp else 0.0
    if not hyp:
        return 0.0
    if len(ref) * len(hyp) > _MATCH_MAX_PAIRS:
        return None
    ids: dict[str, int] = {}
    ref_ids = [ids.setdefault(word, len(ids)) for word in ref]
    hyp_ids = np.array([ids.setdefault(word, len(ids)) for word in hyp])
    steps = np.arange(len(hyp) + 1)
    previous = steps.copy()
    # One row of the edit-distance table per expected word, a whole row at a time.
    for i, word in enumerate(ref_ids, 1):
        current = np.empty_like(previous)
        current[0] = i
        current[1:] = np.minimum(previous[1:] + 1, previous[:-1] + (hyp_ids != word))
        # An insertion carries a cell to the right: current[j] = min over k <= j of current[k] + j - k.
        previous = np.minimum.accumulate(current - steps) + steps
    return round(max(0.0, 1.0 - int(previous[-1]) / len(ref)), 4)