| 1 | import asyncio |
| 2 | import logging |
| 3 | import socket |
| 4 | import time |
| 5 | |
| 6 | import numpy as np |
| 7 | import pyaudio |
| 8 | |
| 9 | |
| 10 | logging.basicConfig(level=logging.INFO) |
| 11 | logger = logging.getLogger(__name__) |
| 12 | |
| 13 | |
| 14 | async def listen_to_F5TTS(text, server_ip="localhost", server_port=9998): |
| 15 | client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 16 | await asyncio.get_event_loop().run_in_executor(None, client_socket.connect, (server_ip, int(server_port))) |
| 17 | |
| 18 | start_time = time.time() |
| 19 | first_chunk_time = None |
| 20 | |
| 21 | async def play_audio_stream(): |
| 22 | nonlocal first_chunk_time |
| 23 | p = pyaudio.PyAudio() |
| 24 | stream = p.open(format=pyaudio.paFloat32, channels=1, rate=24000, output=True, frames_per_buffer=2048) |
| 25 | |
| 26 | try: |
| 27 | while True: |
| 28 | data = await asyncio.get_event_loop().run_in_executor(None, client_socket.recv, 8192) |
| 29 | if not data: |
| 30 | break |
| 31 | if data == b"END": |
| 32 | logger.info("End of audio received.") |
| 33 | break |
| 34 | |
| 35 | audio_array = np.frombuffer(data, dtype=np.float32) |
| 36 | stream.write(audio_array.tobytes()) |
| 37 | |
| 38 | if first_chunk_time is None: |
| 39 | first_chunk_time = time.time() |
| 40 | |
| 41 | finally: |
| 42 | stream.stop_stream() |
| 43 | stream.close() |
| 44 | p.terminate() |
| 45 | |
| 46 | logger.info(f"Total time taken: {time.time() - start_time:.4f} seconds") |
| 47 | |
| 48 | try: |
| 49 | data_to_send = f"{text}".encode("utf-8") |
| 50 | await asyncio.get_event_loop().run_in_executor(None, client_socket.sendall, data_to_send) |
| 51 | await play_audio_stream() |
| 52 | |
| 53 | except Exception as e: |
| 54 | logger.error(f"Error in listen_to_F5TTS: {e}") |
| 55 | |
| 56 | finally: |
| 57 | client_socket.close() |
| 58 | |
| 59 | |
| 60 | if __name__ == "__main__": |
| 61 | text_to_send = "As a Reader assistant, I'm familiar with new technology. which are key to its improved performance in terms of both training speed and inference efficiency. Let's break down the components" |
| 62 | |
| 63 | asyncio.run(listen_to_F5TTS(text_to_send)) |
| 64 |