import microphone
The microphone module measures sound level and captures short signed 16-bit mono audio buffers. Use ``Analog`` for ADC microphone amplifier boards such as MAX4466, MAX9814, and analog LM358 modules. Use ``I2S`` for standard digital I2S microphones such as INMP441 and ICS-43434. For I2S microphones, the three required pins are the GPIO numbers you choose on your board. Match the microphone board labels to the constructor names: SCK/BCLK/BCK goes to ``bclk``, WS/LRCLK/LRC goes to ``ws``, and SD/DOUT/DATA goes to ``data``. ``dbfs()`` reports electrical signal level relative to digital full scale. It is useful for comparing quiet and loud sounds on the same board, but it is not calibrated sound-pressure level. ``db_spl()`` returns None until the microphone has been explicitly calibrated with ``calibrate_spl()``.
Quick example
import microphone
import system
mic = microphone.Analog(pin=34)
mic.begin()
def show_sound():
print({
"level": mic.level(window_ms=100),
"peak": mic.peak(window_ms=100),
"dbfs": mic.dbfs(window_ms=100),
})
system.schedule("sound", 250, 0, -1, show_sound)
import microphone
mic = microphone.I2S(
bclk=14,
ws=15,
data=32,
sample_rate=16000,
channel=microphone.CHANNEL_LEFT,
)
mic.begin()
audio = mic.read(samples=512, timeout_ms=1000)
if audio != None:
print(audio.count, audio.rms(), audio.dbfs())
importmicrophone
Generic analog and I2S microphone module.
The microphone module measures sound level and captures short signed 16-bit mono audio buffers. Use ``Analog`` for ADC microphone amplifier boards such as MAX4466, MAX9814, and analog LM358 modules. Use ``I2S`` for standard digital I2S microphones such as INMP441 and ICS-43434. For I2S microphones, the three required pins are the GPIO numbers you choose on your board. Match the microphone board labels to the constructor names: SCK/BCLK/BCK goes to ``bclk``, WS/LRCLK/LRC goes to ``ws``, and SD/DOUT/DATA goes to ``data``. ``dbfs()`` reports electrical signal level relative to digital full scale. It is useful for comparing quiet and loud sounds on the same board, but it is not calibrated sound-pressure level. ``db_spl()`` returns None until the microphone has been explicitly calibrated with ``calibrate_spl()``.
microphone.Analog(pin: int, sample_rate: int = None, attenuation: int = None, center: any = None, resolution: int = None) -> microphone_device
microphone.Analog(pin: int, sample_rate: int = None, attenuation: int = None, center: any = None, resolution: int = None) -> microphone_deviceCreate an analog microphone object using an ESP32 ADC input.
Analog microphone boards must output a biased analog waveform. Boards that expose only a digital threshold output cannot provide audio samples. Wire VCC to 3V3 or the voltage required by the board, GND to GND, and the board's OUT/AO/SIG pin to the ADC GPIO passed as ``pin``.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
pin |
int |
positional or keyword | Yes | Required ADC-capable GPIO connected to the microphone analog output pin labeled OUT, AO, or SIG. On ESP32-class boards, ADC2 pins can time out while WiFi is active. Prefer ADC1 pins for scripts that also use WiFi. |
sample_rate |
int |
positional or keyword | No | Optional sample rate in samples per second. Defaults to 8000. |
attenuation |
int |
positional or keyword | No | Optional ADC attenuation in dB. Use 0, 2, 6, or 12. Defaults to 11, which maps to the firmware's high-range ADC attenuation. |
center |
any |
positional or keyword | No | Optional DC midpoint. Use "auto" for automatic estimation or pass a raw ADC value such as 2048. Defaults to "auto". |
resolution |
int |
positional or keyword | No | Optional output sample resolution. The current firmware returns signed 16-bit samples. |
microphone_device microphone_device object.
microphone.Analog().begin() -> bool
microphone.Analog().begin() -> boolStart acquisition and allocate hardware resources.
microphone.Analog().stop() -> bool
microphone.Analog().stop() -> boolStop acquisition and release ADC/I2S resources.
microphone.Analog().is_running() -> bool
microphone.Analog().is_running() -> boolReturn True when the microphone is active.
microphone.Analog().read(samples=256, timeout_ms=100) -> audio_buffer
microphone.Analog().read(samples=256, timeout_ms=100) -> audio_bufferCapture signed 16-bit mono samples, or None when no audio is available.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
samples |
int |
positional or keyword | No | Optional value. Defaults to 256. |
timeout_ms |
int |
positional or keyword | No | Optional value. Defaults to 100. |
microphone.Analog().level(window_ms=50) -> float
microphone.Analog().level(window_ms=50) -> floatReturn normalized RMS level from 0.0 through 1.0.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
window_ms |
int |
positional or keyword | No | Optional value. Defaults to 50. |
microphone.Analog().rms(window_ms=50) -> float
microphone.Analog().rms(window_ms=50) -> floatReturn normalized RMS amplitude.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
window_ms |
int |
positional or keyword | No | Optional value. Defaults to 50. |
microphone.Analog().peak(window_ms=50) -> float
microphone.Analog().peak(window_ms=50) -> floatReturn normalized peak amplitude.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
window_ms |
int |
positional or keyword | No | Optional value. Defaults to 50. |
microphone.Analog().peak_to_peak(window_ms=50) -> float
microphone.Analog().peak_to_peak(window_ms=50) -> floatReturn normalized max-min movement.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
window_ms |
int |
positional or keyword | No | Optional value. Defaults to 50. |
microphone.Analog().dbfs(window_ms=50) -> float
microphone.Analog().dbfs(window_ms=50) -> floatReturn RMS level in dBFS. This is not dB SPL.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
window_ms |
int |
positional or keyword | No | Optional value. Defaults to 50. |
microphone.Analog().db_spl(window_ms=125) -> float
microphone.Analog().db_spl(window_ms=125) -> floatReturn estimated SPL after calibrate_spl(), otherwise None.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
window_ms |
int |
positional or keyword | No | Optional value. Defaults to 125. |
microphone.Analog().is_loud(threshold_dbfs=-20, window_ms=50) -> bool
microphone.Analog().is_loud(threshold_dbfs=-20, window_ms=50) -> boolReturn True when dBFS crosses the threshold.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
threshold_dbfs |
float |
positional or keyword | No | Optional value. Defaults to -20. |
window_ms |
int |
positional or keyword | No | Optional value. Defaults to 50. |
microphone.Analog().configure(gain_db=0, dc_block=True, noise_floor_dbfs=-120, smoothing=0, clip_detection=True) -> bool
microphone.Analog().configure(gain_db=0, dc_block=True, noise_floor_dbfs=-120, smoothing=0, clip_detection=True) -> boolChange processing behavior without recreating the microphone.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
gain_db |
float |
positional or keyword | No | Optional value. Defaults to 0. |
dc_block |
bool |
positional or keyword | No | Optional value. Defaults to True. |
noise_floor_dbfs |
any |
positional or keyword | No | Optional value. Defaults to -120. |
smoothing |
any |
positional or keyword | No | Optional value. Defaults to 0. |
clip_detection |
bool |
positional or keyword | No | Optional value. Defaults to True. |
microphone.Analog().calibrate_silence(duration_ms=1000) -> dict
microphone.Analog().calibrate_silence(duration_ms=1000) -> dictMeasure quiet noise floor and DC offset.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
duration_ms |
int |
positional or keyword | No | Optional value. Defaults to 1000. |
microphone.Analog().calibrate_spl(reference_db_spl, duration_ms=1000) -> dict
microphone.Analog().calibrate_spl(reference_db_spl, duration_ms=1000) -> dictCalibrate SPL estimate against a known reference.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
reference_db_spl |
any |
positional or keyword | Yes | Required value. |
duration_ms |
int |
positional or keyword | No | Optional value. Defaults to 1000. |
microphone.Analog().clear_calibration() -> bool
microphone.Analog().clear_calibration() -> boolClear silence and SPL calibration.
microphone.Analog().on_audio(callback, samples_per_buffer=512) -> bool
microphone.Analog().on_audio(callback, samples_per_buffer=512) -> boolRegister a callback receiving a bounded audio summary dict. Pass None to stop.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
callback |
callback |
positional or keyword | Yes | Required value. |
samples_per_buffer |
any |
positional or keyword | No | Optional value. Defaults to 512. |
microphone.Analog().on_level(interval_ms, callback) -> bool
microphone.Analog().on_level(interval_ms, callback) -> boolRegister a callback receiving a level measurement dict.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
interval_ms |
int |
positional or keyword | Yes | Required value. |
callback |
callback |
positional or keyword | Yes | Required value. |
microphone.Analog().on_loud(threshold_dbfs, callback, hysteresis_db=3, cooldown_ms=500) -> bool
microphone.Analog().on_loud(threshold_dbfs, callback, hysteresis_db=3, cooldown_ms=500) -> boolRegister a loud-sound event callback.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
threshold_dbfs |
float |
positional or keyword | Yes | Required value. |
callback |
callback |
positional or keyword | Yes | Required value. |
hysteresis_db |
any |
positional or keyword | No | Optional value. Defaults to 3. |
cooldown_ms |
int |
positional or keyword | No | Optional value. Defaults to 500. |
microphone.Analog().stats() -> dict
microphone.Analog().stats() -> dictReturn source, sample rate, buffer, clipping, and calibration diagnostics.
microphone.Analog().reset_stats() -> bool
microphone.Analog().reset_stats() -> boolReset cumulative counters.
microphone.Analog().close() -> bool
microphone.Analog().close() -> boolStop and close the native microphone object.
microphone.Analog().AudioBuffer Methods
microphone.Analog().AudioBuffer Methods
microphone.Analog().sample(index) -> int
microphone.Analog().sample(index) -> intReturn one signed sample.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
index |
any |
positional or keyword | Yes | Required value. |
microphone.Analog().samples() -> list
microphone.Analog().samples() -> listConvert the buffer to a Berry list. This allocates memory.
microphone.Analog().rms() -> float
microphone.Analog().rms() -> floatReturn normalized RMS for this buffer.
microphone.Analog().peak() -> float
microphone.Analog().peak() -> floatReturn normalized peak for this buffer.
microphone.Analog().dbfs() -> float
microphone.Analog().dbfs() -> floatReturn dBFS for this buffer.
microphone.Analog().bytes() -> bytes
microphone.Analog().bytes() -> bytesReturn little-endian signed 16-bit PCM bytes.
microphone.I2S(bclk: int, ws: int, data: int, sample_rate: int = None, bits: int = None, channel: str = None, i2s: int = None, gain_db: float = None) -> microphone_device
microphone.I2S(bclk: int, ws: int, data: int, sample_rate: int = None, bits: int = None, channel: str = None, i2s: int = None, gain_db: float = None) -> microphone_deviceCreate a standard I2S microphone object.
This supports standard I2S digital microphones. PDM microphones and proprietary codecs are not treated as standard I2S devices. Wiring is explicit: pass the ESP32 GPIO number connected to each microphone signal. Microphone boards commonly label these pins as SCK/BCLK/BCK, WS/LRCLK/LRC, and SD/DOUT/DATA. The example ``bclk=14, ws=15, data=32`` is just one wiring choice, not a fixed requirement. Most I2S microphone boards also have an L/R or SEL pin. Tie that pin to GND for left channel or 3V3 for right channel, then pass the matching ``microphone.CHANNEL_LEFT`` or ``microphone.CHANNEL_RIGHT`` constant. If the readings are silent, try the other channel.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
bclk |
int |
positional or keyword | Yes | Required GPIO number connected to the microphone clock pin labeled SCK, BCLK, or BCK. |
ws |
int |
positional or keyword | Yes | Required GPIO number connected to the microphone word-select pin labeled WS, LRCLK, LRC, or WSEL. |
data |
int |
positional or keyword | Yes | Required GPIO number connected to the microphone data-output pin labeled SD, DOUT, DATA, or DIN on the ESP32 side. |
sample_rate |
int |
positional or keyword | No | Optional audio sample rate. Defaults to 16000. |
bits |
int |
positional or keyword | No | Optional bits per I2S slot. Use 16, 24, or 32. Defaults to 32. |
channel |
str |
positional or keyword | No | Optional data channel. Use microphone.CHANNEL_LEFT, microphone.CHANNEL_RIGHT, or microphone.CHANNEL_MONO. Defaults to microphone.CHANNEL_LEFT. |
i2s |
int |
positional or keyword | No | Optional I2S peripheral number. Defaults to automatic selection. |
gain_db |
float |
positional or keyword | No | Optional digital gain applied after sample conversion. Defaults to 0. |
microphone_device microphone_device object.
microphone.I2S().begin() -> bool
microphone.I2S().begin() -> boolStart acquisition and allocate hardware resources.
microphone.I2S().stop() -> bool
microphone.I2S().stop() -> boolStop acquisition and release ADC/I2S resources.
microphone.I2S().is_running() -> bool
microphone.I2S().is_running() -> boolReturn True when the microphone is active.
microphone.I2S().read(samples=256, timeout_ms=100) -> audio_buffer
microphone.I2S().read(samples=256, timeout_ms=100) -> audio_bufferCapture signed 16-bit mono samples, or None when no audio is available.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
samples |
int |
positional or keyword | No | Optional value. Defaults to 256. |
timeout_ms |
int |
positional or keyword | No | Optional value. Defaults to 100. |
microphone.I2S().level(window_ms=50) -> float
microphone.I2S().level(window_ms=50) -> floatReturn normalized RMS level from 0.0 through 1.0.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
window_ms |
int |
positional or keyword | No | Optional value. Defaults to 50. |
microphone.I2S().rms(window_ms=50) -> float
microphone.I2S().rms(window_ms=50) -> floatReturn normalized RMS amplitude.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
window_ms |
int |
positional or keyword | No | Optional value. Defaults to 50. |
microphone.I2S().peak(window_ms=50) -> float
microphone.I2S().peak(window_ms=50) -> floatReturn normalized peak amplitude.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
window_ms |
int |
positional or keyword | No | Optional value. Defaults to 50. |
microphone.I2S().peak_to_peak(window_ms=50) -> float
microphone.I2S().peak_to_peak(window_ms=50) -> floatReturn normalized max-min movement.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
window_ms |
int |
positional or keyword | No | Optional value. Defaults to 50. |
microphone.I2S().dbfs(window_ms=50) -> float
microphone.I2S().dbfs(window_ms=50) -> floatReturn RMS level in dBFS. This is not dB SPL.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
window_ms |
int |
positional or keyword | No | Optional value. Defaults to 50. |
microphone.I2S().db_spl(window_ms=125) -> float
microphone.I2S().db_spl(window_ms=125) -> floatReturn estimated SPL after calibrate_spl(), otherwise None.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
window_ms |
int |
positional or keyword | No | Optional value. Defaults to 125. |
microphone.I2S().is_loud(threshold_dbfs=-20, window_ms=50) -> bool
microphone.I2S().is_loud(threshold_dbfs=-20, window_ms=50) -> boolReturn True when dBFS crosses the threshold.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
threshold_dbfs |
float |
positional or keyword | No | Optional value. Defaults to -20. |
window_ms |
int |
positional or keyword | No | Optional value. Defaults to 50. |
microphone.I2S().configure(gain_db=0, dc_block=True, noise_floor_dbfs=-120, smoothing=0, clip_detection=True) -> bool
microphone.I2S().configure(gain_db=0, dc_block=True, noise_floor_dbfs=-120, smoothing=0, clip_detection=True) -> boolChange processing behavior without recreating the microphone.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
gain_db |
float |
positional or keyword | No | Optional value. Defaults to 0. |
dc_block |
bool |
positional or keyword | No | Optional value. Defaults to True. |
noise_floor_dbfs |
any |
positional or keyword | No | Optional value. Defaults to -120. |
smoothing |
any |
positional or keyword | No | Optional value. Defaults to 0. |
clip_detection |
bool |
positional or keyword | No | Optional value. Defaults to True. |
microphone.I2S().calibrate_silence(duration_ms=1000) -> dict
microphone.I2S().calibrate_silence(duration_ms=1000) -> dictMeasure quiet noise floor and DC offset.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
duration_ms |
int |
positional or keyword | No | Optional value. Defaults to 1000. |
microphone.I2S().calibrate_spl(reference_db_spl, duration_ms=1000) -> dict
microphone.I2S().calibrate_spl(reference_db_spl, duration_ms=1000) -> dictCalibrate SPL estimate against a known reference.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
reference_db_spl |
any |
positional or keyword | Yes | Required value. |
duration_ms |
int |
positional or keyword | No | Optional value. Defaults to 1000. |
microphone.I2S().clear_calibration() -> bool
microphone.I2S().clear_calibration() -> boolClear silence and SPL calibration.
microphone.I2S().on_audio(callback, samples_per_buffer=512) -> bool
microphone.I2S().on_audio(callback, samples_per_buffer=512) -> boolRegister a callback receiving a bounded audio summary dict. Pass None to stop.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
callback |
callback |
positional or keyword | Yes | Required value. |
samples_per_buffer |
any |
positional or keyword | No | Optional value. Defaults to 512. |
microphone.I2S().on_level(interval_ms, callback) -> bool
microphone.I2S().on_level(interval_ms, callback) -> boolRegister a callback receiving a level measurement dict.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
interval_ms |
int |
positional or keyword | Yes | Required value. |
callback |
callback |
positional or keyword | Yes | Required value. |
microphone.I2S().on_loud(threshold_dbfs, callback, hysteresis_db=3, cooldown_ms=500) -> bool
microphone.I2S().on_loud(threshold_dbfs, callback, hysteresis_db=3, cooldown_ms=500) -> boolRegister a loud-sound event callback.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
threshold_dbfs |
float |
positional or keyword | Yes | Required value. |
callback |
callback |
positional or keyword | Yes | Required value. |
hysteresis_db |
any |
positional or keyword | No | Optional value. Defaults to 3. |
cooldown_ms |
int |
positional or keyword | No | Optional value. Defaults to 500. |
microphone.I2S().stats() -> dict
microphone.I2S().stats() -> dictReturn source, sample rate, buffer, clipping, and calibration diagnostics.
microphone.I2S().reset_stats() -> bool
microphone.I2S().reset_stats() -> boolReset cumulative counters.
microphone.I2S().close() -> bool
microphone.I2S().close() -> boolStop and close the native microphone object.
microphone.I2S().AudioBuffer Methods
microphone.I2S().AudioBuffer Methods
microphone.I2S().sample(index) -> int
microphone.I2S().sample(index) -> intReturn one signed sample.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
index |
any |
positional or keyword | Yes | Required value. |
microphone.I2S().samples() -> list
microphone.I2S().samples() -> listConvert the buffer to a Berry list. This allocates memory.
microphone.I2S().rms() -> float
microphone.I2S().rms() -> floatReturn normalized RMS for this buffer.
microphone.I2S().peak() -> float
microphone.I2S().peak() -> floatReturn normalized peak for this buffer.
microphone.I2S().dbfs() -> float
microphone.I2S().dbfs() -> floatReturn dBFS for this buffer.
microphone.I2S().bytes() -> bytes
microphone.I2S().bytes() -> bytesReturn little-endian signed 16-bit PCM bytes.