25acf9db6e
横向对比云端 gummy 与本地开源模型(faster-whisper/SenseVoice/Paraformer), 重点覆盖中英混说,产出准确率(CER/WER/MER)/速度(延迟/RTF)/资源(cpu/mem/模型大小) 对比报告。公共集(ASCEND/AISHELL/LibriSpeech)统一走 HF 适配器 + 自定义 JSONL manifest。 gummy 引擎对照 server/internal/asr/gummy.go 协议移植。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
43 lines
1.3 KiB
Python
43 lines
1.3 KiB
Python
"""音频加载与重采样工具:统一到 16k/mono,供 gummy(pcm16) 与本地引擎(float) 使用。"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import numpy as np
|
||
import soundfile as sf
|
||
|
||
TARGET_SR = 16000
|
||
|
||
|
||
def _resample(data: np.ndarray, sr: int, target_sr: int) -> np.ndarray:
|
||
if sr == target_sr:
|
||
return data
|
||
import soxr
|
||
|
||
return soxr.resample(data, sr, target_sr)
|
||
|
||
|
||
def load_pcm16(path: str, target_sr: int = TARGET_SR) -> tuple[np.ndarray, int]:
|
||
"""读为 int16 单声道 PCM(gummy 推流用)。"""
|
||
data, sr = sf.read(path, dtype="float32", always_2d=False)
|
||
if data.ndim > 1:
|
||
data = data.mean(axis=1)
|
||
data = _resample(data, sr, target_sr)
|
||
# float32(-1,1) -> int16
|
||
pcm = np.clip(data, -1.0, 1.0)
|
||
pcm = (pcm * 32767.0).astype(np.int16)
|
||
return pcm, target_sr
|
||
|
||
|
||
def load_float(path: str, target_sr: int = TARGET_SR) -> tuple[np.ndarray, int]:
|
||
"""读为 float32 单声道(本地引擎用)。"""
|
||
data, sr = sf.read(path, dtype="float32", always_2d=False)
|
||
if data.ndim > 1:
|
||
data = data.mean(axis=1)
|
||
data = _resample(data, sr, target_sr)
|
||
return data.astype(np.float32), target_sr
|
||
|
||
|
||
def duration_sec(path: str) -> float:
|
||
info = sf.info(path)
|
||
return info.frames / info.samplerate
|