c++ - 如何使用带多线程的 SAPI 将文本转换为 Wave?

标签 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();
}

最佳答案

你有没有 CoInitialized 另一个线程? COM 需要在使用它的每个线程上进行初始化。另外..您是否在另一个线程中使用在一个线程中创建的 COM 对象?因为如果你这样做,你需要编码线程之间的接口(interface)......

关于c++ - 如何使用带多线程的 SAPI 将文本转换为 Wave?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1521080/

相关文章:

C++ 模板在 .h 中声明,在 .hpp 中定义

c++ - 问到c++11中的多线程矩阵乘法

java - 如何解码WAV/RIFF音频数据?

actionscript-3 - 在 as3 中播放连续的 wav 声音

ffmpeg - 如何在python中将wav音频文件转换为aac?

C++如何获取类中的特定对象

c++ - 如何使用 DirectWrite 确定基线位置

c++ - wcstombs_s(),转换后的字符串长度

c++ - 从主线程 C++ 更新信号量

java - 在 Java 中实现这种线程/事件行为的最佳方式是什么?