c++ - 如何在 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
函数时,我得到以下列表:
第一行显示默认的音频输出,下面两个是可用的输出。在 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,对应于错误 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;
}
推荐阅读
- javascript - Ajax/PageMethod 都返回完整页面 HTML 而不是执行 webmethod 并正确返回
- angular - 如何将复选框动态添加到角度形式(Angular 4)
- tensorflow - 为什么在训练我的模型时我在 Keras 中的损失没有改变?
- php - Silverstripe 4 中的 GroupedList,按 many_many_extrafield 分组
- node.js - 如何解析带有文件路径作为变量之一的查询字符串并使用该文件路径变量下载文件?
- android - Python 3 的 Kivy Buildozer 要求错误
- c# - 使用 Linq 查找具有属性的构造函数
- linux - 执行 JAR 文件后如何在命令行中仅模拟一个 ENTER
- ios - 如何确定 iOS 上支持小写的字体是什么?
- javascript - NPM 启动开发服务器问题