0

I need to write shell script that will send a Google Chrome instance to a specified monitor and also route the associated sound there.

I can make the instance appear on the right monitor by passing the appropriate --window-position, and I can identify the positions of monitors with xrandr --listactivemonitors. But, how do I go from that to the ALSA device name?

logiclrd@visor:~$ xrandr --listactivemonitors
Monitors: 3
 0: +*eDP 2560/345x1600/215+1920+60  eDP
 1: +DisplayPort-1 1920/160x1080/90+4480+0  DisplayPort-1
 2: +DisplayPort-8 1920/535x1080/339+0+0  DisplayPort-8
logiclrd@visor:~$ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: Generic [HD-Audio Generic], device 3: HDMI 0 [BenQ BL2581T]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: Generic [HD-Audio Generic], device 7: HDMI 1 [HDMI 1]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: Generic [HD-Audio Generic], device 8: HDMI 2 [SAMSUNG]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: Generic_1 [HD-Audio Generic], device 0: ALC295 Analog [ALC295 Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 2: Dock [WD15 Dock], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 2: Dock [WD15 Dock], device 1: USB Audio [USB Audio #1]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
logiclrd@visor:~$ 

I see nothing in common :-(

(Is there a different way to tell Chrome where to send its sound (other than manually using a GUI tool to reconfigure the output -- this needs to be automated)?)

1 Answers1

1

I'm not sure that the system can guess how to map screens and ALSA devices. Ideally you would need to find how to map the PID of your program with the appropriate PulseAudio device (note that if you use PipeWire, pactl and pacmd stuff still applies). Note that depending on the X server that you use, various utilities could be helpful to retrieve the position of a running application:

1. Get the ID or the node.name of the target device: According to AU: Trying to change PulseAudio application sink/source from command line this is obtained using pactl list source-outputs but in my case this does not print anything. However pactl list sinks prints the device IDs.

Example: In the example below, #54 corresponds the laptop speakers and #96 is a headset.

$ pactl list sinks | egrep -i "destination|description|node.name"
Destination #54
        Description : Audio interne Stéréo analogique
                device.profile.description = "Stéréo analogique"
                device.description = "Audio interne"
                node.name = "alsa_output.pci-0000_00_1f.3.analog-stereo"
Destination #96
        Description : OpenMove by Shokz
                device.description = "OpenMove by Shokz"
                node.name = "bluez_output.A8_F5_E1_B4_41_38.1"

Note: In step 3, you can get the valid device node names using auto-completion (tab key).

2. Get the destination ID of your application: This step seems to depend on your version of pactl. As explained in AU: Trying to change PulseAudio application sink/source from command line, you get it using pactl list sink-inputs.

Example: Assume you're playing a video in Chromium. In this example the corresponding ID is #448:

$ pactl list sink-inputs | egrep -i "destination|application.name ="
Entrée de la destination #70
        Destination : 96
        Latence de la destination : 0 usec
                application.name = "QtPulseAudio:1279"
Entrée de la destination #79
        Destination : 96
        Latence de la destination : 0 usec
                application.name = "QtPulseAudio:1640"
Entrée de la destination #448
        Destination : 54
        Latence de la destination : 0 usec
                application.name = "Chromium"

Note: In step 3, you can get the application IDs using auto-completion (tab key).

3. Remap the application ID to the right device ID: Use pactl move-sink-input as follows.

Example: Using the previous IDs, assume Chromium is playing sounds using the laptop speakers. To redirect the sound to the headset:

pactl move-sink-input 448 96

...or equivalently:

pactl move-sink-input 448 bluez_output.A8_F5_E1_B4_41_38.1

Example: Using the previous IDs, assume Chromium is playing sounds using the headset. To redirect the sound to the laptop speakers:

pactl move-sink-input 448 54

...or equivalently:

pactl move-sink-input 448 alsa_output.pci-0000_00_1f.3.analog-stereo