Skip to content

Instantly share code, notes, and snippets.

@Ozymandias42
Last active May 14, 2021 20:22
Show Gist options
  • Save Ozymandias42/0d3799c3ec6b9994cad4b94386a2107c to your computer and use it in GitHub Desktop.
Save Ozymandias42/0d3799c3ec6b9994cad4b94386a2107c to your computer and use it in GitHub Desktop.
Short bash script to setup a streaming setup with pulseaudio.
#!/usr/bin/env bash
#Short script that creates a streaming setup in pulseaudio. It does the following:
#1. Create a null-sink as ApplicationSink to separate a single applications audio from the rest
#3. Create another null-sink as Input-Combinator; CombineSink
#4. Create remap-source as Virtual Microphone; Vsource
#5. Loopback the the ApplicationSink's audio back to the systems default audio output
#6. Loopback ApplicationSink audio again, this time into the CombineSink
#7. Loopback actual microphone audio into the CombineSink as well
#8. Loopback combined application output and real-mic input from CombineSink into virtual microphone Vsource
# ┌────────────┐
# │ VSource │
# └─────▲──────┘
# │
#┌────────────┐ ┌─────┴──────┐
#│SystemMic ├────►│ CombineSink│
#└────────────┘ └─────▲──────┘
# │
#┌────────────┐ ┌─────┴──────┐
#│ App2Stream ├────►│ AppSink │
#└────────────┘ └─────┬──────┘
# │
#┌────────────┐ ┌─────▼──────┐
#│OtherApps ├────►│ SystemOut │
#└────────────┘ └────────────┘
#Function to choose a sink or source
function selectSinkOrSource(){
local returnVal
select i in $(pactl list $1 short | cut -f2); do returnVal=$i ; break ; done
echo $returnVal
}
#Determine Sink to Redirect to Virtual Microphone
echo "Select output to redirect to virtual mic"
local selectedSink=$(selectSinkOrSource "sinks")
echo "Select real input to combine with virtual mic"
local selectedSource=$(selectSinkOrSource "sources")
pactl load-module module-null-sink sink_name=CombineSink sink_properties=device.description=CombineSink
pactl load-module module-null-sink sink_name=ApplicationSink sink_properties=device.description=ApplicationSink
pactl load-module module-remap-source master=CombineSink.monitor source_name=Vsource
#Loopback Output of ApplicationSink.monitor to System-Default-Out
pactl load-module module-loopback source=ApplicationSink.monitor sink="$selectedSink"
#Loopback Output of ApplicationSink.monitor to CombineSink
pactl load-module module-loopback source=ApplicationSink.monitor sink=CombineSink
#Loopback Output of Real-Microphone to CombineSink
pactl load-module module-loopback source="$selectedSource" sink=CombineSink
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment