如何使用多线程SAPI将Text转换为Wave?

时间:2009-10-05 16:32:35

标签: c++ multithreading wav sapi

我正在尝试使用以下功能将文本转换为wave文件。如果从主UI线程调用它,它工作正常。但是从另一个线程调用时失败了。如何从多线程函数中调用它?

void Pan_Channel::TextToPlaybackFile( CString Text, CString FileName )
{
 // Result variable
 HRESULT Result = S_OK;

 // Voice Object
 CComPtr<ISpVoice> cpVoice;

 // Create a SAPI Voice
 Result = cpVoice.CoCreateInstance( CLSID_SpVoice );

 // Audio format
 CSpStreamFormat cAudioFmt;

 // Set the audio format
 if( SUCCEEDED( Result ) )
 {
  Result = cAudioFmt.AssignFormat( SPSF_8kHz16BitMono );
 }

 // File Stream
 CComPtr<ISpStream> cpStream;

 // Call SPBindToFile, a SAPI helper method,  to bind the audio stream to the file
 if( SUCCEEDED( Result ) )
 {
  Result = SPBindToFile( FileName, SPFM_CREATE_ALWAYS, &cpStream,
   &cAudioFmt.FormatId(), cAudioFmt.WaveFormatExPtr() );
 }

 // set the output to cpStream so that the output audio data will be stored in cpStream
 if( SUCCEEDED( Result ) )
 {
  Result = cpVoice->SetOutput( cpStream, TRUE );
 }

  // Speak the text syncronously
 if( SUCCEEDED( Result ) )
 {
  Result = cpVoice->Speak( Text.AllocSysString(), SPF_DEFAULT, NULL );
 }

 // close the stream
 if( SUCCEEDED( Result ) )
 {
  Result = cpStream->Close();
 }

 // Release stream
 cpStream.Release();

 // Release voice object
 cpVoice.Release();
}

1 个答案:

答案 0 :(得分:2)

你有其他线程的初始化吗?需要使用它在每个线程上初始化COM。另外..你使用在另一个线程中的一个线程中创建的COM对象吗?因为如果你这样做,你需要编写线程之间的接口......