48 lines
1.4 KiB
TypeScript
48 lines
1.4 KiB
TypeScript
import OpenAI from "openai";
|
|
import * as fs from "fs";
|
|
import * as path from "path";
|
|
import { StoryConfig } from "./config";
|
|
|
|
let openaiClient: OpenAI | null = null;
|
|
function getOpenAI(): OpenAI {
|
|
if (!openaiClient) {
|
|
openaiClient = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
|
|
}
|
|
return openaiClient;
|
|
}
|
|
|
|
export async function generateAudio(
|
|
storyConfig: StoryConfig,
|
|
storyName: string,
|
|
chunk: string,
|
|
index: number
|
|
): Promise<string> {
|
|
const speechFile = path.join("stories", storyName, "audio", `chunk_${index}.mp3`);
|
|
|
|
const mp3 = await getOpenAI().audio.speech.create({
|
|
model: "gpt-4o-mini-tts",
|
|
voice: storyConfig.config.tts_voice_id as unknown as string,
|
|
input: chunk,
|
|
instructions: storyConfig.config.tts_instructions,
|
|
});
|
|
|
|
const buffer = Buffer.from(await mp3.arrayBuffer());
|
|
await fs.promises.writeFile(speechFile, buffer);
|
|
|
|
return speechFile;
|
|
}
|
|
|
|
export async function generateSingleAudio(storyConfig: StoryConfig, text: string, outputFile: string): Promise<string> {
|
|
const mp3 = await getOpenAI().audio.speech.create({
|
|
model: "gpt-4o-mini-tts",
|
|
voice: storyConfig.config.tts_voice_id as unknown as string,
|
|
input: text,
|
|
instructions: storyConfig.config.tts_instructions,
|
|
});
|
|
|
|
const buffer = Buffer.from(await mp3.arrayBuffer());
|
|
await fs.promises.writeFile(outputFile, buffer);
|
|
|
|
return outputFile;
|
|
}
|