High-Quality Voice Cloning TTS for 600+ Languages
Python
use soundfile+librosa instead of torchaudio to avoid issues on some devices
Han Zhu4f4b0ccb6e10
diff --git a/README.md b/README.mdindex a4d6634..d4c8e9e 100644--- a/README.md+++ b/README.md@@ -113,8 +113,8 @@ Clone a voice from a short reference audio. Provide `ref_audio` and `ref_text`:```pythonfrom omnivoice import OmniVoice+import soundfile as sfimport torch-import torchaudiomodel = OmniVoice.from_pretrained("k2-fsa/OmniVoice",@@ -127,12 +127,12 @@ audio = model.generate(text="Hello, this is a test of zero-shot voice cloning.",ref_audio="ref.wav",ref_text="Transcription of the reference audio.",-) # audio is a list of `torch.Tensor` with shape (1, T) at 24 kHz.+) # audio is a list of `np.ndarray` with shape (T,) at 24 kHz.# If you don't want to input `ref_text` manually, you can directly omit the `ref_text`.# The model will use Whisper ASR to auto-transcribe it.-torchaudio.save("out.wav", audio[0], 24000)+sf.write("out.wav", audio[0], 24000)```> **Tips**@@ -289,15 +289,6 @@ You can also scan the QR code to join our wechat group or follow our wechat offi----## Community Projects--- **[omnivoice-server](https://github.com/maemreyo/omnivoice-server)** —- OpenAI-compatible HTTP server for serving OmniVoice via `/v1/audio/speech`.- Supports voice profiles for persistent cloning, sentence-level streaming,- and optional Bearer auth.------## Citation```bibtexdiff --git a/omnivoice/cli/demo.py b/omnivoice/cli/demo.pyindex 73dd0a9..b14c3fc 100644--- a/omnivoice/cli/demo.py+++ b/omnivoice/cli/demo.py@@ -213,8 +213,7 @@ def build_demo(except Exception as e:return None, f"Error: {type(e).__name__}: {e}"- waveform = audio[0].squeeze(0).numpy() # (T,)- waveform = (waveform * 32767).astype(np.int16)+ waveform = (audio[0] * 32767).astype(np.int16)return (sampling_rate, waveform), "Done."# Allow external wrappers (e.g. spaces.GPU for ZeroGPU Spaces)diff --git a/omnivoice/cli/infer.py b/omnivoice/cli/infer.pyindex f913304..32447b9 100644--- a/omnivoice/cli/infer.py+++ b/omnivoice/cli/infer.py@@ -23,7 +23,8 @@ import argparseimport loggingimport torch-import torchaudio++import soundfile as sffrom omnivoice.models.omnivoice import OmniVoicefrom omnivoice.utils.common import str2bool@@ -149,7 +150,7 @@ def main():class_temperature=args.class_temperature,)- torchaudio.save(args.output, audios[0], model.sampling_rate)+ sf.write(args.output, audios[0], model.sampling_rate)logging.info(f"Saved to {args.output}")diff --git a/omnivoice/data/dataset.py b/omnivoice/data/dataset.pyindex e96a56f..f3811e9 100644--- a/omnivoice/data/dataset.py+++ b/omnivoice/data/dataset.py@@ -44,8 +44,9 @@ from typing import Any, Dict, Iterator, List, Optional, Tupleimport torchimport torch.distributed as dist-import torchaudioimport webdataset as wds++from omnivoice.utils.audio import load_audio, load_audio_bytesfrom torch.utils.data import IterableDataset@@ -54,12 +55,8 @@ def load_audio_webdataset(data, sample_rate: int = 24000, device="cpu"):Load audio from bytes data and resample to the target sample rate if needed.Return a tensor of shape (1, num_samples)"""- audio, sr = torchaudio.load(io.BytesIO(data))+ audio = torch.from_numpy(load_audio_bytes(data, sample_rate))audio = audio.to(device)- if audio.size(dim=0) > 1:- audio = torch.mean(audio, dim=0)- if sr != sample_rate:- audio = torchaudio.functional.resample(audio, sr, sample_rate)return audio@@ -433,13 +430,9 @@ class JsonlDatasetReader(IterableDataReader):)continuetry:- waveform, sr = torchaudio.load(audio_path)- if waveform.shape[0] > 1:- waveform = waveform.mean(dim=0, keepdim=True)- if sr != self.sample_rate:- waveform = torchaudio.functional.resample(- waveform, sr, self.sample_rate- )+ waveform = torch.from_numpy(+ load_audio(audio_path, self.sample_rate)+ )if self.normalize_audio:waveform = (waveform / (waveform.abs().max() + 1e-7)) * 0.9meta["audio_duration"] = waveform.shape[1] / self.sample_ratediff --git a/omnivoice/eval/mos/utmos.py b/omnivoice/eval/mos/utmos.pyindex 7d19a02..e69d30f 100755--- a/omnivoice/eval/mos/utmos.py+++ b/omnivoice/eval/mos/utmos.py@@ -32,7 +32,7 @@ import torchfrom tqdm import tqdmfrom omnivoice.eval.models.utmos import UTMOS22Strong-from omnivoice.eval.utils import load_waveform+from omnivoice.eval.utils import load_eval_waveformfrom omnivoice.utils.data_utils import read_test_listwarnings.filterwarnings("ignore")@@ -140,7 +140,7 @@ def run_utmos_worker(file_idx, wav_path, language_name):return file_idx, wav_path, language_name, f"File not found: {wav_path}", "error"# Load and preprocess waveform- speech = load_waveform(wav_path, worker_sr, device=worker_device)+ speech = load_eval_waveform(wav_path, worker_sr, device=worker_device)# Compute score# UTMOS expects input shape (Batch, Time)diff --git a/omnivoice/eval/speaker_similarity/sim.py b/omnivoice/eval/speaker_similarity/sim.pyindex 365d1ee..429b128 100755--- a/omnivoice/eval/speaker_similarity/sim.py+++ b/omnivoice/eval/speaker_similarity/sim.py@@ -33,7 +33,7 @@ import torchfrom tqdm import tqdmfrom omnivoice.eval.models.ecapa_tdnn_wavlm import ECAPA_TDNN_WAVLM-from omnivoice.eval.utils import load_waveform+from omnivoice.eval.utils import load_eval_waveformfrom omnivoice.utils.data_utils import read_test_listwarnings.filterwarnings("ignore")@@ -144,7 +144,7 @@ def worker_init(@torch.no_grad()def get_embedding(wav_path: str) -> torch.Tensor:"""Extract embedding for a single file."""- speech = load_waveform(wav_path, worker_sr, device=worker_device, max_seconds=120)+ speech = load_eval_waveform(wav_path, worker_sr, device=worker_device, max_seconds=120)return worker_model([speech])diff --git a/omnivoice/eval/utils.py b/omnivoice/eval/utils.pyindex 5f48b6f..572146a 100644--- a/omnivoice/eval/utils.py+++ b/omnivoice/eval/utils.py@@ -23,7 +23,7 @@ import soundfile as sfimport torch-def load_waveform(+def load_eval_waveform(fname: str,sample_rate: int,dtype: str = "float32",diff --git a/omnivoice/eval/wer/hubert.py b/omnivoice/eval/wer/hubert.pyindex add1d25..48706bb 100755--- a/omnivoice/eval/wer/hubert.py+++ b/omnivoice/eval/wer/hubert.py@@ -31,7 +31,7 @@ import numpy as npimport torchfrom tqdm import tqdm-from omnivoice.eval.utils import load_waveform+from omnivoice.eval.utils import load_eval_waveformfrom omnivoice.eval.wer.common import process_onefrom omnivoice.utils.data_utils import read_test_list@@ -166,7 +166,7 @@ def run_eval_worker(data_chunk, batch_size):try:dataset = [{- "array": load_waveform(+ "array": load_eval_waveform(item["wav_path"], sample_rate=16000, return_numpy=True),"sampling_rate": 16000,diff --git a/omnivoice/eval/wer/minimax.py b/omnivoice/eval/wer/minimax.pyindex 9fb6828..1479304 100755--- a/omnivoice/eval/wer/minimax.py+++ b/omnivoice/eval/wer/minimax.py@@ -34,7 +34,7 @@ import torchimport zhconvfrom tqdm import tqdm-from omnivoice.eval.utils import load_waveform+from omnivoice.eval.utils import load_eval_waveformfrom omnivoice.eval.wer.common import log_metrics, process_onefrom omnivoice.eval.wer.text_norm_omni import text_normalizefrom omnivoice.utils.data_utils import read_test_list@@ -275,7 +275,7 @@ class SpeechEvalDataset(torch.utils.data.Dataset):def __getitem__(self, index):item = self.data_list[index]- waveform = load_waveform(item["wav_path"], sample_rate=16000, return_numpy=True)+ waveform = load_eval_waveform(item["wav_path"], sample_rate=16000, return_numpy=True)return {"array": waveform,"sampling_rate": 16000,diff --git a/omnivoice/eval/wer/seedtts.py b/omnivoice/eval/wer/seedtts.pyindex 2fcb04f..7d1ba98 100755--- a/omnivoice/eval/wer/seedtts.py+++ b/omnivoice/eval/wer/seedtts.py@@ -34,7 +34,7 @@ import zhconvfrom tqdm import tqdmfrom zhon.hanzi import punctuation-from omnivoice.eval.utils import load_waveform+from omnivoice.eval.utils import load_eval_waveformfrom omnivoice.eval.wer.common import process_onefrom omnivoice.utils.data_utils import read_test_list@@ -228,7 +228,7 @@ def run_eval_worker(data_chunk, lang, batch_size):# Load waveforms as arrays, truncating to 30sdataset = [{- "array": load_waveform(+ "array": load_eval_waveform(item["wav_path"], sample_rate=16000, return_numpy=True)[: 16000 * 30],"sampling_rate": 16000,diff --git a/omnivoice/models/omnivoice.py b/omnivoice/models/omnivoice.pyindex a3e8b27..3d0b20e 100644--- a/omnivoice/models/omnivoice.py+++ b/omnivoice/models/omnivoice.py@@ -36,10 +36,11 @@ from dataclasses import dataclass, fieldsfrom functools import partialfrom typing import Any, List, Optional, Union+import librosa+import numpy as npimport torchimport torch.nn as nnimport torch.nn.functional as F-import torchaudiofrom torch.nn.attention.flex_attention import create_block_maskfrom transformers import (AutoFeatureExtractor,@@ -310,12 +311,14 @@ class OmniVoice(PreTrainedModel):@torch.inference_mode()def transcribe(self,- audio: Union[str, tuple[torch.Tensor, int]],+ audio: Union[str, tuple],) -> str:"""Transcribe audio using the loaded Whisper ASR model.Args:- audio: File path or (waveform, sample_rate) tuple.+ audio: File path or ``(waveform, sample_rate)`` tuple.+ Waveform can be a numpy array or torch.Tensor of shape+ ``(1, T)`` or ``(T,)``.Returns:Transcribed text.@@ -329,12 +332,11 @@ class OmniVoice(PreTrainedModel):return self._asr_pipe(audio)["text"].strip()else:waveform, sr = audio- if waveform.dim() == 1:- waveform = waveform.unsqueeze(0)- if waveform.size(0) > 1:- waveform = torch.mean(waveform, dim=0, keepdim=True)+ if isinstance(waveform, torch.Tensor):+ waveform = waveform.cpu().numpy()+ waveform = np.squeeze(waveform) # (1, T) or (T,) → (T,)audio_input = {- "array": waveform.squeeze(0).cpu().numpy(),+ "array": waveform,"sampling_rate": sr,}return self._asr_pipe(audio_input)["text"].strip()@@ -475,7 +477,7 @@ class OmniVoice(PreTrainedModel):speed: Union[float, list[Optional[float]], None] = None,generation_config: Optional[OmniVoiceGenerationConfig] = None,**kwargs,- ) -> list[torch.Tensor]:+ ) -> list[np.ndarray]:"""Generate speech audio given text in various modes.Supports three modes:@@ -522,8 +524,10 @@ class OmniVoice(PreTrainedModel):audio_chunk_threshold: Only apply chunking if estimated audioduration exceeds this threshold (seconds).Returns:- ``audios`` a list of 2-D ``torch.Tensor``, with the shape (1, T) and sampling rate- consistent with the model's audio tokenizer (usually 24000 Hz).+ ``audios`` a list of 1-D ``np.ndarray`` with shape ``(T,)`` and+ sampling rate consistent with the model's audio tokenizer+ (usually 24 000 Hz). Can be saved directly with+ ``soundfile.write("out.wav", audios[0], model.sampling_rate)``."""if self.audio_tokenizer is None or self.text_tokenizer is None:@@ -611,17 +615,19 @@ class OmniVoice(PreTrainedModel):ref_wav = load_audio(ref_audio, self.sampling_rate)else:waveform, sr = ref_audio- if waveform.dim() == 1:- waveform = waveform.unsqueeze(0)- if waveform.size(0) > 1:- waveform = torch.mean(waveform, dim=0, keepdim=True)+ if isinstance(waveform, torch.Tensor):+ waveform = waveform.cpu().numpy()+ if waveform.ndim == 1:+ waveform = waveform[np.newaxis, :]+ if waveform.shape[0] > 1:+ waveform = np.mean(waveform, axis=0, keepdims=True)if sr != self.sampling_rate:- waveform = torchaudio.functional.resample(- waveform, sr, self.sampling_rate+ waveform = librosa.resample(+ waveform, orig_sr=sr, target_sr=self.sampling_rate,)ref_wav = waveform- ref_rms = torch.sqrt(torch.mean(torch.square(ref_wav))).item()+ ref_rms = float(np.sqrt(np.mean(ref_wav ** 2)))if 0 < ref_rms < 0.1:ref_wav = ref_wav * 0.1 / ref_rms@@ -640,13 +646,13 @@ class OmniVoice(PreTrainedModel):lead_sil=100,trail_sil=200,)- if ref_wav.size(-1) == 0:+ if ref_wav.shape[-1] == 0:raise ValueError("Reference audio is empty after silence removal. ""Try setting preprocess_prompt=False.")- ref_duration = ref_wav.size(-1) / self.sampling_rate+ ref_duration = ref_wav.shape[-1] / self.sampling_rateif ref_duration > 20.0:logger.warning("Reference audio is %.1fs long (>20s). This may cause slower "@@ -664,10 +670,14 @@ class OmniVoice(PreTrainedModel):logger.debug("Auto-transcribed ref_text: %s", ref_text)chunk_size = self.audio_tokenizer.config.hop_length- clip_size = int(ref_wav.size(-1) % chunk_size)+ clip_size = int(ref_wav.shape[-1] % chunk_size)ref_wav = ref_wav[:, :-clip_size] if clip_size > 0 else ref_wav+ # numpy → torch at tokenizer boundary+ ref_wav_tensor = torch.from_numpy(ref_wav).to(+ self.audio_tokenizer.device+ )ref_audio_tokens = self.audio_tokenizer.encode(- ref_wav.unsqueeze(0).to(self.audio_tokenizer.device),+ ref_wav_tensor.unsqueeze(0),).audio_codes.squeeze(0) # (C, T)@@ -686,7 +696,7 @@ class OmniVoice(PreTrainedModel):tokens: Union[torch.Tensor, List[torch.Tensor]],rms: Union[float, None],gen_config: OmniVoiceGenerationConfig,- ) -> torch.Tensor:+ ) -> np.ndarray:"""Args:tokens: Audio tokens — either a single tensor of shape@@ -694,7 +704,7 @@ class OmniVoice(PreTrainedModel):rms: RMS of the reference audio for volume adjustment.gen_config: Generation config for post-processing options.Returns:- Decoded and post-processed audio tensor of shape (1, T).+ Decoded and post-processed audio array of shape (T,)."""tokenizer_device = self.audio_tokenizer.deviceif isinstance(tokens, list):@@ -702,6 +712,7 @@ class OmniVoice(PreTrainedModel):self.audio_tokenizer.decode(t.to(tokenizer_device).unsqueeze(0)).audio_values[0].cpu()+ .numpy()for t in tokens]audio_waveform = cross_fade_chunks(chunk_audios, self.sampling_rate)@@ -710,28 +721,30 @@ class OmniVoice(PreTrainedModel):self.audio_tokenizer.decode(tokens.to(tokenizer_device).unsqueeze(0)).audio_values[0].cpu()+ .numpy())- return self._post_process_audio(+ audio_waveform = self._post_process_audio(audio_waveform,postprocess_output=gen_config.postprocess_output,ref_rms=rms,)+ return audio_waveform.squeeze(0)def _post_process_audio(self,- generated_audio: torch.Tensor,+ generated_audio: np.ndarray,postprocess_output: bool,ref_rms: Union[float, None],- ) -> torch.Tensor:+ ) -> np.ndarray:"""Optionally remove long silences, adjust volume, and add edge padding.Args:- generated_audio: Audio tensor of shape (1, T).+ generated_audio: Numpy array of shape (1, T).postprocess_output: If True, remove long silences and apply fade/pad.ref_rms: RMS of the reference audio for volume normalisation.Returns:- Processed audio tensor of shape (1, T).+ Processed numpy array of shape (1, T)."""if postprocess_output:generated_audio = remove_silence(@@ -745,9 +758,7 @@ class OmniVoice(PreTrainedModel):if ref_rms is not None and ref_rms < 0.1:generated_audio = generated_audio * ref_rms / 0.1elif ref_rms is None:- # No reference audio (voice design): peak-normalize to 0.5- # to avoid clipping while keeping a comfortable volume level.- peak = generated_audio.abs().max()+ peak = np.abs(generated_audio).max()if peak > 1e-6:generated_audio = generated_audio / peak * 0.5diff --git a/omnivoice/scripts/denoise_audio.py b/omnivoice/scripts/denoise_audio.pyindex a7900a7..27fce91 100644--- a/omnivoice/scripts/denoise_audio.py+++ b/omnivoice/scripts/denoise_audio.py@@ -73,6 +73,7 @@ from tqdm.auto import tqdmfrom omnivoice.data.batching import StreamLengthGroupDatasetfrom omnivoice.data.dataset import JsonlDatasetReader, WebDatasetReader+import soundfile as sffrom omnivoice.utils.common import str2boolSIDON_INPUT_SAMPLE_RATE = 16_000@@ -367,10 +368,10 @@ def extract_seamless_m4t_features(def serialise_flac(key: str, waveform: torch.Tensor, sample_rate: int) -> dict:buffer = io.BytesIO()- audio = waveform.to(dtype=torch.float32).cpu()- if audio.ndim == 1:- audio = audio.unsqueeze(0)- torchaudio.save(buffer, audio, sample_rate, format="flac", bits_per_sample=16)+ audio = waveform.to(dtype=torch.float32).cpu().numpy()+ if audio.ndim == 2:+ audio = audio.T # (C, T) → (T, C) for soundfile+ sf.write(buffer, audio, sample_rate, format="FLAC")return {"__key__": key, "flac": buffer.getvalue()}diff --git a/omnivoice/scripts/extract_audio_tokens_add_noise.py b/omnivoice/scripts/extract_audio_tokens_add_noise.pyindex 63aff43..37b72ac 100644--- a/omnivoice/scripts/extract_audio_tokens_add_noise.py+++ b/omnivoice/scripts/extract_audio_tokens_add_noise.py@@ -66,13 +66,13 @@ from typing import Anyimport numpy as npimport torchimport torch.nn.functional as F-import torchaudioimport webdataset as wdsfrom torch.utils.data import DataLoader, IterableDatasetfrom tqdm.auto import tqdmfrom transformers import AutoFeatureExtractor, HiggsAudioV2TokenizerModelfrom omnivoice.data.dataset import JsonlDatasetReader, WebDatasetReader+from omnivoice.utils.audio import load_audio_bytesfrom omnivoice.utils.common import str2boolwarnings.filterwarnings(@@ -207,13 +207,7 @@ def serialise_numpy(key: str, tokens: np.ndarray) -> dict:def _load_aug_audio(data, sample_rate=24000):"""Simple audio loader for augmentation files."""- with io.BytesIO(data) as b:- wav, sr = torchaudio.load(b)- if wav.shape[0] > 1:- wav = wav.mean(dim=0, keepdim=True)- if sr != sample_rate:- wav = torchaudio.functional.resample(wav, sr, sample_rate)- return wav+ return torch.from_numpy(load_audio_bytes(data, sample_rate))class SimpleWorkerSampler:diff --git a/omnivoice/scripts/jsonl_to_webdataset.py b/omnivoice/scripts/jsonl_to_webdataset.pyindex b3df95a..4664145 100644--- a/omnivoice/scripts/jsonl_to_webdataset.py+++ b/omnivoice/scripts/jsonl_to_webdataset.py@@ -65,10 +65,13 @@ from concurrent.futures import (from itertools import islicefrom pathlib import Path-import torchaudio+import librosaimport webdataset as wdsfrom tqdm import tqdm+import soundfile as sf++from omnivoice.utils.audio import load_waveformfrom omnivoice.utils.common import str2bool@@ -164,16 +167,16 @@ def process_audio_item(meta, target_sr):if not os.path.exists(audio_path):raise FileNotFoundError(f"{audio_path} not found")- waveform, sr = torchaudio.load(audio_path)+ waveform, sr = load_waveform(audio_path)audio_duration = waveform.shape[1] / srmeta["audio_duration"] = audio_durationif target_sr and sr != target_sr:- waveform = torchaudio.functional.resample(waveform, sr, target_sr)+ waveform = librosa.resample(waveform, orig_sr=sr, target_sr=target_sr)sr = target_sraudio_buffer = io.BytesIO()- torchaudio.save(audio_buffer, waveform, sr, format="flac", bits_per_sample=16)+ sf.write(audio_buffer, waveform.T, sr, format="FLAC")audio_bytes = audio_buffer.getvalue()sample = {diff --git a/omnivoice/utils/audio.py b/omnivoice/utils/audio.pyindex 9fcb2c2..d0ce8cd 100644--- a/omnivoice/utils/audio.py+++ b/omnivoice/utils/audio.py@@ -17,83 +17,157 @@"""Audio I/O and processing utilities.-Provides functions for loading, resampling, silence removal, chunking,-cross-fading, and format conversion. Used by ``OmniVoice.generate()`` during-inference post-processing.+Provides functions for loading, resampling, silence removal,+chunking, cross-fading, and format conversion.++All public functions in this module operate on **numpy float32 arrays**+with shape ``(C, T)`` (channels-first)."""+import io+import logging++import librosaimport numpy as np-import torch-import torchaudio+import soundfile as sffrom pydub import AudioSegmentfrom pydub.silence import detect_leading_silence, detect_nonsilent, split_on_silence+logger = logging.getLogger(__name__)+++# ---------------------------------------------------------------------------+# Loading+# ----------------------------------------------------------------------------def load_audio(audio_path: str, sampling_rate: int):++def load_waveform(audio_path: str):+ """Load audio from a file path, returning (data, sample_rate).++ Tries two backends in order:+ 1. soundfile — covers WAV/FLAC/OGG etc., no ffmpeg needed.+ 2. librosa — covers MP3/M4A etc. via audioread + ffmpeg.++ Returns:+ (data, sample_rate) where data is a numpy float32 array of+ shape (C, T)."""- Load the waveform with torchaudio and resampling if needed.+ try:+ data, sr = sf.read(audio_path, dtype="float32", always_2d=True)+ return data.T, sr # (T, C) → (C, T)+ except Exception:+ # soundfile cannot handle MP3/M4A etc., fall back to librosa.+ data, sr = librosa.load(audio_path, sr=None, mono=False)+ if data.ndim == 1:+ data = data[np.newaxis, :]+ return data, sr+++def load_audio(audio_path: str, sampling_rate: int) -> np.ndarray:+ """Load a waveform from file and resample to the target rate.Parameters:audio_path: path of the audio.sampling_rate: target sampling rate.Returns:- Loaded prompt waveform with target sampling rate,- PyTorch tensor of shape (1, T)+ Numpy float32 array of shape (1, T)."""- try:- waveform, prompt_sampling_rate = torchaudio.load(- audio_path, backend="soundfile"+ data, sr = load_waveform(audio_path)++ if data.shape[0] > 1:+ data = np.mean(data, axis=0, keepdims=True)+ if sr != sampling_rate:+ data = librosa.resample(+ data,+ orig_sr=sr,+ target_sr=sampling_rate,)- except (RuntimeError, OSError):- # Fallback via pydub+ffmpeg for formats torchaudio can't handle- aseg = AudioSegment.from_file(audio_path)- audio_data = np.array(aseg.get_array_of_samples()).astype(np.float32) / 32768.0- if aseg.channels == 1:- waveform = torch.from_numpy(audio_data).unsqueeze(0)- else:- waveform = torch.from_numpy(audio_data.reshape(-1, aseg.channels).T)- prompt_sampling_rate = aseg.frame_rate-- if prompt_sampling_rate != sampling_rate:- waveform = torchaudio.functional.resample(- waveform,- orig_freq=prompt_sampling_rate,- new_freq=sampling_rate,++ return data+++def load_audio_bytes(raw: bytes, sampling_rate: int) -> np.ndarray:+ """Load audio from in-memory bytes and resample.++ Parameters:+ raw: raw audio file bytes (e.g. from WebDataset).+ sampling_rate: target sampling rate.++ Returns:+ Numpy float32 array of shape (1, T).+ """+ buf = io.BytesIO(raw)++ try:+ data, sr = sf.read(buf, dtype="float32", always_2d=True)+ data = data.T # (T, C) → (C, T)+ except Exception:+ buf.seek(0)+ data, sr = librosa.load(buf, sr=None, mono=False)+ if data.ndim == 1:+ data = data[np.newaxis, :]++ if data.shape[0] > 1:+ data = np.mean(data, axis=0, keepdims=True)+ if sr != sampling_rate:+ data = librosa.resample(+ data,+ orig_sr=sr,+ target_sr=sampling_rate,)- if waveform.shape[0] > 1:- waveform = torch.mean(waveform, dim=0, keepdim=True)- return waveform+ return data+++# ---------------------------------------------------------------------------+# Audio processing (all numpy in / numpy out)+# ---------------------------------------------------------------------------+++def numpy_to_audiosegment(audio: np.ndarray, sample_rate: int) -> AudioSegment:+ """Convert a numpy float32 array of shape (C, T) to a pydub AudioSegment."""+ audio_int = (audio * 32768.0).clip(-32768, 32767).astype(np.int16)+ if audio_int.shape[0] > 1:+ audio_int = audio_int.T.flatten() # interleave channels+ return AudioSegment(+ data=audio_int.tobytes(),+ sample_width=2,+ frame_rate=sample_rate,+ channels=audio.shape[0],+ )+++def audiosegment_to_numpy(aseg: AudioSegment) -> np.ndarray:+ """Convert a pydub AudioSegment to a numpy float32 array of shape (C, T)."""+ data = np.array(aseg.get_array_of_samples()).astype(np.float32) / 32768.0+ if aseg.channels == 1:+ return data[np.newaxis, :]+ return data.reshape(-1, aseg.channels).Tdef remove_silence(- audio: torch.Tensor,+ audio: np.ndarray,sampling_rate: int,mid_sil: int = 300,lead_sil: int = 100,trail_sil: int = 300,-):- """- Remove middle silences longer than mid_sil ms, and edge silences longer than edge_sil ms+) -> np.ndarray:+ """Remove middle silences longer than *mid_sil* ms and trim edge silences.Parameters:- audio: PyTorch tensor with shape (C, T).+ audio: numpy array with shape (C, T).sampling_rate: sampling rate of the audio.- mid_sil: the duration of silences in the middle of audio to be removed in ms.- if mid_sil <= 0, no middle silence will be removed.- edge_sil: the duration of silences in the edge of audio to be removed in ms.- trail_sil: the duration of added trailing silence in ms.+ mid_sil: middle-silence threshold in ms (0 to skip).+ lead_sil: kept leading silence in ms.+ trail_sil: kept trailing silence in ms.Returns:- PyTorch tensor with shape (C, T), where C is number of channels- and T is number of audio samples+ Numpy array with shape (C, T')."""- # Load audio file- wave = tensor_to_audiosegment(audio, sampling_rate)+ wave = numpy_to_audiosegment(audio, sampling_rate)if mid_sil > 0:- # Split audio using silences longer than mid_silnon_silent_segs = split_on_silence(wave,min_silence_len=mid_sil,@@ -101,17 +175,13 @@ def remove_silence(keep_silence=mid_sil,seek_step=10,)-- # Concatenate all non-silent segmentswave = AudioSegment.silent(duration=0)for seg in non_silent_segs:wave += seg- # Remove silence longer than 0.1 seconds in the begining and ending of wavewave = remove_silence_edges(wave, lead_sil, trail_sil, -50)- # Convert to PyTorch tensor- return audiosegment_to_tensor(wave)+ return audiosegment_to_numpy(wave)def remove_silence_edges(@@ -119,25 +189,12 @@ def remove_silence_edges(lead_sil: int = 100,trail_sil: int = 300,silence_threshold: float = -50,-):- """- Remove edge silences longer than `keep_silence` ms.-- Parameters:- audio: an AudioSegment object.- keep_silence: kept silence in the edge.- only_edge: If true, only remove edge silences.- silence_threshold: the threshold of silence.-- Returns:- An AudioSegment object- """- # Remove heading silence+) -> AudioSegment:+ """Remove edge silences, keeping *lead_sil* / *trail_sil* ms."""start_idx = detect_leading_silence(audio, silence_threshold=silence_threshold)start_idx = max(0, start_idx - lead_sil)audio = audio[start_idx:]- # Remove trailing silenceaudio = audio.reverse()start_idx = detect_leading_silence(audio, silence_threshold=silence_threshold)start_idx = max(0, start_idx - trail_sil)@@ -147,80 +204,22 @@ def remove_silence_edges(return audio-def audiosegment_to_tensor(aseg):- """- Convert a pydub.AudioSegment to PyTorch audio tensor- """- audio_data = np.array(aseg.get_array_of_samples())-- # Convert to float32 and normalize to [-1, 1] range- audio_data = audio_data.astype(np.float32) / 32768.0-- # Handle channels- if aseg.channels == 1:- # Mono channel: add channel dimension (T) -> (1, T)- tensor_data = torch.from_numpy(audio_data).unsqueeze(0)- else:- # Multi-channel: reshape to (C, T)- tensor_data = torch.from_numpy(audio_data.reshape(-1, aseg.channels).T)-- return tensor_data---def tensor_to_audiosegment(tensor, sample_rate):- """- Convert a PyTorch audio tensor to pydub.AudioSegment-- Parameters:- tensor: Tensor with shape (C, T), where C is the number of channels- and T is the time steps- sample_rate: Audio sample rate- """- # Convert tensor to numpy array- assert isinstance(tensor, torch.Tensor)- audio_np = tensor.cpu().numpy()-- # Convert to int16 type (common format for pydub)- # Assumes tensor values are in [-1, 1] range as floating point- audio_np = (audio_np * 32768.0).clip(-32768, 32767).astype(np.int16)-- # Convert to byte stream- # For multi-channel audio, pydub requires interleaved format- # (e.g., left-right-left-right)- if audio_np.shape[0] > 1:- # Convert to interleaved format- audio_np = audio_np.transpose(1, 0).flatten()- audio_bytes = audio_np.tobytes()-- # Create AudioSegment- audio_segment = AudioSegment(- data=audio_bytes,- sample_width=2,- frame_rate=sample_rate,- channels=tensor.shape[0],- )-- return audio_segment--def fade_and_pad_audio(- audio: torch.Tensor,+ audio: np.ndarray,pad_duration: float = 0.1,fade_duration: float = 0.1,sample_rate: int = 24000,-) -> torch.Tensor:- """- Applies a smooth fade-in and fade-out to the audio, and then pads both sides- with pure silence to prevent abrupt starts and ends (clicks/pops).+) -> np.ndarray:+ """Apply fade-in/out and pad with silence to prevent clicks.Args:- audio: PyTorch tensor of shape (C, T) containing audio data.- pad_duration: Duration of pure silence to add to each end (in seconds).- fade_duration: Duration of the fade-in/out curve (in seconds).- sample_rate: Audio sampling rate.+ audio: numpy array of shape (C, T).+ pad_duration: silence padding duration per side (seconds).+ fade_duration: fade curve duration (seconds).+ sample_rate: audio sampling rate.Returns:- Processed sequence tensor with shape (C, T_new)+ Processed numpy array of shape (C, T_new)."""if audio.shape[-1] == 0:return audio@@ -228,59 +227,53 @@ def fade_and_pad_audio(fade_samples = int(fade_duration * sample_rate)pad_samples = int(pad_duration * sample_rate)- processed = audio.clone()+ processed = audio.copy()if fade_samples > 0:k = min(fade_samples, processed.shape[-1] // 2)-if k > 0:- fade_in = torch.linspace(- 0, 1, k, device=processed.device, dtype=processed.dtype- )[None, :]- processed[..., :k] = processed[..., :k] * fade_in+ fade_in = np.linspace(0, 1, k, dtype=np.float32)[np.newaxis, :]+ processed[..., :k] *= fade_in- fade_out = torch.linspace(- 1, 0, k, device=processed.device, dtype=processed.dtype- )[None, :]- processed[..., -k:] = processed[..., -k:] * fade_out+ fade_out = np.linspace(1, 0, k, dtype=np.float32)[np.newaxis, :]+ processed[..., -k:] *= fade_outif pad_samples > 0:- silence = torch.zeros(+ silence = np.zeros((processed.shape[0], pad_samples),dtype=processed.dtype,- device=processed.device,)- processed = torch.cat([silence, processed, silence], dim=-1)+ processed = np.concatenate([silence, processed, silence], axis=-1)return processeddef trim_long_audio(- audio: torch.Tensor,+ audio: np.ndarray,sampling_rate: int,max_duration: float = 15.0,min_duration: float = 3.0,trim_threshold: float = 20.0,-) -> torch.Tensor:- """Trim audio to <= max_duration by splitting at the largest silence gap.+) -> np.ndarray:+ """Trim audio to <= *max_duration* by splitting at the largest silence gap.Only trims when the audio exceeds *trim_threshold* seconds.Args:- audio: Audio tensor of shape (C, T).- sampling_rate: Audio sampling rate.- max_duration: Maximum duration in seconds.- min_duration: Minimum duration in seconds.- trim_threshold: Only trim if audio is longer than this (seconds).+ audio: numpy array of shape (C, T).+ sampling_rate: audio sampling rate.+ max_duration: maximum duration in seconds.+ min_duration: minimum duration in seconds.+ trim_threshold: only trim if audio is longer than this (seconds).Returns:- Trimmed audio tensor.+ Trimmed numpy array."""- duration = audio.size(-1) / sampling_rate+ duration = audio.shape[-1] / sampling_rateif duration <= trim_threshold:return audio- seg = tensor_to_audiosegment(audio, sampling_rate)+ seg = numpy_to_audiosegment(audio, sampling_rate)nonsilent = detect_nonsilent(seg, min_silence_len=100, silence_thresh=-40, seek_step=10)@@ -290,7 +283,6 @@ def trim_long_audio(max_ms = int(max_duration * 1000)min_ms = int(min_duration * 1000)- # Walk through speech regions; at each gap pick the latest split <= max_durationbest_split = 0for start, end in nonsilent:if start > best_split and start <= max_ms:@@ -302,56 +294,49 @@ def trim_long_audio(best_split = min(max_ms, len(seg))trimmed = seg[:best_split]- return audiosegment_to_tensor(trimmed)+ return audiosegment_to_numpy(trimmed)def cross_fade_chunks(- chunks: list[torch.Tensor],+ chunks: list[np.ndarray],sample_rate: int,silence_duration: float = 0.3,-) -> torch.Tensor:- """Concatenate audio chunks with a short silence gap and fade at boundaries.-- Each boundary is structured as: fade-out tail → silence buffer → fade-in head.- This avoids click artifacts from direct concatenation or overlapping mismatch.+) -> np.ndarray:+ """Concatenate audio chunks with silence gaps and cross-fade at boundaries.Args:- chunks: List of audio tensors, each (C, T).- sample_rate: Audio sample rate.- silence_duration: Total silence gap duration in seconds.+ chunks: list of numpy arrays, each (C, T).+ sample_rate: audio sample rate.+ silence_duration: total silence gap duration in seconds.Returns:- Merged audio tensor (C, T_total).+ Merged numpy array (C, T_total)."""if len(chunks) == 1:return chunks[0]total_n = int(silence_duration * sample_rate)fade_n = total_n // 3- silence_n = fade_n # middle silent gap- merged = chunks[0].clone()+ silence_n = fade_n+ merged = chunks[0].copy()for chunk in chunks[1:]:- dev, dt = merged.device, merged.dtypeparts = [merged]- # Fade out tail of current merged audio- fout_n = min(fade_n, merged.size(-1))+ fout_n = min(fade_n, merged.shape[-1])if fout_n > 0:- w_out = torch.linspace(1, 0, fout_n, device=dev, dtype=dt)[None, :]- parts[-1][..., -fout_n:] = parts[-1][..., -fout_n:] * w_out+ w_out = np.linspace(1, 0, fout_n, dtype=np.float32)[np.newaxis, :]+ parts[-1][..., -fout_n:] *= w_out- # Silent buffer between chunks- parts.append(torch.zeros(chunks[0].shape[0], silence_n, device=dev, dtype=dt))+ parts.append(np.zeros((chunks[0].shape[0], silence_n), dtype=np.float32))- # Fade in head of next chunk- fade_in = chunk.clone()- fin_n = min(fade_n, fade_in.size(-1))+ fade_in = chunk.copy()+ fin_n = min(fade_n, fade_in.shape[-1])if fin_n > 0:- w_in = torch.linspace(0, 1, fin_n, device=dev, dtype=dt)[None, :]- fade_in[..., :fin_n] = fade_in[..., :fin_n] * w_in+ w_in = np.linspace(0, 1, fin_n, dtype=np.float32)[np.newaxis, :]+ fade_in[..., :fin_n] *= w_inparts.append(fade_in)- merged = torch.cat(parts, dim=-1)+ merged = np.concatenate(parts, axis=-1)return mergeddiff --git a/omnivoice/utils/data_utils.py b/omnivoice/utils/data_utils.pyindex 075be64..0b30377 100644--- a/omnivoice/utils/data_utils.py+++ b/omnivoice/utils/data_utils.py@@ -29,10 +29,10 @@ from pathlib import Pathdef read_test_list(path):"""Read a JSONL test list file.- Each line should be a JSON object with fields:- id, text, ref_audio, ref_text, language_id, language_name, duration, speed-- language_id, language_name, duration, and speed are optional (default to None).+ Each line should be a JSON object. Only ``id`` and ``text`` are required;+ all other fields are optional (default to ``None``):+ id, text, ref_audio, ref_text, instruct,+ language_id, language_name, duration, speedReturns a list of dicts."""diff --git a/pyproject.toml b/pyproject.tomlindex c0f0c32..611ff99 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -39,14 +39,14 @@ dependencies = ["tensorboardX","webdataset","numpy",- "soundfile"+ "soundfile",+ "librosa",][project.optional-dependencies]eval = ["jiwer==3.1.0", # WER- "librosa", # Audio processing"s3prl", # Speech representation (HuBERT etc.)"funasr", # ASR models"zhconv", # Chinese character normalizationdiff --git a/uv.lock b/uv.lockindex 8d41c1a..e5f7fcf 100644--- a/uv.lock+++ b/uv.lock@@ -1558,6 +1558,7 @@ source = { editable = "." }dependencies = [{ name = "accelerate" },{ name = "gradio" },+ { name = "librosa" },{ name = "numpy", version = "2.2.6", source = { registry = "https://pypi.org/simple/" }, marker = "python_full_version < '3.11'" },{ name = "numpy", version = "2.4.3", source = { registry = "https://pypi.org/simple/" }, marker = "python_full_version >= '3.11'" },{ name = "pydub" },@@ -1575,7 +1576,6 @@ dependencies = [eval = [{ name = "funasr" },{ name = "jiwer" },- { name = "librosa" },{ name = "s3prl" },{ name = "unidecode" },{ name = "zhconv" },@@ -1588,7 +1588,7 @@ requires-dist = [{ name = "funasr", marker = "extra == 'eval'" },{ name = "gradio" },{ name = "jiwer", marker = "extra == 'eval'", specifier = "==3.1.0" },- { name = "librosa", marker = "extra == 'eval'" },+ { name = "librosa" },{ name = "numpy" },{ name = "pydub" },{ name = "s3prl", marker = "extra == 'eval'" },