GitLab CI commited on
Commit
af326b7
·
1 Parent(s): d2e1f69

Update game build from GitLab CI

Browse files
Files changed (1) hide show
  1. 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 if browser supports audio recording
21
- if (!navigator.mediaDevices?.getUserMedia) {
22
- console.error('Your browser does not support audio recording.')
23
- alert('Your browser does not support audio recording. Please try using a modern browser like Chrome, Firefox, or Edge.')
24
- throw new Error('Audio recording not supported')
25
- }
 
 
 
 
 
 
 
 
 
 
26
 
27
- // Set up audio recording
28
- navigator.mediaDevices.getUserMedia({ audio: true })
29
- .then(stream => {
30
- const audioContext = new (window.AudioContext || window.webkitAudioContext)()
31
- const mediaRecorder = new MediaRecorder(stream)
32
- const audioChunks = []
 
 
33
 
34
- mediaRecorder.ondataavailable = event => {
35
- audioChunks.push(event.data)
36
- }
 
 
37
 
38
- mediaRecorder.onstop = () => {
39
- const audioBlob = new Blob(audioChunks, { type: 'audio/webm' })
40
- audioChunks.length = 0 // Clear chunks after creating the Blob
41
 
42
- // Send the audio chunk to the server
43
- const formData = new FormData()
44
- formData.append('audio', audioBlob)
45
 
46
- fetch(serverUrl, {
47
- method: 'POST',
48
- body: formData
49
- }).then(response => {
50
- console.log('Audio chunk sent successfully')
51
- }).catch(error => {
52
- console.error('Error sending audio chunk:', error)
53
- })
54
- }
55
 
56
- // Start recording in intervals
57
- const chunkInterval = 300 // Chunk duration in milliseconds
58
- setInterval(() => {
59
- if (mediaRecorder.state === 'recording') {
60
- mediaRecorder.stop()
61
- mediaRecorder.start()
62
- } else {
63
- mediaRecorder.start()
 
 
 
 
 
 
 
64
  }
65
- }, chunkInterval)
66
- })
67
- .catch(error => {
68
- console.error('Error accessing microphone:', error)
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