Mic

Mic is used to control the built-in microphone inside the host device. Below is the detailed Mic support for the host:

Controller

SPM1423

ES7210

AtomS3

AtomS3 Lite

AtomS3U

StampS3

CoreS3

Core2

TOUGH

Micropython Example:

 1# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8import time
 9
10
11label0 = None
12
13
14rec_data = None
15
16
17def setup():
18    global label0, rec_data
19
20    M5.begin()
21    Widgets.fillScreen(0x222222)
22    label0 = Widgets.Label("label0", 123, 58, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
23
24    Speaker.begin()
25    Speaker.setVolumePercentage(1)
26    Speaker.end()
27    Mic.begin()
28    rec_data = bytearray(8000 * 5)
29    label0.setText(str("rec..."))
30    Mic.record(rec_data, 8000, False)
31    while Mic.isRecording():
32        time.sleep_ms(500)
33    Mic.end()
34    Speaker.begin()
35    label0.setText(str("play..."))
36    Speaker.playRaw(rec_data, 8000)
37    while Speaker.isPlaying():
38        time.sleep_ms(500)
39    label0.setText(str("done"))
40
41
42def loop():
43    global label0, rec_data
44    M5.update()
45
46
47if __name__ == "__main__":
48    try:
49        setup()
50        while True:
51            loop()
52    except (Exception, KeyboardInterrupt) as e:
53        try:
54            from utility import print_error_msg
55
56            print_error_msg(e)
57        except ImportError:
58            print("please update to latest firmware")

UIFLOW2 Example:

example.png

cores3_mic_example.m5f2

class Mic

Important

Methods of the Mic Class heavily rely on M5.begin() M5.begin.png and M5.update() M5.update.png.

All calls to methods of Mic objects should be placed after M5.begin() M5.begin.png, and M5.update() M5.update.png should be called in the main loop.

Methods

Mic.config([cfg:mic_config_t])
Mic.config('param')
Mic.config(param=value)

Get or set the parameters of the Mic object.

When no parameters are passed, it returns an object of mic_config_t. When a mic_config_t object is passed, Mic sets all supported parameters of the Mic.

When passing parameters from the table below, Mic will get or set the passed parameters.

The following parameters are supported:

Parameter

Type

Description

pin_data_in

(integer)

Serial data line of I2S, representing audio data in binary complement.

pin_bck

(integer)

Serial clock line of I2S, corresponding to each bit of digital audio data.

pin_mck

(integer)

Master clock line of I2S. Generally, to better synchronize between systems, increase the MCLK signal, MCLK frequency = 256 * sampling frequency.

pin_ws

(integer)

Frame clock of I2S, used to switch left and right channel data.

sample_rate

(integer)

Target sampling rate of input audio.

stereo

(boolean)

Use stereo output.

over_sampling

(integer)

Number of times to average the sampling.

magnification

(integer)

Multiplier of the input value.

noise_filter_level

(integer)

Coefficient of the previous value used for noise filtering.

use_adc

(boolean)

Use analog input microphone (only pin_data_in is needed).

dma_buf_len

(integer)

DMA buffer length of I2S.

dma_buf_count

(integer)

Number of DMA buffers of I2S.

task_priority

(integer)

Priority of background tasks.

task_pinned_core

(integer)

CPU used by background tasks.

i2s_port

(integer)

I2S port.

UIFLOW2:

Read property:

Python:

Mic.config("pin_data_in")

get_config_boolean.png

get_config_int.png

Set property:

Python:

Mic.config(pin_data_in=1)

set_config_int.png

set_config_boolean.png

Mic.begin() bool

Start the Mic function. Returns True if successful.

UIFLOW2:

begin.png

Mic.end() bool

Stop the Mic function. Returns True if successful.

UIFLOW2:

end.png

Mic.isRunning() bool

Check if Mic is running. Returns a boolean value.

UIFLOW2:

isRunning.png

Mic.isEnabled() bool

Check if Mic is enabled. Returns a boolean value.

UIFLOW2:

isEnabled.png

Mic.isRecording() int

Check if Mic is recording. Returns an integer value.

Return values:

  • 0=not recording

  • 1=recording (There’s room in the queue)

  • 2=recording (There’s no room in the queue.)

UIFLOW2:

isRecording.png

Mic.setSampleRate(sample_rate) None

Set the sampling rate. The parameter sample_rate generally includes 8000, 11025, 22050, 32000, 44100.

UIFLOW2:

setSampleRate.png

Mic.record(rec_data[, rate[, stereo]]) bool

Record audio data.

The parameter rec_data requires passing a buffer. The parameter rate generally includes 8000, 11025, 22050, 32000, 44100, with a default of 8000. The parameter stereo is passed as True or False.

UIFLOW2:

record.png

class mic_config_t

mic_config_t.pin_data_in: int

Serial data line of I2S, representing audio data in binary complement.

mic_config_t.pin_bck: int

Serial clock line of I2S, corresponding to each bit of digital audio data.

mic_config_t.pin_mck: int

Master clock line of I2S. Generally, to better synchronize between systems, increase the MCLK signal, MCLK frequency = 256 * sampling frequency.

mic_config_t.pin_ws: int

Frame clock of I2S, used to switch left and right channel data.

mic_config_t.sample_rate: int

Target sampling rate of input audio.

mic_config_t.stereo: bool

Use stereo output.

mic_config_t.over_sampling: int

Number of times to average the sampling.

mic_config_t.magnification: int

Multiplier of the input value.

mic_config_t.noise_filter_level: int

Coefficient of the previous value used for noise filtering.

mic_config_t.use_adc: bool

Use analog input microphone (only pin_data_in is needed).

mic_config_t.dma_buf_len: int

DMA buffer length of I2S.

mic_config_t.dma_buf_count: int

Number of DMA buffers of I2S.

mic_config_t.task_priority: int

Priority of background tasks.

mic_config_t.task_pinned_core: int

CPU used by background tasks.

mic_config_t.i2s_port: int

I2S port.