Atomic Echo Base

支持以下产品:

Atomic Echo Base

以下是主机对 Atomic Echo Base 的详细支持:

控制器

Atomic Echo Base

Atom Echo

Atom Lite

Atom Matrix

AtomS3

AtomS3 Lite

AtomS3R

AtomS3R-CAM

AtomS3R-Ext

UiFlow2 应用示例

播放 WAV 文件

在 UiFlow2 中打开 atoms3_play_wav_example.m5f2 项目。

66.wav

此示例从文件系统读取音频文件并播放。

UiFlow2 代码块:

atoms3_play_wav_example.png

示例输出:

None

播放控制

在 UiFlow2 中打开 atoms3_playback_controls_example.m5f2 项目。

66.wav

此示例演示如何使用 AtomicEchoBase 类控制播放。

播放音频 1 秒,暂停 1 秒,然后继续播放。

UiFlow2 代码块:

atoms3_playback_controls_example.png

示例输出:

None

录制音频

在 UiFlow2 中打开 atoms3_record_audio_example.m5f2 项目。

此示例从麦克风录制音频并将其保存到 PCM 缓冲区,然后通过扬声器播放。

UiFlow2 代码块:

atoms3_record_audio_example.png

示例输出:

None

MicroPython 应用示例

播放 WAV 文件

此示例从文件系统读取音频文件并播放。

MicroPython 代码块:

 1# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from hardware import I2C
 9from hardware import Pin
10from base import AtomicEchoBase
11import time
12
13
14i2c1 = None
15base_echo = None
16
17
18def setup():
19    global i2c1, base_echo
20
21    M5.begin()
22    i2c1 = I2C(1, scl=Pin(39), sda=Pin(38), freq=100000)
23    base_echo = AtomicEchoBase(i2c1, 0x18, 1, 16000, 8, 6, 7, 5)
24    base_echo.play_wav_file("/flash/res/audio/66.wav")
25    time.sleep(1)
26    base_echo.pause()
27    time.sleep(1)
28    base_echo.resume()
29
30
31def loop():
32    global i2c1, base_echo
33    M5.update()
34
35
36if __name__ == "__main__":
37    try:
38        setup()
39        while True:
40            loop()
41    except (Exception, KeyboardInterrupt) as e:
42        try:
43            from utility import print_error_msg
44
45            print_error_msg(e)
46        except ImportError:
47            print("please update to latest firmware")

示例输出:

None

播放控制

此示例演示如何使用 AtomicEchoBase 类控制播放。

播放音频 1 秒,暂停 1 秒,然后继续播放。

MicroPython 代码块:

 1# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from hardware import I2C
 9from hardware import Pin
10from base import AtomicEchoBase
11import time
12
13
14i2c1 = None
15base_echo = None
16
17
18def setup():
19    global i2c1, base_echo
20
21    M5.begin()
22    i2c1 = I2C(1, scl=Pin(39), sda=Pin(38), freq=100000)
23    base_echo = AtomicEchoBase(i2c1, 0x18, 1, 16000, 8, 6, 7, 5)
24    base_echo.play_wav_file("/flash/res/audio/66.wav")
25    time.sleep(1)
26    base_echo.pause()
27    time.sleep(1)
28    base_echo.resume()
29
30
31def loop():
32    global i2c1, base_echo
33    M5.update()
34
35
36if __name__ == "__main__":
37    try:
38        setup()
39        while True:
40            loop()
41    except (Exception, KeyboardInterrupt) as e:
42        try:
43            from utility import print_error_msg
44
45            print_error_msg(e)
46        except ImportError:
47            print("please update to latest firmware")

示例输出:

None

录制音频

此示例从麦克风录制音频并将其保存到 PCM 缓冲区,然后通过扬声器播放。

MicroPython 代码块:

 1# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from hardware import I2C
 9from hardware import Pin
10from base import AtomicEchoBase
11
12
13i2c1 = None
14base_echo = None
15
16
17def setup():
18    global i2c1, base_echo
19
20    M5.begin()
21    i2c1 = I2C(1, scl=Pin(39), sda=Pin(38), freq=100000)
22    base_echo = AtomicEchoBase(i2c1, 0x18, 1, 16000, 8, 6, 7, 5)
23    base_echo.record(rate=16000, bits=16, channel=AtomicEchoBase.STEREO, duration=500)
24    base_echo.play_raw(
25        base_echo.pcm_buffer, rate=16000, bits=16, channel=AtomicEchoBase.STEREO, duration=-1
26    )
27
28
29def loop():
30    global i2c1, base_echo
31    M5.update()
32
33
34if __name__ == "__main__":
35    try:
36        setup()
37        while True:
38            loop()
39    except (Exception, KeyboardInterrupt) as e:
40        try:
41            from utility import print_error_msg
42
43            print_error_msg(e)
44        except ImportError:
45            print("please update to latest firmware")

示例输出:

None

AtomicEchoBase

class base.echo.AtomicEchoBase(*args, **kwargs)

基类:object

创建一个 AtomicEchoBase 对象。

参数:
  • i2c (I2C) – I2C 对象

  • address (int) – ES8311的I2C地址。默认值为0x18。

  • i2s_port (int) – I2S 端口号。默认值为 1。

  • sample_rate (int) – 音频的采样率。默认值为 16000。

  • i2s_sck (int) – I2S SCK 引脚。默认值为 -1。

  • i2s_ws (int) – I2S WS 引脚。默认值为 -1。

  • i2s_di (int) – I2S DI 引脚。默认值为 -1。

  • i2s_do (int) – I2S DO 引脚。默认值为 -1。

UIFLOW2:

init.png

MicroPython:

from hardware import I2C
from hardware import Pin
from base import AtomicEchoBase

# atom echo
i2c1 = I2C(1, scl=Pin(21), sda=Pin(25), freq=100000)
base_echo = AtomicEchoBase(i2c1, address=0x18, i2s_port=1, sample_rate=44100, i2s_sck=33, i2s_ws=19, i2s_di=23, i2s_do=22)

# atom lite
i2c1 = I2C(1, scl=Pin(21), sda=Pin(25), freq=100000)
base_echo = AtomicEchoBase(i2c1, address=0x18, i2s_port=1, sample_rate=44100, i2s_sck=33, i2s_ws=19, i2s_di=23, i2s_do=22)

# atom matrix
i2c1 = I2C(1, scl=Pin(21), sda=Pin(25), freq=100000)
base_echo = AtomicEchoBase(i2c1, address=0x18, i2s_port=1, sample_rate=44100, i2s_sck=33, i2s_ws=19, i2s_di=23, i2s_do=22)

# atoms3 / atoms3 lite
i2c1 = I2C(1, scl=Pin(39), sda=Pin(38), freq=100000)
base_echo = AtomicEchoBase(i2c1, address=0x18, i2s_port=1, sample_rate=44100, i2s_sck=8, i2s_ws=6, i2s_di=7, i2s_do=5)

# atoms3r / atoms3r-cam / atoms3-ext
i2c1 = I2C(1, scl=Pin(39), sda=Pin(38), freq=100000)
base_echo = AtomicEchoBase(i2c1, address=0x18, i2s_port=1, sample_rate=44100, i2s_sck=8, i2s_ws=6, i2s_di=7, i2s_do=5)

base_echo.speaker.tone(2000, 1000)
base_echo.speaker.playWavFile('res/audio/66.wav')
MONO = 1

单声道

STEREO = 2

立体声

play_wav_file(file)

播放 WAV 文件。

参数:

file (str) – 要播放的 WAV 文件的路径。

返回:

None

返回类型:

None

UiFlow2 代码块:

play_wav_file.png

MicroPython 代码块:

base_echo.play_wav_file("/flash/res/audio/test.wav")
tone(freq, duration)

播放简单的音调声音。

参数:
  • freq (int) – 音调的频率(以赫兹为单位)。

  • duration (int) – 音调的持续时间(以毫秒为单位)。

返回:

None

返回类型:

None

UiFlow2 代码块:

tone.png

MicroPython 代码块:

base_echo.tone(2000, 50)
play_wav(buf, duration=-1)

播放 WAV 缓冲区。

参数:
  • buf (bytes) – 要播放的 WAV 缓冲区。

  • duration (int) – WAV 缓冲区的持续时间(以毫秒为单位)。当持续时间为 -1 时,它将播放直到停止。(默认值为 -1)。

返回:

None

返回类型:

None

UiFlow2 代码块:

play_wav.png

MicroPython 代码块:

base_echo.play_wav(wav_buffer, duration=1000)
play_raw(buf, rate=16000, bits=16, channel=2, duration=-1)

播放 pcm 缓冲区。

参数:
  • buf (bytes) – 要播放的 PCM 缓冲区。

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

  • bits (int) – 位深度(默认为 16)。

  • channel (int) – 通道数(默认为 2)。

  • duration (int) – PCM 缓冲区的持续时间(以毫秒为单位)。当持续时间为 -1 时,它将播放直到停止。(默认值为 -1)。

返回:

None

返回类型:

None

UiFlow2 代码块:

play_raw.png

MicroPython 代码块:

base_echo.play_raw(pcm_buffer, rate=16000, bits=16, channel=2, duration=1000)
pause()

暂停播放。

UiFlow2 代码块:

pause.png

MicroPython 代码块:

audio.tone(2000, 100)
time.sleep(0.05)
base_echo.pause()
time.sleep(0.05)
base_echo.resume()
返回类型:

None

resume()

继续播放。

UiFlow2 代码块:

resume.png

MicroPython 代码块:

audio.tone(2000, 100)
time.sleep(0.05)
base_echo.pause()
time.sleep(0.05)
base_echo.resume()
stop()

停止播放。

UiFlow2 代码块:

stop.png

MicroPython 代码块:

audio.tone(2000, 100)
time.sleep(0.05)
base_echo.stop()
get_volume()

获取扬声器音量级别。

返回:

音量级别(0-100)。

返回类型:

int

UiFlow2 代码块:

get_volume.png

MicroPython 代码块:

base_echo.get_volume()
set_volume(volume)

设置扬声器音量。

参数:

volume (int) – 音量级别(0-100)。

UiFlow2 代码块:

set_volume.png

MicroPython 代码块:

base_echo.set_volume(50)
record_wav_file(path, rate=16000, bits=16, channel=2, duration=3000)

将音频录制为 WAV 文件。

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

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

  • bits (int) – 位深度(默认为 16)。

  • channel (int) – 通道数(默认为 2)。

  • duration (int) – 录音时长(以毫秒为单位)(默认为 3000)。

UiFlow2 代码块:

record_wav_file.png

MicroPython 代码块:

base_echo.record_wav_file("/flash/res/audio/test.wav", rate=16000, bits=16, channel=2, duration=3000)
record(rate=16000, bits=16, channel=2, duration=3000)

将音频录制到 PCM 缓冲区。

参数:
  • rate (int) – 采样率(默认为 16000)。

  • bits (int) – 位深度(默认为 16)。

  • channel (int) – 通道数(默认为 2)。

  • duration (int) – 录音时长(以毫秒为单位)(默认为 3000)。

UiFlow2 代码块:

record.png

MicroPython 代码块:

base_echo.record(rate=16000, bits=16, channel=2, duration=3000)
property pcm_buffer: bytes

获取 PCM 缓冲区。

返回:

PCM缓冲区。

UiFlow2 代码块:

pcm_buffer.png

MicroPython 代码块:

base_echo.pcm_buffer