Microphone for capturing live audio and AudioFile for reading audio from files.
AudioSource Base Class
All audio sources inherit from theAudioSource abstract base class. Audio sources are designed to be used as context managers, which ensures proper resource management.
Microphone Class
TheMicrophone class represents a physical microphone on your computer and allows you to capture live audio input.
Requirements
Basic Usage
Constructor Parameters
The index of the audio device to use. If
None, the default microphone is used. The device index should be an integer between 0 and pyaudio.get_device_count() - 1.The sample rate in Hertz (samples per second). If
None, the sample rate is automatically determined from the microphone’s default settings.Higher sample rates result in better audio quality but require more bandwidth. Some devices, like older Raspberry Pi models, may not handle high sample rates well.The number of audio samples to read at a time. Higher values help avoid triggering on rapidly changing ambient noise but make detection less sensitive. This value should generally be left at its default.
Selecting a Specific Microphone
You can list available microphones and select a specific one by its device index:Finding Working Microphones
Thelist_working_microphones() static method helps identify which microphones are currently active and receiving audio:
Custom Sample Rate
For specific audio quality requirements, you can set a custom sample rate:AudioFile Class
TheAudioFile class allows you to read audio data from WAV, AIFF, or FLAC files.
Supported Formats
- WAV: PCM/LPCM format only. WAVE_FORMAT_EXTENSIBLE and compressed WAV are not supported.
- AIFF: Both AIFF and AIFF-C (compressed AIFF) formats are supported.
- FLAC: Native FLAC format only. OGG-FLAC is not supported.
Basic Usage
Constructor Parameters
Either a string path to an audio file, or a file-like object (such as
io.BytesIO) containing audio data.Reading Specific Portions
Therecord() method allows you to read specific portions of an audio file:
Sequential Reading
Audio reading operations advance through the stream sequentially. Each call to
record() or listen() continues from where the previous call left off. Re-entering the context manager resets the position to the beginning.Using File-Like Objects
You can use file-like objects instead of file paths:Different File Formats
Context Manager Pattern
BothMicrophone and AudioFile implement the context manager protocol, which means they must be used within a with statement. This ensures proper resource cleanup:
Audio Source Properties
Once an audio source is active (inside awith block), it exposes several properties:
SAMPLE_RATE: The sample rate in HertzSAMPLE_WIDTH: The width of each sample in bytesCHUNK: The number of frames in each bufferstream: The underlying audio stream object
AudioFile sources, additional properties are available:
DURATION: The total duration of the audio file in secondsFRAME_COUNT: The total number of audio frames in the file