arduino-simple-tts

Loading...
Searching...
No Matches
NumberToText.h
1#pragma once
2#include "SimpleTTSBase.h"
3#include "AudioBasic/Collections/Vector.h"
4#include <stdio.h>
5#include <string.h>
6#include <stdlib.h>
7
8namespace simple_tts {
9
15 class NumberToText : public SimpleTTSBase {
16 public:
17
19 audio_tools::Vector<const char*> &say(double value, int decimals=2) {
20 Number number;
21 number.set(value, decimals);
22 return say(number.intValue(), number.decValues());
23 }
24
25 audio_tools::Vector<const char*> &say(int64_t wholeNumber) {
26 Number number;
27 number.set(wholeNumber);
28 return say(number.intValue(), number.decValues());
29 }
30
32 audio_tools::Vector<const char*> &say(const char* wholeNumber, const char* decimals="") {
33 result.clear();
34
35 LOGI("say(number='%s', decimal='%s')",wholeNumber, decimals);
36 Str wn(wholeNumber);
37 wn.trim();
38 if (wn.startsWith("-")){
39 add(third[MINUS]);
40 }
41
42 // number before decimals
43 convert(wholeNumber);
44
45 // add decimals
46 if (Str(decimals).toLong()>0l){
47 convertDecimals(decimals);
48 }
49
50 // provide result to callback
51 if (callback){
52 callback(result, reference);
53 }
54 return result;
55 }
56
58 audio_tools::Vector<const char*> &allTexts() {
59 result.clear();
60 for (int j=0;j<20;j++){
61 add(first[j]);
62 }
63 for (int j=0;j<10;j++){
64 add(second[j]);
65 }
66 for (int j=0;j<7;j++){
67 add(third[j]);
68 }
69 return result;
70 }
71
72 protected:
73 audio_tools::Vector<const char*> result;
74 const char* first[20] = {"ZERO", "ONE", "TWO", "THREE",
75 "FOUR", "FIVE", "SIX", "SEVEN",
76 "EIGHT", "NINE", "TEN", "ELEVEN",
77 "TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN",
78 "SIXTEEN", "SEVENTEEN", "EIGHTEEN", "NINETEEN"};
79
80 const char* second[10] = {"", "TEN", "TWENTY", "THIRTY", "FORTY",
81 "FIFTY", "SIXTY", "SEVENTY", "EIGHTY", "NINETY"};
82
83 const char* third[7] = {"AND","DOT","MINUS", "HUNDRED", "THOUSAND", "MILLION","BILLION"};
84 const int AND = 0;
85 const int DOT = 1;
86 const int MINUS = 2;
87 const int HUNDRED = 3;
88 const int THOUSAND = 4;
89 const int MILLION = 5;
90 const int BILLION = 6;
91
92 void add(const char* str){
93 result.push_back(str);
94 }
95
96 void convertDecimals(const char* decimals) {
97 Str dec(decimals);
98 if (dec.toInt()!=0.0){
99 const char* ch=decimals;
100 add(third[DOT]);
101 while (*ch){
102 int idx = (*ch)-48; // convert to index
103 add(first[idx]);
104 ch++;
105 }
106 }
107 }
108 void convert(const char* strValue){
109 convert(Str(strValue).toLong());
110 }
111
112 // TODO: convert to string operations
113 void convert(int64_t value){
114 if (value < 0) {
115 convert(-value);
116 } else if (value >= 1000000000) {
117 convert(value / 1000000000);
118 add(third[BILLION]);
119 int64_t remainder = value%1000000000;
120 if (remainder) convert(remainder);
121 } else if (value >= 1000000) {
122 convert(value / 1000000);
123 add(third[MILLION]);
124 int64_t remainder = value%1000000;
125 if (remainder) convert(remainder);
126 } else if (value >= 1000) {
127 convert(value / 1000);
128 add(third[THOUSAND]);
129 int64_t remainder = value%1000;
130 if (remainder) convert(remainder);
131 } else if (value >= 100) {
132 convert(value / 100);
133 add(third[HUNDRED]);
134 int64_t remainder = value%100;
135 if (remainder) convert(remainder);
136 } else if (value >= 20) {
137 add(second[value / 10]);
138 if (value % 10) {
139 convert(value % 10);
140 }
141 } else {
142 add(first[value]);
143 }
144 }
145};
146
147} // namespace simple_tts
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::NumberToText
Translates a number into englich words.
Definition: NumberToText.h:15
simple_tts::NumberToText::say
audio_tools::Vector< const char * > & say(const char *wholeNumber, const char *decimals="")
converts a number to it's text representation
Definition: NumberToText.h:32
simple_tts::NumberToText::say
audio_tools::Vector< const char * > & say(double value, int decimals=2)
converts a real number to it's text representation (with the indicated number of digits)
Definition: NumberToText.h:19
simple_tts::NumberToText::allTexts
audio_tools::Vector< const char * > & allTexts()
provides all texts
Definition: NumberToText.h:58
simple_tts::SimpleTTSBase
Common Functionality for TTS classes.
Definition: SimpleTTSBase.h:15