c++ - 在 "event.GetFrom(m_cpVoice)==S_OK"时调用函数(因此事件发生时)[SAPI 5.1 和 C++]

标签 c++ events event-handling sapi

我正在用 3D 模型做一个项目,这很重要。 所以,我使用的是 SAPI 5.1,我想在有 Viseme 事件时异步调用一个函数(以便播放与之相关的动画)。

我该怎么做?

非常感谢。

注意:我使用:hRes = m_cpVoice->Speak(L"All I want is to solve this problem", SPF_ASYNC , NULL); 我知道 CspEvent,event.eEventId。我想要的只是当 Sapi 事件发生时如何调用一个函数

最佳答案

首先,您需要调用m_cpVoice->SetInterest (SPFEI(SPEI_VISEME),SPFEI(SPEI_VISEME));这将告诉 SAPI 在 VISEME 事件触发时发送一个事件。

其次,您需要通过调用 m_cpVoice->SetNotifyCallbackInterface 来设置事件处理程序, 与您的回调。 (它必须实现 ISpNotifyCallback ,这是您的对象将实现的虚拟 C++ 接口(interface)。)

你可以看看SAPI events documentation了解更多详情。

ISpNotifyCallback 的示例实现如下所示:

TTSHandler.h:

class CTTSHandler : ISpNotifyCallback
{
public:
    CTTSHandler(void);
    ~CTTSHandler(void);
    HRESULT Initialize();
    HRESULT DoSpeak();
    HRESULT Uninitialize();

private:
    HRESULT STDMETHODCALLTYPE NotifyCallback(WPARAM wParam, LPARAM lParam);
    void TTSAppStatusMessage(LPCTSTR str);

    CComPtr<ISpAudio>   m_cpOutAudio;
    CComPtr<ISpVoice> m_cpVoice;
    HANDLE m_hSpeakDone;
};

TTSHandler.cpp:

#include "TTSHandler.h"
#include <sphelper.h>

CTTSHandler::CTTSHandler(void) : m_hSpeakDone(INVALID_HANDLE_VALUE)
{
}

CTTSHandler::~CTTSHandler(void)
{
}

HRESULT CTTSHandler::Initialize()
{
    HRESULT hr = m_cpVoice.CoCreateInstance( CLSID_SpVoice );
    if ( SUCCEEDED( hr ) )
    {
        SpCreateDefaultObjectFromCategoryId( SPCAT_AUDIOOUT, &m_cpOutAudio );
    }
    if( SUCCEEDED( hr ) )
    {
        hr = m_cpVoice->SetOutput( m_cpOutAudio, FALSE );
    }
    if ( SUCCEEDED( hr ) )
    {
        hr = m_cpVoice->SetNotifyCallbackInterface(this, 0, 0);
    }
    // We're interested in all TTS events
    if( SUCCEEDED( hr ) )
    {
        hr = m_cpVoice->SetInterest( SPFEI_ALL_TTS_EVENTS, SPFEI_ALL_TTS_EVENTS );
    }
    if (SUCCEEDED(hr))
    {
        m_hSpeakDone = ::CreateEvent(NULL, TRUE, FALSE, NULL);     // anonymous event used to wait for speech done
    }
    return hr;
}

HRESULT CTTSHandler::DoSpeak()
{
    HRESULT hr = m_cpVoice->Speak( L"This is a reasonably long string that should take a while to speak.  This is some more text.", SPF_ASYNC |SPF_IS_NOT_XML, 0 );
    if (FAILED(hr))
    {
        TTSAppStatusMessage(  _T("speak failed\r\n") );
    }
    else
    {
        BOOL fContinue = TRUE;
        while (fContinue)
        {
            DWORD dwWaitId = ::MsgWaitForMultipleObjectsEx(1, &m_hSpeakDone, INFINITE, QS_ALLINPUT, MWMO_INPUTAVAILABLE);
            switch (dwWaitId)
            {
            case WAIT_OBJECT_0:
                {
                    fContinue = FALSE;
                }
                break;

            case WAIT_OBJECT_0 + 1:
                {
                    MSG Msg;
                    while (::PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
                    {
                        ::TranslateMessage(&Msg);
                        ::DispatchMessage(&Msg);
                    }
                }
                break;

            case WAIT_TIMEOUT:
                {
                    hr = S_FALSE;
                    fContinue = FALSE;
                }
                break;

            default:// Unexpected error
                {
                    TTSAppStatusMessage(L"Unexpected error returned from MsgWaitForMultipleObj");
                    hr = HRESULT_FROM_WIN32(::GetLastError());
                    fContinue = FALSE;
                }
                break;
            }
        }
    }
    return hr;
}

HRESULT CTTSHandler::Uninitialize()
{
    m_cpVoice = NULL;
    return S_OK;
}


void CTTSHandler::TTSAppStatusMessage(LPCTSTR szMessage )
{
    wprintf_s(L"%s", szMessage);
}

/////////////////////////////////////////////////////////////////
HRESULT STDMETHODCALLTYPE CTTSHandler::NotifyCallback(WPARAM, LPARAM)
/////////////////////////////////////////////////////////////////
//
// Handles the WM_TTSAPPCUSTOMEVENT application defined message and all
// of it's appropriate SAPI5 events.
//
{

    CSpEvent        event;  // helper class in sphelper.h for events that releases any 
    // allocated memory in it's destructor - SAFER than SPEVENT
    int             i = 0;
    HRESULT         hr = S_OK;

    while( event.GetFrom(m_cpVoice) == S_OK )
    {
        switch( event.eEventId )
        {
        case SPEI_START_INPUT_STREAM:
            TTSAppStatusMessage(  _T("StartStream event\r\n") );
            break; 

        case SPEI_END_INPUT_STREAM:
            TTSAppStatusMessage(  _T("EndStream event\r\n") );
            SetEvent(m_hSpeakDone);
            break;     

        case SPEI_VOICE_CHANGE:
            TTSAppStatusMessage(  _T("Voicechange event\r\n") );
            break;

        case SPEI_TTS_BOOKMARK:
            {
                // Get the string associated with the bookmark
                // and add the null terminator.
                TCHAR szBuff2[MAX_PATH] = _T("Bookmark event: ");

                size_t cEventString = wcslen( event.String() ) + 1;
                WCHAR *pwszEventString = new WCHAR[ cEventString ];
                if ( pwszEventString )
                {
                    wcscpy_s( pwszEventString, cEventString, event.String() );
                    _tcscat_s( szBuff2, _countof(szBuff2), CW2T(pwszEventString) );
                    delete[] pwszEventString;
                }

                _tcscat_s( szBuff2, _countof(szBuff2), _T("\r\n") );
                TTSAppStatusMessage(  szBuff2 );
            }
            break;

        case SPEI_WORD_BOUNDARY:
            TTSAppStatusMessage(  _T("Wordboundary event\r\n") );
            break;

        case SPEI_PHONEME:
            TTSAppStatusMessage(  _T("Phoneme event\r\n") );
            break;

        case SPEI_VISEME:
            TTSAppStatusMessage(  _T("Viseme event\r\n") );
            break;

        case SPEI_SENTENCE_BOUNDARY:
            TTSAppStatusMessage(  _T("Sentence event\r\n") );
            break;

        case SPEI_TTS_AUDIO_LEVEL:
            WCHAR wszBuff[MAX_PATH];
            swprintf_s(wszBuff, _countof(wszBuff), L"Audio level: %d\r\n", (ULONG)event.wParam);
            TTSAppStatusMessage(  CW2T(wszBuff) );
            break;

        case SPEI_TTS_PRIVATE:
            TTSAppStatusMessage(  _T("Private engine event\r\n") );
            break;

        default:
            TTSAppStatusMessage(  _T("Unknown message\r\n") );
            break;
        }
    }
    return hr;
}

关于c++ - 在 "event.GetFrom(m_cpVoice)==S_OK"时调用函数(因此事件发生时)[SAPI 5.1 和 C++],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2866599/

相关文章:

c++ - 交换两个字符串时访问正确的内存时遇到段错误

macos - 让 NSWindow 拒绝键盘事件

javascript - Twitter JavaScript API : adding Web Intent events

c# - 连接到单元测试中的事件

c++ - OpenCV 捕获循环视频/不检测最后一帧

c++ - 理解 QGraphicsItemboundingRect 和 shape 方法之间的交互

c++ - C & C++ : What is the difference between pointer-to and address-of array?

java - 在生成事件的源元素的父元素中居中 JoptionPaneMessageDialog

javascript - 无法在 Firefox 上关闭 on change 事件中的 alert()

.net - 记录事件的调用顺序