-
Notifications
You must be signed in to change notification settings - Fork 0
/
load-data2.py
53 lines (47 loc) · 1.79 KB
/
load-data2.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
import os
import matplotlib.pyplot as plt
import matplotlib.colors as colors
from scipy import signal
from scipy.io import wavfile
import numpy as np
import librosa
import librosa.display
from pathlib import Path
import torch
def read_audio_from_filename(filename):
audio, sr = librosa.load(filename)
ftd = librosa.stft(audio)
D = librosa.power_to_db(np.abs(ftd)**2)
newAudio = librosa.feature.melspectrogram(y=audio, sr=sr, S=D)
return ftd, sr, newAudio
def convert_data(filename):
spect, sr, mel = read_audio_from_filename(filename)
return spect, sr, mel
filename = '/mnt/c/Users/Maia/Downloads/a/Medley-solos-DB_test-0_0a282672-c22c-59ff-faaa-ff9eb73fc8e6.wav.wav'
#filename = '/mnt/c/Users/Maia/Downloads/a/Medley-solos-DB_test-0_0aed2359-7d66-5da2-f041-8fb5d78b61c1.wav.wav'
#filename = '/mnt/c/Users/Maia/Downloads/a/Medley-solos-DB_test-5_0a8d62bd-4d37-522d-fc71-75fab6e20a7b.wav.wav'
#target = '/mnt/c/Users/Maia/Downloads/here.png'
audio, sr = librosa.load(filename)
print(np.shape(audio), np.shape(sr))
print(audio)
ftd = librosa.stft(audio)
print(np.shape(ftd))
#conv, sr, spect = convert_data(filename)
#write_spect(conv, "testconv.png")
#write_spect(np.sqrt(np.real(conv)**2 + np.imag(conv)**2), "testconv-norm.png")
"""wav_dir = ['va-data/', 'te-data/']
target = ['va-data.pt', 'te-data.pt']
ints = [27, 21]
for i in range(2):
numFiles = 0
files = os.listdir(wav_dir[i])
x = torch.empty(size=(len(files), 1025, 129))
y = torch.empty(size=(len(files), 1))
for filename in files:
if numFiles % 200 == 0: print(numFiles, " files processed out of ", len(files), "! ")
conv, sr, spect = convert_data(wav_dir[i] + filename)
x[numFiles] = torch.from_numpy(conv)
y[numFiles] = int(filename[ints[i]])
numFiles += 1
m = {'x': x, 'y': y}
torch.save(m, target[i])"""