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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
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
import soundfile as SF

print("Gradio version:", gr.__version__)
warnings.filterwarnings("ignore")

# Helper to convert file to base64
def file_to_base64_audio(file_path, mime_type="audio/wav"):
    with open(file_path, "rb") as f:
        data = f.read()
    b64 = base64.b64encode(data).decode()
    return f"data:{mime_type};base64,{b64}"

# === Effects Definitions ===
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_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)

# === 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=int(frame_rate),
        sample_width=samples.dtype.itemsize,
        channels=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

# Define eq_map at the global scope
eq_map = {
    "Pop": [(200, 500, -3), (2000, 4000, +4)],
    "EDM": [(60, 250, +6), (8000, 12000, +3)],
    "Rock": [(1000, 3000, +4), (7000, 10000, -3)],
    "Hip-Hop": [(20, 100, +6), (7000, 10000, -4)],
    "Acoustic": [(100, 300, -3), (4000, 8000, +2)],
    "Metal": [(100, 500, -4), (2000, 5000, +6), (7000, 12000, -3)],
    "Trap": [(80, 120, +6), (3000, 6000, -4)],
    "LoFi": [(20, 200, +3), (1000, 3000, -2)],
    "Jazz": [(100, 400, +2), (1500, 3000, +1)],
    "Classical": [(200, 1000, +1), (3000, 6000, +2)],
    "Chillhop": [(50, 200, +3), (2000, 5000, +1)],
    "Ambient": [(100, 500, +4), (6000, 12000, +2)],
    "Jazz Piano": [(100, 1000, +3), (2000, 5000, +2)],
    "Trap EDM": [(60, 120, +6), (2000, 5000, -3)],
    "Indie Rock": [(150, 400, +2), (2000, 5000, +3)],
    "Lo-Fi Jazz": [(80, 200, +3), (2000, 4000, -1)],
    "R&B": [(100, 300, +4), (2000, 4000, +3)],
    "Soul": [(80, 200, +3), (1500, 3500, +4)],
    "Funk": [(80, 200, +5), (1000, 3000, +3)],
    "Default": []
}

# Auto-EQ per Genre function
def auto_eq(audio, genre="Pop"):
    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)

    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)

# === Load Track 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)

# === Vocal Isolation Helpers ===
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 Function ===
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:
        # Load input audio file
        audio = AudioSegment.from_file(audio_file)
        status = "๐Ÿ›  Applying effects..."

        effect_map_real = {
            "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,
            "Limiter": lambda x: apply_limiter(x, limit_dB=-1),
            "Auto Gain": lambda x: apply_auto_gain(x, target_dB=-20),
            "Vocal Distortion": lambda x: apply_vocal_distortion(x),
            "Stage Mode": apply_stage_mode
        }

        history = [audio]  # For undo functionality
        for effect_name in selected_effects:
            if effect_name in effect_map_real:
                audio = effect_map_real[effect_name](audio)
                history.append(audio)

        status = "๐Ÿ’พ Saving final audio..."
        with tempfile.NamedTemporaryFile(delete=False, suffix=f".{export_format.lower()}") 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, selected_effects, isolate_vocals, export_format, genre)
        status = "๐ŸŽ‰ Done!"
        return output_path, waveform_image, session_log, genre, status, history

    except Exception as e:
        status = f"โŒ Error: {str(e)}"
        return None, None, status, "", status, []

# Waveform preview
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="skyblue")
        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:
        return None

# Genre detection stub
def detect_genre(audio_path):
    try:
        y, sr = torchaudio.load(audio_path)
        return "Speech"
    except Exception:
        return "Unknown"

# Session log generator
def generate_session_log(audio_path, effects, isolate_vocals, export_format, genre):
    return json.dumps({
        "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
    }, indent=2)

# Preset Choices (30+ options)
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", "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"],
    "๐ŸŽค 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)"],
    "Studio Master": ["Noise Reduction", "Normalize", "Bass Boost", "Treble Boost", "Limiter"],
    "Podcast Voice": ["Noise Reduction", "Auto Gain", "High Pass Filter (85Hz)"],
    "Lo-Fi Chill": ["Noise Gate", "Low-Pass Filter (3000Hz)", "Mono Downmix", "Bitcrusher"],
    "Vocal Clarity": ["Noise Reduction", "EQ Match", "Reverb", "Auto Gain"],
    "Retro Game Sound": ["Bitcrusher", "Echo", "Mono Downmix"],
    "Live Stream Optimized": ["Noise Reduction", "Auto Gain", "Saturation", "Normalize"],
    "Deep Bass Trap": ["Bass Boost (60-120Hz)", "Low-Pass Filter (200Hz)", "Limiter"],
    "8-bit Voice": ["Bitcrusher", "Pitch Shift (-4 semitones)", "Mono Downmix"],
    "Pop Vocal": ["Noise Reduction", "Normalize", "EQ Match (Pop)", "Auto Gain"],
    "EDM Lead": ["Noise Reduction", "Tape Saturation", "Stereo Widening", "Limiter"],
    "Hip-Hop Beat": ["Bass Boost (60-200Hz)", "Treble Boost (7000-10000Hz)", "Compression"],
    "ASMR Whisper": ["Noise Gate", "Auto Gain", "Low-Pass Filter (5000Hz)"],
    "Jazz Piano Clean": ["Noise Reduction", "EQ Match (Jazz Piano)", "Normalize"],
    "Metal Guitar": ["Noise Reduction", "EQ Match (Metal)", "Compression"],
    "Podcast Intro": ["Echo", "Reverb", "Pitch Shift (+1 semitone)"],
    "Vintage Radio": ["Bitcrusher", "Low-Pass Filter (4000Hz)", "Saturation"],
    "Speech Enhancement": ["Noise Reduction", "High Pass Filter (100Hz)", "Normalize", "Auto Gain"],
    "Nightcore Speed": ["Pitch Shift (+3 semitones)", "Time Stretch (1.2x)", "Treble Boost"],
    "Robot Voice": ["Pitch Shift (-12 semitones)", "Bitcrusher", "Low-Pass Filter (2000Hz)"],
    "Underwater Effect": ["Low-Pass Filter (1000Hz)", "Reverb", "Echo"],
    "Alien Voice": ["Pitch Shift (+7 semitones)", "Tape Saturation", "Echo"],
    "Cinematic Voice": ["Reverb", "Limiter", "Bass Boost", "Auto Gain"],
    "Phone Call Sim": ["Low-Pass Filter (3400Hz)", "Noise Gate", "Compression"],
    "AI Generated Voice": ["Pitch Shift", "Vocal Distortion"],
}

preset_names = list(preset_choices.keys())

# Batch Processing
def batch_process_audio(files, selected_effects, isolate_vocals, preset_name, export_format):
    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)[0:5]
            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)}"

# AI Remastering
def ai_remaster(audio_path):
    try:
        audio = AudioSegment.from_file(audio_path)
        samples, sr = audiosegment_to_array(audio)
        reduced = nr.reduce_noise(y=samples, sr=sr)
        cleaned = array_to_audiosegment(reduced, sr, channels=audio.channels)
        cleaned_wav_path = os.path.join(tempfile.gettempdir(), "cleaned.wav")
        cleaned.export(cleaned_wav_path, format="wav")
        isolated_path = apply_vocal_isolation(cleaned_wav_path)
        final_path = ai_mastering_chain(isolated_path, genre="Pop", target_lufs=-14.0)
        return final_path
    except Exception as e:
        print(f"Remastering Error: {str(e)}")
        return None

def ai_mastering_chain(audio_path, genre="Pop", target_lufs=-14.0):
    audio = AudioSegment.from_file(audio_path)
    audio = auto_eq(audio, genre=genre)
    audio = match_loudness(audio_path, target_lufs=target_lufs)
    audio = apply_stereo_widen(audio, pan_amount=0.3)
    out_path = os.path.join(tempfile.gettempdir(), "mastered_output.wav")
    audio.export(out_path, format="wav")
    return out_path

# Harmonic Saturation
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 Formant Correction
def formant_correct(audio, shift=1.0):
    samples, sr = audiosegment_to_array(audio)
    corrected = librosa.effects.pitch_shift(samples, sr=sr, n_steps=shift)
    return array_to_audiosegment(corrected.astype(np.int16), sr, channels=audio.channels)

# Voice Swap
def clone_voice(source_audio, reference_audio):
    source = AudioSegment.from_file(source_audio)
    ref = AudioSegment.from_file(reference_audio)
    mixed = source.overlay(ref - 10)
    out_path = os.path.join(tempfile.gettempdir(), "cloned_output.wav")
    mixed.export(out_path, format="wav")
    return out_path

# Save/Load Mix Session (.aiproj)
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 data["preset"], data["effects"]

# Prompt-Based Editing
def process_prompt(audio, prompt):
    return apply_noise_reduction(audio)

# Vocal Pitch Correction
def auto_tune_vocal(audio_path, target_key="C"):
    try:
        audio = AudioSegment.from_file(audio_path)
        semitones = key_to_semitone(target_key)
        tuned_audio = apply_pitch_shift(audio, semitones)
        out_path = os.path.join(tempfile.gettempdir(), "autotuned_output.wav")
        tuned_audio.export(out_path, format="wav")
        return out_path
    except Exception as e:
        print(f"Auto-Tune Error: {e}")
        return None

def key_to_semitone(key="C"):
    keys = {"C": 0, "C#": 1, "D": 2, "D#": 3, "E": 4, "F": 5,
            "F#": 6, "G": 7, "G#": 8, "A": 9, "A#": 10, "B": 11}
    return keys.get(key, 0)

# Loop Section Tool
def loop_section(audio_path, start_ms, end_ms, loops=2):
    audio = AudioSegment.from_file(audio_path)
    section = audio[start_ms:end_ms]
    looped = section * loops
    out_path = os.path.join(tempfile.gettempdir(), "looped_output.wav")
    looped.export(out_path, format="wav")
    return out_path

# Frequency Spectrum Visualization
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)

# A/B Compare
def compare_ab(track1_path, track2_path):
    return track1_path, track2_path

# DAW Template Export
def generate_ableton_template(stems):
    template = {
        "format": "Ableton Live",
        "stems": [os.path.basename(s) for s in stems],
        "effects": ["Reverb", "EQ", "Compression"],
        "tempo": 128,
        "title": "Studio Pulse Project"
    }
    out_path = os.path.join(tempfile.gettempdir(), "ableton_template.json")
    with open(out_path, "w") as f:
        json.dump(template, f, indent=2)
    return out_path

# Export Full Mix ZIP
def export_full_mix(stems, final_mix):
    zip_path = os.path.join(tempfile.gettempdir(), "full_export.zip")
    with zipfile.ZipFile(zip_path, "w") as zipf:
        for i, stem in enumerate(stems):
            zipf.write(stem, f"stem_{i}.wav")
        zipf.write(final_mix, "final_mix.wav")
    return zip_path

# Text-to-Sound

# Main UI
with gr.Blocks(css="""
    body {
        font-family: 'Segoe UI', sans-serif;
        background-color: #1f2937;
        color: white;
        padding: 20px;
    }
    .studio-header {
        text-align: center;
        margin-bottom: 30px;
        animation: float 3s ease-in-out infinite;
    }
    @keyframes float {
        0%, 100% { transform: translateY(0); }
        50% { transform: translateY(-10px); }
    }
    .gr-button {
        background-color: #2563eb !important;
        color: white !important;
        border-radius: 10px;
        padding: 10px 20px;
        box-shadow: 0 0 10px #2563eb44;
        border: none;
    }
""") as demo:
    gr.HTML('''
    <div class="studio-header">
        <h3>Where Your Audio Meets Intelligence</h3>
    </div>
    ''')
    gr.Markdown("### Upload, edit, export โ€” powered by AI!")

    # --- Single File Studio Tab ---
    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
        ])

    # --- Remix Mode โ€“ Stem Splitting + Per-Stem Effects ===
    with gr.Tab("๐ŸŽ› Remix Mode"):
        with gr.Row():
            with gr.Column(min_width=200):
                input_audio_remix = gr.Audio(label="Upload Music Track", type="filepath")
                split_button = gr.Button("Split Into Drums, Bass, Vocals, etc.")
            with gr.Column(min_width=400):
                stem_outputs = [
                    gr.File(label="Vocals"),
                    gr.File(label="Drums"),
                    gr.File(label="Bass"),
                    gr.File(label="Other")
                ]
        split_button.click(fn=stem_split, inputs=[input_audio_remix], outputs=stem_outputs)

    # --- AI Remastering Tab โ€“ Now Fixed & Working ===
    with gr.Tab("๐Ÿ”ฎ AI Remastering"):
        gr.Interface(
            fn=ai_remaster,
            inputs=gr.Audio(label="Upload Low-Quality Recording", type="filepath"),
            outputs=gr.Audio(label="Studio-Grade Output", type="filepath"),
            title="Transform Low-Quality Recordings to Studio Sound",
            description="Uses noise reduction, vocal isolation, and mastering to enhance old recordings.",
            allow_flagging="never"
        )

    # --- Harmonic Saturation / Exciter โ€“ Now Included ===
    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.",
            allow_flagging="never"
        )

    # --- Vocal Doubler / Harmonizer โ€“ Added Back ===
    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"
        )

    # --- Vocal Pitch Correction โ€“ Auto-Tune Style ===
    with gr.Tab("๐ŸŽค AI Auto-Tune"):
        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="AI Auto-Tune",
            description="Correct vocal pitch automatically using AI"
        )

    # --- Frequency Spectrum Tab โ€“ Real-time Visualizer ===
    with gr.Tab("๐Ÿ“Š Frequency Spectrum"):
        gr.Interface(
            fn=visualize_spectrum,
            inputs=gr.Audio(label="Upload Track", type="filepath"),
            outputs=gr.Image(label="Spectrum Analysis")
        )

    # --- Loudness Graph Tab โ€“ EBU R128 Matching ===
    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 ===
    with gr.Tab("๐Ÿ“ Save/Load Project"):
        with gr.Row():
            with gr.Column(min_width=300):
                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)")
                )
            with gr.Column(min_width=300):
                gr.Interface(
                    fn=load_project,
                    inputs=gr.File(label="Upload .aiproj File"),
                    outputs=[
                        gr.Dropdown(choices=preset_names, label="Loaded Preset"),
                        gr.CheckboxGroup(choices=preset_choices["Default"], label="Loaded Effects")
                    ],
                    title="Resume Last Project",
                    description="Load your saved session"
                )

    # --- Prompt-Based Editing Tab โ€“ Added Back ===
    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"
        )

    # --- Custom EQ Editor ===
    with gr.Tab("๐ŸŽ› Custom EQ Editor"):
        gr.Interface(
            fn=auto_eq,
            inputs=[
                gr.Audio(label="Upload Track", type="filepath"),
                gr.Dropdown(choices=list(eq_map.keys()), label="Genre", value="Pop")
            ],
            outputs=gr.Audio(label="EQ-Enhanced Output", type="filepath"),
            title="Custom EQ by Genre",
            description="Apply custom EQ based on genre"
        )

    # --- A/B Compare ===
    with gr.Tab("๐ŸŽฏ A/B Compare"):
        gr.Interface(
            fn=compare_ab,
            inputs=[
                gr.Audio(label="Version A", type="filepath"),
                gr.Audio(label="Version B", type="filepath")
            ],
            outputs=[
                gr.Audio(label="Version A", type="filepath"),
                gr.Audio(label="Version B", type="filepath")
            ],
            title="Compare Two Versions",
            description="Hear two mixes side-by-side",
            allow_flagging="never"
        )

    # --- Loop Playback ===
    with gr.Tab("๐Ÿ” Loop Playback"):
        gr.Interface(
            fn=loop_section,
            inputs=[
                gr.Audio(label="Upload Track", type="filepath"),
                gr.Slider(minimum=0, maximum=30000, step=100, value=5000, label="Start MS"),
                gr.Slider(minimum=100, maximum=30000, step=100, value=10000, label="End MS"),
                gr.Slider(minimum=1, maximum=10, value=2, label="Repeat Loops")
            ],
            outputs=gr.Audio(label="Looped Output", type="filepath"),
            title="Repeat a Section",
            description="Useful for editing a specific part"
        )

    # --- Share Effect Chain Tab โ€“ Now Defined! ===
    with gr.Tab("๐Ÿ”— Share Effect Chain"):
        gr.Interface(
            fn=lambda x: json.dumps(x),
            inputs=gr.CheckboxGroup(choices=preset_choices["Default"]),
            outputs=gr.Textbox(label="Share Code", lines=2),
            title="Copy/Paste Effect Chain",
            description="Share your setup via link/code"
        )

    with gr.Tab("๐Ÿ“ฅ Load Shared Chain"):
        gr.Interface(
            fn=json.loads,
            inputs=gr.Textbox(label="Paste Shared Code", lines=2),
            outputs=gr.CheckboxGroup(choices=preset_choices["Default"], label="Loaded Effects"),
            title="Restore From Shared Chain",
            description="Paste shared effect chain JSON to restore settings"
        )

    # --- Keyboard Shortcuts Tab ===
    with gr.Tab("โŒจ Keyboard Shortcuts"):
        gr.Markdown("""
        ### Keyboard Controls  
        - `Ctrl + Z`: Undo last effect  
        - `Ctrl + Y`: Redo  
        - `Spacebar`: Play/Stop playback  
        - `Ctrl + S`: Save current session  
        - `Ctrl + O`: Open session  
        - `Ctrl + C`: Copy effect chain  
        - `Ctrl + V`: Paste effect chain  
        """)

    # --- Vocal Formant Correction โ€“ Now Defined! ===
    with gr.Tab("๐Ÿง‘โ€๐ŸŽค Vocal Formant Correction"):
        gr.Interface(
            fn=formant_correct,
            inputs=[
                gr.Audio(label="Upload Vocal Track", type="filepath"),
                gr.Slider(minimum=-2, maximum=2, value=1.0, label="Formant Shift")
            ],
            outputs=gr.Audio(label="Natural-Sounding Vocal", type="filepath"),
            title="Preserve Vocal Quality During Pitch Shift",
            description="Make pitch-shifted vocals sound more human"
        )

    # --- Voice Swap / Cloning โ€“ New Tab ===
    with gr.Tab("๐Ÿ” Voice Swap / Cloning"):
        gr.Interface(
            fn=clone_voice,
            inputs=[
                gr.File(label="Source Voice Clip"),
                gr.File(label="Reference Voice")
            ],
            outputs=gr.Audio(label="Converted Output", type="filepath"),
            title="Swap Voices Using AI",
            description="Clone or convert voice from one to another"
        )

    # --- DAW Template Export โ€“ Now Included ===
    with gr.Tab("๐ŸŽ› DAW Template Export"):
        gr.Interface(
            fn=generate_ableton_template,
            inputs=[gr.File(label="Upload Stems", file_count="multiple")],
            outputs=gr.File(label="DAW Template (.json/.als/.flp)")
        )

    # --- Export Full Mix ZIP โ€“ Added Back ===
    with gr.Tab("๐Ÿ“ Export Full Mix ZIP"):
        gr.Interface(
            fn=export_full_mix,
            inputs=[
                gr.File(label="Stems", file_count="multiple"),
                gr.File(label="Final Mix")
            ],
            outputs=gr.File(label="Full Mix Archive (.zip)"),
            title="Export Stems + Final Mix Together",
            description="Perfect for sharing with producers or archiving"
        )

# Launch Gradio App
demo.launch()


# === Hugging Face API Integration ===
def hf_api_process(audio_data_url, effects_json, isolate, preset, export_format):
    try:
        import base64, tempfile, json
        from pydub import AudioSegment
        header, base64_data = audio_data_url.split(",", 1)
        audio_bytes = base64.b64decode(base64_data)
        suffix = ".mp3" if "mpeg" in header else ".wav"
        with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as f:
            f.write(audio_bytes)
            input_path = f.name
        effects = json.loads(effects_json) if isinstance(effects_json, str) else effects_json
        output_path, *_ = process_audio(input_path, effects, isolate, preset, export_format)
        with open(output_path, "rb") as f:
            out_b64 = base64.b64encode(f.read()).decode("utf-8")
        mime = "audio/wav" if export_format.lower() == "wav" else "audio/mpeg"
        return f"data:{mime};base64,{out_b64}"
    except Exception as e:
        return f"Error: {str(e)}"

# Add standalone API interface for Hugging Face to access
gr.Interface(
    fn=hf_api_process,
    inputs=[
        gr.Text(label="Audio Base64 Data URL"),
        gr.Textbox(label="Effects (JSON)"),
        gr.Checkbox(label="Isolate Vocals"),
        gr.Textbox(label="Preset"),
        gr.Textbox(label="Export Format")
    ],
    outputs=gr.Text(label="Processed Audio as Base64 URL"),
    allow_flagging="never"
).launch(inline=False, share=False)


# === Hugging Face API Integration ===
def hf_api_process(audio_data_url, effects_json, isolate, preset, export_format):
    try:
        import base64, tempfile, json
        from pydub import AudioSegment
        header, base64_data = audio_data_url.split(",", 1)
        audio_bytes = base64.b64decode(base64_data)
        suffix = ".mp3" if "mpeg" in header else ".wav"
        with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as f:
            f.write(audio_bytes)
            input_path = f.name
        effects = json.loads(effects_json) if isinstance(effects_json, str) else effects_json
        output_path, *_ = process_audio(input_path, effects, isolate, preset, export_format)
        with open(output_path, "rb") as f:
            out_b64 = base64.b64encode(f.read()).decode("utf-8")
        mime = "audio/wav" if export_format.lower() == "wav" else "audio/mpeg"
        return f"data:{mime};base64,{out_b64}"
    except Exception as e:
        return f"Error: {str(e)}"

# Add standalone API interface for Hugging Face to access
gr.Interface(
    fn=hf_api_process,
    inputs=[
        gr.Text(label="Audio Base64 Data URL"),
        gr.Textbox(label="Effects (JSON)"),
        gr.Checkbox(label="Isolate Vocals"),
        gr.Textbox(label="Preset"),
        gr.Textbox(label="Export Format")
    ],
    outputs=gr.Text(label="Processed Audio as Base64 URL"),
    allow_flagging="never"



).launch(inline=False, share=False)