From 33d3d2084403fd34b79c835d2f2fe04f6cd8f738 Mon Sep 17 00:00:00 2001
From: 游雁 <zhifu.gzf@alibaba-inc.com>
Date: 星期三, 13 九月 2023 09:33:54 +0800
Subject: [PATCH] Merge branch 'main' of github.com:alibaba-damo-academy/FunASR add
---
funasr/utils/asr_utils.py | 63 +++++++++++++++++++------------
1 files changed, 38 insertions(+), 25 deletions(-)
diff --git a/funasr/utils/asr_utils.py b/funasr/utils/asr_utils.py
index a3ff3e3..5aa40ec 100644
--- a/funasr/utils/asr_utils.py
+++ b/funasr/utils/asr_utils.py
@@ -5,6 +5,7 @@
from typing import Any, Dict, List, Union
import torchaudio
+import soundfile
import numpy as np
import pkg_resources
from modelscope.utils.logger import get_logger
@@ -27,7 +28,7 @@
def sample_rate_checking(audio_in: Union[str, bytes], audio_format: str):
r_audio_fs = None
- if audio_format == 'wav':
+ if audio_format == 'wav' or audio_format == 'scp':
r_audio_fs = get_sr_from_wav(audio_in)
elif audio_format == 'pcm' and isinstance(audio_in, bytes):
r_audio_fs = get_sr_from_bytes(audio_in)
@@ -58,14 +59,15 @@
if r_recog_type is None and audio_in is not None:
# audio_in is wav, recog_type is wav_file
if os.path.isfile(audio_in):
- audio_type = os.path.basename(audio_in).split(".")[-1].lower()
- if audio_type in SUPPORT_AUDIO_TYPE_SETS:
- r_recog_type = 'wav'
- r_audio_format = 'wav'
- elif audio_type == "scp":
+ audio_type = os.path.basename(audio_in).lower()
+ for support_audio_type in SUPPORT_AUDIO_TYPE_SETS:
+ if audio_type.rfind(".{}".format(support_audio_type)) >= 0:
+ r_recog_type = 'wav'
+ r_audio_format = 'wav'
+ if audio_type.rfind(".scp") >= 0:
r_recog_type = 'wav'
r_audio_format = 'scp'
- else:
+ if r_recog_type is None:
raise NotImplementedError(
f'Not supported audio type: {audio_type}')
@@ -128,21 +130,31 @@
def get_sr_from_wav(fname: str):
fs = None
if os.path.isfile(fname):
- audio_type = os.path.basename(fname).split(".")[-1].lower()
- if audio_type in SUPPORT_AUDIO_TYPE_SETS:
- if audio_type == "pcm":
- fs = None
- else:
- audio, fs = torchaudio.load(fname)
+ audio_type = os.path.basename(fname).lower()
+ for support_audio_type in SUPPORT_AUDIO_TYPE_SETS:
+ if audio_type.rfind(".{}".format(support_audio_type)) >= 0:
+ if support_audio_type == "pcm":
+ fs = None
+ else:
+ try:
+ audio, fs = torchaudio.load(fname)
+ except:
+ audio, fs = soundfile.read(fname)
+ break
+ if audio_type.rfind(".scp") >= 0:
+ with open(fname, encoding="utf-8") as f:
+ for line in f:
+ wav_path = line.split()[1]
+ fs = get_sr_from_wav(wav_path)
+ if fs is not None:
+ break
return fs
elif os.path.isdir(fname):
dir_files = os.listdir(fname)
for file in dir_files:
file_path = os.path.join(fname, file)
if os.path.isfile(file_path):
- audio_type = os.path.basename(file_path).split(".")[-1].lower()
- if audio_type in SUPPORT_AUDIO_TYPE_SETS:
- fs = get_sr_from_wav(file_path)
+ fs = get_sr_from_wav(file_path)
elif os.path.isdir(file_path):
fs = get_sr_from_wav(file_path)
@@ -158,12 +170,12 @@
file_path = os.path.join(dir_path, file)
if os.path.isfile(file_path):
if ends == ".wav" or ends == ".WAV":
- audio_type = os.path.basename(file_path).split(".")[-1].lower()
- if audio_type in SUPPORT_AUDIO_TYPE_SETS:
- return True
- else:
- raise NotImplementedError(
- f'Not supported audio type: {audio_type}')
+ audio_type = os.path.basename(file_path).lower()
+ for support_audio_type in SUPPORT_AUDIO_TYPE_SETS:
+ if audio_type.rfind(".{}".format(support_audio_type)) >= 0:
+ return True
+ raise NotImplementedError(
+ f'Not supported audio type: {audio_type}')
elif file_path.endswith(ends):
return True
elif os.path.isdir(file_path):
@@ -178,9 +190,10 @@
for file in dir_files:
file_path = os.path.join(dir_path, file)
if os.path.isfile(file_path):
- audio_type = os.path.basename(file_path).split(".")[-1].lower()
- if audio_type in SUPPORT_AUDIO_TYPE_SETS:
- wav_list.append(file_path)
+ audio_type = os.path.basename(file_path).lower()
+ for support_audio_type in SUPPORT_AUDIO_TYPE_SETS:
+ if audio_type.rfind(".{}".format(support_audio_type)) >= 0:
+ wav_list.append(file_path)
elif os.path.isdir(file_path):
recursion_dir_all_wav(wav_list, file_path)
--
Gitblit v1.9.1