DEV Community

Cover image for Sound in WSL2 and Windows 11: PulseAudio Configuration Made Easy
Pedro Augusto Fabri
Pedro Augusto Fabri

Posted on • Originally published at linkedin.com

Sound in WSL2 and Windows 11: PulseAudio Configuration Made Easy

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
Enter fullscreen mode Exit fullscreen mode

to this:

load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1 auth-anonymous=1
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

to this:

exit-idle-time = -1
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Find your IP4 address and save that for the next step.

ipconfig

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

4.2 Open PulseAudio on windows

In the folder downloaded at Step 1, execute bin/pulseaudio.exe. You should see something like this:

PulseAudio Windows

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay