Problem Converting Char Array to String When Using Microsoft Speech SDK

I am learning to use the Microsoft Speach SDK 5.1 (SAPI). I want to use the dictate feature of SAPI to capture text, then save that text to a string. I can save the text to a char array using sprintf. I can also even output that char array to a text file. However I cannot find a way to convert the char array to a string which actually results in the text being copied. I have tried multiple methods. Many of them compile, but none of them work.. My compiler is Microsoft Visual C++ .Net 2003.

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200

#include "stdafx.h"
#include <sapi.h>
#include "sphelper.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <string.h>

using namespace std;

string strtextfromspeech = "";
char buffer [50];


int WriteStringToFile()
{
	string newfilename; 
	newfilename = "temp.txt";
	
	ofstream outputfile (newfilename.c_str());
	if (outputfile.is_open())
	{
		outputfile << buffer << "\n";
		
		outputfile.close();
	}
	  else cout << "Unable to open file"; 
	//
	return(0);
}

string ReadStringFromFile()
{
	
	string mline;
	
	ifstream mdatafile ("temp.txt");

	if (mdatafile.is_open())
	{
		
		getline(mdatafile, mline, '\n');
			
		mdatafile.close();
	}

	  else cout << "Unable to open file"; 
	 cout << "mline is " << mline << endl; 
	  return(mline);
}



inline HRESULT BlockForResult(ISpRecoContext * pRecoCtxt, ISpRecoResult ** ppResult)
{

    HRESULT hr = S_OK;
	CSpEvent event;

    while (SUCCEEDED(hr) &&
           SUCCEEDED(hr = event.GetFrom(pRecoCtxt)) &&
           hr == S_FALSE)
    {
        hr = pRecoCtxt->WaitForNotifyEvent(INFINITE);
    }
	
    *ppResult = event.RecoResult();
    if (*ppResult)
    {
        (*ppResult)->AddRef();
	
    }

    return hr;
}

const WCHAR * StopWord()
{
    const WCHAR * pchStop;
    
    LANGID LangId = ::SpGetUserDefaultUILanguage();

    switch (LangId)
    {
        case MAKELANGID(LANG_JAPANESE, SUBLANG_DEFAULT):
            pchStop = L"\x7d42\x4e86\\\x30b7\x30e5\x30fc\x30ea\x30e7\x30fc/\x3057\x3085\x3046\x308a\x3087\x3046";;
            break;

        default:
            pchStop = L"Affirmative";
            break;
    }

    return pchStop;
}

//int GetTextFromSpeech(int argc, char* argv[])
int GetTextFromSpeech()
{
	HRESULT hr = E_FAIL;
    bool fUseTTS = false;            // turn TTS play back on or off


    if (SUCCEEDED(hr = ::CoInitialize(NULL)))
    {
        {
            CComPtr<ISpRecoContext> cpRecoCtxt;
            CComPtr<ISpRecoGrammar> cpGrammar;
            CComPtr<ISpVoice> cpVoice;
            hr = cpRecoCtxt.CoCreateInstance(CLSID_SpSharedRecoContext);
            if(SUCCEEDED(hr))
            {
                hr = cpRecoCtxt->GetVoice(&cpVoice);
            }
           
            if (cpRecoCtxt && cpVoice &&
                SUCCEEDED(hr = cpRecoCtxt->SetNotifyWin32Event()) &&
                SUCCEEDED(hr = cpRecoCtxt->SetInterest(SPFEI(SPEI_RECOGNITION), SPFEI(SPEI_RECOGNITION))) &&
                SUCCEEDED(hr = cpRecoCtxt->SetAudioOptions(SPAO_RETAIN_AUDIO, NULL, NULL)) &&
                SUCCEEDED(hr = cpRecoCtxt->CreateGrammar(0, &cpGrammar)) &&
                SUCCEEDED(hr = cpGrammar->LoadDictation(NULL, SPLO_STATIC)) &&
                SUCCEEDED(hr = cpGrammar->SetDictationState(SPRS_ACTIVE)))
            {
                USES_CONVERSION;
                            
                const WCHAR * const pchStop = StopWord();
                CComPtr<ISpRecoResult> cpResult;

                printf( "I will repeat everything you say.\nSay \"%s\" to exit.\n", W2A(pchStop) );

                while (SUCCEEDED(hr = BlockForResult(cpRecoCtxt, &cpResult)))
                {
                    cpGrammar->SetDictationState( SPRS_INACTIVE );

                    CSpDynamicString dstrText;
		

                    if (SUCCEEDED(cpResult->GetText(SP_GETWHOLEPHRASE, SP_GETWHOLEPHRASE, 
                                                    TRUE, &dstrText, NULL)))
			
                    {
                       printf("I heard:  %s\n If that is correct say *affirmative*\n", W2A(dstrText));
						
						
						

                        if (fUseTTS)
                        {
                            cpVoice->Speak( L"I heard", SPF_ASYNC, NULL);
                            cpVoice->Speak( dstrText, SPF_ASYNC, NULL );
                        }

                     

                       cpResult.Release();
                    }
                    if (_wcsicmp(dstrText, pchStop) == 0)
                    {
                        break;
                    }
					else
					{	
						int n;
						n=sprintf (buffer, "%s", W2A(dstrText));
						buffer[50] = 0;
						printf ("The buffer recorded is: [%s]\n",buffer,n);
	
						strtextfromspeech = buffer;
					  
						//WriteStringToFile();
			
					}
	              
                    cpGrammar->SetDictationState( SPRS_ACTIVE );
                } 
            }
        }
        ::CoUninitialize();
    }
    return hr;
}
            
int main(int argc2, char* argv2[])
{
	GetTextFromSpeech();
	
	//strtextfromspeech = ReadStringFromFile();
	printf ("The string recorded is: %s\n",strtextfromspeech);
	
	
	printf("Type something to quit.");
	
	char strpause[50];
	cin.getline (strpause, 50);

	
	return 0;
}


Here is a typical output:


I will repeat everything you say.
Say "Affirmative" to exit.
I heard: In session
 If that is correct say "affirmative"
I heard: Affirmative
 If that is correct say "affirmative"
the string record is: (null)
Type something to quit.


I want "strtextfromspeech" to equal "buffer". Instead it always equals "(null)". What am I doing wrong? Any help is appreciated.

MN
printf ("The string recorded is: %s\n",strtextfromspeech);//Error

strtextfromspeech is a c++ string class - expecting the printf function to do something sensible with it is expecting too much.

try (get the char* bufffer from the string using the c_str() member function, you know about this function - you have used it earlier in the code)

printf ("The string recorded is: %s\n",strtextfromspeech.c_str());
Last edited on
That fixed it! Thanks for your help guestgulkan.
Topic archived. No new replies allowed.