-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.py
95 lines (78 loc) · 2.82 KB
/
process.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from scipy.io.wavfile import read, write
import numpy as np
import matplotlib.pyplot as plt
import scipy.signal as signal
import os
def fitness(f1,f2):
f1_norm = f1/sum(f1)
f2_norm = f2/sum(f2)
MSE = 0
for i in range(len(f1)):
d = f1_norm[i] - f2_norm[i] # diff in each time slice
MSE_t = 0
for j in range(len(d)):
MSE_t += d[j]**2 # SE in time slice
MSE += MSE_t/len(f1) # total up each slice and take the mean
return MSE
def crop(audio,t):
fs = 44100
length = len(audio)
audio_crop = []
for i in range(length):
x, y = audio[i]
if y>0.9:
for j in range(i, i+fs*t):
p = audio[j]
audio_crop.append(p)
break
return np.array(audio_crop)
def freq(data):
fs = 44100
f, t, Sxx = signal.spectrogram(np.array(data[:,1]), fs, nperseg = 3000, nfft = 10000 )
print(len(f),len(t))
plt.figure()
plt.pcolormesh(t, f, Sxx)
# plt.title('n1,n2 = {},{}'.format(n1,n2))
plt.ylabel('Frequency [Hz]')
plt.xlabel('Time [sec]')
# plt.ylim(0,3000)
return Sxx
# plt.ylim(0, 2000)
def convert_data_x(input, output):
files = os.listdir(input)
for i in range(200):
print('Converting File {}'.format(i))
audio = read(input + '/' + files[i])[1]
audio_crop = crop(audio,2)
params = np.load(direc+'paramsdata/params{}.npy'.format(i))[param_list]
np.save(output + '/audio{}'.format(i),np.array([audio_crop,params]),allow_pickle=True)
def convert_data_f(input, output):
files = os.listdir(input)
print(len(files))
for i in range(len(files)):
print('Converting File {}'.format(i))
audio = read(input + '/' + files[i])[1]
audio_crop = crop(audio,2)
image = img(audio_crop)
size = np.shape(image)[1]
if size != 67:
print('WRONG SIZE', size)
params = np.load(direc+'paramsdata/params{}.npy'.format(i))[param_list]
np.save(output + '/img{}'.format(i),np.array([image,params]),allow_pickle=True)
# np.save(output + '/image{}'.format(i),np.array(image),allow_pickle=True)
def img(audio):
fs = 44100
f, t, Sxx = signal.spectrogram(np.array(audio[:,1]), fs, nperseg = 1500 , nfft = 10000 )
# plt.imshow(Sxx, aspect ='auto')
# plt.show()
return Sxx
if __name__ == "__main__":
# param_list = [0,1,2,3,4,5,6,
#
# ,8,9,10,14,15,16,17,18,19,20,21,22,23,24,25] # all oscillators
param_list = [0,1,14,15,16,17,18,19,20,21,22,23,24,25] # single oscillator
direc = '../data/1osc/testdata'
audio = read(direc + '/' + 'audiodata/audio1.wav')[1]
img(audio)
# convert_data_f(direc+'audiodata',direc+'TRAIN_f')
# convert_data_x('../data/1osc/audiodata','../data/1osc/TRAIN_x')