diff --git a/README.md b/README.md index 70d39bc..6c30f78 100644 --- a/README.md +++ b/README.md @@ -66,9 +66,7 @@ localhost. The app isn't currently set up for that, but it could be. Backing tracks are 16-bit 1-channel 48k wav files. You can make one with: - $ lame --decode input.mp3 intermediate-a.wav && \ - sox intermediate-a.wav intermediate-b.wav remix 1 && \ - sox intermediate-b.wav -r 48000 output.wav + $ sox input.mp3 -r 48000 output.wav remix 1 This should look like: @@ -108,7 +106,7 @@ sudo apt update sudo apt upgrade sudo apt install python3-distutils uuid-dev libcap-dev libpcre3-dev \ nginx python3-pip emacs letsencrypt opus-tools \ - python3-certbot-nginx + python3-certbot-nginx sox libsox-fmt-mp3 sudo python3 -mpip install uwsgi mkdir ~/src cd ~/src && git clone https://github.com/gwillen/solstice-audio-test.git diff --git a/server.py b/server.py index 5a50fb2..996400d 100755 --- a/server.py +++ b/server.py @@ -167,7 +167,7 @@ def clear_whole_buffer(): # For volume scaling. N_PHANTOM_PEOPLE = 2 -AUDIO_DIR = os.path.join(os.path.dirname(__file__), "audio") + RECORDINGS_DIRNAME = "recordings" RECORDINGS_DIR = os.path.join( os.path.dirname(__file__), "html", RECORDINGS_DIRNAME) @@ -302,7 +302,7 @@ def cleanup_(self): tracks = [] def populate_tracks() -> None: - for track in sorted(os.listdir(AUDIO_DIR)): + for track in sorted(os.listdir(util.AUDIO_DIR)): if track != "README": tracks.append(track) tracks.append(METRONOME) diff --git a/server_wrapper.py b/server_wrapper.py index cd5f3bd..0d19653 100755 --- a/server_wrapper.py +++ b/server_wrapper.py @@ -5,7 +5,7 @@ import urllib.parse import numpy as np # type:ignore import opuslib # type:ignore -import traceback +import util import time import struct @@ -252,22 +252,6 @@ def do_GET(environ, start_response) -> None: # If we give a 0-byte response, Chrome Dev Tools gives a misleading error (see https://stackoverflow.com/questions/57477805/why-do-i-get-fetch-failed-loading-when-it-actually-worked) return b'ok', -def die500(start_response, e): - # This is slightly sketchy: this assumes we are currently in the middle - # of an exception handler for the exception e (which happens to be - # true.) - trb = traceback.format_exc().encode("utf-8") - start_response('500 Internal Server Error', [ - ('Content-Type', 'text/plain'), - ("Access-Control-Allow-Origin", "*"), - ("Access-Control-Max-Age", "86400"), - ("Access-Control-Expose-Headers", "X-Audio-Metadata"), - ("X-Audio-Metadata", json.dumps({ - "kill_client": True, - "message": str(e) - }))]) - return trb, - # POST requests absolutely must have a numeric user_id for all requests which # make it as far as handle_post; such requests must be associated with a user # or there's nothing we can do with them, and they will fail. @@ -315,7 +299,7 @@ def do_POST(environ, start_response) -> None: del users[userid] # Log it print("Request raised exception!\nParams:", query_string, "\n", traceback.format_exc(), file=sys.stderr) - return die500(start_response, e) + return util.die500(start_response, e) combined_data = x_audio_metadata.encode('utf-8') + data diff --git a/upload.py b/upload.py new file mode 100644 index 0000000..c3e27b5 --- /dev/null +++ b/upload.py @@ -0,0 +1,45 @@ +import util +import tempfile +import subprocess +import traceback +import sys + +def decode_and_save(in_data_raw): + with tempfile.NamedTemporaryFile() as tmp_upload: + tmp_upload.write(in_data_raw) + tmp_upload.flush() + + subprocess.check_call([ + "sox", + "-t", "mp3", tmp_upload.name, + "-r", "48000", + "-t", "wav", util.UPLOAD_FNAME, + "remix", "1"]) + +def application(environ, start_response): + try: + content_length = int(environ.get('CONTENT_LENGTH', 0)) + in_data_raw = environ['wsgi.input'].read(content_length) + + query_string = environ['QUERY_STRING'] + + if len(query_string) > 0: + query_params = urllib.parse.parse_qs(query_string, strict_parsing=True) + else: + query_params = {} + + decode_and_save(in_data_raw) + + start_response('200 OK', [("Content-Type", "text/plain")]) + return b"ok", + except Exception as e: + print("ERROR:", query_string, "\n", traceback.\ + format_exc(), file=sys.stderr) + return util.die500(start_response, e) + +def serve(): + from wsgiref.simple_server import make_server + make_server(b'',8082,application).serve_forever() + +if __name__ == "__main__": + serve()