ASR Unit

Unit ASR is an AI offline speech recognition unit, featuring the built-in AI smart offline speech module CI-03T. This unit offers powerful functions such as speech recognition, voiceprint recognition, speech enhancement, and speech detection. It supports AEC (Acoustic Echo Cancellation) to effectively eliminate echoes and noise interference, improving the accuracy of speech recognition. Additionally, it supports mid-speech interruption, allowing for flexible interruption during the recognition process and quick response to new commands. The product is pre-configured with wake-up words and feedback commands at the factory. The device uses UART serial communication for data transmission and also supports waking up the device via UART or voice keywords. This unit supports user customization of the wake-up recognition word and can recognize up to 300 command words. It is equipped with a microphone for clear audio capture and includes a speaker for high-quality audio feedback. This product is widely used in AI assistants, smart homes, security monitoring, automotive systems, robotics, smart hardware, healthcare, and other fields, making it an ideal choice for realizing smart voice interactions.

Support the following products:

ASRUnit

UiFlow2 Example

ASR Example

Open the asr_cores3_example.m5f2 project in UiFlow2.

This example shows how to use Unit ASR to get the current command word, command number, and trigger an event when you say hello to do something you want to do.

UiFlow2 Code Block:

example.png

Example output:

None

MicroPython Example

ASR Example

This example shows how to use Unit ASR to get the current command word, command number, and trigger an event when you say hello to do something you want to do.

MicroPython Code Block:

 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 ASRUnit
 9
10
11title0 = None
12label0 = None
13label1 = None
14label2 = None
15label3 = None
16asr_0 = None
17
18
19def asr_0_hello_event(args):
20    global title0, label0, label1, label2, label3, asr_0
21    print("Rec Hello")
22
23
24def setup():
25    global title0, label0, label1, label2, label3, asr_0
26
27    M5.begin()
28    Widgets.fillScreen(0x222222)
29    title0 = Widgets.Title(
30        "UnitASR M5CoreSe Example", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18
31    )
32    label0 = Widgets.Label("msg:", 0, 51, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
33    label1 = Widgets.Label("rec cmd num:", 0, 93, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
34    label2 = Widgets.Label(
35        "rec cmd word:", 0, 134, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18
36    )
37    label3 = Widgets.Label(
38        "rec cmd handler state:", 0, 178, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18
39    )
40
41    asr_0 = ASRUnit(2, port=(1, 2))
42    print(asr_0.search_command_num("hello"))
43    print(asr_0.search_command_word(0x32))
44    asr_0.add_command_word(0x32, "hello", asr_0_hello_event)
45
46
47def loop():
48    global title0, label0, label1, label2, label3, asr_0
49    M5.update()
50    if asr_0.get_received_status():
51        label0.setText(str((str("msg:") + str((asr_0.get_current_raw_message())))))
52        label1.setText(str((str("rec cmd num:") + str((asr_0.get_current_command_num())))))
53        label2.setText(str((str("rec cmd word:") + str((asr_0.get_current_command_word())))))
54        label3.setText(str((str("rec cmd handler state:") + str((asr_0.get_command_handler())))))
55
56
57if __name__ == "__main__":
58    try:
59        setup()
60        while True:
61            loop()
62    except (Exception, KeyboardInterrupt) as e:
63        try:
64            from utility import print_error_msg
65
66            print_error_msg(e)
67        except ImportError:
68            print("please update to latest firmware")

Example output:

None

API

ASRUnit

class unit.asr.ASRUnit(id=1, port=None, verbose=False)

Bases: object

Voice recognition hardware module.

Parameters:
  • id (int) – UART port ID for communication. Default is 1.

  • port (list|tuple) – Tuple containing TX and RX pin numbers.

  • verbose (bool)

UiFlow2 Code Block:

init.png

MicroPython Code Block:

from unit import ASRUnit

# Initialize with UART1, TX on pin 2, RX on pin 1
asr = ASRUnit(id=1, port=(1, 2))
get_received_status()

Get message reception status.

Returns:

True if a message is received, False otherwise.

Return type:

bool

UiFlow2 Code Block:

get_received_status.png

MicroPython Code Block:

asr.get_received_status()
send_message(command_num)

Send command via UART.

Parameters:

command_num (int) – Command number to send (0-255)

Return type:

None

UiFlow2 Code Block:

send_message.png

MicroPython Code Block:

asr.send_message(0x30)
get_current_raw_message()

Get the raw message received in hexadecimal format.

Returns:

The raw message as a string in hexadecimal format.

Return type:

str

UiFlow2 Code Block:

get_current_raw_message.png

MicroPython Code Block:

asr.get_current_raw_message()
get_current_command_word()

Get the command word corresponding to the current command number.

Returns:

The command word as a string.

Return type:

str

UiFlow2 Code Block:

get_current_command_word.png

MicroPython Code Block:

asr.get_current_command_word()
get_current_command_num()

Get the current command number.

Returns:

The current command number as a string.

Return type:

str

UiFlow2 Code Block:

get_current_command_num.png

MicroPython Code Block:

asr.get_current_command_num()
get_command_handler()

Check if the current command has an associated handler.

Returns:

True if the command has an associated handler, False otherwise.

Return type:

bool

UiFlow2 Code Block:

get_command_handler.png

MicroPython Code Block:

asr.get_command_handler()
add_command_word(command_num, command_word, event_handler=None)

Register custom command and handler.

Parameters:
  • command_num (int) – Command number (0-255)

  • command_word (str) – Voice command text

  • event_handler (callable) – Handler function

Return type:

None

UiFlow2 Code Block:

add_command_word.png

MicroPython Code Block:

def custom_handler(unit):
    print("Custom command detected!")

asr.add_command_word(0x50, "custom command", custom_handler)
remove_command_word(command_word)

Remove a command word from the command list by its word.

Parameters:

command_word (str) – Command word to remove

Return type:

None

UiFlow2 Code Block:

remove_command_word.png

MicroPython Code Block:

asr.remove_command_word("custom command")
search_command_num(command_word)

Search for the command number associated with a command word.

Parameters:

command_word (str) – Command word to search for

Returns:

The command number if found, otherwise -1

Return type:

int

UiFlow2 Code Block:

search_command_num.png

MicroPython Code Block:

asr.search_command_num("custom command")
search_command_word(command_num)

Search for the command word associated with a command number.

Parameters:

command_num (int) – Command number to search for

Returns:

The command word if found, otherwise “Unknown command word”

Return type:

str

UiFlow2 Code Block:

search_command_word.png

MicroPython Code Block:

asr.search_command_word(0x50)
get_command_list()

Get the list of all commands and their associated handlers.

Returns:

A dictionary of command numbers and their corresponding command words and handlers.

Return type:

dict

UiFlow2 Code Block:

get_command_list.png

MicroPython Code Block:

asr.get_command_list()
check_tick_callback()

Check if a handler is defined for the current command and schedule its execution.

Returns:

The handler if defined, otherwise None

Return type:

None

MicroPython Code Block:

asr.check_tick_callback()