Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import io
|
| 3 |
+
import typing as T
|
| 4 |
+
|
| 5 |
+
import numpy as np
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import pydub
|
| 8 |
+
from scipy.io import wavfile
|
| 9 |
+
import torch
|
| 10 |
+
import torchaudio
|
| 11 |
+
|
| 12 |
+
def convert(audio):
|
| 13 |
+
# read uploaded file to wav
|
| 14 |
+
rate, data = wavfile.read(audio)
|
| 15 |
+
|
| 16 |
+
# resample from 48000 to 44100
|
| 17 |
+
# from scipy.signal import resample
|
| 18 |
+
# data = resample(data, int(data.shape[0] * 44100 / 48000))
|
| 19 |
+
|
| 20 |
+
# convert to mono
|
| 21 |
+
data = np.mean(data, axis=1)
|
| 22 |
+
|
| 23 |
+
# convert to float32
|
| 24 |
+
data = data.astype(np.float32)
|
| 25 |
+
|
| 26 |
+
# take a random 7 second slice of the audio
|
| 27 |
+
data = data[rate*7:rate*14]
|
| 28 |
+
|
| 29 |
+
spectrogram = spectrogram_from_waveform(
|
| 30 |
+
waveform=data,
|
| 31 |
+
sample_rate=rate,
|
| 32 |
+
# width=768,
|
| 33 |
+
n_fft=8192,
|
| 34 |
+
hop_length=512,
|
| 35 |
+
win_length=8192,
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
spec = image_from_spectrogram(spectrogram)
|
| 39 |
+
|
| 40 |
+
return spec
|
| 41 |
+
|
| 42 |
+
def spectrogram_from_waveform(
|
| 43 |
+
waveform: np.ndarray,
|
| 44 |
+
sample_rate: int,
|
| 45 |
+
n_fft: int,
|
| 46 |
+
hop_length: int,
|
| 47 |
+
win_length: int,
|
| 48 |
+
mel_scale: bool = True,
|
| 49 |
+
n_mels: int = 512,
|
| 50 |
+
) -> np.ndarray:
|
| 51 |
+
"""
|
| 52 |
+
Compute a spectrogram from a waveform.
|
| 53 |
+
"""
|
| 54 |
+
|
| 55 |
+
spectrogram_func = torchaudio.transforms.Spectrogram(
|
| 56 |
+
n_fft=n_fft,
|
| 57 |
+
power=None,
|
| 58 |
+
hop_length=hop_length,
|
| 59 |
+
win_length=win_length,
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
waveform_tensor = torch.from_numpy(waveform.astype(np.float32)).reshape(1, -1)
|
| 63 |
+
Sxx_complex = spectrogram_func(waveform_tensor).numpy()[0]
|
| 64 |
+
|
| 65 |
+
Sxx_mag = np.abs(Sxx_complex)
|
| 66 |
+
|
| 67 |
+
if mel_scale:
|
| 68 |
+
mel_scaler = torchaudio.transforms.MelScale(
|
| 69 |
+
n_mels=n_mels,
|
| 70 |
+
sample_rate=sample_rate,
|
| 71 |
+
f_min=0,
|
| 72 |
+
f_max=10000,
|
| 73 |
+
n_stft=n_fft // 2 + 1,
|
| 74 |
+
norm=None,
|
| 75 |
+
mel_scale="htk",
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
Sxx_mag = mel_scaler(torch.from_numpy(Sxx_mag)).numpy()
|
| 79 |
+
|
| 80 |
+
return Sxx_mag
|
| 81 |
+
|
| 82 |
+
def image_from_spectrogram(
|
| 83 |
+
spectrogram: np.ndarray, max_volume: float = 50, power_for_image: float = 0.25
|
| 84 |
+
) -> Image.Image:
|
| 85 |
+
"""
|
| 86 |
+
Compute a spectrogram image from a spectrogram magnitude array.
|
| 87 |
+
"""
|
| 88 |
+
# Apply the power curve
|
| 89 |
+
data = np.power(spectrogram, power_for_image)
|
| 90 |
+
|
| 91 |
+
# Rescale to 0-255
|
| 92 |
+
data = data * 255 / max_volume
|
| 93 |
+
|
| 94 |
+
# Invert
|
| 95 |
+
data = 255 - data
|
| 96 |
+
|
| 97 |
+
# Convert to a PIL image
|
| 98 |
+
image = Image.fromarray(data.astype(np.uint8))
|
| 99 |
+
|
| 100 |
+
# Flip Y
|
| 101 |
+
image = image.transpose(Image.FLIP_TOP_BOTTOM)
|
| 102 |
+
|
| 103 |
+
# Convert to RGB
|
| 104 |
+
image = image.convert("RGB")
|
| 105 |
+
|
| 106 |
+
return image
|
| 107 |
+
|
| 108 |
+
gr.Interface(fn=convert, inputs=[gr.Audio(source="upload", type="filepath")], outputs=[gr.Image()]).launch()
|