在 SAPI 中说出进度事件

Speak Progress Event in SAPI

本文关键字:事件 SAPI      更新时间:2023-10-16

我正在尝试使用Microsoft SAPI编写文本到语音转换程序。为此,我有以下代码:

ISpVoice * pVoice = NULL;
int main(int argc, char* argv[])
{
if (FAILED(::CoInitialize(NULL)))
return FALSE;
HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
if (SUCCEEDED(hr))
{
hr = pVoice->Speak(L"Anyone who reads Old and Middle English literary texts will be familiar with the mid-brown volumes of the EETS, with the symbol of Alfred's jewel embossed on the front cover. Most of the works attributed to King Alfred or to Aelfric, along with some of those by bishop Wulfstan and much anonymous prose and verse from the pre-Conquest period, are to be found within the Society's three series; all of the surviving medieval drama, most of the Middle English romances, much religious and secular prose and verse including the English works of John Gower, Thomas Hoccleve and most of Caxton's prints all find their place in the publications. Without EETS editions, study of medieval English texts would hardly be possible.", SPF_IS_XML, NULL);
pVoice->Release();
pVoice = NULL;
}
::CoUninitialize();
return TRUE;
}

我想将朗读进度打印到屏幕上,在说出每个单词时打印出来。与System.Speech.Synthesis类似:

synth.SpeakProgress += new EventHandler<SpeakProgressEventArgs>(synth_SpeakProgress);

有关更多详细信息:使用语音合成事件

那么,如何使用 SAP 做到这一点呢?

ISpVoice继承自ISpEventSource,而又继承自ISpNotifySource

使用ISpEventSource::SetInterest()方法注册所需事件,例如SPEI_WORD_BOUNDARY

一个词开始合成。标记语言 (XML) 标记计入边界和偏移量。wParam是正在合成的当前输入流中单词的字符长度。lParam是正在合成的单词的当前文本输入流中的字符位置。

使用各种ISpNotifySource::SetNotify...()方法指定希望如何从 SAP 接收事件:

  • SetNotifySink()通过您提供的ISpNotifySink接口接收事件。

  • SetNotifyWindowMessage()在您选择的HWND接收事件。

  • SetNotifyCallbackFunction()在您提供的回调函数中接收事件。

  • SetNotifyCallbackInterface()通过您提供的ISpNotifyCallback接口接收事件。

  • SetNotifyWin32Event创建一个事件对象,该对象在新事件到达时发出信号。若要等待事件,请使用ISpNotifySource::WaitForNotifyEvent()或具有标准 Win32 等待函数(如WaitForSingleObject())的ISpNotifySource::GetNotifyEventHandle

当您收到新事件的通知时,如果需要,请使用ISpEventSource::GetEvents()获取详细的事件信息。