-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhtstts.cpp
463 lines (374 loc) · 14.6 KB
/
htstts.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
// htstts.cpp : Implementation of HTSTTS
#include "stdafx.h"
#include "htstts.h"
#include "TTSManager.h"
#include "TTSLogger.h"
using namespace htstts;
static double ConvertSapiRate(int r);
static TTSManager ttsManager;
#define SAPI_PROPERTY_BOOKMARK "SAPIROPBKMK"
/******************************************************************************
* Subclass of TextFragment to add some SAPI specific properties.
******************************************************************************/
class SAPITextFragment : public TextFragment {
public:
std::string bookmark;
};
/******************************************************************************
* HTSTTS Constructor
******************************************************************************/
HTSTTS::HTSTTS() : voiceProperties(new FragmentProperties()) {
(*voiceProperties)[PROPERTY_KEY_FRAGMENT_NOSIL_END] = PROPERTY_VALUE_TRUE;
(*voiceProperties)[PROPERTY_KEY_FRAGMENT_NOSIL_BEGIN] = PROPERTY_VALUE_TRUE;
ResetActions();
}
/******************************************************************************
* HTSTTS Destructor
******************************************************************************/
HTSTTS::~HTSTTS() {
}
/******************************************************************************
* Reset action states
******************************************************************************/
void HTSTTS::ResetActions() {
actionAborted = false;
actionSkipSentences = 0;
}
/******************************************************************************
* SetObjectToken
* Handle object token which contains info about the voice to be used.
******************************************************************************/
STDMETHODIMP HTSTTS::SetObjectToken(ISpObjectToken* pToken) {
USES_CONVERSION;
HRESULT hr;
CSpDynamicString voicedir, logpath, langID, textrules;
if (!SUCCEEDED(hr = SpGenericSetObjectToken(pToken, voiceToken))) {
return hr;
}
//- get logfile directory
if (SUCCEEDED(voiceToken->GetStringValue(L"Logfile", &logpath))) {
LOG_SETFILE(W2A(logpath));
}
LOG_DEBUG("Initializing voice");
//- get language of voice
if (SUCCEEDED(voiceToken->GetStringValue(L"Language", &langID))) {
(*voiceProperties)[ PROPERTY_KEY_LANGUAGE ] = W2A(langID);
LOG_DEBUG("Language: " << (*voiceProperties)[ PROPERTY_KEY_LANGUAGE ]);
}
//- get voice model directory
if (!SUCCEEDED(hr = voiceToken->GetStringValue(L"Voxpath", &voicedir))) {
LOG_DEBUG("Voice model directory not specified, quitting!");
return hr;
}
else {
(*voiceProperties)[ PROPERTY_KEY_VOICE_PATH ] = W2A(voicedir);
LOG_DEBUG("Voice model: " << (*voiceProperties)[ PROPERTY_KEY_VOICE_PATH ]);
}
//- get text rules if available Textrules
if (SUCCEEDED(voiceToken->GetStringValue(L"Textrules", &textrules))) {
(*voiceProperties)[ PROPERTY_KEY_TEXTANALYZER_RULES ] = W2A(textrules);
LOG_DEBUG("Voice text rules: " << (*voiceProperties)[ PROPERTY_KEY_TEXTANALYZER_RULES ]);
}
//TODO: voice name or other features?
//TODO: preload voice?
// yet to be checked:
//if ((ttwv = feat_val(curr_vox->features, "tokentowords_func"))) {
// feat_set(curr_vox->features, "old_tokentowords_func", ttwv);
// feat_set(curr_vox->features, "tokentowords_func",
// itemfunc_val(sapi_tokentowords));
//}
return 0;
}
/******************************************************************************
* GetObjectToken
* Retrieve current token which contains info about the voice to be used.
******************************************************************************/
STDMETHODIMP HTSTTS::GetObjectToken(ISpObjectToken** ppToken) {
return SpGenericGetObjectToken(ppToken, voiceToken);
}
/******************************************************************************
* GetOutputFormat
******************************************************************************/
STDMETHODIMP HTSTTS::GetOutputFormat(const GUID* pTargetFormatId,
const WAVEFORMATEX* pTargetWaveFormatEx,
GUID* pDesiredFormatId,
WAVEFORMATEX** ppCoMemDesiredWaveFormatEx) {
//return SpConvertStreamFormatEnum(SPSF_48kHz16BitMono,
// pDesiredFormatId,
// ppCoMemDesiredWaveFormatEx);
WAVEFORMATEX* wfx;
TTSResultPtr result = ttsManager.SynthesizeTextFragment(
TextFragmentPtr(new TextFragment("", this->voiceProperties)));
if ((wfx = (WAVEFORMATEX*)CoTaskMemAlloc(sizeof(*wfx))) == NULL) {
return E_OUTOFMEMORY;
}
memset(wfx, 0, sizeof(*wfx));
wfx->nChannels = 1;
wfx->nSamplesPerSec = result->GetSamplingRate();
wfx->wFormatTag = WAVE_FORMAT_PCM;
wfx->wBitsPerSample = 16;
wfx->nBlockAlign = wfx->nChannels * wfx->wBitsPerSample / 8;
wfx->nAvgBytesPerSec = wfx->nSamplesPerSec * wfx->nBlockAlign;
*pDesiredFormatId = SPDFID_WaveFormatEx;
*ppCoMemDesiredWaveFormatEx = wfx;
return S_OK;
}
/******************************************************************************
* Speak
* SAPI interface function to trigger synthesis.
******************************************************************************/
STDMETHODIMP
HTSTTS::Speak(DWORD dwSpeakFlags,
REFGUID rguidFormatId,
const WAVEFORMATEX* pWaveFormatEx,
const SPVTEXTFRAG* pTextFragList,
ISpTTSEngineSite* pOutputSite) {
LOG_DEBUG("BEGIN__SPEAK");
const SPVTEXTFRAG* curr_frag;
Text fullText;
ResetActions();
//- translate SAPI text fragments to our synthesizers text fragments
for (curr_frag = pTextFragList; curr_frag; curr_frag = curr_frag->pNext) {
char* tmptext;
int len;
LPCWSTR currStart = curr_frag->pTextStart;
ULONG charsLeft = curr_frag->ulTextLen;
ULONG currlen;
ULONG FRAG_SIZE = 500;
// if this fragment has some changed properties for volume/pitch/etc., change the properties, else use voice properties
FragmentPropertiesPtr props = AdjustProperties(&(curr_frag->State), this->voiceProperties);
switch (curr_frag->State.eAction) {
//TODO: change some parameters for spellout/pronounce in text fragment properties and handle them
case SPVA_SpellOut:
LOG_DEBUG("[Speak] Should spell out something");
case SPVA_Pronounce:
LOG_DEBUG("[Speak] Should pronounce something");
case SPVA_Bookmark:
// bookmarks are treated like normal text fragments,
// but will set a special property
LOG_DEBUG("[Speak] Set a bookmark here");
(*props)[SAPI_PROPERTY_BOOKMARK] = std::string((char*)currStart, charsLeft * sizeof(wchar_t)) + std::string("\0");
case SPVA_Speak:
LOG_DEBUG("[Speak] Converting text");
if (curr_frag->ulTextLen == 0) {
continue;
}
while (charsLeft > 0) {
if (charsLeft > FRAG_SIZE) {
currlen = FRAG_SIZE;
}
else {
currlen = charsLeft;
}
len = WideCharToMultiByte(CP_UTF8, 0, currStart, currlen, NULL, 0, NULL, NULL);
if (len == 0) {
continue;
}
tmptext = new char[ len + 1 ];
WideCharToMultiByte(CP_UTF8, 0, currStart, currlen, tmptext, len, NULL, NULL);
tmptext[len] = 0;
LOG_DEBUG("[Speak] Text = " << tmptext);
fullText.push_back(TextFragmentPtr(new TextFragment(tmptext, props)));
delete[] tmptext;
tmptext = NULL;
charsLeft -= currlen;
currStart += currlen;
}
break;
case SPVA_Silence:
LOG_DEBUG("[Speak] Should do silence");
break;
default:
break;
}
}
//- now loop fragments, synthesize, play and handle actions
std::vector<TextFragmentPtr>::iterator it = fullText.begin();
for (; it != fullText.end(); ++it) {
TextFragmentPtr tf = *it;
LOG_DEBUG("[Speak] Handle actions");
HandleActions(pOutputSite);
HandleEventInterests(pOutputSite);
if (actionAborted) {
break;
}
// is this actually a bookmark?
//TODO: only send if interest is there
if (tf->GetProperties()->find(SAPI_PROPERTY_BOOKMARK) != tf->GetProperties()->end()) {
SPEVENT evt;
evt.eEventId = SPEI_TTS_BOOKMARK;
evt.elParamType = SPET_LPARAM_IS_STRING;
evt.ullAudioStreamOffset = 0L;
evt.wParam = atol(tf->GetText().c_str());
evt.lParam = (LPARAM)((*tf->GetProperties())[SAPI_PROPERTY_BOOKMARK].c_str());
(strdup(tf->GetText().c_str()));
pOutputSite->AddEvents(&evt, 1);
continue;
}
LOG_DEBUG("[Speak] Synthesize text fragment");
try {
TTSResultPtr result = ttsManager.SynthesizeTextFragment(tf);
//TODO: is result->GetSamplingRate() correct?
LOG_DEBUG("[Speak] Writing buffer");
//- ugly code incoming again
//- unfortunately we have to prebuffer the whole sample as the SAPI write
//- function does not work on all tested systems
//- when single samples (not bytes!) are written.
//- we could use &result->GetFrames[0] as a double array
//- or use templates for our TTSResults
ULONG b;
INT16* returnBuffer = new INT16[ result->GetFrames().size() ];
for (std::vector<double>::size_type i = 0
; i < result->GetFrames().size()
; ++i) {
double sample = result->GetFrames()[i];
if (sample > 32767.0) {
returnBuffer[i] = 32767;
}
else if (sample < -32768.0) {
returnBuffer[i] = -32768;
}
else {
returnBuffer[i] = (INT16) sample;
}
}
pOutputSite->Write(returnBuffer, (ULONG)result->GetFrames().size() * (ULONG)sizeof(*returnBuffer), &b);
delete[] returnBuffer;
LOG_DEBUG("[Speak] Writing buffer done");
}
catch (const PropertyMissingException& e) {
LOG_DEBUG("[Speak] Property missing: " << e.what());
//TODO: some sort of error messaging to SAPI
return -1;
}
catch (const FileLoadException& e) {
LOG_DEBUG("[Speak] File load failed: " << e.what());
//TODO: some sort of error messaging to SAPI
return -1;
}
catch (const std::exception& e) {
LOG_DEBUG("[Speak] Exception occurred: " << e.what());
//TODO: some sort of error messaging to SAPI
return -1;
}
}
LOG_DEBUG("END__SPEAK");
return S_OK;
}
void HTSTTS::HandleEventInterests(ISpTTSEngineSite* site) {
ULONGLONG interest;
if (site->GetEventInterest(&interest) == S_OK) {
if (interest & SPEI_TTS_BOOKMARK) {
LOG_DEBUG("[HandleEventInterests] Application interested in SPEI_TTS_BOOKMARK");
}
if (interest & SPEI_WORD_BOUNDARY) {
LOG_DEBUG("[HandleEventInterests] Application interested in SPEI_WORD_BOUNDARY");
}
if (interest & SPEI_SENTENCE_BOUNDARY) {
LOG_DEBUG("[HandleEventInterests] Application interested in SPEI_SENTENCE_BOUNDARY");
}
if (interest & SPEI_PHONEME) {
LOG_DEBUG("[HandleEventInterests] Application interested in SPEI_PHONEME");
}
if (interest & SPEI_VISEME) {
LOG_DEBUG("[HandleEventInterests] Application interested in SPEI_VISEME");
}
}
}
/******************************************************************************
* HandleActions
* Handles actions from the SAPI interface
******************************************************************************/
void HTSTTS::HandleActions(ISpTTSEngineSite* site) {
DWORD actions;
actions = site->GetActions();
//- abort action
if (actions & SPVES_ABORT) {
actionAborted = true;
return;
}
//- sentence skip action
if (actions & SPVES_SKIP) {
SPVSKIPTYPE stype;
long count;
site->GetSkipInfo(&stype, &count);
if (stype == SPVST_SENTENCE) {
actionSkipSentences += count;
}
LOG_DEBUG("[HandleActions] Skip sentences.");
}
//- change base speaking rate action
if (actions & SPVES_RATE) {
long adj;
site->GetRate(&adj);
ttsManager.SetBaseSpeakingRate(ConvertSapiRate(adj));
}
//- change base volume
if (actions & SPVES_VOLUME) {
USHORT adj;
site->GetVolume(&adj);
ttsManager.SetBaseVolume((int)adj);
}
}
/******************************************************************************
* AdjustProperties
* Evaluate SAPI XML properties.
******************************************************************************/
FragmentPropertiesPtr HTSTTS::AdjustProperties(const SPVSTATE* state, FragmentPropertiesPtr props) {
FragmentProperties* newProps = NULL;
//TODO: if( state.EmphAdj )
//TODO: if( state.PitchAdj.MiddleAdj )
// bookmark always needs special properties.
if (state->eAction == SPVA_Bookmark) {
if (newProps == NULL) {
newProps = new FragmentProperties(*props);
}
//(*newProps)[SAPI_PROPERTY_BOOKMARK] = PROPERTY_VALUE_TRUE;
}
//- speaking rate changed for this fragment?
if (state->RateAdj) {
std::stringstream ss;
ss << ConvertSapiRate(state->RateAdj);
if (newProps == NULL) {
newProps = new FragmentProperties(*props);
}
(*newProps)[PROPERTY_KEY_RATE] = ss.str();
}
//- volume changed for this fragment?
if (state->Volume != 100) {
std::stringstream ss;
ss << state->Volume;
if (newProps == NULL) {
newProps = new FragmentProperties(*props);
}
(*newProps)[PROPERTY_KEY_VOLUME] = ss.str();
}
//- return either the new properties
//- or if nothing has changed, the old ones
if (newProps) {
(*newProps)[PROPERTY_KEY_FRAGMENT_NOSIL_END] = PROPERTY_VALUE_TRUE;
(*newProps)[PROPERTY_KEY_FRAGMENT_NOSIL_BEGIN] = PROPERTY_VALUE_TRUE;
return FragmentPropertiesPtr(newProps);
}
else {
return props;
}
}
static const double sapi_ratetab_foo[21] = {
0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95,
1.0,
1.25, 1.5, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0, 3.25, 3.5
};
static const double* sapi_ratetab = sapi_ratetab_foo + 10;
/******************************************************************************
* ConvertSapiRate
******************************************************************************/
static double ConvertSapiRate(int r) {
if (r < -10) {
r = -10;
}
else if (r > 10) {
r = 10;
}
return sapi_ratetab[r];
}