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:
class Mic
Important
Methods of the Mic Class heavily rely on M5.begin()
and M5.update()
.
All calls to methods of Mic objects should be placed after M5.begin()
, and M5.update()
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 amic_config_tobject 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")


Set property:
Python:
Mic.config(pin_data_in=1)


- 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:

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.






