BAPSicle/multiplay-flask.py

123 lines
2.3 KiB
Python
Raw Normal View History

2020-10-23 20:10:32 +00:00
import multiprocessing
import bapsicle_standalone
2020-10-23 20:10:32 +00:00
from flask import Flask, render_template
import json
2020-10-23 21:58:53 +00:00
import sounddevice as sd
2020-10-23 20:10:32 +00:00
app = Flask(__name__)
channel_to_q = []
channel_from_q = []
channel_p = []
@app.route("/")
def status():
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-23 21:58:53 +00:00
channel_states.append(details(i))
devices = sd.query_devices()
outputs = []
for device in devices:
if device["max_output_channels"] > 0:
outputs.append(device)
data = {
'channels': channel_states,
'outputs': outputs,
}
2020-10-23 20:10:32 +00:00
return render_template('index.html', data=data)
@app.route("/player/<int:channel>/play")
def play(channel):
channel_to_q[channel].put("PLAY")
return status()
@app.route("/player/<int:channel>/pause")
def pause(channel):
channel_to_q[channel].put("PAUSE")
return status()
@app.route("/player/<int:channel>/unpause")
def unPause(channel):
channel_to_q[channel].put("UNPAUSE")
return status()
@app.route("/player/<int:channel>/stop")
def stop(channel):
channel_to_q[channel].put("STOP")
return status()
@app.route("/player/<int:channel>/seek/<int:pos>")
def seek(channel, pos):
channel_to_q[channel].put("SEEK:" + str(pos))
return status()
2020-10-23 21:58:53 +00:00
@app.route("/player/<int:channel>/output/<name>")
def output(channel, name):
channel_to_q[channel].put("OUTPUT:" + name)
channel_to_q[channel].put("LOAD:test"+str(channel)+".mp3")
return status()
2020-10-23 20:10:32 +00:00
@app.route("/player/<int:channel>/details")
def details(channel):
channel_to_q[channel].put("DETAILS")
while True:
response = channel_from_q[channel].get()
if response and response.startswith("RESP:DETAILS"):
return json.loads(response.strip("RESP:DETAILS:"))
@app.route("/player/all/stop")
def all_stop():
for channel in channel_to_q:
channel.put("STOP")
status()
2020-10-23 21:58:53 +00:00
if __name__ == "__main__":
2020-10-23 21:58:53 +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=bapsicle_standalone.bapsicle,
args=(channel, channel_to_q[-1], channel_from_q[-1])
)
)
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)