Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
"""
|
| 4 |
+
Audio processing tools to convert between spectrogram images and waveforms.
|
| 5 |
+
"""
|
| 6 |
+
import io
|
| 7 |
+
import typing as T
|
| 8 |
+
|
| 9 |
+
import numpy as np
|
| 10 |
+
from PIL import Image
|
| 11 |
+
import pydub
|
| 12 |
+
from scipy.io import wavfile
|
| 13 |
+
import torch
|
| 14 |
+
import torchaudio
|
| 15 |
+
|
| 16 |
+
def wav_bytes_from_spectrogram_image(image: Image.Image) -> T.Tuple[io.BytesIO, float]:
|
| 17 |
+
"""
|
| 18 |
+
Reconstruct a WAV audio clip from a spectrogram image. Also returns the duration in seconds.
|
| 19 |
+
"""
|
| 20 |
+
|
| 21 |
+
max_volume = 50
|
| 22 |
+
power_for_image = 0.25
|
| 23 |
+
Sxx = spectrogram_from_image(image, max_volume=max_volume, power_for_image=power_for_image)
|
| 24 |
+
|
| 25 |
+
sample_rate = 44100 # [Hz]
|
| 26 |
+
clip_duration_ms = 5000 # [ms]
|
| 27 |
+
|
| 28 |
+
bins_per_image = 512
|
| 29 |
+
n_mels = 512
|
| 30 |
+
|
| 31 |
+
# FFT parameters
|
| 32 |
+
window_duration_ms = 100 # [ms]
|
| 33 |
+
padded_duration_ms = 400 # [ms]
|
| 34 |
+
step_size_ms = 10 # [ms]
|
| 35 |
+
|
| 36 |
+
# Derived parameters
|
| 37 |
+
num_samples = int(image.width / float(bins_per_image) * clip_duration_ms) * sample_rate
|
| 38 |
+
n_fft = int(padded_duration_ms / 1000.0 * sample_rate)
|
| 39 |
+
hop_length = int(step_size_ms / 1000.0 * sample_rate)
|
| 40 |
+
win_length = int(window_duration_ms / 1000.0 * sample_rate)
|
| 41 |
+
|
| 42 |
+
samples = waveform_from_spectrogram(
|
| 43 |
+
Sxx=Sxx,
|
| 44 |
+
n_fft=n_fft,
|
| 45 |
+
hop_length=hop_length,
|
| 46 |
+
win_length=win_length,
|
| 47 |
+
num_samples=num_samples,
|
| 48 |
+
sample_rate=sample_rate,
|
| 49 |
+
mel_scale=True,
|
| 50 |
+
n_mels=n_mels,
|
| 51 |
+
max_mel_iters=200,
|
| 52 |
+
num_griffin_lim_iters=32,
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
wav_bytes = io.BytesIO()
|
| 56 |
+
wavfile.write(wav_bytes, sample_rate, samples.astype(np.int16))
|
| 57 |
+
wav_bytes.seek(0)
|
| 58 |
+
|
| 59 |
+
duration_s = float(len(samples)) / sample_rate
|
| 60 |
+
|
| 61 |
+
return wav_bytes
|
| 62 |
+
|
| 63 |
+
gr.Interface(fn=wav_bytes_from_spectrogram_image, inputs=[gr.Image()], outputs=[gr.Audio()]).launch()
|