rbcnp
Posts: 5
Joined: Thu Sep 27, 2018 3:58 pm

pyAudio Select right input

Mon Oct 15, 2018 10:15 pm

Hello guys!

I'm trying to capture audio from an USB interface using Pyaudio library, I already choose the right device index but my USB Card have a Line Input and Mic input, where I select wich I want to use on my code?


I already tested this code on another USB Card that only have one stereo input and it worked just fine, but now I have two stereo inputs.

Code: Select all

import pyaudio
import numpy as np
import time
import sys

form_1 = pyaudio.paInt16 # 16-bit resolution
chans = 2 # 2 channel
samp_rate = 44100 # 44.1kHz sampling rate
chunk = 8192 # 2^12 samples for buffer
dev_index = 2 # device index found by p.get_device_info_by_index(ii)


audio = pyaudio.PyAudio() # create pyaudio instantiation

# create pyaudio stream
stream = audio.open(format = form_1,rate = samp_rate,channels = chans, \
                    input_device_index = dev_index,input = True, \
                    frames_per_buffer=chunk)

# record data chunk
stream.start_stream()
data = np.fromstring(stream.read(chunk),dtype=np.int16)
stream.stop_stream

chunk_length = len(data) / chans
assert chunk_length == int(chunk_length)


#for k in range(8192):
#    print(data[k])



data = np.reshape(data, (chunk_length, chans))

for i in range (len(data)):
    for j in range (chans):
        sys.stdout.write("%d " % data[i][j])
    print(" ")

Attachments
Capturar.JPG
Capturar.JPG (72.05 KiB) Viewed 11670 times

User avatar
MrYsLab
Posts: 449
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: pyAudio Select right input

Wed Oct 17, 2018 9:30 pm

I found the code below here: https://stackoverflow.com/questions/368 ... th-pyaudio.
Perhaps it will help.

Code: Select all

import pyaudio

p = pyaudio.PyAudio()
info = p.get_host_api_info_by_index(0)
numdevices = info.get('deviceCount')
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'))

rbcnp
Posts: 5
Joined: Thu Sep 27, 2018 3:58 pm

Re: pyAudio Select right input

Sat Oct 20, 2018 9:33 pm

The code returned this info, but i really don't know how to use this information

Code: Select all

('Input Device id ', 2, ' - ', u'USB Sound Device: Audio (hw:1,0)')
('Input Device id ', 3, ' - ', u'sysdefault')
('Input Device id ', 11, ' - ', u'spdif')
('Input Device id ', 12, ' - ', u'default')

Return to “Python”