-
Notifications
You must be signed in to change notification settings - Fork 8
/
painter_base.js
341 lines (300 loc) · 10.4 KB
/
painter_base.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/**
* @license
* Copyright 2018 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {drawMusicLine, getColorForUnscaledWeight, getColorForWeight, getConnectorLocation, makePath, makeRect, scaleArray, scaleNote, svgNS} from './utils.js';
export {
PainterBase
}
class PainterBase {
constructor(svgEl, mapEl, minPitch, maxPitch, steps) {
this.svg = svgEl;
this.heatmap = mapEl;
this.steps = steps;
this.musicColor = 'rgb(0, 0, 0)'; //'rgb(245, 0, 87)';
this.config = {
epsilon: 0.00001,
weightCutoff: 0.5,
isTop: false,
isCircles: true,
topNumber: 10,
svgPadding: 100,
noteMargin: 0
};
// Add some padding.
this.minPitch = minPitch - 2;
this.maxPitch = maxPitch + 2;
}
updateWidth() {
throw new Error('updateWidth() not implemented', this);
}
clear() {
this.svg.innerHTML = '';
window.musicLine = null;
}
paintMusic(pitches) {
throw new Error('paintMusic() not implemented', this);
}
/******************
* Attention
*****************/
paintAttention(sequence, step, colors, series) {
if (!sequence) {
return;
}
// First scale the values so the colours are better.
const scaledSequence = scaleArray(sequence);
// Paint the step we're looking at.
const [activeType, activeConnector] =
this.activateNoteAtStep(step, 'current');
if (!activeConnector) return false;
for (let i = 0; i <= step; i++) {
// Find the rect for this step. Note that the array of steps is actually
// for steps [-1, 0, 1]
const el = this.findNoteAtStep(i - 1, activeType);
if (el == null) {
continue;
}
this.setTitleForNote(el, sequence[i]);
if (sequence[i] >= this.config.epsilon) {
el.setAttribute('fill', getColorForWeight(scaledSequence[i], colors));
// If it's big enough to get a tentacle, draw it.
if (scaledSequence[i] > this.config.weightCutoff &&
!this.config.noPaths) {
this.drawAttentionPath(
getConnectorLocation(el), activeConnector, scaledSequence[i],
colors.max, series);
}
} else if (el.getAttribute('fill').trim() == 'rgb(0, 0, 0)') {
el.setAttribute('fill', 'rgb(238, 238, 238)');
}
}
return true;
}
paintAllTheAttention(
sequences, scaledSequences, checkedStatuses, step, colors, series) {
// This may be a made up note-off for the end of the performance
// that doesn't have any data.
if (!step) {
return;
}
// Paint the step we're looking at.
const [activeType, activeConnector] =
this.activateNoteAtStep(step, 'current-all');
if (!activeConnector) return false;
if (this.config.isTop) {
if (window.precomputedSortedAttentions) {
this.paintTopAttention(
window.precomputedSortedAttentions[step], activeType,
activeConnector, colors, series);
} else {
const sortedAttentions = this.getSortedAttentionsIfNeeded(
step, scaledSequences, checkedStatuses);
this.paintTopAttention(
sortedAttentions, activeType, activeConnector, colors, series);
}
} else {
for (let head = 0; head < scaledSequences.length; head++) {
const scaledSequence =
scaledSequences[head][step]; // scaleArray(sequences[head][step]);
// Only paint the biggest weights for this head, and only if it's on.
if (checkedStatuses[head]) {
this.paintHeadAttention(
scaledSequence, head, step, activeType, activeConnector, colors,
series);
}
}
}
return true;
}
paintTopAttention(
sortedAttentions, activeType, activeConnector, colors, series) {
// Go through the top weights, only paint those.
let count = 0;
for (let i = 0; i < sortedAttentions.length; i++) {
// If you've already painted enough attentions, stop.
if (count === this.config.topNumber) {
break;
}
const attn = sortedAttentions[i];
// Find the rect for this step. Note that the array of steps is actually
// for steps [-1, 0, 1]
const el = this.findNoteAtStep(attn.step - 1, activeType);
if (el == null) {
continue;
}
el.setAttribute('class', 'attention-all');
el.setAttribute('fill', colors[attn.head].max);
scaleNote(el);
this.drawAttentionPath(
getConnectorLocation(el), activeConnector, attn.scaledValue + 0.6,
colors[attn.head].max, attn.head, series);
count++;
}
}
paintHeadAttention(
scaledSequence, head, step, activeType, activeConnector, colors, series) {
for (let i = 0; i < step; i++) {
// Find the rect for this step. Note that the array of steps is actually
// for steps [-1, 0, 1]
const el = this.findNoteAtStep(i - 1, activeType);
if (el == null) {
continue;
}
if (scaledSequence[i] > this.config.weightCutoff) {
el.setAttribute('class', 'attention-all');
el.setAttribute('fill', colors[head].max);
scaleNote(el);
this.drawAttentionPath(
getConnectorLocation(el), activeConnector, scaledSequence[i],
colors[head].max, head, series);
} else if (!el.hasAttribute('class')) {
el.setAttribute('class', 'no-attention-all');
}
}
}
/******************
* Heatmap
*****************/
paintHeatMap(heads, currentHead, checkedStatuses, colors) {
return;
if (currentHead !== -1) {
this.paintHeatMapRow(heads[currentHead], colors[currentHead]);
}
// else {
// for (let head = 0; head < heads.length; head++) {
// // Only paint the biggest weights for this head, and only if it's on.
// if (checkedStatuses[head]) {
// this.paintHeatMapRow(heads[head], colors[head]);
// }
// }
// }
}
paintHeatMapRow(steps, colors) {
const size = this.config.heatmapSquare;
for (let y = 0; y < steps.length; y++) {
const stepData = steps[y];
for (let x = 0; x < stepData.length; x++) {
const color = getColorForUnscaledWeight(stepData[x], colors)
const rect =
makeRect(y, size * x, size * y, size, size, color, stepData[x]);
this.heatmap.appendChild(rect);
}
}
}
/******************
* Misc helpers
*****************/
getPositionForPitch(pitch) {
const whiteNotes = [21, 23, 24, 26, 28, 29, 31];
// Since we scaled the canvas to not include pitches we're
// not painting, figure out where this actually goes.
const offset = pitch - this.minPitch;
// If it's a white note, it's on the pitch, otherwise it's half of.
if (((pitch - whiteNotes[0]) % 12 === 0) ||
((pitch - whiteNotes[1]) % 12 === 0) ||
((pitch - whiteNotes[2]) % 12 === 0) ||
((pitch - whiteNotes[3]) % 12 === 0) ||
((pitch - whiteNotes[4]) % 12 === 0) ||
((pitch - whiteNotes[5]) % 12 === 0) ||
((pitch - whiteNotes[6]) % 12 === 0)) {
return this.height - offset * this.config.noteHeight;
} else {
return this.height - offset * this.config.noteHeight -
this.config.noteHeight / 2;
}
}
getSortedAttentionsIfNeeded(step, sequences, checkedStatuses) {
let sortedAttentions = [];
if (this.config.isTop) {
for (let head = 0; head < sequences.length; head++) {
// Skip this head if it's unchecked.
if (!checkedStatuses[head]) {
continue;
}
const seq = sequences[head][step];
for (let s = 0; s < seq.length; s++) {
if (seq[s] > this.config.epsilon) {
sortedAttentions.push({head, value: seq[s], step: s});
}
}
}
}
// Sort this array.
sortedAttentions.sort(function(a, b) {
return b.value - a.value;
});
// Get only the top we care about;
let subset = [];
let subsetValues = [];
// Be generous.
for (let i = 0; i < sortedAttentions.length; i++) {
subset.push(sortedAttentions[i]);
subsetValues.push(sortedAttentions[i].value);
}
// Scale them and add them back.
// subsetValues = scaleArray(subsetValues);
for (let i = 0; i < sortedAttentions.length; i++) {
subset[i].scaledValue = subsetValues[i];
}
return subset;
}
activateNoteAtStep(step, className) {
const el = this.stepToRectMap[step];
if (!el) {
return [null, null];
}
drawMusicLine(el.getAttribute('x'), el.getAttribute('width'));
const type = el.getAttribute('class').replace('hover ', '');
const connector = getConnectorLocation(el);
el.setAttribute('class', className);
scaleNote(el);
return ['note', connector];
}
findNoteAtStep(step, activeType) {
// const el = this.svg.querySelector(`rect[data-index="${step}"]`);
const el = this.stepToRectMap[step];
if (el == null) {
// console.log(`Could not find the rect for index ${step}`);
return el;
}
// For performance, we don't want to point to non-notes.
const thisType = el.getAttribute('class').replace('hover ', '');
if (thisType !== activeType) {
return null;
}
return el;
}
setTitleForNote(el, text) {
el.innerHTML = '';
const title = document.createElementNS(svgNS, 'title');
title.textContent = text;
el.appendChild(title);
}
drawAttentionPath(from, to, value, color, offset = 0, series) {
if (this.config.noPaths) return;
this.svg.appendChild(makePath(
to, from, offset, value, color, this.config.noteWidth,
this.config.isCircles, this.config.weirdMode, series));
}
drawNoteBox(pitch, x, w, index) {
const y = this.getPositionForPitch(pitch);
const rect = makeRect(
index, x, y, w, this.config.noteHeight, this.musicColor, pitch);
rect.setAttribute('class', rect.getAttribute('class') + ' note');
this.svg.appendChild(rect);
return rect;
}
}