PatienceIzere commited on
Commit
2065f5c
·
verified ·
1 Parent(s): f7dd6d5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -39
app.py CHANGED
@@ -144,53 +144,75 @@ def main():
144
  st.header("🎤 Live Audio Recording")
145
 
146
  # Show available audio devices
 
 
 
147
  try:
148
  if 'list_audio_devices' not in app_config:
149
- st.error("Audio device listing not available")
150
- app_config['RECORDING_ENABLED'] = False
151
- return
152
-
153
- devices = app_config['list_audio_devices']()
154
- if not devices:
155
- st.error("No audio input devices found!")
156
- app_config['RECORDING_ENABLED'] = False
157
  else:
158
- # Create a list of display strings for the dropdown
159
- device_options = [f"{i}: {d['name']} (Channels: {d.get('input_channels', 1)})" for i, d in enumerate(devices)]
160
- selected_device_str = st.selectbox(
161
- "Select audio device:",
162
- options=device_options,
163
- index=0
164
- )
165
- # Get the device index from the selected string
166
- selected_device = device_options.index(selected_device_str)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167
  except Exception as e:
168
- st.error(f"Error getting audio devices: {str(e)}")
169
- app_config['RECORDING_ENABLED'] = False
170
 
171
  col1, col2 = st.columns(2)
172
 
173
  with col1:
174
- # Check if we have any audio devices
175
- devices_available = any(device.get('max_input_channels', 0) > 0
176
- for device in app_config.get('AUDIO_DEVICES', []))
177
-
178
- if not devices_available:
179
- st.warning("⚠️ No audio input devices detected. Recording may not work correctly.")
180
-
181
- if st.button("🎤 Start Recording",
182
- disabled=st.session_state.get('recording', False),
183
- key='start_recording_btn'):
184
- try:
185
- # Create a new recorder instance if needed
186
- if 'recorder' not in st.session_state or st.session_state.recorder is None:
187
- print("Creating new AudioRecorder instance...")
188
- st.session_state.recorder = AudioRecorder(device_index=selected_device)
189
-
190
- print("Starting recording...")
191
- if not devices_available:
192
- st.warning("Recording started without an audio device. This will generate silent audio.")
193
-
194
  if st.session_state.recorder.start_recording():
195
  st.session_state.recording = True
196
  st.session_state.recording_started = True
 
144
  st.header("🎤 Live Audio Recording")
145
 
146
  # Show available audio devices
147
+ # Initialize with default values
148
+ selected_device = None
149
+
150
  try:
151
  if 'list_audio_devices' not in app_config:
152
+ st.warning("⚠️ Audio device listing not available. Using default settings.")
153
+ app_config['RECORDING_ENABLED'] = True # Keep recording enabled but with fallback
 
 
 
 
 
 
154
  else:
155
+ devices = app_config['list_audio_devices']()
156
+ if not devices:
157
+ st.warning("⚠️ No audio input devices found. Using fallback mode.")
158
+ app_config['RECORDING_ENABLED'] = True # Keep recording enabled but with fallback
159
+ else:
160
+ # Filter out devices with no input channels
161
+ input_devices = [d for d in devices if d.get('max_input_channels', 0) > 0]
162
+
163
+ if not input_devices:
164
+ st.warning("⚠️ No input devices with recording capability found. Using fallback mode.")
165
+ app_config['RECORDING_ENABLED'] = True # Keep recording enabled but with fallback
166
+ else:
167
+ # Create a list of display strings for the dropdown
168
+ device_options = [f"{i}: {d['name']} (Channels: {d.get('input_channels', 1)})"
169
+ for i, d in enumerate(input_devices)]
170
+
171
+ # Add a default option
172
+ device_options.insert(0, "Default: Use system default device")
173
+
174
+ selected_device_str = st.selectbox(
175
+ "Select audio device:",
176
+ options=device_options,
177
+ index=0
178
+ )
179
+
180
+ # If default is selected, use None to let sounddevice choose
181
+ if selected_device_str == "Default: Use system default device":
182
+ selected_device = None
183
+ else:
184
+ # Get the device index from the selected string
185
+ selected_device = device_options.index(selected_device_str) - 1 # Adjust for default option
186
+
187
+ # Ensure the index is within bounds
188
+ if selected_device >= len(input_devices):
189
+ selected_device = None
190
  except Exception as e:
191
+ st.warning(f"⚠️ Warning: Could not load audio devices: {str(e)}. Using fallback mode.")
192
+ app_config['RECORDING_ENABLED'] = True # Keep recording enabled but with fallback
193
 
194
  col1, col2 = st.columns(2)
195
 
196
  with col1:
197
+ # Check if recording is enabled and we have a valid recorder
198
+ if not app_config.get('RECORDING_ENABLED', False):
199
+ st.warning("⚠️ Recording is not available in the current environment.")
200
+ st.button("🎤 Start Recording", disabled=True)
201
+ else:
202
+ if st.button("🎤 Start Recording",
203
+ disabled=st.session_state.get('recording', False),
204
+ key='start_recording_btn'):
205
+ try:
206
+ # Create a new recorder instance if needed
207
+ if 'recorder' not in st.session_state or st.session_state.recorder is None:
208
+ print("Creating new AudioRecorder instance...")
209
+ st.session_state.recorder = AudioRecorder(device_index=selected_device)
210
+
211
+ print("Starting recording...")
212
+ # Show appropriate message based on device availability
213
+ if selected_device is None:
214
+ st.info("ℹ️ Using system default audio device. If no device is found, silent audio will be generated.")
215
+
 
216
  if st.session_state.recorder.start_recording():
217
  st.session_state.recording = True
218
  st.session_state.recording_started = True