Arduino ESpeak-NG
espeak.h
Go to the documentation of this file.
1 #pragma once
2 #include "config-espk.h"
3 #include "speak_lib.h"
4 #include "pcaudiolib/audio_object.h"
5 #include "espeak-ng-data.h"
6 
7 #if USE_CPP_API
8 #include "FileSystems.h" // https://github.com/pschatzmann/arduino-posix-fs
9 #include "Print.h"
10 #define GUARD_VALUE 23
14 class ESpeakFiles {
15 public:
16  ESpeakFiles(Print &out, const char* path="../../../espeak-ng-data"){
17  // setup min file system
19  if (path!=nullptr){
20  this->path = path;
21  }
22  }
23 
26 
28  bool begin(int buflength=5) {
29  int sample_rate = espeak_Initialize(output, buflength, path, options);
30  return sample_rate!=-1;
31  }
32 
33  // defines a callback which is executed at the begin of the audio output
34  void setOutputBeginCallback(void(*cb)()){
36  }
37 
38  // defines a callback which is executed at the end of the audio output
39  void setOutputEndCallback(void(*cb)()){
41  }
42 
44  bool setVoice(const char *voiceName){
45  espeak_ERROR rc = espeak_SetVoiceByName(voiceName);
46  return rc==EE_OK;
47  }
48 
50  bool setVoice(espeak_VOICE *voice_spec){
51  espeak_ERROR rc = espeak_SetVoiceByProperties (voice_spec);
52  return rc==EE_OK;
53  }
54 
57  return espeak_GetCurrentVoice ();
58  }
59 
61  void end() {
63  }
64 
66  bool say(const char* str){
67  if (str==nullptr) return false;
68  assert(memory_guard==GUARD_VALUE);
71  assert(memory_guard==GUARD_VALUE);
72  return rc==EE_OK;
73  }
74 
76  bool setRate(int rate) {
77  return setParameter(espeakRATE, rate);
78  }
79 
81  bool setVolume(int vol){
82  return setParameter(espeakVOLUME, vol);
83  }
84 
86  bool setPitch(int pitch){
87  return setParameter(espeakPITCH, pitch);
88  }
89 
91  bool setPitchRange(int range){
92  return setParameter(espeakRANGE, range);
93  }
94 
97  return setParameter(espeakPUNCTUATION, punct);
98  }
99 
101  bool setAnnounceCapitals(int caps){
102  return setParameter(espeakCAPITALS, caps);
103  }
104 
106  bool setWordGap(int gap){
107  return setParameter(espeakWORDGAP, gap);
108  }
109 
111  bool setParameter(espeak_PARAMETER parameter, int value, int relative=0){
112  return espeak_SetParameter(parameter, value, relative)==EE_OK;
113  }
114 
116  bool setFlags(int flag){
117  flags = flag;
118  return true;
119  }
120 
121 protected:
122  const short memory_guard = GUARD_VALUE; // check that memory was not overwritten by stack overflow
124  const char* path = "../../../espeak-ng-data-min";
125  void *user_data = nullptr;
126  int options = 0;
127  unsigned int *identifier = nullptr;
128  unsigned int position = 0;
129  unsigned int end_position = 0;
130  unsigned int flags = espeakCHARS_AUTO;
132 };
138 class ESpeak : public ESpeakFiles {
139 public:
140  ESpeak(Print &out, bool setupEnglish=true) : ESpeakFiles(out, "/mem/data"){
141  // setup min file system
142  is_setup_english = setupEnglish;
143  }
145  void add(const char* fileName, const void* fileContent, size_t len){
146  fsm.add(fileName, fileContent, len);
147  }
148 
149  bool begin(int buflength=500) {
150  if (!is_fs_setup){
151  add("/mem/data/phontab", espeak_ng_data_phontab,espeak_ng_data_phontab_len);
152  add("/mem/data/phonindex", espeak_ng_data_phonindex,espeak_ng_data_phonindex_len);
153  add("/mem/data/phondata", espeak_ng_data_phondata,espeak_ng_data_phondata_len);
154  add("/mem/data/intonations", espeak_ng_data_intonations,espeak_ng_data_intonations_len);
155  if (is_setup_english){
156  add("/mem/data/en_dict", espeak_ng_data_en_dict,espeak_ng_data_en_dict_len);
157  add("/mem/data/lang/en", espeak_ng_data_lang_gmw_en, espeak_ng_data_lang_gmw_en_len);
158  }
159  is_fs_setup = true;
160  }
161  return ESpeakFiles::begin(buflength);
162  }
163 
164 protected:
165  file_systems::FileSystemMemory fsm{"/mem"}; // File system data in PROGMEM
167  bool is_fs_setup = false;
168 };
169 
170 #endif
void(* audio_stream_start_callback)()
void espeak_set_audio_output(Print *p)
Defines the audio output.
audio_info espeak_get_audio_info()
Provides the audio information.
void(* audio_stream_stop_callback)()
Simple Arduino C++ class API for ESpeak using files.
Definition: espeak.h:14
bool setFlags(int flag)
sets the flags espeakCHARS_UTF8,espeakCHARS_8BIT,espeakCHARS_WCHAR (by default we use espeakCHARS_AUT...
Definition: espeak.h:116
unsigned int flags
Definition: espeak.h:130
bool setPunctuation(espeak_PUNCT_TYPE punct)
PUNCTUATION: which punctuation characters to announce: value in espeak_PUNCT_TYPE (none,...
Definition: espeak.h:96
void end()
Stops the processing.
Definition: espeak.h:61
const short memory_guard
Definition: espeak.h:122
espeak_VOICE * voice()
Determiens the current voice.
Definition: espeak.h:56
bool setParameter(espeak_PARAMETER parameter, int value, int relative=0)
Sets Parameter: espeakRATE, espeakVOLUME etc.
Definition: espeak.h:111
bool setPitch(int pitch)
PITCH: base pitch, range 0-100. 50=normal.
Definition: espeak.h:86
const espeak_AUDIO_OUTPUT output
Definition: espeak.h:123
unsigned int * identifier
Definition: espeak.h:127
bool begin(int buflength=5)
Starts the processing. Optionally add all relevant configuration files be fore calling this method.
Definition: espeak.h:28
bool setRate(int rate)
speaking speed in word per minute. Values 80 to 450.
Definition: espeak.h:76
void * user_data
Definition: espeak.h:125
bool setVolume(int vol)
volume in range 0-200 or more. 0=silence, 100=normal full volume, greater values may produce amplitud...
Definition: espeak.h:81
unsigned int end_position
Definition: espeak.h:129
espeak_POSITION_TYPE position_type
Definition: espeak.h:131
ESpeakFiles(Print &out, const char *path="../../../espeak-ng-data")
Definition: espeak.h:16
bool setVoice(const char *voiceName)
Defines the voice by i'ts name.
Definition: espeak.h:44
unsigned int position
Definition: espeak.h:128
int options
Definition: espeak.h:126
void setOutputEndCallback(void(*cb)())
Definition: espeak.h:39
void setOutputBeginCallback(void(*cb)())
Definition: espeak.h:34
bool setVoice(espeak_VOICE *voice_spec)
Defines the voice by it's properties.
Definition: espeak.h:50
bool setPitchRange(int range)
RANGE: pitch range, range 0-100. 0-monotone, 50=normal.
Definition: espeak.h:91
const char * path
Definition: espeak.h:124
bool setAnnounceCapitals(int caps)
CAPITALS: announce capital letters by: 0=none, 1=sound icon, 2=spelling, 3 or higher,...
Definition: espeak.h:101
bool setWordGap(int gap)
WORDGAP: pause between words, units of 10mS (at the default speed)
Definition: espeak.h:106
bool say(const char *str)
Outputs the string as sound.
Definition: espeak.h:66
audio_info audioInfo()
Provides information about the sample rate, channels ....
Definition: espeak.h:25
Simple Arduino C++ class API for ESpeak using PROGMEM.
Definition: espeak.h:138
bool is_fs_setup
Definition: espeak.h:167
bool begin(int buflength=500)
Definition: espeak.h:149
ESpeak(Print &out, bool setupEnglish=true)
Definition: espeak.h:140
bool is_setup_english
Definition: espeak.h:166
void add(const char *fileName, const void *fileContent, size_t len)
Adds a configuration file (e.g dictionary or lang). Use the prefix /mem/data/ for the name!
Definition: espeak.h:145
file_systems::FileSystemMemory fsm
Definition: espeak.h:165
unsigned int espeak_ng_data_lang_gmw_en_len
Definition: en.h:15
unsigned int espeak_ng_data_en_dict_len
Definition: en_dict.h:13913
#define GUARD_VALUE
Definition: espeak.h:10
unsigned int espeak_ng_data_intonations_len
Definition: intonations.h:173
unsigned int espeak_ng_data_phondata_len
Definition: phondata.h:45872
unsigned int espeak_ng_data_phonindex_len
Definition: phonindex.h:3259
unsigned int espeak_ng_data_phontab_len
Definition: phontab.h:4650
espeak_VOICE * espeak_GetCurrentVoice(void)
espeak_ERROR
Definition: speak_lib.h:184
@ EE_OK
Definition: speak_lib.h:185
espeak_PUNCT_TYPE
Definition: speak_lib.h:428
espeak_AUDIO_OUTPUT
Definition: speak_lib.h:168
@ AUDIO_OUTPUT_SYNCH_PLAYBACK
Definition: speak_lib.h:179
espeak_POSITION_TYPE
Definition: speak_lib.h:161
@ POS_CHARACTER
Definition: speak_lib.h:162
espeak_ERROR espeak_SetVoiceByProperties(espeak_VOICE *voice_spec)
#define espeakCHARS_AUTO
Definition: speak_lib.h:280
espeak_ERROR espeak_Terminate(void)
espeak_ERROR espeak_Synth(const void *text, size_t size, unsigned int position, espeak_POSITION_TYPE position_type, unsigned int end_position, unsigned int flags, unsigned int *unique_identifier, void *user_data)
espeak_PARAMETER
Definition: speak_lib.h:408
@ espeakRANGE
Definition: speak_lib.h:413
@ espeakWORDGAP
Definition: speak_lib.h:416
@ espeakVOLUME
Definition: speak_lib.h:411
@ espeakPITCH
Definition: speak_lib.h:412
@ espeakRATE
Definition: speak_lib.h:410
@ espeakCAPITALS
Definition: speak_lib.h:415
@ espeakPUNCTUATION
Definition: speak_lib.h:414
espeak_ERROR espeak_SetVoiceByName(const char *name)
espeak_ERROR espeak_SetParameter(espeak_PARAMETER parameter, int value, int relative)
int espeak_Initialize(espeak_AUDIO_OUTPUT output, int buflength, const char *path, int options)
info about sample rate and channels and output stream
Definition: audio_object.h:9
Definition: speak_lib.h:564