mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-27 04:49:47 +01:00
update:抽离公共方法save_audio_to_file到父类
This commit is contained in:
parent
625d079168
commit
d86a2cf9de
8 changed files with 19 additions and 114 deletions
|
|
@ -3,8 +3,6 @@ import json
|
||||||
import asyncio
|
import asyncio
|
||||||
from typing import Optional, Tuple, List
|
from typing import Optional, Tuple, List
|
||||||
import opuslib_next
|
import opuslib_next
|
||||||
import wave
|
|
||||||
import io
|
|
||||||
import os
|
import os
|
||||||
import uuid
|
import uuid
|
||||||
import hmac
|
import hmac
|
||||||
|
|
@ -165,21 +163,6 @@ class ASRProvider(ASRProviderBase):
|
||||||
request += "&enable_voice_detection=false"
|
request += "&enable_voice_detection=false"
|
||||||
return request
|
return request
|
||||||
|
|
||||||
def save_audio_to_file(self, pcm_data: List[bytes], session_id: str) -> str:
|
|
||||||
"""PCM数据保存为WAV文件"""
|
|
||||||
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) # 16-bit
|
|
||||||
wf.setframerate(self.sample_rate)
|
|
||||||
wf.writeframes(b"".join(pcm_data))
|
|
||||||
|
|
||||||
logger.bind(tag=TAG).debug(f"音频文件已保存至: {file_path}")
|
|
||||||
return file_path
|
|
||||||
|
|
||||||
async def _send_request(self, pcm_data: bytes) -> Optional[str]:
|
async def _send_request(self, pcm_data: bytes) -> Optional[str]:
|
||||||
"""发送请求到阿里云ASR服务"""
|
"""发送请求到阿里云ASR服务"""
|
||||||
try:
|
try:
|
||||||
|
|
|
||||||
|
|
@ -36,20 +36,6 @@ class ASRProvider(ASRProviderBase):
|
||||||
# 确保输出目录存在
|
# 确保输出目录存在
|
||||||
os.makedirs(self.output_dir, exist_ok=True)
|
os.makedirs(self.output_dir, exist_ok=True)
|
||||||
|
|
||||||
def save_audio_to_file(self, pcm_data: List[bytes], session_id: str) -> str:
|
|
||||||
"""PCM数据保存为WAV文件"""
|
|
||||||
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
|
|
||||||
|
|
||||||
async def speech_to_text(
|
async def speech_to_text(
|
||||||
self, opus_data: List[bytes], session_id: str
|
self, opus_data: List[bytes], session_id: str
|
||||||
) -> Tuple[Optional[str], Optional[str]]:
|
) -> Tuple[Optional[str], Optional[str]]:
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,10 @@
|
||||||
from abc import ABC, abstractmethod
|
import os
|
||||||
from typing import Optional, Tuple, List
|
import uuid
|
||||||
|
import wave
|
||||||
import opuslib_next
|
import opuslib_next
|
||||||
|
from abc import ABC, abstractmethod
|
||||||
from config.logger import setup_logging
|
from config.logger import setup_logging
|
||||||
|
from typing import Optional, Tuple, List
|
||||||
|
|
||||||
TAG = __name__
|
TAG = __name__
|
||||||
logger = setup_logging()
|
logger = setup_logging()
|
||||||
|
|
@ -11,10 +14,19 @@ class ASRProviderBase(ABC):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.audio_format = "opus"
|
self.audio_format = "opus"
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def save_audio_to_file(self, pcm_data: List[bytes], session_id: str) -> str:
|
def save_audio_to_file(self, pcm_data: List[bytes], session_id: str) -> str:
|
||||||
"""PCM数据保存为WAV文件"""
|
"""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
|
@abstractmethod
|
||||||
async def speech_to_text(
|
async def speech_to_text(
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,13 @@
|
||||||
import time
|
import time
|
||||||
import io
|
|
||||||
import wave
|
|
||||||
import os
|
import os
|
||||||
from typing import Optional, Tuple, List
|
|
||||||
import uuid
|
import uuid
|
||||||
import websockets
|
|
||||||
import json
|
import json
|
||||||
import gzip
|
import gzip
|
||||||
|
import websockets
|
||||||
import opuslib_next
|
from config.logger import setup_logging
|
||||||
|
from typing import Optional, Tuple, List
|
||||||
from core.providers.asr.base import ASRProviderBase
|
from core.providers.asr.base import ASRProviderBase
|
||||||
|
|
||||||
from config.logger import setup_logging
|
|
||||||
|
|
||||||
TAG = __name__
|
TAG = __name__
|
||||||
logger = setup_logging()
|
logger = setup_logging()
|
||||||
|
|
@ -102,20 +98,6 @@ class ASRProvider(ASRProviderBase):
|
||||||
# 确保输出目录存在
|
# 确保输出目录存在
|
||||||
os.makedirs(self.output_dir, exist_ok=True)
|
os.makedirs(self.output_dir, exist_ok=True)
|
||||||
|
|
||||||
def save_audio_to_file(self, pcm_data: List[bytes], session_id: str) -> str:
|
|
||||||
"""PCM数据保存为WAV文件"""
|
|
||||||
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
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _generate_header(
|
def _generate_header(
|
||||||
message_type=CLIENT_FULL_REQUEST, message_type_specific_flags=NO_SEQUENCE
|
message_type=CLIENT_FULL_REQUEST, message_type_specific_flags=NO_SEQUENCE
|
||||||
|
|
|
||||||
|
|
@ -53,20 +53,6 @@ class ASRProvider(ASRProviderBase):
|
||||||
# device="cuda:0", # 启用GPU加速
|
# device="cuda:0", # 启用GPU加速
|
||||||
)
|
)
|
||||||
|
|
||||||
def save_audio_to_file(self, pcm_data: List[bytes], session_id: str) -> str:
|
|
||||||
"""PCM数据保存为WAV文件"""
|
|
||||||
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
|
|
||||||
|
|
||||||
async def speech_to_text(
|
async def speech_to_text(
|
||||||
self, opus_data: List[bytes], session_id: str
|
self, opus_data: List[bytes], session_id: str
|
||||||
) -> Tuple[Optional[str], Optional[str]]:
|
) -> Tuple[Optional[str], Optional[str]]:
|
||||||
|
|
|
||||||
|
|
@ -43,20 +43,6 @@ class ASRProvider(ASRProviderBase):
|
||||||
self.ssl_context.check_hostname = False
|
self.ssl_context.check_hostname = False
|
||||||
self.ssl_context.verify_mode = ssl.CERT_NONE
|
self.ssl_context.verify_mode = ssl.CERT_NONE
|
||||||
|
|
||||||
def save_audio_to_file(self, pcm_data: List[bytes], session_id: str) -> str:
|
|
||||||
"""PCM数据保存为WAV文件"""
|
|
||||||
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
|
|
||||||
|
|
||||||
async def _receive_responses(self, ws) -> None:
|
async def _receive_responses(self, ws) -> None:
|
||||||
"""
|
"""
|
||||||
Asynchronous generator to receive messages from the WebSocket.
|
Asynchronous generator to receive messages from the WebSocket.
|
||||||
|
|
|
||||||
|
|
@ -84,20 +84,6 @@ class ASRProvider(ASRProviderBase):
|
||||||
use_itn=True,
|
use_itn=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
def save_audio_to_file(self, pcm_data: List[bytes], session_id: str) -> str:
|
|
||||||
"""PCM数据保存为WAV文件"""
|
|
||||||
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
|
|
||||||
|
|
||||||
def read_wave(self, wave_filename: str) -> Tuple[np.ndarray, int]:
|
def read_wave(self, wave_filename: str) -> Tuple[np.ndarray, int]:
|
||||||
"""
|
"""
|
||||||
Args:
|
Args:
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,6 @@ import os
|
||||||
import uuid
|
import uuid
|
||||||
from typing import Optional, Tuple, List
|
from typing import Optional, Tuple, List
|
||||||
import wave
|
import wave
|
||||||
import opuslib_next
|
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from core.providers.asr.base import ASRProviderBase
|
from core.providers.asr.base import ASRProviderBase
|
||||||
from config.logger import setup_logging
|
from config.logger import setup_logging
|
||||||
|
|
@ -33,20 +31,6 @@ class ASRProvider(ASRProviderBase):
|
||||||
# 确保输出目录存在
|
# 确保输出目录存在
|
||||||
os.makedirs(self.output_dir, exist_ok=True)
|
os.makedirs(self.output_dir, exist_ok=True)
|
||||||
|
|
||||||
def save_audio_to_file(self, pcm_data: List[bytes], session_id: str) -> str:
|
|
||||||
"""PCM数据保存为WAV文件"""
|
|
||||||
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
|
|
||||||
|
|
||||||
async def speech_to_text(
|
async def speech_to_text(
|
||||||
self, opus_data: List[bytes], session_id: str
|
self, opus_data: List[bytes], session_id: str
|
||||||
) -> Tuple[Optional[str], Optional[str]]:
|
) -> Tuple[Optional[str], Optional[str]]:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue