2012-03-15 72 views
0

我想用delphi做一些语音识别,发现这个简单的项目效果很好,但我不能使用它,因为我无法导入SAPI 5.1。
在“导入类型库”窗口中,我可以找到SAPI 5.4,但遗憾的是我无法使用它。现在我想知道是否有办法导入SAPI 5.1,以便我可以使用下面的示例并根据需要进行扩展。德尔福不能导入SAPI 5.1

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. 

任何帮助表示赞赏,

THX timo314

编辑: 我有德尔福6个人版
SAPI 5.1是不是在 “导入类型库” 窗口中列出
编译时我在第46行和第47行发现错误,说“不兼容的类型参数列表不同”

我注意到SpeechLib_TL示例中附带的B.pas不同于在“导入”目录中创建的导入。
更改SpSharedRecoContext1Recognition和SpSharedRecoContext1假设的参数列表后,应用程序将运行但不调用该过程。就好像程序没有注意到有人在说话。

+2

欢迎来到StackOverflow。 Delphi的哪个版本? “无法导入”是什么意思?你的意思是没有安装你的Windows版本,但你有SAPI 5.4?如果是这样,5.4与早期版本向后兼容;你有什么问题?你会收到错误消息吗?请编辑您的问题,并记住这样做时,我们只有您提供给我们的信息来尝试并帮助您解决问题。如果你不告诉我们,我们无法知道 - 我们无法看到你的屏幕或代码,或从这里读出你的想法。 :) – 2012-03-15 22:42:39

回答

1

很难告诉你提供了什么,但对我来说RecognitionTypeTOleEnum,我会用的 Memo1.Lines.Add(Result.PhraseInfo.GetText(0,SP_GETWHOLEPHRASE,true));
代替
Memo1.Lines.Add(Result.PhraseInfo.GetText(SP_GETWHOLEPHRASE,SP_GETWHOLEPHRASE,true));
作为第一个参数指定要检索的第一个元素。

我也要设置SpSharedRecoContext1.EventInterests := SREAllEvents;

您可能会发现在my blog一些有用的例子和信息...

PS:我曾与SAPI 5.4没有问题,但从来没有尝试过的D6 ....

+0

感谢您的回复,我会看看您的网站。关于这个WepPage [链接(] http://www.blong.com/Conferences/DCon2002/Speech/SAPI51/SAPI51.htm#GramNotify)在Delphi6和更早的版本中似乎有些不同。但是,您的解决方案和对上述网站添加(Result.PhraseInfo.GetText)的更改都不能解决我的问题。 – timo314 2012-03-16 00:00:27

+0

刚刚发现:
DELPHI 6用户重要信息 Delphi 6中的类型库导入有一个错误,请参阅文章“Delphi 6 - 导入的自动化事件错误”。该示例仍然可以在Delphi 6中通过libary类型创建的单元工作,但仅仅是因为没有使用该组件的事件。如果您想使用任何SPVoice事件,您需要阅读文章“Delphi 6 - 导入的自动化事件错误”。
看到[链接](http://delphicikk.atw.hu/listaz.php?id=2380&oldal=51)希望我会在那里找到解决方案,如果是的话,我会在这里发布。 – timo314 2012-03-16 00:25:08

+0

[在此网站上](http://delphi.cjcsoft.net/viewthread.php?tid = 47605)某人对这个问题表现出了一些骇人的解决方法,但是用delphi 5创建的speechLib_TLB取代了[来自这里]的示例项目(http://www.blong.com/Conferences/DCon2002/Speech/ SAPI51/SAPI51.htm#DSR)工作没有任何错误,所以我会在该示例中构建我的应用程序。感谢您的时间和帮助,timo314 – timo314 2012-03-16 01:06:50