mirror of
https://github.com/modelscope/FunASR.git
synced 2026-10-12 02:19:18 +00:00
* Fix #2787: skip validate_frame_rate for raw PCM bytes in load_bytes `validate_frame_rate` calls `pydub.AudioSegment.from_file` which takes ~200 ms. When the caller passes raw PCM bytes (no container header) the call always fails and the exception is silently swallowed, wasting 200 ms on every invocation. Add a lightweight `_is_audio_container` helper that inspects the first few magic bytes of the input. `load_bytes` now only calls `validate_frame_rate` when the payload is a recognised container format (WAV / MP3 / OGG / FLAC / MP4). Raw PCM buffers skip the validation entirely, eliminating the latency hit. Also remove a stray `import pdb` debug statement. * Apply suggestions from code review Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --------- Co-authored-by: Your Name <[email protected]> Co-authored-by: zhifu gao <[email protected]> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
gemini-code-assist[bot]
Your Name
zhifu gao
parent
5850915506
commit
1045123a5c
@@ -15,7 +15,6 @@ try:
|
||||
from funasr.download.file import download_from_url
|
||||
except:
|
||||
print("urllib is not installed, if you infer from url, please install it first.")
|
||||
import pdb
|
||||
import subprocess
|
||||
from subprocess import CalledProcessError, run
|
||||
|
||||
@@ -144,11 +143,45 @@ def load_audio_text_image_video(
|
||||
return data_or_path_or_list
|
||||
|
||||
|
||||
def _is_audio_container(data: bytes) -> bool:
|
||||
"""Return True if *data* starts with a recognised container-format magic header.
|
||||
|
||||
Raw PCM byte streams have no header, so they will return False and the
|
||||
expensive pydub/ffmpeg validation round-trip can be skipped entirely.
|
||||
"""
|
||||
if len(data) < 4:
|
||||
return False
|
||||
# WAV – RIFF....WAVE
|
||||
if data[:4] == b"RIFF":
|
||||
return True
|
||||
# MP3 – ID3 tag or sync word (0xFF 0xEx)
|
||||
if data[:3] == b"ID3" or (data[0] == 0xFF and (data[1] & 0xE0) == 0xE0):
|
||||
return True
|
||||
# OGG
|
||||
if data[:4] == b"OggS":
|
||||
return True
|
||||
# FLAC
|
||||
if data[:4] == b"fLaC":
|
||||
return True
|
||||
# MP4 / M4A / AAC – 'ftyp' box at offset 4
|
||||
if len(data) >= 8 and data[4:8] == b"ftyp":
|
||||
return True
|
||||
# WebM / MKV
|
||||
if data[:4] == b"\x1a\x45\xdf\xa3":
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def load_bytes(input):
|
||||
try:
|
||||
input = validate_frame_rate(input)
|
||||
except:
|
||||
pass
|
||||
# Only run the (expensive) frame-rate validation when the payload is an
|
||||
# actual audio container (WAV, MP3, OGG, …). Raw PCM buffers have no
|
||||
# recognisable header and would cause pydub to spend ~200 ms before
|
||||
# raising an exception that is then silently swallowed anyway.
|
||||
if _is_audio_container(input):
|
||||
try:
|
||||
input = validate_frame_rate(input)
|
||||
except Exception:
|
||||
pass
|
||||
middle_data = np.frombuffer(input, dtype=np.int16)
|
||||
middle_data = np.asarray(middle_data)
|
||||
if middle_data.dtype.kind not in "iu":
|
||||
|
||||
Reference in New Issue
Block a user