-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-audio.ts
More file actions
55 lines (46 loc) · 1.7 KB
/
generate-audio.ts
File metadata and controls
55 lines (46 loc) · 1.7 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
"use server"
import { elevenlabs } from '@ai-sdk/elevenlabs';
import { experimental_generateSpeech as generateSpeech} from "ai"
import { writeFile } from 'fs/promises';
import path from 'path';
import { mp3ToWav } from './mp3-16k_wav';
export async function generateAudio(cleanedStory: string){
// const rawText = await generateStory(genre)
// const cleanedStory = cleanText(rawText.story);
try {
const result = await generateSpeech({
model: elevenlabs.speech('eleven_flash_v2'),
text: cleanedStory,
voice: "cgSgspJ2msm6clMCkdW9",
outputFormat: "mp3",
providerOptions: {
elevenlabs: {
voiceSettings: {
speed: 1.0,
stability: 0.5,
similarityBoost: 0.75,
}
},
}
})
const buffer = Buffer.from(result.audio.base64, "base64");
const baseName = `story-${Date.now()}`;
const mp3Path = path.join(process.cwd(), "public/audios", `${baseName}.mp3`);
const wavPath = path.join(process.cwd(), "public/audios", `${baseName}_16k.wav`);
await writeFile(mp3Path, buffer);
await mp3ToWav(mp3Path, wavPath);
return {
audio: {
base64: result.audio.base64,
format: result.audio.format,
mediaType: result.audio.mediaType,
uint8Array: result.audio.uint8Array,
url: `/audios/${baseName}_16k.wav`,
},
metadata: result.providerMetadata
}
} catch (error) {
console.error('Error generating audio:', error);
throw error;
}
}