Add local MeloTTS support
This commit is contained in:
60
src/setup-tts.ts
Normal file
60
src/setup-tts.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import process from "node:process";
|
||||
import { mkdir, rm } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { spawn } from "node:child_process";
|
||||
|
||||
import { loadConfig } from "./config.js";
|
||||
import { Logger } from "./logger.js";
|
||||
import { MeloTtsService } from "./services/melo-tts.js";
|
||||
|
||||
async function run(command: string, args: string[], cwd = process.cwd()): Promise<void> {
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const child = spawn(command, args, {
|
||||
cwd,
|
||||
stdio: "inherit",
|
||||
windowsHide: true,
|
||||
});
|
||||
|
||||
child.on("error", reject);
|
||||
child.on("exit", (code) => {
|
||||
if (code === 0) {
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
reject(new Error(`${command} ${args.join(" ")} exited with code ${code ?? "null"}`));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export async function setupTts(): Promise<void> {
|
||||
const config = loadConfig();
|
||||
const logger = new Logger(config.DEBUG ? config.LOG_LEVEL : "error");
|
||||
const dockerContext = path.resolve(process.cwd(), "docker", "melotts");
|
||||
const cacheDir = path.resolve(process.cwd(), config.TTS_CACHE_DIR);
|
||||
const outputDir = path.resolve(process.cwd(), config.TTS_OUTPUT_DIR);
|
||||
|
||||
await mkdir(cacheDir, { recursive: true });
|
||||
await mkdir(outputDir, { recursive: true });
|
||||
|
||||
console.log(`MeloTTS Docker 이미지 빌드: ${config.TTS_IMAGE}`);
|
||||
await run("docker", ["build", "-t", config.TTS_IMAGE, dockerContext]);
|
||||
|
||||
const tts = new MeloTtsService(config, logger);
|
||||
const warmupPath = path.join(outputDir, "warmup.wav");
|
||||
|
||||
console.log("MeloTTS 모델 워밍업...");
|
||||
try {
|
||||
await tts.synthesizeToFile("안녕하세요. 로컬 티티에스 준비 테스트입니다.", warmupPath);
|
||||
} finally {
|
||||
await rm(warmupPath, { force: true }).catch(() => undefined);
|
||||
}
|
||||
|
||||
console.log("로컬 TTS 환경 준비 완료");
|
||||
}
|
||||
|
||||
if (import.meta.main) {
|
||||
void setupTts().catch((error) => {
|
||||
console.error(error instanceof Error ? error.message : String(error));
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user