Update app.py
Browse filesissues with mp3 additions best practices music roll back
app.py
CHANGED
|
@@ -78,14 +78,14 @@ def set_deep_house_prompt():
|
|
| 78 |
# 5) AUDIO PROCESSING FUNCTIONS
|
| 79 |
def apply_chorus(segment):
|
| 80 |
# Enhanced chorus effect for richer sound
|
| 81 |
-
delayed = segment -
|
| 82 |
delayed = delayed.set_frame_rate(segment.frame_rate)
|
| 83 |
return segment.overlay(delayed, position=20)
|
| 84 |
|
| 85 |
def apply_eq(segment):
|
| 86 |
# Adjusted EQ for a more balanced sound
|
| 87 |
-
segment = segment.low_pass_filter(
|
| 88 |
-
segment = segment.high_pass_filter(
|
| 89 |
return segment
|
| 90 |
|
| 91 |
def apply_limiter(segment, max_db=-3.0):
|
|
@@ -99,12 +99,6 @@ def apply_final_gain(segment, target_db=-12.0):
|
|
| 99 |
gain_adjustment = target_db - segment.dBFS
|
| 100 |
return segment + gain_adjustment
|
| 101 |
|
| 102 |
-
def apply_fade(segment, fade_in_duration=2000, fade_out_duration=2000):
|
| 103 |
-
# Apply fade-in and fade-out to the segment (durations in milliseconds)
|
| 104 |
-
segment = segment.fade_in(fade_in_duration)
|
| 105 |
-
segment = segment.fade_out(fade_out_duration)
|
| 106 |
-
return segment
|
| 107 |
-
|
| 108 |
# 6) GENERATION & I/O FUNCTIONS
|
| 109 |
def generate_music(instrumental_prompt: str, cfg_scale: float, top_k: int, top_p: float, temperature: float, total_duration: int, crossfade_duration: int):
|
| 110 |
global musicgen_model
|
|
@@ -168,9 +162,6 @@ def generate_music(instrumental_prompt: str, cfg_scale: float, top_k: int, top_p
|
|
| 168 |
chunk_path = f"chunk_{i}.mp3"
|
| 169 |
torchaudio.save(temp_wav_path, audio_chunk, sample_rate, bits_per_sample=24)
|
| 170 |
segment = AudioSegment.from_wav(temp_wav_path)
|
| 171 |
-
# Apply EQ and limiter to each chunk to maintain consistent dynamics
|
| 172 |
-
segment = apply_eq(segment)
|
| 173 |
-
segment = apply_limiter(segment, max_db=-3.0)
|
| 174 |
segment.export(chunk_path, format="mp3", bitrate="320k")
|
| 175 |
os.remove(temp_wav_path)
|
| 176 |
audio_chunks.append(chunk_path)
|
|
@@ -185,8 +176,7 @@ def generate_music(instrumental_prompt: str, cfg_scale: float, top_k: int, top_p
|
|
| 185 |
final_segment = AudioSegment.from_mp3(audio_chunks[0])
|
| 186 |
for i in range(1, len(audio_chunks)):
|
| 187 |
next_segment = AudioSegment.from_mp3(audio_chunks[i])
|
| 188 |
-
|
| 189 |
-
next_segment = next_segment - 2 # Reduce by 2 dB to soften transitions
|
| 190 |
final_segment = final_segment.append(next_segment, crossfade=crossfade_duration)
|
| 191 |
|
| 192 |
# Trim to exact total duration
|
|
@@ -194,9 +184,9 @@ def generate_music(instrumental_prompt: str, cfg_scale: float, top_k: int, top_p
|
|
| 194 |
|
| 195 |
# Post-process with improved dynamics
|
| 196 |
print("Post-processing final track...")
|
|
|
|
| 197 |
final_segment = apply_chorus(final_segment)
|
| 198 |
final_segment = apply_limiter(final_segment, max_db=-3.0)
|
| 199 |
-
final_segment = apply_fade(final_segment, fade_in_duration=2000, fade_out_duration=2000)
|
| 200 |
final_segment = final_segment.normalize(headroom=-6.0)
|
| 201 |
final_segment = apply_final_gain(final_segment, target_db=-12.0)
|
| 202 |
|
|
|
|
| 78 |
# 5) AUDIO PROCESSING FUNCTIONS
|
| 79 |
def apply_chorus(segment):
|
| 80 |
# Enhanced chorus effect for richer sound
|
| 81 |
+
delayed = segment - 6 # Reduced gain to -6 dB for a subtler effect
|
| 82 |
delayed = delayed.set_frame_rate(segment.frame_rate)
|
| 83 |
return segment.overlay(delayed, position=20)
|
| 84 |
|
| 85 |
def apply_eq(segment):
|
| 86 |
# Adjusted EQ for a more balanced sound
|
| 87 |
+
segment = segment.low_pass_filter(8000) # Raised cutoff to 8kHz for brighter highs
|
| 88 |
+
segment = segment.high_pass_filter(80) # Lowered cutoff to 80Hz for deeper bass
|
| 89 |
return segment
|
| 90 |
|
| 91 |
def apply_limiter(segment, max_db=-3.0):
|
|
|
|
| 99 |
gain_adjustment = target_db - segment.dBFS
|
| 100 |
return segment + gain_adjustment
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
# 6) GENERATION & I/O FUNCTIONS
|
| 103 |
def generate_music(instrumental_prompt: str, cfg_scale: float, top_k: int, top_p: float, temperature: float, total_duration: int, crossfade_duration: int):
|
| 104 |
global musicgen_model
|
|
|
|
| 162 |
chunk_path = f"chunk_{i}.mp3"
|
| 163 |
torchaudio.save(temp_wav_path, audio_chunk, sample_rate, bits_per_sample=24)
|
| 164 |
segment = AudioSegment.from_wav(temp_wav_path)
|
|
|
|
|
|
|
|
|
|
| 165 |
segment.export(chunk_path, format="mp3", bitrate="320k")
|
| 166 |
os.remove(temp_wav_path)
|
| 167 |
audio_chunks.append(chunk_path)
|
|
|
|
| 176 |
final_segment = AudioSegment.from_mp3(audio_chunks[0])
|
| 177 |
for i in range(1, len(audio_chunks)):
|
| 178 |
next_segment = AudioSegment.from_mp3(audio_chunks[i])
|
| 179 |
+
next_segment = next_segment + 1 # Reduced gain boost to +1 dB
|
|
|
|
| 180 |
final_segment = final_segment.append(next_segment, crossfade=crossfade_duration)
|
| 181 |
|
| 182 |
# Trim to exact total duration
|
|
|
|
| 184 |
|
| 185 |
# Post-process with improved dynamics
|
| 186 |
print("Post-processing final track...")
|
| 187 |
+
final_segment = apply_eq(final_segment)
|
| 188 |
final_segment = apply_chorus(final_segment)
|
| 189 |
final_segment = apply_limiter(final_segment, max_db=-3.0)
|
|
|
|
| 190 |
final_segment = final_segment.normalize(headroom=-6.0)
|
| 191 |
final_segment = apply_final_gain(final_segment, target_db=-12.0)
|
| 192 |
|