Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,17 +6,20 @@ import os
|
|
6 |
import noisereduce as nr
|
7 |
from scipy.io import wavfile
|
8 |
import subprocess
|
9 |
-
import
|
|
|
|
|
|
|
10 |
|
11 |
# Helper functions
|
12 |
def audiosegment_to_array(audio):
|
13 |
return np.array(audio.get_array_of_samples()), audio.frame_rate
|
14 |
|
15 |
-
def array_to_audiosegment(samples, frame_rate,
|
16 |
return AudioSegment(
|
17 |
samples.tobytes(),
|
18 |
frame_rate=frame_rate,
|
19 |
-
sample_width=
|
20 |
channels=channels
|
21 |
)
|
22 |
|
@@ -66,68 +69,76 @@ def apply_bass_boost(audio, gain=10):
|
|
66 |
def apply_treble_boost(audio, gain=10):
|
67 |
return audio.high_pass_filter(4000).apply_gain(gain)
|
68 |
|
69 |
-
#
|
70 |
-
def
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
audio = AudioSegment.from_file(audio_file)
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
elif effect == "Treble Boost":
|
94 |
-
result = apply_treble_boost(audio)
|
95 |
-
elif effect == "Isolate Vocals":
|
96 |
-
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as f:
|
97 |
-
save_temp_wav(audio, f.name)
|
98 |
-
vocal_path = apply_vocal_isolation(f.name)
|
99 |
-
result = AudioSegment.from_wav(vocal_path)
|
100 |
-
else:
|
101 |
-
result = audio
|
102 |
|
103 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
return f.name
|
106 |
|
107 |
# Gradio Interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
interface = gr.Interface(
|
109 |
fn=process_audio,
|
110 |
inputs=[
|
111 |
gr.Audio(label="Upload Audio", type="filepath"),
|
112 |
-
gr.
|
113 |
-
|
114 |
-
"Normalize",
|
115 |
-
"Noise Reduction",
|
116 |
-
"Compress Dynamic Range",
|
117 |
-
"Add Reverb",
|
118 |
-
"Pitch Shift",
|
119 |
-
"Echo",
|
120 |
-
"Stereo Widening",
|
121 |
-
"Bass Boost",
|
122 |
-
"Treble Boost",
|
123 |
-
|
124 |
-
],
|
125 |
-
label="Select Effect"
|
126 |
-
)
|
127 |
],
|
128 |
outputs=gr.Audio(label="Processed Audio", type="filepath"),
|
129 |
-
title="Fix My Recording - Pro
|
130 |
-
description="Apply
|
|
|
131 |
)
|
132 |
|
133 |
interface.launch()
|
|
|
6 |
import noisereduce as nr
|
7 |
from scipy.io import wavfile
|
8 |
import subprocess
|
9 |
+
import torch
|
10 |
+
from demucs import pretrained
|
11 |
+
from demucs.apply import apply_model
|
12 |
+
from demucs.audio import load_audio, save_audio
|
13 |
|
14 |
# Helper functions
|
15 |
def audiosegment_to_array(audio):
|
16 |
return np.array(audio.get_array_of_samples()), audio.frame_rate
|
17 |
|
18 |
+
def array_to_audiosegment(samples, frame_rate, channels=1):
|
19 |
return AudioSegment(
|
20 |
samples.tobytes(),
|
21 |
frame_rate=frame_rate,
|
22 |
+
sample_width=samples.dtype.itemsize,
|
23 |
channels=channels
|
24 |
)
|
25 |
|
|
|
69 |
def apply_treble_boost(audio, gain=10):
|
70 |
return audio.high_pass_filter(4000).apply_gain(gain)
|
71 |
|
72 |
+
# Vocal Isolation using Demucs
|
73 |
+
def apply_vocal_isolation(audio_path):
|
74 |
+
model = pretrained.get_model(name='htdemucs')
|
75 |
+
wav = load_audio(audio_path)
|
76 |
+
ref = wav.mean(0)
|
77 |
+
wav -= ref[:, None]
|
78 |
+
sources = apply_model(model, wav[None])[0]
|
79 |
+
wav += ref[:, None]
|
80 |
+
vocal_track = sources[3] # index 3 = vocals
|
81 |
+
out_path = os.path.join(tempfile.gettempdir(), "vocals.wav")
|
82 |
+
save_audio(vocal_track, out_path, samplerate=model.samplerate)
|
83 |
+
return out_path
|
84 |
+
|
85 |
+
# Apply selected effects in order
|
86 |
+
def process_audio(audio_file, effects, isolate_vocals):
|
87 |
audio = AudioSegment.from_file(audio_file)
|
88 |
+
original = audio
|
89 |
+
|
90 |
+
effect_map = {
|
91 |
+
"Noise Reduction": apply_noise_reduction,
|
92 |
+
"Compress Dynamic Range": apply_compression,
|
93 |
+
"Add Reverb": apply_reverb,
|
94 |
+
"Pitch Shift": lambda x: apply_pitch_shift(x),
|
95 |
+
"Echo": apply_echo,
|
96 |
+
"Stereo Widening": apply_stereo_widen,
|
97 |
+
"Bass Boost": apply_bass_boost,
|
98 |
+
"Treble Boost": apply_treble_boost,
|
99 |
+
"Normalize": apply_normalize,
|
100 |
+
}
|
101 |
+
|
102 |
+
for effect_name in effects:
|
103 |
+
if effect_name in effect_map:
|
104 |
+
audio = effect_map[effect_name](audio)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:
|
107 |
+
if isolate_vocals:
|
108 |
+
temp_input = os.path.join(tempfile.gettempdir(), "input.wav")
|
109 |
+
audio.export(temp_input, format="wav")
|
110 |
+
vocal_path = apply_vocal_isolation(temp_input)
|
111 |
+
final_audio = AudioSegment.from_wav(vocal_path)
|
112 |
+
else:
|
113 |
+
final_audio = audio
|
114 |
+
|
115 |
+
final_audio.export(f.name, format="wav")
|
116 |
return f.name
|
117 |
|
118 |
# Gradio Interface
|
119 |
+
effect_choices = [
|
120 |
+
"Noise Reduction",
|
121 |
+
"Compress Dynamic Range",
|
122 |
+
"Add Reverb",
|
123 |
+
"Pitch Shift",
|
124 |
+
"Echo",
|
125 |
+
"Stereo Widening",
|
126 |
+
"Bass Boost",
|
127 |
+
"Treble Boost",
|
128 |
+
"Normalize"
|
129 |
+
]
|
130 |
+
|
131 |
interface = gr.Interface(
|
132 |
fn=process_audio,
|
133 |
inputs=[
|
134 |
gr.Audio(label="Upload Audio", type="filepath"),
|
135 |
+
gr.CheckboxGroup(choices=effect_choices, label="Apply Effects in Order"),
|
136 |
+
gr.Checkbox(label="Isolate Vocals After Effects")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
],
|
138 |
outputs=gr.Audio(label="Processed Audio", type="filepath"),
|
139 |
+
title="Fix My Recording - Studio Pro",
|
140 |
+
description="Apply multiple effects in sequence and optionally isolate vocals!",
|
141 |
+
allow_flagging="never"
|
142 |
)
|
143 |
|
144 |
interface.launch()
|