Files
ai1/client/client.py
2026-02-20 02:24:44 +03:00

55 lines
1.8 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import asyncio
import sounddevice as sd
import numpy as np
import websockets
import json
import requests
# Настройки
VAD_URL = "ws://localhost:8001/audio-stream"
TTS_URL = "http://localhost:8005/synthesize"
SAMPLE_RATE = 16000
BLOCK_SIZE = 1600 # 100 мс при 16 кГц
async def audio_stream():
"""Захватывает аудио с микрофона и отправляет в VAD сервис."""
async with websockets.connect(VAD_URL) as websocket:
print("Подключен к VAD сервису. Говорите...")
def callback(indata, frames, time, status):
"""Отправляет аудио-блок в WebSocket."""
audio_bytes = indata.tobytes()
asyncio.run_coroutine_threadsafe(
websocket.send(audio_bytes), loop
)
stream = sd.InputStream(
samplerate=SAMPLE_RATE,
channels=1,
dtype='int16',
blocksize=BLOCK_SIZE,
callback=callback
)
with stream:
while True:
await asyncio.sleep(0.1)
def play_audio(audio_data):
"""Воспроизводит аудио (полученное от TTS)."""
audio_array = np.frombuffer(audio_data, dtype=np.int16)
sd.play(audio_array, samplerate=SAMPLE_RATE)
sd.wait()
async def main():
# Запускаем поток аудио в фоне
asyncio.create_task(audio_stream())
# Основной цикл не нужен, так как всё работает через WebSocket
await asyncio.Future() # бесконечное ожидание
if __name__ == "__main__":
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(main())