Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make "Experimental" PulseAudio Backend A Viable Option #104

Merged

Conversation

Morxemplum
Copy link
Contributor

I noticed that there was an existing implementation of an "experimental" PulseAudio backend. But in every build, this is not used as USE_SDL3_AUDIO is on by default. Well, when trying to build this project for NixOS, the SDL3_AUDIO backend does not work. You will not get an audio output for the reason:

sdl3audio SDL_OpenAudioDeviceStream failed, audio will be unavailable: Audio subsystem is not initialized

NixOS currently does not have an SDL3 package in nixpkgs, and considering that I ditched SDL3 graphics in favor of GLFW, I figured that I should try throwing out SDL3_AUDIO as well.

This pull request actually fixes an issue that is currently present in the PulseAudio backend, where you get crackling audio as a result of many audio underruns. I noticed that the SDL3 backend has a slight difference that wasn't present in the PulseAudio backend. In the method that takes a ShortArray, I noticed that the length is doubled because there's 16 bits in a short as opposed to 8 in a byte. This was not present in the PulseAudio backend. So by simply mirroring this change, that fixed the crackling audio.

void AudioDevice::write2(std::shared_ptr<FakeJni::JShortArray> data, FakeJni::JInt length) {
SDL_PutAudioStreamData(s, data->getArray(), length * 2);

In addition, I made a bit of refactoring to the CMakeLists. Mostly just to take in account of the USE_SDL3_AUDIO option, and use the PulseAudio backend whenever it is off. When you turn off that option, and use GLFW as the window system, you can have a set up that does not use SDL at all.

This pull request does not enable the PulseAudio backend by default. I'm sure that there is a lot more testing that needs to be done before this could be considered. However, I'm sure this can be considered an alternative option, should things with SDL not work out.

Copy link
Member

@ChristopherHX ChristopherHX left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

making pulseaudio required is the merge blocker for me.

CMakeLists.txt Outdated Show resolved Hide resolved
@@ -52,7 +52,7 @@ void AudioDevice::write(std::shared_ptr<FakeJni::JByteArray> data, FakeJni::JInt

void AudioDevice::write2(std::shared_ptr<FakeJni::JShortArray> data, FakeJni::JInt length) {
int error = 0;
if(s && pa_simple_write(s, data->getArray(), length, &error)) {
if(s && pa_simple_write(s, data->getArray(), length * 2, &error)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, you might noticed this is no longer used legacy code as of release 1.0.0.

@@ -22,12 +22,18 @@ else()
endif()

if(TARGET SDL3::SDL3)
target_sources(mcpelauncher-client PRIVATE src/jni/sdl3audio.cpp src/jni/sdl3audio.h)
message(VERBOSE "USING SDL3")
target_link_libraries(mcpelauncher-client SDL3::SDL3)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This link lib is only for audio, e.g. sdl3_audio has it's own SDL_init call for the audio subsystem and is independent from game window.

So it doesn't make sense for me to be enabled if USE_SDL3_AUDIO is off, please join USE_SDL3_AUDIO and TARGET SDL3::SDL3 via and, make if (NOT USE_SDL3_AUDIO) an else() again. Repeating conditions seems unnecessary to me

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. I see, I thought that TARGET SDL3::SDL3 entailed the whole SDL3 system, not just audio. My mistake. Will improve this!

@ChristopherHX
Copy link
Member

Are you saying -DUSE_SDL3_AUDIO=OFF is not working right now?

E.g. you make it possible to use sdl3 with pulseaudio audio.
eglut/glfw should work right now without sdl3 audio.

@Morxemplum
Copy link
Contributor Author

On NixOS, you can't even use SDL3 for the game window, as it will fail to find the video device (See crash message here). This similar reasoning is why a previous attempt of getting this to work on NixOS had no audio output. I've tried to figure out what might be causing this issue, but I ran into a dead end. It seems like NixOS does not have very good SDL3 support yet, as there is no nixpkg available.

As for your example, if I were to follow your comment and merge SDL3::SDL3 and USE_SDL3_AUDIO into the same if statement, wouldn't marking SDL3 as the game window override USE_SDL3_AUDIO and use SDL3_AUDIO regardless? I'm guessing that you're trying to narrow down edge cases, and if you want me to, I'll use my Arch Linux distrobox to try out SDL3 with the PulseAudio backend.

@ChristopherHX
Copy link
Member

can be improved in a follow up pr

@ChristopherHX ChristopherHX merged commit db9c31e into minecraft-linux:master Dec 6, 2024
22 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants