root commited on
Commit
670bed3
1 Parent(s): 50549e3
Files changed (1) hide show
  1. app.py +21 -14
app.py CHANGED
@@ -2585,9 +2585,9 @@ def format_beat_timeline(audio_file, lyrics=None):
2585
  timeline += "S = Strong beat | m = Medium beat | w = Weak beat | 路 = No beat\n\n"
2586
 
2587
  # Calculate total duration and create time markers
2588
- if beats_info['beat_times']:
2589
- # Convert to float if it's a numpy array
2590
- max_beat_time = float(max(beats_info['beat_times'])) if isinstance(max(beats_info['beat_times']), np.ndarray) else max(beats_info['beat_times'])
2591
  total_duration = max_beat_time + 2 # Add 2 seconds of padding
2592
  else:
2593
  total_duration = 30 # Default duration if no beats found
@@ -2668,7 +2668,7 @@ def format_beat_timeline(audio_file, lyrics=None):
2668
  # Calculate end time (start of next measure or end of song)
2669
  if i < len(measure_starts) - 1:
2670
  end_time = measure_starts[i+1][1]
2671
- elif beats_info['beat_times']:
2672
  # Get the last beat time and convert to float if needed
2673
  last_beat = beats_info['beat_times'][-1]
2674
  end_time = float(last_beat) if isinstance(last_beat, np.ndarray) else last_beat
@@ -2685,17 +2685,19 @@ def format_beat_timeline(audio_file, lyrics=None):
2685
  break
2686
 
2687
  # Add phrase information
2688
- if beats_info['phrases']:
2689
  timeline += "\n=== MUSICAL PHRASES ===\n\n"
2690
  for i, phrase in enumerate(beats_info['phrases']):
2691
  if i < 10: # Limit to first 10 phrases
2692
  if not phrase:
2693
  continue
2694
-
2695
- start_beat = phrase[0]
2696
- end_beat = phrase[-1]
2697
- if start_beat >= len(beats_info['beat_times']) or end_beat >= len(beats_info['beat_times']):
2698
  continue
 
 
 
2699
 
2700
  # Convert to float if needed
2701
  phrase_start = beats_info['beat_times'][start_beat]
@@ -2788,11 +2790,12 @@ def format_beat_timeline(audio_file, lyrics=None):
2788
  timeline += f" Syllables: {syllable_count}\n"
2789
 
2790
  # Show ideal timing (if we have enough phrases)
2791
- if beats_info['phrases'] and i < len(beats_info['phrases']):
2792
  phrase = beats_info['phrases'][i]
2793
- if phrase and phrase[0] < len(beats_info['beat_times']) and phrase[-1] < len(beats_info['beat_times']):
2794
- start_beat = phrase[0]
2795
- end_beat = phrase[-1]
 
2796
 
2797
  start_time = beats_info['beat_times'][start_beat]
2798
  start_time = float(start_time) if isinstance(start_time, np.ndarray) else start_time
@@ -2814,7 +2817,11 @@ def format_beat_timeline(audio_file, lyrics=None):
2814
  if j < len(beats_info['beat_times']):
2815
  beat_time = beats_info['beat_times'][j]
2816
  beat_time = float(beat_time) if isinstance(beat_time, np.ndarray) else beat_time
2817
- relative_pos = int((beat_time - start_time) / phrase_duration * syllable_count)
 
 
 
 
2818
 
2819
  while len(syllable_viz) <= relative_pos:
2820
  syllable_viz.append("路")
 
2585
  timeline += "S = Strong beat | m = Medium beat | w = Weak beat | 路 = No beat\n\n"
2586
 
2587
  # Calculate total duration and create time markers
2588
+ if beats_info['beat_times'] and len(beats_info['beat_times']) > 0:
2589
+ # Get the max value safely
2590
+ max_beat_time = max([float(t) if isinstance(t, np.ndarray) else t for t in beats_info['beat_times']])
2591
  total_duration = max_beat_time + 2 # Add 2 seconds of padding
2592
  else:
2593
  total_duration = 30 # Default duration if no beats found
 
2668
  # Calculate end time (start of next measure or end of song)
2669
  if i < len(measure_starts) - 1:
2670
  end_time = measure_starts[i+1][1]
2671
+ elif beats_info['beat_times'] and len(beats_info['beat_times']) > 0:
2672
  # Get the last beat time and convert to float if needed
2673
  last_beat = beats_info['beat_times'][-1]
2674
  end_time = float(last_beat) if isinstance(last_beat, np.ndarray) else last_beat
 
2685
  break
2686
 
2687
  # Add phrase information
2688
+ if 'phrases' in beats_info and beats_info['phrases']:
2689
  timeline += "\n=== MUSICAL PHRASES ===\n\n"
2690
  for i, phrase in enumerate(beats_info['phrases']):
2691
  if i < 10: # Limit to first 10 phrases
2692
  if not phrase:
2693
  continue
2694
+
2695
+ # Safely check phrase indices
2696
+ if not (len(phrase) > 0 and len(beats_info['beat_times']) > 0):
 
2697
  continue
2698
+
2699
+ start_beat = min(phrase[0], len(beats_info['beat_times'])-1)
2700
+ end_beat = min(phrase[-1], len(beats_info['beat_times'])-1)
2701
 
2702
  # Convert to float if needed
2703
  phrase_start = beats_info['beat_times'][start_beat]
 
2790
  timeline += f" Syllables: {syllable_count}\n"
2791
 
2792
  # Show ideal timing (if we have enough phrases)
2793
+ if 'phrases' in beats_info and beats_info['phrases'] and i < len(beats_info['phrases']):
2794
  phrase = beats_info['phrases'][i]
2795
+ # Safely check if phrase has elements and indices are valid
2796
+ if phrase and len(phrase) > 0 and len(beats_info['beat_times']) > 0:
2797
+ start_beat = min(phrase[0], len(beats_info['beat_times'])-1)
2798
+ end_beat = min(phrase[-1], len(beats_info['beat_times'])-1)
2799
 
2800
  start_time = beats_info['beat_times'][start_beat]
2801
  start_time = float(start_time) if isinstance(start_time, np.ndarray) else start_time
 
2817
  if j < len(beats_info['beat_times']):
2818
  beat_time = beats_info['beat_times'][j]
2819
  beat_time = float(beat_time) if isinstance(beat_time, np.ndarray) else beat_time
2820
+ # Handle edge case where phrase_duration is very small
2821
+ if phrase_duration > 0.001: # Avoid division by very small numbers
2822
+ relative_pos = int((beat_time - start_time) / phrase_duration * syllable_count)
2823
+ else:
2824
+ relative_pos = 0
2825
 
2826
  while len(syllable_viz) <= relative_pos:
2827
  syllable_viz.append("路")