<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Pedro Augusto Fabri</title>
    <description>The latest articles on DEV Community by Pedro Augusto Fabri (@pedroafabri).</description>
    <link>https://dev.to/pedroafabri</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F893983%2Fa4bd2171-be9b-43ad-b56f-014190efaa66.png</url>
      <title>DEV Community: Pedro Augusto Fabri</title>
      <link>https://dev.to/pedroafabri</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pedroafabri"/>
    <language>en</language>
    <item>
      <title>Sound in WSL2 and Windows 11: PulseAudio Configuration Made Easy</title>
      <dc:creator>Pedro Augusto Fabri</dc:creator>
      <pubDate>Mon, 20 Jan 2025 15:17:23 +0000</pubDate>
      <link>https://dev.to/pedroafabri/sound-in-wsl2-and-windows-11-pulseaudio-configuration-made-easy-4182</link>
      <guid>https://dev.to/pedroafabri/sound-in-wsl2-and-windows-11-pulseaudio-configuration-made-easy-4182</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;While working on a feature for the &lt;a href="https://github.com/FyroxEngine/Fyrox" rel="noopener noreferrer"&gt;Fyrox&lt;/a&gt; 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.&lt;/p&gt;

&lt;p&gt;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!&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 1 - Download PulseAudio for Windows
&lt;/h2&gt;

&lt;p&gt;Download the zip file from the &lt;a href="https://www.freedesktop.org/wiki/Software/PulseAudio/Ports/Windows/Support/" rel="noopener noreferrer"&gt;Official Website&lt;/a&gt; and extract it somewhere. I like to keep it on &lt;code&gt;C:\pulseaudio&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 2 - Configure PulseAudio
&lt;/h2&gt;

&lt;h3&gt;
  
  
  2.1 Configure server IP
&lt;/h3&gt;

&lt;p&gt;On the extracted folder, open the &lt;code&gt;etc/pulse/default.pa&lt;/code&gt; and change &lt;code&gt;line 61&lt;/code&gt; from this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#load-module module-native-protocol-tcp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1 auth-anonymous=1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  2.2 Prevent server from exiting
&lt;/h3&gt;

&lt;p&gt;Open the &lt;code&gt;etc/pulse/daemon.conf&lt;/code&gt; and change &lt;code&gt;line 39&lt;/code&gt; from this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;; exit-idle-time = 20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;exit-idle-time = -1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Step 3 - Configure PulseAudio on WSL2
&lt;/h2&gt;

&lt;h3&gt;
  
  
  3.1 Installing PulseAudio
&lt;/h3&gt;

&lt;p&gt;Install the PulseAudio on your WSL2 instance with the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt update
sudo apt install pulseaudio pulseaudio-utils
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3.2 Getting the host IP Address
&lt;/h3&gt;

&lt;p&gt;For the next step we'll need the host's IP Address, so open a new PowerShell/CMD terminal (Windows terminal, &lt;strong&gt;NOT WSL!!&lt;/strong&gt;) and write this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ipconfig
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Find your IP4 address and save that for the next step.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmxcpd9l5rrnzyysfj9lx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmxcpd9l5rrnzyysfj9lx.png" alt="ipconfig" width="714" height="821"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  3.3 Configuring PULSE_SERVER environment variable
&lt;/h3&gt;

&lt;p&gt;PulseAudio uses the &lt;code&gt;PULSE_SERVER&lt;/code&gt; environment variable. It must point to the server to connect, in this case, your host IP Address.&lt;/p&gt;

&lt;p&gt;Execute the following command, changing the "HOST_IP" to the address that you got on step 3.2:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export PULSE_SERVER=tcp:HOST_IP
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Step 4 - Test
&lt;/h2&gt;

&lt;h3&gt;
  
  
  4.1 Download test file
&lt;/h3&gt;

&lt;p&gt;You can skip this step if you already have a audio file to test with.&lt;/p&gt;

&lt;p&gt;Use the below command to download a simple audio file to your WSL instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl -LJO https://github.com/pedroafabri/Fyrox/raw/master/fyrox-sound/examples/data/helicopter.wav
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4.2 Open PulseAudio on windows
&lt;/h3&gt;

&lt;p&gt;In the folder downloaded at Step 1, execute &lt;code&gt;bin/pulseaudio.exe&lt;/code&gt;. You should see something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8a9cnn1oi2tcpbytnuxk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8a9cnn1oi2tcpbytnuxk.png" alt="PulseAudio Windows" width="800" height="199"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;NOTE: Those messages does not interfere with the goal of this article.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  4.3 Execute the audio
&lt;/h3&gt;

&lt;p&gt;In your WSL terminal, execute the &lt;code&gt;paplay&lt;/code&gt; command as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;paplay helicopter.wav
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should now hear an helicopter coming from your speakers! :D&lt;/p&gt;




&lt;h2&gt;
  
  
  Bonus Step - Automating
&lt;/h2&gt;

&lt;p&gt;Finding your IP and reconfiguring pulseaudio everytime can be tedious and error-prone, so let's automate that.&lt;/p&gt;

&lt;p&gt;In your WSL terminal, open your &lt;code&gt;.bashrc&lt;/code&gt; file (&lt;code&gt;.zshrc&lt;/code&gt; if you use zsh) and add the following lines at the end of the file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Get current host IP&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;HOST_IP&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;ipconfig.exe | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="s2"&gt;"IPv4 Address"&lt;/span&gt; | &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'{print $NF}'&lt;/span&gt; | &lt;span class="nb"&gt;head&lt;/span&gt; &lt;span class="nt"&gt;-n&lt;/span&gt; 1&lt;span class="si"&gt;)&lt;/span&gt;

&lt;span class="c"&gt;# Configure PulseAudio Server&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;PULSE_SERVER&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;tcp:&lt;span class="nv"&gt;$HOST_IP&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now everytime you open a new WSL terminal the &lt;code&gt;HOST_IP&lt;/code&gt; variable will store the host's IP Address and &lt;code&gt;PULSE_SERVER&lt;/code&gt; will be configured correctly.&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;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!&lt;/p&gt;

&lt;p&gt;Thanks for my dear friend &lt;strong&gt;Fernando Barbosa&lt;/strong&gt; for following these steps to validate if it works.&lt;/p&gt;

</description>
      <category>pulseaudio</category>
      <category>linux</category>
    </item>
  </channel>
</rss>
