The pyAudio.PyAudio().open() function takes an argument known as "input_device_index", in which, if I gave a certain index number that represents my desired microphone, it would use it. Furthermore, another argument known as "Input" must be "True" as well.
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 48000
RECORD_SECONDS = 2
WAVE_OUTPUT_FILENAME = "output.wav"
INPUT_DEVICE_INDEX = 3
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
input_device_index= INPUT_DEVICE_INDEX,
frames_per_buffer=CHUNK)
And I identified the index of my desired device by using:
get_device_info_by_index(i)
in which i is an integer that represents a particular index number. For this case, my 'i' is 3. Which outputs:
{'defaultHighInputLatency': 0.1,
'defaultHighOutputLatency': 0.0126875,
'defaultLowInputLatency': 0.01,
'defaultLowOutputLatency': 0.0033541666666666668,
'defaultSampleRate': 48000.0,
'hostApi': 0,
'index': 3,
'maxInputChannels': 2,
'maxOutputChannels': 2,
'name': 'Scarlett 2i2 USB',
'structVersion': 2}
However, after inputting "input_device_index" in the open() function and running the audio record function, I've encountered an error:
File "<ipython-input-145-166f13e76ff1>", line 1, in <module>
record()
File "/Users/aaron.yong/python_main/audio_test_v5.py", line 56, in record
frames_per_buffer=CHUNK)
File "/anaconda/lib/python3.6/site-packages/pyaudio.py", line 750, in open
stream = Stream(self, *args, **kwargs)
File "/anaconda/lib/python3.6/site-packages/pyaudio.py", line 441, in __init__
self._stream = pa.open(**arguments)
OSError: [Errno -9986] Internal PortAudio error