yunusajib commited on
Commit
a5ec76f
·
verified ·
1 Parent(s): cf17ab8

update code

Browse files
Files changed (1) hide show
  1. app.py +47 -24
app.py CHANGED
@@ -56,40 +56,63 @@ class AudioProcessor:
56
 
57
  def _process_with_ffmpeg(self, audio_input):
58
  # Your existing FFmpeg processing
59
- pass
60
-
61
- def _process_with_sounddevice(self, audio_input):
62
- # Process using sounddevice
63
- duration = 5 # seconds
64
- print(f"Recording with sounddevice (rate={self.sample_rate})...")
65
- audio_data = sd.rec(int(duration * self.sample_rate),
66
- samplerate=self.sample_rate,
67
- channels=1)
68
- sd.wait()
69
- return (audio_data.flatten(), self.sample_rate)
70
-
71
- def _process_with_librosa(self, audio_input):
72
- # Process using librosa
73
- import librosa
74
  if isinstance(audio_input, tuple):
75
  return audio_input
76
- elif isinstance(audio_input, str):
77
- return librosa.load(audio_input, sr=self.sample_rate)
78
- else:
79
- # Handle other input types
80
  with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as tmp:
81
  tmp.write(audio_input)
82
  tmp.flush()
83
- data, sr = librosa.load(tmp.name, sr=self.sample_rate)
 
 
 
 
84
  os.unlink(tmp.name)
85
- return (data, sr)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
 
87
  def _process_fallback(self, audio_input):
88
- # Simple numpy fallback
89
  if isinstance(audio_input, tuple):
90
  return audio_input
91
- return (np.random.random(16000), 16000 # Mock data
92
-
93
  # Modified Interface with Audio Debugging
94
  def create_debug_interface():
95
  audio_processor = AudioProcessor()
 
56
 
57
  def _process_with_ffmpeg(self, audio_input):
58
  # Your existing FFmpeg processing
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  if isinstance(audio_input, tuple):
60
  return audio_input
61
+ try:
62
+ import ffmpeg
63
+ # Process audio file with ffmpeg
 
64
  with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as tmp:
65
  tmp.write(audio_input)
66
  tmp.flush()
67
+ out, _ = (
68
+ ffmpeg.input(tmp.name)
69
+ .output('pipe:', format='f32le', ac=1, ar=self.sample_rate)
70
+ .run(capture_stdout=True)
71
+ )
72
  os.unlink(tmp.name)
73
+ return (np.frombuffer(out, dtype=np.float32), self.sample_rate)
74
+ except Exception as e:
75
+ raise Exception(f"FFmpeg processing failed: {str(e)}")
76
+
77
+ def _process_with_sounddevice(self, audio_input):
78
+ # Process using sounddevice
79
+ if isinstance(audio_input, tuple):
80
+ return audio_input
81
+ try:
82
+ duration = 5 # seconds
83
+ print(f"Recording with sounddevice (rate={self.sample_rate})...")
84
+ audio_data = sd.rec(int(duration * self.sample_rate),
85
+ samplerate=self.sample_rate,
86
+ channels=1)
87
+ sd.wait()
88
+ return (audio_data.flatten(), self.sample_rate)
89
+ except Exception as e:
90
+ raise Exception(f"Sounddevice processing failed: {str(e)}")
91
+
92
+ def _process_with_librosa(self, audio_input):
93
+ # Process using librosa
94
+ try:
95
+ import librosa
96
+ if isinstance(audio_input, tuple):
97
+ return audio_input
98
+ elif isinstance(audio_input, str):
99
+ return librosa.load(audio_input, sr=self.sample_rate)
100
+ else:
101
+ # Handle other input types
102
+ with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as tmp:
103
+ tmp.write(audio_input)
104
+ tmp.flush()
105
+ data, sr = librosa.load(tmp.name, sr=self.sample_rate)
106
+ os.unlink(tmp.name)
107
+ return (data, sr)
108
+ except Exception as e:
109
+ raise Exception(f"Librosa processing failed: {str(e)}")
110
 
111
  def _process_fallback(self, audio_input):
112
+ # Simple numpy fallback with proper error handling
113
  if isinstance(audio_input, tuple):
114
  return audio_input
115
+ return (np.random.random(16000), 16000) # Now properly closed
 
116
  # Modified Interface with Audio Debugging
117
  def create_debug_interface():
118
  audio_processor = AudioProcessor()