Emakefun Speech Recognizer 2.1
载入中...
搜索中...
未找到
speech_recognizer.h
1#pragma once
2
3#include <WString.h>
4#include <Wire.h>
5#include <stdint.h>
6
7namespace emakefun {
8
9/**
10 * @brief 语音识别模块类
11 */
13 public:
14 /**
15 * @brief 语音识别模块默认I2C地址
16 */
17 static constexpr uint8_t kDefaultI2cAddress = 0x30;
18
19 /**
20 * @brief 每条关键词最大字符数
21 */
22 static constexpr uint8_t kMaxKeywordDataBytes = 50;
23
24 /**
25 * @brief 识别模式
26 */
27 enum RecognitionMode : uint8_t {
28 kRecognitionAuto, ///< 自动识别模式
29 kButtonTrigger, ///< 按键触发识别模式
30 kKeywordTrigger, ///< 关键词触发识别模式
31 kKeywordOrButtonTrigger, ///< 按键或关键词触发识别模式
32 };
33
34 /**
35 * @brief 事件类型
36 */
37
38 enum Event : uint8_t {
39 kEventNone = 0, ///< 无事件
40 kEventStartWaitingForTrigger, ///< 开始等待触发
41 kEventButtonTriggered, ///< 被按键触发
42 kEventKeywordTriggered, ///< 被关键词触发
43 kEventStartRecognizing, ///< 开始识别
44 kEventSpeechRecognized, ///< 识别成功
46 };
47
48 /**
49 * @brief 构造函数
50 * @param i2c_address 语音识别模块I2C地址,默认为0x30
51 */
52 SpeechRecognizer(const uint8_t i2c_address = kDefaultI2cAddress);
53
54 /**
55 * @brief 初始化函数
56 * @param[in] wire Wire对象,用于I2C通讯,可选,默认使用Arduino标准的Wire对象进行I2C通讯
57 * @return 初始化结果
58 * @retval true 成功
59 * @retval false 失败,如I2C无法与语音识别模块通讯
60 */
61 int32_t Initialize(TwoWire* const wire = &Wire);
62
63 /**
64 * @brief 设置识别模式
65 * @param[in] recognition_mode 识别模式,参考枚举: @ref RecognitionMode
66 */
67 void SetRecognitionMode(const RecognitionMode recognition_mode);
68
69 /**
70 * @brief 设置识别超时时间, 对自动触发模式( @ref kRecognitionAuto )无效
71 * @param[in] timeout_ms 超时时间,单位毫秒
72 */
73 void SetTimeout(const uint32_t timeout_ms);
74
75 /**
76 * @brief 添加语音识别关键词
77 * @details 最多可以添加50个词,每个词最大长度为50个字节
78 * @param[in] index 索引,范围 0 ~ 255,多个词可以共用一个索引,当在 [关键字触发识别模式]( @ref kKeywordTrigger
79 * ) 或 [按键或关键词触发识别模式]( @ref kKeywordOrButtonTrigger ) 下,索引 0 会被当做关键词
80 * @param[in] keyword 关键词,String类型,最大长度为50 ( 参考定义: @ref kMaxKeywordDataBytes ) 个字节
81 */
82 void AddKeyword(const uint8_t index, const String& keyword);
83
84 /**
85 * @brief 进行语音识别
86 * @details 在loop函数中<b>循环调用</b>该函数以推进语音识别模块的工作,
87 * 调用该函数后可以获取识别结果,可以通过( @ref GetEvent )获取事件
88 * @return 关键词的索引值
89 * @retval <0 未识别到结果
90 * @retval >=0 识别到关键词的索引值,对应 @ref AddKeyword 时设置的关键词索引
91 */
92 int16_t Recognize();
93
94 /**
95 * @brief 获取当前事件
96 * @return 事件类型,参考枚举: @ref Event
97 */
99
100 private:
101 int32_t WaitUntilIdle();
102 TwoWire* wire_ = nullptr;
103 const uint8_t i2c_address_ = 0;
104 SpeechRecognizer(const SpeechRecognizer&) = delete;
105 SpeechRecognizer& operator=(const SpeechRecognizer&) = delete;
106};
107} // namespace emakefun
语音识别模块类
Definition speech_recognizer.h:12
static constexpr uint8_t kDefaultI2cAddress
语音识别模块默认I2C地址
Definition speech_recognizer.h:17
Event GetEvent()
获取当前事件
Definition speech_recognizer.cpp:116
int32_t Initialize(TwoWire *const wire=&Wire)
初始化函数
Definition speech_recognizer.cpp:44
RecognitionMode
识别模式
Definition speech_recognizer.h:27
@ kButtonTrigger
按键触发识别模式
Definition speech_recognizer.h:29
@ kKeywordTrigger
关键词触发识别模式
Definition speech_recognizer.h:30
@ kRecognitionAuto
自动识别模式
Definition speech_recognizer.h:28
@ kKeywordOrButtonTrigger
按键或关键词触发识别模式
Definition speech_recognizer.h:31
static constexpr uint8_t kMaxKeywordDataBytes
每条关键词最大字符数
Definition speech_recognizer.h:22
int16_t Recognize()
进行语音识别
Definition speech_recognizer.cpp:99
void AddKeyword(const uint8_t index, const String &keyword)
添加语音识别关键词
Definition speech_recognizer.cpp:76
void SetRecognitionMode(const RecognitionMode recognition_mode)
设置识别模式
Definition speech_recognizer.cpp:60
void SetTimeout(const uint32_t timeout_ms)
设置识别超时时间, 对自动触发模式( kRecognitionAuto )无效
Definition speech_recognizer.cpp:68
Event
事件类型
Definition speech_recognizer.h:38
@ kEventNone
无事件
Definition speech_recognizer.h:39
@ kEventStartWaitingForTrigger
开始等待触发
Definition speech_recognizer.h:40
@ kEventStartRecognizing
开始识别
Definition speech_recognizer.h:43
@ kEventSpeechRecognized
识别成功
Definition speech_recognizer.h:44
@ kEventSpeechRecognitionTimedOut
识别超时
Definition speech_recognizer.h:45
@ kEventButtonTriggered
被按键触发
Definition speech_recognizer.h:41
@ kEventKeywordTriggered
被关键词触发
Definition speech_recognizer.h:42