arduino-simple-tts
Loading...
Searching...
No Matches
TextToSpeechQueue.h
1#pragma once
2#ifndef NO_SPEECH
3#include <list>
4
5#include "AudioCodecs/CodecMP3Helix.h"
6#include "AudioDictionary.h"
7#include "AudioTools/AudioOutput.h"
8#include "AudioTools/AudioStreams.h"
9#include "AudioTools/AudioTypes.h"
10#include "NumberToText.h"
11
12namespace simple_tts {
13
21 public:
23 TextToSpeechQueue(SimpleTTSBase &tts, AudioStream &sink,
24 AudioDecoder &decoder, AudioDictionaryBase &dict) {
25 tts.registerCallback(callback, this);
26 p_tts = &tts;
27 p_dictionary = &dict;
28 p_decoder = &decoder;
29 p_sink = &sink;
30 decodedStream = new audio_tools::EncodedAudioStream(&sink, &decoder);
31 begin();
32 }
33
34 TextToSpeechQueue(SimpleTTSBase &tts, AudioOutput &sink, AudioDecoder &decoder,
35 AudioDictionaryBase &dict) {
36 tts.registerCallback(callback, this);
37 p_tts = &tts;
38 p_dictionary = &dict;
39 p_decoder = &decoder;
40 p_sink = &sink;
41 decodedStream = new audio_tools::EncodedAudioStream(&sink, &decoder);
42 begin();
43 }
44
46 TextToSpeechQueue(AudioStream &sink, AudioDecoder &decoder,
47 AudioDictionaryBase &dict) {
48 p_dictionary = &dict;
49 p_decoder = &decoder;
50 p_sink = &sink;
51 decodedStream = new audio_tools::EncodedAudioStream(&sink, &decoder);
52 begin();
53 }
54
55 ~TextToSpeechQueue() { delete decodedStream; }
56
58 void say(const char *word) {
59 if (word != nullptr) {
60 LOGI("%s", word);
61 queue.push_back(word);
62 }
63 }
64
66 void sayNow(const char *word) {
67 if (word != nullptr) {
68 queue.push_front(word);
69 }
70 }
71
73 void say(const char *word[], int size) {
74 for (int j = 0; j < size; j++) {
75 say(word[j]);
76 }
77 }
78
80 template <size_t N>
81 void say(char *(&word)[N]) {
82 for (int j = 0; j < N; j++) {
83 say(word[j]);
84 }
85 }
86
88 void say(audio_tools::Vector<const char *> words) {
89 for (auto word : words) {
90 say(word);
91 }
92 }
93
94 // Add this in the Arduino Loop -> we process the next word
95 void process() {
96 int size = queue.size();
97 if (size > 0) {
98 const char *word = (*this)[0];
99 queue.pop_front();
100
101 LOGI("say: %s (size: %d -> %d)", word, size, queue.size());
102 processWord(word);
103 if (queue.size() == 0) {
104 // end();
105 silence(1);
106 }
107 }
108 }
109
111 void processWord(const char *word) {
112 AudioStream *mp3Stream = p_dictionary->get(word);
113 if (mp3Stream != nullptr) {
114 if (!active) {
115 begin();
116 }
117 // decode to audio
118 mp3Stream->begin();
119 copier.begin(*decodedStream, *mp3Stream);
120 copier.copyAll();
121 copier.end();
122 mp3Stream->end();
123
124 } else {
125 LOGE("Word not available in dictionary: %s", word);
126 }
127 }
128
130 void silence(int n = 1) {
131 for (int j = 0; j < n; j++) {
132 processWord("SILENCE");
133 }
134 }
135
137 size_t size() { return queue.size(); }
138
140 bool isEmpty() { return size() == 0; }
141
143 const char *operator[](int n) {
144 if (n < size()) {
145 // auto it = queue.begin();
146 // std::advance(it, n);
147 // return *it;
148 return queue[n];
149 }
150 return nullptr;
151 }
152
154 void begin() {
155 p_decoder->begin();
156 active = true;
157 }
158
160 void end() {
161 p_decoder->end();
162 active = false;
163 clear();
164 }
165
167 void clear() { queue.clear(); }
168
170 void delay(uint32_t delay_ms = 1000) {
171 uint8_t buffer[1024] = {0};
172 unsigned long timeout = millis() + delay_ms;
173 while (timeout > millis()) {
174 p_sink->write((const uint8_t *)buffer, 1024);
175 }
176 }
177
178 protected:
179 // std::list<const char *> queue;
180 Vector<const char *> queue;
181 bool active = false;
182 NumberToText ntt;
183 audio_tools::AudioDecoder *p_decoder = nullptr;
184 audio_tools::EncodedAudioStream *decodedStream = nullptr; // Decoding stream
185 SimpleTTSBase *p_tts = nullptr; // Text source
186 AudioDictionaryBase *p_dictionary =
187 nullptr; // Dictionary to access audio data
188 audio_tools::StreamCopy copier; // copy in to out
189 Print *p_sink = nullptr;
190
192 static void callback(audio_tools::Vector<const char *> words, void *ref) {
194 self->say(words);
195 }
196};
197
198} // namespace simple_tts
199
200#endif
Dictionary which provides a Stream of Audio for the indicated word.
Definition: SimpleTTSBase.h:49
Translates a number into englich words.
Definition: NumberToText.h:15
Common Functionality for TTS classes.
Definition: SimpleTTSBase.h:15
Audio output from text via the indicated audio sink. The text components need to be prerecorded and a...
Definition: TextToSpeechQueue.h:20
void silence(int n=1)
Sends silence to mp3 decoder for n secods.
Definition: TextToSpeechQueue.h:130
void delay(uint32_t delay_ms=1000)
writes silence for the indicated ms
Definition: TextToSpeechQueue.h:170
void say(const char *word)
a simple API to add a single c string to the queue
Definition: TextToSpeechQueue.h:58
void processWord(const char *word)
Output of word to audio sink.
Definition: TextToSpeechQueue.h:111
bool isEmpty()
Returns true if the queue is empty.
Definition: TextToSpeechQueue.h:140
void end()
Ends the processing and clears the queue.
Definition: TextToSpeechQueue.h:160
void say(char *(&word)[N])
Addds an array of c strings.
Definition: TextToSpeechQueue.h:81
TextToSpeechQueue(AudioStream &sink, AudioDecoder &decoder, AudioDictionaryBase &dict)
Default Constructor.
Definition: TextToSpeechQueue.h:46
static void callback(audio_tools::Vector< const char * > words, void *ref)
callback which adds the words to the queue
Definition: TextToSpeechQueue.h:192
void begin()
Opens the processing.
Definition: TextToSpeechQueue.h:154
TextToSpeechQueue(SimpleTTSBase &tts, AudioStream &sink, AudioDecoder &decoder, AudioDictionaryBase &dict)
TextToSpeech which support a SimpleTTSBase.
Definition: TextToSpeechQueue.h:23
void sayNow(const char *word)
Adds a word to the front.
Definition: TextToSpeechQueue.h:66
void clear()
Clears the queue.
Definition: TextToSpeechQueue.h:167
size_t size()
Returns the number of words to be spoken in the queue.
Definition: TextToSpeechQueue.h:137
void say(audio_tools::Vector< const char * > words)
a simple API to say multiple of the supported words
Definition: TextToSpeechQueue.h:88
const char * operator[](int n)
Determines the nth word in the queue.
Definition: TextToSpeechQueue.h:143
void say(const char *word[], int size)
Addds an array of c strings.
Definition: TextToSpeechQueue.h:73