From 171e29b016a761f5184ba28ffbc4e380009e48f8 Mon Sep 17 00:00:00 2001 From: Matthew Stratford Date: Fri, 23 Oct 2020 22:58:53 +0100 Subject: [PATCH] Add very crude output device switching --- bapsicle_standalone.py | 67 ++++++++++++++++++++++++++++++------------ multiplay-flask.py | 28 ++++++++++++++++-- requirements.txt | 5 ++-- templates/index.html | 10 +++++-- 4 files changed, 85 insertions(+), 25 deletions(-) diff --git a/bapsicle_standalone.py b/bapsicle_standalone.py index f49c0a0..ab23236 100644 --- a/bapsicle_standalone.py +++ b/bapsicle_standalone.py @@ -11,9 +11,18 @@ class bapsicle(): "pos": 0, "remaining": 0, "length": 0, - "loop": False + "loop": False, + "output": None } + def isInit(self): + try: + pygame.mixer.music.get_busy() + except: + return False + else: + return True + def isPlaying(self): return bool(pygame.mixer.music.get_busy()) @@ -47,6 +56,21 @@ class bapsicle(): else: self.state["length"] = pygame.mixer.Sound(filename).get_length()/1000 + def output(self, name = None): + pygame.mixer.quit() + try: + if name: + pygame.mixer.init(44100, -16, 1, 1024, devicename=name) + else: + pygame.mixer.init(44100, -16, 1, 1024) + except: + return "FAIL:Failed to init mixer, check sound devices." + else: + if name: + self.state["output"] = name + else: + self.state["output"] = "default" + return "OK" def updateState(self, pos = None): @@ -67,7 +91,7 @@ class bapsicle(): self.state["channel"] = channel - pygame.mixer.init(44100, -16, 1, 1024) + self.output() @@ -75,24 +99,29 @@ class bapsicle(): while True: time.sleep(0.01) incoming_msg = in_q.get() - self.updateState() if (not incoming_msg): continue - if (incoming_msg == 'PLAY'): - self.play() - if (incoming_msg == 'PAUSE'): - self.pause() - if (incoming_msg == 'UNPAUSE'): - self.unpause() - if (incoming_msg == 'STOP'): - self.stop() - if (incoming_msg.startswith("SEEK")): - split = incoming_msg.split(":") - self.seek(float(split[1])) - if (incoming_msg.startswith("LOAD")): - split = incoming_msg.split(":") - self.load(split[1]) + if self.isInit(): + self.updateState() + if (incoming_msg == 'PLAY'): + self.play() + if (incoming_msg == 'PAUSE'): + self.pause() + if (incoming_msg == 'UNPAUSE'): + self.unpause() + if (incoming_msg == 'STOP'): + self.stop() + if (incoming_msg.startswith("SEEK")): + split = incoming_msg.split(":") + self.seek(float(split[1])) + if (incoming_msg.startswith("LOAD")): + split = incoming_msg.split(":") + self.load(split[1]) + if (incoming_msg == 'DETAILS'): + out_q.put(self.getDetails()) - if (incoming_msg == 'DETAILS'): - out_q.put(self.getDetails()) + + if (incoming_msg.startswith("OUTPUT")): + split = incoming_msg.split(":") + out_q.put(self.output(split[1])) diff --git a/multiplay-flask.py b/multiplay-flask.py index 721b07e..dde3bc1 100644 --- a/multiplay-flask.py +++ b/multiplay-flask.py @@ -2,6 +2,7 @@ import multiprocessing from bapsicle_standalone import bapsicle from flask import Flask, render_template import json +import sounddevice as sd app = Flask(__name__) @@ -18,9 +19,23 @@ channel_p = [] @app.route("/") def status(): - data = [] + channel_states = [] for i in range(3): - data.append(details(i)) + 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, + } return render_template('index.html', data=data) @@ -63,6 +78,12 @@ def seek(channel, pos): return status() +@app.route("/player//output/") +def output(channel, name): + channel_to_q[channel].put("OUTPUT:" + name) + channel_to_q[channel].put("LOAD:test"+str(channel)+".mp3") + return status() + @app.route("/player//details") def details(channel): @@ -83,6 +104,9 @@ def all_stop(): if __name__ == "__main__": + + + for channel in range(3): channel_to_q.append(multiprocessing.Queue()) channel_from_q.append(multiprocessing.Queue()) diff --git a/requirements.txt b/requirements.txt index a4edd1b..5e0d18e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ -pygame +pygame==2.0.0.dev20 flask -mutagen \ No newline at end of file +mutagen +sounddevice \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index 510acb4..dbf430e 100644 --- a/templates/index.html +++ b/templates/index.html @@ -6,10 +6,16 @@ {% if data %} - {% for player in data %} - {{player}} Play Stop
+ {% for player in data.channels %} + {{player}} Play Stop
{% endfor %}
+
+ + {% for output in data.outputs %} + Set Channel 0 Set Channel 1 Set Channel 2 - {{output.name}}
+ {% endfor %} +
{% endif %}