-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
openai-audio.html
365 lines (327 loc) · 12.7 KB
/
openai-audio.html
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>OpenAI Audio</title>
<style>
* {
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
.container {
text-align: center;
background-color: white;
padding: 2rem;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
max-width: 800px;
width: 100%;
}
button {
font-size: 1rem;
padding: 0.5rem 1rem;
margin: 0.5rem;
cursor: pointer;
}
#timer {
font-size: 1.5rem;
margin: 1rem 0;
}
#audioPlayback, #prompt {
margin-top: 1rem;
width: 100%;
}
#prompt {
height: 100px;
resize: vertical;
font-size: 16px;
}
#apiResponse {
margin-top: 1rem;
text-align: left;
overflow-x: auto;
}
#formattedResponse {
background-color: #f8f8f8;
padding: 1rem;
border-radius: 4px;
margin-bottom: 1rem;
}
#jsonResponse {
background-color: #f8f8f8;
padding: 1rem;
border-radius: 4px;
overflow-x: auto;
}
pre {
white-space: pre-wrap;
}
#downloadButton {
display: none;
}
</style>
</head>
<body>
<div class="container">
<h1>OpenAI Audio</h1>
<button id="recordButton">Start Recording</button>
<div id="timer">00:00</div>
<audio id="audioPlayback" controls></audio>
<button id="downloadButton">Download Audio</button>
<textarea id="prompt" placeholder="Enter your prompt here"></textarea>
<button id="submitButton">Submit to API</button>
<div id="apiResponse">
<div id="formattedResponse"></div>
<pre id="jsonResponse"></pre>
</div>
</div>
<script type="module">
import { marked } from "https://esm.run/marked";
let audioContext;
let recorder;
let audioChunks = [];
let startTime;
let timerInterval;
let audioBlob;
let isRecording = false;
const recordButton = document.getElementById('recordButton');
const timer = document.getElementById('timer');
const audioPlayback = document.getElementById('audioPlayback');
const promptTextarea = document.getElementById('prompt');
const submitButton = document.getElementById('submitButton');
const formattedResponse = document.getElementById('formattedResponse');
const jsonResponse = document.getElementById('jsonResponse');
const downloadButton = document.getElementById('downloadButton');
recordButton.addEventListener('click', toggleRecording);
submitButton.addEventListener('click', submitToAPI);
downloadButton.addEventListener('click', downloadAudio);
async function toggleRecording() {
if (!isRecording) {
await startRecording();
} else {
stopRecording();
}
}
async function startRecording() {
try {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
audioContext = new (window.AudioContext || window.webkitAudioContext)();
const source = audioContext.createMediaStreamSource(stream);
const processor = audioContext.createScriptProcessor(1024, 1, 1);
source.connect(processor);
processor.connect(audioContext.destination);
audioChunks = [];
processor.onaudioprocess = (e) => {
const inputData = e.inputBuffer.getChannelData(0);
audioChunks.push(new Float32Array(inputData));
};
isRecording = true;
startTime = Date.now();
updateTimer();
timerInterval = setInterval(updateTimer, 1000);
recordButton.textContent = 'Stop Recording';
downloadButton.style.display = 'none';
} catch (error) {
console.error('Error starting recording:', error);
alert('Error starting recording. Please make sure you have given permission to use the microphone.');
}
}
function stopRecording() {
if (audioContext) {
audioContext.close();
audioContext = null;
}
clearInterval(timerInterval);
recordButton.textContent = 'Start Recording';
isRecording = false;
// Convert to WAV
const wavBlob = createWavBlob(audioChunks);
audioBlob = wavBlob;
const audioUrl = URL.createObjectURL(audioBlob);
audioPlayback.src = audioUrl;
downloadButton.style.display = 'inline-block';
}
function updateTimer() {
const elapsed = Math.floor((Date.now() - startTime) / 1000);
const minutes = Math.floor(elapsed / 60).toString().padStart(2, '0');
const seconds = (elapsed % 60).toString().padStart(2, '0');
timer.textContent = `${minutes}:${seconds}`;
}
function createWavBlob(audioChunks) {
const sampleRate = 44100;
const numChannels = 1;
const bitsPerSample = 16;
const bytesPerSample = bitsPerSample / 8;
const blockAlign = numChannels * bytesPerSample;
const buffer = mergeAudioBuffers(audioChunks);
const dataLength = buffer.length * bytesPerSample;
const wavDataLength = 36 + dataLength;
const headerBuffer = new ArrayBuffer(44);
const view = new DataView(headerBuffer);
writeString(view, 0, 'RIFF');
view.setUint32(4, wavDataLength, true);
writeString(view, 8, 'WAVE');
writeString(view, 12, 'fmt ');
view.setUint32(16, 16, true);
view.setUint16(20, 1, true);
view.setUint16(22, numChannels, true);
view.setUint32(24, sampleRate, true);
view.setUint32(28, sampleRate * blockAlign, true);
view.setUint16(32, blockAlign, true);
view.setUint16(34, bitsPerSample, true);
writeString(view, 36, 'data');
view.setUint32(40, dataLength, true);
const wavBuffer = new Int16Array(headerBuffer.byteLength + dataLength);
wavBuffer.set(new Int16Array(headerBuffer));
wavBuffer.set(convertToInt16(buffer), headerBuffer.byteLength / 2);
return new Blob([wavBuffer], { type: 'audio/wav' });
}
function writeString(view, offset, string) {
for (let i = 0; i < string.length; i++) {
view.setUint8(offset + i, string.charCodeAt(i));
}
}
function mergeAudioBuffers(buffers) {
let totalLength = 0;
for (let buffer of buffers) {
totalLength += buffer.length;
}
const result = new Float32Array(totalLength);
let offset = 0;
for (let buffer of buffers) {
result.set(buffer, offset);
offset += buffer.length;
}
return result;
}
function convertToInt16(float32Array) {
const int16Array = new Int16Array(float32Array.length);
for (let i = 0; i < float32Array.length; i++) {
const s = Math.max(-1, Math.min(1, float32Array[i]));
int16Array[i] = s < 0 ? s * 0x8000 : s * 0x7FFF;
}
return int16Array;
}
async function submitToAPI() {
if (!audioBlob) {
alert('Please record audio first.');
return;
}
const apiKey = getAPIKey();
if (!apiKey) {
alert('API Key is required.');
return;
}
submitButton.textContent = 'Processing prompt...';
submitButton.disabled = true;
const base64Audio = await blobToBase64(audioBlob);
const prompt = promptTextarea.value;
const payload = {
model: "gpt-4o-audio-preview",
modalities: ["text"],
messages: [
{
role: "user",
content: [
{type: "text", text: prompt},
{
type: "input_audio",
input_audio: {
data: base64Audio,
format: "wav"
}
}
]
}
]
};
try {
const response = await fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify(payload)
});
const data = await response.json();
displayFormattedResponse(data);
} catch (error) {
console.error('Error:', error);
formattedResponse.innerHTML = marked(`Error: ${error.message}`);
jsonResponse.textContent = '';
} finally {
submitButton.textContent = 'Submit to API';
submitButton.disabled = false;
}
}
function displayFormattedResponse(data) {
let mdResponse = '';
// Display content from choices
if (data.choices && data.choices.length > 0) {
const contents = data.choices.map(choice => choice.message.content).join('\n\n');
mdResponse += `### Response Content\n\n${contents}\n\n`;
}
// Calculate and display token usage and cost
if (data.usage) {
const textTokens = data.usage.prompt_tokens_details.text_tokens || 0;
const audioTokens = data.usage.prompt_tokens_details.audio_tokens || 0;
const textCost = (textTokens * 2.50) / 1000000;
const audioCost = (audioTokens * 100.00) / 1000000;
const totalCostDollars = textCost + audioCost;
const totalCostCents = totalCostDollars * 100;
mdResponse += `### Token Usage and Cost\n\n`;
mdResponse += `- Text input tokens: ${textTokens}\n`;
mdResponse += `- Audio input tokens: ${audioTokens}\n`;
mdResponse += `- Total cost: ${totalCostCents.toFixed(4)} cents\n\n`;
}
// Render markdown response
formattedResponse.innerHTML = marked(mdResponse);
// Display full JSON response
jsonResponse.textContent = JSON.stringify(data, null, 2);
}
function getAPIKey() {
let apiKey = localStorage.getItem('openai_api_key');
if (!apiKey) {
apiKey = prompt('Please enter your OpenAI API Key:');
if (apiKey) {
localStorage.setItem('openai_api_key', apiKey);
}
}
return apiKey;
}
function blobToBase64(blob) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result.split(',')[1]);
reader.onerror = reject;
reader.readAsDataURL(blob);
});
}
function downloadAudio() {
if (audioBlob) {
const url = URL.createObjectURL(audioBlob);
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = 'recorded_audio.wav';
document.body.appendChild(a);
a.click();
setTimeout(() => {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 100);
}
}
</script>
</body>
</html>