From 8a7573f16977d5deb32e8e2764f9fcddedec9be7 Mon Sep 17 00:00:00 2001 From: Looki2000 Date: Tue, 5 Dec 2023 01:40:59 +0100 Subject: [PATCH] Added UDP raw audio stream player script --- .gitignore | 4 +- .../udp raw audio stream player.py | 68 +++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 udp raw audio player/udp raw audio stream player.py diff --git a/.gitignore b/.gitignore index 5102e1d..cd1db50 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,10 @@ finetune_dialogs_tool/temp/ finetune_dialogs_tool/output_dialogs/ -finetune_dialogs_tool/__pycache__/ frontend/voices/* !frontend/voices/lector.wav !frontend/voices/lector source.txt -frontend/__pycache__/ frontend/dupa.py + +*/__pycache__/ \ No newline at end of file diff --git a/udp raw audio player/udp raw audio stream player.py b/udp raw audio player/udp raw audio stream player.py new file mode 100644 index 0000000..e7b7544 --- /dev/null +++ b/udp raw audio player/udp raw audio stream player.py @@ -0,0 +1,68 @@ +import socket +import psutil +import pyaudio + +# This streamer can be used to preview assistant audio for example for presentation. +# For this you have to set OBS to following ffmpeg streaming settings: +# 48khz +# stereo +# PCM signed 16bit little endian + + + +# get local ip adress of main network interface +interfaces = psutil.net_if_addrs() +for interface, addrs in interfaces.items(): + if "Wi-Fi" in interface: + print("name: ", interface) + for addr in addrs: + if addr.family == socket.AF_INET: + UDP_IP = addr.address + print("ip: ", UDP_IP) + break + break + +UDP_PORT = 8081 + +print("port: ", UDP_PORT) + +# PyAudio parameters +CHANNELS = 2 + +# 16-bit audio +WIDTH = 2 +RATE = 48000 + +BUFFER_SIZE = 2048 + + +# Create a UDP socket +sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) +sock.bind((UDP_IP, UDP_PORT)) + +# Initialize PyAudio +p = pyaudio.PyAudio() +stream = p.open(format=p.get_format_from_width(WIDTH), + channels=CHANNELS, + rate=RATE, + output=True) + +print("Stream started. Press Ctrl+C to stop.") + +try: + while True: + # Receive audio data + data, addr = sock.recvfrom(BUFFER_SIZE) + + # Play the audio data + stream.write(data) + +except KeyboardInterrupt: + print("\nStreaming stopped.") + +finally: + # Close the stream and socket + stream.stop_stream() + stream.close() + p.terminate() + sock.close()