this was probably a bad idea to begin with since i've never messed with multithreading or the sapi... but i got the sapi working on its own, but when I put it in its own thread it wont speak anymore. I've tried over and over and over.... i'm not sure if this is the best attempt to post, but here it is, anyone have any ideas?

#include "sapi.h"
#include <process.h> 

HANDLE thread;


long WINAPI speakthread( PCHAR text );


void cmdSpeak( PCHAR text) {
	DWORD dwID;
	DWORD dwRetVal = 0;
	thread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)speakthread,text,0,&dwID);	

	dwRetVal = WaitForSingleObject(thread, INFINITE);

	CloseHandle(thread);
}



long WINAPI speakthread( PCHAR text ){
	ISpVoice* m_pVoice;
	HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&m_pVoice);
    if( SUCCEEDED( hr ) )
    {
		wchar_t* wszStr;
		int len = strlen(text);
		wszStr = new wchar_t[len + 50];
		mbstowcs(wszStr,text,len+1);	
		
        hr = m_pVoice->lpVtbl->Speak(m_pVoice,wszStr, 0, NULL);
		m_pVoice->lpVtbl->Release(m_pVoice);
        m_pVoice = NULL;
	}
	return 0;
}

I know this is messy... sorry. It's hard to be worried about elegance when half the crap I write doesnt work anyway =P

Tried finding answers via google, but info on multithreading seems kinda thin, or just way the hell over my head.

windows.h is included from another part of the project.

Dani AI

Generated

Symptom reported by — SAPI works on the main thread but is silent when moved to a worker thread — almost always points to COM/apartment and thread-lifetime issues. 's working example that initializes COM inside the thread confirms the core cause: each thread that uses COM must initialize and uninitialize the COM library and use the correct apartment model for SAPI.

Practical checklist (concise): initialize COM on the new thread (CoInitializeEx with an STA flag for SAPI-style voices) and call CoUninitialize before the thread exits; create the ISpVoice instance only after COM is initialized in that thread; check and handle HRESULTs at each COM call; release the voice pointer before uninitializing COM; use the correct thread entry signature expected by CreateThread/_beginthreadex (pass a void pointer and cast it inside the thread). Prefer _beginthreadex or the framework thread helper (AfxBeginThread) instead of CreateThread when the C runtime will be used in the thread.

Two more pitfalls to watch for: string conversion and message pumping. Convert multibyte text to UTF-16 with a reliable API (for example MultiByteToWideChar or explicit wide strings) rather than fragile mbstowcs usage. If Speak is used asynchronously (SPF_ASYNC), the thread must pump messages (STA callback model); synchronous Speak calls block until completion and do not need a message loop. Also, waiting immediately on the worker thread from the creator can mask message-pump problems.

For : an "ISpVoice undeclared" error typically means the SAPI headers/libs are not on the compiler/linker paths (include sapi.h, link sapi.lib) or the SDK installer did not run properly. Confirm SDK install/paths and run the installer with appropriate privileges or obtain the SDK files for the development environment.

Recommended Answers

All 6 Replies

I don't know if it's the extra initialization, the flags, or the fact that this is in it's own function, but I call this function from a thread and it speaks fine.

// This function does the actual speaking of the generated TTS script
void CProgNameDlg::AudioTTS()
{
	ISpVoice* Voice = NULL;
	CoInitialize(NULL);
	CoCreateInstance ( CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void**)&Voice );
	Voice -> Speak(stuffToSay.AllocSysString(), SPF_DEFAULT, NULL);
}

PLEASE HELP.When I download SpeechSDK51.exe it doesn't work.I want SAPI work in my computer.
compiler said that
C:\Documents and Settings\lasha\Desktop\SAPI\SAPI.cpp(142) : error C2065: 'ISpVoice' : undeclared identifier
C:\Documents and Settings\lasha\Desktop\SAPI\SAPI.cpp(142) : error C2065: 'pVoice' : undeclared identifier
C:\Documents and Settings\lasha\Desktop\SAPI\SAPI.cpp(147) : error C2227: left of '->Speak' must point to class/struct/union

When I download SpeechSDK51.exe and when I execute it, it doesn't executes.

What should I do?

did you

#include "sapi.h"

yes

It took you 1 year and 2 months to check that? :icon_eek:

It took you 1 year and 2 months to check that? :icon_eek:

I think I know what the problem might be. Let me put together some code and I'll let you know next March. :)

commented: Thanks for the laugh +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.