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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
File-to-Music generation interface
|
| 3 |
+
"""
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import numpy as np
|
| 6 |
+
from typing import Optional
|
| 7 |
+
import logging
|
| 8 |
+
|
| 9 |
+
from models.model_manager import ModelManager
|
| 10 |
+
from utils.ui_components import UIComponents
|
| 11 |
+
from utils.audio_processor import AudioProcessor
|
| 12 |
+
from utils.file_handler import FileHandler
|
| 13 |
+
|
| 14 |
+
logger = logging.getLogger(__name__)
|
| 15 |
+
|
| 16 |
+
class FileToMusicInterface:
|
| 17 |
+
def __init__(self, model_manager: ModelManager):
|
| 18 |
+
self.model_manager = model_manager
|
| 19 |
+
self.audio_processor = AudioProcessor()
|
| 20 |
+
self.file_handler = FileHandler()
|
| 21 |
+
|
| 22 |
+
def create_interface(self) -> gr.Interface:
|
| 23 |
+
"""Create the file-to-music interface"""
|
| 24 |
+
with gr.Group():
|
| 25 |
+
gr.Markdown("## 🎹 File-to-Music Generation")
|
| 26 |
+
gr.Markdown("Upload an audio file to use as inspiration or conditioning")
|
| 27 |
+
|
| 28 |
+
with gr.Row():
|
| 29 |
+
with gr.Column(scale=2):
|
| 30 |
+
file_input = gr.Audio(
|
| 31 |
+
label="Upload Audio File",
|
| 32 |
+
type="filepath",
|
| 33 |
+
source="upload"
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
with gr.Row():
|
| 37 |
+
model_dropdown = UIComponents.create_model_dropdown()
|
| 38 |
+
style_dropdown = gr.Dropdown(
|
| 39 |
+
choices=[
|
| 40 |
+
("Similar Style", "similar"),
|
| 41 |
+
("Different Style", "different"),
|
| 42 |
+
("Enhanced", "enhanced"),
|
| 43 |
+
("Remix", "remix")
|
| 44 |
+
],
|
| 45 |
+
value="similar",
|
| 46 |
+
label="Processing Style"
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
with gr.Row():
|
| 50 |
+
duration_slider = UIComponents.create_duration_slider()
|
| 51 |
+
guidance_slider = UIComponents.create_guidance_slider()
|
| 52 |
+
|
| 53 |
+
# Conditioning options
|
| 54 |
+
with gr.Accordion("Conditioning Options", open=True):
|
| 55 |
+
conditioning_strength = gr.Slider(
|
| 56 |
+
minimum=0.1,
|
| 57 |
+
maximum=1.0,
|
| 58 |
+
value=0.7,
|
| 59 |
+
label="Conditioning Strength"
|
| 60 |
+
)
|
| 61 |
+
pitch_shift = gr.Slider(
|
| 62 |
+
minimum=-12,
|
| 63 |
+
maximum=12,
|
| 64 |
+
value=0,
|
| 65 |
+
step=1,
|
| 66 |
+
label="Pitch Shift (semitones)"
|
| 67 |
+
)
|
| 68 |
+
tempo_change = gr.Slider(
|
| 69 |
+
minimum=0.5,
|
| 70 |
+
maximum=2.0,
|
| 71 |
+
value=1.0,
|
| 72 |
+
step=0.1,
|
| 73 |
+
label="Tempo Multiplier"
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
with gr.Row():
|
| 77 |
+
generate_btn = gr.Button("🎵 Generate Music", variant="primary", scale=2)
|
| 78 |
+
analyze_btn = gr.Button("🔍 Analyze Audio", variant="secondary")
|
| 79 |
+
|
| 80 |
+
with gr.Column(scale=3):
|
| 81 |
+
audio_output = UIComponents.create_audio_player("Generated Music")
|
| 82 |
+
original_player = UIComponents.create_audio_player("Original Audio")
|
| 83 |
+
|
| 84 |
+
with gr.Row():
|
| 85 |
+
download_btn = gr.DownloadButton("💾 Download", variant="secondary")
|
| 86 |
+
compare_btn = gr.Button("⚖️ Compare", variant="secondary")
|
| 87 |
+
|
| 88 |
+
# Analysis results
|
| 89 |
+
with gr.Accordion("Audio Analysis", open=False):
|
| 90 |
+
with gr.Row():
|
| 91 |
+
tempo_text = gr.Textbox(label="Tempo (BPM)", interactive=False)
|
| 92 |
+
key_text = gr.Textbox(label="Estimated Key", interactive=False)
|
| 93 |
+
with gr.Row():
|
| 94 |
+
energy_text = gr.Textbox(label="Energy Level", interactive=False)
|
| 95 |
+
mood_text = gr.Textbox(label="Mood", interactive=False)
|
| 96 |
+
|
| 97 |
+
# Comparison tab
|
| 98 |
+
with gr.Tab("Compare Original vs Generated"):
|
| 99 |
+
with gr.Row():
|
| 100 |
+
original_plot = gr.Plot(label="Original Waveform")
|
| 101 |
+
generated_plot = gr.Plot(label="Generated Waveform")
|
| 102 |
+
|
| 103 |
+
with gr.Row():
|
| 104 |
+
original_spec = gr.Plot(label="Original Spectrogram")
|
| 105 |
+
generated_spec = gr.Plot(label="Generated Spectrogram")
|
| 106 |
+
|
| 107 |
+
# Examples
|
| 108 |
+
gr.Examples(
|
| 109 |
+
examples=[
|
| 110 |
+
["demo_files/example1.wav"],
|
| 111 |
+
["demo_files/example2.wav"],
|
| 112 |
+
["demo_files/example3.wav"]
|
| 113 |
+
],
|
| 114 |
+
inputs=file_input,
|
| 115 |
+
label="Example Audio Files"
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
# Event handlers
|
| 119 |
+
file_input.change(
|
| 120 |
+
fn=self.analyze_uploaded_audio,
|
| 121 |
+
inputs=file_input,
|
| 122 |
+
outputs=[original_player, tempo_text, key_text, energy_text, mood_text, original_plot, original_spec]
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
generate_btn.click(
|
| 126 |
+
fn=self.generate_music_from_file,
|
| 127 |
+
inputs=[
|
| 128 |
+
file_input, model_dropdown, duration_slider, guidance_slider,
|
| 129 |
+
conditioning_strength, pitch_shift, tempo_change
|
| 130 |
+
],
|
| 131 |
+
outputs=[audio_output, generated_plot, generated_spec]
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
analyze_btn.click(
|
| 135 |
+
fn=self.analyze_uploaded_audio,
|
| 136 |
+
inputs=file_input,
|
| 137 |
+
outputs=[original_player, tempo_text, key_text, energy_text, mood_text, original_plot, original_spec]
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
return file_input
|
| 141 |
+
|
| 142 |
+
def analyze_uploaded_audio(self, file_path: str):
|
| 143 |
+
"""Analyze uploaded audio file"""
|
| 144 |
+
try:
|
| 145 |
+
if not file_path:
|
| 146 |
+
raise gr.Error("Please upload an audio file")
|
| 147 |
+
|
| 148 |
+
# Load and process audio
|
| 149 |
+
audio_array, sr = self.audio_processor.load_audio(file_path)
|
| 150 |
+
audio_array = self.audio_processor.normalize_audio(audio_array)
|
| 151 |
+
|
| 152 |
+
# Create visualizations
|
| 153 |
+
waveform_fig = UIComponents.create_audio_visualization(audio_array)
|
| 154 |
+
spectrogram_fig = UIComponents.create_spectrogram_visualization(audio_array, sr)
|
| 155 |
+
|
| 156 |
+
# Analyze audio
|
| 157 |
+
tempo = self.audio_processor.get_tempo(audio_array)
|
| 158 |
+
key = self._estimate_key(audio_array)
|
| 159 |
+
energy = self._calculate_energy(audio_array)
|
| 160 |
+
mood = self._estimate_mood(audio_array)
|
| 161 |
+
|
| 162 |
+
return (
|
| 163 |
+
file_path,
|
| 164 |
+
f"{tempo:.1f}",
|
| 165 |
+
key,
|
| 166 |
+
f"{energy:.2f}",
|
| 167 |
+
mood,
|
| 168 |
+
waveform_fig,
|
| 169 |
+
spectrogram_fig
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
except Exception as e:
|
| 173 |
+
logger.error(f"Audio analysis failed: {str(e)}")
|
| 174 |
+
raise gr.Error(f"Analysis failed: {str(e)}")
|
| 175 |
+
|
| 176 |
+
def generate_music_from_file(
|
| 177 |
+
self,
|
| 178 |
+
file_path: str,
|
| 179 |
+
model_name: str,
|
| 180 |
+
duration: int,
|
| 181 |
+
guidance_scale: float,
|
| 182 |
+
conditioning_strength: float,
|
| 183 |
+
pitch_shift: int,
|
| 184 |
+
tempo_multiplier: float
|
| 185 |
+
):
|
| 186 |
+
"""Generate music from uploaded file"""
|
| 187 |
+
try:
|
| 188 |
+
if not file_path:
|
| 189 |
+
raise gr.Error("Please upload an audio file")
|
| 190 |
+
|
| 191 |
+
# Load and process audio
|
| 192 |
+
audio_array, sr = self.audio_processor.load_audio(file_path)
|
| 193 |
+
|
| 194 |
+
# Apply modifications
|
| 195 |
+
if pitch_shift != 0:
|
| 196 |
+
audio_array = self.audio_processor.change_pitch(audio_array, pitch_shift)
|
| 197 |
+
|
| 198 |
+
if tempo_multiplier != 1.0:
|
| 199 |
+
audio_array = self.audio_processor.change_speed(audio_array, tempo_multiplier)
|
| 200 |
+
|
| 201 |
+
audio_array = self.audio_processor.normalize_audio(audio_array)
|
| 202 |
+
|
| 203 |
+
# Get model
|
| 204 |
+
model = self.model_manager.get_model(model_name)
|
| 205 |
+
if not model:
|
| 206 |
+
raise gr.Error(f"Model {model_name} not available")
|
| 207 |
+
|
| 208 |
+
# Generate music
|
| 209 |
+
logger.info("Generating music from audio file...")
|
| 210 |
+
|
| 211 |
+
generated_audio = model.generate_from_audio(
|
| 212 |
+
audio_array=audio_array,
|
| 213 |
+
duration=duration,
|
| 214 |
+
guidance_scale=guidance_scale
|
| 215 |
+
)
|
| 216 |
+
|
| 217 |
+
# Apply conditioning strength
|
| 218 |
+
if conditioning_strength
|