Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,57 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
**inputs,
|
| 15 |
-
max_new_tokens=max_tokens,
|
| 16 |
-
do_sample=True, # Enables creative sampling
|
| 17 |
-
guidance_scale=3.5, # Higher follows prompt strictly (3.0-4.5 is best)
|
| 18 |
-
top_k=250, # Filters out "noisy" notes
|
| 19 |
-
temperature=1.0 # Controls creativity (1.0 is standard)
|
| 20 |
-
)
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
|
| 25 |
-
|
| 26 |
-
mastered_file_path = apply_audacity_fixes(sampling_rate, audio_data, bass_boost_db, fade_sec)
|
| 27 |
-
|
| 28 |
-
license_cert = (
|
| 29 |
-
f"✅ PREMIUM STUDIO MASTER\n"
|
| 30 |
-
f"AI Engine: MusicGen-Small (High-Guidance Mode)\n"
|
| 31 |
-
f"Bass: +{bass_boost_db}dB | Fade: {fade_sec}s"
|
| 32 |
-
)
|
| 33 |
-
|
| 34 |
-
return mastered_file_path, license_cert
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import numpy as np
|
| 4 |
+
from pydub import AudioSegment, effects
|
| 5 |
+
from pedalboard import Pedalboard, Compressor, Gain, HighpassFilter, LowShelfFilter
|
| 6 |
+
from pedalboard.io import AudioFile
|
| 7 |
+
import scipy.io.wavfile as wavfile
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
# ... (Previous Model Loading Code) ...
|
| 11 |
+
|
| 12 |
+
def master_audio(input_wav_path, bass_boost_db):
|
| 13 |
+
"""
|
| 14 |
+
Applies professional mastering effects similar to Audacity/Logic Pro.
|
| 15 |
+
"""
|
| 16 |
+
# 1. Load the generated audio
|
| 17 |
+
with AudioFile(input_wav_path) as f:
|
| 18 |
+
audio_data = f.read(f.frames)
|
| 19 |
+
sample_rate = f.sample_rate
|
| 20 |
+
|
| 21 |
+
# 2. Define the "Audacity Style" Mastering Chain
|
| 22 |
+
board = Pedalboard([
|
| 23 |
+
# Remove low-end rumble
|
| 24 |
+
HighpassFilter(cutoff_frequency_hz=30),
|
| 25 |
+
|
| 26 |
+
# Dynamic Bass Boost
|
| 27 |
+
LowShelfFilter(cutoff_frequency_hz=150, gain_db=bass_boost_db),
|
| 28 |
+
|
| 29 |
+
# Professional Compressor (Glues the song together)
|
| 30 |
+
Compressor(threshold_db=-12, ratio=4, attack_ms=5, release_ms=50),
|
| 31 |
+
|
| 32 |
+
# Final Volume Gain
|
| 33 |
+
Gain(gain_db=2)
|
| 34 |
+
])
|
| 35 |
+
|
| 36 |
+
# 3. Process the audio
|
| 37 |
+
mastered_audio = board(audio_data, sample_rate)
|
| 38 |
+
|
| 39 |
+
# 4. Save the Final Pro Master
|
| 40 |
+
final_output = "master_v1.mp3"
|
| 41 |
+
with AudioFile(final_output, 'w', sample_rate, mastered_audio.shape[0]) as f:
|
| 42 |
+
f.write(mastered_audio)
|
| 43 |
|
| 44 |
+
return final_output
|
| 45 |
+
|
| 46 |
+
def generate_music(prompt, duration, instruments, energy, bass_boost_db, fade_sec):
|
| 47 |
+
# (The AI generation code from our previous step goes here)
|
| 48 |
+
# ...
|
| 49 |
|
| 50 |
+
# After the AI generates the raw wav, we "Master" it:
|
| 51 |
+
temp_raw = "raw_output.wav"
|
| 52 |
+
wavfile.write(temp_raw, sampling_rate, (audio_data * 32767).astype(np.int16))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
# Call the new mastering function
|
| 55 |
+
final_file = master_audio(temp_raw, bass_boost_db)
|
| 56 |
|
| 57 |
+
return final_file, "🚀 MASTERED: 320kbps High Quality"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|