6565 pre {
6666 white-space : pre-wrap;
6767 }
68+ # downloadButton {
69+ display : none;
70+ }
6871 </ style >
6972</ head >
7073< body >
@@ -73,6 +76,7 @@ <h1>OpenAI Audio</h1>
7376 < button id ="recordButton "> Start Recording</ button >
7477 < div id ="timer "> 00:00</ div >
7578 < audio id ="audioPlayback " controls > </ audio >
79+ < button id ="downloadButton "> Download Audio</ button >
7680 < textarea id ="prompt " placeholder ="Enter your prompt here "> </ textarea >
7781 < button id ="submitButton "> Submit to API</ button >
7882 < div id ="apiResponse ">
@@ -98,9 +102,11 @@ <h1>OpenAI Audio</h1>
98102 const submitButton = document . getElementById ( 'submitButton' ) ;
99103 const formattedResponse = document . getElementById ( 'formattedResponse' ) ;
100104 const jsonResponse = document . getElementById ( 'jsonResponse' ) ;
105+ const downloadButton = document . getElementById ( 'downloadButton' ) ;
101106
102107 recordButton . addEventListener ( 'click' , toggleRecording ) ;
103108 submitButton . addEventListener ( 'click' , submitToAPI ) ;
109+ downloadButton . addEventListener ( 'click' , downloadAudio ) ;
104110
105111 async function toggleRecording ( ) {
106112 if ( ! isRecording ) {
@@ -132,6 +138,7 @@ <h1>OpenAI Audio</h1>
132138 updateTimer ( ) ;
133139 timerInterval = setInterval ( updateTimer , 1000 ) ;
134140 recordButton . textContent = 'Stop Recording' ;
141+ downloadButton . style . display = 'none' ;
135142 } catch ( error ) {
136143 console . error ( 'Error starting recording:' , error ) ;
137144 alert ( 'Error starting recording. Please make sure you have given permission to use the microphone.' ) ;
@@ -153,6 +160,7 @@ <h1>OpenAI Audio</h1>
153160
154161 const audioUrl = URL . createObjectURL ( audioBlob ) ;
155162 audioPlayback . src = audioUrl ;
163+ downloadButton . style . display = 'inline-block' ;
156164 }
157165
158166 function updateTimer ( ) {
@@ -336,6 +344,22 @@ <h1>OpenAI Audio</h1>
336344 reader . readAsDataURL ( blob ) ;
337345 } ) ;
338346 }
347+
348+ function downloadAudio ( ) {
349+ if ( audioBlob ) {
350+ const url = URL . createObjectURL ( audioBlob ) ;
351+ const a = document . createElement ( 'a' ) ;
352+ a . style . display = 'none' ;
353+ a . href = url ;
354+ a . download = 'recorded_audio.wav' ;
355+ document . body . appendChild ( a ) ;
356+ a . click ( ) ;
357+ setTimeout ( ( ) => {
358+ document . body . removeChild ( a ) ;
359+ window . URL . revokeObjectURL ( url ) ;
360+ } , 100 ) ;
361+ }
362+ }
339363 </ script >
340364</ body >
341365</ html >
0 commit comments