Search code examples
delphisapi

delphi can't import SAPI 5.1


I'm trying to do some speech recognition with delphi and found this simple project which works great but I can't use it cause I'm not able to import SAPI 5.1.
In the "Import Type Library" Window I can find SAPI 5.4 but sadly I couldn't get it to work with that. Now I'm wondering whether there is a way to import SAPI 5.1 so I can use the example below and extend it for my needs.

unit Unit3;

interface

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

const SP_GETWHOLEPHRASE = -1;

type
  TForm3 = class(TForm)
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private-Deklarationen }
  public
    SpSharedRecoContext1: TSpSharedRecoContext;
    MyGrammar : ISpeechRecoGrammar;
    procedure SpSharedRecoContext1Recognition(ASender: TObject;
                                              StreamNumber: Integer;
                                              StreamPosition: OleVariant;
                                              RecognitionType: SpeechRecognitionType;
                                              const Result: ISpeechRecoResult);
    procedure SpSharedRecoContext1Hypothesis(ASender: TObject;
                                             StreamNumber: Integer;
                                             StreamPosition: OleVariant;
                                             const Result: ISpeechRecoResult);
    { Public-Deklarationen }
  end;

var
  Form3: TForm3;

implementation

{$R *.dfm}

procedure TForm3.FormClose(Sender: TObject; var Action: TCloseAction);
begin
 SpSharedRecoContext1.Free;
end;

procedure TForm3.FormCreate(Sender: TObject);
begin
  SpSharedRecoContext1 := TSpSharedRecoContext.Create(self);
  SpSharedRecoContext1.OnRecognition   := SpSharedRecoContext1Recognition;
  SpSharedRecoContext1.OnHypothesis    := SpSharedRecoContext1Hypothesis;
  MyGrammar := SpSharedRecoContext1.CreateGrammar(0);
  MyGrammar.DictationSetState(SGDSActive);
end;

procedure TForm3.SpSharedRecoContext1Recognition(ASender: TObject;
                                                 StreamNumber: Integer;
                                                 StreamPosition: OleVariant;
                                                 RecognitionType: SpeechRecognitionType;
                                                 const Result: ISpeechRecoResult);
begin
 Caption := '';
 Memo1.Lines.Add(Result.PhraseInfo.GetText(SP_GETWHOLEPHRASE,SP_GETWHOLEPHRASE,true));
end;

procedure TForm3.SpSharedRecoContext1Hypothesis(ASender: TObject;
                                                StreamNumber: Integer;
                                                StreamPosition: OleVariant;
                                                const Result: ISpeechRecoResult);
begin
 Caption := 'I am listening...';
end;


end.

Any help is appreciated,

thx timo314

Edit: I got delphi 6 Personal Edition
SAPI 5.1 is not listed in the "import Type Library" Window
When Compiling I get errors at line 46 and 47 saying "Incompatible Types Parameter Lists differ"

I noticed that the SpeechLib_TLB.pas that came with the example differs from the one the import created in my "Import" Directory.
After I change the Parameter lists of SpSharedRecoContext1Recognition and SpSharedRecoContext1Hypothesis the Application runs but the procedures aren't called. As if the Programm doesn't notice that someone is speaking.


Solution

  • Hard to tell with what you provided, but for me RecognitionType is a TOleEnum and I would use Memo1.Lines.Add(Result.PhraseInfo.GetText(0,SP_GETWHOLEPHRASE,true));
    instead of
    Memo1.Lines.Add(Result.PhraseInfo.GetText(SP_GETWHOLEPHRASE,SP_GETWHOLEPHRASE,true));
    As the first param specifies the first element to retrieve.

    I would also set SpSharedRecoContext1.EventInterests := SREAllEvents;

    You might find some useful examples and info on my blog...

    PS: I had no problem with SAPI 5.4, but never tried it on D6....