{ Copyright (c)  2025  Xiaomi Corporation }
program kokoro_en;
{
This file shows how to use the text to speech API of sherpa-onnx
with Kokoro TTS models (Chinese + English).

It generates speech from text and saves it to a wave file.

If you want to play it while it is generating, please see
./kokoro-en-playback.pas
}

{$mode objfpc}

uses
  SysUtils,
  sherpa_onnx;

function GetOfflineTts: TSherpaOnnxOfflineTts;
var
  Config: TSherpaOnnxOfflineTtsConfig;
begin
  Config.Model.Kokoro.Model := './kokoro-multi-lang-v1_0/model.onnx';
  Config.Model.Kokoro.Voices := './kokoro-multi-lang-v1_0/voices.bin';
  Config.Model.Kokoro.Tokens := './kokoro-multi-lang-v1_0/tokens.txt';
  Config.Model.Kokoro.DataDir := './kokoro-multi-lang-v1_0/espeak-ng-data';
  Config.Model.Kokoro.DictDir := './kokoro-multi-lang-v1_0/dict';
  Config.Model.Kokoro.Lexicon := './kokoro-multi-lang-v1_0/lexicon-us-en.txt,./kokoro-multi-lang-v1_0/lexicon-zh.txt';
  Config.Model.NumThreads := 2;
  Config.Model.Debug := False;
  Config.MaxNumSentences := 1;

  Result := TSherpaOnnxOfflineTts.Create(Config);
end;

var
  Tts: TSherpaOnnxOfflineTts;
  Audio: TSherpaOnnxGeneratedAudio;

  Text: AnsiString;
  Speed: Single = 1.0;  {Use a larger value to speak faster}
  SpeakerId: Integer = 46;

begin
  Tts := GetOfflineTts;

  WriteLn('There are ', Tts.GetNumSpeakers, ' speakers');

  Text := '中英文语音合成测试。This is generated by next generation Kaldi using Kokoro without Misaki. 你觉得中英文说的如何呢？';

  Audio :=  Tts.Generate(Text, SpeakerId, Speed);
  SherpaOnnxWriteWave('./kokoro-zh-en-46.wav', Audio.Samples, Audio.SampleRate);
  WriteLn('Saved to ./kokoro-zh-en-46.wav');

  FreeAndNil(Tts);
end.

