unit uSilentPlayer;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes,
  System.Variants, utts, mmsystem,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, bass_simple,
  FMX.Controls.Presentation, FMX.StdCtrls;

type
  TfPublicPlayer = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    procedure endPlay(Sender: TObject);
  public
    { Public declarations }
  end;

var
  fPublicPlayer: TfPublicPlayer;
  b1: tbassSimple;
  i, sec: Integer;
  s: string;

implementation

{$R *.fmx}

procedure TfPublicPlayer.endPlay(Sender: TObject);
begin
  try
    TThread.Queue(nil,
      procedure
      begin
        Close;
      end);
  except

  end;
end;

procedure TfPublicPlayer.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  try
    if Assigned(b1) then
      b1.Free;
  except

  end;
end;

procedure TfPublicPlayer.FormCreate(Sender: TObject); // mode(1) sec vol othet
var
  mode: Integer; // mode(2) model texts
  tts: ttts;
  p, v, m: string;
begin
  hide;
  try
    mode := StrToIntDef(ParamStr(1), 1);
    case mode of
      1:
        begin
          b1 := tbassSimple.Create(0);
          b1.Volume := StrToIntDef(ParamStr(2), 255);
          sec := StrToIntDef(ParamStr(1), 1);
          s := '';
          for i := 3 to ParamCount do
          begin
            s := s + ParamStr(i) + ' ';
          end;
          Delete(s, length(s), 1);
          b1.Play(s);
          b1.OnEndPlay := endPlay;
        end;
      2:
        begin
          s := '';
          m := ParamStr(2);
          for i := 3 to ParamCount do
          begin
            s := s + ParamStr(i) + ' ';
          end;
          Delete(s, length(s), 1);
          p := ExtractFilePath(ParamStr(0)) + 'piper\piper.exe';
          v := ExtractFilePath(ParamStr(0)) + 'piper\voices';
          tts := ttts.Create(p, v);
          tts.OnPlayFinished := endPlay;
          tts.SetModel(m);
          tts.TextToSpeech(s, true);
        end;
    end;
  except

  end;

end;

end.
