/
ttsengobj.cpp
256 lines (225 loc) · 7.26 KB
/
ttsengobj.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
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright © Microsoft Corporation. All rights reserved
/*******************************************************************************
* TtsEngObj.cpp *
*---------------*
* Description:
* This module is the main implementation file for the CTTSEngObj class.
*******************************************************************************/
//--- Additional includes
#include "stdafx.h"
#include "TtsEngObj.h"
#include <stdio.h>
#include <iostream>
#include <windows.h>
//--- Local
std::string to_utf8(const std::wstring& s, ULONG length)
{
std::string utf8;
int len = WideCharToMultiByte(CP_UTF8, 0, s.c_str(), length, NULL, 0, NULL, NULL);
if (len > 0)
{
utf8.resize(len);
WideCharToMultiByte(CP_UTF8, 0, s.c_str(), length, &utf8[0], len, NULL, NULL);
}
return utf8;
}
enum class MessageType {
LIFECYCLE,
SPEECH,
ERR
};
HRESULT emit(MessageType type, std::string data) {
HANDLE pipe = CreateFile(
L"\\\\.\\pipe\\my_pipe",
GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if (pipe == INVALID_HANDLE_VALUE)
{
fprintf(stderr, "Failed to connect to pipe.");
return E_HANDLE;
}
std::string typeString;
if (type == MessageType::LIFECYCLE)
{
typeString = "lifecycle";
}
else if (type == MessageType::SPEECH)
{
typeString = "speech";
}
else
{
typeString = "error";
}
std::string message(typeString + ":" + data);
DWORD numBytesWritten = 0;
BOOL result = WriteFile(
pipe, // handle to our outbound pipe
message.c_str(), // data to send
message.size(),
&numBytesWritten, // will store actual amount of data sent
NULL // not using overlapped IO
);
// Close the pipe (automatically disconnects client too)
CloseHandle(pipe);
if (!result)
{
fprintf(stderr, "Failed to send data.");
return E_FAIL;
}
return S_OK;
}
/*****************************************************************************
* CTTSEngObj::FinalConstruct *
*----------------------------*
* Description:
* Constructor
*****************************************************************************/
HRESULT CTTSEngObj::FinalConstruct()
{
HRESULT hr = S_OK;
//--- Init vars
m_hVoiceData = NULL;
m_pVoiceData = NULL;
m_pWordList = NULL;
m_ulNumWords = 0;
hr = m_cpVoice.CoCreateInstance(CLSID_SpVoice);
if (FAILED(hr))
{
emit(MessageType::ERR, "Voice initialization failed");
}
else
{
emit(MessageType::LIFECYCLE, "Voice initialization succeeded");
}
return hr;
}
/*****************************************************************************
* CTTSEngObj::FinalRelease *
*--------------------------*
* Description:
* destructor
*****************************************************************************/
void CTTSEngObj::FinalRelease()
{
delete m_pWordList;
if (m_pVoiceData)
{
::UnmapViewOfFile((void*)m_pVoiceData);
}
if (m_hVoiceData)
{
::CloseHandle(m_hVoiceData);
}
emit(MessageType::LIFECYCLE, "Voice destroyed");
}
//
//=== 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)
{
return SpGenericSetObjectToken(pToken, m_cpToken);
}
//
//=== 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)
{
//--- Check args
if (SP_IS_BAD_INTERFACE_PTR(pOutputSite) || SP_IS_BAD_READ_PTR(pTextFragList))
{
return E_INVALIDARG;
}
HRESULT hr = S_OK;
for (const SPVTEXTFRAG* textFrag = pTextFragList; textFrag != NULL; textFrag = textFrag->pNext)
{
if (textFrag->State.eAction == SPVA_Bookmark)
{
continue;
}
const std::wstring& text = textFrag->pTextStart;
hr = m_cpVoice->Speak(text.substr(0, textFrag->ulTextLen).c_str(), dwSpeakFlags | SPF_ASYNC | SPF_PURGEBEFORESPEAK, 0);
if (FAILED(hr))
{
emit(MessageType::ERR, "Speaking failed");
break;
}
hr = emit(MessageType::SPEECH, to_utf8(textFrag->pTextStart, textFrag->ulTextLen));
if (FAILED(hr))
{
break;
}
}
return hr;
}
/*****************************************************************************
* 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)
{
return SpConvertStreamFormatEnum(SPSF_11kHz16BitMono, pDesiredFormatId, ppCoMemDesiredWaveFormatEx);
}