diff --git a/data/starwars.wav b/data/starwars.wav new file mode 100644 index 0000000..1a2dd7a Binary files /dev/null and b/data/starwars.wav differ diff --git a/data/trying.wav b/data/trying.wav new file mode 100644 index 0000000..f86c5a2 Binary files /dev/null and b/data/trying.wav differ diff --git a/output b/output new file mode 100644 index 0000000..bf8dbdb Binary files /dev/null and b/output differ diff --git a/play_audio/simple_play.py b/play_audio/simple_play.py new file mode 100644 index 0000000..d3c0118 --- /dev/null +++ b/play_audio/simple_play.py @@ -0,0 +1,6 @@ +import simpleaudio as sa + +filename = 'myfile.wav' +wave_obj = sa.WaveObject.from_wave_file(filename) +play_obj = wave_obj.play() +play_obj.wait_done() # Wait until sound has finished playing \ No newline at end of file diff --git a/requirement.in b/requirement.in new file mode 100644 index 0000000..08bcfdd --- /dev/null +++ b/requirement.in @@ -0,0 +1 @@ +simpleaudio \ No newline at end of file diff --git a/requirement.txt b/requirement.txt new file mode 100644 index 0000000..1dbd98e --- /dev/null +++ b/requirement.txt @@ -0,0 +1,8 @@ +# +# This file is autogenerated by pip-compile with python 3.8 +# To update, run: +# +# pip-compile requirement.in +# +simpleaudio==1.0.4 + # via -r requirement.in diff --git a/socket_send_recieve/README.md b/socket_send_recieve/README.md new file mode 100644 index 0000000..879d219 --- /dev/null +++ b/socket_send_recieve/README.md @@ -0,0 +1,17 @@ +# Socket Send & Receive + +This example is loosely built from knowledge gained in reading https://defn.io/2018/02/25/web-app-from-scratch-01/ series. + +## Why? + +I needed a simple way to pipe the contents of a wave file produced by a Text to Speech model to a remote device (raspberrypi). + +## Usage + +The `socket_receiver.py` is intended to be a single connection reciever of binary wave files. You +can use `sender.py ` to marshal a `.wav` file to the receiver, which immediately plays +back the contents. The reciever then waits for the next connection to repeat the process. + +## Producing sample clips + +I ran into some trouble initially producing a valid `.wav` file using Audacity. I did find that a Mono channel, 22050 Hz, file using the `signed 16bit PCM` encoding worked. diff --git a/socket_send_recieve/sender.py b/socket_send_recieve/sender.py new file mode 100644 index 0000000..3ee8dfa --- /dev/null +++ b/socket_send_recieve/sender.py @@ -0,0 +1,21 @@ +import socket +import sys + +# TODO Paramaterize the server target values +# TODO use python logger + +if len(sys.argv) < 2: + raise ValueError("Missing input wav file") + +wave_file_path = sys.argv[1] + +if not wave_file_path.endswith('.wav'): + raise ValueError("File must have .wav file extension.") + +(HOST,PORT)=('localhost',9000) +s=socket.socket(socket.AF_INET,socket.SOCK_STREAM); s.connect((HOST,PORT)) + +with open(wave_file_path, 'rb') as wave_file: + s.sendfile(wave_file) +s.close() + diff --git a/socket_send_recieve/socket_receiver.py b/socket_send_recieve/socket_receiver.py new file mode 100644 index 0000000..d1d1817 --- /dev/null +++ b/socket_send_recieve/socket_receiver.py @@ -0,0 +1,47 @@ +import socket +import time + +import simpleaudio as sa + +HOST = "127.0.0.1" +PORT = 9000 + +def save_file(client_socket): + timestamp = str(int(time.time())) + file_name = f"output_{timestamp}.wav" + with open(file_name,'wb') as f: + while True: + l = client_socket.recv(1024) + if not l: break + f.write(l) + +def play_file(conn): + # you could consider sa.play_buffer ... I don't know if it would be more performant. + try: + wave_object = sa.WaveObject.from_wave_file(conn.makefile(mode="rb")) + play_obj = wave_object.play() + play_obj.wait_done() + except Exception as e: + print(f"Hit an error: {e} skipping playback") + return + +# By default, socket.socket creates TCP sockets. +with socket.socket() as server_sock: + # This tells the kernel to reuse sockets that are in `TIME_WAIT` state. + server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + + # This tells the socket what address to bind to. + server_sock.bind((HOST, PORT)) + + # 0 is the number of pending connections the socket may have before + # new connections are refused. Since this server is going to process + # one connection at a time, we want to refuse any additional connections. + server_sock.listen(0) + print(f"Listening on {HOST}:{PORT}...") + + while True: + # This is a blocking call as we wait for a new connection + client_sock, client_addr = server_sock.accept() + print(f"New connection from {client_addr}.") + with client_sock as conn: + play_file(conn) \ No newline at end of file