From c475dbb5d5d72e87db75885a76aea4ff8e24de65 Mon Sep 17 00:00:00 2001 From: Matthew Stratford Date: Tue, 2 Nov 2021 22:48:52 +0000 Subject: [PATCH] Switch to SDL outputs with pulseaudio for linux --- dev/scripts/get_linux_outputs.py | 2 +- helpers/device_manager.py | 15 +++++++++++++++ ui-templates/config_player.html | 21 +++++++++++++++++++++ web_server.py | 8 +++++++- 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/dev/scripts/get_linux_outputs.py b/dev/scripts/get_linux_outputs.py index 323b6f2..6449302 100644 --- a/dev/scripts/get_linux_outputs.py +++ b/dev/scripts/get_linux_outputs.py @@ -5,7 +5,7 @@ os.putenv('SDL_AUDIODRIVER', 'pulseaudio') import pygame._sdl2 as sdl2 import pygame from pygame import mixer -#pygame.init() +pygame.init() import time mixer.init(44100, -16, 2, 1024) is_capture = 0 # zero to request playback devices, non-zero to request recording devices diff --git a/helpers/device_manager.py b/helpers/device_manager.py index a7871f9..aa5a7b0 100644 --- a/helpers/device_manager.py +++ b/helpers/device_manager.py @@ -1,6 +1,12 @@ from typing import Any, Dict, List, Optional, Tuple import sounddevice as sd from helpers.os_environment import isLinux, isMacOS, isWindows +import os + +os.environ["PYGAME_HIDE_SUPPORT_PROMPT"] = "hide" +os.putenv('SDL_AUDIODRIVER', 'pulseaudio') +import pygame._sdl2 as sdl2 +from pygame import mixer import glob if isWindows(): @@ -53,6 +59,15 @@ class DeviceManager: return host_apis + @classmethod + def getAudioDevices(cls) -> List[str]: + mixer.init(44100, -16, 2, 1024) + is_capture = 0 # zero to request playback devices, non-zero to request recording devices + num = sdl2.get_num_audio_devices(is_capture) + names = [str(sdl2.get_audio_device_name(i, is_capture), encoding="utf-8") for i in range(num)] + mixer.quit() + return names + @classmethod def getSerialPorts(cls) -> List[Optional[str]]: """Lists serial port names diff --git a/ui-templates/config_player.html b/ui-templates/config_player.html index 78d2c79..6ddbfd3 100644 --- a/ui-templates/config_player.html +++ b/ui-templates/config_player.html @@ -33,6 +33,26 @@ Set for: Default Audio Output

+{% if data.sdl_direct %} +Linux (Pulse Audio) +
+ +{% for output in data.outputs %} +Set for: + {% for channel in data.channels %} + {% if not channel %} + Player {{loop.index0}} + {% elif channel.output == output %} + Player {{channel.channel}} + {% else %} + Player {{channel.channel}} + {% endif %} + / + {% endfor %} +{% if output %}{{output}}{% else %}System Default Output{% endif %}
+{% endfor %} +
+{% else %} {% for host_api in data.outputs %} {{host_api.name}}
@@ -54,4 +74,5 @@ Default Audio Output {% endfor %} {% endfor %} +{% endif %} {% endblock %} diff --git a/web_server.py b/web_server.py index 1ae5068..5b809ef 100644 --- a/web_server.py +++ b/web_server.py @@ -17,6 +17,7 @@ import json import os from helpers.os_environment import ( + isLinux, resolve_external_file_path, resolve_local_file_path, ) @@ -171,11 +172,16 @@ def ui_config_player(request): for i in range(server_state.get()["num_channels"]): channel_states.append(status(i)) - outputs = DeviceManager.getAudioOutputs() + outputs = None + if isLinux(): + outputs = DeviceManager.getAudioDevices() + else: + outputs = DeviceManager.getAudioOutputs() data = { "channels": channel_states, "outputs": outputs, + "sdl_direct": isLinux(), "ui_page": "config", "ui_title": "Player Config", }