Switch to SDL outputs with pulseaudio for linux

This commit is contained in:
Matthew Stratford 2021-11-02 22:48:52 +00:00
parent 592cf11a79
commit c475dbb5d5
4 changed files with 44 additions and 2 deletions

View file

@ -5,7 +5,7 @@ os.putenv('SDL_AUDIODRIVER', 'pulseaudio')
import pygame._sdl2 as sdl2 import pygame._sdl2 as sdl2
import pygame import pygame
from pygame import mixer from pygame import mixer
#pygame.init() pygame.init()
import time import time
mixer.init(44100, -16, 2, 1024) mixer.init(44100, -16, 2, 1024)
is_capture = 0 # zero to request playback devices, non-zero to request recording devices is_capture = 0 # zero to request playback devices, non-zero to request recording devices

View file

@ -1,6 +1,12 @@
from typing import Any, Dict, List, Optional, Tuple from typing import Any, Dict, List, Optional, Tuple
import sounddevice as sd import sounddevice as sd
from helpers.os_environment import isLinux, isMacOS, isWindows 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 import glob
if isWindows(): if isWindows():
@ -53,6 +59,15 @@ class DeviceManager:
return host_apis 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 @classmethod
def getSerialPorts(cls) -> List[Optional[str]]: def getSerialPorts(cls) -> List[Optional[str]]:
"""Lists serial port names """Lists serial port names

View file

@ -33,6 +33,26 @@ Set for:
Default Audio Output Default Audio Output
</code> </code>
</p> </p>
{% if data.sdl_direct %}
Linux (Pulse Audio)
<br>
<code>
{% for output in data.outputs %}
Set for:
{% for channel in data.channels %}
{% if not channel %}
Player {{loop.index0}}
{% elif channel.output == output %}
<strong>Player {{channel.channel}}</strong>
{% else %}
<a href="/player/{{channel.channel}}/output/{{output}}">Player {{channel.channel}}</a>
{% endif %}
/
{% endfor %}
{% if output %}{{output}}{% else %}System Default Output{% endif %}<br>
{% endfor %}
</code>
{% else %}
{% for host_api in data.outputs %} {% for host_api in data.outputs %}
{{host_api.name}} {{host_api.name}}
<br> <br>
@ -54,4 +74,5 @@ Default Audio Output
{% endfor %} {% endfor %}
</code> </code>
{% endfor %} {% endfor %}
{% endif %}
{% endblock %} {% endblock %}

View file

@ -17,6 +17,7 @@ import json
import os import os
from helpers.os_environment import ( from helpers.os_environment import (
isLinux,
resolve_external_file_path, resolve_external_file_path,
resolve_local_file_path, resolve_local_file_path,
) )
@ -171,11 +172,16 @@ def ui_config_player(request):
for i in range(server_state.get()["num_channels"]): for i in range(server_state.get()["num_channels"]):
channel_states.append(status(i)) channel_states.append(status(i))
outputs = None
if isLinux():
outputs = DeviceManager.getAudioDevices()
else:
outputs = DeviceManager.getAudioOutputs() outputs = DeviceManager.getAudioOutputs()
data = { data = {
"channels": channel_states, "channels": channel_states,
"outputs": outputs, "outputs": outputs,
"sdl_direct": isLinux(),
"ui_page": "config", "ui_page": "config",
"ui_title": "Player Config", "ui_title": "Player Config",
} }