migrate to root dir
This commit is contained in:
47
lib/pipeline/tts.ts
Normal file
47
lib/pipeline/tts.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
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;
|
||||
}
|
Reference in New Issue
Block a user