thasri commited on
Commit
6a30923
·
1 Parent(s): f7ec484

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +118 -0
main.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import librosa
3
+ import numpy as np
4
+ from spleeter.separator import Separator
5
+ import threading
6
+ import queue
7
+ import matplotlib.pyplot as plt
8
+ import soundfile as sf
9
+
10
+ # Load the song
11
+ def load_audio(file_path):
12
+ signal, sr = librosa.load(file_path, sr=None)
13
+ return signal, sr
14
+
15
+ # Spleeter separation
16
+ def spleeter_separate(audio, sr):
17
+ separator = Separator('spleeter:5stems')
18
+ prediction = separator.separate(audio)
19
+ return prediction['vocals'], prediction['accompaniment'], prediction['bass'], prediction['drums'], prediction['other']
20
+
21
+ # Function to control the volume of each stem
22
+ def adjust_volume(stems, volumes):
23
+ adjusted_stems = []
24
+ for stem, volume in zip(stems, volumes):
25
+ adjusted_stem = stem * volume
26
+ adjusted_stems.append(adjusted_stem)
27
+ return adjusted_stems
28
+
29
+ # Function to handle the separation and volume adjustment
30
+ def process_audio(file_path, volumes, result_queue):
31
+ audio, sr = load_audio(file_path)
32
+ stems = spleeter_separate(audio, sr)
33
+ adjusted_stems = adjust_volume(stems, volumes)
34
+ result_queue.put(adjusted_stems)
35
+
36
+ # Multithreaded processing
37
+ def process_audio_threaded(file_path, volumes, result_queue):
38
+ thread = threading.Thread(target=process_audio, args=(file_path, volumes, result_queue))
39
+ thread.start()
40
+
41
+ # Gradio interface
42
+ def separate_audio(file, vocals, accompaniment, bass, drums, other):
43
+ file_path = file.name
44
+
45
+ volumes = [vocals, accompaniment, bass, drums, other]
46
+
47
+ result_queue = queue.Queue()
48
+ process_audio_threaded(file_path, volumes, result_queue)
49
+ adjusted_stems = result_queue.get()
50
+
51
+ # Reconstruct the audio with adjusted stems
52
+ reconstructed_audio = sum(adjusted_stems)
53
+
54
+ return reconstructed_audio.astype(np.float32)
55
+
56
+ # Plot waveform and spectrogram
57
+ def plot_audio(signal, sr, title):
58
+ plt.figure(figsize=(10, 6))
59
+
60
+ # Waveform plot
61
+ plt.subplot(2, 1, 1)
62
+ librosa.display.waveplot(signal, sr=sr)
63
+ plt.title('Waveform')
64
+
65
+ # Spectrogram plot
66
+ plt.subplot(2, 1, 2)
67
+ plt.specgram(signal, NFFT=2048, Fs=2, Fc=0, noverlap=128, cmap='inferno', sides='default', mode='default', scale='dB')
68
+ plt.title('Spectrogram')
69
+
70
+ plt.tight_layout()
71
+ plt.suptitle(title, fontsize=14)
72
+ plt.show()
73
+
74
+ iface = gr.Interface(
75
+ fn=separate_audio,
76
+ inputs=[
77
+ gr.inputs.Audio(label="Audio file"),
78
+ gr.inputs.Slider(0.0, 1.0, step=0.1, label="Vocals"),
79
+ gr.inputs.Slider(0.0, 1.0, step=0.1, label="Accompaniment"),
80
+ gr.inputs.Slider(0.0, 1.0, step=0.1, label="Bass"),
81
+ gr.inputs.Slider(0.0, 1.0, step=0.1, label="Drums"),
82
+ gr.inputs.Slider(0.0, 1.0, step=0.1, label="Other")
83
+ ],
84
+ outputs=gr.outputs.Audio(type="numpy", label="Separated Audio"),
85
+ title="Song Stem Separation",
86
+ description="Isolate vocals, accompaniment, bass, and drums of any song using the Spleeter model.",
87
+ )
88
+
89
+ # Add visualizations and support for different audio formats
90
+ def visualize_and_save_audio(inputs, output):
91
+ audio_file = inputs[0]
92
+ vocals = inputs[1]
93
+ accompaniment = inputs[2]
94
+ bass = inputs[3]
95
+ drums = inputs[4]
96
+ other = inputs[5]
97
+
98
+ # Load the original audio file
99
+ signal, sr = librosa.load(audio_file.name, sr=None)
100
+
101
+ # Plot waveform and spectrogram of the original audio
102
+ plot_audio(signal, sr, "Original Audio")
103
+
104
+ # Save the separated audio to a file
105
+ output_file = "separated_audio.wav"
106
+ sf.write(output_file, output.astype(np.float32), sr)
107
+
108
+ # Load the separated audio file
109
+ separated_signal, separated_sr = librosa.load(output_file, sr=None)
110
+
111
+ # Plot waveform and spectrogram of the separated audio
112
+ plot_audio(separated_signal, separated_sr, "Separated Audio")
113
+
114
+ # Provide feedback to the user
115
+ print("Audio separated successfully! Separated audio saved to 'separated_audio.wav'.")
116
+
117
+ iface.launch()
118
+