File size: 3,389 Bytes
45a4975
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SKYNET Voice Authentication</title>
<style>
  body, html {
    height: 100%;
    margin: 0;
    font-family: Arial, sans-serif;
    color: white;
    text-align: center;
  }
  .bg {
    background-image: url('/static/skynet.png');
    height: 100%;
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
    position: relative;
  }
  .content {
    position: absolute;
    bottom: 10%;
    width: 100%;
  }
  .controls {
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .button {
    background-color: red;
    color: white;
    padding: 10px 20px;
    border: none;
    border-radius: 5px;
    font-size: 16px;
    cursor: pointer;
    margin: 5px;
  }
  .button:hover {
    background-color: darkred;
  }
  audio {
    margin: 0 20px;
  }
</style>
</head>
<body>

<div class="bg">
  <div class="content">
    <h1>Voice Authentication</h1>
    <div class="controls">
      <button id="recordButton" class="button">Record</button>
      <button id="stopButton" class="button" disabled>Stop</button>
      <audio id="audioPlayback" controls style="display:none;"></audio>
      <button id="submitButton" class="button" style="float:right; display:none;">Submit</button>
     </div>
  <p id="result"></p>
  </div>
</div>

<script>
  let recordButton = document.getElementById('recordButton');
  let stopButton = document.getElementById('stopButton');
  let audioPlayback = document.getElementById('audioPlayback');
  let submitButton = document.getElementById('submitButton');

  let mediaRecorder;
  let audioBlob;
  let audioChunks = [];

  recordButton.onclick = function() {
    navigator.mediaDevices.getUserMedia({ audio: true })
      .then(stream => {
        mediaRecorder = new MediaRecorder(stream);
        mediaRecorder.start();
        audioChunks = [];

        document.getElementById("result").innerHTML = "recording started";
        stopButton.disabled = false;
        recordButton.disabled = true;

        mediaRecorder.ondataavailable = function(event) {
          audioChunks.push(event.data);
        };

        mediaRecorder.onstop = function() {
          document.getElementById("result").innerHTML = "recording stopped";
          audioBlob = new Blob(audioChunks);
          let audioUrl = URL.createObjectURL(audioBlob);
          audioPlayback.src = audioUrl;
          audioPlayback.style.display = 'block';
        };
      });
  };

  stopButton.onclick = function() {
    mediaRecorder.stop();
    stopButton.disabled = true;
    recordButton.disabled = false;
    submitButton.style.display = 'block';
  };

  // Submit button handler
  submitButton.onclick = async function() {
    document.getElementById("result").innerHTML = "Processing..."
    //await new Promise(r => setTimeout(r, 2000));
    console.log('Submit the audio for processing');
    let formData = new FormData();
    formData.append('audio_data', audioBlob);
    fetch('/compare_audio2', {
            method: 'POST',
            body: formData
     })
    .then(response => response.json())
    .then(data => {
        document.getElementById("result").innerHTML = data.result;
    })
    .catch(error => console.error('Error:', error));
  };
</script>

</body>
</html>