Move audio device to DeviceManager

This commit is contained in:
Matthew Stratford 2020-11-03 21:24:45 +00:00
parent c1e88fcf71
commit 94e01aa144
No known key found for this signature in database
GPG key ID: 9E53C8B3F0B57395
2 changed files with 29 additions and 7 deletions

27
helpers/device_manager.py Normal file
View file

@ -0,0 +1,27 @@
import sounddevice as sd
import importlib
from os_environment import isMacOS
class DeviceManager():
@classmethod
def _isOutput(self, device):
return device["max_output_channels"] > 0
@classmethod
def _getDevices(self):
# To update the list of devices
# Sadly this doesn't work on MacOS.
if not isMacOS():
sd._terminate()
sd._initialize()
devices = sd.query_devices()
return devices
@classmethod
def getOutputs(self):
outputs = filter(self._isOutput, self._getDevices())
return outputs
# TODO: Maybe some hotplug event triggers support for the players?

View file

@ -2,10 +2,10 @@ import multiprocessing
import player
from flask import Flask, render_template, send_from_directory, request
import json
import sounddevice as sd
import setproctitle
import logging
from helpers.os_environment import isMacOS
from helpers.device_manager import DeviceManager
setproctitle.setproctitle("BAPSicle - Server")
@ -55,12 +55,7 @@ def ui_config():
for i in range(3):
channel_states.append(status(i))
devices = sd.query_devices()
outputs = []
for device in devices:
if device["max_output_channels"] > 0:
outputs.append(device)
outputs = DeviceManager.getOutputs()
data = {
'channels': channel_states,