-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathsynthesize.ts
More file actions
178 lines (164 loc) · 7.88 KB
/
Copy pathsynthesize.ts
File metadata and controls
178 lines (164 loc) · 7.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { defineCommand } from '../../command';
import { CLIError } from '../../errors/base';
import { ExitCode } from '../../errors/codes';
import { request, requestJson } from '../../client/http';
import { speechEndpoint } from '../../client/endpoints';
import { parseSSE } from '../../client/stream';
import { detectOutputFormat, formatOutput } from '../../output/formatter';
import { saveAudioOutput } from '../../output/audio';
import { writeFileSync } from 'fs';
import { readTextFromPathOrStdin } from '../../utils/fs';
import { T2A_FORMATS, formatList, validateAudioFormat, validateT2AStreaming } from '../../utils/audio-formats';
import type { Config } from '../../config/schema';
import type { GlobalFlags } from '../../types/flags';
import type { SpeechRequest, SpeechResponse } from '../../types/api';
export default defineCommand({
name: 'speech synthesize',
description: 'Synchronous TTS, up to 10k chars (speech-2.8-hd / 2.6 / 02)',
apiDocs: '/docs/api-reference/speech-t2a-http',
usage: 'mmx speech synthesize --text <text> [--out <path>] [flags]',
options: [
{ flag: '--model <model>', description: 'Model ID (default: speech-2.8-hd)' },
{ flag: '--text <text>', description: 'Text to synthesize' },
{ flag: '--text-file <path>', description: 'Read text from file (use - for stdin)' },
{ flag: '--voice <id>', description: 'Voice ID (default: English_expressive_narrator)' },
{ flag: '--speed <n>', description: 'Speech speed multiplier', type: 'number' },
{ flag: '--volume <n>', description: 'Volume level', type: 'number' },
{ flag: '--pitch <n>', description: 'Pitch adjustment', type: 'number' },
{ flag: '--format <fmt>', description: `Audio format: ${formatList(T2A_FORMATS)} (default: mp3)` },
{ flag: '--sample-rate <hz>', description: 'Sample rate (default: 32000)', type: 'number' },
{ flag: '--bitrate <bps>', description: 'Bitrate (default: 128000)', type: 'number' },
{ flag: '--channels <n>', description: 'Audio channels (default: 1)', type: 'number' },
{ flag: '--language <code>', description: 'Language boost' },
{ flag: '--subtitles', description: 'Include subtitle timing data' },
{ flag: '--pronunciation <from/to>', description: 'Custom pronunciation (repeatable)', type: 'array' },
{ flag: '--out <path>', description: 'Save audio to file (uses hex decoding)' },
{ flag: '--stream', description: 'Stream raw audio to stdout' },
],
examples: [
'mmx speech synthesize --text "Hello, world!"',
'mmx speech synthesize --text "Hello, world!" --out hello.mp3',
'mmx speech synthesize --text "Hello" --subtitles --out hello.mp3',
'echo "Breaking news." | mmx speech synthesize --text-file - --out news.mp3',
'mmx speech synthesize --text "Stream" --stream | mpv --no-terminal -',
],
async run(config: Config, flags: GlobalFlags) {
let text = (flags.text ?? (flags._positional as string[]|undefined)?.[0]) as string | undefined;
if (flags.textFile) {
text = readTextFromPathOrStdin(flags.textFile as string);
}
if (!text) {
throw new CLIError(
'--text or --text-file is required.',
ExitCode.USAGE,
'mmx speech synthesize --text "Hello" --out hello.mp3',
);
}
const model = (flags.model as string)
|| config.defaultSpeechModel
|| 'speech-2.8-hd';
const voice = (flags.voice as string) || 'English_expressive_narrator';
const ts = new Date().toISOString().slice(0, 19).replace(/[T:]/g, '-');
const ext = (flags.format as string) || 'mp3';
validateAudioFormat(ext, T2A_FORMATS);
validateT2AStreaming(ext, flags.stream === true);
const outPath = (flags.out as string | undefined) ?? `speech_${ts}.${ext}`;
const outFormat = 'hex';
const format = detectOutputFormat(config.output);
const body: SpeechRequest = {
model,
text,
voice_setting: {
voice_id: voice,
speed: (flags.speed as number) ?? undefined,
vol: (flags.volume as number) ?? undefined,
pitch: (flags.pitch as number) ?? undefined,
},
audio_setting: {
format: (flags.format as string) || 'mp3',
sample_rate: (flags.sampleRate as number) ?? 32000,
bitrate: (flags.bitrate as number) ?? 128000,
channel: (flags.channels as number) ?? 1,
},
output_format: outFormat,
stream: flags.stream === true,
};
if (flags.language) body.language_boost = flags.language as string;
if (flags.subtitles) body.subtitle_enable = true; // Correct API parameter name
if (flags.pronunciation) {
body.pronunciation_dict = (flags.pronunciation as string[]).map(p => {
const [from, to] = p.split('/');
return { tone: to || from!, text: from! };
});
}
if (config.dryRun) {
console.log(formatOutput({ request: body }, format));
return;
}
const url = speechEndpoint(config.baseUrl);
if (flags.stream) {
const res = await request(config, { url, method: 'POST', body, stream: true });
for await (const event of parseSSE(res)) {
if (!event.data || event.data === '[DONE]') break;
const parsed = JSON.parse(event.data);
const audioHex = parsed?.data?.audio;
if (audioHex) {
process.stdout.write(Buffer.from(audioHex, 'hex'));
}
}
return;
}
const response = await requestJson<SpeechResponse>(config, {
url,
method: 'POST',
body,
});
if (!config.quiet) process.stderr.write(`[Model: ${model}]\n`);
saveAudioOutput(response, outPath, format, config.quiet);
// Download and save subtitle file when --subtitles is requested
if (flags.subtitles && response.data.subtitle_file) {
try {
// Download the subtitle JSON file from the URL
const subtitleRes = await fetch(response.data.subtitle_file);
if (!subtitleRes.ok) {
throw new CLIError(`Failed to download subtitle file: ${subtitleRes.status}`, ExitCode.GENERAL);
}
// API returns a flat array, not { subtitles: [...] }
const subtitleArray = await subtitleRes.json() as Array<{ text: string; time_begin: number; time_end: number }>;
if (subtitleArray?.length) {
// Convert to SRT format (API returns time in milliseconds)
const subtitlePath = outPath.replace(/\.[^.]+$/, '') + '.srt';
const srtContent = subtitleArray
.map((s, i) => {
// API already returns milliseconds, use directly
const fmt = (ms: number) => {
const h = String(Math.floor(ms / 3600000)).padStart(2, '0');
const m = String(Math.floor((ms % 3600000) / 60000)).padStart(2, '0');
const sec = String(Math.floor((ms % 60000) / 1000)).padStart(2, '0');
const mil = String(Math.round(ms % 1000)).padStart(3, '0');
return `${h}:${m}:${sec},${mil}`;
};
return `${i + 1}\n${fmt(s.time_begin)} --> ${fmt(s.time_end)}\n${s.text}`;
})
.join('\n\n');
writeFileSync(subtitlePath, srtContent, 'utf-8');
if (!config.quiet) {
console.log(formatOutput({ subtitles: subtitlePath }, format));
} else {
console.log(subtitlePath);
}
}
} catch (err) {
// Non-fatal: log warning but don't fail the whole synthesis
if (!config.quiet) {
process.stderr.write(`Warning: failed to download subtitles: ${(err as Error).message}\n`);
}
}
} else if (flags.subtitles && !response.data.subtitle_file) {
// Warn if --subtitles was requested but API didn't return subtitle_file
if (!config.quiet) {
process.stderr.write(`Warning: subtitles requested but not returned by API\n`);
}
}
},
});