arduino-simple-tts

Loading...
Searching...
No Matches
TextToSpeech.h
1#pragma once
2#ifndef NO_SPEECH
3#include "AudioCodecs/CodecMP3Helix.h"
4#include "AudioTools/AudioOutput.h"
5#include "AudioTools/AudioStreams.h"
6#include "AudioTools/AudioTypes.h"
7#include "NumberToText.h"
8#include "AudioDictionary.h"
9
10namespace simple_tts {
11
18 class TextToSpeech {
19 public:
21 TextToSpeech(SimpleTTSBase &tts, AudioStream &sink, AudioDecoder &decoder,
22 AudioDictionaryBase &dict) {
23 tts.registerCallback(callback, this);
24 p_tts = &tts;
25 p_dictionary = &dict;
26 p_decoder = &decoder;
27 p_sink = &sink;
28 decodedStream = new audio_tools::EncodedAudioStream(&sink, &decoder);
29 begin();
30 }
31
32 TextToSpeech(SimpleTTSBase &tts, AudioOutput &sink, AudioDecoder &decoder,
33 AudioDictionaryBase &dict) {
34 tts.registerCallback(callback, this);
35 p_tts = &tts;
36 p_dictionary = &dict;
37 p_decoder = &decoder;
38 p_sink = &sink;
39 decodedStream = new audio_tools::EncodedAudioStream(&sink, &decoder);
40 begin();
41 }
42
44 TextToSpeech(AudioStream &sink, AudioDecoder &decoder,
45 AudioDictionaryBase &dict) {
46 p_dictionary = &dict;
47 p_decoder = &decoder;
48 p_sink = &sink;
49 decodedStream = new audio_tools::EncodedAudioStream(&sink, &decoder);
50 begin();
51 }
52
53
54 ~TextToSpeech() { delete decodedStream; }
55
57 void say(const char *word) {
58 LOGI("say: %s",word);
59 AudioStream *mp3Stream = p_dictionary->get(word);
60 if (mp3Stream != nullptr) {
61 if (!active) begin();
62 mp3Stream->begin();
63 copier.begin(*decodedStream, *mp3Stream);
64 copier.copyAll(0, 0);
65 copier.end();
66 mp3Stream->end();
67 } else {
68 LOGE("Word not available in dictionary: %s", word);
69 }
70 }
71
72 void begin() {
73 p_decoder->begin();
74 active = true;
75 }
76
77 void end() {
78 p_decoder->end();
79 active = false;
80 }
81
83 void say(audio_tools::Vector<const char *> words) {
84 begin();
85 for (auto word : words) {
86 say(word);
87 }
88 end();
89 }
90
92 void delay(uint32_t delay_ms){
93 uint8_t buffer[1024] = {0};
94 unsigned long timeout = millis()+delay_ms;
95 while(timeout>millis()){
96 p_sink->write((const uint8_t*)buffer,1024);
97 }
98 }
99
100 protected:
101 bool active = false;
102 NumberToText ntt;
103 audio_tools::AudioDecoder *p_decoder = nullptr;
104 audio_tools::EncodedAudioStream *decodedStream = nullptr; // Decoding stream
105 SimpleTTSBase *p_tts = nullptr; // Text source
106 AudioDictionaryBase *p_dictionary = nullptr; // Dictionary to access audio data
107 audio_tools::StreamCopy copier; // copy in to out
108 Print *p_sink=nullptr;
109
111 static void callback(audio_tools::Vector<const char *> words, void* ref) {
112 TextToSpeech* self = (TextToSpeech*)ref;
113 self->say(words); }
114};
115
116} // namespace simple_tts
117
118#endif
simple_tts::AudioDictionaryBase
Dictionary which provides a Stream of Audio for the indicated word.
Definition: SimpleTTSBase.h:49
simple_tts::NumberToText
Translates a number into englich words.
Definition: NumberToText.h:15
simple_tts::SimpleTTSBase
Common Functionality for TTS classes.
Definition: SimpleTTSBase.h:15
simple_tts::TextToSpeech
Audio output from text via the indicated audio sink. The text components need to be prerecorded and a...
Definition: TextToSpeech.h:18
simple_tts::TextToSpeech::TextToSpeech
TextToSpeech(AudioStream &sink, AudioDecoder &decoder, AudioDictionaryBase &dict)
Default Constructor.
Definition: TextToSpeech.h:44
simple_tts::TextToSpeech::say
void say(audio_tools::Vector< const char * > words)
a simple API to say multiple of the supported words
Definition: TextToSpeech.h:83
simple_tts::TextToSpeech::TextToSpeech
TextToSpeech(SimpleTTSBase &tts, AudioStream &sink, AudioDecoder &decoder, AudioDictionaryBase &dict)
TextToSpeech which support a SimpleTTSBase.
Definition: TextToSpeech.h:21
simple_tts::TextToSpeech::delay
void delay(uint32_t delay_ms)
writes silence for the indicated ms
Definition: TextToSpeech.h:92
simple_tts::TextToSpeech::say
void say(const char *word)
a simple API to say one of the supported words
Definition: TextToSpeech.h:57
simple_tts::TextToSpeech::callback
static void callback(audio_tools::Vector< const char * > words, void *ref)
callback which says the words
Definition: TextToSpeech.h:111