|
<!DOCTYPE html> |
|
<html lang="en"> |
|
<head> |
|
<meta charset="UTF-8"> |
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
|
<title>Voice Chat Interface</title> |
|
</head> |
|
<body> |
|
<h1>Voice Chat</h1> |
|
<button id="recordButton">Start Listening</button> |
|
<button id="generateButton">Generate Speech</button> |
|
<audio id="audioPlayer" controls></audio> |
|
|
|
<script> |
|
let mediaRecorder; |
|
let audioChunks = []; |
|
|
|
document.getElementById("recordButton").addEventListener("click", async () => { |
|
if (!mediaRecorder || mediaRecorder.state === "inactive") { |
|
const stream = await navigator.mediaDevices.getUserMedia({ audio: true }); |
|
mediaRecorder = new MediaRecorder(stream); |
|
mediaRecorder.ondataavailable = event => audioChunks.push(event.data); |
|
mediaRecorder.onstop = async () => { |
|
const audioBlob = new Blob(audioChunks, { type: "audio/wav" }); |
|
const formData = new FormData(); |
|
formData.append("file", audioBlob, "recording.wav"); |
|
|
|
const response = await fetch("/chat/", { |
|
method: "POST", |
|
body: formData |
|
}); |
|
const audioData = await response.blob(); |
|
document.getElementById("audioPlayer").src = URL.createObjectURL(audioData); |
|
}; |
|
audioChunks = []; |
|
mediaRecorder.start(); |
|
setTimeout(() => mediaRecorder.stop(), 5000); |
|
} |
|
}); |
|
|
|
document.getElementById("generateButton").addEventListener("click", async () => { |
|
const inputText = prompt("Enter text to convert to speech:"); |
|
if (inputText) { |
|
const response = await fetch("/tts/", { |
|
method: "POST", |
|
headers: { "Content-Type": "application/json" }, |
|
body: JSON.stringify({ text:inputText}) |
|
}); |
|
const audioData = await response.blob(); |
|
document.getElementById("audioPlayer").src = URL.createObjectURL(audioData); |
|
} |
|
}); |
|
</script> |
|
</body> |
|
</html> |
|
|