|
<!DOCTYPE html> |
|
<html> |
|
<head> |
|
<meta charset="utf-8" /> |
|
<meta name="viewport" content="width=device-width" /> |
|
<title>Voice Recorder</title> |
|
<style> |
|
.card { |
|
max-width: 600px; |
|
margin: 2rem auto; |
|
padding: 2rem; |
|
border-radius: 10px; |
|
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1); |
|
text-align: center; |
|
} |
|
|
|
.button { |
|
padding: 1rem 2rem; |
|
margin: 0.5rem; |
|
border: none; |
|
border-radius: 5px; |
|
cursor: pointer; |
|
font-size: 1rem; |
|
} |
|
|
|
.record { |
|
background-color: #ff4444; |
|
color: white; |
|
} |
|
|
|
.record.recording { |
|
background-color: #aa0000; |
|
} |
|
|
|
.status { |
|
margin-top: 1rem; |
|
font-style: italic; |
|
} |
|
|
|
.response { |
|
margin-top: 1rem; |
|
padding: 1rem; |
|
background-color: #f5f5f5; |
|
border-radius: 5px; |
|
} |
|
</style> |
|
</head> |
|
<body> |
|
<div class="card"> |
|
<h1>Voice Recorder</h1> |
|
<button id="recordButton" class="button record">Start Recording</button> |
|
<div id="status" class="status">Click to start recording</div> |
|
<div id="response" class="response"></div> |
|
</div> |
|
|
|
<script> |
|
class VoiceRecorder { |
|
constructor() { |
|
this.mediaRecorder = null; |
|
this.audioChunks = []; |
|
this.isRecording = false; |
|
this.recordButton = document.getElementById('recordButton'); |
|
this.statusDiv = document.getElementById('status'); |
|
this.responseDiv = document.getElementById('response'); |
|
|
|
this.recordButton.addEventListener('click', () => this.toggleRecording()); |
|
|
|
|
|
this.ELEVEN_LABS_API_KEY = 'sk_4202af01226d4d527984c445a2e729d21ec28c5cfb309df7'; |
|
} |
|
|
|
async setupMediaRecorder() { |
|
try { |
|
const stream = await navigator.mediaDevices.getUserMedia({ audio: true }); |
|
this.mediaRecorder = new MediaRecorder(stream); |
|
|
|
this.mediaRecorder.ondataavailable = (event) => { |
|
this.audioChunks.push(event.data); |
|
}; |
|
|
|
this.mediaRecorder.onstop = async () => { |
|
const audioBlob = new Blob(this.audioChunks, { type: 'audio/wav' }); |
|
await this.sendToElevenLabs(audioBlob); |
|
this.audioChunks = []; |
|
}; |
|
|
|
} catch (error) { |
|
console.error('Error accessing microphone:', error); |
|
this.statusDiv.textContent = 'Error accessing microphone. Please ensure microphone permissions are granted.'; |
|
} |
|
} |
|
|
|
async toggleRecording() { |
|
if (!this.mediaRecorder) { |
|
await this.setupMediaRecorder(); |
|
} |
|
|
|
if (!this.isRecording) { |
|
this.startRecording(); |
|
} else { |
|
this.stopRecording(); |
|
} |
|
} |
|
|
|
startRecording() { |
|
this.mediaRecorder.start(); |
|
this.isRecording = true; |
|
this.recordButton.textContent = 'Stop Recording'; |
|
this.recordButton.classList.add('recording'); |
|
this.statusDiv.textContent = 'Recording...'; |
|
} |
|
|
|
stopRecording() { |
|
this.mediaRecorder.stop(); |
|
this.isRecording = false; |
|
this.recordButton.textContent = 'Start Recording'; |
|
this.recordButton.classList.remove('recording'); |
|
this.statusDiv.textContent = 'Processing audio...'; |
|
} |
|
|
|
async sendToElevenLabs(audioBlob) { |
|
try { |
|
const formData = new FormData(); |
|
formData.append('audio', audioBlob); |
|
|
|
const response = await fetch('https://api.elevenlabs.io/v1/speech-to-text', { |
|
method: 'POST', |
|
headers: { |
|
'xi-api-key': this.ELEVEN_LABS_API_KEY, |
|
}, |
|
body: formData |
|
}); |
|
|
|
if (!response.ok) { |
|
throw new Error(`HTTP error! status: ${response.status}`); |
|
} |
|
|
|
const data = await response.json(); |
|
this.responseDiv.textContent = `Transcription: ${data.text}`; |
|
this.statusDiv.textContent = 'Transcription complete'; |
|
|
|
} catch (error) { |
|
console.error('Error sending to Eleven Labs:', error); |
|
this.statusDiv.textContent = 'Error processing audio. Please try again.'; |
|
} |
|
} |
|
} |
|
|
|
|
|
window.addEventListener('load', () => { |
|
new VoiceRecorder(); |
|
}); |
|
</script> |
|
</body> |
|
</html> |