[SOLVED] Set HDMI monitor as Default sound output

Forum rules
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
89123612
Level 1
Level 1
Posts: 39
Joined: Mon Nov 22, 2021 12:10 pm

[SOLVED] Set HDMI monitor as Default sound output

Post by 89123612 »

Hello everyone,
I am a new user of Mint 20 Cinnamon, after having used Mint 19 just for a couple of months. After I updated the version I noticed a new problem happening, but since I updated Mint around the same time I plugged an HDMI monitor to my laptop for the first time, it could well be that the version change is only coincidental.

Please be patient if my questions are silly but I have absolutely no experience, I also tried to browse the previous threads and found hints regarding Pulse Audio and HDAJackRetask, but to be honest since I lack basic knowledge I tried to replicate some of the steps in a kind of "mechanical" way so it is not surprising that I had no luck, it is also entirely possible that I messed things further. My bad of course! I hope to find some more tailored guidance here.

The problem is that once I power up the computer, the sound is coming from the laptop speakers, while I wish it to come out of the HDMI monitor speakers, because their sound quality is better.
I can manually change the output between the two by clicking on the icon on the lower right of the screen, but the settings is lost at every reboot.

As I said, I tried to set the default output source with Pulse Audio but I was only able to choose a single output at a time from the Settings and Output menus, while I was trying to find a way to force the laptop speakers permanently off as to have the HDMI monitor playing all the time (at least that's what I thought was possible as per my understanding of some previous posts). I tried to check some console readings but I wasn't able to understand them.

Where do I start?
Again, thank you very much!
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
SMG
Level 25
Level 25
Posts: 32007
Joined: Sun Jul 26, 2020 6:15 pm
Location: USA

Re: Set HDMI monitor as Default sound output

Post by SMG »

89123612 wrote: Mon Nov 22, 2021 1:03 pmThe problem is that once I power up the computer, the sound is coming from the laptop speakers, while I wish it to come out of the HDMI monitor speakers, because their sound quality is better.
I can manually change the output between the two by clicking on the icon on the lower right of the screen, but the settings is lost at every reboot.

As I said, I tried to set the default output source with Pulse Audio but I was only able to choose a single output at a time from the Settings and Output menus, while I was trying to find a way to force the laptop speakers permanently off as to have the HDMI monitor playing all the time (at least that's what I thought was possible as per my understanding of some previous posts). I tried to check some console readings but I wasn't able to understand them.
Welcome to the forum, 89123612.

What you mean when you say you tried to set the default output source with Pulse Audio, but was only able to choose a single output? Does that mean it is only letting you set one monitor speaker as the output?

Maybe something in this older webpage How to Use PulseAudio to Manage Sounds on Ubuntu 18.04 (you can skip the early parts about installing Pulse Audio since you already have it installed) will give you ideas of what to try.
Image
A woman typing on a laptop with LM20.3 Cinnamon.
89123612
Level 1
Level 1
Posts: 39
Joined: Mon Nov 22, 2021 12:10 pm

Re: Set HDMI monitor as Default sound output

Post by 89123612 »

SMG wrote: Fri Nov 26, 2021 9:34 pm
89123612 wrote: Mon Nov 22, 2021 1:03 pmThe problem is that once I power up the computer, the sound is coming from the laptop speakers, while I wish it to come out of the HDMI monitor speakers, because their sound quality is better.
I can manually change the output between the two by clicking on the icon on the lower right of the screen, but the settings is lost at every reboot.

As I said, I tried to set the default output source with Pulse Audio but I was only able to choose a single output at a time from the Settings and Output menus, while I was trying to find a way to force the laptop speakers permanently off as to have the HDMI monitor playing all the time (at least that's what I thought was possible as per my understanding of some previous posts). I tried to check some console readings but I wasn't able to understand them.
Welcome to the forum, 89123612.

What you mean when you say you tried to set the default output source with Pulse Audio, but was only able to choose a single output? Does that mean it is only letting you set one monitor speaker as the output?

Maybe something in this older webpage How to Use PulseAudio to Manage Sounds on Ubuntu 18.04 (you can skip the early parts about installing Pulse Audio since you already have it installed) will give you ideas of what to try.

Hello SMG, thanks for your welcome and for coming back to me!

I'll admit that I wasn't very clear, most likely because I have some problems with English. I didn't mean to say that Pulse Audio allows me to set only one monitor speaker as output.
What I wanted to say is that, if I go into the "Configuration" tab of Pulse Audio, I am able to select "Digital stereo (HDMI) Output" OR "Analog Stereo Duplex", among the others. I choose Digital Stereo HDMI since I want the sound to come from my speakers, and all is well. The problem is that I cannot find a way to prevent "Analog stereo duplex" to be the default choice the next time I start the computer. From what I've read, there should be a way to select the default Output source, also by preventing the unwanted source to be selected at next boot. At least that's what I understood. But I can only select one source, which would be fine of course if only it would stay selected also for the next computer use.
If I go in the "Output Devices" tab, again I find "Internal Audio Digital stereo" set, and the HDMI correctly plugged in and sound coming from the monitor speakers. No complaints.

But, next time I start the computer I find the "Internal Audio Analog Stereo" selected, and the sound coming from the laptop speakers. Again, from what I've read there should be a way to select a "fallback" device by flagging one of the outputs, and preventing it to be changed everytime, but I cannot select a device over another since I only see Analog Stereo or Digital stereo at any given time in the output section, based on what my computers prefers to choose randomly at each reboot. I've tried to give some commands with the console which should have selected the Digital Audio output once and for all, also by editing the default.pa file in pulseaudio and putting in the address of the HDMI monitor (I believe it was called the "sink" or something similar?) so as it can be always set as default, but I had no luck!

I'll attach a couple of pics of what I'm talking about (with the flag in the Output section, I can change the background color but not actually flag/unflag anything, the choice is fixed.)

Thanks again!
Attachments
Schermata del 2021-11-28 12-10-25.png
Schermata del 2021-11-28 12-11-00.png
Schermata del 2021-11-28 12-12-38.png
User avatar
AndyMH
Level 21
Level 21
Posts: 13759
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: Set HDMI monitor as Default sound output

Post by AndyMH »

Same/similar issue:
viewtopic.php?f=48&t=341943&p=1966718&h ... d#p1966718
The answer is in this link https://unix.stackexchange.com/question ... -the-shell
I haven't got round to doing it yet. But you want a script of the following form:

Code: Select all

#!/bin/bash
if xrandr | grep -q "HDMI-2 connected"; then
   pacmd set-card-profile 0 output:hdmi-stereo
fi
checks to see if the monitor is connected and if so sets the sound output. Modify as appropriate, make your script executable and put in it your startup applications.

EDIT - thought I would get around to doing it. The script above didn't work - wrong output profile. Final working script:

Code: Select all

#!/bin/bash
# find out what card first with pacmd list-cards, then find the right output profile
# with pacmd list-sinks | grep name with HDMI on the monitor connected manually
# https://unix.stackexchange.com/questions/62818/how-can-i-switch-between-different-audio-output-hardware-using-the-shell
if xrandr | grep -q "HDMI-2 connected"; then
   pacmd set-card-profile 0 output:hdmi-stereo-extra1
fi
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
User avatar
SMG
Level 25
Level 25
Posts: 32007
Joined: Sun Jul 26, 2020 6:15 pm
Location: USA

Re: Set HDMI monitor as Default sound output

Post by SMG »

89123612 wrote: Sun Nov 28, 2021 7:20 am The problem is that I cannot find a way to prevent "Analog stereo duplex" to be the default choice the next time I start the computer. From what I've read, there should be a way to select the default Output source, also by preventing the unwanted source to be selected at next boot. At least that's what I understood. But I can only select one source, which would be fine of course if only it would stay selected also for the next computer use.
From what I read, I thought you could do that as well.

I do not currently have multiple outputs to run any tests, but I did find this tutorial How-to: Setup default audio output Ubuntu (Pulseaudio) which sounds like it should give you the outcome you want. At the bottom it references an ArchLinux page, but there is a typo in the link. The correct link is ArchLinux: PulseAudio.

It sounds like you will always have the monitor attached, but if you do not, then AndyMH's script may help you achieve your goal.
Image
A woman typing on a laptop with LM20.3 Cinnamon.
89123612
Level 1
Level 1
Posts: 39
Joined: Mon Nov 22, 2021 12:10 pm

Re: Set HDMI monitor as Default sound output

Post by 89123612 »

AndyMH wrote: Sun Nov 28, 2021 12:59 pm Same/similar issue:
viewtopic.php?f=48&t=341943&p=1966718&h ... d#p1966718
The answer is in this link https://unix.stackexchange.com/question ... -the-shell
I haven't got round to doing it yet. But you want a script of the following form:

Code: Select all

#!/bin/bash
if xrandr | grep -q "HDMI-2 connected"; then
   pacmd set-card-profile 0 output:hdmi-stereo
fi
checks to see if the monitor is connected and if so sets the sound output. Modify as appropriate, make your script executable and put in it your startup applications.

EDIT - thought I would get around to doing it. The script above didn't work - wrong output profile. Final working script:

Code: Select all

#!/bin/bash
# find out what card first with pacmd list-cards, then find the right output profile
# with pacmd list-sinks | grep name with HDMI on the monitor connected manually
# https://unix.stackexchange.com/questions/62818/how-can-i-switch-between-different-audio-output-hardware-using-the-shell
if xrandr | grep -q "HDMI-2 connected"; then
   pacmd set-card-profile 0 output:hdmi-stereo-extra1
fi

Thank you AndyMH for taking the time to help me, I was trying to do what you suggested but I realized I lack some basic knowledge to do it. I've tried to read some guides regarding how to do an executable script and insert it on startup applications but they all suggest different paths to achieve the same things. I'll have to take some time to understand how to do it properly, I get it is very easy but still I am not yet able to do it. I will definitely educate myself and come back to you.

In the meantime, however, I am still not sure about the names of the output source I want to set as default. If I do:

$ pacmd list-cards

That's what I get

Code: Select all

1 card(s) available.
    index: 0
	name: <alsa_card.pci-0000_00_1f.3>
	driver: <module-alsa-card.c>
	owner module: 7
	properties:
		alsa.card = "0"
		alsa.card_name = "HDA Intel PCH"
		alsa.long_card_name = "HDA Intel PCH at 0x94328000 irq 134"
		alsa.driver_name = "snd_hda_intel"
		device.bus_path = "pci-0000:00:1f.3"
		sysfs.path = "/devices/pci0000:00/0000:00:1f.3/sound/card0"
		device.bus = "pci"
		device.vendor.id = "8086"
		device.vendor.name = "Intel Corporation"
		device.product.id = "a170"
		device.product.name = "100 Series/C230 Series Chipset Family HD Audio Controller"
		device.form_factor = "internal"
		device.string = "0"
		device.description = "Audio interno"
		module-udev-detect.discovered = "1"
		device.icon_name = "audio-card-pci"
	profiles:
		input:analog-stereo: Ingresso «Stereo analogico» (priority 32833, available: unknown)
		output:analog-stereo: Uscita «Stereo analogico» (priority 39268, available: unknown)
		output:analog-stereo+input:analog-stereo: Duplex stereo analogico (priority 39333, available: unknown)
		output:hdmi-stereo: Uscita «Digital Stereo (HDMI)» (priority 38668, available: unknown)
		output:hdmi-stereo+input:analog-stereo: Uscita «Digital Stereo (HDMI)» + Ingresso «Stereo analogico» (priority 38733, available: unknown)
		output:hdmi-stereo-extra1: Uscita «Digital Stereo (HDMI 2)» (priority 5700, available: no)
		output:hdmi-stereo-extra1+input:analog-stereo: Uscita «Digital Stereo (HDMI 2)» + Ingresso «Stereo analogico» (priority 5765, available: unknown)
		output:hdmi-surround-extra1: Uscita «Digital Surround 5.1 (HDMI 2)» (priority 600, available: no)
		output:hdmi-surround-extra1+input:analog-stereo: Uscita «Digital Surround 5.1 (HDMI 2)» + Ingresso «Stereo analogico» (priority 665, available: unknown)
		output:hdmi-surround71-extra1: Uscita «Digital Surround 7.1 (HDMI 2)» (priority 600, available: no)
		output:hdmi-surround71-extra1+input:analog-stereo: Uscita «Digital Surround 7.1 (HDMI 2)» + Ingresso «Stereo analogico» (priority 665, available: unknown)
		output:hdmi-stereo-extra2: Uscita «Digital Stereo (HDMI 3)» (priority 5700, available: no)
		output:hdmi-stereo-extra2+input:analog-stereo: Uscita «Digital Stereo (HDMI 3)» + Ingresso «Stereo analogico» (priority 5765, available: unknown)
		output:hdmi-surround-extra2: Uscita «Digital Surround 5.1 (HDMI 3)» (priority 600, available: no)
		output:hdmi-surround-extra2+input:analog-stereo: Uscita «Digital Surround 5.1 (HDMI 3)» + Ingresso «Stereo analogico» (priority 665, available: unknown)
		output:hdmi-surround71-extra2: Uscita «Digital Surround 7.1 (HDMI 3)» (priority 600, available: no)
		output:hdmi-surround71-extra2+input:analog-stereo: Uscita «Digital Surround 7.1 (HDMI 3)» + Ingresso «Stereo analogico» (priority 665, available: unknown)
		output:hdmi-stereo-extra3: Uscita «Digital Stereo (HDMI 4)» (priority 5700, available: no)
		output:hdmi-stereo-extra3+input:analog-stereo: Uscita «Digital Stereo (HDMI 4)» + Ingresso «Stereo analogico» (priority 5765, available: unknown)
		output:hdmi-surround-extra3: Uscita «Digital Surround 5.1 (HDMI 4)» (priority 600, available: no)
		output:hdmi-surround-extra3+input:analog-stereo: Uscita «Digital Surround 5.1 (HDMI 4)» + Ingresso «Stereo analogico» (priority 665, available: unknown)
		output:hdmi-surround71-extra3: Uscita «Digital Surround 7.1 (HDMI 4)» (priority 600, available: no)
		output:hdmi-surround71-extra3+input:analog-stereo: Uscita «Digital Surround 7.1 (HDMI 4)» + Ingresso «Stereo analogico» (priority 665, available: unknown)
		output:hdmi-stereo-extra4: Uscita «Digital Stereo (HDMI 5)» (priority 5700, available: no)
		output:hdmi-stereo-extra4+input:analog-stereo: Uscita «Digital Stereo (HDMI 5)» + Ingresso «Stereo analogico» (priority 5765, available: unknown)
		output:hdmi-surround-extra4: Uscita «Digital Surround 5.1 (HDMI 5)» (priority 600, available: no)
		output:hdmi-surround-extra4+input:analog-stereo: Uscita «Digital Surround 5.1 (HDMI 5)» + Ingresso «Stereo analogico» (priority 665, available: unknown)
		output:hdmi-surround71-extra4: Uscita «Digital Surround 7.1 (HDMI 5)» (priority 600, available: no)
		output:hdmi-surround71-extra4+input:analog-stereo: Uscita «Digital Surround 7.1 (HDMI 5)» + Ingresso «Stereo analogico» (priority 665, available: unknown)
		off: Spento (priority 0, available: unknown)
	active profile: <output:hdmi-stereo>
	sinks:
		alsa_output.pci-0000_00_1f.3.hdmi-stereo/#0: Audio interno Digital Stereo (HDMI)
	sources:
		alsa_output.pci-0000_00_1f.3.hdmi-stereo.monitor/#0: Monitor of Audio interno Digital Stereo (HDMI)
	ports:
		analog-input-internal-mic: Microfono interno (priority 8900, latency offset 0 usec, available: unknown)
			properties:
				device.icon_name = "audio-input-microphone"
		analog-input-mic: Microfono (priority 8700, latency offset 0 usec, available: no)
			properties:
				device.icon_name = "audio-input-microphone"
		analog-output-speaker: Altoparlanti (priority 10000, latency offset 0 usec, available: unknown)
			properties:
				device.icon_name = "audio-speakers"
		analog-output-headphones: Cuffie analogiche (priority 9900, latency offset 0 usec, available: no)
			properties:
				device.icon_name = "audio-headphones"
		hdmi-output-0: HDMI / DisplayPort (priority 5900, latency offset 0 usec, available: yes)
			properties:
				device.icon_name = "video-display"
				device.product.name = "Q24i-1L"
		hdmi-output-1: HDMI / DisplayPort 2 (priority 5800, latency offset 0 usec, available: no)
			properties:
				device.icon_name = "video-display"
		hdmi-output-2: HDMI / DisplayPort 3 (priority 5700, latency offset 0 usec, available: no)
			properties:
				device.icon_name = "video-display"
		hdmi-output-3: HDMI / DisplayPort 4 (priority 5600, latency offset 0 usec, available: no)
			properties:
				device.icon_name = "video-display"
		hdmi-output-4: HDMI / DisplayPort 5 (priority 5500, latency offset 0 usec, available: no)
			properties:
				device.icon_name = "video-display"


While with

$ pacmd list-sinks | grep name

That's the result

Code: Select all

name: <alsa_output.pci-0000_00_1f.3.hdmi-stereo>
		alsa.name = "HDMI 0"
		alsa.subdevice_name = "subdevice #0"
		alsa.card_name = "HDA Intel PCH"
		alsa.long_card_name = "HDA Intel PCH at 0x94328000 irq 134"
		alsa.driver_name = "snd_hda_intel"
		device.vendor.name = "Intel Corporation"
		device.product.name = "100 Series/C230 Series Chipset Family HD Audio Controller"
		device.profile.name = "hdmi-stereo"
		device.icon_name = "audio-card-pci"
				device.icon_name = "video-display"
				device.product.name = "Q24i-1L"

So if I understand it correctly the card is 0, the profile is Output n. 3 (hdmi-stereo) and the sink is alsa_output.pci-0000_00_1f.3.hdmi-stereo, that is to say "HDMI 0".
In fact, with the command

pacmd list-sinks | less

(which is in the link kindly provided by SMG) that's what I get

Code: Select all

 sink(s) available.
  * index: 0
        name: <alsa_output.pci-0000_00_1f.3.hdmi-stereo>
        driver: <module-alsa-card.c>
        flags: HARDWARE DECIBEL_VOLUME LATENCY DYNAMIC_LATENCY
        state: SUSPENDED
        suspend cause: IDLE
        priority: 9030
        volume: front-left: 49156 /  75% / -7,49 dB,   front-right: 49156 /  75% / -7,49 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: 0
        sample spec: s16le ch 2 48000 Hz
        channel map: front-left,front-right
                     Stereo
        used by: 0
        linked by: 1
Am I correct? Probably I am making some confusion between output names, sources, sinks, profile numbers and so on.
Trying to connect all these dots and editing my default.pa file, trying to follow the advices you have given me while at the same time editing previous suggestions found online which proved unsuccessful, that's the bottom of my default.pa file:

Code: Select all

### Modules to allow autoloading of filters (such as echo cancellation)
### on demand. module-filter-heuristics tries to determine what filters
### make sense, and module-filter-apply does the heavy-lifting of
### loading modules and rerouting streams.
load-module module-filter-heuristics
load-module module-filter-apply
load-module module alsa-sink device=hw:0,3

### Make some devices default
#set-default-sink output
#set-default-source input
set-card-profile 3 output:hdmi-stereo
set-default-sink 0
Again, sorry if I missed something but being so novice this all seems kind of overwhelming sometimes :lol:
and above all thank you for your time!
User avatar
AndyMH
Level 21
Level 21
Posts: 13759
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: Set HDMI monitor as Default sound output

Post by AndyMH »

Code: Select all

#!/bin/bash
if xrandr | grep -q "HDMI-2 connected"; then
   pacmd set-card-profile 0 output:hdmi-stereo-extra1
fi
Step by step:

#!/bin/bash - this is called a shebang and it tells linux what to use to run the script, in this case bash. More info here:
https://en.wikipedia.org/wiki/Shebang_(Unix)

if xrandr | grep -q "HDMI-2 connected"; then. I only want to set the sound to HDMI when my monitor is connected, if not connected then I want it to default to the laptop speakers. xrandr dumps a lot of info to the terminal. What we want to know is what port does your monitor connect to. Look at the output from xrandr and find out which port it is connecting on. If different (which is likely) to HDMI-2 then change to the correct port. | is the pipe command, so the output of xrandr is being passed to grep which is looking for the phrase "HDMI-2 connected". If there is a match, then the statement is effectively true.

Test the if statement by adding something to generate terminal output, e.g.

Code: Select all

#!/bin/bash
if xrandr | grep -q "HDMI-2 connected"; then
   echo "monitor found"
   pacmd set-card-profile 0 output:hdmi-stereo-extra1
fi
Setting the output:
pacmd set-card-profile 0 output:hdmi-stereo-extra1

Your output from pacmd list-cards shows that the profile is 0:

Code: Select all

1 card(s) available.
    index: 0
so the first part of the command is pacmd set-card-profile 0

The second part output:hdmi-stereo-extra1 is what I got wrong first time round. With the sound output manually set to HDMI pacmd list-sinks | grep name generates:

Code: Select all

andy@T432 ~ $ pacmd list-sinks | grep name
	name: <alsa_output.pci-0000_00_1b.0.hdmi-stereo-extra1>
		alsa.name = "HDMI 1"
		alsa.subdevice_name = "subdevice #0"
		alsa.card_name = "HDA Intel PCH"
		alsa.long_card_name = "HDA Intel PCH at 0xf2530000 irq 30"
		alsa.driver_name = "snd_hda_intel"
		device.vendor.name = "Intel Corporation"
		device.product.name = "7 Series/C216 Chipset Family High Definition Audio Controller"
		device.profile.name = "hdmi-stereo-extra1"  <<<<<<<<<<<<<<<<<<<<<<< this is what I want
		device.icon_name = "audio-card-pci"
				device.icon_name = "video-display"
				device.product.name = "LG ULTRAWIDE"
Tested that by manually setting the sound output to the laptop speakers and repeated:

Code: Select all

andy@T432 ~ $ pacmd list-sinks | grep name
	name: <alsa_output.pci-0000_00_1b.0.analog-stereo>
		alsa.name = "ALC3202 Analog"
		alsa.subdevice_name = "subdevice #0"
		alsa.card_name = "HDA Intel PCH"
		alsa.long_card_name = "HDA Intel PCH at 0xf2530000 irq 30"
		alsa.driver_name = "snd_hda_intel"
		device.vendor.name = "Intel Corporation"
		device.product.name = "7 Series/C216 Chipset Family High Definition Audio Controller"
		device.profile.name = "analog-stereo"  <<<<<<<<<<<<< output to laptop speakers
		device.icon_name = "audio-card-pci"
				device.icon_name = "audio-speakers"
				device.icon_name = "audio-headphones"
So I know that "hdmi-stereo-extra1" is what I want for HDMI output. Hence pacmd set-card-profile 0 output:hdmi-stereo-extra1.

Making your bash script executable - open your file manager, navigate to where you saved the file and right click on it. You then want the properties and then the permissions tab. Check the box 'allow executing as program'.
Screenshot from 2021-11-30 12-43-24.png
Where to save the file - if you save the script somewhere in PATH* you only need to specify the filename to run it, anywhere else you need to specify the full pathname. Simplest thing - if it doesn't exist create a folder bin in /home/you and put your script in there. Next time you boot /home/you/bin will be added to PATH. Open a terminal and if you called it setsound then

Code: Select all

andy@T432 ~ $ setsound
runs it. Test it.

Adding it to startup applications, if running cinnamon it is mint menu > preferences > startup applications. Click the + button at the bottom of the screen for a new startup app. You want a custom command.
Screenshot from 2021-11-30 12-56-30.png
Screenshot from 2021-11-30 12-56-30.png (18.89 KiB) Viewed 8178 times
If I had not saved the script somewhere in PATH, I would have to specify the full pathname, e.g. /home/andy/myscripts/setsound. I set a startup delay just in case other stuff needs initialising before the sound output is set.



* the folders linux searches when it is looking for a command to run, you can see what it is with echo $PATH.
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
89123612
Level 1
Level 1
Posts: 39
Joined: Mon Nov 22, 2021 12:10 pm

Re: Set HDMI monitor as Default sound output

Post by 89123612 »

Ok Andy, thanks again for your help, it is really appreciated. I guess helping me is not rewarding though since as I said I lack all the basic principles like what is echo or PATH. I tried anyways but I have been very slow and worse than that, I got stuck.

So after having re-checked, I am now pretty sure of the names I have to insert, which indeed are different from yours, so the command in my case should read:

Code: Select all

#!/bin/bash
if xrandr | grep -q "HDMI-0 connected"; then
   pacmd set-card-profile 0 output:hdmi-stereo
fi
But apparently I am wrong since I have been not able to test the command. If I insert this

Code: Select all

#!/bin/bash
if xrandr | grep -q "HDMI-0 connected"; then
   echo "monitor found"
   pacmd set-card-profile 0 output:hdmi-stereo
fi
I don't get any "monitor found" echo in the terminal, which I suppose should happen. So maybe I've already messed something.

Anyway, I then saved the command in a text, made the text executable, named it setsound and saved it in /home/luca/bin
When I try to run it for testing, however, I get

Code: Select all

/home/luca/bin/setsound: riga 1: #!/bin/bash: File o directory non esistente
which in English should translate #!/bin/bash - no such file or directory

Not sure if this comes from the same reason why the echo doesn't work, from an error in syntax or in creating the executable text file and putting it in the bin folder.
However I will check again and see if I can find what I am doing wrong! Then I will add the script to the startup application when it will be working.
Thanks again and sorry for being so disappointing! :(
User avatar
AndyMH
Level 21
Level 21
Posts: 13759
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: Set HDMI monitor as Default sound output

Post by AndyMH »

In a terminal whereis bash, this is what I get:

Code: Select all

andy@T432 ~ $ whereis bash
bash: /usr/bin/bash /etc/bash.bashrc /usr/share/man/man1/bash.1.gz
whereis tells you where a command lives.

If you get the same, won't go into the details of "usrmerge" and /bin/bash should work, but change #!/bin/bash to #!/usr/bin/bash. Post the output from xrandr with your monitor connected so I can check that was correct.

You can also check that where you put the script is in PATH, in a terminal echo $PATH. My output

Code: Select all

andy@T432 ~ $ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/andy/.local/sbin:/home/andy/.local/bin
I told you to put the script in /home/you/bin, and /home/you/bin should appear in the PATH output. My output is slightly different, I've added a couple of folders in /home/andy/.local rather than /home/andy/bin. When you boot, mint should put /home/you/bin in PATH (unless they have changed it).

You can also check the sound command. Manually set your sound output to your laptop speakers and then in a terminal

Code: Select all

pacmd set-card-profile 0 output:hdmi-stereo
Did it work, did it switch to HDMI sound out?

We will debug one line at a time :)
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
89123612
Level 1
Level 1
Posts: 39
Joined: Mon Nov 22, 2021 12:10 pm

Re: Set HDMI monitor as Default sound output

Post by 89123612 »

AndyMH wrote: Tue Nov 30, 2021 2:10 pm In a terminal whereis bash, this is what I get:

Code: Select all

andy@T432 ~ $ whereis bash
bash: /usr/bin/bash /etc/bash.bashrc /usr/share/man/man1/bash.1.gz
whereis tells you where a command lives.

If you get the same, won't go into the details of "usrmerge" and /bin/bash should work, but change #!/bin/bash to #!/usr/bin/bash. Post the output from xrandr with your monitor connected so I can check that was correct.

You can also check that where you put the script is in PATH, in a terminal echo $PATH. My output

Code: Select all

andy@T432 ~ $ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/andy/.local/sbin:/home/andy/.local/bin
I told you to put the script in /home/you/bin, and /home/you/bin should appear in the PATH output. My output is slightly different, I've added a couple of folders in /home/andy/.local rather than /home/andy/bin. When you boot, mint should put /home/you/bin in PATH (unless they have changed it).

You can also check the sound command. Manually set your sound output to your laptop speakers and then in a terminal

Code: Select all

pacmd set-card-profile 0 output:hdmi-stereo
Did it work, did it switch to HDMI sound out?

We will debug one line at a time :)
Thank you again Andy!
Starting from the end of your message, yes, if I give the command

Code: Select all

pacmd set-card-profile 0 output:hdmi-stereo
the output is switched to HDMI and all is working correctly, so at least we have a point here!

With whereis bash that's the response

Code: Select all

bash: /bin/bash /etc/bash.bashrc /usr/share/man/man1/bash.1.gz
If I put echo $PATH, that's what I get

Code: Select all

/home/luca/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
With xrandr that's the output:

Code: Select all

Screen 0: minimum 320 x 200, current 1920 x 1080, maximum 16384 x 16384
eDP-1 connected (normal left inverted right x axis y axis)
   1366x768      60.00 +  40.00  
   1360x768      59.80    59.96  
   1280x720      60.00    59.99    59.86    59.74  
   1024x768      60.04    60.00  
   960x720       60.00  
   928x696       60.05  
   896x672       60.01  
   1024x576      59.95    59.96    59.90    59.82  
   960x600       59.93    60.00  
   960x540       59.96    59.99    59.63    59.82  
   800x600       60.00    60.32    56.25  
   840x525       60.01    59.88  
   864x486       59.92    59.57  
   800x512       60.17  
   700x525       59.98  
   800x450       59.95    59.82  
   640x512       60.02  
   720x450       59.89  
   700x450       59.96    59.88  
   640x480       60.00    59.94  
   720x405       59.51    58.99  
   684x384       59.88    59.85  
   680x384       59.80    59.96  
   640x400       59.88    59.98  
   576x432       60.06  
   640x360       59.86    59.83    59.84    59.32  
   512x384       60.00  
   512x288       60.00    59.92  
   480x270       59.63    59.82  
   400x300       60.32    56.34  
   432x243       59.92    59.57  
   320x240       60.05  
   360x202       59.51    59.13  
   320x180       59.84    59.32  
HDMI-1 connected primary 1920x1080+0+0 (normal left inverted right x axis y axis) 527mm x 296mm
   1920x1080     60.00*+  74.99    50.00    59.94  
   1920x1080i    60.00    50.00    50.00    59.94  
   1680x1050     59.88  
   1600x900      60.00  
   1280x1024     75.02    70.00    60.02  
   1440x900      59.90  
   1152x864      75.00  
   1280x720      60.00    50.00    59.94  
   1024x768      75.03    70.07    60.00  
   832x624       74.55  
   800x600       72.19    75.00    60.32    56.25  
   720x576       50.00  
   720x480       60.00    59.94  
   640x480       75.00    72.81    60.00    59.94  
   720x400       70.08  
What i don't get here is why I see a reference to "HDMI-1 connected" while with pacmd list-sinks | grep name
I see among other things that alsa.name = "HDMI 0", not sure if this difference is relevant or not, probably not given that pacmd set-card-profile 0 output:hdmi-stereo works correctly.

Now I just need to put all this together to make the script work, so that I can set it to automatically start at each reboot. I really don't want to take too much advantage of your kindness, so every time I post here I also try to educate myself a little on Google but it is so dispersive sometimes. I promise I will try again though!
Finally, I've noticed that even if the script is not yet working, sometimes upon reboot the HDMI is correctly set as output, sometimes not. Of course it is nice when it is, but I don't get the rationale.
User avatar
AndyMH
Level 21
Level 21
Posts: 13759
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: Set HDMI monitor as Default sound output

Post by AndyMH »

So instead of

Code: Select all

#!/bin/bash
if xrandr | grep -q "HDMI-0 connected"; then
   echo "monitor found"
   pacmd set-card-profile 0 output:hdmi-stereo
fi
you want

Code: Select all

#!/bin/bash
if xrandr | grep -q "HDMI-1 connected"; then
   echo "monitor found"
   pacmd set-card-profile 0 output:hdmi-stereo
fi
grep is searching for a specific phrase which you can see in your xrandr output:

Code: Select all

HDMI-1 connected primary 1920x1080+0+0 (normal left inverted right x axis y axis) 527mm x 296mm
Once you have got it working you can remove the echo command.

Why the alsa name is different from the xrandr output - no idea, maybe alsa is counting from zero.
sometimes upon reboot the HDMI is correctly set as output, sometimes not.
It seems to be completely random, sometimes I've got sound via HDMI on boot, sometimes the laptop, hence the script.
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
89123612
Level 1
Level 1
Posts: 39
Joined: Mon Nov 22, 2021 12:10 pm

Re: Set HDMI monitor as Default sound output

Post by 89123612 »

Alright Andy, thanks again,
the script is now working and the echo too, so I removed the echo and wrote down the updated version of the script on a text file which again I've put on
/home/me/bin, however I still have the

Code: Select all

#!/bin/bash: File o directory non esistente


problem. As I said previously this is what I get from echo $PATH

Code: Select all

/home/luca/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
I will try to work on it later as soon as I end my workshift. One step at a time :wink: 8)
User avatar
AndyMH
Level 21
Level 21
Posts: 13759
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: Set HDMI monitor as Default sound output

Post by AndyMH »

Code: Select all

#!/bin/bash: File o directory non esistente
The only thing I can think of is did you make the script executable and described in one of my earlier posts? But that is not consistent with the script working and you have /home/luca/bin in PATH??

My bash skills are rudimentary, I know enough to get by. Hopefully someone else can contribute.
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
User avatar
SMG
Level 25
Level 25
Posts: 32007
Joined: Sun Jul 26, 2020 6:15 pm
Location: USA

Re: Set HDMI monitor as Default sound output

Post by SMG »

89123612 wrote: Wed Dec 01, 2021 9:45 amproblem. As I said previously this is what I get from echo $PATH

Code: Select all

/home/luca/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
My understanding of PATH is that it is executed in the order it is written.

If you look at your PATH and AndyMH's PATH you'll see you have /home/luca/bin first while it is last Andy's sequence.

andy@T432 ~ $ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/andy/.local/sbin:/home/andy/.local/bin

/home/luca/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

Try adding it to the end of PATH instead of at the beginning and see if the error clears.
Image
A woman typing on a laptop with LM20.3 Cinnamon.
User avatar
AndyMH
Level 21
Level 21
Posts: 13759
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: Set HDMI monitor as Default sound output

Post by AndyMH »

It's done automatically at the end of the default .profile:

Code: Select all

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi
and it adds it to the beginning of PATH (I would have added it at the end).
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
User avatar
SMG
Level 25
Level 25
Posts: 32007
Joined: Sun Jul 26, 2020 6:15 pm
Location: USA

Re: Set HDMI monitor as Default sound output

Post by SMG »

AndyMH wrote: Wed Dec 01, 2021 6:43 pm It's done automatically at the end of the default .profile:

Code: Select all

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi
and it adds it to the beginning of PATH (I would have added it at the end).
I never looked that closely at that file, but I do see it now.

!#/bin/bash: No such file or directory mentions the possibility of the file's encoding being a reason (that's the second answer since the original problem in that case was the ! and # being flip-flopped).
Image
A woman typing on a laptop with LM20.3 Cinnamon.
89123612
Level 1
Level 1
Posts: 39
Joined: Mon Nov 22, 2021 12:10 pm

Re: Set HDMI monitor as Default sound output

Post by 89123612 »

Hey guys, once again, thank you for taking the time to help, you both are so knowledgeable and helpful!

I'm sorry but I'm not sure I can really follow your last messages.

Andy yes, the script is executable, has the same "read and write" options in the Permissions tab as yours, and as far as I can tell it works.
I'll clarify that by this I mean that if I manually set the sound to the laptop speakers, and run the setsound script by CLICKING on it and choosing "Esegui" (Run I guess), then I can verify in the sound panel that the output has been effectively changed. I'll attach some photos of what I'm talking about.
https://ibb.co/XJHvXNn
https://ibb.co/wLVq4yL
https://ibb.co/4ZQvDYm

Likewise, if I go to terminal and I MANUALLY copy the script:

Code: Select all

#!bin/bash
if xrandr | grep -q "HDMI-1 connected"; then
   pacmd set-card-profile 0 output:hdmi-stereo
fi
with the same method I can determine that it worked because the output has been changed.

ON THE OTHER HAND, if I click on the same setsound file and I choose "Esegui nel terminale" (which I guess translates as Run or Execute in TERMINAL) it does NOT work, with that I mean that the sound output stays the same and I have no additional feedbacks from the system.
The same goes if in the terminal I insert the

Code: Select all

$ setsound
command; it does not work and as I said I get the

Code: Select all

/home/luca/bin/setsound: riga 1: #!bin/bash: File o directory non esistente 
error in the terminal.

So i guess that as you and SMG pointed out, it really has to do with our different PATH:

Code: Select all

andy@T432 ~ $ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/andy/.local/sbin:/home/andy/.local/bin

Code: Select all

luca@luca-HP-Pavilion-Gaming-Notebook:~$ echo $PATH
/home/luca/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

So, if that's really the issue, how can I add the /home/luca/bin to the beggining of PATH?

That's where I stop following you both guys. I've tried to check in my .profile and indeed I see the following

Code: Select all

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi
but I have no idea if I have to change it or else how to make sure my script indeed is on PATH. For sure it is located in home/luca/bin.

I've never thought it would be so difficult, more so I really appreciate your help guys, and have a nice evening (or day!)
User avatar
AndyMH
Level 21
Level 21
Posts: 13759
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: Set HDMI monitor as Default sound output

Post by AndyMH »

There is nothing wrong with your PATH, it is showing as the first item /home/luca/bin.

So if setsound in a terminal doesn't work, try using the full pathname /home/luca/bin/setsound.e.g.

Code: Select all

luca@luca-HP-Pavilion-Gaming-Notebook:~$ /home/luca/bin/setsound
If that doesn't work check that you have marked setsound as executable.
Screenshot from 2021-11-30 12-43-24.png
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
89123612
Level 1
Level 1
Posts: 39
Joined: Mon Nov 22, 2021 12:10 pm

Re: Set HDMI monitor as Default sound output

Post by 89123612 »

OK guys, here's an update.
I'm a little bit hesitant to say this since as I said the computer has proven to be a little bit unpredictable in switching the output at each reboot, but so far IT SEEMS to be now working!

I've triple checked everything and the script was already correctly placed, the file already marked as executable, but only working if run in certain conditions I've described above.
So here's what I've changed:
1) I deleted the old setsound
2) I created a new setsound file with exactly the same commands (I've just copied the text)
3) I tried to test it by running /home/luca/bin/setsound in terminal as per the last suggestion of AndyMH. I got the usual error:

Code: Select all

luca@luca-HP-Pavilion-Gaming-Notebook:~$ /home/luca/bin/setsound
/home/luca/bin/setsound: riga 1: #!bin/bash: File o directory non esistente
so I thought there was still something wrong
4) Nonetheless, I inserted the full pathname in the startup applications:
Image
I've tried to reboot some times and the sound output remains correct. I'll test and check it again for a few days to see if it sticks but I think that the full pathname hint may have done the trick.
I don't see why I would continue to get that error in the terminal but as long as it does not prevent the script from working, obviously, I couldn't care less. I've also noticed that the welcome sound of Mint after I enter the password is still coming from the laptop speakers, I don't know if it is something which isn't changeable or if it has to do with the startup delay of the script but, again, that's a note I make just out of pure curiosity.
In a few days ll duly report if things stay this way, if they do I will mark the thread as solved in which case I hope it will be useful to someone else in the future.
Meanwhile, and once again, many thanks to Andy and SMG for their great help. Have a nice day guys!
Last edited by SMG on Fri Dec 03, 2021 8:23 pm, edited 1 time in total.
Reason: Switched to other ibb link so image could be embedded in the post.
User avatar
AndyMH
Level 21
Level 21
Posts: 13759
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: Set HDMI monitor as Default sound output

Post by AndyMH »

I've also noticed that the welcome sound of Mint after I enter the password is still coming from the laptop speakers
That is because your startup applications do not run until after you have logged in and I also put a 30 second delay on the setsound script running just to make sure everything else was up and running.

Why you are getting that error message - no idea, but the script does seem to work.
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
Locked

Return to “Sound”