fix: TTS voices cleanup — filter placeholders, auto-model switch, entrypoint always writes config + v0.9.3
## Problems 1. 'some_other_voice_you_want' in voice list — placeholder from openedai-speech default config 2. KeyError: 'irina' — user selected tts-1-hd but irina is only in tts-1 (Piper) 3. Custom uploaded voices not picked up after restart ## Fixes ### entrypoint.sh — ALWAYS writes config Previously only wrote config if it didn't exist. Now ALWAYS writes (overwrites image defaults that have placeholders). Then scans /app/voices/ for custom .wav files and adds them as XTTS voices with language: ru. No manual config editing needed. ### /api/tts/voices — 3 improvements 1. Filters placeholder voices (some_other_voice_you_want, your_voice, etc.) 2. Returns model info per voice: tts-1 (Piper), tts-1-hd (XTTS), or both 3. Parses voice_to_speaker.yaml to know which section each voice belongs to ### TTSTab — auto model switch + voice labels - Voice dropdown shows '(Piper)' or '(XTTS)' or '(both)' after each voice - When user selects a Piper-only voice (irina), auto-switches model to tts-1 - When user selects an XTTS-only voice (custom upload), auto-switches to tts-1-hd ### /api/tts/upload-voice — simplified No longer modifies voice_to_speaker.yaml. Just saves the .wav file. The entrypoint scans /app/voices/ on startup and auto-adds .wav files as XTTS voices. This is simpler and more reliable. ## Version → 0.9.3
This commit is contained in:
@@ -120,7 +120,7 @@ export async function GET() {
|
||||
ok: envError === null,
|
||||
error: envError,
|
||||
},
|
||||
version: "0.9.2",
|
||||
version: "0.9.3",
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { promises as fs, existsSync } from "node:fs";
|
||||
import { promises as fs } from "node:fs";
|
||||
import path from "node:path";
|
||||
import { PROJECT_DIR } from "@/lib/docker";
|
||||
|
||||
@@ -7,21 +7,14 @@ export const dynamic = "force-dynamic";
|
||||
export const runtime = "nodejs";
|
||||
|
||||
const TTS_VOICES_DIR = path.join(PROJECT_DIR, "gpu", "tts", "voices");
|
||||
const TTS_CONFIG_DIR = path.join(PROJECT_DIR, "gpu", "tts", "config");
|
||||
const VOICE_CONFIG_PATH = path.join(TTS_CONFIG_DIR, "voice_to_speaker.yaml");
|
||||
|
||||
/**
|
||||
* POST /api/tts/upload-voice
|
||||
*
|
||||
* Uploads a custom voice sample (WAV file) for XTTSv2 voice cloning.
|
||||
* The file is saved to gpu/tts/voices/ AND the voice_to_speaker.yaml config
|
||||
* is updated so openedai-speech picks it up after restart.
|
||||
*
|
||||
* For XTTS voice samples:
|
||||
* - Mono (single channel) 22050 Hz WAV file
|
||||
* - 6-30 seconds long
|
||||
* - Low noise, no background sounds
|
||||
* - Even speaking pace
|
||||
* The file is saved to gpu/tts/voices/. The custom entrypoint.sh automatically
|
||||
* scans this directory on startup and adds .wav files as XTTS voices.
|
||||
* No config file modification needed — restart TTS to pick up new voices.
|
||||
*/
|
||||
export async function POST(req: NextRequest) {
|
||||
const contentType = req.headers.get("content-type") || "";
|
||||
@@ -36,7 +29,6 @@ export async function POST(req: NextRequest) {
|
||||
const form = await req.formData();
|
||||
const file = form.get("file");
|
||||
const name = form.get("name") as string | null;
|
||||
const language = (form.get("language") as string) || null;
|
||||
|
||||
if (!file || !(file instanceof File)) {
|
||||
return NextResponse.json(
|
||||
@@ -45,7 +37,6 @@ export async function POST(req: NextRequest) {
|
||||
);
|
||||
}
|
||||
|
||||
// Voice name — use provided name or derive from filename
|
||||
let voiceName: string;
|
||||
if (name) {
|
||||
voiceName = name.replace(/[^\w\-]/g, "_");
|
||||
@@ -53,28 +44,20 @@ export async function POST(req: NextRequest) {
|
||||
voiceName = path.parse(file.name).name.replace(/[^\w\-]/g, "_");
|
||||
}
|
||||
|
||||
// Ensure .wav extension for XTTS voices
|
||||
const filename = `${voiceName}.wav`;
|
||||
const dest = path.join(TTS_VOICES_DIR, filename);
|
||||
|
||||
// Create directories if they don't exist
|
||||
await fs.mkdir(TTS_VOICES_DIR, { recursive: true });
|
||||
await fs.mkdir(TTS_CONFIG_DIR, { recursive: true });
|
||||
|
||||
const buffer = Buffer.from(await file.arrayBuffer());
|
||||
await fs.writeFile(dest, buffer);
|
||||
|
||||
// Now update voice_to_speaker.yaml to include the new voice
|
||||
// openedai-speech reads this config at startup — without it, the voice
|
||||
// won't appear in the voices list even though the WAV file exists.
|
||||
await updateVoiceConfig(voiceName, filename, language);
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
voice: voiceName,
|
||||
path: filename,
|
||||
size: buffer.length,
|
||||
hint: "Voice uploaded and config updated. Restart TTS container to apply.",
|
||||
hint: "Voice uploaded. Restart TTS — the entrypoint will auto-detect and add it to the config.",
|
||||
});
|
||||
} catch (err) {
|
||||
return NextResponse.json(
|
||||
@@ -86,8 +69,7 @@ export async function POST(req: NextRequest) {
|
||||
|
||||
/**
|
||||
* GET /api/tts/upload-voice
|
||||
*
|
||||
* Lists voice files on disk (in gpu/tts/voices/).
|
||||
* Lists voice files on disk.
|
||||
*/
|
||||
export async function GET() {
|
||||
try {
|
||||
@@ -100,100 +82,3 @@ export async function GET() {
|
||||
return NextResponse.json({ ok: true, voices: [] });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update voice_to_speaker.yaml to add a new voice entry.
|
||||
* If the file doesn't exist, create it with the default built-in voices
|
||||
* plus the new custom voice.
|
||||
*/
|
||||
async function updateVoiceConfig(
|
||||
voiceName: string,
|
||||
filename: string,
|
||||
language: string | null
|
||||
): Promise<void> {
|
||||
let existing = "";
|
||||
try {
|
||||
existing = await fs.readFile(VOICE_CONFIG_PATH, "utf-8");
|
||||
} catch {
|
||||
// File doesn't exist — create from scratch with default voices
|
||||
existing = createDefaultVoiceConfig();
|
||||
}
|
||||
|
||||
// Check if voice already exists in config
|
||||
if (existing.includes(` ${voiceName}:`)) {
|
||||
// Voice already in config — update the speaker file path
|
||||
existing = existing.replace(
|
||||
new RegExp(
|
||||
` ${voiceName}:\\n(?: [^\\n]+\\n)*`,
|
||||
"m"
|
||||
),
|
||||
formatVoiceEntry(voiceName, filename, language)
|
||||
);
|
||||
} else {
|
||||
// Add new voice under tts-1-hd section
|
||||
// Find the tts-1-hd: section and append
|
||||
if (existing.includes("tts-1-hd:")) {
|
||||
existing = existing.replace(
|
||||
/(tts-1-hd:\n)/,
|
||||
`$1${formatVoiceEntry(voiceName, filename, language)}`
|
||||
);
|
||||
} else {
|
||||
// No tts-1-hd section — add it
|
||||
existing += `\ntts-1-hd:\n${formatVoiceEntry(voiceName, filename, language)}`;
|
||||
}
|
||||
}
|
||||
|
||||
await fs.writeFile(VOICE_CONFIG_PATH, existing, "utf-8");
|
||||
}
|
||||
|
||||
function formatVoiceEntry(
|
||||
name: string,
|
||||
filename: string,
|
||||
language: string | null
|
||||
): string {
|
||||
let entry = ` ${name}:\n model: xtts\n speaker: voices/${filename}\n`;
|
||||
if (language) {
|
||||
entry += ` language: ${language}\n`;
|
||||
}
|
||||
return entry;
|
||||
}
|
||||
|
||||
function createDefaultVoiceConfig(): string {
|
||||
return `# voice_to_speaker.yaml — openedai-speech voice configuration
|
||||
# Custom voices are added here. Restart TTS container after changes.
|
||||
|
||||
tts-1:
|
||||
alloy:
|
||||
model: voices/en_US-amy-medium.onnx
|
||||
echo:
|
||||
model: voices/en_US-libritts-high.onnx
|
||||
fable:
|
||||
model: voices/en_US-ryan-high.onnx
|
||||
onyx:
|
||||
model: voices/en_US-daniel-low.onnx
|
||||
nova:
|
||||
model: voices/en_US-lessac-medium.onnx
|
||||
shimmer:
|
||||
model: voices/en_US-nora-medium.onnx
|
||||
|
||||
tts-1-hd:
|
||||
alloy:
|
||||
model: xtts
|
||||
speaker: voices/alloy.wav
|
||||
echo:
|
||||
model: xtts
|
||||
speaker: voices/echo.wav
|
||||
fable:
|
||||
model: xtts
|
||||
speaker: voices/fable.wav
|
||||
onyx:
|
||||
model: xtts
|
||||
speaker: voices/onyx.wav
|
||||
nova:
|
||||
model: xtts
|
||||
speaker: voices/nova.wav
|
||||
shimmer:
|
||||
model: xtts
|
||||
speaker: voices/shimmer.wav
|
||||
`;
|
||||
}
|
||||
|
||||
@@ -10,18 +10,46 @@ const TTS_VOICES_DIR = path.join(PROJECT_DIR, "gpu", "tts", "voices");
|
||||
const TTS_CONFIG_DIR = path.join(PROJECT_DIR, "gpu", "tts", "config");
|
||||
const VOICE_CONFIG_PATH = path.join(TTS_CONFIG_DIR, "voice_to_speaker.yaml");
|
||||
|
||||
// Voices to filter out (placeholders from openedai-speech defaults)
|
||||
const PLACEHOLDER_VOICES = new Set([
|
||||
"some_other_voice_you_want",
|
||||
"your_voice",
|
||||
"custom",
|
||||
"example",
|
||||
"test",
|
||||
]);
|
||||
|
||||
interface VoiceInfo {
|
||||
voice_id: string;
|
||||
model: "tts-1" | "tts-1-hd" | "both";
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/tts/voices
|
||||
*
|
||||
* Returns available TTS voices. Tries two sources:
|
||||
* 1. openedai-speech /v1/audio/voices API (if TTS is running)
|
||||
* 2. Falls back to reading voice_to_speaker.yaml + scanning voices/ dir
|
||||
* (so voices show up even when TTS is stopped)
|
||||
* Returns available TTS voices with model info.
|
||||
* Sources: TTS API + voice_to_speaker.yaml + directory scan.
|
||||
*/
|
||||
export async function GET() {
|
||||
const voices: string[] = [];
|
||||
const voiceMap = new Map<string, VoiceInfo>();
|
||||
|
||||
// Source 1: Try the TTS API
|
||||
// Helper to add/update a voice
|
||||
const addVoice = (name: string, model: "tts-1" | "tts-1-hd" | "both") => {
|
||||
const lower = name.toLowerCase();
|
||||
if (PLACEHOLDER_VOICES.has(lower)) return;
|
||||
if (lower.includes("some_other") || lower.includes("your_voice")) return;
|
||||
|
||||
const existing = voiceMap.get(name);
|
||||
if (existing) {
|
||||
if (existing.model !== model) {
|
||||
existing.model = "both";
|
||||
}
|
||||
} else {
|
||||
voiceMap.set(name, { voice_id: name, model });
|
||||
}
|
||||
};
|
||||
|
||||
// Source 1: TTS API
|
||||
try {
|
||||
const res = await fetch("http://tts:8000/v1/audio/voices", {
|
||||
cache: "no-store",
|
||||
@@ -32,56 +60,64 @@ export async function GET() {
|
||||
if (data.voices && Array.isArray(data.voices)) {
|
||||
for (const v of data.voices) {
|
||||
const id = v.voice_id || v.name;
|
||||
if (id && !voices.includes(id)) voices.push(id);
|
||||
if (id) addVoice(id, "both"); // API doesn't tell us which model
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
/* TTS not running — fall through to file scan */
|
||||
/* TTS not running */
|
||||
}
|
||||
|
||||
// Source 2: Read voice_to_speaker.yaml (always available, even when TTS is off)
|
||||
// Source 2: Parse voice_to_speaker.yaml
|
||||
try {
|
||||
const yaml = await fs.readFile(VOICE_CONFIG_PATH, "utf-8");
|
||||
// Parse voice names — they're indented under "tts-1:" and "tts-1-hd:"
|
||||
// Format: " voicename:\n model: ..."
|
||||
const matches = yaml.matchAll(/^\s{2}(\w[\w\-]*):/gm);
|
||||
for (const m of matches) {
|
||||
const name = m[1];
|
||||
if (!voices.includes(name)) voices.push(name);
|
||||
const lines = yaml.split("\n");
|
||||
let currentSection: "tts-1" | "tts-1-hd" | null = null;
|
||||
for (const line of lines) {
|
||||
// Section header: "tts-1:" or "tts-1-hd:"
|
||||
const sectionMatch = line.match(/^(tts-1|tts-1-hd):\s*$/);
|
||||
if (sectionMatch) {
|
||||
currentSection = sectionMatch[1] as "tts-1" | "tts-1-hd";
|
||||
continue;
|
||||
}
|
||||
// Voice entry: " voicename:" (2 spaces indent)
|
||||
const voiceMatch = line.match(/^ ([a-zA-Z][\w\-]*):\s*$/);
|
||||
if (voiceMatch && currentSection) {
|
||||
addVoice(voiceMatch[1], currentSection);
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
/* config doesn't exist yet */
|
||||
/* config doesn't exist */
|
||||
}
|
||||
|
||||
// Source 3: Scan voices/ directory for .onnx files (Piper voices)
|
||||
// Source 3: Scan voices/ directory
|
||||
try {
|
||||
const entries = await fs.readdir(TTS_VOICES_DIR);
|
||||
for (const f of entries) {
|
||||
if (f.endsWith(".onnx")) {
|
||||
// e.g. ru_RU-irina-medium.onnx → irina
|
||||
const match = f.match(/^[a-z]{2}_([A-Z]{2})-([a-z]+)-/i);
|
||||
if (match) {
|
||||
const name = match[2];
|
||||
if (!voices.includes(name)) voices.push(name);
|
||||
}
|
||||
// Piper voice: ru_RU-irina-medium.onnx → irina
|
||||
const match = f.match(/^[a-z]{2}_[A-Z]{2}-([a-z]+)-/i);
|
||||
if (match) addVoice(match[1], "tts-1");
|
||||
}
|
||||
if (f.endsWith(".wav")) {
|
||||
const name = f.replace(/\.wav$/, "");
|
||||
if (!voices.includes(name)) voices.push(name);
|
||||
// Don't add built-in XTTS voices again (they're already in config)
|
||||
if (!["alloy", "echo", "fable", "onyx", "nova", "shimmer"].includes(name)) {
|
||||
addVoice(name, "tts-1-hd");
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
/* dir doesn't exist */
|
||||
}
|
||||
|
||||
// Always include defaults so the dropdown isn't empty
|
||||
// Always include defaults
|
||||
for (const d of ["alloy", "echo", "fable", "onyx", "nova", "shimmer"]) {
|
||||
if (!voices.includes(d)) voices.push(d);
|
||||
if (!voiceMap.has(d)) {
|
||||
addVoice(d, "both");
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
voices: voices.map((v) => ({ voice_id: v })),
|
||||
});
|
||||
const voices = Array.from(voiceMap.values());
|
||||
return NextResponse.json({ ok: true, voices });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user