Working raw tcp implementation
parent
43850b2269
commit
27ae98347e
Binary file not shown.
Binary file not shown.
@ -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
|
@ -0,0 +1 @@
|
|||||||
|
simpleaudio
|
@ -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
|
@ -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 <wave_file_path>` 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.
|
@ -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()
|
||||||
|
|
@ -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)
|
Loading…
Reference in New Issue