如何在 Windows 10 上以编程方式定义音频输出?

How to define an audio output programmatically on Windows 10?

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

我在Windows 7开始开发这个程序,但现在我切换到Windows 10。但是,定义音频输出的函数不再起作用了。我没有在我的代码中编辑任何东西,在 Windows 7 上它运行完美。

Here is the code which lists the available audio outputs

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;
}

Here is the code which allows the definition of an audio output

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 函数时,我得到以下清单:

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

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

我按照@NikolayShmyrev 的建议编辑了代码,但它没有改变任何东西。但是,我继续深入研究问题,发现问题来自另一个功能。确实,当我从Windows 7切换到Windows 10时,我遇到了语音合成功能和语音转WAV文件功能的其他问题。当我启动程序并调用 Text-To-Speech 函数时,一切正常。当我调用 Speech2Wav 函数时,它也起作用了。然而,当我回忆起Text-To-Speech函数时,变量HRESULT hr = S_OK;改变了它的值并且没有播放声音。 hr 的值设置为 -2147200968 对应于 Error 0x80045038: SPERR_STREAM_CLOSED (source/list of error codes )

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

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

Here is the code for the function 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;
}

Here is the code from the Text-To-Speech function

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;
}