PatienceIzere commited on
Commit
2a547a7
·
verified ·
1 Parent(s): cc15387

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -15
app.py CHANGED
@@ -251,13 +251,18 @@ def main():
251
  st.session_state.recording_started = False
252
  return
253
 
254
- # Save the recording
 
 
 
 
255
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
256
- os.makedirs("recordings", exist_ok=True)
257
- audio_file = os.path.join("recordings", f"recording_{timestamp}.wav")
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
- if os.path.exists("recordings"):
379
- # Get all wav files and sort by modification time
380
- recordings = sorted(
381
- glob.glob("recordings/*.wav"),
382
- key=os.path.getmtime,
383
- reverse=True
384
- )
385
- # Remove old recordings, keeping the specified number
386
- for old_recording in recordings[keep_last:]:
387
- try:
 
 
 
 
 
 
 
388
  os.remove(old_recording)
389
- except Exception as e:
390
- print(f"Error removing {old_recording}: {e}")
 
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