Skip to content

Commit

Permalink
upload: create backing track upload handler
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffkaufman committed Jan 25, 2021
1 parent 382e09c commit fc812bf
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 24 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 2 additions & 18 deletions server_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down
45 changes: 45 additions & 0 deletions upload.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit fc812bf

Please sign in to comment.