File size: 3,988 Bytes
0446ab5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05e0e8d
0446ab5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05e0e8d
0446ab5
 
5331e27
0446ab5
 
 
 
 
 
f73b286
0446ab5
98cb795
0446ab5
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from huggingface_hub import from_pretrained_keras
import numpy as np
import tensorflow as tf
from tensorflow.keras import layers
import tensorflow_io as tfio

import gradio as gr
import librosa
import librosa.display
import matplotlib.pyplot as plt

class MelSpec(layers.Layer):
    def __init__(
        self,
        frame_length=1024,
        frame_step=256,
        fft_length=None,
        sampling_rate=22050,
        num_mel_channels=80,
        freq_min=125,
        freq_max=7600,
        **kwargs,
    ):
        super().__init__(**kwargs)
        self.frame_length = frame_length
        self.frame_step = frame_step
        self.fft_length = fft_length
        self.sampling_rate = sampling_rate
        self.num_mel_channels = num_mel_channels
        self.freq_min = freq_min
        self.freq_max = freq_max
        self.mel_filterbank = tf.signal.linear_to_mel_weight_matrix(
            num_mel_bins=self.num_mel_channels,
            num_spectrogram_bins=self.frame_length // 2 + 1,
            sample_rate=self.sampling_rate,
            lower_edge_hertz=self.freq_min,
            upper_edge_hertz=self.freq_max,
        )

    def call(self, audio):
        stft = tf.signal.stft(
            tf.squeeze(audio, -1),
            self.frame_length,
            self.frame_step,
            self.fft_length,
            pad_end=True,
        )

        # Taking the magnitude of the STFT output
        magnitude = tf.abs(stft)

        # Multiplying the Mel-filterbank with the magnitude and scaling it using the db scale
        mel = tf.matmul(tf.square(magnitude), self.mel_filterbank)
        log_mel_spec = tfio.audio.dbscale(mel, top_db=80)
        return log_mel_spec
       

    def get_config(self):
        config = super(MelSpec, self).get_config()
        config.update(
            {
                "frame_length": self.frame_length,
                "frame_step": self.frame_step,
                "fft_length": self.fft_length,
                "sampling_rate": self.sampling_rate,
                "num_mel_channels": self.num_mel_channels,
                "freq_min": self.freq_min,
                "freq_max": self.freq_max,
            }
        )
        return config

model = from_pretrained_keras("keras-io/MelGAN-spectrogram-inversion")

def inference(audio, model):
    input, sr = librosa.load(audio)
    # input, sr = audio
    x = tf.expand_dims(input, axis=-1)
    mel = MelSpec()(x)
    audio_sample = tf.expand_dims(mel, axis=0)
    pred = model.predict(audio_sample, batch_size=1, verbose=0)  
    return input, pred.squeeze(), sr

def predict(audio):
    x, x_pred, sr = inference(audio, model)
    fig, ax = plt.subplots(nrows=2, ncols=1, sharex=True, figsize=(10, 8), dpi=120)
    D = librosa.amplitude_to_db(np.abs(librosa.stft(x)), ref=np.max)
    img = librosa.display.specshow(D, y_axis='linear', x_axis='time',
                                  sr=sr, ax=ax[0])
    ax[0].set(title='Spectrogram of Original  sample audio')
    ax[0].label_outer()

    D = librosa.amplitude_to_db(np.abs(librosa.stft(x_pred)), ref=np.max)
    img = librosa.display.specshow(D, y_axis='linear', x_axis='time',
                                  sr=sr, ax=ax[1])
    ax[1].set(title='Spectrogram of synthesis  sample audio ')
    ax[1].label_outer()
    return plt.gcf()

inputs = [
        gr.Audio(source = "upload", label='Upload audio file', type="filepath"),
]

examples = ["sample_1.wav", "sample_2.wav"]

gr.Interface(
    fn=predict,
    title="MelGAN-based spectrogram inversion",
    description = "Inversion of audio from mel-spectrograms using the MelGAN architecture and feature matching",
    inputs=inputs,
    examples=examples,
    outputs=gr.Plot(),
    cache_examples=False,
    article = "Author: <a href=\"https://huggingface.co./vumichien\">Vu Minh Chien</a>. Based on the keras example from <a href=\"https://keras.io/examples/audio/melgan_spectrogram_inversion/\">Darshan Deshpande</a>",
).launch(debug=False, enable_queue=True)