Spaces:
Running
Running
RedTachyon
commited on
Commit
•
44d9d8c
1
Parent(s):
83cfc4f
Rename
Browse files
app.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import scipy.io.wavfile as wavfile
|
4 |
+
import scipy.fft as fft
|
5 |
+
import yt_dlp
|
6 |
+
|
7 |
+
|
8 |
+
def fft_derivative(audio_data: np.ndarray, sample_rate: int) -> np.ndarray:
|
9 |
+
freq_domain = fft.fft(audio_data)
|
10 |
+
freqs = fft.fftfreq(len(audio_data), 1 / sample_rate)
|
11 |
+
freq_derivative = 1j * freqs * freq_domain
|
12 |
+
time_domain_derivative = fft.ifft(freq_derivative)
|
13 |
+
return np.real(time_domain_derivative)
|
14 |
+
|
15 |
+
|
16 |
+
def convert_file(audio_data: tuple[int, np.ndarray]) -> np.ndarray:
|
17 |
+
sample_rate, audio = audio_data
|
18 |
+
if len(audio.shape) == 2:
|
19 |
+
audio = np.mean(audio, axis=1)
|
20 |
+
derivative = fft_derivative(audio, sample_rate)
|
21 |
+
derivative = np.int16(derivative / np.max(np.abs(derivative)) * 32767)
|
22 |
+
return derivative
|
23 |
+
|
24 |
+
|
25 |
+
def process_audio_file(audio_file: str) -> tuple[int, np.ndarray]:
|
26 |
+
sample_rate, audio_data = wavfile.read(audio_file)
|
27 |
+
return sample_rate, audio_data
|
28 |
+
|
29 |
+
|
30 |
+
def process_youtube_link(youtube_link: str) -> str:
|
31 |
+
song_file = "downloaded_song.wav"
|
32 |
+
ydl_opts = {
|
33 |
+
'outtmpl': song_file.replace(".wav", ""),
|
34 |
+
'postprocessors': [{
|
35 |
+
'key': 'FFmpegExtractAudio',
|
36 |
+
'preferredcodec': 'wav',
|
37 |
+
}]
|
38 |
+
}
|
39 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
40 |
+
ydl.download([youtube_link])
|
41 |
+
return song_file
|
42 |
+
|
43 |
+
|
44 |
+
def process_audio(audio: tuple[int, np.ndarray]) -> tuple[int, np.ndarray]:
|
45 |
+
return audio[0], convert_file(audio)
|
46 |
+
|
47 |
+
|
48 |
+
with gr.Blocks() as app:
|
49 |
+
with gr.Column():
|
50 |
+
youtube_link = gr.Textbox(label="YouTube Link", value="https://www.youtube.com/watch?v=YmJIccPWnEk")
|
51 |
+
download_button = gr.Button("Download")
|
52 |
+
audio_input = gr.Audio(label="Upload Audio File", type="numpy")
|
53 |
+
process_button = gr.Button("Process Audio")
|
54 |
+
audio_output = gr.Audio(label="Derivative Audio Output")
|
55 |
+
|
56 |
+
|
57 |
+
def download_audio(youtube_link: str):
|
58 |
+
song_file = process_youtube_link(youtube_link)
|
59 |
+
sample_rate, audio_data = process_audio_file(song_file)
|
60 |
+
return sample_rate, audio_data
|
61 |
+
|
62 |
+
|
63 |
+
def process_audio_input(audio):
|
64 |
+
sample_rate, processed_audio = process_audio(audio)
|
65 |
+
return sample_rate, processed_audio
|
66 |
+
|
67 |
+
|
68 |
+
download_button.click(download_audio, inputs=youtube_link, outputs=[audio_input])
|
69 |
+
process_button.click(process_audio_input, inputs=audio_input, outputs=audio_output)
|
70 |
+
|
71 |
+
if __name__ == "__main__":
|
72 |
+
app.launch()
|