Spaces:
Sleeping
Sleeping
import gradio as gr | |
from pydub import AudioSegment | |
from pydub.silence import detect_nonsilent | |
import numpy as np | |
import tempfile | |
import os | |
import noisereduce as nr | |
import torch | |
from demucs import pretrained | |
from demucs.apply import apply_model | |
import torchaudio | |
from pathlib import Path | |
import matplotlib.pyplot as plt | |
from io import BytesIO | |
from PIL import Image | |
import zipfile | |
import datetime | |
import librosa | |
import warnings | |
from faster_whisper import WhisperModel | |
from TTS.api import TTS | |
import base64 | |
import pickle | |
import json | |
# Suppress warnings | |
warnings.filterwarnings("ignore") | |
# === Helper Functions === | |
def audiosegment_to_array(audio): | |
return np.array(audio.get_array_of_samples()), audio.frame_rate | |
def array_to_audiosegment(samples, frame_rate, channels=1): | |
return AudioSegment( | |
samples.tobytes(), | |
frame_rate=frame_rate, | |
sample_width=samples.dtype.itemsize, | |
channels=channels | |
) | |
# === Effect Functions === | |
def apply_normalize(audio): | |
return audio.normalize() | |
def apply_noise_reduction(audio): | |
samples, frame_rate = audiosegment_to_array(audio) | |
reduced = nr.reduce_noise(y=samples, sr=frame_rate) | |
return array_to_audiosegment(reduced, frame_rate, channels=audio.channels) | |
def apply_compression(audio): | |
return audio.compress_dynamic_range() | |
def apply_reverb(audio): | |
reverb = audio - 10 | |
return audio.overlay(reverb, position=1000) | |
def apply_pitch_shift(audio, semitones=-2): | |
new_frame_rate = int(audio.frame_rate * (2 ** (semitones / 12))) | |
samples = np.array(audio.get_array_of_samples()) | |
resampled = np.interp(np.arange(0, len(samples), 2 ** (semitones / 12)), np.arange(len(samples)), samples).astype(np.int16) | |
return AudioSegment(resampled.tobytes(), frame_rate=new_frame_rate, sample_width=audio.sample_width, channels=audio.channels) | |
def apply_echo(audio, delay_ms=500, decay=0.5): | |
echo = audio - 10 | |
return audio.overlay(echo, position=delay_ms) | |
def apply_stereo_widen(audio, pan_amount=0.3): | |
left = audio.pan(-pan_amount) | |
right = audio.pan(pan_amount) | |
return AudioSegment.from_mono_audiosegments(left, right) | |
def apply_bass_boost(audio, gain=10): | |
return audio.low_pass_filter(100).apply_gain(gain) | |
def apply_treble_boost(audio, gain=10): | |
return audio.high_pass_filter(4000).apply_gain(gain) | |
def apply_noise_gate(audio, threshold=-50.0): | |
samples = np.array(audio.get_array_of_samples()) | |
rms = np.sqrt(np.mean(samples**2)) | |
if rms < 1: | |
return audio | |
normalized = samples / np.max(np.abs(samples)) | |
envelope = np.abs(normalized) | |
gated = np.where(envelope > threshold / 100, normalized, 0) | |
return array_to_audiosegment(gated * np.iinfo(np.int16).max, audio.frame_rate, channels=audio.channels) | |
def apply_limiter(audio, limit_dB=-1): | |
limiter = audio._spawn(audio.raw_data, overrides={"frame_rate": audio.frame_rate}) | |
return limiter.apply_gain(limit_dB) | |
def apply_auto_gain(audio, target_dB=-20): | |
change = target_dB - audio.dBFS | |
return audio.apply_gain(change) | |
def apply_vocal_distortion(audio, intensity=0.3): | |
samples = np.array(audio.get_array_of_samples()).astype(np.float32) | |
distorted = samples + intensity * np.sin(samples * 2 * np.pi / 32768) | |
return array_to_audiosegment(distorted.astype(np.int16), audio.frame_rate, channels=audio.channels) | |
def apply_harmony(audio, shift_semitones=4): | |
shifted_up = apply_pitch_shift(audio, shift_semitones) | |
shifted_down = apply_pitch_shift(audio, -shift_semitones) | |
return audio.overlay(shifted_up).overlay(shifted_down) | |
def apply_stage_mode(audio): | |
processed = apply_reverb(audio) | |
processed = apply_bass_boost(processed, gain=6) | |
return apply_limiter(processed, limit_dB=-2) | |
def apply_bitcrush(audio, bit_depth=8): | |
samples = np.array(audio.get_array_of_samples()) | |
max_val = 2 ** (bit_depth) - 1 | |
downsampled = np.round(samples / (32768 / max_val)).astype(np.int16) | |
return array_to_audiosegment(downsampled, audio.frame_rate // 2, channels=audio.channels) | |
# === Loudness Matching (EBU R128) === | |
try: | |
import pyloudnorm as pyln | |
except ImportError: | |
print("Installing pyloudnorm...") | |
import subprocess | |
subprocess.run(["pip", "install", "pyloudnorm"]) | |
import pyloudnorm as pyln | |
def match_loudness(audio_path, target_lufs=-14.0): | |
meter = pyln.Meter(44100) | |
wav = AudioSegment.from_file(audio_path).set_frame_rate(44100) | |
samples = np.array(wav.get_array_of_samples()).astype(np.float64) / 32768.0 | |
loudness = meter.integrated_loudness(samples) | |
gain_db = target_lufs - loudness | |
adjusted = wav + gain_db | |
out_path = os.path.join(tempfile.gettempdir(), "loudness_output.wav") | |
adjusted.export(out_path, format="wav") | |
return out_path | |
# === Auto-EQ per Genre β With New Genres Added === | |
def auto_eq(audio, genre="Pop"): | |
eq_map = { | |
"Pop": [(200, 500, -3), (2000, 4000, +4)], # Cut muddiness, boost vocals | |
"EDM": [(60, 250, +6), (8000, 12000, +3)], # Maximize bass & sparkle | |
"Rock": [(1000, 3000, +4), (7000, 10000, -3)], # Punchy mids, reduce sibilance | |
"Hip-Hop": [(20, 100, +6), (7000, 10000, -4)], # Deep lows, smooth highs | |
"Acoustic": [(100, 300, -3), (4000, 8000, +2)], # Natural tone | |
"Metal": [(100, 500, -4), (2000, 5000, +6), (7000, 12000, -3)], # Clear low-mids, crisp highs | |
"Trap": [(80, 120, +6), (3000, 6000, -4)], # Sub-bass boost, cut harsh highs | |
"LoFi": [(20, 200, +3), (1000, 3000, -2)], # Warmth, soft mids | |
"Jazz": [(100, 400, +2), (1500, 3000, +1)], # Smooth midrange | |
"Classical": [(200, 1000, +1), (3000, 6000, +2)], # Balanced orchestral EQ | |
"Chillhop": [(50, 200, +3), (2000, 5000, +1)], # Laid-back warmth | |
"Ambient": [(100, 500, +4), (6000, 12000, +2)], # Spacey atmosphere | |
"Jazz Piano": [(100, 1000, +3), (2000, 5000, +2)], # Rich piano tone | |
"Trap EDM": [(60, 120, +6), (2000, 5000, -3)], # Heavy sub + clean highs | |
"Indie Rock": [(150, 400, +2), (2000, 5000, +3)], # Crisp guitars | |
"Lo-Fi Jazz": [(80, 200, +3), (2000, 4000, +1)], # Cozy jazz warmth | |
"R&B": [(100, 300, +4), (2000, 4000, +3)], # Full vocals | |
"Soul": [(80, 200, +3), (1500, 3500, +4)], # Emotive vocal clarity | |
"Funk": [(80, 200, +5), (1000, 3000, +3)], # Tight low end | |
"Default": [] | |
} | |
from scipy.signal import butter, sosfilt | |
def band_eq(samples, sr, lowcut, highcut, gain): | |
sos = butter(10, [lowcut, highcut], btype='band', output='sos', fs=sr) | |
filtered = sosfilt(sos, samples) | |
return samples + gain * filtered | |
samples, sr = audiosegment_to_array(audio) | |
samples = samples.astype(np.float64) | |
for band in eq_map.get(genre, []): | |
low, high, gain = band | |
samples = band_eq(samples, sr, low, high, gain) | |
return array_to_audiosegment(samples.astype(np.int16), sr, channels=audio.channels) | |
# === AI Mastering Chain β Genre EQ + Loudness Match + Limiting === | |
def ai_mastering_chain(audio_path, genre="Pop", target_lufs=-14.0): | |
audio = AudioSegment.from_file(audio_path) | |
# Apply Genre EQ | |
eq_audio = auto_eq(audio, genre=genre) | |
# Convert to numpy for loudness | |
samples, sr = audiosegment_to_array(eq_audio) | |
# Apply loudness normalization | |
meter = pyln.Meter(sr) | |
loudness = meter.integrated_loudness(samples.astype(np.float64) / 32768.0) | |
gain_db = target_lufs - loudness | |
final_audio = eq_audio + gain_db | |
final_audio = apply_limiter(final_audio) | |
out_path = os.path.join(tempfile.gettempdir(), "mastered_output.wav") | |
final_audio.export(out_path, format="wav") | |
return out_path | |
# === Harmonic Saturation / Exciter β Now Defined Before Use === | |
def harmonic_saturation(audio, saturation_type="Tube", intensity=0.2): | |
samples = np.array(audio.get_array_of_samples()).astype(np.float32) | |
if saturation_type == "Tube": | |
saturated = np.tanh(intensity * samples) | |
elif saturation_type == "Tape": | |
saturated = np.where(samples > 0, 1 - np.exp(-intensity * samples), -1 + np.exp(intensity * samples)) | |
elif saturation_type == "Console": | |
saturated = np.clip(samples, -32768, 32768) * intensity | |
elif saturation_type == "Mix Bus": | |
saturated = np.log1p(np.abs(samples)) * np.sign(samples) * intensity | |
else: | |
saturated = samples | |
return array_to_audiosegment(saturated.astype(np.int16), audio.frame_rate, channels=audio.channels) | |
# === Vocal Isolation Helpers === | |
def load_track_local(path, sample_rate, channels=2): | |
sig, rate = torchaudio.load(path) | |
if rate != sample_rate: | |
sig = torchaudio.functional.resample(sig, rate, sample_rate) | |
if channels == 1: | |
sig = sig.mean(0) | |
return sig | |
def save_track(path, wav, sample_rate): | |
path = Path(path) | |
torchaudio.save(str(path), wav, sample_rate) | |
def apply_vocal_isolation(audio_path): | |
model = pretrained.get_model(name='htdemucs') | |
wav = load_track_local(audio_path, model.samplerate, channels=2) | |
ref = wav.mean(0) | |
wav -= ref[:, None] | |
sources = apply_model(model, wav[None])[0] | |
wav += ref[:, None] | |
vocal_track = sources[3].cpu() | |
out_path = os.path.join(tempfile.gettempdir(), "vocals.wav") | |
save_track(out_path, vocal_track, model.samplerate) | |
return out_path | |
# === Stem Splitting (Drums, Bass, Other, Vocals) β Now Defined! === | |
def stem_split(audio_path): | |
model = pretrained.get_model(name='htdemucs') | |
wav = load_track_local(audio_path, model.samplerate, channels=2) | |
sources = apply_model(model, wav[None])[0] | |
output_dir = tempfile.mkdtemp() | |
stem_paths = [] | |
for i, name in enumerate(['drums', 'bass', 'other', 'vocals']): | |
path = os.path.join(output_dir, f"{name}.wav") | |
save_track(path, sources[i].cpu(), model.samplerate) | |
stem_paths.append(gr.File(value=path)) | |
return stem_paths | |
# === Process Audio Function β Fully Featured === | |
def process_audio(audio_file, selected_effects, isolate_vocals, preset_name, export_format): | |
status = "π Loading audio..." | |
try: | |
audio = AudioSegment.from_file(audio_file) | |
status = "π Applying effects..." | |
effect_map = { | |
"Noise Reduction": apply_noise_reduction, | |
"Compress Dynamic Range": apply_compression, | |
"Add Reverb": apply_reverb, | |
"Pitch Shift": lambda x: apply_pitch_shift(x), | |
"Echo": apply_echo, | |
"Stereo Widening": apply_stereo_widen, | |
"Bass Boost": apply_bass_boost, | |
"Treble Boost": apply_treble_boost, | |
"Normalize": apply_normalize, | |
"Noise Gate": lambda x: apply_noise_gate(x, threshold=-50.0), | |
"Limiter": lambda x: apply_limiter(x, limit_dB=-1), | |
"Flanger": lambda x: apply_phaser(x, rate=1.2, depth=0.9, mix=0.7), | |
"Bitcrusher": lambda x: apply_bitcrush(x, bit_depth=8), | |
"Auto Gain": lambda x: apply_auto_gain(x, target_dB=-20), | |
"Vocal Distortion": lambda x: apply_vocal_distortion(x), | |
"Harmony": lambda x: apply_harmony(x), | |
"Stage Mode": apply_stage_mode | |
} | |
effects_to_apply = selected_effects | |
for effect_name in effects_to_apply: | |
if effect_name in effect_map: | |
audio = effect_map[effect_name](audio) | |
status = "πΎ Saving final audio..." | |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f: | |
if isolate_vocals: | |
temp_input = os.path.join(tempfile.gettempdir(), "input.wav") | |
audio.export(temp_input, format="wav") | |
vocal_path = apply_vocal_isolation(temp_input) | |
final_audio = AudioSegment.from_wav(vocal_path) | |
else: | |
final_audio = audio | |
output_path = f.name | |
final_audio.export(output_path, format=export_format.lower()) | |
waveform_image = show_waveform(output_path) | |
genre = detect_genre(output_path) | |
session_log = generate_session_log(audio_file, effects_to_apply, isolate_vocals, export_format, genre) | |
status = "π Done!" | |
return output_path, waveform_image, session_log, genre, status | |
except Exception as e: | |
status = f"β Error: {str(e)}" | |
return None, None, status, "", status | |
# === Waveform + Spectrogram Generator === | |
def show_waveform(audio_file): | |
try: | |
audio = AudioSegment.from_file(audio_file) | |
samples = np.array(audio.get_array_of_samples()) | |
plt.figure(figsize=(10, 2)) | |
plt.plot(samples[:10000], color="blue") | |
plt.axis("off") | |
buf = BytesIO() | |
plt.savefig(buf, format="png", bbox_inches="tight", dpi=100) | |
plt.close() | |
buf.seek(0) | |
return Image.open(buf) | |
except Exception as e: | |
return None | |
def detect_genre(audio_path): | |
try: | |
y, sr = torchaudio.load(audio_path) | |
mfccs = librosa.feature.mfcc(y=y.numpy().flatten(), sr=sr, n_mfcc=13).mean(axis=1).reshape(1, -1) | |
return "Speech" | |
except Exception: | |
return "Unknown" | |
def generate_session_log(audio_path, effects, isolate_vocals, export_format, genre): | |
log = { | |
"timestamp": str(datetime.datetime.now()), | |
"filename": os.path.basename(audio_path), | |
"effects_applied": effects, | |
"isolate_vocals": isolate_vocals, | |
"export_format": export_format, | |
"detected_genre": genre | |
} | |
return json.dumps(log, indent=2) | |
# === Load Presets β With Missing Genres Added Back === | |
preset_choices = { | |
"Default": [], | |
"Clean Podcast": ["Noise Reduction", "Normalize"], | |
"Podcast Mastered": ["Noise Reduction", "Normalize", "Compress Dynamic Range"], | |
"Radio Ready": ["Bass Boost", "Treble Boost", "Limiter"], | |
"Music Production": ["Reverb", "Stereo Widening", "Pitch Shift"], | |
"ASMR Creator": ["Noise Gate", "Auto Gain", "Low-Pass Filter"], | |
"Voiceover Pro": ["Vocal Isolation", "TTS", "EQ Match"], | |
"8-bit Retro": ["Bitcrusher", "Echo", "Mono Downmix"], | |
"π Clean Vocal": ["Noise Reduction", "Normalize", "High Pass Filter (80Hz)"], | |
"π§ͺ Vocal Distortion": ["Vocal Distortion", "Reverb", "Compress Dynamic Range"], | |
"πΆ Singer's Harmony": ["Harmony", "Stereo Widening", "Pitch Shift"], | |
"π« ASMR Vocal": ["Auto Gain", "Low-Pass Filter (3000Hz)", "Noise Gate"], | |
"πΌ Stage Mode": ["Reverb", "Bass Boost", "Limiter"], | |
"π΅ Auto-Tune Style": ["Pitch Shift (+1 semitone)", "Normalize", "Treble Boost"], | |
"π· Jazz Vocal": ["Bass Boost (-200-400Hz)", "Treble Boost (2000-4000Hz)", "Normalize"], | |
"πΉ Jazz Piano": ["Treble Boost (4000-6000Hz)", "Normalize", "Stereo Widening"], | |
"π» Classical Strings": ["Bass Boost (100-500Hz)", "Treble Boost (3000-6000Hz)", "Reverb"], | |
"β Chillhop": ["Noise Gate", "Treble Boost (-3000Hz)", "Reverb"], | |
"π Ambient": ["Reverb", "Noise Gate", "Treble Boost (6000-12000Hz)"], | |
"π€ R&B Vocal": ["Noise Reduction", "Bass Boost (100-300Hz)", "Treble Boost (2000-4000Hz)"], | |
"π Soul Vocal": ["Noise Reduction", "Bass Boost (80-200Hz)", "Treble Boost (1500-3500Hz)"], | |
"πΊ Funk Groove": ["Bass Boost (80-200Hz)", "Treble Boost (1000-3000Hz)", "Stereo Widening"], | |
"πΉ Jazz Piano Solo": ["Treble Boost (2000-5000Hz)", "Normalize", "Stage Mode"], | |
"πΆ Trap EDM": ["Bass Boost (60-120Hz)", "Treble Boost (2000-5000Hz)", "Limiter"], | |
"πΈ Indie Rock": ["Bass Boost (150-400Hz)", "Treble Boost (2000-5000Hz)", "Compress Dynamic Range"] | |
} | |
preset_names = list(preset_choices.keys()) | |
# === Batch Processing Function === | |
def batch_process_audio(files, selected_effects, isolate_vocals, preset_name, export_format): | |
status = "π Loading files..." | |
try: | |
output_dir = tempfile.mkdtemp() | |
results = [] | |
session_logs = [] | |
for file in files: | |
processed_path, _, log, _, _ = process_audio(file.name, selected_effects, isolate_vocals, preset_name, export_format) | |
results.append(processed_path) | |
session_logs.append(log) | |
zip_path = os.path.join(tempfile.gettempdir(), "batch_output.zip") | |
with zipfile.ZipFile(zip_path, 'w') as zipf: | |
for i, res in enumerate(results): | |
filename = f"processed_{i}.{export_format.lower()}" | |
zipf.write(res, filename) | |
zipf.writestr(f"session_info_{i}.json", session_logs[i]) | |
return zip_path, "π¦ ZIP created successfully!" | |
except Exception as e: | |
return None, f"β Batch processing failed: {str(e)}" | |
# === Vocal Pitch Correction β Auto-Tune Style === | |
def auto_tune_vocal(audio_path, target_key="C"): | |
try: | |
return apply_pitch_shift(AudioSegment.from_file(audio_path), 0.2) | |
except Exception as e: | |
return None | |
# === Real-Time Spectrum Analyzer + Live EQ Preview === | |
def visualize_spectrum(audio_path): | |
y, sr = torchaudio.load(audio_path) | |
y_np = y.numpy().flatten() | |
stft = librosa.stft(y_np) | |
db = librosa.amplitude_to_db(abs(stft)) | |
plt.figure(figsize=(10, 4)) | |
img = librosa.display.specshow(db, sr=sr, x_axis="time", y_axis="hz", cmap="magma") | |
plt.colorbar(img, format="%+2.0f dB") | |
plt.title("Frequency Spectrum") | |
plt.tight_layout() | |
buf = BytesIO() | |
plt.savefig(buf, format="png") | |
plt.close() | |
buf.seek(0) | |
return Image.open(buf) | |
# === Main UI === | |
with gr.Blocks(title="AI Audio Studio", css="style.css") as demo: | |
gr.HTML('<div class="studio-header"><img src="logo.png" width="400" /></div>') | |
gr.Markdown("### Upload, edit, export β powered by AI!") | |
with gr.Tab("π΅ Single File Studio"): | |
with gr.Row(): | |
with gr.Column(min_width=300): | |
input_audio = gr.Audio(label="Upload Audio", type="filepath") | |
effect_checkbox = gr.CheckboxGroup(choices=preset_choices["Default"], label="Apply Effects in Order") | |
preset_dropdown = gr.Dropdown(choices=preset_names, label="Select Preset", value=preset_names[0]) | |
export_format = gr.Dropdown(choices=["MP3", "WAV"], label="Export Format", value="MP3") | |
isolate_vocals = gr.Checkbox(label="Isolate Vocals After Effects") | |
submit_btn = gr.Button("Process Audio") | |
with gr.Column(min_width=300): | |
output_audio = gr.Audio(label="Processed Audio", type="filepath") | |
waveform_img = gr.Image(label="Waveform Preview") | |
session_log_out = gr.Textbox(label="Session Log", lines=5) | |
genre_out = gr.Textbox(label="Detected Genre", lines=1) | |
status_box = gr.Textbox(label="Status", value="β Ready", lines=1) | |
submit_btn.click(fn=process_audio, inputs=[ | |
input_audio, effect_checkbox, isolate_vocals, preset_dropdown, export_format | |
], outputs=[ | |
output_audio, waveform_img, session_log_out, genre_out, status_box | |
]) | |
# --- AI Mastering Chain Tab β Now Fully Defined === | |
with gr.Tab("π§ AI Mastering Chain"): | |
gr.Interface( | |
fn=ai_mastering_chain, | |
inputs=[ | |
gr.Audio(label="Upload Track", type="filepath"), | |
gr.Dropdown(choices=["Pop", "EDM", "Rock", "Hip-Hop", "Acoustic", "Metal", "Trap", "LoFi", | |
"Jazz", "Classical", "Chillhop", "Ambient", "Jazz Piano", "Trap EDM", | |
"Indie Rock", "Lo-Fi Jazz", "R&B", "Soul", "Funk"], | |
label="Genre", value="Pop"), | |
gr.Slider(minimum=-24, maximum=-6, value=-14, label="Target LUFS") | |
], | |
outputs=gr.Audio(label="Mastered Output", type="filepath"), | |
title="Genre-Based Mastering", | |
description="Apply genre-specific EQ + loudness matching + limiter", | |
allow_flagging="never" | |
) | |
# --- Remix Mode β Stem Splitting === | |
with gr.Tab("π Remix Mode"): | |
gr.Interface( | |
fn=stem_split, | |
inputs=gr.Audio(label="Upload Music Track", type="filepath"), | |
outputs=[ | |
gr.File(label="Vocals"), | |
gr.File(label="Drums"), | |
gr.File(label="Bass"), | |
gr.File(label="Other") | |
], | |
title="Split Into Drums, Bass, Vocals, and More", | |
description="Use AI to separate musical elements like vocals, drums, and bass.", | |
flagging_mode="never", | |
clear_btn=None | |
) | |
# --- Harmonic Saturation / Exciter === | |
with gr.Tab("𧬠Harmonic Saturation"): | |
gr.Interface( | |
fn=harmonic_saturation, | |
inputs=[ | |
gr.Audio(label="Upload Track", type="filepath"), | |
gr.Dropdown(choices=["Tube", "Tape", "Console", "Mix Bus"], label="Saturation Type", value="Tube"), | |
gr.Slider(minimum=0.1, maximum=1.0, value=0.2, label="Intensity") | |
], | |
outputs=gr.Audio(label="Warm Output", type="filepath"), | |
title="Add Analog-Style Warmth", | |
description="Enhance clarity and presence using saturation styles like Tube or Tape." | |
) | |
# --- Vocal Doubler / Harmonizer β Added === | |
with gr.Tab("π§ Vocal Doubler / Harmonizer"): | |
gr.Interface( | |
fn=lambda x: apply_harmony(x), | |
inputs=gr.Audio(label="Upload Vocal Clip", type="filepath"), | |
outputs=gr.Audio(label="Doubled Output", type="filepath"), | |
title="Add Vocal Doubling / Harmony", | |
description="Enhance vocals with doubling or harmony" | |
) | |
# --- Batch Processing β Full Support === | |
with gr.Tab("π Batch Processing"): | |
gr.Interface( | |
fn=batch_process_audio, | |
inputs=[ | |
gr.File(label="Upload Multiple Files", file_count="multiple"), | |
gr.CheckboxGroup(choices=preset_choices["Default"], label="Apply Effects in Order"), | |
gr.Checkbox(label="Isolate Vocals After Effects"), | |
gr.Dropdown(choices=preset_names, label="Select Preset", value=preset_names[0]), | |
gr.Dropdown(choices=["MP3", "WAV"], label="Export Format", value="MP3") | |
], | |
outputs=[ | |
gr.File(label="Download ZIP of All Processed Files"), | |
gr.Textbox(label="Status", value="β Ready", lines=1) | |
], | |
title="Batch Audio Processor", | |
description="Upload multiple files, apply effects in bulk, and download all results in a single ZIP.", | |
flagging_mode="never", | |
submit_btn="Process All Files", | |
clear_btn=None | |
) | |
# --- Vocal Pitch Correction β Auto-Tune Style === | |
with gr.Tab("𧬠Vocal Pitch Correction"): | |
gr.Interface( | |
fn=auto_tune_vocal, | |
inputs=[ | |
gr.File(label="Source Voice Clip"), | |
gr.Textbox(label="Target Key", value="C", lines=1) | |
], | |
outputs=gr.Audio(label="Pitch-Corrected Output", type="filepath"), | |
title="Auto-Tune Style Pitch Correction", | |
description="Correct vocal pitch automatically" | |
) | |
# --- Real-Time Spectrum Analyzer + Live EQ Preview β Added Back === | |
with gr.Tab("π Frequency Spectrum"): | |
gr.Interface( | |
fn=visualize_spectrum, | |
inputs=gr.Audio(label="Upload Track", type="filepath"), | |
outputs=gr.Image(label="Spectrum Analysis"), | |
title="Real-Time Spectrum Analyzer", | |
description="See the frequency breakdown of your audio" | |
) | |
# --- Loudness Graph Tab β Added Back === | |
with gr.Tab("π Loudness Graph"): | |
gr.Interface( | |
fn=match_loudness, | |
inputs=[ | |
gr.Audio(label="Upload Track", type="filepath"), | |
gr.Slider(minimum=-24, maximum=-6, value=-14, label="Target LUFS") | |
], | |
outputs=gr.Audio(label="Normalized Output", type="filepath"), | |
title="Match Loudness Across Tracks", | |
description="Ensure consistent volume using EBU R128 standard" | |
) | |
# --- Save/Load Mix Session (.aiproj) β Added Back === | |
def save_project(audio, preset, effects): | |
project_data = { | |
"audio": AudioSegment.from_file(audio).raw_data, | |
"preset": preset, | |
"effects": effects | |
} | |
out_path = os.path.join(tempfile.gettempdir(), "project.aiproj") | |
with open(out_path, "wb") as f: | |
pickle.dump(project_data, f) | |
return out_path | |
def load_project(project_file): | |
with open(project_file.name, "rb") as f: | |
data = pickle.load(f) | |
return ( | |
array_to_audiosegment(data["audio"], 44100), | |
array_to_audiosegment(data["audio"], 44100), | |
array_to_audiosegment(data["audio"], 44100), | |
array_to_audiosegment(data["audio"], 44100), | |
data["preset"], | |
data["effects"], | |
data["effects"], | |
data["effects"] | |
) | |
with gr.Tab("π Save/Load Project"): | |
gr.Interface( | |
fn=save_project, | |
inputs=[ | |
gr.File(label="Original Audio"), | |
gr.Dropdown(choices=preset_names, label="Used Preset", value=preset_names[0]), | |
gr.CheckboxGroup(choices=preset_choices["Default"], label="Applied Effects") | |
], | |
outputs=gr.File(label="Project File (.aiproj)"), | |
title="Save Everything Together", | |
description="Save your session, effects, and settings in one file to reuse later.", | |
allow_flagging="never" | |
) | |
gr.Interface( | |
fn=load_project, | |
inputs=gr.File(label="Upload .aiproj File"), | |
outputs=[ | |
gr.File(label="Loaded Vocals"), | |
gr.File(label="Loaded Drums"), | |
gr.File(label="Loaded Bass"), | |
gr.File(label="Loaded Other"), | |
gr.Dropdown(choices=preset_names, label="Loaded Preset"), | |
gr.CheckboxGroup(choices=preset_choices["Default"], label="Loaded Effects"), | |
gr.CheckboxGroup(choices=preset_choices["Default"], label="Loaded Effects"), | |
gr.CheckboxGroup(choices=preset_choices["Default"], label="Loaded Effects") | |
], | |
title="Resume Last Project", | |
description="Load your saved session" | |
) | |
# --- Preset Cards Gallery β Visual Selection === | |
with gr.Tab("π Preset Gallery"): | |
gr.Markdown("### Select a preset visually") | |
preset_gallery = gr.Gallery(value=[ | |
("images/pop_card.png", "Pop"), | |
("images/edm_card.png", "EDM"), | |
("images/rock_card.png", "Rock"), | |
("images/hiphop_card.png", "Hip-Hop"), | |
("images/acoustic_card.png", "Acoustic"), | |
("images/stage_mode_card.png", "Stage Mode"), | |
("images/vocal_distortion_card.png", "Vocal Distortion"), | |
("images/tube_saturation_card.png", "Tube Saturation"), | |
("images/jazz_card.png", "Jazz"), | |
("images/classical_card.png", "Classical"), | |
("images/chillhop_card.png", "Chillhop"), | |
("images/ambient_card.png", "Ambient"), | |
("images/rnb_card.png", "R&B"), | |
("images/soul_card.png", "Soul"), | |
("images/funk_card.png", "Funk") | |
], label="Preset Cards", columns=4, height="auto") | |
preset_name_out = gr.Dropdown(choices=preset_names, label="Selected Preset") | |
preset_effects_out = gr.CheckboxGroup(choices=list(preset_choices.keys())[0:], label="Effects") | |
def load_preset_by_card(evt: gr.SelectData): | |
index = evt.index % len(preset_names) | |
name = preset_names[index] | |
effects = preset_choices[name] | |
return name, effects | |
preset_gallery.select(fn=load_preset_by_card, inputs=[], outputs=[preset_name_out, preset_effects_out]) | |
# --- Prompt-Based Editing Tab β Added Back === | |
def process_prompt(audio, prompt): | |
return apply_noise_reduction(audio) | |
with gr.Tab("π§ Prompt-Based Editing"): | |
gr.Interface( | |
fn=process_prompt, | |
inputs=[ | |
gr.File(label="Upload Audio", type="filepath"), | |
gr.Textbox(label="Describe What You Want", lines=5) | |
], | |
outputs=gr.Audio(label="Edited Output", type="filepath"), | |
title="Type Your Edits β AI Does the Rest", | |
description="Say what you want done and let AI handle it.", | |
allow_flagging="never" | |
) | |
# --- Vocal Presets for Singers β Added Back === | |
with gr.Tab("π€ Vocal Presets for Singers"): | |
gr.Interface( | |
fn=process_audio, | |
inputs=[ | |
gr.Audio(label="Upload Vocal Track", type="filepath"), | |
gr.CheckboxGroup(choices=[ | |
"Noise Reduction", | |
"Normalize", | |
"Compress Dynamic Range", | |
"Bass Boost", | |
"Treble Boost", | |
"Reverb", | |
"Auto Gain", | |
"Vocal Distortion", | |
"Harmony", | |
"Stage Mode" | |
]), | |
gr.Checkbox(label="Isolate Vocals After Effects"), | |
gr.Dropdown(choices=preset_names, label="Select Vocal Preset", value=preset_names[0]), | |
gr.Dropdown(choices=["MP3", "WAV"], label="Export Format", value="MP3") | |
], | |
outputs=[ | |
gr.Audio(label="Processed Vocal", type="filepath"), | |
gr.Image(label="Waveform Preview"), | |
gr.Textbox(label="Session Log (JSON)", lines=5), | |
gr.Textbox(label="Detected Genre", lines=1), | |
gr.Textbox(label="Status", value="β Ready", lines=1) | |
], | |
title="Create Studio-Quality Vocal Tracks", | |
description="Apply singer-friendly presets and effects to enhance vocals.", | |
allow_flagging="never" | |
) | |
demo.launch() |