Emakefun TTS 2.1
载入中...
搜索中...
未找到
tts.h
1#ifndef EMAKEFUN_TTS_H_
2#define EMAKEFUN_TTS_H_
3
4#include <WString.h>
5#include <Wire.h>
6#include <stdint.h>
7
8namespace emakefun {
9
10/**
11 * @class Tts
12 * @brief 语音合成模块
13 */
14class Tts {
15 public:
16 /**
17 * @brief 语音合成模块默认I2C地址
18 */
19 static constexpr uint8_t kDefaultI2cAddress = 0x40;
20
21 /**
22 * @brief 文本数据最大字节数
23 */
24 static constexpr uint8_t kMaxTextBytesSize = 250;
25
26 /**
27 * @brief 缓存块索引最大值
28 */
29 static constexpr uint8_t kMaxCacheIndex = 15;
30
31 /**
32 * @brief 从缓存合成播放次数的最小值
33 */
34 static constexpr uint8_t kMinSpeechCount = 1;
35
36 /**
37 * @brief 从缓存合成播放次数的最大值
38 */
39 static constexpr uint8_t kMaxSpeechCount = 15;
40
41 /**
42 * @enum ErrorCode
43 * @brief 错误码
44 */
45 enum ErrorCode : uint32_t {
46 kOK = 0, /**< 0:成功 */
47 kI2cDataTooLongToFitInTransmitBuffer = 1, /**< 1:I2C数据太长,无法装入传输缓冲区 */
48 kI2cReceivedNackOnTransmitOfAddress = 2, /**< 2:在I2C发送地址时收到NACK */
49 kI2cReceivedNackOnTransmitOfData = 3, /**< 3:在I2C发送数据时收到NACK */
50 kI2cOtherError = 4, /**< 4:其他I2C错误 */
51 kI2cTimeout = 5, /**< 5:I2C通讯超时 */
52 kInvalidParameter = 6, /**< 6:参数错误 */
53 kUnknownError = 7, /**< 7: 未知错误*/
54 };
55
56 /**
57 * @enum TextEncodingType
58 * @brief 文本编码类型
59 */
60 enum TextEncodingType : uint8_t {
61 kGb23212 = 0x00, /**< GB2312 */
62 kGbk = 0x01, /**< GBK */
63 kBig5 = 0x02, /**< BIG5 */
64 kUtf16le = 0x03, /**< UTF16LE */
65 kUtf8 = 0x04, /**< UTF8 */
66 };
67
68 /**
69 * @brief 构造函数
70 * @param di2c_address 语音合成模块I2C地址,默认为0x40
71 */
72 explicit Tts(const uint8_t i2c_address = kDefaultI2cAddress, TwoWire& wire = Wire);
73
74 explicit Tts(TwoWire& wire) : Tts(kDefaultI2cAddress, wire) {
75 }
76
77 /**
78 * @brief 初始化函数
79 * @return 返回值请参考 @ref ErrorCode
80 */
82
83 /**
84 * @brief 文本转语音并播放
85 * @param[in] text 文本数据,数据长度不大于250个字节
86 * @param[in] text_encoding_type 文本编码类型,参考 @ref TextEncodingType, 默认为 @ref kUtf8
87 * @return 返回值请参考 @ref ErrorCode
88 */
89 ErrorCode Play(const String& text, const TextEncodingType text_encoding_type = kUtf8);
90
91 /**
92 * @brief 从缓存块0的文本开始转语音并播放
93 * @param[in] text_encoding_type 文本编码类型,参考 @ref TextEncodingType,默认为 @ref kUtf8
94 * @param[in] synthesizing_count 合成播放次数,范围 1 ~ 15
95 * @return 返回值请参考 @ref ErrorCode
96 */
97 ErrorCode PlayFromCache(const TextEncodingType text_encoding_type = kUtf8, uint8_t count = kMinSpeechCount);
98
99 /**
100 * @brief 将文本内容上传到指定缓存块
101 * @param[in] text 文本数据,数据长度不大于250个字节
102 * @param[in] cache_index 缓存块索引,范围 0 ~ 15
103 * @return 返回值请参考 @ref ErrorCode
104 */
105 ErrorCode PushTextToCache(const String& text, const uint8_t cache_index);
106
107 /**
108 * @brief 停止播放
109 * @return 返回值请参考 @ref ErrorCode
110 */
112
113 /**
114 * @brief 暂停播放
115 * @return 返回值请参考 @ref ErrorCode
116 */
118
119 /**
120 * @brief 恢复播放
121 * @return 返回值请参考 @ref ErrorCode
122 */
124
125 private:
126 Tts(const Tts&) = delete;
127 Tts& operator=(const Tts&) = delete;
128 ErrorCode I2cWrite(const uint8_t* data, const uint8_t length);
129
130 const uint8_t i2c_address_ = kDefaultI2cAddress;
131 TwoWire& wire_ = Wire;
132};
133} // namespace emakefun
134
135#endif
语音合成模块
定义 tts.h:14
static constexpr uint8_t kMaxTextBytesSize
文本数据最大字节数
定义 tts.h:24
ErrorCode PlayFromCache(const TextEncodingType text_encoding_type=kUtf8, uint8_t count=kMinSpeechCount)
从缓存块0的文本开始转语音并播放
static constexpr uint8_t kMaxCacheIndex
缓存块索引最大值
定义 tts.h:29
static constexpr uint8_t kMinSpeechCount
从缓存合成播放次数的最小值
定义 tts.h:34
static constexpr uint8_t kMaxSpeechCount
从缓存合成播放次数的最大值
定义 tts.h:39
ErrorCode Stop()
停止播放
ErrorCode Pause()
暂停播放
ErrorCode Play(const String &text, const TextEncodingType text_encoding_type=kUtf8)
文本转语音并播放
ErrorCode PushTextToCache(const String &text, const uint8_t cache_index)
将文本内容上传到指定缓存块
Tts(const uint8_t i2c_address=kDefaultI2cAddress, TwoWire &wire=Wire)
构造函数
ErrorCode
错误码
定义 tts.h:45
@ kI2cOtherError
定义 tts.h:50
@ kOK
定义 tts.h:46
@ kI2cDataTooLongToFitInTransmitBuffer
定义 tts.h:47
@ kI2cReceivedNackOnTransmitOfAddress
定义 tts.h:48
@ kI2cReceivedNackOnTransmitOfData
定义 tts.h:49
@ kI2cTimeout
定义 tts.h:51
@ kUnknownError
定义 tts.h:53
@ kInvalidParameter
定义 tts.h:52
ErrorCode Initialize()
初始化函数
ErrorCode Resume()
恢复播放
static constexpr uint8_t kDefaultI2cAddress
语音合成模块默认I2C地址
定义 tts.h:19
TextEncodingType
文本编码类型
定义 tts.h:60
@ kUtf16le
定义 tts.h:64
@ kGbk
定义 tts.h:62
@ kBig5
定义 tts.h:63
@ kGb23212
定义 tts.h:61
@ kUtf8
定义 tts.h:65