views:

116

answers:

2

Hello,

I'm doing a project with a 3D model, that speaks. So, I'm using SAPI 5.1, and I want to call a function asynchronously when there's a Viseme event (in order to play the animation related to).

How could I do it?

Thank you very much.

Note: I use : hRes = m_cpVoice->Speak(L"All I want is to solve this problem", SPF_ASYNC , NULL); And I know the CspEvent, event.eEventId . All I want is how to call a function when Sapi event happens

A: 

I have the same problem. Any answer?

TBArchimboldi
A: 

First, you need to call m_cpVoice->SetInterest(SPFEI(SPEI_VISEME), SPFEI(SPEI_VISEME)); that will tell SAPI to send an event when a VISEME event fires.

Second, you need to set up an event handler by calling m_cpVoice->SetNotifyCallbackInterface, with your callback. (It must implement ISpNotifyCallback, which is a virtual C++ interface that your object would implement.)

You can look at the SAPI events documentation for more details.

A sample implementation of ISpNotifyCallback would look like this:

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;
}
Eric Brown
Thanks for your answer. I have a problem with the second part. SetNotifyCallbackInterface uses as first parametrer "ISpNotifyCallback* " and a don't know how to implement this. I have looked for it on Sapi events documentation, but I definitely don't know how to do it. (Sapi headers neither says anything about it. I'm a bit lost, so I would apreciate so much a little explanation. Thanks again
Jesuskiewicz
The MSDN docs pretty much say it all: "This is a C++ virtual interface that can be implemented by a SAPI client application to receive notifications."I'll update the response to include a sample.
Eric Brown
Thank you very much. That makes possible to compile my program. But I have (a last) problem. When I create an object of CSpeechHandler on main.cpp writing "CSpeechHandler* cs= new CSpeechHandler();" MY program doesn't do anything. And if I try to call the function "cs->SetupTTSHandlers();" (To do this I have had to make SetupTTSHandlers public) It speaks but never calls the NotifyCallback function. So how to iniziate that calls? PD: I have introduce on the example "hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)" in order to make it runs
Jesuskiewicz
The sample I posted isn't complete. There's a complete worked example of TTS events in the Windows 7 SDK (and almost certainly in the Windows Vista SDK as well) - look in %windowssdkdir%\Samples\winui\speech\ttsapplication. That app uses SetNotifyWindowsMessage, but I changed it to use SetNotifyCallbackInterface without any difficulty.
Eric Brown
I know about that example, but it contains a lot of windows stuff (I don't use windows, I use a Win32 console aplication) and I can't see how to make the events start "NotifyCallback" on an automatic way. I don't see it. PD: When I have work on C++, I have always work with sequential programs...so It's difficult to me
Jesuskiewicz
Thanks for all, but any new suggestion?
Jesuskiewicz
You're not pumping messages (which are needed to get callbacks) while the speech is processing. I've edited the source code to provide a complete working example with events.
Eric Brown
Thanks you very much. You are an anonymous hero of the contemporany age. You have saved me from being lost a lot of hours.Thanks again.
Jesuskiewicz