Tri4 commited on
Commit
615ea00
·
verified ·
1 Parent(s): 7638b1b

Update static/js/script.js

Browse files
Files changed (1) hide show
  1. static/js/script.js +49 -27
static/js/script.js CHANGED
@@ -2,8 +2,15 @@ let isRecording = false;
2
  let mediaRecorder;
3
  let recordedChunks = [];
4
 
5
- // Handles the recording button click event
6
- document.getElementById('recordButton').addEventListener('click', function () {
 
 
 
 
 
 
 
7
  if (!isRecording) {
8
  startRecording();
9
  } else {
@@ -14,19 +21,29 @@ document.getElementById('recordButton').addEventListener('click', function () {
14
  function startRecording() {
15
  navigator.mediaDevices.getUserMedia({ audio: true })
16
  .then(function (stream) {
17
- mediaRecorder = new MediaRecorder(stream);
18
- mediaRecorder.start();
19
 
20
  mediaRecorder.ondataavailable = function (e) {
21
- recordedChunks.push(e.data);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  };
23
 
24
- // Update button text and style
25
- document.getElementById('recordButton').textContent = 'Stop';
26
- document.getElementById('recordButton').classList.add('recording');
27
- document.getElementById('recordStatus').textContent = 'Recording...';
28
- document.getElementById('transcribeContainer').style.display = 'none'; // Hide Transcribe button initially
29
- isRecording = true;
30
  })
31
  .catch(function (err) {
32
  console.error('The following error occurred: ' + err);
@@ -34,40 +51,45 @@ function startRecording() {
34
  }
35
 
36
  function stopRecording() {
37
- mediaRecorder.stop();
 
 
38
 
39
  mediaRecorder.onstop = function () {
40
- const blob = new Blob(recordedChunks, { type: 'audio/webm' });
41
  recordedChunks = [];
42
  const audioURL = window.URL.createObjectURL(blob);
43
 
44
- // Update button text and style
45
- document.getElementById('recordButton').textContent = 'Start';
46
- document.getElementById('recordButton').classList.remove('recording');
47
- document.getElementById('recordStatus').textContent = 'Tap to Record';
48
- document.getElementById('transcribeContainer').style.display = 'block'; // Show Transcribe button
49
 
50
- // Set up the Transcribe button click event
51
- document.getElementById('transcribeButton').addEventListener('click', function () {
52
- transcribeAudio(blob);
53
- });
54
 
 
 
55
  isRecording = false;
56
  };
57
  }
58
 
59
- function transcribeAudio(blob) {
 
 
 
 
 
60
  const formData = new FormData();
61
- formData.append('audio', blob, 'audio.webm');
62
 
63
- //fetch('https://tri4-semalab.hf.space/transcribe', { // https://jikoni-semabox.hf.space/transcribe
64
- fetch('https://jikoni-semabox.hf.space/transcribe', { // https://tri4-semalab.hf.space/transcribe
65
  method: 'POST',
66
  body: formData
67
  })
68
  .then(response => response.json())
69
  .then(data => {
70
- document.getElementById('output').textContent = data.transcription;
71
  })
72
  .catch(error => {
73
  console.error('Error:', error);
 
2
  let mediaRecorder;
3
  let recordedChunks = [];
4
 
5
+ // Get elements
6
+ const micContainer = document.getElementsByClassName('mic-container')[0];
7
+ const circle = micContainer.getElementsByClassName('circle')[0];
8
+ const outputContainer = document.getElementById('output');
9
+ const audioPlayerContainer = document.getElementById('audioPlayerContainer');
10
+ const audioPlayer = document.getElementById('audioPlayer');
11
+
12
+ // Handle click event for the microphone container
13
+ micContainer.addEventListener('click', function () {
14
  if (!isRecording) {
15
  startRecording();
16
  } else {
 
21
  function startRecording() {
22
  navigator.mediaDevices.getUserMedia({ audio: true })
23
  .then(function (stream) {
24
+ // Create a MediaRecorder instance with MIME type 'audio/wav'
25
+ mediaRecorder = new MediaRecorder(stream, { mimeType: 'audio/wav' });
26
 
27
  mediaRecorder.ondataavailable = function (e) {
28
+ if (e.data.size > 0) {
29
+ recordedChunks.push(e.data);
30
+ }
31
+ };
32
+
33
+ mediaRecorder.onstart = function () {
34
+ // Update UI for recording
35
+ circle.classList.add('active');
36
+ outputContainer.textContent = 'Recording...';
37
+ audioPlayerContainer.style.display = 'none'; // Hide audio player initially
38
+ isRecording = true;
39
+ };
40
+
41
+ mediaRecorder.onerror = function (e) {
42
+ console.error('Error recording audio:', e);
43
+ stopRecording(); // Stop recording on error
44
  };
45
 
46
+ mediaRecorder.start();
 
 
 
 
 
47
  })
48
  .catch(function (err) {
49
  console.error('The following error occurred: ' + err);
 
51
  }
52
 
53
  function stopRecording() {
54
+ if (mediaRecorder) {
55
+ mediaRecorder.stop();
56
+ }
57
 
58
  mediaRecorder.onstop = function () {
59
+ const blob = new Blob(recordedChunks, { type: 'audio/wav' });
60
  recordedChunks = [];
61
  const audioURL = window.URL.createObjectURL(blob);
62
 
63
+ // Update UI after recording
64
+ circle.classList.remove('active');
65
+ outputContainer.textContent = 'Click to Transcribe';
 
 
66
 
67
+ // Display audio player
68
+ audioPlayer.src = audioURL;
69
+ audioPlayerContainer.style.display = 'block';
 
70
 
71
+ // Handle transcription
72
+ outputContainer.addEventListener('click', handleTranscribeClick, { once: true });
73
  isRecording = false;
74
  };
75
  }
76
 
77
+ function handleTranscribeClick() {
78
+ transcribeAudio();
79
+ }
80
+
81
+ function transcribeAudio() {
82
+ const blob = new Blob(recordedChunks, { type: 'audio/wav' });
83
  const formData = new FormData();
84
+ formData.append('audio', blob, 'audio.wav');
85
 
86
+ fetch('https://jikoni-semabox.hf.space/transcribe', {
 
87
  method: 'POST',
88
  body: formData
89
  })
90
  .then(response => response.json())
91
  .then(data => {
92
+ outputContainer.textContent = `Transcription: ${data.transcription || 'No transcription available'}`;
93
  })
94
  .catch(error => {
95
  console.error('Error:', error);