Gamahea commited on
Commit
f773a0f
·
1 Parent(s): 0c7e16c

Deploy Music Generation Studio - 2025-12-12 21:06

Browse files
Files changed (1) hide show
  1. backend/services/export_service.py +46 -2
backend/services/export_service.py CHANGED
@@ -57,8 +57,52 @@ class ExportService:
57
 
58
  audio_data.append(audio)
59
 
60
- # Concatenate all clips
61
- merged_audio = np.concatenate(audio_data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  # Normalize
64
  max_val = np.abs(merged_audio).max()
 
57
 
58
  audio_data.append(audio)
59
 
60
+ # Apply crossfading between clips (2 second overlap)
61
+ crossfade_duration = 2.0 # seconds
62
+ crossfade_samples = int(crossfade_duration * sample_rate)
63
+
64
+ if len(audio_data) == 1:
65
+ # Single clip, no crossfading needed
66
+ merged_audio = audio_data[0]
67
+ else:
68
+ # Start with first clip
69
+ merged_audio = audio_data[0].copy()
70
+
71
+ # Crossfade each subsequent clip
72
+ for i in range(1, len(audio_data)):
73
+ current_clip = audio_data[i]
74
+
75
+ # Calculate overlap region
76
+ overlap_samples = min(crossfade_samples, len(merged_audio), len(current_clip))
77
+
78
+ if overlap_samples > 0:
79
+ # Create fade out curve for end of previous audio
80
+ fade_out = np.linspace(1.0, 0.0, overlap_samples)
81
+ # Create fade in curve for start of current clip
82
+ fade_in = np.linspace(0.0, 1.0, overlap_samples)
83
+
84
+ # Handle stereo vs mono
85
+ if merged_audio.ndim == 2:
86
+ fade_out = fade_out[:, np.newaxis]
87
+ fade_in = fade_in[:, np.newaxis]
88
+
89
+ # Apply crossfade
90
+ merged_audio[-overlap_samples:] = (
91
+ merged_audio[-overlap_samples:] * fade_out +
92
+ current_clip[:overlap_samples] * fade_in
93
+ )
94
+
95
+ # Append the rest of the current clip
96
+ if len(current_clip) > overlap_samples:
97
+ merged_audio = np.concatenate([
98
+ merged_audio,
99
+ current_clip[overlap_samples:]
100
+ ])
101
+
102
+ logger.info(f"Applied {crossfade_duration}s crossfade between clips {i-1} and {i}")
103
+ else:
104
+ # No overlap possible, just concatenate
105
+ merged_audio = np.concatenate([merged_audio, current_clip])
106
 
107
  # Normalize
108
  max_val = np.abs(merged_audio).max()