"""Lightweight silence trimming for vendored Kokoro audio chunks.

This is intentionally simpler than upstream librosa-derived trimming, but preserves
the same call shape: trim(audio) -> (trimmed_audio, [start, end]).
"""

from __future__ import annotations

import numpy as np


def trim(y: np.ndarray, *, threshold: float = 1e-4):
    y = np.asarray(y)
    if y.size == 0:
        return y, np.asarray([0, 0])

    magnitude = np.abs(y)
    nonzero = np.flatnonzero(magnitude > threshold)
    if nonzero.size == 0:
        return y[:0], np.asarray([0, 0])

    start = int(nonzero[0])
    end = int(nonzero[-1] + 1)
    return y[start:end], np.asarray([start, end])
