refactor: stop buffering text and start streaming asap
This commit is contained in:
parent
9c2c31be96
commit
a6472f2075
1 changed files with 33 additions and 33 deletions
66
kokoro.ts
66
kokoro.ts
|
|
@ -3,14 +3,14 @@
|
|||
* 2. Modify onnxruntime-node/dist/binding.js for bun
|
||||
* 3. Profit */
|
||||
import os from "node:os";
|
||||
import { $ } from "bun";
|
||||
import { $, which } from "bun";
|
||||
import chalk from "chalk";
|
||||
import { KokoroTTS, TextSplitterStream } from "kokoro-js";
|
||||
|
||||
const model_id = "onnx-community/Kokoro-82M-v1.0-ONNX";
|
||||
const tts = await KokoroTTS.from_pretrained(model_id, {
|
||||
dtype: "fp32", // Options: "fp32", "fp16", "q8", "q4", "q4f16"
|
||||
device: "cpu", // Options: "wasm", "webgpu" (web) or "cpu" (node). If using "webgpu", we recommend using dtype="fp32".
|
||||
dtype: "fp32", // fp32 and q8 work best; fp16 results in silent segments
|
||||
device: "cpu",
|
||||
});
|
||||
|
||||
const stream = new TextSplitterStream();
|
||||
|
|
@ -21,46 +21,46 @@ if (!input) {
|
|||
}
|
||||
stream.push(input);
|
||||
|
||||
console.log(chalk.blue("Welcome to aitts!\n"));
|
||||
console.log(input);
|
||||
|
||||
await $`rm -r ${os.homedir()}/.aitts/`.nothrow();
|
||||
await $`mkdir ${os.homedir()}/.aitts/`.nothrow();
|
||||
|
||||
let fileId = 0;
|
||||
let buffer = "";
|
||||
for (const text of stream) {
|
||||
buffer += ` ${text}`;
|
||||
if (buffer.length < 100) continue;
|
||||
const files: string[] = [];
|
||||
|
||||
console.log(`${chalk.blue(fileId)}:${buffer}`);
|
||||
const audio = await tts.generate(buffer, {
|
||||
voice: "af_heart",
|
||||
const processText = async (text: string) => {
|
||||
const audio = await tts.generate(text, {
|
||||
voice: "af_bella",
|
||||
speed: 1.2,
|
||||
});
|
||||
|
||||
const file = `${os.homedir()}/.aitts/${fileId}.wav`;
|
||||
audio.save(file);
|
||||
|
||||
files.push(file);
|
||||
fileId++;
|
||||
buffer = "";
|
||||
}
|
||||
};
|
||||
|
||||
if (buffer) {
|
||||
console.log(`${chalk.blue(fileId)}:${buffer}`);
|
||||
const audio = await tts.generate(buffer, {
|
||||
voice: "af_heart",
|
||||
speed: 1.2,
|
||||
});
|
||||
|
||||
const file = `${os.homedir()}/.aitts/${fileId}.wav`;
|
||||
audio.save(file);
|
||||
}
|
||||
|
||||
console.log(chalk.yellow("Combine wav file with sox..."));
|
||||
await $`sox ${os.homedir()}/.aitts/* ${os.homedir()}/.aitts/all.wav`;
|
||||
console.log(chalk.green("Success: ~/.aitts/all.wav"));
|
||||
|
||||
console.log(chalk.yellow("Listening with mpv..."));
|
||||
await Bun.spawn({
|
||||
cmd: ["mpv", `${os.homedir()}/.aitts/all.wav`],
|
||||
const play = async () => {
|
||||
for (const file of files) {
|
||||
console.log(chalk.green(`Playing ${file}`));
|
||||
await Bun.spawn({
|
||||
cmd: ["mpv", file],
|
||||
stdout: "inherit",
|
||||
}).exited;
|
||||
console.log(chalk.green("Have a nice day!"));
|
||||
}).exited;
|
||||
process.stdout.moveCursor(-9999, -5);
|
||||
process.stdout.clearScreenDown();
|
||||
}
|
||||
};
|
||||
|
||||
let playing: Promise<void> | undefined;
|
||||
for (const text of stream) {
|
||||
await processText(text);
|
||||
|
||||
if (!playing && which("mpv")) {
|
||||
playing = play();
|
||||
}
|
||||
}
|
||||
|
||||
await playing;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue