I created a little program in C++ where I use the SAPI library. In my code, I list the number of voices installed on my system. When I compile, I get 11, but there are only 8 installed and the only voice speaking is Microsoft Anna. I checked it in the registry (HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices).
I have several languages installed , especially languages from the Microsoft Speech Platform but none can be used.
Furthermore, when I change the voice ID, I get an unhandled exception error and I think it is because the chosen ID does not exist.
Here is my code
#include "stdafx.h"
int main( int argc, char* argv[] )
{
CComPtr<ISpObjectToken> cpVoiceToken;
CComPtr<IEnumSpObjectTokens> cpEnum;
ISpVoice * pVoice = NULL;
ULONG count = 0;
string str;
if( FAILED( ::CoInitialize( NULL ) ) )
return FALSE;
HRESULT hr = CoCreateInstance( CLSID_SpVoice, NULL, CLSCTX_ALL,
IID_ISpVoice, ( void ** )&pVoice );
if( SUCCEEDED( hr ) )
{
//Enumerate Voices
hr = SpEnumTokens( SPCAT_VOICES, NULL /*L"Gender=Female"*/, NULL, &cpEnum);
printf( "Success\n" );
}
else
{
printf( "Failed to initialize SAPI" );
}
if( SUCCEEDED( hr ) )
{
//Get number of voices
hr = cpEnum->GetCount( &count );
printf( "TTS voices found: %i\n", count );
}
else
{
printf( "Failed to enumerate voices" );
hr = S_OK;
}
if( SUCCEEDED( hr ) )
{
cpVoiceToken.Release();
cpEnum->Item( 3, &cpVoiceToken ); //3 represents the ID of the voice
pVoice->SetVoice( cpVoiceToken );
hr = pVoice->Speak( L"You have selected Microsoft Server Speech Text to Speech Voice (en-GB, Hazel) as the computer's default voice.", 0, NULL ); //speak sentence
pVoice->Release();
pVoice = NULL;
}
::CoUninitialize();
system( "PAUSE" );
}
The only voice working is Microsoft Anna, and I don't understand why. If the other languages were not available, the program won't show me that there are so many(11). I wonder if the Microsoft Speech Platform languages are compatible with SAPI.
After many tries and fails, I managed to find an answer to my problem.
I compiled my program in Win32. So I decided to change it to x64 and I recompiled the solution. I changed the voice ID in my program, and the voices from the Microsoft Speech Platform worked. This means that the MS Speech Platform languages are 64 bit voices and Microsoft Anna is a 32 bit voice.
The following post inspired me.
Related
I have two discrete video adapters on my PC: GTX 1060 and RTX 2080 ti.
I would like to use second one for my DXUT app. I found command line argument -adapter# to specify it, however, my program crashed when I run with -adapter1 (1 is adapter ordinal for RTX2080) option.
I started debugging and figured out the following issue: EnumOutputs returns only DXGI_ERROR_NOT_FOUND.
For GTX1060 first EnumOutputs call returns correct output.
Code section:
HRESULT CD3D11Enumeration::EnumerateOutputs( _In_ CD3D11EnumAdapterInfo* pAdapterInfo )
{
HRESULT hr;
IDXGIOutput* pOutput;
for( int iOutput = 0; ; ++iOutput )
{
pOutput = nullptr;
//next line returns at once DXGI_ERROR_NOT_FOUND for RTX2080
hr = pAdapterInfo->m_pAdapter->EnumOutputs( iOutput, &pOutput );
if( DXGI_ERROR_NOT_FOUND == hr )
{
return S_OK;
}
...
}
hr = EnumerateOutputs( pAdapterInfo );
if( FAILED( hr ) || pAdapterInfo->outputInfoList.empty() ) //failed here cause second condition is true
{
delete pAdapterInfo;
continue;
}
Who know how to fix this problem?
All drivers are up to date.
P.S. I also tried to specify graphic card via Windows and GeForce software, but It seems appropriate only for laptop case with both integrate/discrete cards.
Oh my God..
The problem is only GTX1060 connected to my monitor.
My tutor explained me that it's impossible to render frame buffer in this case.
I'm developing a C++ application which implements the Microsoft Speech API (SAPI). I developed numerous functions related to Text-To-Speech. Among them a function which allows the listing of the audio outputs, and a function that allows to define an audio output.
I started developing this program on Windows 7, but now I switched to Windows 10. However, the function that defines the audio output doesn't work anymore. I didn't edited a thing in my code, and on Windows 7 it worked perfectly.
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;
}
When I start my program and call the getAudioOut function, I get following listing:
The first line shows the default audio output, and the two below are the available outputs. On Windows 7, when I set the second audio output (Lautsprecher / Kopfhörer) as default, then no sound comes out from the first (Digitalaudio), which makes sense. However, on Windows 10 I have reproduced the same procedure but it doesn't work. The audio output is always defined according to the audio menu.
My question is, does anyone have experienced this issue? Is there an alternative to define an audio output programmatically?
I edited the code as #NikolayShmyrev suggested but it didn't change a thing. However, I continued to dig into the problem an found out that the issue came from another function. Indeed, when I switched from Windows 7 to Windows 10, I encountered other problems with the speech synthesis function and the speech to WAV file function. When I started the program and called the Text-To-Speech function, everything worked great. When I called the Speech2Wav function, it worked too. However, when I recalled the Text-To-Speech function, the variable HRESULT hr = S_OK; changed its value and no sound was played. The value of hr was set to -2147200968 which corresponds to Error 0x80045038: SPERR_STREAM_CLOSED (source/list of error codes)
To solve this issue I had to define an audio output like this cpVoice->SetOutput( cpAudioOutToken, TRUE ); in the Text-To-Speech function.
This brings us back to the problem I stated above. When I set the audio output in the function setAudioOut, I release its value at the end cpAudioOutToken.Release();
However, I reuse the same variable in the Text-To-Speech function. Its value was set to nothing because I released it when I defined the audio output. This is why the audio output was always set to default. In order to solve the problem, I assigned the value of cpAudioOutToken to another variable called 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;
}
I am using C++ to code a neuroscience experiment in my research lab. We are studying tactile perception, and we use a parallel port to trigger our brain stimulating device. The timing is very important.
We recently started using xaudio2 to play very simple WAV files, which are used to trigger our vibrotactile stimulators (for example, our "tactile" stimuli are 100 and 200 Hz sounds with a duration of 100 ms, that move a piezo-electric stimulator that is placed on the hand).
Our problem is that we need to send out 3 commands to the brain stimulator via the parallel port: once 40 ms before the tactile stimulus, once 10 ms after the start of the stimulus, and a third time 60 ms into the stimulus. Remember, the tactile stimulus lasts 100 ms.
However, the way xaudio2 triggers sound is that it plays the wave and blocks until it is finished. As a consequence, the program ignores the two parallel port commands which should be sent during the stimulus.
Does anybody know how I can make sure the tactile stimulus is still triggered for the entirety of its 100 ms duration, but also send out parallel port commands during it? I am using the MSDN XAudio2Samples as the basic structure for playing the wav files, and the PlayWave function is the one which "blocks" any other input while the Wav file is playing -- but I can't figure out how to modify it so that it will also take my parallel port commands (which are Out32(888,1)) while a sound is being played.
Thank you!
Here is the code for the PlayWave function:
//--------------------------------------------------------------------------------------
// Name: PlayWave
// Desc: Plays a wave and blocks until the wave finishes playing
//--------------------------------------------------------------------------------------
_Use_decl_annotations_
HRESULT PlayWave( IXAudio2* pXaudio2, LPCWSTR szFilename )
{
//
// Locate the wave file
//
WCHAR strFilePath[MAX_PATH];
HRESULT hr = FindMediaFileCch( strFilePath, MAX_PATH, szFilename );
if( FAILED( hr ) )
{
wprintf( L"Failed to find media file: %s\n", szFilename );
return hr;
}
//
// Read in the wave file
//
std::unique_ptr<uint8_t[]> waveFile;
DirectX::WAVData waveData;
if ( FAILED( hr = DirectX::LoadWAVAudioFromFileEx( strFilePath, waveFile, waveData ) ) )
{
wprintf( L"Failed reading WAV file: %#X (%s)\n", hr, strFilePath );
return hr;
}
//
// Play the wave using a XAudio2SourceVoice
//
// Create the source voice
IXAudio2SourceVoice* pSourceVoice;
if( FAILED( hr = pXaudio2->CreateSourceVoice( &pSourceVoice, waveData.wfx ) ) )
{
wprintf( L"Error %#X creating source voice\n", hr );
return hr;
}
// Submit the wave sample data using an XAUDIO2_BUFFER structure
XAUDIO2_BUFFER buffer = {0};
buffer.pAudioData = waveData.startAudio;
buffer.Flags = XAUDIO2_END_OF_STREAM; // tell the source voice not to expect any data after this buffer
buffer.AudioBytes = waveData.audioBytes;
if ( waveData.loopLength > 0 )
{
buffer.LoopBegin = waveData.loopStart;
buffer.LoopLength = waveData.loopLength;
buffer.LoopCount = 1; // We'll just assume we play the loop twice
}
#if (_WIN32_WINNT < 0x0602 /*_WIN32_WINNT_WIN8*/)
if ( waveData.seek )
{
XAUDIO2_BUFFER_WMA xwmaBuffer = {0};
xwmaBuffer.pDecodedPacketCumulativeBytes = waveData.seek;
xwmaBuffer.PacketCount = waveData.seekCount;
if( FAILED( hr = pSourceVoice->SubmitSourceBuffer( &buffer, &xwmaBuffer ) ) )
{
wprintf( L"Error %#X submitting source buffer (xWMA)\n", hr );
pSourceVoice->DestroyVoice();
return hr;
}
}
#else
if ( waveData.seek )
{
wprintf( L"This platform does not support xWMA or XMA2\n" );
pSourceVoice->DestroyVoice();
return hr;
}
#endif
else if( FAILED( hr = pSourceVoice->SubmitSourceBuffer( &buffer ) ) )
{
wprintf( L"Error %#X submitting source buffer\n", hr );
pSourceVoice->DestroyVoice();
return hr;
}
hr = pSourceVoice->Start( 0 );
// Let the sound play
BOOL isRunning = TRUE;
while( SUCCEEDED( hr ) && isRunning )
{
XAUDIO2_VOICE_STATE state;
pSourceVoice->GetState( &state );
isRunning = ( state.BuffersQueued > 0 ) != 0;
// Wait till the escape key is pressed
if( GetAsyncKeyState( VK_ESCAPE ) )
break;
Sleep( 10 );
}
// Wait till the escape key is released
while( GetAsyncKeyState( VK_ESCAPE ) )
Sleep( 10 );
pSourceVoice->DestroyVoice();
return hr;
}
The following code runs fine:
HRESULT hr = S_OK;
D2D1_FACTORY_OPTIONS options;
ZeroMemory( &options, sizeof( D2D1_FACTORY_OPTIONS ) );
#ifdef _DEBUG
// options.debugLevel = D2D1_DEBUG_LEVEL_INFORMATION;
#endif
hr = D2D1CreateFactory(
D2D1_FACTORY_TYPE_SINGLE_THREADED,
__uuidof( ID2D1Factory2 ),
&options,
&m_sp2DFactory
);
if ( FAILED( hr ) )
return hr;
However, if I remove the //, thus enabling the D2D1_DEBUG_LEVEL_INFORMATION I get:
hr: E_NOINTERFACE No such interface supported.
My System is the latest Windows 10 TechPreview. I am using Visual Studio 2013. As far as I understand it, all DirectX Debugging libraries come with Visual Studio 2013. Is the D2D1_DEBUG_LEVEL_INFORMATION flag no longer supported? I couldn't find any info on that.
Does anyone know how to get hold of the Excel.Application IDispatch* pointer associated with an excel process into which an dll has been loaded?
A key thing here is that the process is excel.exe, and the pointer I need must belong to that process. Using the Running Object Table will not fly since Excel only registers its first instance with that.
I'm hoping there is some low-level COM trickery, but I'm not an expert in that field.
EDITED II Code is under the WTFPL license version 2.
EDITED: Add PID parameter to allow filtering when several Excel processes are currently running, as per comment suggestion from #EricBrown.
I managed to get a working IDispatch* to an Excel "Application" object without using the ROT. The trick is to use MSAA. My code works as a stand alone console application, but I think that if the code is executed in an Excel process, via DLL Injection, it MAY works fine. You may have to be in a dedicated thread. Let me know if you want me to push the expriment to the DLL injection level.
Tested OK on Window7 64b, with a UNICODE builds (32 bits and 64 bits).
Excel version 2010 64 bits (version "14")
I get the IDispatch via the "application" property from an "Worksheet" object. Consequence: there must be an opened worksheet. In order to find the good MSSA Window, I need the class name of the Top Level Excel Frame Window. In Excel 2010, it's "XLMAIN". The class name for worksheets is "EXCEL7" and that seems to be a "standard".
I was not able to directly get a working IDispatch* from the main Excel Window, but have not tried very hard. That may involve #import with a automation DLL from Excel, in order to QueryInterface the IDispatch that MSAA gives for the main Window (that IDispatch is NOT for an Application object)
#include <atlbase.h>
#pragma comment( lib, "Oleacc.lib" )
HRESULT GetExcelAppDispatch( CComPtr<IDispatch> & spIDispatchExcelApp, DWORD dwExcelPID ) {
struct ew {
struct ep {
_TCHAR* pszClassName;
DWORD dwPID;
HWND hWnd;
};
static BOOL CALLBACK ewp( HWND hWnd, LPARAM lParam ) {
TCHAR szClassName[ 64 ];
if ( GetClassName( hWnd, szClassName, 64 ) ) {
ep* pep = reinterpret_cast<ep*>( lParam );
if ( _tcscmp( szClassName, pep->pszClassName ) == 0 ) {
if ( pep->dwPID == 0 ) {
pep->hWnd = hWnd;
return FALSE;
} else {
DWORD dwPID;
if ( GetWindowThreadProcessId( hWnd, &dwPID ) ) {
if ( dwPID == pep->dwPID ) {
pep->hWnd = hWnd;
return FALSE;
}
}
}
}
}
return TRUE;
}
};
ew::ep ep;
ep.pszClassName = _TEXT( "XLMAIN" );
ep.dwPID = dwExcelPID;
ep.hWnd = NULL;
EnumWindows( ew::ewp, reinterpret_cast<LPARAM>( &ep ) );
HWND hWndExcel = ep.hWnd;
if ( ep.hWnd == NULL ) {
printf( "Can't Find Main Excel Window with EnumWindows\n" );
return -1;
}
ep.pszClassName = _TEXT( "EXCEL7" );
ep.dwPID = 0;
ep.hWnd = NULL;
EnumChildWindows( hWndExcel, ew::ewp, reinterpret_cast<LPARAM>( &ep ) );
HWND hWndWorkSheet = ep.hWnd;
if ( hWndWorkSheet == NULL ) {
printf( "Can't Find a WorkSheet with EnumChildWindows\n" );
return -1;
}
CComPtr<IDispatch> spIDispatchWorkSheet;
HRESULT hr = AccessibleObjectFromWindow( hWndWorkSheet, OBJID_NATIVEOM, IID_IDispatch,
reinterpret_cast<void**>( &spIDispatchWorkSheet ) );
if ( FAILED( hr ) || ( spIDispatchWorkSheet == 0 ) ) {
printf( "AccessibleObjectFromWindow Failed\n" );
return hr;
}
CComVariant vExcelApp;
hr = spIDispatchWorkSheet.GetPropertyByName( CComBSTR( "Application" ), &vExcelApp );
if ( SUCCEEDED( hr ) && ( vExcelApp.vt == VT_DISPATCH ) ) {
spIDispatchExcelApp = vExcelApp.pdispVal;
return S_OK;
}
return hr;
}
int _tmain(int argc, _TCHAR* argv[])
{
DWORD dwExcelPID = 0;
if ( argc > 1 ) dwExcelPID = _ttol( argv[ 1 ] );
HRESULT hr = CoInitialize( NULL );
bool bCoUnInitializeTodo = false;
if ( SUCCEEDED( hr ) ) {
bCoUnInitializeTodo = true;
CComPtr<IDispatch> spDispatchExcelApp;
hr = GetExcelAppDispatch( spDispatchExcelApp, dwExcelPID );
if ( SUCCEEDED( hr ) && spDispatchExcelApp ) {
CComVariant vExcelVer;
hr = spDispatchExcelApp.GetPropertyByName( CComBSTR( "Version" ), &vExcelVer );
if ( SUCCEEDED( hr ) && ( vExcelVer.vt == VT_BSTR ) ) {
wprintf( L"Excel Version is %s\n", vExcelVer.bstrVal );
}
}
}
if ( bCoUnInitializeTodo ) CoUninitialize();
return 0;
}
You should be able to find out how to do this by reviewing the code in ExcelDNA. This project contains code that hooks back into Excel from the extension library. The code is likely to be more elaborate that you need, but will implement the reference you require.
This is how I do it: (acknowledge #manuell). dispatch_wrapper is a class, here is the constructor to set m_disp_application:
dispatch_wrapper(void)
{
DWORD target_process_id = ::GetProcessId(::GetCurrentProcess());
if (getProcessName() == "excel.exe"){
HWND hwnd = ::FindWindowEx(0, 0, "XLMAIN", NULL);
while (hwnd){
DWORD process_id;
::GetWindowThreadProcessId(hwnd, &process_id);
if (process_id == target_process_id){
HWND hwnd_desk = ::FindWindowEx(hwnd, 0, "XLDESK", NULL);
HWND hwnd_7 = ::FindWindowEx(hwnd_desk, 0, "EXCEL7", NULL);
IDispatch* p = nullptr;
if (SUCCEEDED(::AccessibleObjectFromWindow(hwnd_7, OBJID_NATIVEOM, IID_IDispatch, (void**)&p))){
LPOLESTR name[1] = {L"Application"};
DISPID dispid;
if (SUCCEEDED(p->GetIDsOfNames(IID_NULL, name, 1U, LOCALE_SYSTEM_DEFAULT, &dispid))){
CComVariant v;
DISPPARAMS dp;
::memset(&dp, NULL, sizeof(DISPPARAMS));
EXCEPINFO ei;
::memset(&ei, NULL, sizeof(EXCEPINFO));
if (SUCCEEDED(p->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, &dp, &v, &ei, NULL))){
if (v.vt == VT_DISPATCH){
m_disp_application = v.pdispVal;
m_disp_application->AddRef();
return;
}
}
}
}
}
hwnd = ::FindWindowEx(0, hwnd, "XLMAIN", NULL);
}
}
m_disp_application = nullptr;
}
getProcessName() returns lower case.
Because Office applications register their documents in the ROT, you can attach to instances beside the first one (which is already in the ROT) by getting IDispatch for documents in the ROT, then you can use document.Application.hwnd (this is VBA, you need to translate to IDispatch::GetIDsOfNames and IDispatch::Invoke with DISPATCH_PROPERTYGET) to get the window handles of all Excel instances.
Now you have a mapping between IDispatch and Windows handles of all Excel instances, it is time to find your own Excel instance. You can call GetWindowThreadProcessId on the window handles to get the process ids, then compare to your own process id returned by GetCurrentProcessId to see which excel window belongs to your current process, and look up in the HWND to IDispatch mapping to find your current Excel application's IDispatch interface.