Skip to content

Commit 42dba22

Browse files
committed
Vector was moved to Collections
1 parent 0553b8c commit 42dba22

File tree

3 files changed

+83
-7
lines changed

3 files changed

+83
-7
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* @file tts-udp-server.h
3+
* @author Phil Schatzmann
4+
* @brief A simple UDP server which speaks out words
5+
* @version 0.1
6+
* @date 2022-03-17
7+
*
8+
* @copyright Copyright (c) 2022
9+
*
10+
*/
11+
12+
#include "SimpleTTS.h"
13+
#include "AudioCodecs/CodecMP3Helix.h"
14+
#include "AudioLibs/Communication.h"
15+
16+
I2SStream out;
17+
VolumeStream volume(out);
18+
19+
MP3DecoderHelix mp3;
20+
AudioDictionary dictionary(ExampleAudioDictionaryValues);
21+
NumberToText ntt;
22+
TextToSpeechQueue queue(ntt, volume, mp3, dictionary);
23+
UDPStream udp;
24+
char buffer[160];
25+
26+
// Provides a fixed string from dictionary for a temporary input string
27+
const char *toName(char *str) {
28+
for (auto e : ExampleAudioDictionaryValues) {
29+
Str str1(str);
30+
str1.trim();
31+
if (str1.equalsIgnoreCase(e.name)) {
32+
return e.name;
33+
}
34+
}
35+
return nullptr;
36+
}
37+
38+
void setup() {
39+
Serial.begin(115200);
40+
AudioLogger::instance().begin(Serial, AudioLogger::Info);
41+
42+
// setting the volume
43+
volume.setVolume(0.6);
44+
45+
// setup out
46+
auto cfg = out.defaultConfig();
47+
cfg.sample_rate = 24000;
48+
cfg.channels = 1;
49+
out.begin(cfg);
50+
}
51+
52+
void loop() {
53+
// fill queue with words from UDP
54+
while (udp.available()) {
55+
if (udp.readBytesUntil(' ', (uint8_t*)buffer, 160) > 0) {
56+
Str bufferStr((const char*)buffer);
57+
bufferStr.trim();
58+
if (bufferStr.isNumber()) {
59+
double value = bufferStr.toDouble();
60+
int decimals = bufferStr.numberOfDecimals();
61+
for (auto str : ntt.say(value, decimals)) {
62+
queue.say(toName((char*)str));
63+
}
64+
} else {
65+
queue.say(toName(buffer));
66+
}
67+
}
68+
}
69+
// play next word in queue
70+
queue.process();
71+
}

src/NumberToText.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22
#include "SimpleTTSBase.h"
3-
#include "AudioBasic/Vector.h"
3+
#include "AudioBasic/Collections/Vector.h"
44
#include <stdio.h>
55
#include <string.h>
66
#include <stdlib.h>

src/TextToSpeechQueue.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,18 @@ class TextToSpeechQueue {
5656

5757
/// a simple API to add a single c string to the queue
5858
void say(const char *word) {
59-
LOGI("%s", word);
60-
queue.push_back(word);
59+
if (word != nullptr) {
60+
LOGI("%s", word);
61+
queue.push_back(word);
62+
}
6163
}
6264

6365
/// Adds a word to the front
64-
void sayNow(const char *word) { queue.push_front(word); }
66+
void sayNow(const char *word) {
67+
if (word != nullptr) {
68+
queue.push_front(word);
69+
}
70+
}
6571

6672
/// Addds an array of c strings
6773
void say(const char *word[], int size) {
@@ -121,8 +127,8 @@ class TextToSpeechQueue {
121127
}
122128

123129
/// Sends silence to mp3 decoder for n secods
124-
void silence(int n=1){
125-
for (int j=0;j<n;j++){
130+
void silence(int n = 1) {
131+
for (int j = 0; j < n; j++) {
126132
processWord("SILENCE");
127133
}
128134
}
@@ -182,7 +188,6 @@ class TextToSpeechQueue {
182188
audio_tools::StreamCopy copier; // copy in to out
183189
Print *p_sink = nullptr;
184190

185-
186191
/// callback which adds the words to the queue
187192
static void callback(audio_tools::Vector<const char *> words, void *ref) {
188193
TextToSpeechQueue *self = (TextToSpeechQueue *)ref;

0 commit comments

Comments
 (0)