Microphone class provides access to your computer’s audio input devices for real-time speech recognition. This guide covers device selection, configuration, and best practices for working with microphone input.
Basic Usage
The simplest way to capture audio from your default microphone:Device Selection
Listing Available Microphones
Uselist_microphone_names() to discover all available audio input devices:
Using a Specific Device
Select a microphone by its index:Device indices range from 0 to
pyaudio.get_device_count() - 1. If you specify an invalid index, you’ll get an assertion error with the valid range.Finding Working Microphones
Thelist_working_microphones() method identifies microphones that are actively receiving audio:
Microphone Configuration
Sample Rate
The sample rate controls audio quality and processing speed:Higher sample rates (e.g., 44100 Hz) provide better audio quality but require more processing power. Some devices like Raspberry Pi may struggle with high sample rates. The default auto-detection usually provides the best balance.
Chunk Size
Chunk size affects noise sensitivity and detection responsiveness:The default chunk size of 1024 works well for most applications. Only adjust if you’re experiencing issues with ambient noise triggering false positives.
Real-Time Recognition
Complete Example
Here’s a complete microphone recognition example from the library:Troubleshooting
PyAudio installation errors
PyAudio installation errors
The
Microphone class requires PyAudio. If you get an AttributeError: Could not find PyAudio, install it:No audio detected
No audio detected
If
listen() never returns or times out:- Check that your microphone is not muted
- Verify the correct device index is selected
- Test with
list_working_microphones()while making noise - Adjust the energy threshold:
Device index out of range
Device index out of range
If you get a device index error:
Audio cutting off too early
Audio cutting off too early
If speech is being cut off prematurely:
Poor recognition quality
Poor recognition quality
If recognition accuracy is low:
- Use
adjust_for_ambient_noise()before each listening session - Position the microphone closer to the speaker
- Use a higher-quality microphone
- Increase the sample rate if your system can handle it:
Continuous listening in the background
Continuous listening in the background
For applications that need to listen continuously, see the Background Listening guide.
API Reference
Microphone Class
list_microphone_names()- Returns list of all microphone nameslist_working_microphones()- Returns dict mapping indices to names of active microphones
Microphone class must be used as a context manager (with with statement) to properly open and close the audio stream.
Attributes (after entering context):
SAMPLE_RATE- Sample rate in HzSAMPLE_WIDTH- Sample width in bytesCHUNK- Chunk size in frames
See Also
- Ambient Noise Calibration - Improve accuracy in noisy environments
- Background Listening - Continuous recognition in a background thread
- Language Support - Recognize speech in different languages