RTcmix on Windows using WSL2

Windows 10 offers a capability that is useful for those of us employing RTcmix for our music and audio work. The Windows Subsystem for Linux feature runs a full Linux distribution within the context of Windows itself; no partitioning, rebooting or other 'traditional' Linux/Windows actions areare necessary. RTcmix standalone runs well in the WSL2 environment.

The setup for doing this is a little complicated, because three different setup operations have to take place:



Enable WSL2 and install a Linux distro

First of all, verify that you are running Windows 10, v 2004 (build 19041 or higher). To verify this, type "winver" into the start/search box and run the command. You can also run this command by typing the Windows logo key + R and typing "winver" into the "open" text box and hitting "OK". If you don't have this version of Windows installed, use Windows Update to get it.

Follow the directions in this page to install the WSL2 package:

	https://docs.microsoft.com/en-us/windows/wsl/install-win10
I installed Ubuntu 18.04 as my Linux distro. The rest of these instructions assume this Linux is being used. I imagine that things are similar for other distros.

[note: You will probably need to install the Linux kernel extension from the https://aka.ms/wsl2kernel page as discussed in the web page above]

[note: On my newer (May 2020, ASUS ROG Zephyrus M) Windows machine, the 'virtualization' machine feature (hypervisor, hyper-v, etc.) was installed and running, so I had no problem downloading and running Ubuntu, On my older (May 2017, 2017 ASUS ROG G752 laptop) machine I had to turn on this feature. The error I got that signalled this problen ocurred at the end of the Ubuntu install process: "a feature is missing...". See this page for how I managed to get it running]



Build and install RTcmix

The first step is to make sure the Ubuntu package system is updated. Start a Linux terminal window, and type:
	sudo apt update
[note: On my newer Windows machine, the 'malwarebytes' virus and firewall software presented no problems. The Synantec virus/firewall software on my older machine prevented the package manager from communicating with the Ubuntu servers. I had to disable the Symantec firewall and I used the Windows Defender firewall instead]

I like tcsh as my shell, but when I typed it in my Linux terminal I got:

	Command 'tcsh' not found, but can be installed with:
	sudo apt install tcsh
So I did that, and then I could say:
	chsh -s /usr/bin/tcsh brad
and my Linux terminals would run tcsh. But you can stick with bash (the default shell) if you want.

Next I had to install a set of packages that would allow me to build RTcmix on Ubuntu:

	sudo apt install build-essential
	sudo apt install flex
	sudo apt install bison
	sudo apt install pmidi
	sudo apt install alsa
	sudo apt install libasound2-dev
Download and unpack RTcmix from rtcmix.org ("RTcmix on the Command Line"). If you'd prefer, here is a tar archive of the RTcmix I used on my Windows/Ubuntu machines. It has the minor changes listed below incorporated already.

Once you have unpacked the RTcmix archive, go into the top-level RTcmix folder and type:

	./configure --with-alsa
At this point, if you aren't using my Windows RTcmix package (same link as above), you will need to make three minor code changes:
After making these changes, navigate back to the top-level RTcmix directory and type:
	make
	make install
The "CMIX" and "play/cmixplay" executable commands should now be located in the RTcmix/bin directory. Because we haven't set up real-time audio yet, "play/cmixplay" won't work, but the "CMIX" command will work if you turn off audio playback while processing. For example, running the following score:
	set_option("play = 0")

	rsetparams(44100, 2)
	load("WAVETABLE")

	rtoutput("outputsound.aif")

	WAVETABLE(0, 7, 20000, 8.00, 0.5)
should produce a soundfile ("outputsound.aif") that can be played -- a 7-second long middle "C" sine wave tone.

[note: you might want to set up the RTcmix/bin directory on your command path so you can access the RTcmix executable commands from any directory where you are working in a Linux terminal window]



Setting up real-time playback with alsa/pulseaudio

[note: these instructions are based on this web page about setting up the WSL environment]

1. Go to:
	https://www.freedesktop.org/wiki/Software/PulseAudio/Ports/Windows/Support/
and click on the link that says "zipfile containing.preview binaries". This will download the pulseaudio-1.1.zip archive. Unpack that archive and store the "pulseaudio-1.1" folder somewhere on your system.


2. Go into the "pulseaudio-1.1/etc/pulse" directory and edit the file "default.pa". Change the line (should be about line 60) that says:
	#load-module module-native-protocol-tcp
to
	load-module module-native-protocol-tcp auth-anonymous=1

Be sure to get rid of the "#" at the beginning of the line.


3. Also in that "pulseaudio-1.1/etc/pulse" directory, edit the file "daemon.conf" (it used to be called "daemon.pa"), and change the line (around line 39):

	; exit-idle-time = 20
to
	exit-idle-time = -1

Be sure to get rid of the ";" at the beginning of the line.


4. You can now start the Windows pulseaudio server. Go into the "pulseaudio-1.1/bin" directory and (from Windows) start up the "pulseaudio.exe" file. You should see a command-window start up (you may have to launch the "pulseaudio.exe" file several times). This window may say things like:

	W: [(null)] pulsecore/core-util.c: Secure directory creation not supported on Win32.
	W: [(null)] pulsecore/core-util.c: Secure directory creation not supported on Win32.
	W: [(null)] pulsecore/core-util.c: Secure directory creation not supported on Win32.
	W: [(null)] pulsecore/core.c: failed to allocate shared memory pool. Falling back to a normal memory pool.
	W: [(null)] pulsecore/core-util.c: Secure directory creation not supported on Win32.
	W: [(null)] pulsecore/core-util.c: Secure directory creation not supported on Win32.
	W: [(null)] pulsecore/core-util.c: Secure directory creation not supported on Win32.
	E: [(null)] daemon/main.c: Failed to load directory.
but that's ok. Do not close the window (although of course you can minimize it). That's the pulseaudio server.

[note: At some point you may need to change your firewall settings. I think they don't become an issue until you try to connect to this pulseaudio server from the Linux side; but you might want to put in an exception for "pulseaudio.exe" in your firewall rules now. Or turn off your anti-virus/firewall software temporarily and see what needs to be done to allow everything to connect and work]


5. In a Linux terminal, download the pulseaudio tools by saying:
	sudo apt install pulseaudio-utils

6. Next you need to set up the proper remote (Windows) server information. If you use bash as your shell, type:
	export PULSE_SERVER=tcp:$(grep nameserver /etc/resolv.conf | awk '{ print $2 }')
or type this:
	setenv PULSE_SERVER  `grep nameserver /etc/resolv.conf | awk '{ print "tcp:"$2 }'`
if you are using tcsh or other csh-derived shells.

[note: You may want to put those environment-setting lines in your .bashrc or .tcshrc configuration files so you don't have to enter them every time you start a Linux terminal]


6A. You can now check to see if your connection to the Windows by typing the following (bash) command:
	nc -vz $(grep nameserver /etc/resolv.conf | awk '{print $2}') 4713
or this (tcsh) command:
	nc -vz `grep nameserver /etc/resolv.conf | awk ' { print $2 } '`  4713
The nc command should immediately return "Connection to 4713 port [tcp/*] succeeded!". If it doesn't, it probably means you have a firewall issue.


7. To configure ALSA to work wth the pulseaudio server, create the file "/etc/asound.conf" and insert the following content:
	pcm.!default {
	   type pulse
	   # If defaults.namehint.showall is set to off in alsa.conf, then this is
	   # necessary to make this pcm show up in the list returned by
	   # snd_device_name_hint or aplay -L
	   hint.description "Default Audio Device"
	}
	ctl.!default {
	   type pulse
	}
Once you have done that, you should be able to use the pulseaudio server system. ALSA comes with a test soundfile player you can try from a Linux terminal window:
	aplay somesoundfile.wav
[note: "aplay" doesn't seem to work well with non-wav formats]

If that works, then you should be able to say (from the commends in the "RTcmix/bin" directory:
	play -D pulse somesoundfile.wav
	play -D pulse somesoundfile.aif
	CMIX -D pulse < somescorefile.sco
And that's it! Remember that you will have to restart the "pulseaudio.exe" server from Windows every time you logout or restart your machine.


Brad Garton
July, 2020