-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathin_out.py
71 lines (54 loc) · 1.92 KB
/
in_out.py
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
import os
import numpy as np
import h5py
import librosa
import soundfile
from scipy.signal import decimate
from matplotlib import pyplot as plt
# ----------------------------------------------------------------------------
def load_h5(h5_path):
# load training data
with h5py.File(h5_path, 'r') as hf:
print('List of arrays in input file:', hf.keys())
X = np.array(hf.get('data'))
Y = np.array(hf.get('label'))
print('Shape of X:', X.shape)
print('Shape of Y:', Y.shape)
return X, Y
def upsample_wav(wav, args, model):
# load signal
x_hr, fs = librosa.load(wav, sr=args.sr)
x_lr_t = decimate(x_hr, args.r)
# pad to mutliple of patch size to ensure model runs over entire sample
x_hr = np.pad(x_hr, (0, args.patch_size - (x_hr.shape[0] % args.patch_size)), 'constant', constant_values=(0,0))
# downscale signal
x_lr = decimate(x_hr, args.r)
# upscale the low-res version
P = model.predict(x_lr.reshape((1,len(x_lr),1)))
x_pr = P.flatten()
# crop so that it works with scaling ratio
x_hr = x_hr[:len(x_pr)]
x_lr_t = x_lr_t[:len(x_pr)]
# save the file
outname = wav + '.' + args.out_label
soundfile.write(outname + '.lr.wav', x_lr_t, fs / args.r)
soundfile.write(outname + '.hr.wav', x_hr, fs)
soundfile.write(outname + '.pr.wav', x_pr, fs)
# save the spectrum
S = get_spectrum(x_pr, n_fft=2048)
save_spectrum(S, outfile=outname + '.pr.png')
S = get_spectrum(x_hr, n_fft=2048)
save_spectrum(S, outfile=outname + '.hr.png')
S = get_spectrum(x_lr, n_fft=2048/args.r)
save_spectrum(S, outfile=outname + '.lr.png')
# ----------------------------------------------------------------------------
def get_spectrum(x, n_fft=2048):
S = librosa.stft(x, n_fft)
#p = np.angle(S)
S = np.log1p(np.abs(S))
return S
def save_spectrum(S, lim=800, outfile='spectrogram.png'):
plt.imshow(S.T, aspect=10)
# plt.xlim([0,lim])
plt.tight_layout()
plt.savefig(outfile)