user5804127
user5804127

Reputation: 49

Selecting audio device for playback via Python

How to get a list of audio devices in linux (like aplay -L) and select audio device for the output? are there any libs to import? Are they already imported?

Upvotes: 2

Views: 2255

Answers (1)

Rolf of Saxony
Rolf of Saxony

Reputation: 22458

I searched high and low for what you are asking for when writing a sound equaliser and never found a satisfactory answer.
There is an alsaaudio library available:

import alsaaudio   #alternative method pure python
for device_number, card_name in enumerate(alsaaudio.cards()):
    print device_number, card_name

I ended up banging pacmd and cat options onto the command line, retrieving and parsing the results to discover what was what.
Examples:

    sink_list = os.popen('cat /proc/asound/cards | grep "\[" | cut -c2').readlines()
    for s in sink_list:
        s = s.strip()
        self.sinks_available.append("hw:"+s)

for pulseaudio you need pacmd

# Identify the default sound card device
    sink = os.popen('pacmd list | grep "device.master_device" | cut --delimiter== -f2').readline()
    if sink =="":
        sink = os.popen('pacmd list-cards | grep -C1 sinks | grep pci | cut --delimiter=/ -f1').readline()

It's all most unsatisfactory and un-python like but needs must when the devil drives

Upvotes: 1

Related Questions