2

I need to get a static output from pw-top, of pipewire, for a script I want to make. I really just need a list of pipewire devices.

For example, I want to be able to run pw-top | grep output and then do stuff to that. Basically, I want to get a list pf pipewire devices and their IDs.

Sort of output I want, for speakers and headphones only:

<device name> <id>
<device name> <id>
<device name> <id>

etc

I tried the following:

[aggam@aggam-archlinux ~]$ pactl list cards | grep -E 'port.type|Name|Card'
Card #39
    Name: alsa_card.pci-0000_01_00.1
                port.type = "hdmi"
                port.type = "hdmi"
                port.type = "hdmi"
                port.type = "hdmi"
                port.type = "hdmi"
                port.type = "hdmi"
Card #40
    Name: alsa_card.usb-0c76_USB_PnP_Audio_Device-00
                port.type = "mic"
Card #41
    Name: alsa_card.pci-0000_00_1f.3
                port.type = "mic"
                port.type = "mic"
                port.type = "line"
                port.type = "line"
                port.type = "headphones"
                port.type = "spdif"

But

  1. some show up as speakers and mics? I assume the headphones are both...
  2. those are pulseaudio devices, and I want to avoid issues, so I want their pipewire device ID

EDIT 2: my pw-top:

S   ID  QUANT   RATE    WAIT    BUSY   W/Q   B/Q  ERR FORMAT           NAME
S   28      0      0    ---     ---   ---   ---     0                  Dummy-Driver
S   29      0      0    ---     ---   ---   ---     0                  Freewheel-Driver
S   36      0      0    ---     ---   ---   ---     0                  Midi-Bridge
S   42      0      0    ---     ---   ---   ---     0                  alsa_output.pci-0000_01_00.1.hdmi-stereo-extra3
S   43      0      0    ---     ---   ---   ---     0                  alsa_input.usb-0c76_USB_PnP_Audio_Device-00.mono-fallback
S   44      0      0    ---     ---   ---   ---     0                  alsa_output.pci-0000_00_1f.3.analog-stereo
S   45      0      0    ---     ---   ---   ---     0                  alsa_input.pci-0000_00_1f.3.analog-stereo
S   71      0      0    ---     ---   ---   ---     0                  alsa_output.usb-KURZWEIL_MPS-Piano-00.analog-stereo
S   69      0      0    ---     ---   ---   ---     0                  alsa_input.usb-KURZWEIL_MPS-Piano-00.analog-stereo
I   67      0      0   0.0us   0.0us  0.00  0.00    0    F32LE 2 48000 Firefox

notice how the piano output is 71. output of pactl list short sinks is

42  alsa_output.pci-0000_01_00.1.hdmi-stereo-extra3 PipeWire    s32le 2ch 48000Hz   SUSPENDED
44  alsa_output.pci-0000_00_1f.3.analog-stereo  PipeWire    s32le 2ch 48000Hz   SUSPENDED
401 alsa_output.usb-KURZWEIL_MPS-Piano-00.analog-stereo PipeWire    s16le 2ch 48000Hz   SUSPENDED

notice how the piano is 401 all of a sudden...

sef sf
  • 72
  • 7
  • Please [edit] your question and tell us exactly what information you want to extract. Since `pw-top` is designed to be used as a live monitor, it is almost certainly not the best way for you to get whatever info you need in a script. If you explain what you need, we should be able to give you another way to get it. – terdon Dec 21 '22 at 11:14
  • @terdon I want to get a list pf pipewire devices and their IDs – sef sf Dec 21 '22 at 11:32
  • `pactl list cards` – Artem S. Tashkinov Dec 21 '22 at 11:34
  • Yes, please ***EDIT*** your question and tell us what you need there because comments are easy to miss, hard to read and can be deleted without warning. Also, we need to see exactly what you need. What is the output of `pw-top` that you would like to have? We need to know exactly what you need to be able to give you a good alternative. – terdon Dec 21 '22 at 11:37
  • @ArtemS.Tashkinov yeah but I want to get speakers/headphones, and it's gonna be annoying to format. pw-top's format is really convenient... is there an alternative? – sef sf Dec 21 '22 at 11:38
  • If you show us what you need, we will show you how to get it, but we cannot help you if you don't show what format you actually want to have. Show us what you would like to get from `pw-top | grep output` and how you would like to use it and we can help you get it. But you won't be able to use pw-top since it is not designed to be parseable. – terdon Dec 21 '22 at 11:44
  • @terdon I want a list of output devices, with their names and IDs, so I can switch to them. I want to make a dmenu for switching audio devices. – sef sf Dec 21 '22 at 11:50
  • So you just need something like this: `pactl list cards | awk '(/^Card/){sub(/#/,"",$2); id=$2} ($1~/^Name:/){print id,$2}`? Ah, just saw your edit, that's perfect thank you! Someone more knowledgeable than I am should be able to help now! – terdon Dec 21 '22 at 11:58
  • or just `pactl list short cards`? or `pactl list short sinks`? – terdon Dec 21 '22 at 12:00
  • @terdon `pactl list short sinks` is the closest one to what I'm looking for, since it actually has pipewire ID's (same ones that show up on pw-top), but for one of my devices the ID is different for some reason... – sef sf Dec 21 '22 at 12:02
  • Great, one step closer then! OK, please add the output you get to your question and show the output you would want (preferably by showing what you see in `pw-top` so we can compare). Also have a look at `man pactl` in case that helps. – terdon Dec 21 '22 at 12:05
  • @terdon I made an edit. also, thank you so much for your help and patience! – sef sf Dec 21 '22 at 12:21

1 Answers1

0

I figured it out! I ended up not using a shell command, rather I made an entire python script. The script gets all devices, makes a dictionary of them and then lets you use dmenu/bemenu to select which one you want.

the script can be found here on my github, but the code for just that part is:

sp = subprocess.run(['wpctl', 'status'], stdout=subprocess.PIPE).stdout.decode('utf-8')\
        .split("Sinks")[1].split('├─')[0]
newsp = []

for line in sp.split('\n'):
    if '.' in line:
        newsp.append(line.replace(' ', '')[1:])

devices = {}  # name: id
for line in newsp:
    line = line.split('.')
    devices[line[1].split('[vol')[0]] = line[0].replace('*', '')
sef sf
  • 72
  • 7