[Model] Reduce MOSS TTS reference audio preparation overhead (#7725)
Signed-off-by: Sy03 <1370724210@qq.com>
This commit is contained in:
2 files changed
+31
-4
No files matched your search
@@ -27,6 +27,7 @@ from __future__ import annotations
|
||||
import asyncio
|
||||
import hashlib
|
||||
from collections.abc import Awaitable, Callable
|
||||
from functools import lru_cache
|
||||
from typing import Any
|
||||
|
||||
import numpy as np
|
||||
@@ -50,15 +51,24 @@ def _sha1(s: str) -> str:
|
||||
return hashlib.sha1((s or "").encode("utf-8")).hexdigest()
|
||||
|
||||
|
||||
@lru_cache(maxsize=16)
|
||||
def _reference_resampler(sr: int, sr_target: int) -> torch.nn.Module:
|
||||
import torchaudio
|
||||
|
||||
# Match functional.resample's float32 kernel construction, rather than
|
||||
# Resample's default float64 construction followed by a float32 cast.
|
||||
return torchaudio.transforms.Resample(sr, sr_target, dtype=torch.float32)
|
||||
|
||||
|
||||
def _prep_wav_sync(waveform: _Waveform, sr: int, sr_target: int) -> torch.Tensor:
|
||||
"""Tensor-ise + resample one clip to ``sr_target`` (the blocking prep)."""
|
||||
wav = torch.tensor(waveform, dtype=torch.float32)
|
||||
# Keep the tensor independent from the server-side waveform/cache buffer.
|
||||
wav_np = np.array(waveform, dtype=np.float32, order="C", copy=True)
|
||||
wav = torch.from_numpy(wav_np)
|
||||
if wav.dim() == 1:
|
||||
wav = wav.unsqueeze(0)
|
||||
if sr != sr_target:
|
||||
import torchaudio
|
||||
|
||||
wav = torchaudio.functional.resample(wav, sr, sr_target)
|
||||
wav = _reference_resampler(sr, sr_target)(wav)
|
||||
return wav
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user