Skip to main content

Microphone (mic)

This module controls PDM microphone capture, based on the I2S_PDM driver. It supports initialization, data reading, release, and simple audio statistics analysis.

Methods

1. mic.init(sample_rate, bit_width, slot_mode, buff_size)

Function: Initialize microphone capture, fixed to use GPIO40 (clock) and GPIO41 (data input).

Arguments:

ArgumentTypeDescription
sample_rateintSample rate (e.g. 16000, 44100, etc.)
bit_widthintBit width (usually 16)
slot_modeintSlot mode (mono or stereo configuration, matching your driver definition)
buff_sizeintBuffer size (in bytes)

Example:

import mic

mic.init(16000, 16, 1, 4096)

2. mic.read_data(buffer) -> int

Function: Read audio data from PDM and write it into the passed buffer.

Arguments:

  • buffer: a bytearray that must be pre-allocated with sufficient size, not smaller than buff_size.

Return value:

  • The actual number of bytes read (int)

Note:

  • The buffer length must be greater than or equal to the buff_size set during initialization.
  • Calling it before initialization will raise an exception.

Example:

import mic

buffer = bytearray(4096)
bytes_read = mic.read_data(buffer)
print("Read", bytes_read, "bytes")

3. mic.release()

Function: Release microphone resources and close I2S PDM.

Example:

mic.release()

4. mic.calculate_stats() -> dict

Function: Automatically reads one frame of data from PDM and calculates its maximum, minimum, and average values.

Return value:

  • A dictionary containing:
    • max: maximum sample value (int16)
    • min: minimum sample value (int16)
    • avg: average sample value (int16)

Example:

stats = mic.calculate_stats()
print("Max:", stats['max'])
print("Min:", stats['min'])
print("Avg:", stats['avg'])

Note:

  • It automatically allocates and releases the buffer internally;
  • Calling it before initialization will raise an exception.

Usage Example

import mic

# Initialize
mic.init(16000, 16, 1, 4096)

# Read data
buffer = bytearray(4096)
mic.read_data(buffer)

# Calculate statistics
stats = mic.calculate_stats()
print(stats)

# Release resources
mic.release()

⚠ Notes

  • GPIOs are fixed at initialization:
    • Clock CLK_IO = 40
    • Data DIN_IO = 41
  • Each initialization overwrites the previous configuration;
  • You must call mic.init() before calling other methods;
  • calculate_stats() is suitable for simple volume detection and sound energy analysis.