Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mansam
Created March 3, 2014 19:13
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mansam/9332445 to your computer and use it in GitHub Desktop.
Save mansam/9332445 to your computer and use it in GitHub Desktop.
Audio device detection w/ pyaudio
# lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp.
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.
Bus 001 Device 006: ID 05e3:0606 Genesys Logic, Inc. USB 2.0 Hub / D-Link DUB-H4 USB 2.0 Hub
Bus 001 Device 009: ID 1130:f211 Tenx Technology, Inc. TP6911 Audio Headset
# cat d.py
import pyaudio
p = pyaudio.PyAudio()
info = p.get_host_api_info_by_index(0)
numdevices = info.get('deviceCount')
#for each audio device, determine if is an input or an output and add it to the appropriate list and dictionary
for i in range (0,numdevices):
if p.get_device_info_by_host_api_device_index(0,i).get('maxInputChannels')>0:
print "Input Device id ", i, " - ", p.get_device_info_by_host_api_device_index(0,i).get('name')
if p.get_device_info_by_host_api_device_index(0,i).get('maxOutputChannels')>0:
print "Output Device id ", i, " - ", p.get_device_info_by_host_api_device_index(0,i).get('name')
devinfo = p.get_device_info_by_index(1)
print "Selected device is ",devinfo.get('name')
if p.is_format_supported(44100.0, # Sample rate
input_device=devinfo["index"],
input_channels=devinfo['maxInputChannels'],
input_format=pyaudio.paInt16):
print 'Yay!'
p.terminate()
@KhanWarden
Copy link

invalid syntax here:
print "Input Device id ", i, " - ", p.get_device_info_by_host_api_device_index(0,i).get('name')

@mansam
Copy link
Author

mansam commented Aug 30, 2021

Which version of Python are you using? This is old Python 2 code, it will throw a syntax error on that print statement under Python 3.

@KhanWarden
Copy link

KhanWarden commented Aug 31, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment