#!/bin/bash
# 获取第一个USB播放设备名
USB_SINK=$(pacmd list-sinks | grep -A1 "name:" | grep -i "usb" | head -n1 | awk '{print $2}' | tr -d '<>')
# 获取第一个内置播放设备名
DEFAULT_SINK=$(pacmd list-sinks | grep -A1 "name:" | grep -v -i "usb" | head -n1 | awk '{print $2}' | tr -d '<>')

# 获取第一个USB录音设备名
USB_SOURCE=$(pacmd list-sources | grep -A1 "name:" | grep -i "usb" | grep -i "alsa_input" | head -n1 | awk '{print $2}' | tr -d '<>')
# 获取第一个内置录音设备名
DEFAULT_SOURCE=$(pacmd list-sources | grep -A1 "name:" | grep -v -i "usb" | grep -i "alsa_input" | head -n1 | awk '{print $2}' | tr -d '<>')

# 动态设置播放设备
if [ -n "$USB_SINK" ]; then
    echo "set default sink:${USB_SINK}"
    pacmd set-default-sink "$USB_SINK"
else
    echo "set default sink:${DEFAULT_SINK}"
    pacmd set-default-sink "$DEFAULT_SINK"
fi

# 动态设置录音设备
if [ -n "$USB_SOURCE" ]; then
    echo "set default source:${USB_SOURCE}"
    pacmd set-default-source "$USB_SOURCE"
else
    echo "set default source:${DEFAULT_SOURCE}"
    pacmd set-default-source "$DEFAULT_SOURCE"
fi
amixer -D pulse sset Master 80%
