Announcement

Collapse
No announcement yet.

How to switch audio output device with CLI????

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    [KDE] How to switch audio output device with CLI????

    My audio is supposed to be routed through my monitor HDMI. But every time my monitor shuts off for any reason, audio switches to my laptop speakers and I have to manually switch it back via Kubuntu GUI. This is tiresome.

    I would like to know how to do it through the CLI?


    I have tried using pactl and pacmd which have been utterly unsuccessful owing to them only recognizing the name or index of the currently used "sink". I can't even use the index because every time I switch the audio, the index increments by 2.

    Here are some examples:
    if I type
    $ pactl get-default-sink

    depending on which device is active I get either
    alsa_output.pci-0000_00_1f.3.analog-stereo
    or
    alsa_output.pci-0000_00_1f.3.hdmi-stereo



    This is the result of me trying to change from analog to HDMI

    $: pactl set-default-sink alsa_output.pci-0000_00_1f.3.hdmi-stereo
    $: Failure: No such entity


    It doesn't seem to matter what arguments are provided. Nothing works. Names, indexes, doesn't matter.

    Obviously there's a way to switch because the GUI does it. But how do I do it via the CLI?




    #2
    See https://askubuntu.com/questions/1407...ne/14083#14083
    Windows no longer obstructs my view.
    Using Kubuntu Linux since March 23, 2007.
    "It is a capital mistake to theorize before one has data." - Sherlock Holmes

    Comment


      #3
      That's how it's SUPPOSED to work. That's not how it does work.

      When I list out the sinks, it shows the active sink only. Any name I pass to set-default-sink other than the active sink generates an error message.

      When the laptop speaker is active:

      $ pacmd list-sinks | grep -e 'name:' -e 'index:'
      * index: 2
      name: <alsa_output.pci-0000_00_1f.3.analog-stereo>


      After I swtich to HDMI via GUI:


      $ pacmd list-sinks | grep -e 'name:' -e 'index:'
      * index: 4
      name: <alsa_output.pci-0000_00_1f.3.hdmi-stereo>


      Try to switch back by CLI


      $ pacmd set-default-sink alsa_output.pci-0000_00_1f.3.analog-stereo
      Sink alsa_output.pci-0000_00_1f.3.analog-stereo does not exist.



      Editing /etc/pulse/default.pa so that: load-module module-stream-restore restore_device=false​ and restarting pulseaudio has no effect.
      Last edited by TwoFistedJustice; Aug 16, 2023, 10:48 AM.

      Comment


        #4
        Maybe pulseaudio isn't actually controlling which device is used but is being controlled from somewhere else?


        This is dialog I used to change the device. Where does it keep its configuration and set the device?


        KDE Devices

        Comment


          #5
          When I was having sound issues I found the definitive site for accurate information was the arch linux write up. Now that may be less true if you have an up to date version of Kubuntu which uses Pipewire in place of Pulse Audio (since 22.10).

          Comment


            #6
            Originally posted by shag00 View Post
            When I was having sound issues I found the definitive site for accurate information was the arch linux write up. Now that may be less true if you have an up to date version of Kubuntu which uses Pipewire in place of Pulse Audio (since 22.10).
            I just checked an 22.04 is running pipewire as a process. But I can't make heads or tails of it in relation to my issue. Note, my issue is not a "sound" issue, but a speaker selection issue.

            Comment


              #7
              Are you saying you use your speakers for something other than sound? Unfortunately, I have not fiddled with my sound setup since pipewire was introduced and I am not sure how relevant the Puse Audio link is as the main files they reference, /etc/pulse/default.pa, ~/.config/pulse/client.conf etc no longer contain data or exist in 23.04. I had exactly the same type of issue you had and the it was fixable using Pulse and I assume it is likely fixable in Pipewire. What you are looking to do is turn off the auto sink selection and then force the sink you want.

              The current thinking is auto this and auto that is a step forward and much better than forcing the user to select what he wants to use. Mostly works well until it's deployed to a million users that want to use it the way they want to lol.

              Comment


                #8
                Originally posted by shag00 View Post
                Are you saying you use your speakers for something other than sound?
                I'm saying I have more than one set of speakers and I want to be able to switch between them using a terminal command and that the way that used to be the standard method (pacmd) no longer works. I think it's being overridden by Plasma since I can do it through sytem-settings but not through the terminal (yet).

                Comment


                  #9
                  I have a partial solution. It won't let me switch using a keyboard command. But I figured out how to set it up so the sound will switch back to HDMI output automatically when the monitor comes back on.

                  The directory ~/.config/pulse contains the default settings for pulseaudio.

                  It contains:


                  total 160
                  -rw------- 1 1000 1000 45056 Aug 17 19:19 7c628c8754e54d6aa7ea8794d35ef7e7-card-database.tdb
                  -rw------- 1 1000 1000 41 Aug 17 19:52 7c628c8754e54d6aa7ea8794d35ef7e7-default-sink
                  -rw------- 1 1000 1000 43 Aug 17 19:36 7c628c8754e54d6aa7ea8794d35ef7e7-default-sink.BAK
                  -rw------- 1 1000 1000 1 Aug 17 19:52 7c628c8754e54d6aa7ea8794d35ef7e7-default-source
                  -rw------- 1 1000 1000 12288 Aug 17 19:42 7c628c8754e54d6aa7ea8794d35ef7e7-device-volumes.tdb
                  -rw------- 1 1000 1000 12288 Jul 28 14:28 7c628c8754e54d6aa7ea8794d35ef7e7-stream-volumes.tdb
                  -rw------- 1 1000 1000 45056 Feb 2 2023 9c655962c40145bf86eb0971e71d229a-card-database.tdb
                  -rw------- 1 1000 1000 39 Apr 14 19:20 9c655962c40145bf86eb0971e71d229a-default-sink
                  -rw------- 1 1000 1000 1 Apr 14 19:20 9c655962c40145bf86eb0971e71d229a-default-source
                  -rw------- 1 1000 1000 12288 Apr 13 20:09 9c655962c40145bf86eb0971e71d229a-device-volumes.tdb
                  -rw------- 1 1000 1000 12288 Jan 10 2023 9c655962c40145bf86eb0971e71d229a-stream-volumes.tdb
                  -rw------- 1 1000 1000 256 Jun 9 2021 cookie




                  The file 7c628c8754e54d6aa7ea8794d35ef7e7-default-sink contained: alsa_output.pci-0000_00_1f.3.analog-stereo

                  I changed it to contain: alsa_output.pci-0000_00_1f.3.hdmi-stereo

                  Now when my monitor comes back on, the sound switches away from my laptop speakers and back to my not-crappy speakers.

                  Comment


                    #10

                    Everything below the line is moot. It doesn't work reliably.



                    My "fix" in the previous post did not work after I set up my system anew.


                    I got it working again by following this page from the docs ( which was actually linked from Snowhog's forum link above, but which I failed to notice before. )

                    https://www.freedesktop.org/wiki/Sof...DefaultDevice/



                    The gist of it is:
                    Modify /etc/pulse/default.pa
                    By adding:

                    26: load-module module-stream-restore restore_device=false
                    148: set-default-sink alsa_output.pci-0000_00_1f.3.hdmi-stereo


                    I got my sink name by setting the sound output to the speaker I wanted and then ran

                    $ pactl get-default-sink


                    It works if I turn my monitor off and then on again right away and also after the monitor times out.
                    Last edited by TwoFistedJustice; Sep 02, 2023, 06:35 PM.

                    Comment


                      #11
                      Not what you are asking but if you have pavuctl installed you can disable the laptop speakers in the configuration tab and that will likely stop the default device switching.

                      Comment


                        #12
                        Originally posted by Bings View Post
                        Not what you are asking but if you have pavuctl installed you can disable the laptop speakers in the configuration tab and that will likely stop the default device switching.
                        I just installed it. It looks promising.

                        Comment

                        Working...
                        X