feat: enhance TTS CLI with sox combining and wait flag

This commit is contained in:
mitchell 2026-04-28 16:56:21 -04:00
parent 6b75836647
commit b3fb6fd66f
2 changed files with 66 additions and 11 deletions

65
tts.ts
View file

@ -1,20 +1,51 @@
import os from "node:os"; import os from "node:os";
import { parseArgs } from "node:util";
import { $, which } from "bun";
import chalk from "chalk"; import chalk from "chalk";
const { values: args } = parseArgs({
args: Bun.argv,
options: {
wait: { type: "boolean" },
w: { type: "boolean" },
},
strict: true,
allowPositionals: true,
});
const wait = args.wait ?? args.w;
const text = await Bun.stdin.text(); const text = await Bun.stdin.text();
if (!text) process.exit(1); if (!text) {
console.error("No input given.");
process.exit(1);
}
const home = os.homedir(); const home = os.homedir();
const cwd = `${home}/.ttsc/`;
$.cwd(cwd);
await $`rm *.wav`.nothrow();
console.log(chalk.blue("Welcome to TTSC!"));
console.log(
chalk.yellow(
"Beginning TTS processing... (This will take extra time the first time)",
),
);
const koko = Bun.spawn({ const koko = Bun.spawn({
cmd: ["uv", "run", "--", "python", "-u", "main.py"], cmd: ["uv", "run", "--", "python", "-u", "main.py"],
cwd: `${home}/.ttsc/`, cwd,
stdin: Buffer.from(text), stdin: Buffer.from(text),
stdout: "pipe", stdout: "pipe",
stderr: "inherit", stderr: "inherit",
}); });
if (!wait) {
koko.exited.then(() => console.log(chalk.green("\nProcessing finished.")));
}
const decoder = new TextDecoder(); const decoder = new TextDecoder();
for await (const line of koko.stdout) { for await (const line of koko.stdout) {
const decoded = decoder.decode(line).trim(); const decoded = decoder.decode(line).trim();
@ -28,16 +59,40 @@ for await (const line of koko.stdout) {
} }
for (const info of infos) { for (const info of infos) {
console.log(`\n${chalk.blue(info.file)}: ${info.text}\n`); const file = `${home}/.ttsc/${info.file}.wav`;
console.log(`\n${chalk.blue(file)}:\n${info.text}`);
if (!wait) {
await Bun.spawn({ await Bun.spawn({
cmd: ["mpv", `${info.file}.wav`], cmd: ["mpv", file],
cwd: `${home}/.ttsc/`, cwd,
stdin: "ignore", stdin: "ignore",
stdout: "inherit", stdout: "inherit",
stderr: "inherit", stderr: "inherit",
}).exited; }).exited;
} }
}
} }
await koko.exited; await koko.exited;
if (!wait) process.exit();
console.log(chalk.green("\nProcessing finished."));
console.log(chalk.yellow(`Combining audio with sox...`));
if (which("sox")) {
await $`sox *.wav output.wav`;
console.log(chalk.green(`Playing audio from ${cwd}output.wav`));
} else {
console.error("Sox not found. Unable to combine outputs.");
console.log(chalk.yellow(`Audio available in ${cwd}`));
process.exit();
}
await Bun.spawn({
cmd: ["mpv", "output.wav"],
cwd,
stdin: "ignore",
stdout: "inherit",
stderr: "inherit",
}).exited;

View file

@ -9,5 +9,5 @@ pipeline = KPipeline(lang_code="a", repo_id="hexgrad/Kokoro-82M")
generator = pipeline(text, voice="af_heart", speed=1.2) generator = pipeline(text, voice="af_heart", speed=1.2)
for i, (gs, ps, audio) in enumerate(generator): for i, (gs, ps, audio) in enumerate(generator):
print(json.dumps({"file": i, "text": gs}))
sf.write(f"{i}.wav", audio, 24000) sf.write(f"{i}.wav", audio, 24000)
print(json.dumps({"file": i, "text": gs}))