//下面的代码调用sapi,可以实现朗读文本文件,
//但是不知道如何把文本转换成wav文件?
//安装的控件 Microsoft Speech SDK 5.1#include <sapi.h>void CSapitestDlg::OnSpeak()
{
UpdateData();
ISpVoice * pVoice = NULL; if (FAILED(CoInitialize(NULL)))
{
AfxMessageBox("Error to intiliaze COM");
return;
}
HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
if( SUCCEEDED( hr ) )
{
hr = pVoice->Speak(m_Text.AllocSysString(), 0, NULL);
pVoice->Release();
pVoice = NULL;
} CoUninitialize();
}
//但是不知道如何把文本转换成wav文件?
//安装的控件 Microsoft Speech SDK 5.1#include <sapi.h>void CSapitestDlg::OnSpeak()
{
UpdateData();
ISpVoice * pVoice = NULL; if (FAILED(CoInitialize(NULL)))
{
AfxMessageBox("Error to intiliaze COM");
return;
}
HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
if( SUCCEEDED( hr ) )
{
hr = pVoice->Speak(m_Text.AllocSysString(), 0, NULL);
pVoice->Release();
pVoice = NULL;
} CoUninitialize();
}
http://topic.csdn.net/t/20031110/14/2445499.html
=================================================
case IDC_SAVETOWAV:
{
USES_CONVERSION; TCHAR szFileName[256];
_tcscpy(szFileName, _T( "\0 ")); BOOL bFileOpened = CallSaveFileDialog( szFileName,
_T( "WAV (*.wav)\0*.wav\0All Files (*.*)\0*.*\0 ") ); if (bFileOpened == FALSE) break; wcscpy( m_szWFileName, T2W(szFileName) ); CSpStreamFormat OriginalFmt;
hr = m_cpVoice-> GetOutputStream( &cpOldStream );
if (hr == S_OK)
{
hr = OriginalFmt.AssignFormat(cpOldStream);
}
else
{
hr = E_FAIL;
}
// User SAPI helper function in sphelper.h to create a wav file
if (SUCCEEDED(hr))
{
hr = SPBindToFile( m_szWFileName, SPFM_CREATE_ALWAYS, &cpWavStream, &OriginalFmt.FormatId(), OriginalFmt.WaveFormatExPtr() );
}
if( SUCCEEDED( hr ) )
{
// Set the voice 's output to the wav file instead of the speakers
hr = m_cpVoice-> SetOutput(cpWavStream, TRUE);
} if ( SUCCEEDED( hr ) )
{
// Do the Speak
HandleSpeak();
} // Set output back to original stream
// Wait until the speak is finished if saving to a wav file so that
// the smart pointer cpWavStream doesn 't get released before its
// finished writing to the wav.
m_cpVoice-> WaitUntilDone( INFINITE );
cpWavStream.Release(); // Reset output
m_cpVoice-> SetOutput( cpOldStream, FALSE );
case IDC_SAVETOWAV:
{
USES_CONVERSION;
TCHAR szFileName[256];
_tcscpy(szFileName, _T( "\0 "));
BOOL bFileOpened = CallSaveFileDialog(szFileName,
_T( "WAV (*.wav)\0*.wav\0All Files (*.*)\0*.*\0 ") ); if (bFileOpened == FALSE)break;
wcscpy(m_szWFileName,T2W(szFileName));
CSpStreamFormat OriginalFmt;
hr = m_cpVoice->GetOutputStream(&cpOldStream );
if (hr == S_OK)
{
hr = OriginalFmt.AssignFormat(cpOldStream);
}
else
{
hr = E_FAIL;
}
if(SUCCEEDED(hr))
{
hr=SPBindToFile(m_szWFileName,SPFM_CREATE_ALWAYS,&cpWavStream, &OriginalFmt.FormatId(),OriginalFmt.WaveFormatExPtr());
}
if(SUCCEEDED(hr))
{
hr=m_cpVoice->SetOutput(cpWavStream,TRUE);
} if(SUCCEEDED(hr))
{
HandleSpeak();
} m_cpVoice->WaitUntilDone(INFINITE);
cpWavStream.Release();
m_cpVoice->SetOutput(cpOldStream,FALSE);