From 7e9696f15635a83ff810cc86b79c0932cb4c8a27 Mon Sep 17 00:00:00 2001
From: Djraemon <154407769+Djraemon@users.noreply.github.com>
Date: 星期一, 04 十一月 2024 11:04:52 +0800
Subject: [PATCH] Fix audio format 2.0 (#2186)

---
 runtime/python/onnxruntime/funasr_onnx/vad_bin.py               |   17 +++++++++++++++++
 runtime/python/onnxruntime/funasr_onnx/paraformer_bin.py        |   16 ++++++++++++++++
 runtime/python/onnxruntime/funasr_onnx/paraformer_online_bin.py |   17 +++++++++++++++++
 runtime/python/onnxruntime/demo_paraformer_online.py            |    3 ++-
 4 files changed, 52 insertions(+), 1 deletions(-)

diff --git a/runtime/python/onnxruntime/demo_paraformer_online.py b/runtime/python/onnxruntime/demo_paraformer_online.py
index bb72b41..cca5357 100644
--- a/runtime/python/onnxruntime/demo_paraformer_online.py
+++ b/runtime/python/onnxruntime/demo_paraformer_online.py
@@ -3,7 +3,8 @@
 from pathlib import Path
 
 model_dir = "damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-online"
-wav_path = ["{}/.cache/modelscope/hub/{}/example/asr_example.wav".format(Path.home(), model_dir)]
+#wav_path = ["{}/.cache/modelscope/hub/{}/example/asr_example.wav".format(Path.home(), model_dir)]
+wav_path = "{}/.cache/modelscope/hub/{}/example/asr_example.wav".format(Path.home(), model_dir)
 
 chunk_size = [5, 10, 5]
 model = Paraformer(
diff --git a/runtime/python/onnxruntime/funasr_onnx/paraformer_bin.py b/runtime/python/onnxruntime/funasr_onnx/paraformer_bin.py
index 4f35fcc..bde7f44 100644
--- a/runtime/python/onnxruntime/funasr_onnx/paraformer_bin.py
+++ b/runtime/python/onnxruntime/funasr_onnx/paraformer_bin.py
@@ -175,7 +175,23 @@
         plt.savefig(plotname, bbox_inches="tight")
 
     def load_data(self, wav_content: Union[str, np.ndarray, List[str]], fs: int = None) -> List:
+        def convert_to_wav(input_path, output_path):
+            from pydub import AudioSegment
+            try:
+                audio = AudioSegment.from_mp3(input_path)
+                audio.export(output_path, format="wav")
+                print("闊抽鏂囦欢涓簃p3鏍煎紡锛屽凡杞崲涓簑av鏍煎紡")
+                
+            except Exception as e:
+                print(f"杞崲澶辫触:{e}")
+
         def load_wav(path: str) -> np.ndarray:
+            if not path.lower().endswith('.wav'):
+                import os
+                input_path = path
+                path = os.path.splitext(path)[0]+'.wav'
+                convert_to_wav(input_path,path) #灏唌p3鏍煎紡杞崲鎴恮av鏍煎紡
+
             waveform, _ = librosa.load(path, sr=fs)
             return waveform
 
diff --git a/runtime/python/onnxruntime/funasr_onnx/paraformer_online_bin.py b/runtime/python/onnxruntime/funasr_onnx/paraformer_online_bin.py
index ddd857d..3f63ea0 100644
--- a/runtime/python/onnxruntime/funasr_onnx/paraformer_online_bin.py
+++ b/runtime/python/onnxruntime/funasr_onnx/paraformer_online_bin.py
@@ -208,7 +208,24 @@
         return asr_res
 
     def load_data(self, wav_content: Union[str, np.ndarray, List[str]], fs: int = None) -> List:
+        
+        def convert_to_wav(input_path, output_path):
+            from pydub import AudioSegment
+            try:
+                audio = AudioSegment.from_mp3(input_path)
+                audio.export(output_path, format="wav")
+                print("闊抽鏂囦欢涓簃p3鏍煎紡锛屽凡杞崲涓簑av鏍煎紡")
+                
+            except Exception as e:
+                print(f"杞崲澶辫触:{e}")
+
         def load_wav(path: str) -> np.ndarray:
+            if not path.lower().endswith('.wav'):
+                import os
+                input_path = path
+                path = os.path.splitext(path)[0]+'.wav'
+                convert_to_wav(input_path,path) #灏唌p3鏍煎紡杞崲鎴恮av鏍煎紡
+
             waveform, _ = librosa.load(path, sr=fs)
             return waveform
 
diff --git a/runtime/python/onnxruntime/funasr_onnx/vad_bin.py b/runtime/python/onnxruntime/funasr_onnx/vad_bin.py
index 92928a8..3f21004 100644
--- a/runtime/python/onnxruntime/funasr_onnx/vad_bin.py
+++ b/runtime/python/onnxruntime/funasr_onnx/vad_bin.py
@@ -143,7 +143,24 @@
         return segments
 
     def load_data(self, wav_content: Union[str, np.ndarray, List[str]], fs: int = None) -> List:
+        
+        def convert_to_wav(input_path, output_path):
+            from pydub import AudioSegment
+            try:
+                audio = AudioSegment.from_mp3(input_path)
+                audio.export(output_path, format="wav")
+                print("闊抽鏂囦欢涓簃p3鏍煎紡锛屽凡杞崲涓簑av鏍煎紡")
+                
+            except Exception as e:
+                print(f"杞崲澶辫触:{e}")
+
         def load_wav(path: str) -> np.ndarray:
+            if not path.lower().endswith('.wav'):
+                import os
+                input_path = path
+                path = os.path.splitext(path)[0]+'.wav'
+                convert_to_wav(input_path,path) #灏唌p3鏍煎紡杞崲鎴恮av鏍煎紡
+
             waveform, _ = librosa.load(path, sr=fs)
             return waveform
 

--
Gitblit v1.9.1