Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -251,13 +251,18 @@ def main():
|
|
| 251 |
st.session_state.recording_started = False
|
| 252 |
return
|
| 253 |
|
| 254 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 256 |
-
|
| 257 |
-
audio_file = os.path.join(
|
| 258 |
|
| 259 |
print(f"Saving recording to {audio_file}...")
|
| 260 |
try:
|
|
|
|
| 261 |
saved_file = st.session_state.recorder.save_recording(audio_file)
|
| 262 |
if saved_file and os.path.exists(saved_file):
|
| 263 |
print(f"Successfully saved recording to {saved_file}")
|
|
@@ -375,19 +380,27 @@ def main():
|
|
| 375 |
def clean_up_recordings(keep_last=5):
|
| 376 |
"""Clean up old recording files, keeping only the most recent ones."""
|
| 377 |
try:
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 388 |
os.remove(old_recording)
|
| 389 |
-
|
| 390 |
-
|
|
|
|
| 391 |
except Exception as e:
|
| 392 |
print(f"Error in clean_up_recordings: {e}")
|
| 393 |
|
|
|
|
| 251 |
st.session_state.recording_started = False
|
| 252 |
return
|
| 253 |
|
| 254 |
+
# Ensure recordings directory exists
|
| 255 |
+
recordings_dir = os.path.abspath("recordings")
|
| 256 |
+
os.makedirs(recordings_dir, exist_ok=True)
|
| 257 |
+
|
| 258 |
+
# Generate filename with full path
|
| 259 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 260 |
+
filename = f"recording_{timestamp}.wav"
|
| 261 |
+
audio_file = os.path.join(recordings_dir, filename)
|
| 262 |
|
| 263 |
print(f"Saving recording to {audio_file}...")
|
| 264 |
try:
|
| 265 |
+
# Save with the full absolute path
|
| 266 |
saved_file = st.session_state.recorder.save_recording(audio_file)
|
| 267 |
if saved_file and os.path.exists(saved_file):
|
| 268 |
print(f"Successfully saved recording to {saved_file}")
|
|
|
|
| 380 |
def clean_up_recordings(keep_last=5):
|
| 381 |
"""Clean up old recording files, keeping only the most recent ones."""
|
| 382 |
try:
|
| 383 |
+
# Use absolute path for the recordings directory
|
| 384 |
+
recordings_dir = os.path.abspath("recordings")
|
| 385 |
+
|
| 386 |
+
# Ensure the recordings directory exists
|
| 387 |
+
os.makedirs(recordings_dir, exist_ok=True)
|
| 388 |
+
|
| 389 |
+
# Get all wav files and sort by modification time
|
| 390 |
+
recordings = sorted(
|
| 391 |
+
glob.glob(os.path.join(recordings_dir, "*.wav")),
|
| 392 |
+
key=os.path.getmtime,
|
| 393 |
+
reverse=True
|
| 394 |
+
)
|
| 395 |
+
|
| 396 |
+
# Remove old recordings, keeping the specified number
|
| 397 |
+
for old_recording in recordings[keep_last:]:
|
| 398 |
+
try:
|
| 399 |
+
if os.path.exists(old_recording):
|
| 400 |
os.remove(old_recording)
|
| 401 |
+
print(f"Removed old recording: {old_recording}")
|
| 402 |
+
except Exception as e:
|
| 403 |
+
print(f"Error removing {old_recording}: {e}")
|
| 404 |
except Exception as e:
|
| 405 |
print(f"Error in clean_up_recordings: {e}")
|
| 406 |
|