Spaces:
Running
Running
Update app.py
Browse files
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.
|
| 150 |
-
app_config['RECORDING_ENABLED'] =
|
| 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 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
except Exception as e:
|
| 168 |
-
st.
|
| 169 |
-
app_config['RECORDING_ENABLED'] =
|
| 170 |
|
| 171 |
col1, col2 = st.columns(2)
|
| 172 |
|
| 173 |
with col1:
|
| 174 |
-
# Check if we have
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
st.
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 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
|