diff --git a/.config/scripts/pipewire/link-virtual-mic.sh b/.config/scripts/pipewire/link-virtual-mic.sh new file mode 100755 index 0000000..32d567c --- /dev/null +++ b/.config/scripts/pipewire/link-virtual-mic.sh @@ -0,0 +1,71 @@ +#!/bin/sh + +# See https://superuser.com/questions/1675877/how-to-create-a-new-pipewire-virtual-device-that-to-combines-an-real-input-and-o + +declare -A mapping +declare -A mappingDetach + +# Map various audio outputs to be inputs to other sinks: +# +# Loopback Monitor -\ +# -> Combined Sink/Source -> Virtual Microphone -> Voice software +# rnnoise mic output -/ +mapping+=( + # Link loopback and mic to combined sink + ["loopback:monitor_FL"]="mixed-mic:input_FL" + ["loopback:monitor_FR"]="mixed-mic:input_FR" + + #["output\.filter-chain.*:capture_1"]="mixed-mic:input_FL" + #["output\.filter-chain.*:capture_2"]="mixed-mic:input_FR" + ["output\.rnnoise_source:capture_1"]="mixed-mic:input_FL" + ["output\.rnnoise_source:capture_2"]="mixed-mic:input_FR" + # Link combined sink to virtual mic + #["combined-sink:monitor_FL"]="mixed-mic:input_FL" + #["combined-sink:monitor_FR"]="mixed-mic:input_FR" +) + +mappingDetach+=( + ["output\.rnnoise_source:capture_1"]="mixed-mic:input_FL" + ["output\.rnnoise_source:capture_2"]="mixed-mic:input_FR" +) + +completed=false +tries=0 + +if [[ "$1" == "--disconnect" ]] || [[ "$1" == "-d" ]]; then + for key in ${!mapping[@]}; do + value=${mapping[${key}]} + + tmp=$(pw-link -o | grep -e ${key}) || break + output=$(echo $tmp | head -n1) + + tmp=$(pw-link -i | grep -e ${value}) || break + input=$(echo $tmp | head -n1) + + pw-link -d ${key} ${input} &> /dev/null + done + exit +fi + +while not $completed && [ "$tries" -lt "20" ]; do + for key in ${!mapping[@]}; do + completed=false + value=${mapping[${key}]} + + # Check if the outpts and inputs exist before trying to link them, otherwise causes issues. + tmp=$(pw-link -o | grep -e ${key}) || break + output=$(echo $tmp | head -n1) + + tmp=$(pw-link -i | grep -e ${value}) || break + input=$(echo $tmp | head -n1) + + if [[ "$1" == "--disconnect-all" ]] || [[ "$1" == "-da" ]]; then + pw-link -d ${output} ${input} &> /dev/null + else + pw-link ${output} ${input} &> /dev/null + fi + completed=true + done + sleep 1 + tries=$((tries+1)) +done