用SAPI的delphi包控件有spvoice,SpFileStream,spAdouiout等控,现我想TTS读出的文字保存成wav文件(路胫指定),以下给出VC代码,求对应的delphi代码,十wan火急!!!!
------------------------------------------------
vc代码
=================================================
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 );-----------------------------------------------------------------------
------------------------------------------------
vc代码
=================================================
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 );-----------------------------------------------------------------------
szFileName: array[0..255] of char;
bFileOpened: boolean;
OriginalFmt: CSpStreamFormat;
begin
case XX of
IDC_SAVETOWAV:
begin
// USES_CONVERSION; szFileName[0] := #0; bFileOpened := OpenDiaLog1.Execute;
// CallSaveFileDialog( szFileName,
// _T("WAV (*.wav)\0*.wav\0All Files (*.*)\0*.*\0") ); if (not bFileOpened) then
exit;
szFileName := OpenDialog1.FileName;// wcscpy(m_szWFileName, T2W(szFileName));
GetMem(m_szWFileName, SizeOf(szFileName) * 2);
WideCharToMultiByte(CP_ACP, MB_COMPOSITE, Pchar(szFileName), SizeOf(szFileName),
PWideChar(m_szWFileName), SizeOf(m_szWFileName));
hr := m_cpVoice^.GetOutputStream(@cpOldStream);
if (hr = S_OK) then
begin
hr := OriginalFmt.AssignFormat(cpOldStream);
end
else
begin
hr := E_FAIL;
end;
// User SAPI helper function in sphelper.h to create a wav file
if (SUCCEEDED(hr)) then
begin
hr := SPBindToFile(m_szWFileName, SPFM_CREATE_ALWAYS, @cpWavStream,
@OriginalFmt.FormatId(), OriginalFmt.WaveFormatExPtr());
end;
if (SUCCEEDED(hr)) then
begin
// Set the voice's output to the wav file instead of the speakers
hr := m_cpVoice^.SetOutput(cpWavStream, TRUE);
end; if (SUCCEEDED(hr)) then
begin
// Do the Speak
HandleSpeak();
end; // 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);
end;
end;
end;
SAPI 函数function BindToFile(var pszFileName: Word; eMode: SPFILEMODE; var pFormatId: TGUID;var pWaveFormatEx: WaveFormatEx; ullEventInterest: Largeuint): HResult; stdcall;
我就是想把文字读出时保存成wav文件,可是我搞了好久都搞不定,急死人!!将代吗贴出来给你指点一下:
-------------------------------------------------------------------
uses SpeechLib_TLB, OleServer,MMsystem;//包含单元
-------------------------------------------------------------------
public//定义公共变量
pMode:SPFILEMODE; voiceObj:ISpvoice;
steamFmtobj:ISpStreamFormat;
streamobj:ISpStream;
guid:TGUID; end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);//一按钮事件(将读出的文字保存到指定的wav文件)
var
pGuid:pointer;
iSpeechFlags :integer;//flag
pwFileName :PWideChar;//保存目录
wTemp :word;//目录地址
WFormatEx :WaveFormatEx;//格式
text: PWideChar;
iOutCount: cardinal;
begin
iSpeechFlags:= SVSFlagsAsync or SVSFPurgeBeforeSpeak or SVSFIsXML;
//wTemp:=PWideString('c:\1.wav');
pwFileName:='c:\1.wav';
wTemp:=integer(@pwFileName);
WFormatEx.wFormatTag:=WAVE_FORMAT_PCM;
WFormatEx.nChannels:=1;//mono
WFormatEx.nSamplesPerSec:=11025; //11kHz sampling
WFormatEx.nAvgBytesPerSec:=11025; //we aim to use 8 bit sound so only 11k per second
WFormatEx.nBlockAlign:=4; //only 4 byte in each sample
WFormatEx.wBitsPerSample:=16; //采样16bit
WFormatEx.cbSize:=0; //WFormatEx:=WAVE_FORMAT_1M16;
//创建com
voiceObj:=createcomobject(CLASS_SpVoice) as ISpvoice;
voiceObj.GetOutputStream(steamFmtobj);
streamobj:=createcomobject(CLASS_SpStream) as ISpStream;
olecheck(steamFmtobj.GetFormat(guid,pGuid));//这里出错了,说什么指针无效
{-------------------------------------------------------
hr = cpSpStream->BindToFile(WAVE_FILENAME,
SPFM_CREATE_ALWAYS,
&Fmt.FormatId(),
Fmt.WaveFormatExPtr(),
NULL);
-------------------------------------------------------}
{pchar('c:\1.wav')}
{ function BindToFile(var pszFileName: Word; eMode: SPFILEMODE; var pFormatId: TGUID;
var pWaveFormatEx: WaveFormatEx; ullEventInterest: Largeuint): HResult; stdcall;
}
olecheck(streamobj.BindToFile( wTemp,
SPFM_CREATE_ALWAYS,
guid,
WFormatEx,
0 ));
olecheck(voiceObj.SetOutput(streamobj,1)); text:='hello,hello,hello,hello'; olecheck(voiceObj.Speak(text,0,iOutCount)); olecheck(voiceObj.WaitUntilDone(1000));
streamobj.Close;
showmessage('x');
end;-------------------------------------------------------------------------
以上代码是根据上面的VC代码过来的,我对VC不太了解,请aiirii兄帮帮我