Mic
Mic 用于控制主机内部集成的按键。以下是主机的 Mic 支持详细:
Controller |
SPM1423 |
ES7210 |
|---|---|---|
AtomS3 |
||
AtomS3 Lite |
||
AtomS3U |
✔ |
|
StampS3 |
||
CoreS3 |
✔ |
|
Core2 |
✔ |
|
TOUGH |
Additional controller support:
Controller |
Microphone path |
|---|---|
StopWatch |
Analog MEMS microphone through ES8311; |
StickS3 |
Analog MEMS microphone through ES8311; |
For writable recording paths, see Resource Files and Paths.
MicroPython 应用示例:
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 应用示例:
class Mic
重要
Mic Class 的方法严重依赖 M5.begin()
和 M5.update()
。
对 Mic 对象方法的所有调用都应放在 M5.begin()
之后,并且应在主循环中调用 M5.update()
。
备注
Mic is registered in the M5 module, not in the hardware
module. Use from M5 import * to make Mic available. Do not
write from hardware import Mic.
Methods
- Mic.config([cfg:mic_config_t])
- Mic.config('param')
- Mic.config(param=value)
获取或者设置 Mic 对象的参数。
当不传入任何参数时,会返回
mic_config_t的对象。当传入一个mic_config_t的对象, Mic 会设置 Mic 的所有支持的参数。当传入下表中的参数, Mic 会对传入的参数进行获取或者设置。
以下是支持的参数:
Parameter
类型
Description
pin_data_in
(integer)
I2S 的串行数据线,用二进制补码表示的音频数据。
pin_bck
(integer)
I2S 的串行时钟线,对应数字音频的每一位数据。
pin_mck
(integer)
I2S 的主时钟线。一般为了使系统间能够更好地同步时增加MCLK信号,MCLK的频率 = 256 * 采样频率。
pin_ws
(integer)
I2S 的帧时钟,用于切换左右声道的数据。
sample_rate
(integer)
输入音频的目标采样率。
立体声
(boolean)
使用双声道输出。
过采样
(integer)
求平均值的采样次数。
放大倍数。
(integer)
输入值的乘数。
噪声滤波等级
(integer)
先前值的系数,用于噪声过滤。
use_adc
(boolean)
使用模拟输入麦克风(仅需要 pin_data_in )。
dma_buf_len
(integer)
I2S 的DMA缓冲区长度。
dma_buf_count
(integer)
I2S 的DMA缓冲区数量。
task_priority
(integer)
后台任务优先级。
task_pinned_core
(integer)
后台任务使用的CPU。
i2s_port
(integer)
I2S端口。
UIFLOW2:
读取属性:
Python:
Mic.config("pin_data_in")


设置属性:
Python:
Mic.config(pin_data_in=1)


class mic_config_t
- Mic.recordWavFile(path, rate=16000, time=5, stereo=False) bool
Record 16-bit PCM audio to a WAV file, including its header.
pathis a writable filesystem path,rateis the sample rate in Hz andtimeis the duration in seconds.stereo=Falseselects mono. This call blocks while recording and returnsTrueon completion; filesystem failures raiseOSError. It requires working microphone initialization; it is not the ADFaudio.RecorderAPI.Use keywords for the duration:
recordWavFile(path, 1)sets a 1 Hz sample rate, not a one-second recording. A one-second, 16 kHz, mono recording contains 32,000 PCM bytes plus the WAV header (32,044 bytes for this implementation). The parent directory must already exist. Recording opens the destination for writing and replaces its contents.import M5 M5.begin() # Prepare /flash/res/audio first; choose a new destination filename. M5.Mic.recordWavFile("/flash/res/audio/recording.wav", rate=16000, time=1)







