Jump to content
Sign in to follow this  
lucarnet

Problem execution function DLL VOSK in Delphi11.2

Recommended Posts

Hello,
I am integrating a "VOSK speech recognition" into a Delphi 11.2/Fmx Windows and Android home automation application.
Under w11, Execution of the library functions "libvosk.dll" is fine except for the execution time (for example, the "vosk_model_new" function).
=>In IDE Delphi DEBUG or RELEASE mode, the time is 2 minutes and 30 seconds, with countless threads events (see attached log file:
"Thread Start: Thread ID: 5792. Process pandroidacceslib.exe (4900) Thread Exit: Thread ID: 19852. Process pandroidacceslib.exe (4900)").
=>But in Commanline window, the time execution of the same programexe is OK(1 to 2 seconds) (execution tests in Python or C program, with the same dll, also take about 2 seconds).
Does anyone have any ideas, for a solution?
PS: Calling the library in cdecl or stdcall mode gives the same problem.
Thanks.
unit VoskModel;

interface

uses
SysUtils, Classes, vosk_api;

type
TVoskModel = class(TComponent)
private
{ Private declarations }
FModel: PVoskModel;
FModelPath: String;
protected
{ Protected declarations }
public
{ Public declarations }
//constructor Create(AOwner: TComponent); overload; override;
constructor Create(AOwner: TComponent; AModelPath: String); reintroduce;
destructor Destroy; override;
function FindWord(AWord: String): Integer;
property Model: PVoskModel read FModel;
published
{ Published declarations }
property ModelPath: String read FModelPath;
end;


implementation
//Conversion string en tbytes avec 0Terminal pour C
function string_converttbytesc(vstr:string):tbytes;
begin
setlength(result,0);
  if vstr<>'' then
  begin
  result := TEncoding.UTF8.GetBytes(vstr);
  //Ajout 0terminale en fin de array of byte
  setlength(result,length(result)+1);
  result[length(result)-1]:=ord(#0);
  end;
end;
constructor TVoskModel.Create(AOwner: TComponent; AModelPath: String);
var bytes:tbytes;
begin
  inherited Create(AOwner);
  FModelPath := AModelPath;
  bytes:=string_converttbytesc(FModelPath);
  FModel := vosk_model_new(PAnsiChar(bytes));//FModelPath));
end;
...
unit umaintestrecord3;

interface

uses
  {$IF CompilerVersion >= 33.0}
  // Delphi 10.3 Rio
  System.Permissions,
  {$ENDIF}
	...

type
  TForm1 = class(TForm)
		...  
	public
    { Déclarations publiques }
    FTVoskModel: TVoskModel;
    FTVoskRecognizer: TVoskRecognizer;
    //============================================================================
    //VOSK
    //============================================================================
    function for_voskmodel_create_parModeldir(VSampleRate:longint;var vmes:string):boolean;
		....
	end;

var
  Form1: TForm1;

implementation

{$R *.fmx}
uses
    Math
    ;
//==========================================================================================
//VOSK
//==========================================================================================

function TForm1.for_voskmodel_create_parModeldir(VSampleRate:longint;var vmes:string):boolean;
var vficlong:string;
begin
result:=false;vmes:='ModelDir:"'+FVoskModel_Dir+'":';
  try
//Impossibite de deployer un repertoire dans projet/deploiement: Sol:deployer un zip et dezipper
vficlong:=IncludeTrailingPathDelimiter(FParamGen_DirDataVosk)+FVoskModel_Dir;
    if not DirectoryExists(vficlong)
    then showmessage('Modele Vosk "'+FVoskModel_Dir+'" introuvable')
    else begin
    String_Concatener_SLINEBREAK(vmes,'fichier Vosk "'+vficlong+'":OK');
        if FTVoskModel<>nil then
        begin
        FTVoskModel.free;
        FTVoskModel:=nil;
        end;
        if FTVoskRecognizer<>nil then
        begin
        FTVoskRecognizer.free;
        FTVoskRecognizer:=nil;
        end;

    FTVoskModel:=TVoskModel.Create(Self,vficlong);
        if FTVoskModel.Model=nil
        then String_Concatener_SLINEBREAK(vmes,'FModel.Model non initialisée')
        else begin
        FTVoskRecognizer:=TVoskRecognizer.Create(Self,FTVoskModel,VSampleRate);//FHeader.SampleRate);
          if FTVoskRecognizer=nil
          then String_Concatener_SLINEBREAK(vmes,'FRecognizer=nil')
          else begin
          FTVoskRecognizer.setwords(Boolean_Boolean_convert_Int(FVoskModel_setwords));
          FTVoskRecognizer.setPartialwords(Boolean_Boolean_convert_Int(FVoskModel_setPartialwords));
          String_Concatener_SLINEBREAK(vmes,'for_voskmodel_create:OK(samplerate='+inttostr(VSampleRate)+')');
          result:=true;
          end;
        end
    end;
  except
      on E:exception do
      vmes:=vmes+'for_voskmodel_create:'+E.message
  end;
end;
unit vosk_apiandroid;
//==========================================================================================
//Equivalent fichier entete "vosk_api.h"   V1.0
//------------------------------------------------------------------------------------------
//Pour Windows 32 ou 64: on passe par les fonctions en direct soit en librairie statique
//------------------------------------------------------------------------------------------
//Pour Android: on Charge d'abord Library dans le code soit Librairie dynamique
//-Deployer arm64-v8a\libvosk.so dans repertoire library "library\lib\arm64-v8a"
//-Deployer le ou les modeles Vosk:
//  1)zipper le model (car deploiement de fichier uniquement):  (ex:vosk-model-small-fr-0.22.zip)
//    Attention:le zip contient le repertoire parent (ex:"vosk-model-small-fr-0.22")
//  2)Deployer le fichier.zip (ex:vosk-model-small-fr-0.22.zip) dans ".\assets\internal"
//  Coder Loadlibrary dans le formcreate, et Freelibrary dans le formdestroy
//==========================================================================================


{$MINENUMSIZE 4}

interface
uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  System.IOUtils

  ;

const
  {$IF Defined(WIN32)}
  LIB_VOSK = 'libvosk.dll';
  _PU = '';
  {$ELSEIF Defined(WIN64)}
  LIB_VOSK = 'libvosk.dll';
  _PU = '';
  {$ELSEIF Defined(ANDROID)}
  //LIB_VOSK = 'libvosk.so'; On code "Loadlibrary" dans le formcreate
  _PU = '';
  {$ELSE}
   // {$MESSAGE Error 'Unsupported platform'}
  {$ENDIF}
type
  PModel = Pointer;
  PPModel = ^PModel;
  PVoskModel = Pointer;
  PPVoskModel = ^PVoskModel;
  PVoskSpkModel = Pointer;
  PPVoskSpkModel = ^PVoskSpkModel;
  PVoskRecognizer = Pointer;
  PPVoskRecognizer = ^PVoskRecognizer;
  PVoskBatchModel = Pointer;
  PPVoskBatchModel = ^PVoskBatchModel;
  PVoskBatchRecognizer = Pointer;
  PPVoskBatchRecognizer = ^PVoskBatchRecognizer;

(** Loads model data from the file and returns the model object
 *
 * @param model_path: the path of the model on the filesystem
 * @returns model object or NULL if problem occured *)
{
function vosk_model_new(const model_path: PAnsiChar): PVoskModel; cdecl;
  external LIB_VOSK name _PU + 'vosk_model_new';
...

 

logevent_execmainvosk.txt

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×