Skip to content

Instantly share code, notes, and snippets.

@untainsYD
Created July 8, 2025 14:55
Show Gist options
  • Save untainsYD/190066865c94733a548c4be120d6171b to your computer and use it in GitHub Desktop.
Save untainsYD/190066865c94733a548c4be120d6171b to your computer and use it in GitHub Desktop.
Sony MX5/MX6 audio profile switcher
# Sony MX5 profile switcher with proper profile names
# Features:
# - Automatic notifications when switching profiles
# - Toggle between LDAC and mSBC with visual feedback
# - Shows profile transition (from → to) in notifications
DEVICE_MAC="AC:80:0A:30:3A:DD"
get_card_name() {
pactl list cards short | grep bluez_card.${DEVICE_MAC//:/_} | cut -f2
}
get_active_profile() {
local card=$1
pactl list cards | grep -A50 "$card" | grep "Active Profile:" | awk '{print $3}'
}
switch_profile() {
local profile=$1
echo "Switching to profile: $profile"
# First, ensure card exists
CARD=$(get_card_name)
if [ -z "$CARD" ]; then
echo "Device not connected"
return 1
fi
# Switch to the profile
pactl set-card-profile "$CARD" "$profile"
local result=$?
# Check if successful
if [ $result -eq 0 ]; then
echo "Successfully switched to $profile"
# Show current active profile
echo "Active profile: $(get_active_profile "$CARD")"
return 0
else
echo "Failed to switch profile"
echo "Available profiles:"
pactl list cards | grep -A50 "$CARD" | sed -n '/Profiles:/,/Active Profile:/p' | grep -E "^\s+[a-z]" | sed 's/^[[:space:]]*/ /'
return 1
fi
}
get_profile_name() {
local profile=$1
case "$profile" in
"a2dp-sink") echo "LDAC (High Quality)" ;;
"a2dp-sink-sbc") echo "SBC (Standard)" ;;
"a2dp-sink-sbc_xq") echo "SBC-XQ (High Quality)" ;;
"a2dp-sink-aac") echo "AAC (High Quality)" ;;
"headset-head-unit") echo "mSBC (Voice Calls)" ;;
"headset-head-unit-cvsd") echo "CVSD (Voice Calls)" ;;
"off") echo "Disabled" ;;
*) echo "Unknown" ;;
esac
}
toggle_ldac_msbc() {
CARD=$(get_card_name)
if [ -z "$CARD" ]; then
echo "Device not connected"
notify-send -u critical "Bluetooth Audio" "Device not connected"
exit 1
fi
CURRENT_PROFILE=$(get_active_profile "$CARD")
FROM_NAME=$(get_profile_name "$CURRENT_PROFILE")
case "$CURRENT_PROFILE" in
"a2dp-sink"|"a2dp-sink-sbc"|"a2dp-sink-sbc_xq"|"a2dp-sink-aac")
# Currently on A2DP, switch to mSBC
echo "Currently on A2DP ($CURRENT_PROFILE), switching to mSBC..."
if switch_profile "headset-head-unit"; then
TO_NAME="mSBC (Voice Calls)"
notify-send -i audio-headset -t 2000 "Audio Profile Switched" "$FROM_NAME → $TO_NAME"
else
notify-send -u critical "Audio Profile" "Failed to switch profile"
fi
;;
"headset-head-unit"|"headset-head-unit-cvsd")
# Currently on HFP, switch to LDAC
echo "Currently on HFP ($CURRENT_PROFILE), switching to LDAC..."
if switch_profile "a2dp-sink"; then
TO_NAME="LDAC (High Quality)"
notify-send -i audio-headset "Audio Profile Switched" "$FROM_NAME → $TO_NAME"
else
notify-send -u critical "Audio Profile" "Failed to switch profile"
fi
;;
"off")
# Device is off, default to LDAC
echo "Device is off, switching to LDAC..."
if switch_profile "a2dp-sink"; then
TO_NAME="LDAC (High Quality)"
notify-send -i audio-headset "Audio Profile Switched" "$FROM_NAME → $TO_NAME"
else
notify-send -u critical "Audio Profile" "Failed to switch profile"
fi
;;
*)
echo "Unknown profile: $CURRENT_PROFILE, switching to LDAC..."
if switch_profile "a2dp-sink"; then
TO_NAME="LDAC (High Quality)"
notify-send -i audio-headset "Audio Profile Switched" "$FROM_NAME → $TO_NAME"
else
notify-send -u critical "Audio Profile" "Failed to switch profile"
fi
;;
esac
}
case "$1" in
ldac|hq)
# Use the exact profile name from your system
if switch_profile "a2dp-sink"; then
notify-send -i audio-headset -t 2000 "Audio Profile" "Switched to LDAC (High Quality)"
fi
;;
sbc)
if switch_profile "a2dp-sink-sbc"; then
notify-send -i audio-headset -t 2000 "Audio Profile" "Switched to SBC (Standard)"
fi
;;
sbc-xq)
if switch_profile "a2dp-sink-sbc_xq"; then
notify-send -i audio-headset -t 2000 "Audio Profile" "Switched to SBC-XQ (High Quality)"
fi
;;
aac)
if switch_profile "a2dp-sink-aac"; then
notify-send -i audio-headset -t 2000 "Audio Profile" "Switched to AAC (High Quality)"
fi
;;
msbc|voice)
# mSBC profile is just "headset-head-unit"
if switch_profile "headset-head-unit"; then
notify-send -i audio-headset -t 2000 "Audio Profile" "Switched to mSBC (Voice Calls)"
fi
;;
cvsd)
if switch_profile "headset-head-unit-cvsd"; then
notify-send -i audio-headset -t 2000 "Audio Profile" "Switched to CVSD (Voice Calls)"
fi
;;
off)
if switch_profile "off"; then
notify-send -i audio-headset -t 2000 "Audio Profile" "Audio Disabled"
fi
;;
toggle)
# Toggle between LDAC and mSBC
toggle_ldac_msbc
;;
list)
# Show all available profiles
CARD=$(get_card_name)
if [ -z "$CARD" ]; then
echo "Device not connected"
exit 1
fi
echo "Available profiles for $CARD:"
pactl list cards | grep -A50 "$CARD" | sed -n '/Profiles:/,/Active Profile:/p' | grep -E "^\s+[a-z]" | sed 's/^[[:space:]]*/ /'
echo ""
echo "Active profile: $(get_active_profile "$CARD")"
;;
status)
# Just show current status
CARD=$(get_card_name)
if [ -z "$CARD" ]; then
echo "Device not connected"
exit 1
fi
CURRENT_PROFILE=$(get_active_profile "$CARD")
echo "Current profile: $CURRENT_PROFILE"
case "$CURRENT_PROFILE" in
"a2dp-sink") echo "Mode: High Quality Audio (LDAC)" ;;
"a2dp-sink-sbc") echo "Mode: Standard Audio (SBC)" ;;
"a2dp-sink-sbc_xq") echo "Mode: High Quality Audio (SBC-XQ)" ;;
"a2dp-sink-aac") echo "Mode: High Quality Audio (AAC)" ;;
"headset-head-unit") echo "Mode: Voice Calls (mSBC)" ;;
"headset-head-unit-cvsd") echo "Mode: Voice Calls (CVSD)" ;;
"off") echo "Mode: Disabled" ;;
*) echo "Mode: Unknown" ;;
esac
;;
*)
echo "Usage: $0 {ldac|hq|sbc|sbc-xq|aac|msbc|voice|cvsd|off|toggle|list|status}"
echo ""
echo "Profiles:"
echo " ldac/hq - High quality LDAC codec (default A2DP)"
echo " sbc - Standard SBC codec"
echo " sbc-xq - SBC-XQ (high quality SBC)"
echo " aac - AAC codec"
echo " msbc/voice - mSBC for voice calls (HFP)"
echo " cvsd - CVSD for voice calls (HFP fallback)"
echo " off - Disable audio"
echo " toggle - Toggle between LDAC and mSBC"
echo " list - Show all available profiles"
echo " status - Show current profile status"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment