Spaces:
Runtime error
Runtime error
GitLab CI
commited on
Commit
·
af326b7
1
Parent(s):
d2e1f69
Update game build from GitLab CI
Browse files- server/static/index.html +72 -43
server/static/index.html
CHANGED
|
@@ -17,56 +17,85 @@
|
|
| 17 |
// URL of the server to send audio chunks
|
| 18 |
const serverUrl = "./api/process"
|
| 19 |
|
| 20 |
-
// Check
|
| 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 |
-
console.error('Error sending audio chunk:', error)
|
| 53 |
-
})
|
| 54 |
-
}
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
}
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
</script>
|
| 71 |
</body>
|
| 72 |
|
|
|
|
| 17 |
// URL of the server to send audio chunks
|
| 18 |
const serverUrl = "./api/process"
|
| 19 |
|
| 20 |
+
// Check server availability first
|
| 21 |
+
fetch("./api/data", {
|
| 22 |
+
method: 'GET'
|
| 23 |
+
})
|
| 24 |
+
.then(response => {
|
| 25 |
+
if (!response.ok) {
|
| 26 |
+
throw new Error(`Server check failed: ${response.status}`)
|
| 27 |
+
}
|
| 28 |
+
console.log('Server check successful')
|
| 29 |
+
setupAudioRecording()
|
| 30 |
+
})
|
| 31 |
+
.catch(error => {
|
| 32 |
+
console.error('Server check failed:', error)
|
| 33 |
+
alert('Could not connect to the server. Please try again later.')
|
| 34 |
+
throw error
|
| 35 |
+
})
|
| 36 |
|
| 37 |
+
// Move existing audio setup into a function
|
| 38 |
+
function setupAudioRecording() {
|
| 39 |
+
// Check if browser supports audio recording
|
| 40 |
+
if (!navigator.mediaDevices?.getUserMedia) {
|
| 41 |
+
console.error('Your browser does not support audio recording.')
|
| 42 |
+
alert('Your browser does not support audio recording. Please try using a modern browser like Chrome, Firefox, or Edge.')
|
| 43 |
+
throw new Error('Audio recording not supported')
|
| 44 |
+
}
|
| 45 |
|
| 46 |
+
navigator.mediaDevices.getUserMedia({ audio: true })
|
| 47 |
+
.then(stream => {
|
| 48 |
+
const audioContext = new (window.AudioContext || window.webkitAudioContext)()
|
| 49 |
+
const mediaRecorder = new MediaRecorder(stream)
|
| 50 |
+
const audioChunks = []
|
| 51 |
|
| 52 |
+
mediaRecorder.ondataavailable = event => {
|
| 53 |
+
audioChunks.push(event.data)
|
| 54 |
+
}
|
| 55 |
|
| 56 |
+
mediaRecorder.onstop = () => {
|
| 57 |
+
const audioBlob = new Blob(audioChunks, { type: 'audio/webm' })
|
| 58 |
+
audioChunks.length = 0 // Clear chunks after creating the Blob
|
| 59 |
|
| 60 |
+
// Convert Blob to base64
|
| 61 |
+
const reader = new FileReader()
|
| 62 |
+
reader.readAsDataURL(audioBlob)
|
| 63 |
+
reader.onloadend = () => {
|
| 64 |
+
// Extract the base64 data (remove the data URL prefix)
|
| 65 |
+
const base64Audio = reader.result.split(',')[1]
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
// Send as JSON with base64-encoded audio
|
| 68 |
+
fetch(serverUrl, {
|
| 69 |
+
method: 'POST',
|
| 70 |
+
headers: {
|
| 71 |
+
'Content-Type': 'application/json'
|
| 72 |
+
},
|
| 73 |
+
body: JSON.stringify({
|
| 74 |
+
audio_chunk: base64Audio
|
| 75 |
+
})
|
| 76 |
+
}).then(response => {
|
| 77 |
+
console.log('Audio chunk sent successfully')
|
| 78 |
+
}).catch(error => {
|
| 79 |
+
console.error('Error sending audio chunk:', error)
|
| 80 |
+
})
|
| 81 |
+
}
|
| 82 |
}
|
| 83 |
+
|
| 84 |
+
// Start recording in intervals
|
| 85 |
+
const chunkInterval = 300 // Chunk duration in milliseconds
|
| 86 |
+
setInterval(() => {
|
| 87 |
+
if (mediaRecorder.state === 'recording') {
|
| 88 |
+
mediaRecorder.stop()
|
| 89 |
+
mediaRecorder.start()
|
| 90 |
+
} else {
|
| 91 |
+
mediaRecorder.start()
|
| 92 |
+
}
|
| 93 |
+
}, chunkInterval)
|
| 94 |
+
})
|
| 95 |
+
.catch(error => {
|
| 96 |
+
console.error('Error accessing microphone:', error)
|
| 97 |
+
})
|
| 98 |
+
}
|
| 99 |
</script>
|
| 100 |
</body>
|
| 101 |
|