⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adsapien.pas

📁 测试用例
💻 PAS
📖 第 1 页 / 共 5 页
字号:
    ApdSRERR_RULEALREADYACTIVE          :
      raise EApdSapiEngineException.Create (ErrorCode,
                                            ApdStrSRERR_RULEALREADYACTIVE);
    ApdSRERR_RULENOTACTIVE              :
      raise EApdSapiEngineException.Create (ErrorCode,
                                            ApdStrSRERR_RULENOTACTIVE);
    ApdSRERR_NOUSERSELECTED             :
      raise EApdSapiEngineException.Create (ErrorCode,
                                            ApdStrSRERR_NOUSERSELECTED);
    ApdSRERR_BAD_PRONUNCIATION          :
      raise EApdSapiEngineException.Create (ErrorCode,
                                            ApdStrSRERR_BAD_PRONUNCIATION);
    ApdSRERR_DATAFILEERROR              :
      raise EApdSapiEngineException.Create (ErrorCode,
                                            ApdStrSRERR_DATAFILEERROR);
    ApdSRERR_GRAMMARALREADYACTIVE       :
      raise EApdSapiEngineException.Create (ErrorCode,
                                            ApdStrSRERR_GRAMMARALREADYACTIVE);
    ApdSRERR_GRAMMARNOTACTIVE           :
      raise EApdSapiEngineException.Create (ErrorCode,
                                            ApdStrSRERR_GRAMMARNOTACTIVE);
    ApdSRERR_GLOBALGRAMMARALREADYACTIVE :
      raise EApdSapiEngineException.Create (ErrorCode,
                                       ApdStrSRERR_GLOBALGRAMMARALREADYACTIVE);
    ApdSRERR_LANGUAGEMISMATCH           :
      raise EApdSapiEngineException.Create (ErrorCode,
                                            ApdStrSRERR_LANGUAGEMISMATCH);
    ApdSRERR_MULTIPLELANG               :
      raise EApdSapiEngineException.Create (ErrorCode,
                                            ApdStrSRERR_MULTIPLELANG);
    ApdSRERR_LDGRAMMARNOWORDS           :
      raise EApdSapiEngineException.Create (ErrorCode,
                                            ApdStrSRERR_LDGRAMMARNOWORDS);
    ApdSRERR_NOLEXICON                  :
      raise EApdSapiEngineException.Create (ErrorCode, ApdStrSRERR_NOLEXICON);
    ApdSRERR_SPEAKEREXISTS              :
      raise EApdSapiEngineException.Create (ErrorCode,
                                            ApdStrSRERR_SPEAKEREXISTS);
    ApdSRERR_GRAMMARENGINEMISMATCH      :
      raise EApdSapiEngineException.Create (ErrorCode,
                                            ApdStrSRERR_GRAMMARENGINEMISMATCH);
    ApdSRERR_BOOKMARKEXISTS             :
      raise EApdSapiEngineException.Create (ErrorCode,
                                            ApdStrSRERR_BOOKMARKEXISTS);
    ApdSRERR_BOOKMARKDOESNOTEXIST       :
      raise EApdSapiEngineException.Create (ErrorCode,
                                            ApdStrSRERR_BOOKMARKDOESNOTEXIST);
    ApdSRERR_MICWIZARDCANCELED          :
      raise EApdSapiEngineException.Create (ErrorCode,
                                            ApdStrSRERR_MICWIZARDCANCELED);
    ApdSRERR_WORDTOOLONG                :
      raise EApdSapiEngineException.Create (ErrorCode,
                                            ApdStrSRERR_WORDTOOLONG);
    ApdSRERR_BAD_WORD                   :
      raise EApdSapiEngineException.Create (ErrorCode, ApdStrSRERR_BAD_WORD);
    ApdE_WRONGTYPE                      :
      raise EApdSapiEngineException.Create (ErrorCode, ApdStrE_WRONGTYPE);
    ApdE_BUFFERTOOSMALL                 :
      raise EApdSapiEngineException.Create (ErrorCode, ApdStrE_BUFFERTOOSMALL);
    else
      raise EApdSapiEngineException.Create (ErrorCode, ApdStrE_UNKNOWN);
  end;
end;

procedure TApdCustomSapiEngine.DeRegisterPhraseFinishHook (
                                 PhraseFinishMethod : TApdPhraseFinishMethod);
var
  Idx : Integer;
  Event : PApdRegisteredEvent;

begin
  for Idx := FPhraseFinishClients.Count - 1 downto 0 do begin
    Event := FPhraseFinishClients[Idx];
    if (@Event.CallBack = @PhraseFinishMethod) then
      FPhraseFinishClients.Delete (Idx);
  end;
end;

function TApdCustomSapiEngine.GetSRAmplitude : Word;
begin
  result := FSRAmplitude;
end;

function TApdCustomSapiEngine.GetSRAutoGain : Integer;
begin
  if Assigned (FiDirectSR) then
    FSRAutoGain := FiDirectSR.AutoGain;
  Result := FSRAutoGain;
end;

procedure TApdCustomSapiEngine.InitializeSapi;                         {!!.01}
{ InitializeSapi is called by Loaded to initialize the SAPI engines }  {!!.01}
{ It should not be called directly UNLESS the TApdCustomSapiEngine  }  {!!.01}
{ is being created dynamically.  In this case, InitializeSapi       }  {!!.01}
{ be called after the creation to initialize the SAPI engine.       }  {!!.01}
begin                                                                  {!!.01}
  if InitSS then                                                       {!!.04}
    InitializeSS;                                                      {!!.04}
  if InitSR then                                                       {!!.04}
    InitializeSR;                                                      {!!.04}
end;                                                                   {!!.01}

procedure TApdCustomSapiEngine.InitializeSpeaking (var CSet : Integer;
                                                   var Options : Integer);
begin
  Speaking := True;
  case CharSet of
    csText           : CSet := ApdCHARSET_TEXT;
    csIPAPhonetic    : CSet := ApdCHARSET_IPAPHONETIC;
    csEnginePhonetic : CSet := ApdCHARSET_ENGINEPHONETIC;
  else
    CSet := ApdCHARSET_TEXT;
  end;
  Options := 0;
  if toTagged in TTSOptions then
    Options := Options or ApdTTSDATAFLAG_TAGGED;
end;

procedure TApdCustomSapiEngine.InitializeSR;                             {!!.04}
begin                                                                    {!!.04}
  if (not ((csDesigning in ComponentState) or                            {!!.04}
           (csLoading in ComponentState))) and                           {!!.04}
      (FiDirectSR = nil) then begin                                      {!!.04}

    { Create the SAPI Interaces }                                        {!!.04}

    try                                                                  {!!.04}
      FiDirectSR := TDirectSR.Create (Self);                             {!!.04}
      FiDirectSR.Visible := False;                                       {!!.04}
      FSREngines.FiDirectSR := FiDirectSR;                               {!!.04}
      FiDirectSR.Initialized := 1;                                       {!!.04}
    except                                                               {!!.04}
      on EOleError do                                                    {!!.04}
        TriggerSRError (Self, 0, '', ApdStrE_CANNOTCREATESR);            {!!.04}
    end;                                                                 {!!.04}

    { Set the speaker.  Since there doesn't seem to be a way to determine
      what the users are, use a default user.  Sapi will automatically
      use this user name. }

    try                                                                  {!!.04}
      FiDirectSR.Speaker := scApdDefaultUser;                            {!!.04}
    except                                                               {!!.04}
      on EOleError do                                                    {!!.04}
        TriggerSRError (Self, 0, '', ApdStrE_CANNOTSETSPEAKER);          {!!.04}
    end;                                                                 {!!.04}

    try                                                                  {!!.04}
      FiDirectSR.Microphone := scApdDefaultMic;                          {!!.04}
    except                                                               {!!.04}
      on EOleError do                                                    {!!.04}
        TriggerSRError (Self, 0, '', ApdStrE_CANNOTSETMIC);              {!!.04}
    end;                                                                 {!!.04}

    { Connect events }                                                   {!!.04}

    FiDirectSR.OnPhraseFinish := TriggerPhraseFinish;                    {!!.04}
    FiDirectSR.OnVUMeter := TriggerVUMeter;                              {!!.04}
    FiDirectSR.OnUtteranceBegin := TriggerSpeakStart;                    {!!.04}
    FiDirectSR.OnUtteranceEnd := TriggerSpeakStop;                       {!!.04}
    FiDirectSR.OnInterference := TriggerInterference;                    {!!.04}
    FiDirectSR.OnPhraseHypothesis := TriggerPhraseHypothesis;            {!!.04}
    FiDirectSR.OnTraining := TriggerTrainingRequested;                   {!!.04}
    FiDirectSR.OnWarning := TriggerSRWarning;                            {!!.04}
    FiDirectSR.OnError := TriggerSRError;                                {!!.04}
  end;                                                                   {!!.04}
end;                                                                     {!!.04}

procedure TApdCustomSapiEngine.InitializeSS;                             {!!.04}
begin                                                                    {!!.04}
  if (not ((csDesigning in ComponentState) or                            {!!.04}
           (csLoading in ComponentState))) and                           {!!.04}
      (FiDirectSS = nil) then begin                                      {!!.04}

    { Create the SAPI Interaces }                                        {!!.04}

    try                                                                  {!!.04}
      FiDirectSS := TDirectSS.Create (Self);                             {!!.04}
      FiDirectSS.Visible := False;                                       {!!.04}
      FSSVoices.FiDirectSS := FiDirectSS;                                {!!.04}
      FiDirectSS.Initialized := 1;                                       {!!.04}
    except                                                               {!!.04}
      on EOleError do                                                    {!!.04}
        TriggerSSError (Self, 0, '', ApdStrE_CANNOTCREATESS);            {!!.04}
    end;                                                                 {!!.04}

    { Connect events }                                                   {!!.04}

    FiDirectSS.OnAudioStart := TriggerAudioStart;                        {!!.04}
    FiDirectSS.OnAudioStop := TriggerAudioStop;                          {!!.04}
    FiDirectSS.OnError := TriggerSSError;                                {!!.04}
    FiDirectSS.Onwarning := TriggerSSWarning;                            {!!.04}
    FiDirectSS.OnAttribChanged := TriggerSSAttribChanged;                {!!.04}
  end;                                                                   {!!.04}
end;                                                                     {!!.04}

function TApdCustomSapiEngine.IsSapi4Installed : Boolean;
var
  SysDir : PChar;
begin
  Result := True;
  SysDir := StrAlloc (Max_Path);
  try
    if GetWindowsDirectory (SysDir, Max_Path) <> 0 then begin
      if not FileExists (SysDir + '\Speech\Speech.DLL') then           {!!.01}
        Result := False;
    end;
  finally
    StrDispose (SysDir);
  end;        
end;

procedure TApdCustomSapiEngine.Listen;
var
  GrammarString : string;
  i : Integer;
begin
  if SREngines.Count = 0 then
    raise EApdSapiEngineException.CreateFmt (ecNoSREngines, [0]);
  if Dictation then begin
    GrammarString := '[Grammar]'^M^J'type=dictation'^M^J;
    if WordList.Count > 0 then begin
      GrammarString := GrammarString + '[WordGroups]'^M^J +
                       '=ApdWords'^M^J + '[ApdWords]'^M^J;
      for i := 0 to WordList.Count - 1 do
        GrammarString := GrammarString + '=' + WordList[i] + ^M^J;
    end;

  end else begin
    GrammarString := '[Grammar]'^M^J'type=cfg'^M^J;
    if WordList.Count > 0 then begin
      GrammarString := GrammarString + '[<Start>]'^M^J;
      for i := 0 to WordList.Count - 1 do
        GrammarString := GrammarString + '<Start>=' + WordList[i] + ^M^J;
    end;
  end;


  FiDirectSR.GrammarFromString (GrammarString);

  case Duplex of
    sdFull :
      Listening := True;
    sdHalf :
      if Speaking then begin
        Speaking := False;
        Listening := True;
      end;
    sdHalfDelayed :
      if Speaking then
        FWaitMode := wmWaitSpeaking;
  end;
end;

procedure TApdCustomSapiEngine.Loaded;
begin
  inherited Loaded;

  if IsSapi4Installed then                                             {!!.04}
    InitializeSapi                                                     {!!.01}
  else begin                                                           {!!.04}
    if Assigned (FOnSRError) then                                      {!!.04}
      FOnSRError (Self, 0, '', ApdStrE_NOSAPI4);                       {!!.04}
    if Assigned (FOnSSError) then                                      {!!.04}
      FOnSSError (Self, 0, '', ApdStrE_NOSAPI4);                       {!!.04}
  end;                                                                 {!!.04}
end;

procedure TApdCustomSapiEngine.PauseListening;
begin
  FiDirectSR.Pause;
end;

procedure TApdCustomSapiEngine.PauseSpeaking;
begin
  FiDirectSS.AudioPause;
end;

procedure TApdCustomSapiEngine.RegisterPhraseFinishHook (
                                 PhraseFinishMethod : TApdPhraseFinishMethod);

var
  Event : PApdRegisteredEvent;
  Idx : Integer;
  AlreadyRegistered : Boolean;

begin
  AlreadyRegistered := False;
  for Idx := 0 to FPhraseFinishClients.Count - 1 do begin
    Event := FPhraseFinishClients[Idx];
    if (@Event.CallBack = @PhraseFinishMethod) then
      AlreadyRegistered := True;
  end;
  if not AlreadyRegistered then begin
    New (Event);
    Event.CallBack := PhraseFinishMethod;
    Event.EventType := etPhraseFinish;
    Event.Active := True;
    FPhraseFinishClients.Add (Event); 
  end;    
end;

procedure TApdCustomSapiEngine.ResumeListening;
begin
  FiDirectSR.Resume;
end;

procedure TApdCustomSapiEngine.ResumeSpeaking;
begin
  FiDirectSS.AudioResume;
end;

procedure TApdCustomSapiEngine.SetAutoTrain (v : Boolean);
begin
  if FAutoTrain <> v then
    FAutoTrain := v;
end;

procedure TApdCustomSapiEngine.SetCharSet (v : TApdCharacterSet);
begin
  if FCharSet <> v then
    FCharSet := v;
end;

procedure TApdCustomSapiEngine.SetDictation (v : Boolean);
begin
  if v <> FDictation then
    FDictation := v;
e

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -