2020-10-23 20:10:32 +00:00
|
|
|
import multiprocessing
|
2020-10-24 13:44:26 +00:00
|
|
|
import player
|
2020-10-24 20:31:52 +00:00
|
|
|
from flask import Flask, render_template, send_from_directory, request
|
2020-10-23 20:10:32 +00:00
|
|
|
import json
|
2020-10-23 21:58:53 +00:00
|
|
|
import sounddevice as sd
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2020-10-24 20:31:52 +00:00
|
|
|
|
|
|
|
class BAPSicleServer():
|
|
|
|
def __init__(self):
|
|
|
|
startServer()
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
stopServer()
|
|
|
|
|
2020-10-26 21:25:02 +00:00
|
|
|
def get_flask(self):
|
|
|
|
return app
|
|
|
|
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2020-10-24 02:13:02 +00:00
|
|
|
app = Flask(__name__, static_url_path='')
|
2020-10-23 20:10:32 +00:00
|
|
|
|
|
|
|
channel_to_q = []
|
|
|
|
channel_from_q = []
|
|
|
|
channel_p = []
|
|
|
|
|
|
|
|
|
2020-10-24 12:47:48 +00:00
|
|
|
@app.errorhandler(404)
|
|
|
|
def page_not_found(e):
|
|
|
|
data = {
|
2020-10-24 14:04:33 +00:00
|
|
|
'ui_page': "404",
|
|
|
|
"ui_title": "404"
|
2020-10-24 12:47:48 +00:00
|
|
|
}
|
|
|
|
return render_template('404.html', data=data), 404
|
2020-10-23 20:10:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/")
|
2020-10-24 02:13:02 +00:00
|
|
|
def ui_index():
|
|
|
|
data = {
|
2020-10-24 14:04:33 +00:00
|
|
|
'ui_page': "index",
|
|
|
|
"ui_title": ""
|
2020-10-24 02:13:02 +00:00
|
|
|
}
|
|
|
|
return render_template('index.html', data=data)
|
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
|
|
|
|
@app.route("/config")
|
|
|
|
def ui_config():
|
2020-10-23 21:58:53 +00:00
|
|
|
channel_states = []
|
2020-10-23 20:10:32 +00:00
|
|
|
for i in range(3):
|
2020-10-24 14:04:33 +00:00
|
|
|
channel_states.append(details(i))
|
2020-10-23 21:58:53 +00:00
|
|
|
|
|
|
|
devices = sd.query_devices()
|
|
|
|
outputs = []
|
2020-10-24 14:04:33 +00:00
|
|
|
|
2020-10-23 21:58:53 +00:00
|
|
|
for device in devices:
|
2020-10-24 14:04:33 +00:00
|
|
|
if device["max_output_channels"] > 0:
|
|
|
|
outputs.append(device)
|
|
|
|
|
|
|
|
data = {
|
|
|
|
'channels': channel_states,
|
|
|
|
'outputs': outputs,
|
|
|
|
'ui_page': "config",
|
|
|
|
"ui_title": "Config"
|
|
|
|
}
|
|
|
|
return render_template('config.html', data=data)
|
2020-10-23 21:58:53 +00:00
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
|
|
|
|
@app.route("/status")
|
|
|
|
def ui_status():
|
|
|
|
channel_states = []
|
|
|
|
for i in range(3):
|
|
|
|
channel_states.append(details(i))
|
2020-10-23 21:58:53 +00:00
|
|
|
|
|
|
|
data = {
|
2020-10-24 14:04:33 +00:00
|
|
|
'channels': channel_states,
|
|
|
|
'ui_page': "status",
|
|
|
|
"ui_title": "Status"
|
2020-10-23 21:58:53 +00:00
|
|
|
}
|
2020-10-24 02:13:02 +00:00
|
|
|
return render_template('status.html', data=data)
|
2020-10-23 20:10:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/player/<int:channel>/play")
|
|
|
|
def play(channel):
|
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
channel_to_q[channel].put("PLAY")
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
return ui_status()
|
2020-10-23 20:10:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/player/<int:channel>/pause")
|
|
|
|
def pause(channel):
|
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
channel_to_q[channel].put("PAUSE")
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
return ui_status()
|
2020-10-23 20:10:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/player/<int:channel>/unpause")
|
|
|
|
def unPause(channel):
|
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
channel_to_q[channel].put("UNPAUSE")
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
return ui_status()
|
2020-10-23 20:10:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/player/<int:channel>/stop")
|
|
|
|
def stop(channel):
|
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
channel_to_q[channel].put("STOP")
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
return ui_status()
|
2020-10-23 20:10:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/player/<int:channel>/seek/<int:pos>")
|
|
|
|
def seek(channel, pos):
|
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
channel_to_q[channel].put("SEEK:" + str(pos))
|
|
|
|
|
|
|
|
return ui_status()
|
2020-10-23 20:10:32 +00:00
|
|
|
|
|
|
|
|
2020-10-23 21:58:53 +00:00
|
|
|
@app.route("/player/<int:channel>/output/<name>")
|
|
|
|
def output(channel, name):
|
2020-10-24 14:04:33 +00:00
|
|
|
channel_to_q[channel].put("OUTPUT:" + name)
|
|
|
|
channel_to_q[channel].put("LOAD:test"+str(channel)+".mp3")
|
|
|
|
return ui_status()
|
2020-10-23 21:58:53 +00:00
|
|
|
|
2020-10-23 20:10:32 +00:00
|
|
|
|
|
|
|
@app.route("/player/<int:channel>/details")
|
|
|
|
def details(channel):
|
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
channel_to_q[channel].put("DETAILS")
|
|
|
|
while True:
|
|
|
|
response = channel_from_q[channel].get()
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
if response and response.startswith("RESP:DETAILS"):
|
|
|
|
return json.loads(response.strip("RESP:DETAILS:"))
|
2020-10-23 20:10:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/player/all/stop")
|
|
|
|
def all_stop():
|
2020-10-24 14:04:33 +00:00
|
|
|
for channel in channel_to_q:
|
|
|
|
channel.put("STOP")
|
|
|
|
ui_status()
|
2020-10-24 02:13:02 +00:00
|
|
|
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2020-10-24 02:13:02 +00:00
|
|
|
@app.route('/static/<path:path>')
|
|
|
|
def send_static(path):
|
|
|
|
return send_from_directory('ui-static', path)
|
2020-10-23 21:58:53 +00:00
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
|
2020-10-24 20:31:52 +00:00
|
|
|
def startServer():
|
2020-10-24 14:04:33 +00:00
|
|
|
for channel in range(3):
|
|
|
|
channel_to_q.append(multiprocessing.Queue())
|
|
|
|
channel_from_q.append(multiprocessing.Queue())
|
|
|
|
# channel_to_q[-1].put_nowait("LOAD:test"+str(channel)+".mp3")
|
|
|
|
channel_p.append(
|
|
|
|
multiprocessing.Process(
|
|
|
|
target=player.Player,
|
2020-10-24 20:31:52 +00:00
|
|
|
args=(channel, channel_to_q[-1], channel_from_q[-1]),
|
|
|
|
daemon=True
|
2020-10-24 14:04:33 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
channel_p[channel].start()
|
|
|
|
|
|
|
|
# Don't use reloader, it causes Nested Processes!
|
|
|
|
app.run(host='0.0.0.0', port=5000, debug=True, use_reloader=False)
|
2020-10-24 20:31:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
def stopServer():
|
|
|
|
print("Stopping server.py")
|
|
|
|
for q in channel_to_q:
|
|
|
|
q.put("QUIT")
|
|
|
|
for player in channel_p:
|
|
|
|
player.join()
|
2020-10-26 21:25:02 +00:00
|
|
|
global app
|
2020-10-24 20:31:52 +00:00
|
|
|
app = None
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
print("BAPSicle is a service. Please run it like one.")
|