A PCM file that saves the cost of converting text to speech
Long Cplugin14ctrl::onspeak (LPCTSTR ucontents, long Uvolume)
{
Todo:add your dispatch handler code here
CString cstr=ucontents;
AfxMessageBox (CSTR);
//com initialization:
ccomptr<ispvoice> m_cpvoice;
HRESULT hr = m_cpvoice.cocreateinstance (clsid_spvoice);
if (FAILED (:: CoInitialize (NULL)))
Return-1;
TCHAR szfilename[256]= "x:\\xx.wav";//Assuming this contains the path to the target file
Uses_conversion;
WCHAR m_szwfilename[1000];
wcscpy (M_szwfilename, t2w (szFileName));//Convert to wide string
//Set Current sampling frequency
ccomptr<ispaudio> m_ CpOutAudio1; Sound Output Interface
Spcreatedefaultobjectfromcategoryid (spcat_audioout, &m_cpoutaudio1);//Create Interface
Spstreamformat EFMT = Spsf_44khz16bitmono;
Cspstreamformat Fmt;
Fmt.assignformat (EFMT);
if (m_cpoutaudio1)
{
hr = M_cpoutaudio1->setformat (Fmt.formatid (), fmt.waveformatexptr ());
}
Else hr = E_FAIL;
if (SUCCEEDED (HR))
{
M_cpvoice->setoutput (m_cpoutaudio1, FALSE);
}
Create an output stream, bind to a WAV file
Cspstreamformat originalfmt;
Ccomqiptr<ispstream> Cpwavstream;
Ccomqiptr<ispstreamformat> Cpoldstream;
hr = M_cpvoice->getoutputstream (&cpoldstream);
if (hr = = S_OK)
{
hr = Originalfmt.assignformat (Cpoldstream);
}
Else
hr = E_FAIL;
Create a WAV file using the functions provided in Sphelper.h
if (SUCCEEDED (HR))
{
hr = Spbindtofile (M_szwfilename, Spfm_create_always, &cpwavstream,
&originalfmt.formatid (), originalfmt.waveformatexptr ());
}
if (SUCCEEDED (HR))
{
Set the output of the sound to a WAV file instead of speakers
M_cpvoice->setoutput (Cpwavstream, TRUE);
}
Sound Setting Range 0--100
M_cpvoice->setvolume (Uvolume);
Set speech speed -10---10
M_cpvoice->setrate (-1);
Start reading
BSTR BSTR = Cstr.allocsysstring ();
M_cpvoice->speak (BSTR, Spf_async | Spf_is_not_xml, 0);
Waiting for the reading to end
M_cpvoice->waituntildone (INFINITE);
Cpwavstream.release ();
Gets the current sampling frequency 44khz32-35
/*
Spstreamformat efmt;
Ccomptr<ispstreamformat> Cpstream;
HRESULT Hroutputstream = M_cpvoice->getoutputstream (&cpstream);
if (Hroutputstream = = S_OK)
{
Cspstreamformat Fmt;
hr = Fmt.assignformat (Cpstream);
if (SUCCEEDED (HR))
{
EFMT = Fmt.computeformatenum ();
}
}
CString Strint;
Strint.format ("%d", efmt);
AfxMessageBox (Strint);
*/
Reposition the output to the original stream
M_cpvoice->setoutput (Cpoldstream, FALSE);
Releasing a BSTR
SysFreeString (BSTR);
End to release resources
:: CoUninitialize ();
return 0;
}
Microsoft Speech SDK 5.1 text to Speech