Integrating External Microphone with Python
Hi all! Since I had been working on Audio Analytics and recognition for some time now, I would like to share some basics about integration of external microphone arrays (usually USB) with your computer.
You have a lot if options to work with but the library I used was pyAudio. You can install it with the following command:
$ sudo apt-get install python-pyaudio
Know your microphone array!
The inputs from microphones are digitally quantized by an analog to digital converters. Know the resolution of your ADC. In my case it was an 8-bit ADC.
Getting Started with your code
- Importing libraries, wave for saving the audio if you want to:
2. Defining the basic parameters based of the characteristics of your microphone and your ADC:
CHUNK = 256 #ADC information [8 bit = 2⁸ steps]
FORMAT = pyaudio.paInt16 # 16 bit encoding
CHANNELS = 2
RATE = 44100 #sample rate
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = “output.wav”
3. Define an audio object and an input stream for the same
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
4. Read the input clip for a specified duration of time
frames = 
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data) # 2 bytes(16 bits) per channel
print(“* done recording”)
5. Close/ Stop the stream
6. Save the clip
wf = wave.open(WAVE_OUTPUT_FILENAME, ‘wb’)
Furthering the Code
This brings us to the end of the basics about the interfacing. But you can search about other aspects and extensions regarding audio processing. A demo neural network is available in tensorflow on the official website wherein you can have a model for speech recognition and accordingly train various keywords to trigger specific actions. You can use this code to break the input in the intervals of 5 seconds and have analysis on that under a while loop which keeps on running until the end of the clip or till the termination of the live source.
Until next time!