My default audio device gets swapped every time my computer sleeps

I have two monitors that present as having sound devices, but only one actually does. When my computer wakes up from a sleep it will always be on the HDMI 2 output, which results in no sound. I have to go into the panel and make the switch to HDMI 3.

Is there any way around this?

Please post responses to these commands to show information about PulseAudio Sinks (outputs)

pacmd list-sinks

and

pacmd dump | grep -Ev 'module|source'
$ pacmd list-sinks
2 sink(s) available.
  * index: 1
        name: <alsa_output.pci-0000_28_00.4.analog-stereo>
        driver: <module-alsa-card.c>
        flags: HARDWARE HW_MUTE_CTRL HW_VOLUME_CTRL DECIBEL_VOLUME LATENCY 
        state: SUSPENDED
        suspend cause: IDLE
        priority: 9039
        volume: front-left: 45221 /  69% / -9.67 dB,   front-right: 45221 /  69% / -9.67 dB
                balance 0.00
        base volume: 65536 / 100% / 0.00 dB
        volume steps: 65537
        muted: no
        current latency: 0.00 ms
        max request: 0 KiB
        max rewind: 0 KiB
        monitor source: 1
        sample spec: s16le 2ch 44100Hz
        channel map: front-left,front-right
                     Stereo
        used by: 0
        linked by: 0
        fixed latency: 100.14 ms
        card: 1 <alsa_card.pci-0000_28_00.4>
        module: 7
        properties:
                alsa.resolution_bits = "16"
                device.api = "alsa"
                device.class = "sound"
                alsa.class = "generic"
                alsa.subclass = "generic-mix"
                alsa.name = "ALC892 Analog"
                alsa.id = "ALC892 Analog"
                alsa.subdevice = "0"
                alsa.subdevice_name = "subdevice #0"
                alsa.device = "0"
                alsa.card = "1"
                alsa.card_name = "HD-Audio Generic"
                alsa.long_card_name = "HD-Audio Generic at 0xf7400000 irq 96"
                alsa.driver_name = "snd_hda_intel"
                device.bus_path = "pci-0000:28:00.4"
                sysfs.path = "/devices/pci0000:00/0000:00:08.1/0000:28:00.4/sound/card1"
                device.bus = "pci"
                device.vendor.id = "1022"
                device.vendor.name = "Advanced Micro Devices, Inc. [AMD]"
                device.product.id = "1487"
                device.product.name = "Starship/Matisse HD Audio Controller"
                device.string = "front:1"
                device.buffering.buffer_size = "17664"
                device.buffering.fragment_size = "2944"
                device.access_mode = "mmap"
                device.profile.name = "analog-stereo"
                device.profile.description = "Analog Stereo"
                device.description = "Starship/Matisse HD Audio Controller Analog Stereo"
                alsa.mixer_name = "Realtek ALC892"
                alsa.components = "HDA:10ec0892,1462ec02,00100302"
                module-udev-detect.discovered = "1"
                device.icon_name = "audio-card-pci"
        ports:
                analog-output-lineout: Line Out (priority 9000, latency offset 0 usec, available: no)
                        properties:

                analog-output-headphones: Headphones (priority 9900, latency offset 0 usec, available: yes)
                        properties:
                                device.icon_name = "audio-headphones"
        active port: <analog-output-headphones>
    index: 20
        name: <alsa_output.pci-0000_26_00.1.hdmi-stereo-extra2>
        driver: <module-alsa-card.c>
        flags: HARDWARE DECIBEL_VOLUME LATENCY DYNAMIC_LATENCY
        state: RUNNING
        suspend cause: (none)
        priority: 9030
        volume: front-left: 49151 /  75% / -7.50 dB,   front-right: 49151 /  75% / -7.50 dB
                balance 0.00
        base volume: 65536 / 100% / 0.00 dB
        volume steps: 65537
        muted: no
        current latency: 18.68 ms
        max request: 3 KiB
        max rewind: 3 KiB
        monitor source: 22
        sample spec: s16le 2ch 48000Hz
        channel map: front-left,front-right
                     Stereo
        used by: 1
        linked by: 2
        configured latency: 18.75 ms; range is 0.50 .. 1837.33 ms
        card: 0 <alsa_card.pci-0000_26_00.1>
        module: 6
        properties:
                alsa.resolution_bits = "16"
                device.api = "alsa"
                device.class = "sound"
                alsa.class = "generic"
                alsa.subclass = "generic-mix"
                alsa.name = "HDMI 2"
                alsa.id = "HDMI 2"
                alsa.subdevice = "0"
                alsa.subdevice_name = "subdevice #0"
                alsa.device = "8"
                alsa.card = "0"
                alsa.card_name = "HDA NVidia"
                alsa.long_card_name = "HDA NVidia at 0xf7080000 irq 94"
                alsa.driver_name = "snd_hda_intel"
                device.bus_path = "pci-0000:26:00.1"
                sysfs.path = "/devices/pci0000:00/0000:00:03.1/0000:26:00.1/sound/card0"
                device.bus = "pci"
                device.vendor.id = "10de"
                device.vendor.name = "NVIDIA Corporation"
                device.product.id = "10f0"
                device.product.name = "GP104 High Definition Audio Controller"
                device.string = "hdmi:0,2"
                device.buffering.buffer_size = "352768"
                device.buffering.fragment_size = "176384"
                device.access_mode = "mmap+timer"
                device.profile.name = "hdmi-stereo-extra2"
                device.profile.description = "Digital Stereo (HDMI 3)"
                device.description = "GP104 High Definition Audio Controller Digital Stereo (HDMI 3)"
                alsa.mixer_name = "Nvidia GPU 83 HDMI/DP"
                alsa.components = "HDA:10de0083,10438625,00100100"
                module-udev-detect.discovered = "1"
                device.icon_name = "audio-card-pci"
        ports:
                hdmi-output-2: HDMI / DisplayPort 3 (priority 5700, latency offset 0 usec, available: yes)
                        properties:
                                device.icon_name = "video-display"
                                device.product.name = "MX27AQ
      "
        active port: <hdmi-output-2>

.

$ pacmd dump | grep -Ev 'module|source'
### Configuration dump generated at Sat Oct 17 12:30:25 2020


set-sink-volume alsa_output.pci-0000_28_00.4.analog-stereo 0xb0a5
set-sink-mute alsa_output.pci-0000_28_00.4.analog-stereo no
suspend-sink alsa_output.pci-0000_28_00.4.analog-stereo yes
set-sink-volume alsa_output.pci-0000_26_00.1.hdmi-stereo-extra2 0xbfff
set-sink-mute alsa_output.pci-0000_26_00.1.hdmi-stereo-extra2 no
suspend-sink alsa_output.pci-0000_26_00.1.hdmi-stereo-extra2 no


set-card-profile alsa_card.pci-0000_26_00.1 output:hdmi-stereo-extra2
set-card-profile alsa_card.pci-0000_28_00.4 output:analog-stereo
set-card-profile alsa_card.usb-046d_0825_B295D9E0-02 input:mono-fallback
set-default-sink alsa_output.pci-0000_28_00.4.analog-stereo

### EOF

I assume that data posted is for when the correct HDMI output has been set
from the profile shown:

device.profile.description = "Digital Stereo (HDMI 3)"

but you can probably see that some other numbers for this device are different

                device.profile.name = "hdmi-stereo-extra2"

                hdmi-output-2: HDMI / DisplayPort 3 (priority 5700, latency offset 0 usec, available: yes)

active port: <hdmi-output-2>

The line in pacmd dump has most of the command needed to set the correct Profile when needed
use this command in terminal to save having to adjust profile manually

pacmd set-card-profile alsa_card.pci-0000_26_00.1 output:hdmi-stereo-extra2

but that is just a workaround. It might work to prevent selection of the wrong Profile after resuming from sleep, but I would not expect it to survive a system reboot

For a complete solution, PulseAudio has to be stopped from connecting to “Digital Stereo (HDMI 2)”
If the output can be located and muted in ALSA, PulseAudio should detect that the output is no longer available and use the correct output

please post response to

amixer --card=0

so I can identify the correct item to mute

Putting it in ~/.config/pulse/default.pa does. I need this on my desktop to set lineout as default. It’s been quite awhile it stopped detecting the jack plug. I never got to check if it is a hardware problem or if it’s pulseaudio fault. But something changed around a year ago (or a bit more).

As an example, here’s my default.pa:

[mbb@mocho-desktop ~]$ cat .config/pulse/default.pa
.include /etc/pulse/default.pa

set-card-profile alsa_card.pci-0000_00_1b.0 output:analog-stereo+input:analog-stereo
set-sink-port alsa_output.pci-0000_00_1b.0.analog-stereo analog-output-lineout
#load-module module-equalizer-sink
#load-module module-dbus-protocol

### BEGIN: Equalized audio configuration
### Generated from: pulseaudio-equalizer
.nofail
load-module module-ladspa-sink sink_name=ladspa_output.mbeq_1197.mbeq sink_master=alsa_output.pci-0000_00_1b.0.analog-stereo plugin=mbeq_1197 label=mbeq control=-10.0,-12.0,-8.0,-6.0,-6.0,-4.0,-4.0,-2.0,-2.0,-4.0,-4.0,-2.0,0.0,0.0,2.0
set-default-sink ladspa_output.mbeq_1197.mbeq
set-sink-mute alsa_output.pci-0000_00_1b.0.analog-stereo 0
.fail
### END: Equalized audio configuration