-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.py
153 lines (130 loc) · 4.64 KB
/
loader.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from cmath import tanh
import numpy
import torch
import torch.nn as nn
import collections
import ctypes
import random
pymidirenderer = ctypes.cdll.LoadLibrary("render-build/libpymidirender.so")
def loadTable_frames(path): # 加载单个表格文件
with open(path) as f:
line = f.readline()
while line:
arr = line.strip().split(",")
if len(arr) > 1:
freq_frame = []
lab_frame = []
for i in range(1, len(arr)):
pair = arr[i].split(":")
freq_frame.append(float(pair[0]))
lab_frame.append(int(pair[1]))
yield (freq_frame, lab_frame)
line = f.readline()
def loadTable_frames_norm(path): # 归一化
# 先获取最大
max_n = 0
for data in loadTable_frames(path):
n = max(data[0])
if n > max_n:
max_n = n
if max_n > 0:
for data in loadTable_frames(path):
tmp = []
for n in data[0]:
tmp.append(n/max_n)
yield(tmp, data[1])
def loadTable_tensor(path): # 加载为张量
que = collections.deque(maxlen=512) # 用队列保存,提高性能
count = 0 # 统计数量
for i in loadTable_frames_norm(path):
que.append(i)
if len(que) >= 512:
if count % 256 == 0:
# 构建tensor
tensor_in = []
tensor_out = []
for n in que:
tensor_in.append(n[0])
tensor_out.append(n[1])
yield(
torch.tensor([[tensor_in]]),
torch.tensor([[tensor_out]]))
count += 1
def loadMidi(path, sf, callback, section=4, sectionShift=0):
def cb(a, b, c):
buf_b = []
buf_c = []
for i in range(0, 512):
buf_b.append(b[i])
buf_c.append(c[i])
callback(a, buf_b, buf_c)
callback_type = ctypes.CFUNCTYPE(
None,
ctypes.c_int,
ctypes.POINTER(ctypes.c_float),
ctypes.POINTER(ctypes.c_int))
pymidirenderer.midirender_render(ctypes.c_char_p(path.encode()),
ctypes.c_char_p(sf.encode()),
ctypes.c_int(44100),
ctypes.c_int(8192),
ctypes.c_int(section),
ctypes.c_int(sectionShift),
ctypes.c_int(16),
ctypes.c_int(16),
ctypes.c_float(2),
callback_type(cb))
def getDatasetList():
res = []
with open("datas/midi/process.sh") as f:
line = f.readline()
while line:
arr = line.strip().split(" ")
if len(arr) > 2:
res.append(arr[2:])
line = f.readline()
random.shuffle(res)
return res
def loadMidi_norm(path, sf, callback, section=4, sectionShift=0):
with open(path+"/freqmax.txt") as out:
maxv = float(out.readline())
print("maxv:", maxv)
def cb(count, input, output):
tmp = []
for n in input:
tmp.append(n/maxv)
callback(tmp, output)
loadMidi(path+"/file.mid", sf, cb, section, sectionShift)
def loadMidi_tensor(path, sf, callback, section=4, sectionShift=0): # 加载为张量
que = collections.deque(maxlen=512) # 用队列保存,提高性能
count = [0] # 统计数量
def cb(input, output):
que.append((input, output))
if len(que) >= 512:
if count[0] % 256 == 0:
# 构建tensor
tensor_in = []
tensor_out = []
for n in que:
tensor_in.append(n[0])
tensor_out.append(n[1])
callback(
torch.tensor([[tensor_in]]),
torch.tensor([[tensor_out]]))
count[0] += 1
loadMidi_norm(path, sf, cb, section, sectionShift)
def loadAllDataset(callback):
arr = getDatasetList()
for line in arr:
loadMidi_tensor(line[0], "datas/sndfnt.sf2", callback,
section=int(line[1]),
sectionShift=int(line[2]))
if __name__ == '__main__':
# print(getDatasetList())
def callback(a, b):
print(a.size(), b.size())
loadAllDataset(callback)
#id = 0
# for i in loadTable_tensor("./render-build/out.txt"):
# print(i[0].size(), i[1].size())
# #numpy.savetxt("test/"+str(id)+".txt", numpy.array(i[1][0][0]))
# id += 1