// Copyright (c)  2025  Xiaomi Corporation (authors: Fangjun Kuang)

const sherpa_onnx = require('sherpa-onnx');

function createOfflineTts() {
  let offlineTtsKokoroModelConfig = {
    model: './kokoro-multi-lang-v1_0/model.onnx',
    voices: './kokoro-multi-lang-v1_0/voices.bin',
    tokens: './kokoro-multi-lang-v1_0/tokens.txt',
    dataDir: './kokoro-multi-lang-v1_0/espeak-ng-data',
    dictDir: './kokoro-multi-lang-v1_0/dict',
    lexicon:
        './kokoro-multi-lang-v1_0/lexicon-us-en.txt,./kokoro-multi-lang-v1_0/lexicon-zh.txt',
    lengthScale: 1.0,
  };
  let offlineTtsModelConfig = {
    offlineTtsKokoroModelConfig: offlineTtsKokoroModelConfig,
    numThreads: 1,
    debug: 1,
    provider: 'cpu',
  };

  let offlineTtsConfig = {
    offlineTtsModelConfig: offlineTtsModelConfig,
    maxNumSentences: 1,
  };

  return sherpa_onnx.createOfflineTts(offlineTtsConfig);
}

const tts = createOfflineTts();
const speakerId = 49;
const speed = 1.0;
const text =
    '中英文语音合成测试。This is generated by next generation Kaldi using Kokoro without Misaki. 你觉得中英文说的如何呢？'

const audio = tts.generate({text: text, sid: speakerId, speed: speed});
tts.save('./test-kokoro-zh-en-49.wav', audio);
console.log('Saved to test-kokoro-zh-en-49.wav successfully.');
tts.free();
