-
Notifications
You must be signed in to change notification settings - Fork 0
/
openai-tts.js
38 lines (33 loc) · 1.28 KB
/
openai-tts.js
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
// openai-tts.js
export async function requestOpenAITTS(openaiApiKey, text, selectedVoice, hdAudio) {
let model = hdAudio ? 'tts-1-hd' : 'tts-1';
try {
const response = await fetch('https://api.openai.com/v1/audio/speech', {
method: 'POST',
headers: {
'Authorization': `Bearer ${openaiApiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: model,
input: text,
voice: selectedVoice
})
});
if (response.status === 429) {
console.error('You are making too many requests, or you are out of funds on your account.');
alert('You are making too many requests, or you are out of funds on your account.');
return null;
}
if (!response.ok) {
console.error('Error from OpenAI API:', response.statusText);
return null;
}
document.getElementById('processing').classList.add('processing-lightgreen');
const buffer = await response.arrayBuffer();
return new Blob([buffer], { type: 'audio/mpeg' });
} catch (error) {
console.error('Error sending text to OpenAI API:', error);
return null;
}
}