Text to Speech Integration for C++ project

I have been trying to integrate the Text-to-Speech Tutorial (SAPI 5.3) from Microsoft msdn site http://msdn.microsoft.com/en-us/library/ms720163%28v=vs.85%29 and just cant figure it out. I am trying to get the program to turn the text it outputs into the cmd window in to speech. Its just a little project I have been working on and any help would be great thanks. I all ready have the #included files I need in the beginning of the code. I am still new at c++ but I am able to write some very neat cmd programs but this Text to speech just stumps me, thanks. just a person trying to learn something new.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>
#include <ctime>
#include <stdafx.h>
#include <sapi.h>


int main(int argc, char* argv[])
{
std::string Response[] = {
"HELLO",
"HOW ARE YOU?",
"BYE.",
"AWSOME."
};

srand((unsigned) time(NULL));

std::string sInput = "";
std::string sResponse = "";

while(1) {
std::cout << ">";
std::getline(std::cin, sInput);
int nSelection = rand() % 5;
sResponse = Response[nSelection];
std::cout << sResponse << std::endl;
}

return 0;
}

Last edited on
The problem is with line 25. Declare nSelection outside of it.
There was also crashing when trying to access an element ouside of Response[] range. Response[] is created with '3' elements. Namely 0, 1, 2, 3. But rand() % 5 will give a range of 0 to 4...see the problem ?
I took the liberty to make the changes here :D
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
int main(int argc, char* argv[])
{	std::string Response[] = {
		"HELLO",
		"HOW ARE YOU?",
		"BYE.",
		"AWSOME.",
		"SHAZAM!!!"
	};

	srand((unsigned) time(NULL));

	std::string sInput = "";
	std::string sResponse = "";

	int nSelection =0;

	srand((unsigned) time(NULL));

	while(1) {
		std::cout << ">";
		std::getline(std::cin, sInput);

		nSelection = rand() % 5;
		sResponse = Response[nSelection];
		std::cout << sResponse << std::endl;

		if(sInput == "q")   //a way to break the loop !
		{	std::cout << "CYA LATER XD";
			break;
		}
	}
char temp;
cin >> temp; //simple pause before exiting

return 0;
}

OH and this
1
2
sResponse = Response[nSelection];
std::cout << sResponse << std::endl;

can be simplified to this: std::cout << Response[nSelection] << std::endl;
which can be simplified even more but I'll let u figure it out :D
Cheers
Last edited on
Response[] is created with '3' elements. Namely 0, 1, 2, 3.


I think you meant to put 0, 1 and 2.
@Cye
The link you posted has working examples and it should be fairly simple to adapt your program to use it. Specifically look at step 4. Heres my simple example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
#include <ctime>
#include <sapi.h>

//type something in to have it spoken!
//enter quit to exit
int main()
{
	 ISpVoice * pVoice = NULL;

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

    HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);    
	if(SUCCEEDED(hr))
	{		
		std::wstring in = L"";

		while( std::getline(std::wcin, in) && in != L"quit") {						
		
			hr = pVoice->Speak((LPCWSTR)in.c_str(), NULL, NULL);        
		}
	
		pVoice->Release();
        pVoice = NULL;
	}
    ::CoUninitialize();

	return EXIT_SUCCESS;
}
Response[] is created with '3' elements. Namely 0, 1, 2, 3.


I think you meant to put 0, 1 and 2.

Oopsie. I meant 4 elements - lines 11 through 14 in Cye's original post.
Thanks for all the help so far and the fast responses, will look in to this later to day and see if I can solve this after all, will let ya know thanks again.

soranz

I made that statment even simpler ass you stated was possible bye adding using namespace std; and removing all the std:: from my code.

1
2
nSelection = rand() % 5;
		cout << Response[nSelection] << endl;
Last edited on
This is also possible:
cout << Response[rand() % 5] << endl;
But, for ur own readability it might make more sense not to do this - I just wanted to point it out XD
Topic archived. No new replies allowed.