akshansh36 commited on
Commit
6d36763
·
verified ·
1 Parent(s): 6358e84

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -26
app.py CHANGED
@@ -11,19 +11,20 @@ with gr.Blocks() as demo:
11
  audio_input = gr.Audio(sources="microphone", streaming=True, visible=False)
12
  audio_output = gr.Audio(streaming=True, visible=False)
13
 
14
- # Custom HTML for Web Audio API
15
  html = gr.HTML("""
16
- <button id="startButton">Start</button>
17
- <button id="stopButton">Stop</button>
18
- <div id="status"></div>
19
-
20
- <script>
21
- let audioContext;
22
- let mediaStreamSource;
23
- let processor;
24
- let recording = false;
25
-
26
- async function startRecording() {
 
 
27
  audioContext = new (window.AudioContext || window.webkitAudioContext)();
28
  const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
29
  mediaStreamSource = audioContext.createMediaStreamSource(stream);
@@ -35,28 +36,41 @@ with gr.Blocks() as demo:
35
  processor.onaudioprocess = function(e) {
36
  if (!recording) return;
37
  const audioData = e.inputBuffer.getChannelData(0);
 
38
  // Send audio data to the server
39
- gradioApp().querySelector('#component-0').querySelector('input[type=file]').files = new FileList([new File([audioData], 'audio.wav', {type: 'audio/wav'})]);
 
 
 
 
40
  gradioApp().querySelector('#component-0').querySelector('button[type=submit]').click();
41
  };
42
 
43
  recording = true;
44
  document.getElementById('status').textContent = 'Recording...';
 
 
 
 
 
45
  }
46
-
47
- function stopRecording() {
48
- if (processor) {
49
- processor.disconnect();
50
- mediaStreamSource.disconnect();
51
- }
52
- recording = false;
53
- document.getElementById('status').textContent = 'Stopped';
54
  }
55
-
56
- document.getElementById('startButton').addEventListener('click', startRecording);
57
- document.getElementById('stopButton').addEventListener('click', stopRecording);
58
- </script>
59
- """)
 
 
 
 
 
60
 
61
  audio_input.stream(process_audio, inputs=audio_input, outputs=audio_output)
62
 
 
11
  audio_input = gr.Audio(sources="microphone", streaming=True, visible=False)
12
  audio_output = gr.Audio(streaming=True, visible=False)
13
 
 
14
  html = gr.HTML("""
15
+ <button id="startButton">Start Recording</button>
16
+ <button id="stopButton" disabled>Stop Recording</button>
17
+ <div id="status">Ready</div>
18
+ <div id="debug"></div>
19
+
20
+ <script>
21
+ let audioContext;
22
+ let mediaStreamSource;
23
+ let processor;
24
+ let recording = false;
25
+
26
+ async function startRecording() {
27
+ try {
28
  audioContext = new (window.AudioContext || window.webkitAudioContext)();
29
  const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
30
  mediaStreamSource = audioContext.createMediaStreamSource(stream);
 
36
  processor.onaudioprocess = function(e) {
37
  if (!recording) return;
38
  const audioData = e.inputBuffer.getChannelData(0);
39
+ document.getElementById('debug').textContent = 'Processing audio chunk...';
40
  // Send audio data to the server
41
+ const blob = new Blob([audioData], {type: 'audio/wav'});
42
+ const file = new File([blob], 'audio.wav', {type: 'audio/wav'});
43
+ const dt = new DataTransfer();
44
+ dt.items.add(file);
45
+ gradioApp().querySelector('#component-0').querySelector('input[type=file]').files = dt.files;
46
  gradioApp().querySelector('#component-0').querySelector('button[type=submit]').click();
47
  };
48
 
49
  recording = true;
50
  document.getElementById('status').textContent = 'Recording...';
51
+ document.getElementById('startButton').disabled = true;
52
+ document.getElementById('stopButton').disabled = false;
53
+ } catch (err) {
54
+ console.error('Error starting recording:', err);
55
+ document.getElementById('status').textContent = 'Error: ' + err.message;
56
  }
57
+ }
58
+
59
+ function stopRecording() {
60
+ if (processor) {
61
+ processor.disconnect();
62
+ mediaStreamSource.disconnect();
 
 
63
  }
64
+ recording = false;
65
+ document.getElementById('status').textContent = 'Stopped';
66
+ document.getElementById('startButton').disabled = false;
67
+ document.getElementById('stopButton').disabled = true;
68
+ }
69
+
70
+ document.getElementById('startButton').addEventListener('click', startRecording);
71
+ document.getElementById('stopButton').addEventListener('click', stopRecording);
72
+ </script>
73
+ """)
74
 
75
  audio_input.stream(process_audio, inputs=audio_input, outputs=audio_output)
76