ageraustine commited on
Commit
86b5b34
1 Parent(s): 38546ed

mixings with scipy

Browse files
Files changed (1) hide show
  1. app.py +112 -53
app.py CHANGED
@@ -4,6 +4,11 @@ import numpy as np
4
  import os
5
  import base64
6
  from streamlit import session_state as st_state
 
 
 
 
 
7
 
8
  # Try to get API_URL from environment variables, if not found set to a default value
9
  try:
@@ -33,7 +38,15 @@ if 'augmented_audio' not in st_state:
33
 
34
  # Streamlit app title
35
  st.title("Songlabai")
36
-
 
 
 
 
 
 
 
 
37
  uploaded_file = st.file_uploader("Upload Music File", type=["mp3", "wav", "ogg", "flac", "aac"])
38
 
39
  genres = [
@@ -67,61 +80,107 @@ if st.button("Generate Audio"):
67
  sample_rate = response.json()[0]['sample_rate']
68
  st.audio(st_state.audio, format="audio/wav", sample_rate=sample_rate, start_time=0)
69
 
 
 
 
70
  # Post-processing options
71
  st.header("Post-processing Options")
72
- apply_stereo = st.checkbox("Apply Stereo Effect")
73
- reverse = st.checkbox("Reverse Audio")
74
- change_speed = st.checkbox("Change Speed")
75
- if change_speed:
76
- speed_factor = st.slider("Speed Factor:", min_value=0.1, max_value=2.0, value=1.0, step=0.1)
77
-
78
- # Pitch shifting
79
- st.header("Pitch Shifting")
80
- pitch_shift = st.checkbox("Pitch Shift")
81
- if pitch_shift:
82
- pitch_semitones = st.slider("Pitch (semitones):", min_value=-12, max_value=12, value=0, step=1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
 
84
  # Reverb
85
- st.header("Reverb")
86
- add_reverb = st.checkbox("Add Reverb")
87
- if add_reverb:
88
- reverb_room_scale = st.slider("Room Scale:", min_value=0.0, max_value=100.0, value=50.0)
89
- reverb_damping = st.slider("Damping:", min_value=0.0, max_value=100.0, value=50.0)
90
- reverb_wet_only = st.checkbox("Wet Only", value=False)
91
-
92
- # Apply selected post-processing
93
- if apply_stereo or reverse or change_speed or pitch_shift or add_reverb:
94
- st.text("Applying post-processing...")
95
-
96
- # Apply selected post-processing
97
- st_state.augmented_audio = st_state.audio.copy()
98
-
99
- if apply_stereo:
100
- # Create a stereo effect by duplicating the audio and panning left and right
101
- audio_left = st_state.augmented_audio
102
- audio_right = st_state.augmented_audio
103
- st_state.augmented_audio = np.stack([audio_left, audio_right], axis=-1)
104
-
105
- if reverse:
106
- # Reverse the audio array
107
- st_state.augmented_audio = np.flip(st_state.augmented_audio)
108
-
109
- if change_speed:
110
- # Change the speed by resampling the audio
111
- new_sample_rate = int(sample_rate * speed_factor)
112
- st_state.augmented_audio = scipy.signal.resample(st_state.augmented_audio, int(len(st_state.augmented_audio) * speed_factor))
113
-
114
- if pitch_shift:
115
- # Pitch shift using the Fourier shift method
116
- pitch_shift_factor = 2 ** (pitch_semitones / 12)
117
- st_state.augmented_audio = scipy.signal.resample(st_state.augmented_audio, int(len(st_state.augmented_audio) / pitch_shift_factor))
118
-
119
- if add_reverb:
120
- # Apply reverb using the convolution method
121
- reverb_ir = scipy.signal.exponential(reverb_room_scale, reverb_damping, reverb_wet_only)
122
- st_state.augmented_audio = scipy.signal.fftconvolve(st_state.augmented_audio, reverb_ir)
123
-
124
- # Play the processed audio
125
  st.audio(st_state.augmented_audio, format="audio/wav", sample_rate=sample_rate, start_time=0)
126
-
127
  st.link_button("Download/Save", "https://songlabai.com/subcribe/")
 
4
  import os
5
  import base64
6
  from streamlit import session_state as st_state
7
+ import librosa
8
+ import soundfile as sf
9
+ from scipy.signal import butter, sosfilt
10
+
11
+
12
 
13
  # Try to get API_URL from environment variables, if not found set to a default value
14
  try:
 
38
 
39
  # Streamlit app title
40
  st.title("Songlabai")
41
+
42
+ # Initialize session state variables
43
+ if 'vocal_audio' not in st_state:
44
+ st_state.vocal_audio = None
45
+ if 'audio' not in st_state:
46
+ st_state.audio = None
47
+ if 'augmented_audio' not in st_state:
48
+ st_state.augmented_audio = None
49
+
50
  uploaded_file = st.file_uploader("Upload Music File", type=["mp3", "wav", "ogg", "flac", "aac"])
51
 
52
  genres = [
 
80
  sample_rate = response.json()[0]['sample_rate']
81
  st.audio(st_state.audio, format="audio/wav", sample_rate=sample_rate, start_time=0)
82
 
83
+
84
+ vocal_file = st.file_uploader("Upload Vocal File", type=["mp3", "wav", "ogg", "flac", "aac"])
85
+
86
  # Post-processing options
87
  st.header("Post-processing Options")
88
+
89
+ # Mixing
90
+ mix_vocals = st.checkbox("Mix Vocals")
91
+ if mix_vocals and st_state.vocal_audio is not None:
92
+ # Load the vocal audio
93
+ vocal_audio, _ = librosa.load(vocal_file, sr=sample_rate, mono=False)
94
+
95
+ # Adjust the vocal audio length to match the generated audio
96
+ vocal_audio = librosa.util.fix_length(vocal_audio, len(st_state.audio))
97
+
98
+ # Mix the vocal audio with the generated audio
99
+ st_state.augmented_audio = (st_state.audio + vocal_audio) / 2
100
+
101
+ # Mastering
102
+ st.subheader("Mastering")
103
+
104
+ # Volume Balance
105
+ volume_balance = st.slider("Volume Balance", min_value=-10.0, max_value=10.0, value=0.0, step=0.1)
106
+ if st.button("Apply Volume Balance"):
107
+ if st_state.augmented_audio is None:
108
+ st_state.augmented_audio = st_state.audio
109
+ st_state.augmented_audio *= 10 ** (volume_balance / 20)
110
+
111
+ # Compression
112
+ compression_ratio = st.slider("Compression Ratio", min_value=1.0, max_value=10.0, value=3.0, step=0.1)
113
+ if st.button("Apply Compression"):
114
+ if st_state.augmented_audio is None:
115
+ st_state.augmented_audio = st_state.audio
116
+
117
+ # Apply compression using a simple soft-knee compressor
118
+ threshold = -20 # dBFS
119
+ ratio = compression_ratio
120
+ knee = 10 # dB
121
+ max_gain = 20 # dB
122
+
123
+ def compress(x, threshold, ratio, knee, max_gain):
124
+ over = np.maximum(x - threshold, 0)
125
+ gain = over / (over + knee) * (1 - (1 / ratio)) + 1
126
+ gain = np.maximum(gain, 1 - max_gain)
127
+ return x * gain
128
+
129
+ st_state.augmented_audio = compress(st_state.augmented_audio, threshold, ratio, knee, max_gain)
130
+
131
+ # EQ
132
+ eq_low = st.slider("EQ Low", min_value=-10.0, max_value=10.0, value=0.0, step=0.1)
133
+ eq_mid = st.slider("EQ Mid", min_value=-10.0, max_value=10.0, value=0.0, step=0.1)
134
+ eq_high = st.slider("EQ High", min_value=-10.0, max_value=10.0, value=0.0, step=0.1)
135
+
136
+ if st.button("Apply EQ"):
137
+ if st_state.augmented_audio is None:
138
+ st_state.augmented_audio = st_state.audio
139
+
140
+ # Apply a simple 3-band EQ using a butterworth filter
141
+ nyquist = sample_rate / 2
142
+ low_cutoff = 200 / nyquist
143
+ mid_cutoff = 2000 / nyquist
144
+ high_cutoff = 8000 / nyquist
145
+
146
+ low_sos = butter(4, low_cutoff, btype='low', output='sos', analog=False)
147
+ mid_sos = butter(4, [low_cutoff, mid_cutoff], btype='band', output='sos', analog=False)
148
+ high_sos = butter(4, high_cutoff, btype='high', output='sos', analog=False)
149
+
150
+ st_state.augmented_audio = sosfilt(np.dstack((low_sos, mid_sos, high_sos)),
151
+ st_state.augmented_audio,
152
+ np.stack((eq_low, eq_mid, eq_high)))
153
 
154
  # Reverb
155
+ reverb_amount = st.slider("Reverb Amount", min_value=0.0, max_value=1.0, value=0.5, step=0.01)
156
+ if st.button("Apply Reverb"):
157
+ if st_state.augmented_audio is None:
158
+ st_state.augmented_audio = st_state.audio
159
+
160
+ # Apply a simple reverb effect using convolution
161
+ ir_length = int(sample_rate * 2.5) # 2.5 seconds
162
+ ir = np.zeros(ir_length)
163
+ ir[0] = 1
164
+ ir = np.append(ir, np.zeros(len(st_state.augmented_audio) - ir_length))
165
+ reverb = np.convolve(st_state.augmented_audio, ir, mode='full')[:len(st_state.augmented_audio)]
166
+ st_state.augmented_audio = st_state.augmented_audio + reverb_amount * reverb
167
+
168
+ # Delay
169
+ delay_amount = st.slider("Delay Amount", min_value=0.0, max_value=1.0, value=0.5, step=0.01)
170
+ delay_time = st.slider("Delay Time (ms)", min_value=10, max_value=500, value=100, step=10)
171
+ if st.button("Apply Delay"):
172
+ if st_state.augmented_audio is None:
173
+ st_state.augmented_audio = st_state.audio
174
+
175
+ # Apply a simple delay effect
176
+ delay_samples = int(delay_time / 1000 * sample_rate)
177
+ delay = np.zeros(len(st_state.augmented_audio) + delay_samples)
178
+ delay[delay_samples:] = st_state.augmented_audio
179
+ delay[:len(st_state.augmented_audio)] += delay_amount * delay[:-delay_samples]
180
+ st_state.augmented_audio = delay[:len(st_state.augmented_audio)]
181
+
182
+ # Display the final audio
183
+ if st_state.augmented_audio is not None:
 
 
 
 
 
 
 
 
 
 
 
184
  st.audio(st_state.augmented_audio, format="audio/wav", sample_rate=sample_rate, start_time=0)
185
+
186
  st.link_button("Download/Save", "https://songlabai.com/subcribe/")