update:抽离公共方法save_audio_to_file到父类

This commit is contained in:
hrz
2025-05-29 10:38:01 +08:00
parent 625d079168
commit d86a2cf9de
8 changed files with 19 additions and 114 deletions
+16 -4
View File
@@ -1,7 +1,10 @@
from abc import ABC, abstractmethod
from typing import Optional, Tuple, List
import os
import uuid
import wave
import opuslib_next
from abc import ABC, abstractmethod
from config.logger import setup_logging
from typing import Optional, Tuple, List
TAG = __name__
logger = setup_logging()
@@ -11,10 +14,19 @@ class ASRProviderBase(ABC):
def __init__(self):
self.audio_format = "opus"
@abstractmethod
def save_audio_to_file(self, pcm_data: List[bytes], session_id: str) -> str:
"""PCM数据保存为WAV文件"""
pass
module_name = __name__.split(".")[-1]
file_name = f"asr_{module_name}_{session_id}_{uuid.uuid4()}.wav"
file_path = os.path.join(self.output_dir, file_name)
with wave.open(file_path, "wb") as wf:
wf.setnchannels(1)
wf.setsampwidth(2) # 2 bytes = 16-bit
wf.setframerate(16000)
wf.writeframes(b"".join(pcm_data))
return file_path
@abstractmethod
async def speech_to_text(