Introduction
While working on a feature for the Fyrox open-source engine, I ran into quite a bit of trouble configuring audio on WSL2 and Windows 11, but after some trial and error, I finally found a straightforward solution.
In this article, I’ll guide you through the steps I took to make it work seamlessly, so you can avoid the same headaches I went through!
Step 1 - Download PulseAudio for Windows
Download the zip file from the Official Website and extract it somewhere. I like to keep it on C:\pulseaudio
.
Step 2 - Configure PulseAudio
2.1 Configure server IP
On the extracted folder, open the etc/pulse/default.pa
and change line 61
from this:
#load-module module-native-protocol-tcp
to this:
load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1 auth-anonymous=1
Note: You MUST remove the trailing # to uncomment this line. Also, if that command is not on line 61, use the search function of your editor to find the original configuration.
2.2 Prevent server from exiting
Open the etc/pulse/daemon.conf
and change line 39
from this:
; exit-idle-time = 20
to this:
exit-idle-time = -1
Note: You MUST remove the trailing ; to uncomment this line. Also, if that command is not on line 39, use the search function of your editor to find the original configuration.
Step 3 - Configure PulseAudio on WSL2
3.1 Installing PulseAudio
Install the PulseAudio on your WSL2 instance with the following command:
sudo apt update
sudo apt install pulseaudio pulseaudio-utils
3.2 Getting the host IP Address
For the next step we'll need the host's IP Address, so open a new PowerShell/CMD terminal (Windows terminal, NOT WSL!!) and write this command:
ipconfig
Find your IP4 address and save that for the next step.
3.3 Configuring PULSE_SERVER environment variable
PulseAudio uses the PULSE_SERVER
environment variable. It must point to the server to connect, in this case, your host IP Address.
Execute the following command, changing the "HOST_IP" to the address that you got on step 3.2:
export PULSE_SERVER=tcp:HOST_IP
NOTE: This command will create the environment variable only for the current terminal session. In the Bonus Step of this article I'll explain how to keep the value through sessions.
Step 4 - Test
4.1 Download test file
You can skip this step if you already have a audio file to test with.
Use the below command to download a simple audio file to your WSL instance:
curl -LJO https://github.com/pedroafabri/Fyrox/raw/master/fyrox-sound/examples/data/helicopter.wav
4.2 Open PulseAudio on windows
In the folder downloaded at Step 1, execute bin/pulseaudio.exe
. You should see something like this:
NOTE: Those messages does not interfere with the goal of this article.
4.3 Execute the audio
In your WSL terminal, execute the paplay
command as below:
paplay helicopter.wav
You should now hear an helicopter coming from your speakers! :D
Bonus Step - Automating
Finding your IP and reconfiguring pulseaudio everytime can be tedious and error-prone, so let's automate that.
In your WSL terminal, open your .bashrc
file (.zshrc
if you use zsh) and add the following lines at the end of the file:
# Get current host IP
export HOST_IP=$(ipconfig.exe | grep "IPv4 Address" | awk '{print $NF}' | head -n 1)
# Configure PulseAudio Server
export PULSE_SERVER=tcp:$HOST_IP
Now everytime you open a new WSL terminal the HOST_IP
variable will store the host's IP Address and PULSE_SERVER
will be configured correctly.
Conclusion
This was a quick-written article just to document how I managed to make this work. Please, feel free to comment if you find any issue!
Thanks for my dear friend Fernando Barbosa for following these steps to validate if it works.
Top comments (0)