From 94e01aa1440ecb1064bf6c3a60e968d7a3278787 Mon Sep 17 00:00:00 2001 From: Matthew Stratford Date: Tue, 3 Nov 2020 21:24:45 +0000 Subject: [PATCH] Move audio device to DeviceManager --- helpers/device_manager.py | 27 +++++++++++++++++++++++++++ server.py | 9 ++------- 2 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 helpers/device_manager.py diff --git a/helpers/device_manager.py b/helpers/device_manager.py new file mode 100644 index 0000000..266b823 --- /dev/null +++ b/helpers/device_manager.py @@ -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? diff --git a/server.py b/server.py index c44412b..afe60f1 100644 --- a/server.py +++ b/server.py @@ -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,