-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
187 lines (166 loc) · 5.84 KB
/
main.js
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
const App = {
init: function() {
this.audioPlayer = document.getElementById("audioPlayer");
this.eventData = [];
this.lastNumberTakingEvent = null;
this.numberTakingEvents = new Set();
this.numbers = new Set("0123456789");
this.keyMap = [];
this.outputCsv = document.getElementById("outputCsv");
const keyDetector = document.getElementById("keyDetector");
const mappedKeyCount = document.getElementById("mappedKeyCount");
const songInput = document.getElementById("songInput");
songInput.addEventListener("change", () => {
this.audioPlayer.setAttribute(
"src",
URL.createObjectURL(songInput.files[0])
);
});
keyDetector.addEventListener("keypress", e => this.onKeyPress(e));
mappedKeyCount.addEventListener("change", e => this.onMapKeys(e));
this.onMapKeys({ target: { value: mappedKeyCount.value } });
},
onKeyPress: function(e) {
if (this.numbers.has(e.key) && this.lastNumberTakingEvent !== null) {
this.eventData[this.lastNumberTakingEvent].value = e.key;
} else {
if (this.numberTakingEvents.has(e.key)) {
this.lastNumberTakingEvent = this.eventData.length;
}
this.eventData.push({
currentTime: this.audioPlayer.currentTime,
key: e.key,
value: null
});
}
e.preventDefault();
window.requestAnimationFrame(() => this.renderOutput());
},
onMapKeys: function(e) {
const value = parseInt(e.target.value, 10);
if (!isFinite(value) || value < 0 || value > 32) {
return;
}
while (this.keyMap.length < value) {
this.keyMap.push({ label: "", key: "", takesNumber: false });
}
while (this.keyMap.length > value) {
this.keyMap.pop();
}
window.requestAnimationFrame(() => this.renderKeyMappings());
},
renderKeyMappings: function() {
const keyMappingsDiv = document.getElementById("keyMappings");
while (keyMappingsDiv.children.length > this.keyMap.length) {
keyMappingsDiv.removeChild(keyMappingsDiv.lastChild);
}
for (let i = keyMappingsDiv.children.length; i < this.keyMap.length; ++i) {
const mappingDiv = document.createElement("div");
const labelInput = document.createElement("input");
labelInput.setAttribute("type", "text");
labelInput.setAttribute("size", 10);
labelInput.setAttribute("placeholder", "Column Name");
labelInput.setAttribute("value", this.keyMap[i].label);
labelInput.addEventListener("change", e => {
this.keyMap[i].label = e.target.value;
});
mappingDiv.appendChild(labelInput);
const keyInput = document.createElement("input");
keyInput.setAttribute("type", "text");
keyInput.setAttribute("size", 10);
keyInput.setAttribute("maxlength", 1);
keyInput.setAttribute("placeholder", "Key");
keyInput.setAttribute("value", this.keyMap[i].key);
keyInput.addEventListener("change", e => {
this.keyMap[i].key = e.target.value;
if (this.keyMap[i].takesNumber) {
this.numberTakingEvents.add(this.keyMap[i].key);
} else {
this.numberTakingEvents.delete(this.keyMap[i].key);
}
});
mappingDiv.appendChild(keyInput);
const takesNumberInput = document.createElement("input");
takesNumberInput.setAttribute("type", "checkbox");
takesNumberInput.setAttribute("id", "chkNumberInput" + i);
if (this.keyMap[i].takesNumber) {
takesNumberInput.setAttribute("checked", "");
}
takesNumberInput.addEventListener("change", e => {
this.keyMap[i].takesNumber = e.target.checked;
if (this.keyMap[i].takesNumber) {
this.numberTakingEvents.add(this.keyMap[i].key);
} else {
this.numberTakingEvents.delete(this.keyMap[i].key);
}
});
const takesNumberLabel = document.createElement("label");
takesNumberLabel.setAttribute("for", "chkNumberInput" + i);
takesNumberLabel.textContent = "Takes number";
mappingDiv.appendChild(takesNumberInput);
mappingDiv.appendChild(takesNumberLabel);
keyMappingsDiv.appendChild(mappingDiv);
}
},
renderHeader: function(columns) {
const allColumns = ["time"];
let mappingIdx = 0;
for (let column of columns) {
while (mappingIdx < this.keyMap.length) {
const mapping = this.keyMap[mappingIdx];
++mappingIdx;
if (column === mapping.key && mapping.label.length > 0) {
column = mapping.label;
break;
}
}
allColumns.push(column);
}
return allColumns.join(",");
},
renderOutput: function() {
const interval = 10;
const rows = new Map();
const header = new Set();
let maxBucket = 0;
for (const mapping of this.keyMap) {
if (mapping.label.length > 0 && mapping.key.length > 0) {
header.add(mapping.key);
}
}
for (const point of this.eventData) {
let bucket = Math.floor(point.currentTime / interval) * interval;
maxBucket = Math.max(bucket, maxBucket);
let row = rows.get(bucket);
if (row == null) {
row = new Map();
}
let cell = row.get(point.key);
if (cell == null) {
cell = [];
}
cell.push(point.value == null ? point.key : point.value);
row.set(point.key, cell);
header.add(point.key);
rows.set(bucket, row);
}
const output = [];
output.push(this.renderHeader(header.values()));
for (let curBucket = 0; curBucket <= maxBucket; curBucket += interval) {
let row = rows.get(curBucket);
if (row == null) {
row = new Map();
}
output.push(
[curBucket]
.concat(
Array.from(header.values()).map(cell =>
row.has(cell) ? row.get(cell).join(" ") : ""
)
)
.join(",")
);
}
this.outputCsv.innerHTML = output.join("\n");
}
};