-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathstatistics.js
More file actions
158 lines (145 loc) · 5.64 KB
/
statistics.js
File metadata and controls
158 lines (145 loc) · 5.64 KB
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
// Copyright (c) 2016-21 Walter Bender
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the The GNU Affero General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// You should have received a copy of the GNU Affero General Public
// License along with this library; if not, write to the Free Software
// Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
/*
global
docById, analyzeProject, runAnalytics, scoreToChartData,
getChartOptions
*/
/* exported StatsWindow */
/** This widget displays the status of selected parameters and notes as they are being played. */
class StatsWindow {
/**
* @constructor
*/
constructor(activity) {
this.activity = activity;
this.isOpen = true;
this.widgetWindow = window.widgetWindows.windowFor(this, "stats", "stats");
this.widgetWindow.clear();
this.widgetWindow.show();
this.widgetWindow.onclose = () => {
this.isOpen = false;
this.activity.blocks.showBlocks();
this.widgetWindow.destroy();
this.activity.logo.statsWindow = null;
};
// Lazy-load Chart.js on demand instead of eagerly at startup.
// This saves ~3-5 MB of heap memory when the statistics widget
// is never opened (the common case).
// If Chart is already loaded (e.g. previously used), call synchronously.
if (typeof Chart !== "undefined") {
this.doAnalytics();
} else {
this._ensureChartLoaded()
.then(() => {
this.doAnalytics();
})
.catch(err => {
// eslint-disable-next-line no-console
console.error("Failed to load Chart.js:", err);
});
}
this.widgetWindow.onmaximize = () => {
this.widgetWindow.getWidgetBody().innerHTML = "";
if (this.widgetWindow.isMaximized()) {
this.widgetWindow.getWidgetBody().style.display = "flex";
this.widgetWindow.getWidgetBody().style.justifyContent = "space-between";
this.widgetWindow.getWidgetBody().style.padding = "0 2vw";
} else {
this.widgetWindow.getWidgetBody().style.padding = "0 0";
}
this.doAnalytics();
};
this.widgetWindow.sendToCenter();
}
/**
* Lazily loads Chart.js via RequireJS if not already available.
* @returns {Promise<void>}
*/
_ensureChartLoaded() {
if (typeof Chart !== "undefined") {
return Promise.resolve();
}
return new Promise((resolve, reject) => {
// eslint-disable-next-line no-undef
require(["Chart"], () => {
if (typeof Chart !== "undefined") {
resolve();
} else {
reject(new Error("Chart global not found after loading"));
}
}, reject);
});
}
/**
* Renders and carries out analysis of the MB project.
* @public
* @returns {void}
*/
doAnalytics() {
this.activity.blocks.activeBlock = null;
const myChart = docById("myChart");
const ctx = myChart.getContext("2d");
this.activity.loading = true;
document.body.style.cursor = "wait";
let myRadarChart = null;
const scores = analyzeProject(this.activity);
runAnalytics(this.activity);
const data = scoreToChartData(scores);
const __callback = () => {
const imageData = myRadarChart.toBase64Image();
const img = new Image();
img.src = imageData;
if (this.widgetWindow.isMaximized()) {
img.width = this.widgetWindow.getWidgetFrame().getBoundingClientRect().height - 80;
} else {
img.width = 200;
}
this.widgetWindow.getWidgetBody().appendChild(img);
this.activity.blocks.hideBlocks();
this.activity.showBlocksAfterRun = false;
document.body.style.cursor = "default";
};
const options = getChartOptions(__callback);
myRadarChart = new Chart(ctx).Radar(data, options);
this.jsonObject = document.createElement("ul");
this.jsonObject.style.float = "left";
this.widgetWindow.getWidgetBody().appendChild(this.jsonObject);
}
/**
* @public
* @param {Array} stats
* @returns {void}
*/
displayInfo(stats) {
const lowHertz = stats["lowestNote"][2] + 0.5;
const highHertz = stats["highestNote"][2] + 0.5;
this.jsonObject.innerHTML = `<li>duples: ${stats["duples"]}</li>
<li>triplets: ${stats["triplets"]}</li>
<li>quintuplets: ${stats["quintuplets"]}</li>
<li style=\"white-space: pre-wrap; width: 150px\">pitch names: ${Array.from(
stats["pitchNames"]
).join(", ")}</li>
<li>number of notes: ${stats["numberOfNotes"]}</li>
<li style=\"white-space: pre-wrap; width: 150px\">lowest note: ${
stats["lowestNote"][0]
},${lowHertz.toFixed(0)}Hz</li>
<li style=\"white-space: pre-wrap; width: 150px\">highest note: ${
stats["highestNote"][0]
},${highHertz.toFixed(0)}Hz</li>
<li>rests used: ${stats["rests"]}</li>
<li>ornaments used: ${stats["ornaments"]}</li>`;
}
}
/* istanbul ignore next */
if (typeof module !== "undefined") {
module.exports = StatsWindow;
}