-
Notifications
You must be signed in to change notification settings - Fork 0
/
beautifyscore.js
379 lines (298 loc) · 9.41 KB
/
beautifyscore.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
"use strict";
(function() {
hdots_prefs.registerPlugin("beauty_engine", "pportion3", "Proportional Layout", beautifyScore2);
hdots_prefs.registerPluginPreference("beauty_engine", "pportion3",
{
type: "text",
label: "One beat, measured in spaces",
name: "beatInSpaces",
def: 3.7
});
// Static to passOne and passTwo
var lineMels;
var lineCount;
function IncLineData() {
lineCount++;
lineMels[lineCount] = [];
}
function beautifyScore2(pref, staff, pass) {
switch(pass) {
case 1:
beautifyScorePassOne(pref, staff);
break;
case 2:
beautifyScorePassTwo(pref, staff);
break;
}
}
function beautifyScorePassTwo(pref, staff) {
var maxX = staff.details.maxX;
var i = 0, j;
var line;
var melsThisLine;
var lineLength;
var padding;
var mel;
for (i = 0; i < lineCount; i++) {
line = lineMels[i];
melsThisLine = line.length;
lineLength = line[melsThisLine-1].c.x;
padding = (maxX - lineLength) / melsThisLine;
//logit([maxX, melsThisLine, line[melsThisLine-1].toSource(), line[melsThisLine-1].c.x]);
//logit("Padding = " + padding);
for (j = 0; j < melsThisLine-1; j++) { // Don't do the staffControl
mel = line[j];
mel.paddingRight += padding;
}
}
}
function beautifyScorePassOne(pref, staff) {
var beatInPixels = +parseFloat(pref.beatInSpaces);
beatInPixels = beatInPixels * staff.details.space;
var beatUnit; // What length note takes one beat...
var beatCount = 0; // Count up parts of a beat...
var beatFraction;
var beatWidth;
var w;
var padding;
var lastx;
lineMels = [];
lineCount = -1; // So the new data starts at zero
IncLineData();
var lastMel;
score.data.forEach
(function(mel)
{
if (!mel) {return;}
if (mel.type === "melody" || mel.staffEnd) { // We like having the end of the staff padding out as well
lineMels[lineCount].push(mel);
if (lastMel) {
beatWidth = (beatInPixels * lastMel.beatFraction);
lastx = lastMel.c.x /* + lastMel.rect.width + lastMel.paddingRight */;
w = mel.c.x - lastx;
padding = beatWidth - w;
if (padding > 0) {
lastMel.paddingRight += padding;
}
//logit(["Beauty " + mel.note + ":" + mel.duration , beatWidth, mel.beatFraction, lastMel.paddingRight, beatWidth, w]);
}
lastMel = mel;
}
if (mel.type === "timesig") {
beatUnit = mel.beatUnit;
//logit(["Beauty Beat Unit", beatUnit]);
}
if (mel.newBar) {
mel.beatCount = beatCount;
beatCount = 0;
}
if (mel.staffEnd) {
lastMel = undefined;
IncLineData();
}
});
}
}());
hdots_prefs.registerPlugin("beauty_engine", "beat", "To the beat", beautifyScore);
hdots_prefs.registerPluginPreference("beauty_engine", "beat",
{
type: "text",
label: "Length of line, in pixels",
name: "linelen",
def: "2000"
});
hdots_prefs.registerPlugin("beauty_engine", "natural", "The natural layout", dummy);
function dummy(pref, staff) {
}
function beautifyScore(pref, staff, pass) {
if (pass === 2) {return;} // Second pass not needed
var FORCEWIDTH = +pref.linelen;
var i, l, data;
var mel, mel2, measureList, melodyNoteList;
var lastBarMel;
var measureNumber;
var lineNumber, lineMeasureNumber;
var beatUnit, beatsPerBar;
var maxMeasuresInLine = 0;
var beatCount = 0;
var beatCountOnLine = 1;
var t, tmp;
var isMeasureStart;
data = score.data;
l = data.length;
measureList = [];
melodyNoteList = [];
measureNumber = 0;
lineNumber = 1;
lineMeasureNumber = 0;
var spaceForLeadIn = 0; // How much space to reserve for lead ins.
var BEATLENGTH;
var idx = 0;
var len = data.length;
var a;
var mel, prevMel, staffMel;
var newX;
// Find the next start of a measure bar.
// Sometimes there are measure bars with no melody notes inside, such
// as when a bar starts off the beginning of a line, then a repeat bar
// shows up after the time sig. Basicly, any melody bar that indicates
// it has beats.
function getMeasureStart() {
var mel;
while (idx < len) {
mel = data[idx];
idx++;
if (mel.newBar && mel.measureLength && mel.beatsPerBar) {return mel; }
}
}
// And read off the melody notes to the end of the line. Also return
// the number of measures in this line for later calculation.
function getlineMels() {
var mel;
var a = [];
var i;
var m = 0;
while (idx < len) {
mel = data[idx];
idx++;
if (mel.type === "melody") {a.push(mel);}
if (mel.newBar) {m++;}
if (mel.staffEnd) {idx--; break;}
}
return {
a: a,
m:m
};
}
// This sets a mel's position as fixed. It is used by the drawing
// routine to forceably set the note position and work from there.
function setFixedPosition(mel, toX) {
mel.forceToX = toX;
}
// There are some things that should be immune from being forced.
// Things like the clef, key signatures, time signatures.
// Also, the leadins are left alone.
//
// But we do measure the length of the longest lead in and set the
// spaceForLeadIn to that.
(function() {
var inLeadIn = false;
isMeasureStart = true;
var getNextMelodyX = false;
isMeasureStart = true;
l = data.length;
for (i = 0; i < l; i++) {
mel = data[i];
if (!mel.c) {continue;}
if (mel.isLeadIn) {
inLeadIn = true;
getNextMelodyX = true;
} else if (mel.newBar) {
inLeadIn = false;
}
if (inLeadIn) {
mel.noForceX = true;
} else {
if (mel.type === "melody") {
if (getNextMelodyX) {
if (mel.c && typeof mel.c.x === "number") {spaceForLeadIn = Math.max(spaceForLeadIn, mel.c.x)};
getNextMelodyX = false;
}
isMeasureStart = false;
}
if (inLeadIn) {mel.noForceX = true};
if (mel.type === "gracenote") { isMeasureStart = false; }
if (mel.staffEnd) { isMeasureStart = true;}
if (isMeasureStart) { mel.noForceX = true;}
}
}
}());
var offSet;
var m;
// Start collecting data
while (((staffMel = getMeasureStart()) !== undefined)) {
//logit(["StaffMel", staffMel]);
// Get the info for this line of music.
a = getlineMels();
//logit(a);
m = a.m; // The number of measures
a = a.a; // and the melody note list.
// If the first measure is a lead in, don't count it towards the nummber
// of measures.
if ( staffMel.isLeadIn) {m--;}
//logit(dumpNotes(a));
// Set the width of each beat in pixels.
BEATLENGTH = FORCEWIDTH/(staffMel.beatsPerBar*m);
offSet = spaceForLeadIn;
// Run through the melody notes on the line.
for (i = 0; i < a.length; i++) {
mel = a[i];
if (mel.noForceX) {continue;} // Some of them are to be left alone.
newX = offSet; // The position for this note
offSet += BEATLENGTH * mel.beatFraction; // And for the next note..
//logit(["Adjusting", staffMel.beatsPerBar, m, BEATLENGTH, mel.beatFraction, newX]);
setFixedPosition(mel, newX);
}
};
// Now, we walk through the data once more -- backwards.
// And we shift most of the other mel's around to slide back in front
// of the melody note they are attached to.
l = data.length;
var offSet = 0;
for (i = 0; i < l; i++) {
mel = data[l-i];
if (!mel) {continue;}
if (!mel.c) {continue;}
if (mel.type === "melody") {
isMeasureStart = false;
mel2 = mel;
offSet = mel.c.x - mel.forceToX;
} else {
//logit(mel.type + " offset = " + offSet);
if (!mel.noForceX) {
mel.forceToX = mel.c.x - offSet;
}
}
if (mel.staffEnd) { mel.forceToX = FORCEWIDTH + spaceForLeadIn;}
}
}
/* Debugging, can be dropped later */
function dumpNotes(a) {
var o;
var i;
var mel;
var s = [];
for (i = 0; i < a.length; i++) {
s.push(a[i].bww);
}
return s.join(" ");
}
function calcNewStaffGap(staff) {
//staffGap = calcNewStaffGap(staff);
var stuff = [];
var maxGap = 0;
var gap;
var i;
// Find the highest and lowest points on each staff line.
score.data.forEach
(function(mel)
{
var r = mel.rect;
if (!r) {return;}
var y = r.y;
var y1 = r.y + r.height;
if (isNaN(y) || isNaN(y1)) {return;}
var maxY = Math.max(y, y1);
var minY = Math.min(y, y1);
var lineDet = stuff[mel.c.staffCounter];
if (!lineDet) {lineDet = {maxY:0, minY:999999}; }
lineDet.maxY = Math.max(lineDet.maxY, maxY);
lineDet.minY = Math.min(lineDet.minY, minY);
stuff[mel.c.staffCounter] = lineDet;
});
for (i = 0+2; i < stuff.length; i++) {
gap = stuff[i].minY - stuff[i-1].maxY;
maxGap = Math.max(maxGap, gap);
}
return maxGap + (1 * staff.details.space);
}