File size: 1,518 Bytes
90f7c1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
import matplotlib.pyplot as plt
from scipy.io import wavfile
import torch
import librosa
from torch.nn import functional as F


def save_curve_plot(pred, midi, gt, savepath):
    plt.style.use('default')
    fig, ax = plt.subplots(figsize=(12, 3))

    pred[pred == 0] = np.nan
    midi[midi == 0] = np.nan
    gt[gt == 0] = np.nan

    # im = ax.imshow(tensor, aspect="auto", origin="lower", interpolation='none')
    ax.plot(range(len(pred)), pred, color='tab:green', label='pred')
    ax.plot(range(len(midi)), midi, color='tab:blue', label='midi')
    ax.plot(range(len(gt)), gt, color='grey', label='gt')
    # plt.colorbar(im, ax=ax)
    plt.tight_layout()
    fig.canvas.draw()
    plt.legend()
    plt.savefig(savepath)
    plt.close()
#
#
# def save_audio(file_path, sampling_rate, audio):
#     audio = np.clip(audio.detach().cpu().squeeze().numpy(), -0.999, 0.999)
#     wavfile.write(file_path, sampling_rate, (audio * 32767).astype("int16"))


def minmax_norm_diff(tensor: torch.Tensor, vmax: float = librosa.note_to_hz('C6'),
                     vmin: float = 0) -> torch.Tensor:
    tensor = torch.clip(tensor, vmin, vmax)
    tensor = 2 * (tensor - vmin) / (vmax - vmin) - 1
    return tensor


def reverse_minmax_norm_diff(tensor: torch.Tensor, vmax: float = librosa.note_to_hz('C6'),
                             vmin: float = 0) -> torch.Tensor:
    tensor = torch.clip(tensor, -1.0, 1.0)
    tensor = (tensor + 1) / 2
    tensor = tensor * (vmax - vmin) + vmin
    return tensor