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
My apologies, after restarting my compiler, the code works after all. I've pasted a snippet of pyaudio.py's init function which shows how it works.
Thanks!
def __init__(self,
PA_manager,
rate,
channels,
format,
input=False,
output=False,
input_device_index=None,
output_device_index=None,
frames_per_buffer=1024,
start=True,
input_host_api_specific_stream_info=None,
output_host_api_specific_stream_info=None,
stream_callback=None):
"""
Initialize a stream; this should be called by
:py:func:`PyAudio.open`. A stream can either be input, output,
or both.
:param PA_manager: A reference to the managing :py:class:`PyAudio`
instance
:param rate: Sampling rate
:param channels: Number of channels
:param format: Sampling size and format. See |PaSampleFormat|.
:param input: Specifies whether this is an input stream.
Defaults to ``False``.
:param output: Specifies whether this is an output stream.
Defaults to ``False``.
:param input_device_index: Index of Input Device to use.
Unspecified (or ``None``) uses default device.
Ignored if `input` is ``False``.
:param output_device_index:
Index of Output Device to use.
Unspecified (or ``None``) uses the default device.
Ignored if `output` is ``False``.
:param frames_per_buffer: Specifies the number of frames per buffer.
:param start: Start the stream running immediately.
Defaults to ``True``. In general, there is no reason to set
this to ``False``.
:param input_host_api_specific_stream_info: Specifies a host API
specific stream information data structure for input.