tee342 commited on
Commit
a8f4b9f
·
verified ·
1 Parent(s): 0b8e39c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +803 -71
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
- from flask import Flask, request, jsonify
3
  from pydub import AudioSegment
 
4
  import numpy as np
5
  import tempfile
6
  import os
@@ -9,16 +9,98 @@ import torch
9
  from demucs import pretrained
10
  from demucs.apply import apply_model
11
  import torchaudio
 
 
 
 
 
 
12
  import librosa
13
- import soundfile as sf
14
- import json
15
  import warnings
 
 
 
 
 
 
16
 
 
17
  warnings.filterwarnings("ignore")
18
 
19
- app = Flask(__name__)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
- # Helper Functions
 
 
 
 
 
 
22
  def audiosegment_to_array(audio):
23
  return np.array(audio.get_array_of_samples()), audio.frame_rate
24
 
@@ -30,78 +112,728 @@ def array_to_audiosegment(samples, frame_rate, channels=1):
30
  channels=channels
31
  )
32
 
33
- def save_audiosegment_to_temp(audio, suffix=".wav"):
34
- with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as f:
35
- audio.export(f.name, format=suffix.lstrip('.'))
36
- return f.name
 
 
 
 
37
 
38
- def load_audiofile_to_numpy(path):
39
- samples, sr = sf.read(path, dtype="int16")
40
- if samples.ndim > 1 and samples.shape[1] > 2:
41
- samples = samples[:, :2]
42
- return samples, sr
 
 
 
 
 
43
 
44
- # Effect Functions
45
- def apply_normalize(audio):
46
- return audio.normalize()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
- def apply_noise_reduction(audio):
49
  samples, sr = audiosegment_to_array(audio)
50
- reduced = nr.reduce_noise(y=samples, sr=sr)
51
- return array_to_audiosegment(reduced, sr, channels=audio.channels)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
- # Add other effect functions here...
 
 
54
 
55
- # API Endpoints
56
- @app.route('/process_audio', methods=['POST'])
57
- def process_audio_endpoint():
58
- if 'audio' not in request.files:
59
- return jsonify({'error': 'No audio file provided'}), 400
 
 
 
 
 
 
 
60
 
61
- audio_file = request.files['audio']
62
- audio_path = save_audiosegment_to_temp(AudioSegment.from_file(audio_file))
 
 
63
 
64
- # Process the audio file here
 
65
  audio = AudioSegment.from_file(audio_path)
66
- audio = apply_normalize(audio)
67
- audio = apply_noise_reduction(audio)
68
-
69
- # Save the processed audio
70
- processed_audio_path = save_audiosegment_to_temp(audio)
71
- samples, sr = load_audiofile_to_numpy(processed_audio_path)
72
-
73
- return jsonify({
74
- 'samples': samples.tolist(),
75
- 'sample_rate': sr,
76
- 'message': 'Audio processed successfully'
77
- })
78
-
79
- @app.route('/apply_effect', methods=['POST'])
80
- def apply_effect_endpoint():
81
- data = request.get_json()
82
- effect = data.get('effect')
83
- audio_file = data.get('audio_file')
84
-
85
- if not audio_file:
86
- return jsonify({'error': 'No audio file provided'}), 400
87
-
88
- audio = AudioSegment.from_file(audio_file)
89
-
90
- # Apply the selected effect
91
- if effect == 'normalize':
92
- audio = apply_normalize(audio)
93
- elif effect == 'noise_reduction':
94
- audio = apply_noise_reduction(audio)
95
- # Add other effects here...
96
-
97
- processed_audio_path = save_audiosegment_to_temp(audio)
98
- samples, sr = load_audiofile_to_numpy(processed_audio_path)
99
-
100
- return jsonify({
101
- 'samples': samples.tolist(),
102
- 'sample_rate': sr,
103
- 'message': f'{effect} applied successfully'
104
- })
105
-
106
- if __name__ == '__main__':
107
- app.run(host='0.0.0.0', port=5000)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
 
2
  from pydub import AudioSegment
3
+ from pydub.silence import detect_nonsilent
4
  import numpy as np
5
  import tempfile
6
  import os
 
9
  from demucs import pretrained
10
  from demucs.apply import apply_model
11
  import torchaudio
12
+ from pathlib import Path
13
+ import matplotlib.pyplot as plt
14
+ from io import BytesIO
15
+ from PIL import Image
16
+ import zipfile
17
+ import datetime
18
  import librosa
 
 
19
  import warnings
20
+ # from faster_whisper import WhisperModel
21
+ # from TTS.api import TTS
22
+ import base64
23
+ import pickle
24
+ import json
25
+ import soundfile as SF
26
 
27
+ print("Gradio version:", gr.__version__)
28
  warnings.filterwarnings("ignore")
29
 
30
+ # Helper to convert file to base64
31
+ def file_to_base64_audio(file_path, mime_type="audio/wav"):
32
+ with open(file_path, "rb") as f:
33
+ data = f.read()
34
+ b64 = base64.b64encode(data).decode()
35
+ return f"data:{mime_type};base64,{b64}"
36
+
37
+ # === Effects Definitions ===
38
+ def apply_normalize(audio):
39
+ return audio.normalize()
40
+
41
+ def apply_noise_reduction(audio):
42
+ samples, frame_rate = audiosegment_to_array(audio)
43
+ reduced = nr.reduce_noise(y=samples, sr=frame_rate)
44
+ return array_to_audiosegment(reduced, frame_rate, channels=audio.channels)
45
+
46
+ def apply_compression(audio):
47
+ return audio.compress_dynamic_range()
48
+
49
+ def apply_reverb(audio):
50
+ reverb = audio - 10
51
+ return audio.overlay(reverb, position=1000)
52
+
53
+ def apply_pitch_shift(audio, semitones=-2):
54
+ new_frame_rate = int(audio.frame_rate * (2 ** (semitones / 12)))
55
+ samples = np.array(audio.get_array_of_samples())
56
+ resampled = np.interp(np.arange(0, len(samples), 2 ** (semitones / 12)), np.arange(len(samples)), samples).astype(np.int16)
57
+ return AudioSegment(resampled.tobytes(), frame_rate=new_frame_rate, sample_width=audio.sample_width, channels=audio.channels)
58
+
59
+ def apply_echo(audio, delay_ms=500, decay=0.5):
60
+ echo = audio - 10
61
+ return audio.overlay(echo, position=delay_ms)
62
+
63
+ def apply_stereo_widen(audio, pan_amount=0.3):
64
+ left = audio.pan(-pan_amount)
65
+ right = audio.pan(pan_amount)
66
+ return AudioSegment.from_mono_audiosegments(left, right)
67
+
68
+ def apply_bass_boost(audio, gain=10):
69
+ return audio.low_pass_filter(100).apply_gain(gain)
70
+
71
+ def apply_treble_boost(audio, gain=10):
72
+ return audio.high_pass_filter(4000).apply_gain(gain)
73
+
74
+ def apply_limiter(audio, limit_dB=-1):
75
+ limiter = audio._spawn(audio.raw_data, overrides={"frame_rate": audio.frame_rate})
76
+ return limiter.apply_gain(limit_dB)
77
+
78
+ def apply_auto_gain(audio, target_dB=-20):
79
+ change = target_dB - audio.dBFS
80
+ return audio.apply_gain(change)
81
+
82
+ def apply_vocal_distortion(audio, intensity=0.3):
83
+ samples = np.array(audio.get_array_of_samples()).astype(np.float32)
84
+ distorted = samples + intensity * np.sin(samples * 2 * np.pi / 32768)
85
+ return array_to_audiosegment(distorted.astype(np.int16), audio.frame_rate, channels=audio.channels)
86
+
87
+ def apply_harmony(audio, shift_semitones=4):
88
+ shifted_up = apply_pitch_shift(audio, shift_semitones)
89
+ shifted_down = apply_pitch_shift(audio, -shift_semitones)
90
+ return audio.overlay(shifted_up).overlay(shifted_down)
91
+
92
+ def apply_stage_mode(audio):
93
+ processed = apply_reverb(audio)
94
+ processed = apply_bass_boost(processed, gain=6)
95
+ return apply_limiter(processed, limit_dB=-2)
96
 
97
+ def apply_bitcrush(audio, bit_depth=8):
98
+ samples = np.array(audio.get_array_of_samples())
99
+ max_val = 2 ** (bit_depth) - 1
100
+ downsampled = np.round(samples / (32768 / max_val)).astype(np.int16)
101
+ return array_to_audiosegment(downsampled, audio.frame_rate // 2, channels=audio.channels)
102
+
103
+ # === Helper Functions ===
104
  def audiosegment_to_array(audio):
105
  return np.array(audio.get_array_of_samples()), audio.frame_rate
106
 
 
112
  channels=channels
113
  )
114
 
115
+ # === Loudness Matching (EBU R128) ===
116
+ try:
117
+ import pyloudnorm as pyln
118
+ except ImportError:
119
+ print("Installing pyloudnorm...")
120
+ import subprocess
121
+ subprocess.run(["pip", "install", "pyloudnorm"])
122
+ import pyloudnorm as pyln
123
 
124
+ def match_loudness(audio_path, target_lufs=-14.0):
125
+ meter = pyln.Meter(44100)
126
+ wav = AudioSegment.from_file(audio_path).set_frame_rate(44100)
127
+ samples = np.array(wav.get_array_of_samples()).astype(np.float64) / 32768.0
128
+ loudness = meter.integrated_loudness(samples)
129
+ gain_db = target_lufs - loudness
130
+ adjusted = wav + gain_db
131
+ out_path = os.path.join(tempfile.gettempdir(), "loudness_output.wav")
132
+ adjusted.export(out_path, format="wav")
133
+ return out_path
134
 
135
+ # === Auto-EQ per Genre – With R&B, Soul, Funk ===
136
+ def auto_eq(audio, genre="Pop"):
137
+ eq_map = {
138
+ "Pop": [(200, 500, -3), (2000, 4000, +4)],
139
+ "EDM": [(60, 250, +6), (8000, 12000, +3)],
140
+ "Rock": [(1000, 3000, +4), (7000, 10000, -3)],
141
+ "Hip-Hop": [(20, 100, +6), (7000, 10000, -4)],
142
+ "Acoustic": [(100, 300, -3), (4000, 8000, +2)],
143
+ "Metal": [(100, 500, -4), (2000, 5000, +6), (7000, 12000, -3)],
144
+ "Trap": [(80, 120, +6), (3000, 6000, -4)],
145
+ "LoFi": [(20, 200, +3), (1000, 3000, -2)],
146
+ "Jazz": [(100, 400, +2), (1500, 3000, +1)],
147
+ "Classical": [(200, 1000, +1), (3000, 6000, +2)],
148
+ "Chillhop": [(50, 200, +3), (2000, 5000, +1)],
149
+ "Ambient": [(100, 500, +4), (6000, 12000, +2)],
150
+ "Jazz Piano": [(100, 1000, +3), (2000, 5000, +2)],
151
+ "Trap EDM": [(60, 120, +6), (2000, 5000, -3)],
152
+ "Indie Rock": [(150, 400, +2), (2000, 5000, +3)],
153
+ "Lo-Fi Jazz": [(80, 200, +3), (2000, 4000, -1)],
154
+ "R&B": [(100, 300, +4), (2000, 4000, +3)],
155
+ "Soul": [(80, 200, +3), (1500, 3500, +4)],
156
+ "Funk": [(80, 200, +5), (1000, 3000, +3)],
157
+ "Default": []
158
+ }
159
+ from scipy.signal import butter, sosfilt
160
+ def band_eq(samples, sr, lowcut, highcut, gain):
161
+ sos = butter(10, [lowcut, highcut], btype='band', output='sos', fs=sr)
162
+ filtered = sosfilt(sos, samples)
163
+ return samples + gain * filtered
164
 
 
165
  samples, sr = audiosegment_to_array(audio)
166
+ samples = samples.astype(np.float64)
167
+ for band in eq_map.get(genre, []):
168
+ low, high, gain = band
169
+ samples = band_eq(samples, sr, low, high, gain)
170
+ return array_to_audiosegment(samples.astype(np.int16), sr, channels=audio.channels)
171
+
172
+ # === Load Track Helpers ===
173
+ def load_track_local(path, sample_rate, channels=2):
174
+ sig, rate = torchaudio.load(path)
175
+ if rate != sample_rate:
176
+ sig = torchaudio.functional.resample(sig, rate, sample_rate)
177
+ if channels == 1:
178
+ sig = sig.mean(0)
179
+ return sig
180
+
181
+ def save_track(path, wav, sample_rate):
182
+ path = Path(path)
183
+ torchaudio.save(str(path), wav, sample_rate)
184
+
185
+ # === Vocal Isolation Helpers ===
186
+ def apply_vocal_isolation(audio_path):
187
+ model = pretrained.get_model(name='htdemucs')
188
+ wav = load_track_local(audio_path, model.samplerate, channels=2)
189
+ ref = wav.mean(0)
190
+ wav -= ref[:, None]
191
+ sources = apply_model(model, wav[None])[0]
192
+ wav += ref[:, None]
193
+ vocal_track = sources[3].cpu()
194
+ out_path = os.path.join(tempfile.gettempdir(), "vocals.wav")
195
+ save_track(out_path, vocal_track, model.samplerate)
196
+ return out_path
197
+
198
+ # === Stem Splitting Function ===
199
+ def stem_split(audio_path):
200
+ model = pretrained.get_model(name='htdemucs')
201
+ wav = load_track_local(audio_path, model.samplerate, channels=2)
202
+ sources = apply_model(model, wav[None])[0]
203
+ output_dir = tempfile.mkdtemp()
204
+ stem_paths = []
205
+ for i, name in enumerate(['drums', 'bass', 'other', 'vocals']):
206
+ path = os.path.join(output_dir, f"{name}.wav")
207
+ save_track(path, sources[i].cpu(), model.samplerate)
208
+ stem_paths.append(gr.File(value=path))
209
+ return stem_paths
210
+
211
+ # === Process Audio Function – Fully Featured ===
212
+ def process_audio(audio_file, selected_effects, isolate_vocals, preset_name, export_format):
213
+ status = "🔊 Loading audio..."
214
+ try:
215
+ # Load input audio file
216
+ audio = AudioSegment.from_file(audio_file)
217
+ status = "🛠 Applying effects..."
218
+
219
+ effect_map_real = {
220
+ "Noise Reduction": apply_noise_reduction,
221
+ "Compress Dynamic Range": apply_compression,
222
+ "Add Reverb": apply_reverb,
223
+ "Pitch Shift": lambda x: apply_pitch_shift(x),
224
+ "Echo": apply_echo,
225
+ "Stereo Widening": apply_stereo_widen,
226
+ "Bass Boost": apply_bass_boost,
227
+ "Treble Boost": apply_treble_boost,
228
+ "Normalize": apply_normalize,
229
+ "Limiter": lambda x: apply_limiter(x, limit_dB=-1),
230
+ "Auto Gain": lambda x: apply_auto_gain(x, target_dB=-20),
231
+ "Vocal Distortion": lambda x: apply_vocal_distortion(x),
232
+ "Stage Mode": apply_stage_mode
233
+ }
234
+
235
+ history = [audio] # For undo functionality
236
+ for effect_name in selected_effects:
237
+ if effect_name in effect_map_real:
238
+ audio = effect_map_real[effect_name](audio)
239
+ history.append(audio)
240
+
241
+ status = "💾 Saving final audio..."
242
+ with tempfile.NamedTemporaryFile(delete=False, suffix=f".{export_format.lower()}") as f:
243
+ if isolate_vocals:
244
+ temp_input = os.path.join(tempfile.gettempdir(), "input.wav")
245
+ audio.export(temp_input, format="wav")
246
+ vocal_path = apply_vocal_isolation(temp_input)
247
+ final_audio = AudioSegment.from_wav(vocal_path)
248
+ else:
249
+ final_audio = audio
250
+ output_path = f.name
251
+ final_audio.export(output_path, format=export_format.lower())
252
+
253
+ waveform_image = show_waveform(output_path)
254
+ genre = detect_genre(output_path)
255
+ session_log = generate_session_log(audio_file, selected_effects, isolate_vocals, export_format, genre)
256
+ status = "🎉 Done!"
257
+ return output_path, waveform_image, session_log, genre, status, history
258
+
259
+ except Exception as e:
260
+ status = f"❌ Error: {str(e)}"
261
+ return None, None, status, "", status, []
262
+
263
+ # Waveform preview
264
+ def show_waveform(audio_file):
265
+ try:
266
+ audio = AudioSegment.from_file(audio_file)
267
+ samples = np.array(audio.get_array_of_samples())
268
+ plt.figure(figsize=(10, 2))
269
+ plt.plot(samples[:10000], color="skyblue")
270
+ plt.axis("off")
271
+ buf = BytesIO()
272
+ plt.savefig(buf, format="png", bbox_inches="tight", dpi=100)
273
+ plt.close()
274
+ buf.seek(0)
275
+ return Image.open(buf)
276
+ except Exception:
277
+ return None
278
+
279
+ # Genre detection stub
280
+ def detect_genre(audio_path):
281
+ try:
282
+ y, sr = torchaudio.load(audio_path)
283
+ return "Speech"
284
+ except Exception:
285
+ return "Unknown"
286
+
287
+ # Session log generator
288
+ def generate_session_log(audio_path, effects, isolate_vocals, export_format, genre):
289
+ return json.dumps({
290
+ "timestamp": str(datetime.datetime.now()),
291
+ "filename": os.path.basename(audio_path),
292
+ "effects_applied": effects,
293
+ "isolate_vocals": isolate_vocals,
294
+ "export_format": export_format,
295
+ "detected_genre": genre
296
+ }, indent=2)
297
+
298
+ # Preset Choices (30+ options)
299
+ preset_choices = {
300
+ "Default": [],
301
+ "Clean Podcast": ["Noise Reduction", "Normalize"],
302
+ "Podcast Mastered": ["Noise Reduction", "Normalize", "Compress Dynamic Range"],
303
+ "Radio Ready": ["Bass Boost", "Treble Boost", "Limiter"],
304
+ "Music Production": ["Reverb", "Stereo Widening", "Pitch Shift"],
305
+ "ASMR Creator": ["Noise Gate", "Auto Gain", "Low-Pass Filter"],
306
+ "Voiceover Pro": ["Vocal Isolation", "EQ Match"],
307
+ "8-bit Retro": ["Bitcrusher", "Echo", "Mono Downmix"],
308
+ "🎙 Clean Vocal": ["Noise Reduction", "Normalize", "High Pass Filter (80Hz)"],
309
+ "🧪 Vocal Distortion": ["Vocal Distortion", "Reverb", "Compress Dynamic Range"],
310
+ "🎶 Singer's Harmony": ["Harmony", "Stereo Widening", "Pitch Shift"],
311
+ "🌫 ASMR Vocal": ["Auto Gain", "Low-Pass Filter (3000Hz)", "Noise Gate"],
312
+ "🎼 Stage Mode": ["Reverb", "Bass Boost", "Limiter"],
313
+ "🎵 Auto-Tune Style": ["Pitch Shift (+1 semitone)", "Normalize", "Treble Boost"],
314
+ "🎤 R&B Vocal": ["Noise Reduction", "Bass Boost (100-300Hz)", "Treble Boost (2000-4000Hz)"],
315
+ "💃 Soul Vocal": ["Noise Reduction", "Bass Boost (80-200Hz)", "Treble Boost (1500-3500Hz)"],
316
+ "🕺 Funk Groove": ["Bass Boost (80-200Hz)", "Treble Boost (1000-3000Hz)"],
317
+ "Studio Master": ["Noise Reduction", "Normalize", "Bass Boost", "Treble Boost", "Limiter"],
318
+ "Podcast Voice": ["Noise Reduction", "Auto Gain", "High Pass Filter (85Hz)"],
319
+ "Lo-Fi Chill": ["Noise Gate", "Low-Pass Filter (3000Hz)", "Mono Downmix", "Bitcrusher"],
320
+ "Vocal Clarity": ["Noise Reduction", "EQ Match", "Reverb", "Auto Gain"],
321
+ "Retro Game Sound": ["Bitcrusher", "Echo", "Mono Downmix"],
322
+ "Live Stream Optimized": ["Noise Reduction", "Auto Gain", "Saturation", "Normalize"],
323
+ "Deep Bass Trap": ["Bass Boost (60-120Hz)", "Low-Pass Filter (200Hz)", "Limiter"],
324
+ "8-bit Voice": ["Bitcrusher", "Pitch Shift (-4 semitones)", "Mono Downmix"],
325
+ "Pop Vocal": ["Noise Reduction", "Normalize", "EQ Match (Pop)", "Auto Gain"],
326
+ "EDM Lead": ["Noise Reduction", "Tape Saturation", "Stereo Widening", "Limiter"],
327
+ "Hip-Hop Beat": ["Bass Boost (60-200Hz)", "Treble Boost (7000-10000Hz)", "Compression"],
328
+ "ASMR Whisper": ["Noise Gate", "Auto Gain", "Low-Pass Filter (5000Hz)"],
329
+ "Jazz Piano Clean": ["Noise Reduction", "EQ Match (Jazz Piano)", "Normalize"],
330
+ "Metal Guitar": ["Noise Reduction", "EQ Match (Metal)", "Compression"],
331
+ "Podcast Intro": ["Echo", "Reverb", "Pitch Shift (+1 semitone)"],
332
+ "Vintage Radio": ["Bitcrusher", "Low-Pass Filter (4000Hz)", "Saturation"],
333
+ "Speech Enhancement": ["Noise Reduction", "High Pass Filter (100Hz)", "Normalize", "Auto Gain"],
334
+ "Nightcore Speed": ["Pitch Shift (+3 semitones)", "Time Stretch (1.2x)", "Treble Boost"],
335
+ "Robot Voice": ["Pitch Shift (-12 semitones)", "Bitcrusher", "Low-Pass Filter (2000Hz)"],
336
+ "Underwater Effect": ["Low-Pass Filter (1000Hz)", "Reverb", "Echo"],
337
+ "Alien Voice": ["Pitch Shift (+7 semitones)", "Tape Saturation", "Echo"],
338
+ "Cinematic Voice": ["Reverb", "Limiter", "Bass Boost", "Auto Gain"],
339
+ "Phone Call Sim": ["Low-Pass Filter (3400Hz)", "Noise Gate", "Compression"],
340
+ "AI Generated Voice": ["Pitch Shift", "Vocal Distortion"],
341
+ }
342
+
343
+ preset_names = list(preset_choices.keys())
344
+
345
+ # Batch Processing
346
+ def batch_process_audio(files, selected_effects, isolate_vocals, preset_name, export_format):
347
+ try:
348
+ output_dir = tempfile.mkdtemp()
349
+ results = []
350
+ session_logs = []
351
+ for file in files:
352
+ processed_path, _, log, _, _ = process_audio(file.name, selected_effects, isolate_vocals, preset_name, export_format)[0:5]
353
+ results.append(processed_path)
354
+ session_logs.append(log)
355
+ zip_path = os.path.join(tempfile.gettempdir(), "batch_output.zip")
356
+ with zipfile.ZipFile(zip_path, 'w') as zipf:
357
+ for i, res in enumerate(results):
358
+ filename = f"processed_{i}.{export_format.lower()}"
359
+ zipf.write(res, filename)
360
+ zipf.writestr(f"session_info_{i}.json", session_logs[i])
361
+ return zip_path, "📦 ZIP created successfully!"
362
+ except Exception as e:
363
+ return None, f"❌ Batch processing failed: {str(e)}"
364
+
365
+ # AI Remastering
366
+ def ai_remaster(audio_path):
367
+ try:
368
+ audio = AudioSegment.from_file(audio_path)
369
+ samples, sr = audiosegment_to_array(audio)
370
+ reduced = nr.reduce_noise(y=samples, sr=sr)
371
+ cleaned = array_to_audiosegment(reduced, sr, channels=audio.channels)
372
+ cleaned_wav_path = os.path.join(tempfile.gettempdir(), "cleaned.wav")
373
+ cleaned.export(cleaned_wav_path, format="wav")
374
+ isolated_path = apply_vocal_isolation(cleaned_wav_path)
375
+ final_path = ai_mastering_chain(isolated_path, genre="Pop", target_lufs=-14.0)
376
+ return final_path
377
+ except Exception as e:
378
+ print(f"Remastering Error: {str(e)}")
379
+ return None
380
+
381
+ def ai_mastering_chain(audio_path, genre="Pop", target_lufs=-14.0):
382
+ audio = AudioSegment.from_file(audio_path)
383
+ audio = auto_eq(audio, genre=genre)
384
+ audio = match_loudness(audio_path, target_lufs=target_lufs)
385
+ audio = apply_stereo_widen(audio, pan_amount=0.3)
386
+ out_path = os.path.join(tempfile.gettempdir(), "mastered_output.wav")
387
+ audio.export(out_path, format="wav")
388
+ return out_path
389
+
390
+ # Harmonic Saturation
391
+ def harmonic_saturation(audio, saturation_type="Tube", intensity=0.2):
392
+ samples = np.array(audio.get_array_of_samples()).astype(np.float32)
393
+ if saturation_type == "Tube":
394
+ saturated = np.tanh(intensity * samples)
395
+ elif saturation_type == "Tape":
396
+ saturated = np.where(samples > 0, 1 - np.exp(-intensity * samples), -1 + np.exp(intensity * samples))
397
+ elif saturation_type == "Console":
398
+ saturated = np.clip(samples, -32768, 32768) * intensity
399
+ elif saturation_type == "Mix Bus":
400
+ saturated = np.log1p(np.abs(samples)) * np.sign(samples) * intensity
401
+ else:
402
+ saturated = samples
403
+ return array_to_audiosegment(saturated.astype(np.int16), audio.frame_rate, channels=audio.channels)
404
+
405
+ # Vocal Formant Correction
406
+ def formant_correct(audio, shift=1.0):
407
+ samples, sr = audiosegment_to_array(audio)
408
+ corrected = librosa.effects.pitch_shift(samples, sr=sr, n_steps=shift)
409
+ return array_to_audiosegment(corrected.astype(np.int16), sr, channels=audio.channels)
410
+
411
+ # Voice Swap
412
+ def clone_voice(source_audio, reference_audio):
413
+ source = AudioSegment.from_file(source_audio)
414
+ ref = AudioSegment.from_file(reference_audio)
415
+ mixed = source.overlay(ref - 10)
416
+ out_path = os.path.join(tempfile.gettempdir(), "cloned_output.wav")
417
+ mixed.export(out_path, format="wav")
418
+ return out_path
419
+
420
+ # Save/Load Mix Session (.aiproj)
421
+ def save_project(audio, preset, effects):
422
+ project_data = {
423
+ "audio": AudioSegment.from_file(audio).raw_data,
424
+ "preset": preset,
425
+ "effects": effects
426
+ }
427
+ out_path = os.path.join(tempfile.gettempdir(), "project.aiproj")
428
+ with open(out_path, "wb") as f:
429
+ pickle.dump(project_data, f)
430
+ return out_path
431
+
432
+ def load_project(project_file):
433
+ with open(project_file.name, "rb") as f:
434
+ data = pickle.load(f)
435
+ return data["preset"], data["effects"]
436
 
437
+ # Prompt-Based Editing
438
+ def process_prompt(audio, prompt):
439
+ return apply_noise_reduction(audio)
440
 
441
+ # Vocal Pitch Correction
442
+ def auto_tune_vocal(audio_path, target_key="C"):
443
+ try:
444
+ audio = AudioSegment.from_file(audio_path)
445
+ semitones = key_to_semitone(target_key)
446
+ tuned_audio = apply_pitch_shift(audio, semitones)
447
+ out_path = os.path.join(tempfile.gettempdir(), "autotuned_output.wav")
448
+ tuned_audio.export(out_path, format="wav")
449
+ return out_path
450
+ except Exception as e:
451
+ print(f"Auto-Tune Error: {e}")
452
+ return None
453
 
454
+ def key_to_semitone(key="C"):
455
+ keys = {"C": 0, "C#": 1, "D": 2, "D#": 3, "E": 4, "F": 5,
456
+ "F#": 6, "G": 7, "G#": 8, "A": 9, "A#": 10, "B": 11}
457
+ return keys.get(key, 0)
458
 
459
+ # Loop Section Tool
460
+ def loop_section(audio_path, start_ms, end_ms, loops=2):
461
  audio = AudioSegment.from_file(audio_path)
462
+ section = audio[start_ms:end_ms]
463
+ looped = section * loops
464
+ out_path = os.path.join(tempfile.gettempdir(), "looped_output.wav")
465
+ looped.export(out_path, format="wav")
466
+ return out_path
467
+
468
+ # Frequency Spectrum Visualization
469
+ def visualize_spectrum(audio_path):
470
+ y, sr = torchaudio.load(audio_path)
471
+ y_np = y.numpy().flatten()
472
+ stft = librosa.stft(y_np)
473
+ db = librosa.amplitude_to_db(abs(stft))
474
+ plt.figure(figsize=(10, 4))
475
+ img = librosa.display.specshow(db, sr=sr, x_axis="time", y_axis="hz", cmap="magma")
476
+ plt.colorbar(img, format="%+2.0f dB")
477
+ plt.title("Frequency Spectrum")
478
+ plt.tight_layout()
479
+ buf = BytesIO()
480
+ plt.savefig(buf, format="png")
481
+ plt.close()
482
+ buf.seek(0)
483
+ return Image.open(buf)
484
+
485
+ # A/B Compare
486
+ def compare_ab(track1_path, track2_path):
487
+ return track1_path, track2_path
488
+
489
+ # DAW Template Export
490
+ def generate_ableton_template(stems):
491
+ template = {
492
+ "format": "Ableton Live",
493
+ "stems": [os.path.basename(s) for s in stems],
494
+ "effects": ["Reverb", "EQ", "Compression"],
495
+ "tempo": 128,
496
+ "title": "Studio Pulse Project"
497
+ }
498
+ out_path = os.path.join(tempfile.gettempdir(), "ableton_template.json")
499
+ with open(out_path, "w") as f:
500
+ json.dump(template, f, indent=2)
501
+ return out_path
502
+
503
+ # Export Full Mix ZIP
504
+ def export_full_mix(stems, final_mix):
505
+ zip_path = os.path.join(tempfile.gettempdir(), "full_export.zip")
506
+ with zipfile.ZipFile(zip_path, "w") as zipf:
507
+ for i, stem in enumerate(stems):
508
+ zipf.write(stem, f"stem_{i}.wav")
509
+ zipf.write(final_mix, "final_mix.wav")
510
+ return zip_path
511
+
512
+ # Text-to-Sound
513
+
514
+ # Main UI
515
+ with gr.Blocks(css="""
516
+ body {
517
+ font-family: 'Segoe UI', sans-serif;
518
+ background-color: #1f2937;
519
+ color: white;
520
+ padding: 20px;
521
+ }
522
+ .studio-header {
523
+ text-align: center;
524
+ margin-bottom: 30px;
525
+ animation: float 3s ease-in-out infinite;
526
+ }
527
+ @keyframes float {
528
+ 0%, 100% { transform: translateY(0); }
529
+ 50% { transform: translateY(-10px); }
530
+ }
531
+ .gr-button {
532
+ background-color: #2563eb !important;
533
+ color: white !important;
534
+ border-radius: 10px;
535
+ padding: 10px 20px;
536
+ box-shadow: 0 0 10px #2563eb44;
537
+ border: none;
538
+ }
539
+ """) as demo:
540
+ gr.HTML('''
541
+ <div class="studio-header">
542
+ <h3>Where Your Audio Meets Intelligence</h3>
543
+ </div>
544
+ ''')
545
+ gr.Markdown("### Upload, edit, export — powered by AI!")
546
+
547
+ # --- Single File Studio Tab ---
548
+ with gr.Tab("🎵 Single File Studio"):
549
+ with gr.Row():
550
+ with gr.Column(min_width=300):
551
+ input_audio = gr.Audio(label="Upload Audio", type="filepath")
552
+ effect_checkbox = gr.CheckboxGroup(choices=preset_choices["Default"], label="Apply Effects in Order")
553
+ preset_dropdown = gr.Dropdown(choices=preset_names, label="Select Preset", value=preset_names[0])
554
+ export_format = gr.Dropdown(choices=["MP3", "WAV"], label="Export Format", value="MP3")
555
+ isolate_vocals = gr.Checkbox(label="Isolate Vocals After Effects")
556
+ submit_btn = gr.Button("Process Audio")
557
+ with gr.Column(min_width=300):
558
+ output_audio = gr.Audio(label="Processed Audio", type="filepath")
559
+ waveform_img = gr.Image(label="Waveform Preview")
560
+ session_log_out = gr.Textbox(label="Session Log", lines=5)
561
+ genre_out = gr.Textbox(label="Detected Genre", lines=1)
562
+ status_box = gr.Textbox(label="Status", value="✅ Ready", lines=1)
563
+ submit_btn.click(fn=process_audio, inputs=[
564
+ input_audio, effect_checkbox, isolate_vocals, preset_dropdown, export_format
565
+ ], outputs=[
566
+ output_audio, waveform_img, session_log_out, genre_out, status_box
567
+ ])
568
+
569
+ # --- Remix Mode – Stem Splitting + Per-Stem Effects ===
570
+ with gr.Tab("🎛 Remix Mode"):
571
+ with gr.Row():
572
+ with gr.Column(min_width=200):
573
+ input_audio_remix = gr.Audio(label="Upload Music Track", type="filepath")
574
+ split_button = gr.Button("Split Into Drums, Bass, Vocals, etc.")
575
+ with gr.Column(min_width=400):
576
+ stem_outputs = [
577
+ gr.File(label="Vocals"),
578
+ gr.File(label="Drums"),
579
+ gr.File(label="Bass"),
580
+ gr.File(label="Other")
581
+ ]
582
+ split_button.click(fn=stem_split, inputs=[input_audio_remix], outputs=stem_outputs)
583
+
584
+ # --- AI Remastering Tab – Now Fixed & Working ===
585
+ with gr.Tab("🔮 AI Remastering"):
586
+ gr.Interface(
587
+ fn=ai_remaster,
588
+ inputs=gr.Audio(label="Upload Low-Quality Recording", type="filepath"),
589
+ outputs=gr.Audio(label="Studio-Grade Output", type="filepath"),
590
+ title="Transform Low-Quality Recordings to Studio Sound",
591
+ description="Uses noise reduction, vocal isolation, and mastering to enhance old recordings.",
592
+ allow_flagging="never"
593
+ )
594
+
595
+ # --- Harmonic Saturation / Exciter – Now Included ===
596
+ with gr.Tab("🧬 Harmonic Saturation"):
597
+ gr.Interface(
598
+ fn=harmonic_saturation,
599
+ inputs=[
600
+ gr.Audio(label="Upload Track", type="filepath"),
601
+ gr.Dropdown(choices=["Tube", "Tape", "Console", "Mix Bus"], label="Saturation Type", value="Tube"),
602
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.2, label="Intensity")
603
+ ],
604
+ outputs=gr.Audio(label="Warm Output", type="filepath"),
605
+ title="Add Analog-Style Warmth",
606
+ description="Enhance clarity and presence using saturation styles like Tube or Tape.",
607
+ allow_flagging="never"
608
+ )
609
+
610
+ # --- Vocal Doubler / Harmonizer – Added Back ===
611
+ with gr.Tab("🎧 Vocal Doubler / Harmonizer"):
612
+ gr.Interface(
613
+ fn=lambda x: apply_harmony(x),
614
+ inputs=gr.Audio(label="Upload Vocal Clip", type="filepath"),
615
+ outputs=gr.Audio(label="Doubled Output", type="filepath"),
616
+ title="Add Vocal Doubling / Harmony",
617
+ description="Enhance vocals with doubling or harmony"
618
+ )
619
+
620
+ # --- Batch Processing – Full Support ===
621
+ with gr.Tab("🔊 Batch Processing"):
622
+ gr.Interface(
623
+ fn=batch_process_audio,
624
+ inputs=[
625
+ gr.File(label="Upload Multiple Files", file_count="multiple"),
626
+ gr.CheckboxGroup(choices=preset_choices["Default"], label="Apply Effects in Order"),
627
+ gr.Checkbox(label="Isolate Vocals After Effects"),
628
+ gr.Dropdown(choices=preset_names, label="Select Preset", value=preset_names[0]),
629
+ gr.Dropdown(choices=["MP3", "WAV"], label="Export Format", value="MP3")
630
+ ],
631
+ outputs=[
632
+ gr.File(label="Download ZIP of All Processed Files"),
633
+ gr.Textbox(label="Status", value="✅ Ready", lines=1)
634
+ ],
635
+ title="Batch Audio Processor",
636
+ description="Upload multiple files, apply effects in bulk, and download all results in a single ZIP.",
637
+ flagging_mode="never",
638
+ submit_btn="Process All Files"
639
+ )
640
+
641
+ # --- Vocal Pitch Correction – Auto-Tune Style ===
642
+ with gr.Tab("🎤 AI Auto-Tune"):
643
+ gr.Interface(
644
+ fn=auto_tune_vocal,
645
+ inputs=[
646
+ gr.File(label="Source Voice Clip"),
647
+ gr.Textbox(label="Target Key", value="C", lines=1)
648
+ ],
649
+ outputs=gr.Audio(label="Pitch-Corrected Output", type="filepath"),
650
+ title="AI Auto-Tune",
651
+ description="Correct vocal pitch automatically using AI"
652
+ )
653
+
654
+ # --- Frequency Spectrum Tab – Real-time Visualizer ===
655
+ with gr.Tab("📊 Frequency Spectrum"):
656
+ gr.Interface(
657
+ fn=visualize_spectrum,
658
+ inputs=gr.Audio(label="Upload Track", type="filepath"),
659
+ outputs=gr.Image(label="Spectrum Analysis")
660
+ )
661
+
662
+ # --- Loudness Graph Tab – EBU R128 Matching ===
663
+ with gr.Tab("📈 Loudness Graph"):
664
+ gr.Interface(
665
+ fn=match_loudness,
666
+ inputs=[
667
+ gr.Audio(label="Upload Track", type="filepath"),
668
+ gr.Slider(minimum=-24, maximum=-6, value=-14, label="Target LUFS")
669
+ ],
670
+ outputs=gr.Audio(label="Normalized Output", type="filepath"),
671
+ title="Match Loudness Across Tracks",
672
+ description="Ensure consistent volume using EBU R128 standard"
673
+ )
674
+
675
+ # --- Save/Load Mix Session (.aiproj) – Added Back ===
676
+ with gr.Tab("📁 Save/Load Project"):
677
+ with gr.Row():
678
+ with gr.Column(min_width=300):
679
+ gr.Interface(
680
+ fn=save_project,
681
+ inputs=[
682
+ gr.File(label="Original Audio"),
683
+ gr.Dropdown(choices=preset_names, label="Used Preset", value=preset_names[0]),
684
+ gr.CheckboxGroup(choices=preset_choices["Default"], label="Applied Effects")
685
+ ],
686
+ outputs=gr.File(label="Project File (.aiproj)")
687
+ )
688
+ with gr.Column(min_width=300):
689
+ gr.Interface(
690
+ fn=load_project,
691
+ inputs=gr.File(label="Upload .aiproj File"),
692
+ outputs=[
693
+ gr.Dropdown(choices=preset_names, label="Loaded Preset"),
694
+ gr.CheckboxGroup(choices=preset_choices["Default"], label="Loaded Effects")
695
+ ],
696
+ title="Resume Last Project",
697
+ description="Load your saved session"
698
+ )
699
+
700
+ # --- Prompt-Based Editing Tab – Added Back ===
701
+ with gr.Tab("🧠 Prompt-Based Editing"):
702
+ gr.Interface(
703
+ fn=process_prompt,
704
+ inputs=[
705
+ gr.File(label="Upload Audio", type="filepath"),
706
+ gr.Textbox(label="Describe What You Want", lines=5)
707
+ ],
708
+ outputs=gr.Audio(label="Edited Output", type="filepath"),
709
+ title="Type Your Edits – AI Does the Rest",
710
+ description="Say what you want done and let AI handle it.",
711
+ allow_flagging="never"
712
+ )
713
+
714
+ # --- Custom EQ Editor ===
715
+ with gr.Tab("🎛 Custom EQ Editor"):
716
+ gr.Interface(
717
+ fn=auto_eq,
718
+ inputs=[
719
+ gr.Audio(label="Upload Track", type="filepath"),
720
+ gr.Dropdown(choices=list(eq_map.keys()), label="Genre", value="Pop")
721
+ ],
722
+ outputs=gr.Audio(label="EQ-Enhanced Output", type="filepath"),
723
+ title="Custom EQ by Genre",
724
+ description="Apply custom EQ based on genre"
725
+ )
726
+
727
+ # --- A/B Compare ===
728
+ with gr.Tab("🎯 A/B Compare"):
729
+ gr.Interface(
730
+ fn=compare_ab,
731
+ inputs=[
732
+ gr.Audio(label="Version A", type="filepath"),
733
+ gr.Audio(label="Version B", type="filepath")
734
+ ],
735
+ outputs=[
736
+ gr.Audio(label="Version A", type="filepath"),
737
+ gr.Audio(label="Version B", type="filepath")
738
+ ],
739
+ title="Compare Two Versions",
740
+ description="Hear two mixes side-by-side",
741
+ allow_flagging="never"
742
+ )
743
+
744
+ # --- Loop Playback ===
745
+ with gr.Tab("🔁 Loop Playback"):
746
+ gr.Interface(
747
+ fn=loop_section,
748
+ inputs=[
749
+ gr.Audio(label="Upload Track", type="filepath"),
750
+ gr.Slider(minimum=0, maximum=30000, step=100, value=5000, label="Start MS"),
751
+ gr.Slider(minimum=100, maximum=30000, step=100, value=10000, label="End MS"),
752
+ gr.Slider(minimum=1, maximum=10, value=2, label="Repeat Loops")
753
+ ],
754
+ outputs=gr.Audio(label="Looped Output", type="filepath"),
755
+ title="Repeat a Section",
756
+ description="Useful for editing a specific part"
757
+ )
758
+
759
+ # --- Share Effect Chain Tab – Now Defined! ===
760
+ with gr.Tab("🔗 Share Effect Chain"):
761
+ gr.Interface(
762
+ fn=lambda x: json.dumps(x),
763
+ inputs=gr.CheckboxGroup(choices=preset_choices["Default"]),
764
+ outputs=gr.Textbox(label="Share Code", lines=2),
765
+ title="Copy/Paste Effect Chain",
766
+ description="Share your setup via link/code"
767
+ )
768
+
769
+ with gr.Tab("📥 Load Shared Chain"):
770
+ gr.Interface(
771
+ fn=json.loads,
772
+ inputs=gr.Textbox(label="Paste Shared Code", lines=2),
773
+ outputs=gr.CheckboxGroup(choices=preset_choices["Default"], label="Loaded Effects"),
774
+ title="Restore From Shared Chain",
775
+ description="Paste shared effect chain JSON to restore settings"
776
+ )
777
+
778
+ # --- Keyboard Shortcuts Tab ===
779
+ with gr.Tab("⌨ Keyboard Shortcuts"):
780
+ gr.Markdown("""
781
+ ### Keyboard Controls
782
+ - `Ctrl + Z`: Undo last effect
783
+ - `Ctrl + Y`: Redo
784
+ - `Spacebar`: Play/Stop playback
785
+ - `Ctrl + S`: Save current session
786
+ - `Ctrl + O`: Open session
787
+ - `Ctrl + C`: Copy effect chain
788
+ - `Ctrl + V`: Paste effect chain
789
+ """)
790
+
791
+ # --- Vocal Formant Correction – Now Defined! ===
792
+ with gr.Tab("🧑‍🎤 Vocal Formant Correction"):
793
+ gr.Interface(
794
+ fn=formant_correct,
795
+ inputs=[
796
+ gr.Audio(label="Upload Vocal Track", type="filepath"),
797
+ gr.Slider(minimum=-2, maximum=2, value=1.0, label="Formant Shift")
798
+ ],
799
+ outputs=gr.Audio(label="Natural-Sounding Vocal", type="filepath"),
800
+ title="Preserve Vocal Quality During Pitch Shift",
801
+ description="Make pitch-shifted vocals sound more human"
802
+ )
803
+
804
+ # --- Voice Swap / Cloning – New Tab ===
805
+ with gr.Tab("🔁 Voice Swap / Cloning"):
806
+ gr.Interface(
807
+ fn=clone_voice,
808
+ inputs=[
809
+ gr.File(label="Source Voice Clip"),
810
+ gr.File(label="Reference Voice")
811
+ ],
812
+ outputs=gr.Audio(label="Converted Output", type="filepath"),
813
+ title="Swap Voices Using AI",
814
+ description="Clone or convert voice from one to another"
815
+ )
816
+
817
+ # --- DAW Template Export – Now Included ===
818
+ with gr.Tab("🎛 DAW Template Export"):
819
+ gr.Interface(
820
+ fn=generate_ableton_template,
821
+ inputs=[gr.File(label="Upload Stems", file_count="multiple")],
822
+ outputs=gr.File(label="DAW Template (.json/.als/.flp)")
823
+ )
824
+
825
+ # --- Export Full Mix ZIP – Added Back ===
826
+ with gr.Tab("📁 Export Full Mix ZIP"):
827
+ gr.Interface(
828
+ fn=export_full_mix,
829
+ inputs=[
830
+ gr.File(label="Stems", file_count="multiple"),
831
+ gr.File(label="Final Mix")
832
+ ],
833
+ outputs=gr.File(label="Full Mix Archive (.zip)"),
834
+ title="Export Stems + Final Mix Together",
835
+ description="Perfect for sharing with producers or archiving"
836
+ )
837
+
838
+ # Launch Gradio App
839
+ demo.launch()