Skip to content

High-Quality Voice Cloning TTS for 600+ Languages

Python

Avoid decoding the full reference audio just to get its duration (#220)

aradix857ff52a10fc24
diff --git a/omnivoice/cli/infer_batch.py b/omnivoice/cli/infer_batch.py
index 94a0d82..186ad6b 100644
--- a/omnivoice/cli/infer_batch.py
+++ b/omnivoice/cli/infer_batch.py
@@ -247,6 +247,21 @@ def process_init(rank_queue, model_checkpoint, warmup=0):
logging.info(f"Worker on {worker_device} initialized successfully.")
+def _get_audio_duration(audio_path: str) -> float:
+ """Return the duration of an audio file in seconds.
+
+ Reads only the file header, so the samples are never decoded or resampled.
+ Falls back to a full decode for formats ``soundfile`` cannot inspect
+ (e.g. MP3/M4A on older libsndfile builds).
+ """
+ try:
+ info = sf.info(audio_path)
+ return info.frames / info.samplerate
+ except Exception:
+ wav = load_audio(audio_path, SAMPLING_RATE)
+ return wav.shape[-1] / SAMPLING_RATE
+
+
def estimate_sample_total_duration(
duration_estimator: RuleDurationEstimator,
text: str,
@@ -261,8 +276,7 @@ def estimate_sample_total_duration(
duration contributes to the total.
"""
if ref_audio_path is not None:
- ref_wav = load_audio(ref_audio_path, SAMPLING_RATE)
- ref_duration = ref_wav.shape[-1] / SAMPLING_RATE
+ ref_duration = _get_audio_duration(ref_audio_path)
else:
ref_duration = 0