-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·239 lines (206 loc) · 6.99 KB
/
index.js
File metadata and controls
executable file
·239 lines (206 loc) · 6.99 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const OpenAI = require("openai").default;
function printUsage() {
console.log(`Usage: tts [options]
Generate speech from text using OpenAI's TTS API.
Options:
-t, --text <text> Text to speak (required unless -f is used)
-f, --file <path> Read text from a file instead
-o, --output <path> Output file (default: speech-<timestamp>.mp3)
-v, --voice <voice> Voice: alloy, ash, ballad, coral, echo, fable,
marin, nova, onyx, sage, shimmer, verse, cedar
(default: alloy)
-m, --model <model> Model: gpt-4o-mini-tts, tts-1, tts-1-hd
(default: gpt-4o-mini-tts)
--format <fmt> Output format: mp3, opus, aac, flac, wav, pcm
(default: mp3)
--speed <n> Speed 0.25-4.0 (default: 1.0)
--instructions <text> Voice style instructions (gpt-4o-mini-tts only)
--install-skill Install the Claude Code skill to ~/.claude/skills/
--uninstall-skill Remove the Claude Code skill
-h, --help Show this help message
Environment:
OPENAI_API_KEY Required. Your OpenAI API key.
OPENAI_BASE_URL Optional. Custom API base URL (for proxies).
TTS_VOICE Optional. Default voice (overridden by -v flag).
TTS_MODEL Optional. Default model (overridden by -m flag).
TTS_FORMAT Optional. Default format (overridden by --format flag).
TTS_SPEED Optional. Default speed (overridden by --speed flag).
Examples:
tts -t "Hello world"
tts -f article.txt -v nova -o reading.mp3
tts -t "Breaking news" --instructions "Speak like a news anchor" -o news.mp3
npx @fionoble/tts-cli --install-skill
`);
}
function installSkill() {
const os = require("os");
const skillDir = path.join(os.homedir(), ".claude", "skills", "tts");
const source = path.join(__dirname, "skill", "SKILL.md");
if (!fs.existsSync(source)) {
console.error("Error: SKILL.md not found in package. Try reinstalling.");
process.exit(1);
}
fs.mkdirSync(skillDir, { recursive: true });
fs.copyFileSync(source, path.join(skillDir, "SKILL.md"));
console.log(`Claude Code skill installed to ${skillDir}/SKILL.md`);
}
function uninstallSkill() {
const os = require("os");
const skillDir = path.join(os.homedir(), ".claude", "skills", "tts");
const skillFile = path.join(skillDir, "SKILL.md");
if (fs.existsSync(skillFile)) {
fs.unlinkSync(skillFile);
fs.rmdirSync(skillDir);
console.log("Claude Code skill removed.");
} else {
console.log("Skill not installed, nothing to remove.");
}
}
const VALID_VOICES = [
"alloy", "ash", "ballad", "coral", "echo", "fable",
"marin", "nova", "onyx", "sage", "shimmer", "verse", "cedar",
];
const VALID_MODELS = ["gpt-4o-mini-tts", "tts-1", "tts-1-hd"];
const VALID_FORMATS = ["mp3", "opus", "aac", "flac", "wav", "pcm"];
function parseArgs(argv) {
const args = {
text: null,
file: null,
output: null,
voice: process.env.TTS_VOICE || "alloy",
model: process.env.TTS_MODEL || "gpt-4o-mini-tts",
format: process.env.TTS_FORMAT || "mp3",
speed: process.env.TTS_SPEED ? parseFloat(process.env.TTS_SPEED) : 1.0,
instructions: null,
};
for (let i = 2; i < argv.length; i++) {
const arg = argv[i];
switch (arg) {
case "-t":
case "--text":
args.text = argv[++i];
break;
case "-f":
case "--file":
args.file = argv[++i];
break;
case "-o":
case "--output":
args.output = argv[++i];
break;
case "-v":
case "--voice":
args.voice = argv[++i];
break;
case "-m":
case "--model":
args.model = argv[++i];
break;
case "--format":
args.format = argv[++i];
break;
case "--speed":
args.speed = parseFloat(argv[++i]);
break;
case "--instructions":
args.instructions = argv[++i];
break;
case "--install-skill":
installSkill();
process.exit(0);
case "--uninstall-skill":
uninstallSkill();
process.exit(0);
case "-h":
case "--help":
printUsage();
process.exit(0);
default:
console.error(`Unknown option: ${arg}`);
process.exit(1);
}
}
return args;
}
function resolveOutputPath(output, format) {
if (output) {
return path.resolve(output);
}
const timestamp = Date.now();
const ext = format === "pcm" ? "pcm" : format;
return path.resolve(`speech-${timestamp}.${ext}`);
}
async function main() {
const args = parseArgs(process.argv);
// Read text from file if -f was used
if (args.file) {
const filePath = path.resolve(args.file);
if (!fs.existsSync(filePath)) {
console.error(`File not found: ${filePath}`);
process.exit(1);
}
args.text = fs.readFileSync(filePath, "utf-8").trim();
}
if (!args.text) {
console.error("Error: --text or --file is required");
printUsage();
process.exit(1);
}
if (!VALID_VOICES.includes(args.voice)) {
console.error(`Error: Invalid voice "${args.voice}". Valid voices: ${VALID_VOICES.join(", ")}`);
process.exit(1);
}
if (!VALID_MODELS.includes(args.model)) {
console.error(`Error: Invalid model "${args.model}". Valid models: ${VALID_MODELS.join(", ")}`);
process.exit(1);
}
if (!VALID_FORMATS.includes(args.format)) {
console.error(`Error: Invalid format "${args.format}". Valid formats: ${VALID_FORMATS.join(", ")}`);
process.exit(1);
}
if (args.speed < 0.25 || args.speed > 4.0) {
console.error("Error: Speed must be between 0.25 and 4.0");
process.exit(1);
}
if (args.instructions && args.model !== "gpt-4o-mini-tts") {
console.error("Error: --instructions is only supported with the gpt-4o-mini-tts model");
process.exit(1);
}
if (!process.env.OPENAI_API_KEY) {
console.error("Error: OPENAI_API_KEY environment variable is not set");
process.exit(1);
}
const clientOptions = {};
if (process.env.OPENAI_BASE_URL) {
clientOptions.baseURL = process.env.OPENAI_BASE_URL;
}
const client = new OpenAI(clientOptions);
const outputPath = resolveOutputPath(args.output, args.format);
console.log(`Generating speech with voice "${args.voice}"...`);
try {
const params = {
model: args.model,
voice: args.voice,
input: args.text,
response_format: args.format,
speed: args.speed,
};
if (args.instructions) {
params.instructions = args.instructions;
}
const response = await client.audio.speech.create(params);
const buffer = Buffer.from(await response.arrayBuffer());
fs.writeFileSync(outputPath, buffer);
console.log(`Saved: ${outputPath}`);
} catch (err) {
console.error(`Error: ${err.message}`);
if (err.status) {
console.error(`Status: ${err.status}`);
}
process.exit(1);
}
}
main();