Spaces:
Running
Running
Delete melspec_gen.py
Browse files- melspec_gen.py +0 -61
melspec_gen.py
DELETED
@@ -1,61 +0,0 @@
|
|
1 |
-
from scipy.io.wavfile import read
|
2 |
-
import torch
|
3 |
-
import numpy as np
|
4 |
-
import os
|
5 |
-
from multiprocessing import Pool
|
6 |
-
#from tqdm import tqdm
|
7 |
-
|
8 |
-
# Change here
|
9 |
-
base="/mnt/beegfs/home/espinosa/wallon/wallon_ms_02/p02/"
|
10 |
-
|
11 |
-
hann_window = {}
|
12 |
-
def load_wav_to_torch(full_path):
|
13 |
-
sampling_rate, data = read(full_path)
|
14 |
-
# data, sampling_rate = librosa.load(full_path)
|
15 |
-
return torch.FloatTensor(data.astype(np.float32)), sampling_rate
|
16 |
-
|
17 |
-
def spectrogram_torch(y, n_fft, sampling_rate, hop_size, win_size, center=False):
|
18 |
-
if torch.min(y) < -1.:
|
19 |
-
print('min value is ', torch.min(y))
|
20 |
-
if torch.max(y) > 1.:
|
21 |
-
print('max value is ', torch.max(y))
|
22 |
-
|
23 |
-
global hann_window
|
24 |
-
dtype_device = str(y.dtype) + '_' + str(y.device)
|
25 |
-
wnsize_dtype_device = str(win_size) + '_' + dtype_device
|
26 |
-
if wnsize_dtype_device not in hann_window:
|
27 |
-
hann_window[wnsize_dtype_device] = torch.hann_window(win_size).to(dtype=y.dtype, device=y.device)
|
28 |
-
|
29 |
-
y = torch.nn.functional.pad(y.unsqueeze(1), (int((n_fft-hop_size)/2), int((n_fft-hop_size)/2)), mode='reflect')
|
30 |
-
y = y.squeeze(1)
|
31 |
-
|
32 |
-
spec = torch.stft(y, n_fft, hop_length=hop_size, win_length=win_size, window=hann_window[wnsize_dtype_device],
|
33 |
-
center=center, pad_mode='reflect', normalized=False, onesided=True)
|
34 |
-
|
35 |
-
spec = torch.sqrt(spec.pow(2).sum(-1) + 1e-6)
|
36 |
-
return spec
|
37 |
-
|
38 |
-
def get_audio(filename):
|
39 |
-
max_wave_length = 32768.0
|
40 |
-
filter_length = 1024
|
41 |
-
hop_length = 256
|
42 |
-
win_length = 1024
|
43 |
-
audio, sampling_rate = load_wav_to_torch(filename)
|
44 |
-
audio_norm = audio / max_wave_length
|
45 |
-
audio_norm = audio_norm.unsqueeze(0)
|
46 |
-
spec_filename = filename.replace(".wav", ".spec.pt")
|
47 |
-
spec = spectrogram_torch(audio_norm, filter_length,
|
48 |
-
sampling_rate, hop_length, win_length,
|
49 |
-
center=False)
|
50 |
-
spec = torch.squeeze(spec, 0)
|
51 |
-
torch.save(spec, spec_filename)
|
52 |
-
|
53 |
-
if __name__=="__main__":
|
54 |
-
waves = []
|
55 |
-
for wav_name in os.listdir(base):
|
56 |
-
wav_path = os.path.join(base, wav_name)
|
57 |
-
if wav_path.endswith(".wav"):
|
58 |
-
waves.append(wav_path)
|
59 |
-
|
60 |
-
for wav_path in waves:
|
61 |
-
get_audio(wav_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|