Skip to content

Instantly share code, notes, and snippets.

@MichaelNesterenko
Last active October 24, 2021 23:30
Show Gist options
  • Save MichaelNesterenko/c4928ac8436545a590fe633b68b0c8fc to your computer and use it in GitHub Desktop.
Save MichaelNesterenko/c4928ac8436545a590fe633b68b0c8fc to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
###
### Inspired by https://newbedev.com/redirecting-pulseaudio-sink-to-a-virtual-source
### with help from https://www.freedesktop.org/wiki/Software/PulseAudio/Documentation/User/Modules
###
MODULE_REGISTRY="modules.txt"
function load_module() {
echo "loading: $@"
pactl load-module "$@" >> $MODULE_REGISTRY
}
function unload_modules() {
for module_idx in $(tac $MODULE_REGISTRY); do
echo "unloading $module_idx"
pactl unload-module "$module_idx"
done
: > $MODULE_REGISTRY
}
function device_description() {
echo -n "device.description=$1"
}
###########################################
set -e
##########################################
mixer_name="$1" # some name which identifies input and output that mixer combines
original_output_name="$2" # pactl list sinks
original_input_name="$3" # pactl list sources
mixer_output_name="${mixer_name}-combiner"
mixer_input_name="${mixer_name}-virtual-mic"
mixer_silence_sink="${mixer_name}-silence"
unload_modules
load_module module-null-sink sink_name="$mixer_name" sink_properties=$(device_description "$mixer_name")
load_module module-combine-sink sink_name="$mixer_output_name" slaves="$mixer_name,$original_output_name" sink_properties=$(device_description "$mixer_output_name")
load_module module-loopback source="$original_input_name" sink="$mixer_name" latency_msec=20
load_module module-null-sink sink_name="$mixer_silence_sink" sink_properties=$(device_description "$mixer_silence_sink") rate=44100
load_module module-echo-cancel \
source_name="$mixer_input_name" source_properties=$(device_description "$mixer_input_name") source_master="${mixer_name}.monitor" \
sink_name="$mixer_silence_sink" sink_properties=$(device_description "$mixer_silence_sink") sink_master="$mixer_silence_sink" aec_method=null
echo "new output(headphones) name: $mixer_output_name"
echo "new input(mic) name: $mixer_input_name"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment