首页 > 解决方案 > 如何在 Windows 10 上以编程方式定义音频输出?

问题描述

我正在开发一个实现 Microsoft Speech API (SAPI) 的 C++ 应用程序。我开发了许多与文本转语音相关的功能。其中一个允许列出音频输出的函数,一个允许定义音频输出的函数。

我开始在 Windows 7 上开发此程序,但现在我切换到 Windows 10。但是,定义音频输出的功能不再起作用。我没有在我的代码中编辑任何东西,并且在 Windows 7 上它运行良好。

这是列出可用音频输出的代码

int getAudioOut( int auOut ) //get audio outputs function
{
    if( SUCCEEDED( hr ) )
    {
       //Enumerate Audio Outputs
       hr = SpEnumTokens( SPCAT_AUDIOOUT, NULL, NULL, &cpEnum );
       cpEnum->GetCount( &vCount );
       cpEnum->Item( saveAudio, &cpAudioOutToken );
       SpGetDescription( cpAudioOutToken, &dynStr );
       printf( "Defined audio output is: %ls\n\n", dynStr );
       dynStr.Clear();

       //Loop through the audio output list and enumerate them all
       for( audioOut = 0; audioOut <= vCount - 1; audioOut++ )
       {
          cpAudioOutToken.Release();
          cpEnum->Item( audioOut, &cpAudioOutToken );
          SpGetDescription( cpAudioOutToken, &dynStr );
          printf( "Defined Audio Output %i - %ls\n", audioOut, dynStr );
          dynStr.Clear();
       }
       printf( "\n" );
       audioOut = saveAudio;

       cpEnum.Release();
       cpAudioOutToken.Release();
    }
    else
    {
       printf( "Could not enumerate available audio outputs\n" );
    }

    return true;
}

这是允许定义音频输出的代码

int setAudioOut( int auOut ) //define audio output function
{
   if( SUCCEEDED( hr ) )
   {
      hr = SpEnumTokens( SPCAT_AUDIOOUT, NULL, NULL, &cpEnum );
      cpEnum->GetCount( &vCount );
      size_t nOut = auOut;

      if( nOut >= vCount )
      {
         cout << "Not so many audio outputs available! Try again\n" << endl;
      }
      else
      {
         cout << "Success" << endl;
      }

       ULONG audioOut = static_cast<ULONG>( nOut ); //convert nOut to ULONG audioOut

       cpEnum->Item( audioOut, &cpAudioOutToken );
       SpGetDescription( cpAudioOutToken, &dynStr );
       printf( "You chose %ls\n\n", dynStr );
       cpVoice->SetOutput( cpAudioOutToken, TRUE ); //Initialization of the Audio Output
       dynStr.Clear();

       cpEnum.Release();
       cpAudioOutToken.Release();

       saveAudio = audioOut; //define saveAudio to audioOut value
    }
    else
    {
       printf( "Could not set audio output\n" );
    }

    return true;
}

当我启动我的程序并调用该getAudioOut函数时,我得到以下列表:

getAudioOut 函数列表

第一行显示默认的音频输出,下面两个是可用的输出。在 Windows 7 上,当我将第二个音频输出 (Lautsprecher / Kopfhörer) 设置为默认值时,第一个 (Digitalaudio) 不会发出声音,这是有道理的。但是,在 Windows 10 上,我复制了相同的过程,但它不起作用。音频输出始终根据音频菜单定义。

音频菜单

我的问题是,有人遇到过这个问题吗?是否有替代方案以编程方式定义音频输出?

标签: c++audiotext-to-speechsapispeech-synthesis

解决方案


我按照@NikolayShmyrev 的建议编辑了代码,但并没有改变任何事情。但是,我继续深入研究问题,发现问题来自另一个功能。确实,当我从 Windows 7 切换到 Windows 10 时,我遇到了语音合成功能和语音转 WAV 文件功能的其他问题。当我启动程序并调用Text-To-Speech函数时,一切都很好。当我调用该Speech2Wav函数时,它也起作用了。但是,当我调用该Text-To-Speech函数时,变量HRESULT hr = S_OK;的值发生了变化,并且没有播放任何声音。的值hr设置为-2147200968,对应于错误 0x80045038:SPERR_STREAM_CLOSED/错误代码列表

为了解决这个问题,我必须cpVoice->SetOutput( cpAudioOutToken, TRUE );Text-To-Speech函数中定义这样的音频输出。

这让我们回到了我上面提到的问题。当我在函数中设置音频输出时setAudioOut,我在最后释放它的值cpAudioOutToken.Release(); 但是,我在函数中重用了相同的变量Text-To-Speech。它的值设置为空,因为我在定义音频输出时释放了它。这就是为什么音频输出总是设置为默认值的原因。为了解决这个问题,我将 的值分配cpAudioOutToken给另一个名为cpSpeechOutToken.

这是函数的代码setAudioOut

int setAudioOut( int auOut ) //define audio output function
{
   if( SUCCEEDED( hr ) )
   {
      hr = SpEnumTokens( SPCAT_AUDIOOUT, NULL, NULL, &cpEnum );
      cpEnum->GetCount( &vCount );
      size_t nOut = auOut;

      if( nOut >= vCount )
      {
         cout << "Not so many audio outputs available! Try again\n" << endl;
         return 0;
      }
      else
      {
         cout << "Success" << endl;
      }

      ULONG audioOut = static_cast<ULONG>( nOut ); //convert nOut to ULONG audioOut

      cpEnum->Item( audioOut, &cpAudioOutToken );
      SpGetDescription( cpAudioOutToken, &dynStr );
      printf( "You chose %ls\n\n", dynStr );
      cpVoice->SetOutput( cpAudioOutToken, TRUE ); //Initialization of the Audio Output
      dynStr.Clear();

      cpEnum.Release();
      cpSpeechOutToken = cpAudioOutToken;
      cpAudioOutToken.Release();
      saveAudio = audioOut; //define saveAudio to audioOut value
   }
   else
   {
      printf( "Could not set audio output\n" );
   }
   return true;
}

这是Text-To-Speech函数的代码

int ttsSpeak( const char* text ) //Text to Speech speaking function
{
   if( SUCCEEDED( hr ) )
   {
      string xmlSentence( text );
      hr = SpEnumTokens( SPCAT_VOICES_WIN10, NULL, NULL, &cpEnum );
      //Replace SPCAT_VOICES_WIN10 with SPCAT_VOICES if you want to use it on Windows 7

      cpEnum->Item( saveVoice, &cpVoiceToken ); //get saveVoice token defined at line 175
      cpVoice->SetVoice( cpVoiceToken ); //Initialization of the voice

      //string strText( text );

      int wchars_num = MultiByteToWideChar( CP_ACP, 0, xmlSentence.c_str(), -1, NULL, 0 );
      wchar_t* wstr = new wchar_t[ wchars_num ];
      MultiByteToWideChar( CP_ACP, 0, xmlSentence.c_str(), -1, wstr, wchars_num );

      printf( "Text To Speech processing\n" );
      cpVoice->SetOutput( cpSpeechOutToken, TRUE );
      hr = cpVoice->Speak( wstr, SVSFIsXML, NULL );

      saveText = xmlSentence.c_str();

      cpEnum.Release();
      cpVoiceToken.Release();
      delete new wchar_t[ wchars_num ];
  }
  else
  {
     printf( "Could not speak entered text\n" );
  }
  return true;
}

推荐阅读