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; M5.Mic.

StickS3

Analog MEMS microphone through ES8311; M5.Mic.

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 应用示例:

example.png

cores3_mic_example.m5f2

class Mic

重要

Mic Class 的方法严重依赖 M5.begin() M5.begin.png 和 M5.update() M5.update.png。

对 Mic 对象方法的所有调用都应放在 M5.begin() M5.begin.png 之后,并且应在主循环中调用 M5.update() M5.update.png。

备注

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")

get_config_boolean.png

get_config_int.png

设置属性:

Python:

Mic.config(pin_data_in=1)

set_config_int.png

set_config_boolean.png

Mic.begin() → bool

启动 Mic 功能。执行成功返回 True 。

UIFLOW2:

begin.png

Mic.end() → bool

停止 Mic 功能。若成功则返回 True。

UIFLOW2:

end.png

Mic.isRunning() → bool

获取 Mic 是否处于运行状态, 返回bool类型。

UIFLOW2:

isRunning.png

Mic.isEnabled() → bool

获取 Mic 是否处于使能状态, 返回bool类型。

UIFLOW2:

isEnabled.png

Mic.isRecording() → int

获取 Mic 是否处于录音状态, 返回int类型。

返回值:

  • 0=未录制

  • 1=录音(队列中有空间)

  • 2=录制中(队列中没有空间。)

UIFLOW2:

isRecording.png

Mic.setSampleRate(sample_rate) → None

设置采样率。参数 sample_rate 一般有 8000 , 11025 ,22050 ,32000 ,44100

UIFLOW2:

setSampleRate.png

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

录制音频数据。

参数 rec_data 要求传入一个buffer。参数 rate 一般有 8000, 11025,22050,32000,44100, 默认填8000。参数 stereo 传入True或者False。

UIFLOW2:

record.png

class mic_config_t

mic_config_t.pin_data_in: int

I2S 的串行数据线,用二进制补码表示的音频数据。

mic_config_t.pin_bck: int

I2S 的串行时钟线,对应数字音频的每一位数据。

mic_config_t.pin_mck: int

I2S 的主时钟线。一般为了使系统间能够更好地同步时增加MCLK信号,MCLK的频率 = 256 * 采样频率。

mic_config_t.pin_ws: int

I2S 的帧时钟,用于切换左右声道的数据。

mic_config_t.sample_rate: int

输入音频的目标采样率。

mic_config_t.stereo: bool

使用双声道输出。

mic_config_t.over_sampling: int

求平均值的采样次数。

mic_config_t.magnification: int

输入值的乘数。

mic_config_t.noise_filter_level: int

先前值的系数,用于噪声过滤。

mic_config_t.use_adc: bool

使用模拟输入麦克风(仅需要 pin_data_in )。

mic_config_t.dma_buf_len: int

I2S 的DMA缓冲区长度。

mic_config_t.dma_buf_count: int

I2S 的DMA缓冲区数量。

mic_config_t.task_priority: int

后台任务优先级。

mic_config_t.task_pinned_core: int

后台任务使用的CPU。

mic_config_t.i2s_port: int

I2S端口。

Mic.recordWavFile(path, rate=16000, time=5, stereo=False) → bool

Record 16-bit PCM audio to a WAV file, including its header. path is a writable filesystem path, rate is the sample rate in Hz and time is the duration in seconds. stereo=False selects mono. This call blocks while recording and returns True on completion; filesystem failures raise OSError. It requires working microphone initialization; it is not the ADF audio.Recorder API.

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)