arduino-simple-tts

Loading...
Searching...
No Matches
SimpleTTSBase.h
1#pragma once
2#include <stdio.h>
3#include <string.h>
4#include <stdlib.h>
5
6#include "AudioTools.h"
7
8namespace simple_tts {
9
15 class SimpleTTSBase {
16 public:
17 virtual audio_tools::Vector<const char*>& allTexts() = 0;
18 virtual void registerCallback(
19 void (*ptr)(audio_tools::Vector<const char*> words, void* refx),
20 void* ref) {
21 callback = ptr;
22 reference = ref;
23 }
24
25 // Creates all missing audio recording files for the indicated source. We make sure
26 // that the output is lowercase
27 void printCSV(Print &out) {
28 for (auto txt : allTexts()) {
29 if (txt!=nullptr && strlen(txt)>0){
30 StrExt str = txt;
31 str.toLowerCase(); // convert txt to lowercase
32 out.print(str.c_str());
33 out.print(",");
34 out.println(str.c_str());
35 }
36 }
37 }
38
39 protected:
40 void (*callback)(audio_tools::Vector<const char*> word, void* ref) = nullptr;
41 void* reference = nullptr;
42};
43
49 class AudioDictionaryBase {
50 public:
51 virtual AudioStream* get(const char* word) = 0;
52};
53
60 struct AudioDictionaryEntry {
61 const char* name;
62 AudioStream* audio;
63};
64
72 struct AudioSDEntry {
73 const char* name;
74 const char* text = nullptr; // optional - when different from name
75};
76
83 struct SimpleTime {
84 int hour = 0;
85 int minute = 0;
86 SimpleTime() = default;
87 SimpleTime(int hour, int minutes) {
88 this->hour = hour;
89 this->minute = minutes;
90 }
91 bool operator==(const SimpleTime& alt) {
92 return alt.hour == hour && alt.minute == minute;
93 }
94 bool operator!=(const SimpleTime& alt) {
95 return alt.hour != hour || alt.minute != minute;
96 }
97};
98
106 class Number {
107public:
108 void set(double value, int digits=2) {
109 char format[10];
110 // e.g. %0.2f for 2 digits.
111 sprintf(format,"%%0.%df", digits);
112 LOGD("format: %s", format);
113 memset(buffer, 0, buffer_len);
114 // convert to string
115 sprintf(buffer, format, value);
116 LOGD("number: %s",buffer);
117 dot = strchr(buffer, '.');
118 if (dot!=nullptr){
119 // split string
120 *dot = 0;
121 } else {
122 // no decimal
123 dot = buffer+strlen(buffer);
124 }
125
126 LOGD("int: %s", intValue());
127 LOGD("dec: %s", decValues());
128 }
129
130 void set(int64_t wholeNumber) {
131 memset(buffer,0,buffer_len);
132 // convert to string
133 sprintf(buffer, "%lld", wholeNumber);
134 //ltoa(wholeNumber, buffer, 10);
135 dot = buffer+strlen(buffer);
136 }
137
139 const char* intValue() {
140 return buffer;
141 }
142
144 const char* decValues() {
145 return dot+1;
146 }
147
148 // converts a decimal to a full number: 1 gives 10, 01 dives 1 if digits are 2. This is used e.g. for USD 1.1 which
149 // will need to render 1 dollar and 10 cents; attn: 1.101 should still give 10 cents!
150 const char* decAsInt(const char* decimals, int digits){
151 memset(buffer, 0, buffer_len);
152 memset(buffer,'0', digits);
153 int len = min((int)strlen(decimals),digits);
154 memcpy(buffer, decimals, len);
155 return buffer;
156 }
157
158protected:
159 // convert to string
160 static const int buffer_len = 100;
161 char buffer[buffer_len];
162 char* dot=buffer;
163
164};
165
166} // namespace simple_tts
simple_tts::AudioDictionaryBase
Dictionary which provides a Stream of Audio for the indicated word.
Definition: SimpleTTSBase.h:49
simple_tts::Number
Convert numbers to string and provide integer and decimals.
Definition: SimpleTTSBase.h:106
simple_tts::Number::intValue
const char * intValue()
provides the full number
Definition: SimpleTTSBase.h:139
simple_tts::Number::decValues
const char * decValues()
provides the decimals after the .
Definition: SimpleTTSBase.h:144
simple_tts::SimpleTTSBase
Common Functionality for TTS classes.
Definition: SimpleTTSBase.h:15
simple_tts::AudioDictionaryEntry
An Entry for the in Memory Audio Dictionary.
Definition: SimpleTTSBase.h:60
simple_tts::AudioSDEntry
An Entry for the in Memory Audio Dictionary which contains the full text.
Definition: SimpleTTSBase.h:72
simple_tts::SimpleTime
Just a simple structure with the hour and minutes.
Definition: SimpleTTSBase.h:83