Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,290 +7,322 @@ import librosa
|
|
| 7 |
import soundfile as sf
|
| 8 |
from pathlib import Path
|
| 9 |
|
| 10 |
-
class
|
| 11 |
def __init__(self):
|
| 12 |
-
self.
|
| 13 |
-
"
|
| 14 |
-
{"name": "
|
| 15 |
-
{"name": "
|
| 16 |
-
{"name": "
|
| 17 |
-
{"name": "
|
| 18 |
],
|
| 19 |
-
"
|
| 20 |
-
{"name": "
|
| 21 |
-
{"name": "
|
| 22 |
-
{"name": "
|
|
|
|
| 23 |
],
|
| 24 |
-
"
|
| 25 |
-
{"name": "
|
| 26 |
-
{"name": "
|
|
|
|
|
|
|
| 27 |
],
|
| 28 |
-
"
|
| 29 |
-
{"
|
|
|
|
|
|
|
|
|
|
| 30 |
]
|
| 31 |
}
|
| 32 |
|
| 33 |
-
def
|
| 34 |
-
"""Convert MP3 to
|
| 35 |
try:
|
|
|
|
| 36 |
y, sr = librosa.load(audio_path, sr=22050, mono=True)
|
| 37 |
-
wav_path = tempfile.mktemp(suffix='.wav')
|
| 38 |
-
sf.write(wav_path, y, sr)
|
| 39 |
-
return wav_path, sr
|
| 40 |
-
except Exception as e:
|
| 41 |
-
raise Exception(f"Audio conversion failed: {str(e)}")
|
| 42 |
-
|
| 43 |
-
def extract_melody_to_midi(self, audio_path, style="melodic"):
|
| 44 |
-
"""Convert audio to MIDI with style-based extraction"""
|
| 45 |
-
try:
|
| 46 |
-
wav_path, sr = self.convert_to_wav(audio_path)
|
| 47 |
-
y, sr = librosa.load(wav_path, sr=sr)
|
| 48 |
|
|
|
|
| 49 |
midi = pretty_midi.PrettyMIDI()
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
instruments = self.create_instruments(["melody", "chords", "bass", "drums"])
|
| 60 |
-
|
| 61 |
-
# Extract melody and chords
|
| 62 |
-
melody_notes, chord_notes = self.analyze_audio(y, sr, style)
|
| 63 |
-
|
| 64 |
-
# Assign notes to instruments
|
| 65 |
-
if melody_notes and instruments.get('melody'):
|
| 66 |
-
instruments['melody'].notes.extend(melody_notes[:20]) # Limit notes
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
|
| 71 |
-
#
|
| 72 |
-
|
| 73 |
-
self.add_basic_drums(instruments['drums'], y, sr)
|
| 74 |
|
| 75 |
-
#
|
| 76 |
-
|
| 77 |
-
if instrument.notes:
|
| 78 |
-
midi.instruments.append(instrument)
|
| 79 |
|
| 80 |
-
return
|
| 81 |
|
| 82 |
except Exception as e:
|
| 83 |
-
raise Exception(f"
|
| 84 |
-
|
| 85 |
-
def create_instruments(self, types):
|
| 86 |
-
"""Create instrument objects based on types"""
|
| 87 |
-
instruments = {}
|
| 88 |
-
for inst_type in types:
|
| 89 |
-
if inst_type in self.instrument_map:
|
| 90 |
-
# Pick a random instrument from the category
|
| 91 |
-
import random
|
| 92 |
-
inst_info = random.choice(self.instrument_map[inst_type])
|
| 93 |
-
instrument = pretty_midi.Instrument(
|
| 94 |
-
program=inst_info['program'],
|
| 95 |
-
is_drum=inst_info.get('is_drum', False),
|
| 96 |
-
name=inst_info['name']
|
| 97 |
-
)
|
| 98 |
-
instruments[inst_type] = instrument
|
| 99 |
-
return instruments
|
| 100 |
|
| 101 |
-
def
|
| 102 |
-
"""Extract
|
| 103 |
-
|
| 104 |
-
|
|
|
|
| 105 |
|
| 106 |
-
# Detect
|
| 107 |
-
|
| 108 |
-
|
|
|
|
| 109 |
|
| 110 |
-
|
| 111 |
-
for i, onset_time in enumerate(onset_times[:30]): # Limit to 30 notes
|
| 112 |
-
start_idx = int(onset_time * sr)
|
| 113 |
-
end_idx = min(start_idx + int(0.4 * sr), len(y))
|
| 114 |
-
|
| 115 |
-
if end_idx > start_idx:
|
| 116 |
-
segment = y[start_idx:end_idx]
|
| 117 |
-
freq = self.detect_pitch(segment, sr)
|
| 118 |
-
|
| 119 |
-
if 100 < freq < 800: # Reasonable pitch range
|
| 120 |
-
midi_note = int(69 + 12 * np.log2(freq / 440.0))
|
| 121 |
-
|
| 122 |
-
if 48 <= midi_note <= 84: # Good MIDI range
|
| 123 |
-
velocity = np.random.randint(70, 100)
|
| 124 |
-
|
| 125 |
-
note = pretty_midi.Note(
|
| 126 |
-
velocity=velocity,
|
| 127 |
-
pitch=midi_note,
|
| 128 |
-
start=onset_time,
|
| 129 |
-
end=onset_time + 0.5
|
| 130 |
-
)
|
| 131 |
-
|
| 132 |
-
# Separate melody and chords
|
| 133 |
-
if i % 3 == 0: # Every 3rd note as melody
|
| 134 |
-
melody_notes.append(note)
|
| 135 |
-
else: # Others as chords
|
| 136 |
-
chord_notes.append(note)
|
| 137 |
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
-
def
|
| 141 |
-
"""
|
| 142 |
-
|
| 143 |
-
#
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
-
#
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
pass
|
| 155 |
-
return 440 # Default to A4
|
| 156 |
|
| 157 |
-
def
|
| 158 |
-
"""Add simple
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
beat_times = librosa.frames_to_time(beats, sr=sr)
|
| 162 |
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
#
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
note = pretty_midi.Note(
|
| 168 |
-
velocity=
|
|
|
|
|
|
|
|
|
|
| 169 |
)
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
-
|
| 173 |
-
if i % 4 in [1, 3]:
|
| 174 |
note = pretty_midi.Note(
|
| 175 |
-
velocity=
|
|
|
|
|
|
|
|
|
|
| 176 |
)
|
| 177 |
-
|
| 178 |
-
except:
|
| 179 |
-
pass
|
| 180 |
|
| 181 |
-
def
|
| 182 |
-
"""Add human feel to
|
| 183 |
-
for instrument in
|
| 184 |
for note in instrument.notes:
|
| 185 |
# Humanize timing
|
| 186 |
-
timing_shift = np.random.normal(0, 0.
|
| 187 |
note.start = max(0, note.start + timing_shift)
|
| 188 |
|
| 189 |
# Humanize velocity
|
| 190 |
-
vel_shift = np.random.randint(-
|
| 191 |
-
note.velocity = max(40, min(127, note.velocity + vel_shift))
|
| 192 |
-
|
| 193 |
-
|
|
|
|
|
|
|
|
|
|
| 194 |
|
| 195 |
-
def
|
| 196 |
-
"""
|
| 197 |
if not files:
|
| 198 |
-
return None,
|
| 199 |
|
| 200 |
-
converter =
|
| 201 |
-
|
| 202 |
|
| 203 |
for file in files:
|
| 204 |
try:
|
| 205 |
-
#
|
| 206 |
-
|
| 207 |
|
| 208 |
-
#
|
| 209 |
-
|
|
|
|
| 210 |
|
| 211 |
-
|
| 212 |
-
midi_path = tempfile.mktemp(suffix='_music.mid')
|
| 213 |
-
humanized_midi.write(midi_path)
|
| 214 |
-
|
| 215 |
-
# Create audio preview with better sounds
|
| 216 |
-
audio_path = tempfile.mktemp(suffix='_preview.wav')
|
| 217 |
-
audio_data = humanized_midi.synthesize()
|
| 218 |
-
sf.write(audio_path, audio_data, 44100)
|
| 219 |
-
|
| 220 |
-
processed_files.append((midi_path, audio_path))
|
| 221 |
|
| 222 |
except Exception as e:
|
| 223 |
-
return None,
|
| 224 |
|
| 225 |
-
if
|
| 226 |
-
|
| 227 |
-
midi_files = [f[0] for f in processed_files]
|
| 228 |
-
audio_preview = processed_files[0][1]
|
| 229 |
-
return midi_files, audio_preview, f"β
Created {len(processed_files)} tracks with better sounds!"
|
| 230 |
else:
|
| 231 |
-
return None,
|
| 232 |
|
| 233 |
-
# Simple interface
|
| 234 |
-
with gr.Blocks(theme=gr.themes.Soft(), title="MP3
|
| 235 |
gr.Markdown("""
|
| 236 |
-
# π΅ MP3
|
| 237 |
-
**Convert your
|
| 238 |
|
| 239 |
-
Upload MP3 β Get back
|
| 240 |
""")
|
| 241 |
|
| 242 |
with gr.Row():
|
| 243 |
with gr.Column():
|
|
|
|
| 244 |
file_input = gr.File(
|
| 245 |
file_count="multiple",
|
| 246 |
file_types=[".mp3", ".wav", ".m4a"],
|
| 247 |
-
label="Upload your
|
| 248 |
)
|
| 249 |
|
| 250 |
style = gr.Radio(
|
| 251 |
-
["
|
| 252 |
-
value="
|
| 253 |
-
label="Music Style",
|
| 254 |
-
info="Choose the
|
| 255 |
)
|
| 256 |
|
| 257 |
-
intensity = gr.Slider(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
|
| 259 |
-
process_btn = gr.Button("
|
| 260 |
|
| 261 |
with gr.Column():
|
|
|
|
| 262 |
file_output = gr.File(
|
| 263 |
file_count="multiple",
|
| 264 |
-
label="Download
|
| 265 |
)
|
| 266 |
|
| 267 |
audio_output = gr.Audio(
|
| 268 |
-
label="
|
| 269 |
type="filepath"
|
| 270 |
)
|
| 271 |
|
| 272 |
status = gr.Textbox(label="Status")
|
| 273 |
|
| 274 |
-
with gr.Accordion("πΈ What
|
| 275 |
gr.Markdown("""
|
| 276 |
-
**Instead of
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
|
| 283 |
-
**
|
| 284 |
-
- **Melodic**: Focus on leads and strings
|
| 285 |
-
- **Electronic**: Synths and electronic drums
|
| 286 |
-
- **Acoustic**: Guitars and real instruments
|
| 287 |
-
- **Balanced**: Mix of everything
|
| 288 |
""")
|
| 289 |
|
| 290 |
process_btn.click(
|
| 291 |
-
fn=
|
| 292 |
inputs=[file_input, style, intensity],
|
| 293 |
-
outputs=[file_output,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 294 |
)
|
| 295 |
|
| 296 |
if __name__ == "__main__":
|
|
|
|
| 7 |
import soundfile as sf
|
| 8 |
from pathlib import Path
|
| 9 |
|
| 10 |
+
class MP3ToHumanizedMP3:
|
| 11 |
def __init__(self):
|
| 12 |
+
self.instrument_sets = {
|
| 13 |
+
"pop": [
|
| 14 |
+
{"program": 0, "name": "Drums", "is_drum": True, "volume": 0.8},
|
| 15 |
+
{"program": 33, "name": "Bass Guitar", "is_drum": False, "volume": 0.7},
|
| 16 |
+
{"program": 25, "name": "Acoustic Guitar", "is_drum": False, "volume": 0.6},
|
| 17 |
+
{"program": 1, "name": "Piano", "is_drum": False, "volume": 0.5},
|
| 18 |
],
|
| 19 |
+
"electronic": [
|
| 20 |
+
{"program": 0, "name": "Drums", "is_drum": True, "volume": 0.9},
|
| 21 |
+
{"program": 39, "name": "Synth Bass", "is_drum": False, "volume": 0.8},
|
| 22 |
+
{"program": 81, "name": "Lead Synth", "is_drum": False, "volume": 0.7},
|
| 23 |
+
{"program": 89, "name": "Pad", "is_drum": False, "volume": 0.4},
|
| 24 |
],
|
| 25 |
+
"rock": [
|
| 26 |
+
{"program": 0, "name": "Drums", "is_drum": True, "volume": 0.9},
|
| 27 |
+
{"program": 33, "name": "Bass", "is_drum": False, "volume": 0.7},
|
| 28 |
+
{"program": 30, "name": "Distortion Guitar", "is_drum": False, "volume": 0.8},
|
| 29 |
+
{"program": 27, "name": "Clean Guitar", "is_drum": False, "volume": 0.6},
|
| 30 |
],
|
| 31 |
+
"cinematic": [
|
| 32 |
+
{"program": 0, "name": "Drums", "is_drum": True, "volume": 0.6},
|
| 33 |
+
{"program": 48, "name": "String Ensemble", "is_drum": False, "volume": 0.8},
|
| 34 |
+
{"program": 61, "name": "French Horn", "is_drum": False, "volume": 0.7},
|
| 35 |
+
{"program": 5, "name": "Electric Piano", "is_drum": False, "volume": 0.5},
|
| 36 |
]
|
| 37 |
}
|
| 38 |
|
| 39 |
+
def create_full_song(self, audio_path, style="pop", intensity=0.7):
|
| 40 |
+
"""Convert MP3 to a complete humanized song"""
|
| 41 |
try:
|
| 42 |
+
# Load and analyze original audio
|
| 43 |
y, sr = librosa.load(audio_path, sr=22050, mono=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
# Create MIDI structure
|
| 46 |
midi = pretty_midi.PrettyMIDI()
|
| 47 |
|
| 48 |
+
# Add instruments based on style
|
| 49 |
+
for inst_info in self.instrument_sets[style]:
|
| 50 |
+
instrument = pretty_midi.Instrument(
|
| 51 |
+
program=inst_info["program"],
|
| 52 |
+
is_drum=inst_info["is_drum"],
|
| 53 |
+
name=inst_info["name"]
|
| 54 |
+
)
|
| 55 |
+
midi.instruments.append(instrument)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
# Extract musical elements from audio
|
| 58 |
+
self.extract_music_to_instruments(midi, y, sr, style, intensity)
|
| 59 |
|
| 60 |
+
# Humanize the performance
|
| 61 |
+
self.humanize_performance(midi, intensity)
|
|
|
|
| 62 |
|
| 63 |
+
# Synthesize to audio
|
| 64 |
+
audio_data = midi.synthesize()
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
return audio_data, sr
|
| 67 |
|
| 68 |
except Exception as e:
|
| 69 |
+
raise Exception(f"Song creation failed: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
def extract_music_to_instruments(self, midi, y, sr, style, intensity):
|
| 72 |
+
"""Extract different musical parts and assign to instruments"""
|
| 73 |
+
# Get tempo and beats
|
| 74 |
+
tempo, beat_frames = librosa.beat.beat_track(y=y, sr=sr)
|
| 75 |
+
beat_times = librosa.frames_to_time(beat_frames, sr=sr)
|
| 76 |
|
| 77 |
+
# Detect melody/pitch content
|
| 78 |
+
f0, voiced_flag, voiced_probs = librosa.pyin(
|
| 79 |
+
y, fmin=librosa.note_to_hz('C2'), fmax=librosa.note_to_hz('C6'), sr=sr
|
| 80 |
+
)
|
| 81 |
|
| 82 |
+
times = librosa.times_like(f0, sr=sr)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
# Assign notes to instruments based on style
|
| 85 |
+
instruments = midi.instruments
|
| 86 |
+
|
| 87 |
+
# Add drum pattern
|
| 88 |
+
drum_instrument = next((inst for inst in instruments if inst.is_drum), None)
|
| 89 |
+
if drum_instrument and len(beat_times) > 0:
|
| 90 |
+
self.add_drum_pattern(drum_instrument, beat_times, style)
|
| 91 |
+
|
| 92 |
+
# Add bass line to bass instrument
|
| 93 |
+
bass_instrument = next((inst for inst in instruments if not inst.is_drum and 32 <= inst.program <= 39), None)
|
| 94 |
+
if bass_instrument and len(beat_times) > 0:
|
| 95 |
+
self.add_bass_line(bass_instrument, beat_times, f0, times, voiced_flag)
|
| 96 |
+
|
| 97 |
+
# Add melody to lead instrument
|
| 98 |
+
lead_instrument = next((inst for inst in instruments if not inst.is_drum and inst.program not in range(32, 40)), None)
|
| 99 |
+
if lead_instrument:
|
| 100 |
+
self.add_melody(lead_instrument, f0, times, voiced_flag, intensity)
|
| 101 |
+
|
| 102 |
+
# Add chords/pads to remaining instruments
|
| 103 |
+
other_instruments = [inst for inst in instruments if not inst.is_drum and inst != bass_instrument and inst != lead_instrument]
|
| 104 |
+
for inst in other_instruments:
|
| 105 |
+
self.add_harmony(inst, beat_times, f0, times, style)
|
| 106 |
|
| 107 |
+
def add_drum_pattern(self, drums, beat_times, style):
|
| 108 |
+
"""Add style-appropriate drum pattern"""
|
| 109 |
+
for i, beat_time in enumerate(beat_times[:32]): # First 32 beats
|
| 110 |
+
# Kick on strong beats
|
| 111 |
+
if i % 4 == 0:
|
| 112 |
+
note = pretty_midi.Note(
|
| 113 |
+
velocity=90, pitch=36, start=beat_time, end=beat_time + 0.3
|
| 114 |
+
)
|
| 115 |
+
drums.notes.append(note)
|
| 116 |
+
|
| 117 |
+
# Snare on beats 2 and 4
|
| 118 |
+
if i % 4 in [2]:
|
| 119 |
+
note = pretty_midi.Note(
|
| 120 |
+
velocity=80, pitch=38, start=beat_time, end=beat_time + 0.2
|
| 121 |
+
)
|
| 122 |
+
drums.notes.append(note)
|
| 123 |
|
| 124 |
+
# Hi-hats
|
| 125 |
+
if style in ["electronic", "pop"]:
|
| 126 |
+
note = pretty_midi.Note(
|
| 127 |
+
velocity=70, pitch=42, start=beat_time, end=beat_time + 0.1
|
| 128 |
+
)
|
| 129 |
+
drums.notes.append(note)
|
|
|
|
|
|
|
| 130 |
|
| 131 |
+
def add_bass_line(self, bass, beat_times, f0, times, voiced_flag):
|
| 132 |
+
"""Add simple bass line"""
|
| 133 |
+
if len(f0) == 0:
|
| 134 |
+
return
|
|
|
|
| 135 |
|
| 136 |
+
for i, beat_time in enumerate(beat_times[:16]):
|
| 137 |
+
if i % 2 == 0: # Every other beat
|
| 138 |
+
# Find a pitch around this time
|
| 139 |
+
time_idx = min(int(beat_time * 100), len(f0) - 1)
|
| 140 |
+
if voiced_flag[time_idx] and not np.isnan(f0[time_idx]):
|
| 141 |
+
midi_note = int(69 + 12 * np.log2(f0[time_idx] / 440.0))
|
| 142 |
+
# Put in bass range
|
| 143 |
+
bass_note = max(36, min(55, midi_note - 12))
|
| 144 |
+
|
| 145 |
note = pretty_midi.Note(
|
| 146 |
+
velocity=80,
|
| 147 |
+
pitch=bass_note,
|
| 148 |
+
start=beat_time,
|
| 149 |
+
end=beat_time + 0.8
|
| 150 |
)
|
| 151 |
+
bass.notes.append(note)
|
| 152 |
+
|
| 153 |
+
def add_melody(self, lead, f0, times, voiced_flag, intensity):
|
| 154 |
+
"""Extract and add melody"""
|
| 155 |
+
if len(f0) == 0:
|
| 156 |
+
return
|
| 157 |
+
|
| 158 |
+
note_start = None
|
| 159 |
+
current_pitch = None
|
| 160 |
+
|
| 161 |
+
for i, (time, freq, voiced) in enumerate(zip(times, f0, voiced_flag)):
|
| 162 |
+
if voiced and not np.isnan(freq):
|
| 163 |
+
midi_note = int(69 + 12 * np.log2(freq / 440.0))
|
| 164 |
+
if 60 <= midi_note <= 84: # Good melody range
|
| 165 |
+
if current_pitch != midi_note:
|
| 166 |
+
if current_pitch is not None and note_start is not None:
|
| 167 |
+
# End previous note
|
| 168 |
+
note = pretty_midi.Note(
|
| 169 |
+
velocity=np.random.randint(70, 90),
|
| 170 |
+
pitch=current_pitch,
|
| 171 |
+
start=note_start,
|
| 172 |
+
end=time
|
| 173 |
+
)
|
| 174 |
+
lead.notes.append(note)
|
| 175 |
+
|
| 176 |
+
# Start new note
|
| 177 |
+
current_pitch = midi_note
|
| 178 |
+
note_start = time
|
| 179 |
+
else:
|
| 180 |
+
if current_pitch is not None and note_start is not None:
|
| 181 |
+
# End current note
|
| 182 |
+
note = pretty_midi.Note(
|
| 183 |
+
velocity=np.random.randint(70, 90),
|
| 184 |
+
pitch=current_pitch,
|
| 185 |
+
start=note_start,
|
| 186 |
+
end=time
|
| 187 |
+
)
|
| 188 |
+
lead.notes.append(note)
|
| 189 |
+
current_pitch = None
|
| 190 |
+
note_start = None
|
| 191 |
+
|
| 192 |
+
def add_harmony(self, instrument, beat_times, f0, times, style):
|
| 193 |
+
"""Add chordal harmony"""
|
| 194 |
+
for i, beat_time in enumerate(beat_times[:8]):
|
| 195 |
+
if i % 2 == 0: # Every 2 beats
|
| 196 |
+
# Simple chord based on style
|
| 197 |
+
if style == "pop":
|
| 198 |
+
chord_notes = [60, 64, 67] # C Major
|
| 199 |
+
elif style == "electronic":
|
| 200 |
+
chord_notes = [65, 69, 72] # F Major
|
| 201 |
+
elif style == "rock":
|
| 202 |
+
chord_notes = [59, 62, 65] # B Minor
|
| 203 |
+
else:
|
| 204 |
+
chord_notes = [60, 64, 67] # C Major
|
| 205 |
|
| 206 |
+
for note_pitch in chord_notes:
|
|
|
|
| 207 |
note = pretty_midi.Note(
|
| 208 |
+
velocity=60,
|
| 209 |
+
pitch=note_pitch,
|
| 210 |
+
start=beat_time,
|
| 211 |
+
end=beat_time + 1.0
|
| 212 |
)
|
| 213 |
+
instrument.notes.append(note)
|
|
|
|
|
|
|
| 214 |
|
| 215 |
+
def humanize_performance(self, midi, intensity):
|
| 216 |
+
"""Add human feel to all instruments"""
|
| 217 |
+
for instrument in midi.instruments:
|
| 218 |
for note in instrument.notes:
|
| 219 |
# Humanize timing
|
| 220 |
+
timing_shift = np.random.normal(0, 0.02 * intensity)
|
| 221 |
note.start = max(0, note.start + timing_shift)
|
| 222 |
|
| 223 |
# Humanize velocity
|
| 224 |
+
vel_shift = np.random.randint(-15, 15)
|
| 225 |
+
note.velocity = max(40, min(127, note.velocity + int(vel_shift * intensity)))
|
| 226 |
+
|
| 227 |
+
# Humanize duration (except drums)
|
| 228 |
+
if not instrument.is_drum:
|
| 229 |
+
duration_shift = np.random.normal(0, 0.1 * intensity)
|
| 230 |
+
note.end = max(note.start + 0.1, note.end + duration_shift)
|
| 231 |
|
| 232 |
+
def process_to_mp3(files, style, intensity):
|
| 233 |
+
"""Process audio files and return MP3 results"""
|
| 234 |
if not files:
|
| 235 |
+
return None, "Please upload audio files"
|
| 236 |
|
| 237 |
+
converter = MP3ToHumanizedMP3()
|
| 238 |
+
output_files = []
|
| 239 |
|
| 240 |
for file in files:
|
| 241 |
try:
|
| 242 |
+
# Create full humanized song
|
| 243 |
+
audio_data, sr = converter.create_full_song(file.name, style, intensity)
|
| 244 |
|
| 245 |
+
# Save as MP3
|
| 246 |
+
mp3_path = tempfile.mktemp(suffix='_humanized.mp3')
|
| 247 |
+
sf.write(mp3_path, audio_data, sr)
|
| 248 |
|
| 249 |
+
output_files.append(mp3_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
|
| 251 |
except Exception as e:
|
| 252 |
+
return None, f"Error processing {file.name}: {str(e)}"
|
| 253 |
|
| 254 |
+
if output_files:
|
| 255 |
+
return output_files, f"β
Created {len(output_files)} humanized songs!"
|
|
|
|
|
|
|
|
|
|
| 256 |
else:
|
| 257 |
+
return None, "β No files were processed successfully"
|
| 258 |
|
| 259 |
+
# Simple interface focused on MP3 output
|
| 260 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="MP3 Humanizer") as demo:
|
| 261 |
gr.Markdown("""
|
| 262 |
+
# π΅ MP3 Humanizer
|
| 263 |
+
**Convert your AI music to human-sounding MP3 songs!**
|
| 264 |
|
| 265 |
+
Upload MP3 β Get back humanized MP3 with full instrumentation
|
| 266 |
""")
|
| 267 |
|
| 268 |
with gr.Row():
|
| 269 |
with gr.Column():
|
| 270 |
+
gr.Markdown("### π Upload Your AI Music")
|
| 271 |
file_input = gr.File(
|
| 272 |
file_count="multiple",
|
| 273 |
file_types=[".mp3", ".wav", ".m4a"],
|
| 274 |
+
label="Upload your AI-generated music"
|
| 275 |
)
|
| 276 |
|
| 277 |
style = gr.Radio(
|
| 278 |
+
["pop", "electronic", "rock", "cinematic"],
|
| 279 |
+
value="pop",
|
| 280 |
+
label="π΅ Music Style",
|
| 281 |
+
info="Choose the style for your humanized song"
|
| 282 |
)
|
| 283 |
|
| 284 |
+
intensity = gr.Slider(
|
| 285 |
+
0.1, 1.0, value=0.7,
|
| 286 |
+
label="ποΈ Humanization Intensity",
|
| 287 |
+
info="How much human feel to add"
|
| 288 |
+
)
|
| 289 |
|
| 290 |
+
process_btn = gr.Button("β¨ Create Humanized MP3", variant="primary")
|
| 291 |
|
| 292 |
with gr.Column():
|
| 293 |
+
gr.Markdown("### π₯ Download Humanized Songs")
|
| 294 |
file_output = gr.File(
|
| 295 |
file_count="multiple",
|
| 296 |
+
label="Download Your Humanized MP3 Files"
|
| 297 |
)
|
| 298 |
|
| 299 |
audio_output = gr.Audio(
|
| 300 |
+
label="Preview Your Humanized Song",
|
| 301 |
type="filepath"
|
| 302 |
)
|
| 303 |
|
| 304 |
status = gr.Textbox(label="Status")
|
| 305 |
|
| 306 |
+
with gr.Accordion("πΈ What You'll Get", open=True):
|
| 307 |
gr.Markdown("""
|
| 308 |
+
**Instead of just piano, you get full songs with:**
|
| 309 |
+
|
| 310 |
+
**Pop Style:** Drums, Bass Guitar, Acoustic Guitar, Piano
|
| 311 |
+
**Electronic:** Electronic Drums, Synth Bass, Lead Synth, Pads
|
| 312 |
+
**Rock:** Drums, Bass, Electric Guitars
|
| 313 |
+
**Cinematic:** Orchestral Drums, Strings, Horns, Piano
|
| 314 |
|
| 315 |
+
**Output:** Complete MP3 files ready to use!
|
|
|
|
|
|
|
|
|
|
|
|
|
| 316 |
""")
|
| 317 |
|
| 318 |
process_btn.click(
|
| 319 |
+
fn=process_to_mp3,
|
| 320 |
inputs=[file_input, style, intensity],
|
| 321 |
+
outputs=[file_output, status]
|
| 322 |
+
).then(
|
| 323 |
+
lambda files: files[0] if files else None,
|
| 324 |
+
inputs=[file_output],
|
| 325 |
+
outputs=[audio_output]
|
| 326 |
)
|
| 327 |
|
| 328 |
if __name__ == "__main__":
|