Click to See Complete Forum and Search --> : SAPI Speech SDK User-input error


Daedalus357
October 14th, 2008, 10:46 PM
I'm currently working with developing an application (currently console-based) with Text-To-Speech capability. So far my implementation of the TTS SDK into my program has been almost seamless, until i attempted to implement a user-input action. up to this point, all the application's TTS has been pre-designated in each line. I want my application to be able to read the user's name back to them as it is speaking, to make it a little more... "friendly" for a lack of better terms.

anyway, the code in question is from my prototype AI (see attached ode snippet). As i said, i want to be able to read my user's input, send it to the TTS speech, and have the program read it.

this is the error i receive when i compile / build my program:

error C2664: 'Speak' : cannot convert parameter 1 from 'char' to 'const unsigned short *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.

I've tried searching through the MSDN knowledge database, specifically on the SAPI Speech SDK, but to no avail. This has been an ongoing headache for the past 5 hours, so any help or suggestions will be greatly appreciated!



-----------------CODE SNIPPET-----------------------------

#include <stdafx.h>
#include <sapi.h>
#include <iostream>


int main(int argc, int response, char* argv[])
{
ISpVoice *pVoice = NULL;

if (FAILED(::CoInitialize(NULL)))
return FALSE;

HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
if( SUCCEEDED( hr ) )
{
std::cout << "Good morning user.\n";
hr = pVoice->Speak(L"Good Morning User", 0, NULL);
std::cout << "How are you today?\n\n";
hr = pVoice->Speak(L"How are you today?", SPF_IS_XML, NULL );

std::cout << "please select a response:\n 1) I am well\n 2) I am not well\n selection: ";

std::cin >> response;

/* -------PROBLEMATIC USER-INPUT CODE----------

char test;
std::cin >> test;

pVoice->Speak(test, 0, NULL);

-----------END OF PROBLEMATIC CODING---------*/


//------------Functional Code resumes here----------


while (response !=1 || response !=2)
{
if (response == 1)
{
std::cout << "I am happy to hear that, user\n";
hr = pVoice->Speak(L"I am happy to hear that, user.", 0, NULL);
return 0;
}
else if (response == 2)
{
std::cout << "I am sorry to hear that, user.\n";
hr = pVoice->Speak(L"I am sorry to hear that, user.", 0, NULL);
return 0;
}
else
{
std::cout << "I am sorry user, that choice was invalid.\n";
hr = pVoice->Speak(L"I am sorry user, that choice was invalid.", 0, NULL);

std::cout << "please select a response:\n 1) I am well\n 2) I am not well\n selection: ";
std::cin >> response;
}
}

pVoice->Release();
pVoice = NULL;
}
::CoUninitialize();
return TRUE;
}

UnfitElf
October 15th, 2008, 03:44 AM
Hi Daedalus357,

I just tryed your sample code and managed to get it compiling fine..

In my sapi.h file Speak takes a LPCWSTR as its first paramater.

In your code there are a couple of mistakes :) look at the following

char test[MAX_PATH]; // <-- Want more than one character
std::cin >> test; // <-- Get user input

pVoice->Speak(CA2T(test), 0, NULL); // <-- CA2T() converts to a wide char type that Speak is wanting


Here is a simple example for you to play around with

ISpVoice *pVoice = NULL;

if (FAILED(::CoInitialize(NULL)))
return FALSE;

HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
if (SUCCEEDED(hr))
{
char name[128];
std::cout << "Please enter your name: ";
std::cin >> name;

char sentence[1024];
strcpy(sentence, "Hello ");
strcat(sentence, name);
strcat(sentence, ", how are you today? Oh i have to go, talk to you tomorrow!");

hr = pVoice->Speak(CA2W(sentence), 0, NULL);

std::cout << "Good morning user.\n";

pVoice->Release();
pVoice = NULL;
}

::CoUninitialize();
return TRUE;

Notsosuperhero
October 15th, 2008, 11:40 AM
You could use the Windows function MultiByteToWideChar (http://msdn.microsoft.com/en-us/library/ms776413(VS.85).aspx) to convert it.

Like UnfitElf said, you need an array not just single char for a whole sentence.


char Test[256];
wchar_t wTest[256];
std::cin >> Test;
MultByteToWideChar(0, 0, Test, 256, wTest, 256);
pVoice->Speak(wTest, 0, 0);

Daedalus357
October 15th, 2008, 11:48 AM
Hi Daedalus357,

I just tryed your sample code and managed to get it compiling fine..

In my sapi.h file Speak takes a LPCWSTR as its first paramater.

In your code there are a couple of mistakes :) look at the following

char test[MAX_PATH]; // <-- Want more than one character
std::cin >> test; // <-- Get user input

pVoice->Speak(CA2T(test), 0, NULL); // <-- CA2T() converts to a wide char type that Speak is wanting



What include file do i need in order to run CA2T// CA2W? it comes up as an Undeclared Identifier... (this is the first i've heard of that code, so i'm unfamiliar as to what it needs to run).

Notsosuperhero
October 15th, 2008, 12:16 PM
Include the file AtlBase.h for CA2T/CA2W