游雁
2024-12-23 1e5ef6ed9a6f64ecca7b9ef9481519b271f793a3
funasr/utils/load_utils.py
@@ -10,7 +10,6 @@
import time
import logging
from torch.nn.utils.rnn import pad_sequence
from pydub import AudioSegment
try:
    from funasr.download.file import download_from_url
@@ -19,6 +18,11 @@
import pdb
import subprocess
from subprocess import CalledProcessError, run
try:
    from pydub import AudioSegment
except:
    pass
def is_ffmpeg_installed():
@@ -138,7 +142,10 @@
def load_bytes(input):
    input = validate_frame_rate(input)
    try:
        input = validate_frame_rate(input)
    except:
        pass
    middle_data = np.frombuffer(input, dtype=np.int16)
    middle_data = np.asarray(middle_data)
    if middle_data.dtype.kind not in "iu":
@@ -153,16 +160,22 @@
    array = np.frombuffer((middle_data.astype(dtype) - offset) / abs_max, dtype=np.float32)
    return array
def validate_frame_rate(
    input,
    fs: int = 16000,
):
    # 将文件读取为字节流
    byte_data = BytesIO(input)
    # 使用 pydub 加载音频
    audio = AudioSegment.from_file(byte_data)
    try:
        audio = AudioSegment.from_file(byte_data)
    except:
        raise RuntimeError(
            "You are decoding the pcm data, please install pydub first. via `pip install pydub`."
        )
    # 确保采样率为 16000 Hz
    if audio.frame_rate != fs:
@@ -175,7 +188,7 @@
        # 获取重新采样后的字节流数据
        input = output.read()
    return input