PDM Unit

这是 PDM 单元的驱动程序库,提供了一套控制 PDM 麦克风的方法。通过 I2S 接口,该模块可记录音频数据并保存为 WAV 文件。

支持以下产品:

pdm

UiFlow2 应用示例

录音音频并播放

在 UiFlow2 中打开 pdm_cores3_example.m5f2 项目。

这个示例展示了录音音频并播放。

UiFlow2 代码块:

example.png

示例输出:

None

MicroPython 应用示例

录音音频并播放

这个示例展示了录音音频并播放。

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 *
 8from unit import PDMUnit
 9import time
10
11
12label0 = None
13title0 = None
14pdm_0 = None
15
16
17rec_data = None
18
19
20def setup():
21    global label0, title0, pdm_0, rec_data
22
23    M5.begin()
24    Widgets.fillScreen(0x222222)
25    label0 = Widgets.Label("label0", 128, 114, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
26    title0 = Widgets.Title("PDMUnit CoreS3 Example", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18)
27
28    pdm_0 = PDMUnit((1, 2), i2s_port=2, sample_rate=44100)
29    Speaker.begin()
30    Speaker.setVolumePercentage(1)
31    Speaker.end()
32    pdm_0.begin()
33    rec_data = bytearray(44100 * 10)
34    label0.setText(str("rec..."))
35    pdm_0.record(rec_data, _, False)
36    time.sleep_ms(100)
37    while pdm_0.isRecording():
38        label0.setText(str("rec..."))
39        time.sleep_ms(100)
40    pdm_0.end()
41    Speaker.begin()
42    label0.setText(str("play..."))
43    Speaker.playRaw(rec_data, 44100 * 2)
44    while Speaker.isPlaying():
45        time.sleep_ms(100)
46    label0.setText(str("done"))
47
48
49def loop():
50    global label0, title0, pdm_0, rec_data
51    M5.update()
52
53
54if __name__ == "__main__":
55    try:
56        setup()
57        while True:
58            loop()
59    except (Exception, KeyboardInterrupt) as e:
60        try:
61            from utility import print_error_msg
62
63            print_error_msg(e)
64        except ImportError:
65            print("please update to latest firmware")

示例输出:

None

API

PDMUnit

class unit.pdm.PDMUnit(*args, **kwargs)

基类:object

PDM Unit class.

参数:
  • port (list | tuple) – 连接到 PDM Unit

  • i2s_port (int) – I2S 端口号。

  • sample_rate (int) – 采样率。

UiFlow2 代码块:

init.png

MicroPython 代码块:

from unit import PDMUnit

pdm_0 = PDMUnit((1, 2), i2s_port=0, sample_rate=44100)
begin()

初始化 PDM 麦克风。

返回:

如果初始化成功,返回 True,否则返回 False。

返回类型:

bool

UiFlow2 代码块:

begin.png

MicroPython 代码块:

pdm.begin()
end()

停止 PDM 麦克风。

UiFlow2 代码块:

end.png

MicroPython 代码块:

pdm.end()
record(buffer, sample_rate=16000, stereo=False)

将音频数据记录到提供的缓冲区中。

参数:
  • buffer (bytearray) – 存储记录的音频数据的缓冲区。

  • sample_rate (int) – 采样率(默认:16000)

  • stereo (bool) – True 表示立体声录音,False 表示单声道录音(默认:False)

返回:

如果录音成功开始,返回 True,否则返回 False。

返回类型:

bool

UiFlow2 代码块:

record.png

MicroPython 代码块:

rec_data = bytearray(16000 * 5)  # 5 seconds buffer
pdm.record(rec_data, 16000, False)
isRecording()

检查录音是否正在进行。

返回:

返回记录的字节数。

  • 0 - 未录音

  • 1 - 录音(队列有可用空间)

  • 2 - 录音(队列已满)

返回类型:

int

UiFlow2 代码块:

isRecording.png

MicroPython 代码块:

pdm.isRecording()
recordWavFile(path, rate=16000, time=5, stereo=False)

直接将音频记录到 WAV 文件中。

参数:
  • path (str) – 保存 WAV 文件的路径

  • rate (int) – 采样率(默认:16000)

  • time (int) – 录音时长(默认:5秒)

  • stereo (bool) – True 表示立体声录音,False 表示单声道录音(默认:False)

返回:

如果录音成功,返回 True,否则返回 False。

返回类型:

bool

UiFlow2 代码块:

recordWavFile.png

MicroPython 代码块:

pdm.recordWavFile("/sd/test.wav", 16000, 5, False)
config(**kwargs)

配置 PDM 麦克风参数。

参数:

kwargs

配置参数

  • pin_data_in: 数据输入引脚

  • pin_ws: 字选引脚

  • sample_rate: 采样率

  • stereo: 立体声模式

  • over_sampling: 过采样率

  • noise_filter_level: 噪声过滤级别

  • magnification: 音频放大倍数

  • dma_buf_len: DMA 缓冲区长度

  • dma_buf_count: DMA 缓冲区计数

  • task_priority: 任务优先级

  • task_pinned_core: 指定任务运行的核心

  • i2s_port: I2S 端口号

UiFlow2 代码块:

get_config_boolean.png

get_config_int.png

set_config_boolean.png

set_config_int.png

MicroPython 代码块:

pdm.config(
    dma_buf_count=3,
    dma_buf_len=256,
    over_sampling=2,
    noise_filter_level=0,
    sample_rate=16000,
    pin_data_in=1,
    pin_ws=2,
    pin_bck=-1,
    pin_mck=-1,
    use_adc=False,
    stereo=False,
    magnification=1,
    task_priority=2,
    task_pinned_core=255,
    i2s_port=i2s_port,
)