Live Audio over SSH
Preface
Rather than having a second monitor connected to my laptop, I use 2 laptops simultaneously. The problem with this is if there is audio playing from both computers, it must be "down mixed" to play on, for instance, 1 pair of Bluetooth headphones.
This is a little driver I wrote as a Bash script to do just that. Of course, both computers are connected to the same LAN so initially it simply piped unencrypted audio using BSD's netcat tool, however later I made and tested a proof of concept that uses the SSH protocol encrypted over the internet to send live audio from one computer to another.
The result is I can now work on video editing on the main laptop while watching YouTube on the second laptop, and hear both at the same time in headphones connected to only the main laptop. In reality, there are countless uses for mixing down audio from two devices simultaneously.
Technologies
PipeWire: a modern userland (ring 2) Linux audio driver replacing PulseAudio in most modern Linux distributions (however any other audio driver can be used in its place)
netcat: an ancient but still useful simple tool for transferring text over TCP/UDP (used for local low-latency unencrypted transmission over LAN)
SSH: an OpenBSD protocol primarily for secure remote management (personally my favorite protocol to transfer any kind of data/files/audio/commands/internet proxy/VPN)
Source Code
The source code is dependent on the platform and audio hardware used, but can be easily modified to fit most configurations
Additional PipeWire arguments for latency, audio fidelity, device selection can be used to tweak the script
A virtual audio sink (output device) can be created on the second computer in order for each application to output the audio directly into this driver if all other audio output devices are turned off or don't exist
Efficiency: While running, the LAN version of this driver uses an incredible 0.2% of the CPU (or just 1% of a single core on this netbook) and around 1.7 MB of RAM
(The 2 pw-cat below are just diagnostic processes used to visually verify that audio is being sent, and do not have to run when using the driver normally)
Let's see it in action
In this video
Visual representation of the audio being sent and received on both computers via characters in a terminal
The volume slowly lowered to 0 to show the decrease in the rate of characters being printed in the terminal
htop (system Table of Processes) showing the efficiency of the netcat process is
pw-top (PipeWire Table of Processes) showing the audio transfer rate