feat: tts语音生成
This commit is contained in:
@@ -1,14 +1,19 @@
|
||||
# websocket_service.py
|
||||
from fastapi import APIRouter, WebSocket, WebSocketDisconnect
|
||||
from typing import Set
|
||||
from aip import AipSpeech
|
||||
from app.constants.asr import APP_ID, API_KEY, SECRET_KEY
|
||||
import json
|
||||
|
||||
# 导入修改后的TTS模块
|
||||
from . import tts
|
||||
|
||||
router = APIRouter()
|
||||
active_connections: Set[WebSocket] = set()
|
||||
|
||||
asr_client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
|
||||
|
||||
|
||||
async def asr_buffer(buffer_data: bytes) -> str:
|
||||
result = asr_client.asr(buffer_data, 'pcm', 16000, {'dev_pid': 1537})
|
||||
if result.get('err_msg') == 'success.':
|
||||
@@ -16,6 +21,7 @@ async def asr_buffer(buffer_data: bytes) -> str:
|
||||
else:
|
||||
return '语音转换失败'
|
||||
|
||||
|
||||
async def broadcast_online_count():
|
||||
data = {"online_count": len(active_connections), 'type': 'count'}
|
||||
to_remove = set()
|
||||
@@ -27,12 +33,14 @@ async def broadcast_online_count():
|
||||
for ws in to_remove:
|
||||
active_connections.remove(ws)
|
||||
|
||||
|
||||
@router.websocket("/websocket")
|
||||
async def websocket_online_count(websocket: WebSocket):
|
||||
await websocket.accept()
|
||||
active_connections.add(websocket)
|
||||
await broadcast_online_count()
|
||||
temp_buffer = bytes()
|
||||
|
||||
try:
|
||||
while True:
|
||||
message = await websocket.receive()
|
||||
@@ -45,15 +53,59 @@ async def websocket_online_count(websocket: WebSocket):
|
||||
except Exception:
|
||||
continue
|
||||
msg_type = data.get("type")
|
||||
|
||||
if msg_type == "ping":
|
||||
await websocket.send_json({"online_count": len(active_connections), "type": "count"})
|
||||
|
||||
elif msg_type == "asr_end":
|
||||
asr_text = await asr_buffer(temp_buffer)
|
||||
await websocket.send_json({"type": "asr_result", "result": asr_text})
|
||||
temp_buffer = bytes()
|
||||
|
||||
# 修改:TTS处理支持消息ID
|
||||
elif msg_type == "tts_text":
|
||||
message_id = data.get("messageId")
|
||||
text = data.get("text", "")
|
||||
|
||||
if not message_id:
|
||||
await websocket.send_json({
|
||||
"type": "tts_error",
|
||||
"message": "缺少messageId参数"
|
||||
})
|
||||
continue
|
||||
|
||||
print(f"收到TTS文本请求 [{message_id}]: {text}")
|
||||
try:
|
||||
await tts.handle_tts_text(websocket, message_id, text)
|
||||
except Exception as e:
|
||||
print(f"TTS文本处理异常 [{message_id}]: {e}")
|
||||
await websocket.send_json({
|
||||
"type": "tts_error",
|
||||
"messageId": message_id,
|
||||
"message": f"TTS处理失败: {str(e)}"
|
||||
})
|
||||
|
||||
elif msg_type == "tts_cancel":
|
||||
message_id = data.get("messageId")
|
||||
if message_id:
|
||||
print(f"收到TTS取消请求 [{message_id}]")
|
||||
try:
|
||||
await tts.handle_tts_cancel(websocket, message_id)
|
||||
except Exception as e:
|
||||
print(f"TTS取消处理异常 [{message_id}]: {e}")
|
||||
|
||||
except WebSocketDisconnect:
|
||||
active_connections.remove(websocket)
|
||||
await broadcast_online_count()
|
||||
except Exception:
|
||||
active_connections.remove(websocket)
|
||||
pass
|
||||
except Exception as e:
|
||||
print(f"WebSocket异常: {e}")
|
||||
finally:
|
||||
# 清理资源
|
||||
active_connections.discard(websocket)
|
||||
|
||||
# 清理所有TTS资源
|
||||
try:
|
||||
await tts.cleanup_connection_tts(websocket)
|
||||
except:
|
||||
pass
|
||||
|
||||
await broadcast_online_count()
|
||||
|
||||
Reference in New Issue
Block a user