tsrivallabh commited on
Commit
9715d48
·
verified ·
1 Parent(s): ad0d9c0

Update templates/index.html

Browse files
Files changed (1) hide show
  1. templates/index.html +52 -24
templates/index.html CHANGED
@@ -54,34 +54,62 @@
54
  let fftPlot = null; // To store the Chart.js instance
55
 
56
  startButton.addEventListener("click", async () => {
57
- // Request microphone permission
58
  try {
59
- await navigator.mediaDevices.getUserMedia({ audio: true });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  statusText.textContent = "Recording...";
61
 
62
- // Send POST request to Flask endpoint
63
- const response = await fetch("/process_audio", {
64
- method: "POST",
65
- });
66
-
67
- // Parse JSON response
68
- const data = await response.json();
69
-
70
- // Display results
71
- statusText.textContent = "";
72
- if (data.message) {
73
- displayNoSpeechDetected(data.message);
74
- } else {
75
- displayFFTPlot(
76
- data.peak_frequencies,
77
- data.peak_magnitudes,
78
- data.sorted_indices
79
- );
80
- displayResults(data);
81
- }
82
  } catch (err) {
83
- statusText.textContent = "Microphone permission denied.";
84
- console.error("Microphone permission denied:", err);
85
  }
86
  });
87
 
 
54
  let fftPlot = null; // To store the Chart.js instance
55
 
56
  startButton.addEventListener("click", async () => {
 
57
  try {
58
+ // Request microphone permission and record audio
59
+ const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
60
+ const mediaRecorder = new MediaRecorder(stream);
61
+ const audioChunks = [];
62
+
63
+ mediaRecorder.ondataavailable = (event) => {
64
+ audioChunks.push(event.data);
65
+ };
66
+
67
+ mediaRecorder.onstop = async () => {
68
+ const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
69
+ const formData = new FormData();
70
+ formData.append('audio', audioBlob, 'recording.wav');
71
+
72
+ statusText.textContent = "Processing...";
73
+
74
+ try {
75
+ const response = await fetch("/process_audio", {
76
+ method: "POST",
77
+ body: formData
78
+ });
79
+
80
+ if (!response.ok) {
81
+ throw new Error(`HTTP error! status: ${response.status}`);
82
+ }
83
+
84
+ const data = await response.json();
85
+
86
+ statusText.textContent = "";
87
+ if (data.message) {
88
+ displayNoSpeechDetected(data.message);
89
+ } else {
90
+ displayFFTPlot(
91
+ data.peak_frequencies,
92
+ data.peak_magnitudes,
93
+ data.sorted_indices
94
+ );
95
+ displayResults(data);
96
+ }
97
+ } catch (error) {
98
+ console.error("Error processing audio:", error);
99
+ statusText.textContent = "Error processing audio.";
100
+ }
101
+ };
102
+
103
+ mediaRecorder.start();
104
  statusText.textContent = "Recording...";
105
 
106
+ setTimeout(() => {
107
+ mediaRecorder.stop();
108
+ }, 5000); // Record for 5 seconds
109
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
  } catch (err) {
111
+ console.error("Microphone permission denied or other error:", err);
112
+ statusText.textContent = "Microphone permission denied or other error.";
113
  }
114
  });
115