-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathttsengobj.cpp
More file actions
540 lines (483 loc) · 14 KB
/
ttsengobj.cpp
File metadata and controls
540 lines (483 loc) · 14 KB
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/*******************************************************************************
* TtsEngObj.cpp *
*---------------*
* Description:
* This module is the main implementation file for the CTTSEngObj class.
*-------------------------------------------------------------------------------
* Creation Date: 03/24/99
* Copyright (c) Microsoft Corporation. All rights reserved.
* All Rights Reserved
*
*******************************************************************************/
//--- Additional includes
#include "stdafx.h"
#include "TtsEngObj.h"
#include "SMITS5.C"
//--- Local
//DECtalk pitch table
static const short PitchTable[9] =
{
122,
208,
89,
155,
110,
296,
240,
106,
200
};
//DECtalk range table
static const short RangeTable[9] =
{
100,
240,
80,
90,
135,
180,
135,
80,
175
};
//Convert SAPI rate to DECtalk rate
long CTTSEngObj::SAPI2DTRate(long rate, long BassRate)
{
long NewRate = BassRate + (rate * 40);
if(NewRate > (long)caps.dwMaximumSpeakingRate)
{
NewRate = caps.dwMaximumSpeakingRate;
}
else if(NewRate < (long)caps.dwMinimumSpeakingRate)
{
NewRate = caps.dwMinimumSpeakingRate;
}
return NewRate;
}
//Convert SAPI pitch to DECtalk pitch
long CTTSEngObj::SAPI2DTPitch(long pitch, long BassPitch)
{
long NewPitch = BassPitch + (pitch * 10);
if(NewPitch > 350)
{
NewPitch = 350;
}
else if(NewPitch < 50)
{
NewPitch = 50;
}
return NewPitch;
}
//Convert SAPI range to DECtalk range
long CTTSEngObj::SAPI2DTRange(long range, long BassRange)
{
long NewRange = BassRange + (range * 10);
if(NewRange > 250)
{
NewRange = 250;
}
else if(NewRange < 0)
{
NewRange = 0;
}
return NewRange;
}
//DECtalk callback
void CTTSEngObj::callback(LONG LParam1, LONG lParam2, DWORD user, UINT msg)
{
CTTSEngObj *SAPI = (CTTSEngObj*)user;
if(!SAPI || !SAPI->m_OutputSite)
{
return;
}
if(msg == SAPI->BufferMessage)
{
LPTTS_BUFFER_T buffer = (LPTTS_BUFFER_T)lParam2;
if(SAPI->m_OutputSite->GetActions() & SPVES_ABORT)
{
TextToSpeechAddBuffer(SAPI->engine, buffer);
return;
}
short *samples = (short *)buffer->lpData;
for(DWORD i = 0; i < buffer->dwBufferLength/sizeof(short); i++)
{
double dsamp = samples[i] / 32768.0;
dsamp = dsamp * SAPI->gain;
dsamp = dsamp * 32768.0;
samples[i] = (short)dsamp;
}
SAPI->m_OutputSite->Write(buffer->lpData, buffer->dwBufferLength, NULL);
TextToSpeechAddBuffer(SAPI->engine, buffer);
}
}
/*****************************************************************************
* CTTSEngObj::FinalConstruct *
*----------------------------*
* Description:
* Constructor
*****************************************************************************/
HRESULT CTTSEngObj::FinalConstruct()
{
HRESULT hr = S_OK;
m_OutputSite = NULL;
engine = NULL;
memset(&caps, 0, sizeof(caps));
memset(buffer, 0, sizeof(buffer));
memset(samples, 0, sizeof(samples));
//Get some Info about the engine
TextToSpeechGetCaps(&caps);
//Initialize DECtalk
DT$OpenMem();
TextToSpeechStartupEx(&engine, WAVE_MAPPER, DO_NOT_USE_AUDIO_DEVICE, callback, (LONG)this);
if(!engine)
{
return hr;
}
//Register buffer message
BufferMessage = RegisterWindowMessage("DECtalkBufferMessage");
if(!BufferMessage)
{
return hr;
}
//Set the synth to output audio to memory buffers
if (TextToSpeechOpenInMemory(engine, WAVE_FORMAT_1M16))
{
return hr;
}
//Set up buffers
for (int i = 0; i < NUMBUFFERS; i++)
{
buffer[i].lpData = (LPSTR)samples[i];
buffer[i].dwMaximumBufferLength = BUFFERSIZE*2;
TextToSpeechAddBuffer(engine, &buffer[i]);
}
//Enable phonemic Input, the silence and emphasis tags use this
TextToSpeechSpeak(engine, "[:phone on]", TTS_NORMAL);
gain = 0;
SpellingMode = false;
LastRate = 0;
LastPitch = 0;
LastRange = 0;
return hr;
} /* CTTSEngObj::FinalConstruct */
/*****************************************************************************
* CTTSEngObj::FinalRelease *
*--------------------------*
* Description:
* destructor
*****************************************************************************/
void CTTSEngObj::FinalRelease()
{
//Reset and shutdown DECtalk
if(engine)
{
TextToSpeechReset(engine, TRUE);
TextToSpeechUnloadUserDictionary(engine);
TextToSpeechCloseInMemory(engine);
TextToSpeechShutdown(engine);
engine = NULL;
}
DT$CloseMem();
m_OutputSite = NULL;
memset(&caps, 0, sizeof(caps));
memset(&buffer, 0, sizeof(buffer));
memset(samples, 0, sizeof(samples));
} /* CTTSEngObj::FinalRelease */
//
//=== ISpObjectWithToken Implementation ======================================
//
/*****************************************************************************
* CTTSEngObj::SetObjectToken *
*----------------------------*
* Description:
* This function performs the majority of the initialization of the voice.
* Once the object token has been provided, the filenames are read from the
* token key and the files are mapped.
*****************************************************************************/
STDMETHODIMP CTTSEngObj::SetObjectToken(ISpObjectToken * pToken)
{
HRESULT hr = SpGenericSetObjectToken(pToken, m_cpToken);
if( SUCCEEDED( hr ) )
{
if(!engine)
{
return hr;
}
//Set default settings
TextToSpeechGetSpeaker(engine, &m_voice);
TextToSpeechGetRate(engine, &m_rate);
TextToSpeechGetLanguage(engine, &m_language);
//Load settings from the token
m_cpToken->GetDWORD( L"Voice", &m_voice);
m_cpToken->GetDWORD( L"Rate", &m_rate);
m_cpToken->GetDWORD( L"Language", &m_language);
//Check if we are in a valid range for m_voice
if(m_voice > WENDY)
{
m_voice = WENDY;
}
else if(m_voice < PAUL)
{
m_voice = PAUL;
}
//Set default voice
TextToSpeechSetSpeaker(engine, m_voice);
//Check if we are in a valid range for m_rate
if(m_rate > caps.dwMaximumSpeakingRate)
{
m_rate = caps.dwMaximumSpeakingRate;
}
else if(m_rate < caps.dwMinimumSpeakingRate)
{
m_rate = caps.dwMinimumSpeakingRate;
}
//Set default rate
TextToSpeechSetRate(engine, m_rate);
//Set default language
TextToSpeechSetLanguage(engine, m_language);
//Load a user dictionary
WCHAR *filename = NULL;
m_cpToken->GetStringValue(L"UserDict", &filename);
if(filename)
{
long strsize = WideCharToMultiByte(CP_ACP, 0, filename, -1, NULL, 0, NULL, NULL);
char *dictpath = (char *)malloc(strsize+1);
if(dictpath)
{
dictpath[strsize] = 0;
WideCharToMultiByte(CP_ACP, 0, filename, -1, dictpath, strsize, NULL, NULL);
TextToSpeechLoadUserDictionary(engine, dictpath);
free(dictpath);
}
}
}
return hr;
} /* CTTSEngObj::SetObjectToken */
//
//=== ISpTTSEngine Implementation ============================================
//
/*****************************************************************************
* CTTSEngObj::Speak *
*-------------------*
* Description:
* This is the primary method that SAPI calls to render text.
*-----------------------------------------------------------------------------
* Input Parameters
*
* pUser
* Pointer to the current user profile object. This object contains
* information like what languages are being used and this object
* also gives access to resources like the SAPI master lexicon object.
*
* dwSpeakFlags
* This is a set of flags used to control the behavior of the
* SAPI voice object and the associated engine.
*
* VoiceFmtIndex
* Zero based index specifying the output format that should
* be used during rendering.
*
* pTextFragList
* A linked list of text fragments to be rendered. There is
* one fragement per XML state change. If the input text does
* not contain any XML markup, there will only be a single fragment.
*
* pOutputSite
* The interface back to SAPI where all output audio samples and events are written.
*
* Return Values
* S_OK - This should be returned after successful rendering or if
* rendering was interrupted because *pfContinue changed to FALSE.
* E_INVALIDARG
* E_OUTOFMEMORY
*
*****************************************************************************/
STDMETHODIMP CTTSEngObj::Speak( DWORD dwSpeakFlags,
REFGUID rguidFormatId,
const WAVEFORMATEX * pWaveFormatEx,
const SPVTEXTFRAG* pTextFragList,
ISpTTSEngineSite* pOutputSite )
{
HRESULT hr = S_OK;
//--- Check args
if( SP_IS_BAD_INTERFACE_PTR( pOutputSite ) ||
SP_IS_BAD_READ_PTR( pTextFragList ) )
{
hr = E_INVALIDARG;
}
else
{
if(!engine)
{
return hr;
}
m_OutputSite = pOutputSite;
while(pTextFragList != NULL)
{
//Stop speech
if( pOutputSite->GetActions() & SPVES_ABORT )
{
TextToSpeechReset(engine, FALSE);
m_OutputSite = NULL;
return hr;
}
//Set DECtalk voice parameters
TextToSpeechGetSpeaker(engine, &m_voice);
long rate = 0;
pOutputSite->GetRate(&rate);
rate += pTextFragList->State.RateAdj;
long NewDTRate = SAPI2DTRate(rate, m_rate);
unsigned short volume = 100;
pOutputSite->GetVolume(&volume);
volume = (volume * (unsigned short)pTextFragList->State.Volume) / 100;
gain = volume / 100.0;
if(gain > 1)
{
gain = 1;
}
else if(gain < 0)
{
gain = 0;
}
long NewDTPitch = SAPI2DTPitch(pTextFragList->State.PitchAdj.MiddleAdj, PitchTable[m_voice]);
long NewDTRange = SAPI2DTRange(pTextFragList->State.PitchAdj.RangeAdj, RangeTable[m_voice]);
if(NewDTRate != LastRate || NewDTPitch != LastPitch || NewDTRange != LastRange)
{
LastRate = NewDTRate;
LastPitch = NewDTPitch;
LastRange = NewDTRange;
char params[64];
memset(params, 0, sizeof(params));
sprintf(params, "[:ra %d :dv ap %d pr %d]", NewDTRate, NewDTPitch, NewDTRange);
TextToSpeechSpeak(engine, params, TTS_NORMAL);
}
//Emphasis
if(pTextFragList->State.EmphAdj)
{
TextToSpeechSpeak(engine, "[\"]", TTS_NORMAL);
}
//--- Do skip?
if( pOutputSite->GetActions() & SPVES_SKIP )
{
long lSkipCnt;
SPVSKIPTYPE eType;
hr = pOutputSite->GetSkipInfo( &eType, &lSkipCnt );
if( SUCCEEDED( hr ) )
{
//--- Notify SAPI how many items we skipped. We're returning zero
// because this feature isn't implemented.
hr = pOutputSite->CompleteSkip( 0 );
}
}
switch(pTextFragList->State.eAction)
{
case SPVA_Speak:
{
//Disable spelling mode
if(SpellingMode)
{
TextToSpeechSpeak(engine, "[:mode spell off]", TTS_NORMAL);
SpellingMode = false;
}
//Convert Unicode text to ANSI for DECtalk
long strsize = WideCharToMultiByte(CP_ACP, 0, pTextFragList->pTextStart, pTextFragList->ulTextLen, NULL, 0, NULL, NULL);
char *text2speak = (char *)malloc(strsize+1);
if(text2speak)
{
text2speak[strsize] = 0;
WideCharToMultiByte(CP_ACP, 0, pTextFragList->pTextStart, pTextFragList->ulTextLen, text2speak, strsize, NULL, NULL);
TextToSpeechSpeak(engine, text2speak, TTS_NORMAL);
free(text2speak);
}
//Hack for the shark, which sends each word as It's own fragment!
TextToSpeechSpeak(engine, "\n", TTS_NORMAL);
break;
}
case SPVA_SpellOut:
{
//Enable spelling mode
if(!SpellingMode)
{
TextToSpeechSpeak(engine, "[:mode spell on]", TTS_NORMAL);
SpellingMode = true;
}
//Convert Unicode text to ANSI for DECtalk
long strsize = WideCharToMultiByte(CP_ACP, 0, pTextFragList->pTextStart, pTextFragList->ulTextLen, NULL, 0, NULL, NULL);
char *text2speak = (char *)malloc(strsize+1);
if(text2speak)
{
text2speak[strsize] = 0;
WideCharToMultiByte(CP_ACP, 0, pTextFragList->pTextStart, pTextFragList->ulTextLen, text2speak, strsize, NULL, NULL);
TextToSpeechSpeak(engine, text2speak, TTS_NORMAL);
free(text2speak);
}
//Hack for the shark, which sends each word as It's own fragment!
TextToSpeechSpeak(engine, "\n", TTS_NORMAL);
break;
}
case SPVA_Silence:
{
//Insert a pause
char silence[64];
memset(silence, 0, sizeof(silence));
sprintf(silence, "[_<%d>]", pTextFragList->State.SilenceMSecs);
TextToSpeechSpeak(engine, silence, TTS_NORMAL);
break;
}
//--- Fire a bookmark event ---------------------------------
case SPVA_Bookmark:
{
//--- The bookmark is NOT a null terminated string in the Item, but we need
//--- to convert it to one. Allocate enough space for the string.
WCHAR * pszBookmark = (WCHAR *)malloc((pTextFragList->ulTextLen + 1) * sizeof(WCHAR));
if (pszBookmark)
{
memcpy(pszBookmark, pTextFragList->pTextStart, pTextFragList->ulTextLen * sizeof(WCHAR));
pszBookmark[pTextFragList->ulTextLen] = 0;
//--- Queue the event
SPEVENT Event;
Event.eEventId = SPEI_TTS_BOOKMARK;
Event.elParamType = SPET_LPARAM_IS_STRING;
Event.ullAudioStreamOffset = 0;
Event.lParam = (LPARAM)pszBookmark;
Event.wParam = _wtol(pszBookmark);
hr = pOutputSite->AddEvents( &Event, 1 );
free(pszBookmark);
}
break;
}
}
pTextFragList = pTextFragList->pNext;
}
//Synthesize text
TextToSpeechSpeak(engine, "", TTS_FORCE);
//Wait for synthesis to complete
TextToSpeechSync(engine);
m_OutputSite = NULL;
//--- S_FALSE just says that we hit the end, return okay
if( hr == S_FALSE )
{
hr = S_OK;
}
}
return hr;
} /* CTTSEngObj::Speak */
/*****************************************************************************
* CTTSEngObj::GetVoiceFormat *
*----------------------------*
* Description:
* This method returns the output data format associated with the
* specified format Index. Formats are in order of quality with the best
* starting at 0.
*****************************************************************************/
STDMETHODIMP CTTSEngObj::GetOutputFormat( const GUID * pTargetFormatId, const WAVEFORMATEX * pTargetWaveFormatEx,
GUID * pDesiredFormatId, WAVEFORMATEX ** ppCoMemDesiredWaveFormatEx )
{
HRESULT hr = S_OK;
if(!engine)
{
return hr;
}
hr = SpConvertStreamFormatEnum(SPSF_11kHz16BitMono, pDesiredFormatId, ppCoMemDesiredWaveFormatEx);
return hr;
} /* CTTSEngObj::GetVoiceFormat */