Add very crude output device switching

This commit is contained in:
Matthew Stratford 2020-10-23 22:58:53 +01:00
parent 40f39bf28e
commit 171e29b016
4 changed files with 85 additions and 25 deletions

View file

@ -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]))

View file

@ -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/<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()
@app.route("/player/<int:channel>/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())

View file

@ -1,3 +1,4 @@
pygame
pygame==2.0.0.dev20
flask
mutagen
mutagen
sounddevice

View file

@ -6,10 +6,16 @@
<body>
{% if data %}
<code>
{% for player in data %}
{{player}} <a href="/player/{{player['channel']}}/play">Play</a> <a href="/player/{{player.channel}}/stop">Stop</a></a><br>
{% for player in data.channels %}
{{player}} <a href="/player/{{player['channel']}}/play">Play</a> <a href="/player/{{player.channel}}/stop">Stop</a><br>
{% endfor %}
</code>
<br>
<code>
{% for output in data.outputs %}
<a href="/player/0/output/{{output.name}}">Set Channel 0</a> <a href="/player/1/output/{{output.name}}">Set Channel 1</a> <a href="/player/2/output/{{output.name}}">Set Channel 2</a> - {{output.name}}<br>
{% endfor %}
</code>
{% endif %}