首页 > 代码库 > Microsoft speech sdk 5.1 text to speech

Microsoft speech sdk 5.1 text to speech

//用于将文字转化成的语音保存成本地的pcm文件

 

long CPlugin14Ctrl::OnSpeak(LPCTSTR uContents, long uVolume)
{
// TODO: Add your dispatch handler code here

CString cStr=uContents ;
//AfxMessageBox(cStr);

//COM初始化:

CComPtr<ISpVoice> m_cpVoice;
HRESULT hr = m_cpVoice.CoCreateInstance(CLSID_SpVoice );

if (FAILED(::CoInitialize(NULL)))
return -1;

TCHAR szFileName[256]="X:\\xx.wav"; //假设这里面保存着目标文件的路径

USES_CONVERSION;
WCHAR m_szWFileName[1000];

wcscpy( m_szWFileName, T2W(szFileName) );//转换成宽字符串


//设置当前采样频率
CComPtr<ISpAudio> m_cpOutAudio1; //声音输出接口
SpCreateDefaultObjectFromCategoryId( SPCAT_AUDIOOUT, &m_cpOutAudio1 ); //创建接口
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 );
}

//创建一个输出流,绑定到wav文件
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;
// 使用sphelper.h中提供的函数创建 wav 文件

if (SUCCEEDED(hr))
{
hr = SPBindToFile( m_szWFileName, SPFM_CREATE_ALWAYS, &cpWavStream,
&OriginalFmt.FormatId(), OriginalFmt.WaveFormatExPtr() );
}
if( SUCCEEDED( hr ) )
{
//设置声音的输出到 wav 文件,而不是 speakers
m_cpVoice->SetOutput(cpWavStream, TRUE);
}

//声音设置范围 0--100
m_cpVoice->SetVolume(uVolume);

//设置语速-10---10
m_cpVoice->SetRate(-1);

//开始朗读
BSTR bstr = cStr.AllocSysString();
m_cpVoice->Speak(bstr, SPF_ASYNC | SPF_IS_NOT_XML, 0 );


//等待朗读结束
m_cpVoice->WaitUntilDone( INFINITE );
cpWavStream.Release();

//获取当前采样频率 44KHZ 32-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);

*/
//把输出重新定位到原来的流
m_cpVoice->SetOutput( cpOldStream, FALSE );

//释放BSTR
SysFreeString(bstr);


//结束要释放资源
::CoUninitialize();


return 0;
}

Microsoft speech sdk 5.1 text to speech