Hi David,
Good job as i just comfirmed the same with the code you set!

Cheers,
Charlie


  ----- Original Message ----- 
  From: burkleyd 
  To: advanced_delphi@yahoogroups.com 
  Sent: Saturday, February 14, 2009 2:24 PM
  Subject: [advanced_delphi] Re: Redirect Speech API (SAPI) Text to Speech to a 
WAV file


  --- In advanced_delphi@yahoogroups.com, "Glenn Lawler" <gblaw...@...>
  wrote:
  >
  > We use SAPI for text to speech in a number of utility
  > programs and embedded in a number of applications we
  > provide to our customer base.
  > 
  > I recently had an opportunity which would be
  > enhanced if we could save the audio output of
  > text to speech to a WAV file.
  > 
  > I Did a little research and found that SAPI does indeed
  > have that capability. (see:
  > http://msdn.microsoft.com/en-us/library/ms723597(VS.85).aspx
  > )
  > 
  > Here is a short code stub of what we are doing:
  > 
  > --------------------------
  > var
  > SAPI_OLEObject: variant;
  > SAPI_FileObject: variant;
  > ErrorMsg: string;
  > 
  > try
  > SAPI_FileObject:=CreateOLEObject('SAPI.SpFileStream');
  > except
  > on E: Exception do begin
  > ErrorMsg:='Error creating SAPI File: ' + E.Message;
  > end;
  > end;
  > 
  > try
  > SAPI_FileObject.Open('O:\TMP\Test.wav',SSFMCreateForWrite,False);
  > except
  > on E: Exception do begin
  > ErrorMsg:='Error opening SAPI File: ' + E.Message;
  > end;
  > end;
  > 
  > try
  > SAPI_OLEObject:=CreateOLEObject('SAPI.SpVoice');
  > except
  > on E: Exception do begin
  > ErrorMsg:='Error opening Speech API: ' + E.Message;
  > end;
  > end;
  > 
  > try
  > SAPI_OLEObject.AudioOutputStream:=SAPI_FileObject;
  > except
  > on E: Exception do begin
  > ErrorMsg:='Error assigning SAPI_FileObject: ' + E.Message;
  > end;
  > end;
  > --------------------------
  > 
  > The code goes on to call SAPI_OLEObject.Speak(FName,Flags);
  > to speak the text. The error occurs in the last block on:
  > 
  > SAPI_OLEObject.AudioOutputStream:=SAPI_FileObject;
  > 
  > ErrorMsg = 'Error assigning SAPI_FileObject: Member not found'
  > 
  > First thing I did was Google: Delphi AudioOutputStream
  > and found that many others have had this same problem. I came
  > up with a lot of postings on this exact error, but no one who
  > had figured out how to fix the problem. It would seem that
  > "Member not found" means that AudioOutputStream is not a valid
  > property of object created with the OLE string 'SAPI.SpVoice',
  > however, the documentation from Microsoft seems clear that it
  > is a property. I am using the SAPI that ships with Vista Business,
  > so I believe I should have a new enough version.
  > 
  > We have done quite a bit with OLE automation of MS Office and
  > a lot of other things, and I have run into many situations
  > where you need to do things slightly differently in Delphi than
  > you would do in other languages, like VB, for example. I am
  > guessing this might be one of those cases.
  > 
  > Does anyone have any experience with redirecting SAPI output
  > to a WAV file?
  > 
  > Glenn Lawler
  > www.incodesystems.com
  >

  I found an excellent web page that goes into
  the details about SAPI 5.1. There's also a
  .zip file, containing examples, that you can
  download.

  http://www.blong.com/Conferences/DCon2002/Speech/SAPI51/SAPI51.htm

  From the information I got off the web page (above)
  you'll need to import the type library...
  "Microsoft Speech Object Library (Version 5.0)"
  and install it.

  After you've got it compiled...
  I found the following code on another website
  that actually works. I was able to save the
  audible text, from an edit box, to a .wav file.

  Hope this helps,
  David

  P.S. If you can figure out how to playback
  the saved .wav file thru the Speak method...
  I'd be interested in seeing the code.

  // Be aware of possible line-wrapping
  // Be aware of possible unwanted space(s)
  // Be aware of probable missing indentations

  // Start Of Code
  unit Unit1;

  interface

  uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls, ActiveX, OleServer, SpeechLib_TLB;

  type
  TSAPI_Form = class(TForm)
  Edit1: TEdit;
  Button1: TButton;
  Button2: TButton;
  procedure Button1Click(Sender: TObject);
  procedure FormCreate(Sender: TObject);
  procedure Button2Click(Sender: TObject);
  private
  { Private declarations }
  public
  { Public declarations }
  end;

  var
  SAPI_Form: TSAPI_Form;
  SpFileStream1: TSpFileStream;
  SpVoice1: TSpVoice;
  FileName : TFileName;

  implementation

  {$R *.dfm}

  procedure TSAPI_Form.Button1Click(Sender: TObject);
  begin
  FileName := 'sapi_voice_test.wav';
  SpFileStream1 := TSpFilestream.Create(nil);
  SPVoice1 := TSpVoice.Create(nil);
  SpFileStream1.Open(FileName, SSFMCreateForWrite, False);
  SpVoice1.AudioOutputStream := SPFileStream1.DefaultInterface;
  SpVoice1.Speak(Edit1.Text, SVSFDefault);
  SPFileStream1.Close;
  SPVoice1.Free;
  SpFileStream1.Free;
  end;

  procedure TSAPI_Form.FormCreate(Sender: TObject);
  begin
  Button1.Caption := 'Save Editbox to a Wave file';
  Button2.Caption := 'Speak Editbox';
  Edit1.Text := 'This is a test. And only a test.';
  end;

  procedure TSAPI_Form.Button2Click(Sender: TObject);
  begin
  SPVoice1 := TSpVoice.Create(nil);
  SpVoice1.Speak(Edit1.Text, SVSFDefault);
  SPVoice1.Free;
  end;

  end.
  // End Of Code


  

Reply via email to