Skip to content

Instantly share code, notes, and snippets.

@max-te
Created January 10, 2024 16:03
Show Gist options
  • Save max-te/c550b0dea44e1ed39e632b30d1fdc104 to your computer and use it in GitHub Desktop.
Save max-te/c550b0dea44e1ed39e632b30d1fdc104 to your computer and use it in GitHub Desktop.
Switch bluetooth audio profile based on wether something uses the mic
#!/bin/bash
if ! (pactl list short sources | grep virt); then
pactl load-module module-remap-source source_properties="device.description=virt" source_name=virt channel_map=mono channels=1
fi
trap "pactl unload-module module-remap-source" SIGINT
pactl set-default-source virt
LAST_STATE=""
pactl subscribe | while read line; do
echo $line
if [[ "$line" == *"change"* ]]; then
HAS_CONSUMER=$(pactl --format=json list source-outputs | jq -r '[.[].properties."target.object" == "virt"] | any')
if [ "$HAS_CONSUMER" != "$LAST_STATE" ]; then
echo "$HAS_CONSUMER"
if $HAS_CONSUMER == "true"; then
echo "Headset mode"
pactl set-card-profile bluez_card.AC_12_2F_0A_C0_ED headset-head-unit-msbc
else
echo "Hifi mode"
pactl set-card-profile bluez_card.AC_12_2F_0A_C0_ED a2dp-sink
fi
fi
LAST_STATE="$HAS_CONSUMER"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment