Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 842681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:00:07+00:00 2026-05-15T06:00:07+00:00

I’m doing a project with a 3D model, that speaks. So, I’m using SAPI

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T06:00:08+00:00Added an answer on May 15, 2026 at 6:00 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 510k
  • Answers 510k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer select lt.user, lt.team, lt.league, l.game, l.type, l.name from tbl_leaguetables lt… May 16, 2026 at 4:53 pm
  • Editorial Team
    Editorial Team added an answer Create a DispatcherTimer at application startup and wire up to… May 16, 2026 at 4:53 pm
  • Editorial Team
    Editorial Team added an answer There's git commit -a which is like git add -u… May 16, 2026 at 4:53 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I am trying to understand how to use SyndicationItem to display feed which is
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.