-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
count-note-beats_mu͒2.qml
412 lines (398 loc) · 10.5 KB
/
count-note-beats_mu͒2.qml
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*-
* Copyright © 2020, 2021, 2023
* mirabilos <m@mirbsd.org>
*
* Provided that these terms and disclaimer and all copyright notices
* are retained or reproduced in an accompanying document, permission
* is granted to deal in this work without restriction, including un‐
* limited rights to use, publicly perform, distribute, sell, modify,
* merge, give away, or sublicence.
*
* This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to
* the utmost extent permitted by applicable law, neither express nor
* implied; without malicious intent or gross negligence. In no event
* may a licensor, author or contributor be held liable for indirect,
* direct, other damage, loss, or other issues arising in any way out
* of dealing in the work, even if advised of the possibility of such
* damage or existence of a defect, except proven that it results out
* of said person’s immediate fault when using the work as intended.
*-
* Number notes’ beats. Note that short measures could be aligned any
* conceivable way (beginning, end, middle) so we assume beginning as
* MuseScore doesn’t record short measure alignment.
*
* Makes use of some techniques demonstrated by the MuseScore example
* plugins. No copyright is claimed for these or the API extracts.
*
* mu͒2 note: measure noOffset and irregular properties aren’t exposed
* to QML, so we’re counting measures one by one here (instead of the
* proper measure number). Port to mu͒2 done for Jinx from IRC ☺
*/
import MuseScore 1.0
MuseScore {
description: "This mu͒2 plugin inserts beat numbers as staff text.";
version: "3";
menuPath: "Plugins.Notes.Count note beats";
function buildMeasureMap(score) {
var map = {};
var no = 1;
var cursor = score.newCursor();
cursor.rewind(0);
var lastTS = null;
while (cursor.measure) {
var m = cursor.measure;
var tick = m.firstSegment.tick;
var thisTS = null;
var s = m.firstSegment;
while (s !== null) {
if (s.segmentType === Segment.ChordRest)
break;
if (s.segmentType === Segment.All)
throw new Error("Segment.All found");
if (s.segmentType !== Segment.TimeSig) {
s = s.nextInMeasure;
continue;
}
console.log("TimeSig found in m." + no);
var e = s.elementAt(0);
if (e === null) {
console.log("TimeSig was nil");
thisTS = null;
break;
}
thisTS = [e.denominator, e.numerator];
break;
}
if (thisTS !== null)
lastTS = thisTS;
if (lastTS === null)
throw new Error("ERROR: no time signature for m." + no);
var tsD = lastTS[0];
var tsN = lastTS[1];
var ticksB = division * 4.0 / tsD;
var ticksM = ticksB * tsN;
//no += m.noOffset;
var cur = {
"tick": tick,
"tsD": tsD,
"tsN": tsN,
"ticksB": ticksB,
"ticksM": ticksM,
"past" : (tick + ticksM),
"no": no
};
map[cur.tick] = cur;
console.log(tsN + "/" + tsD + " measure " + no +
" at tick " + cur.tick + " length " + ticksM);
//if (!m.irregular)
++no;
cursor.nextMeasure();
}
return map;
}
function showPos(cursor, measureMap) {
var t = cursor.segment.tick;
var m = measureMap[cursor.measure.firstSegment.tick];
var b = "?";
if (m && t >= m.tick && t < m.past) {
b = 1 + (t - m.tick) / m.ticksB;
}
return "St" + (cursor.staffIdx + 1) +
" Vc" + (cursor.voice + 1) +
" Ms" + m.no + " Bt" + b;
}
function nameElementType(elementType) {
switch (elementType) {
case Element.ACCIDENTAL:
return "ACCIDENTAL";
case Element.AMBITUS:
return "AMBITUS";
case Element.ARPEGGIO:
return "ARPEGGIO";
case Element.ARTICULATION:
return "ARTICULATION";
case Element.BAGPIPE_EMBELLISHMENT:
return "BAGPIPE_EMBELLISHMENT";
case Element.BAR_LINE:
return "BAR_LINE";
case Element.BEAM:
return "BEAM";
case Element.BEND:
return "BEND";
case Element.BRACKET:
return "BRACKET";
case Element.BRACKET_ITEM:
return "BRACKET_ITEM";
case Element.BREATH:
return "BREATH";
case Element.CHORD:
return "CHORD";
case Element.CHORDLINE:
return "CHORDLINE";
case Element.CLEF:
return "CLEF";
case Element.COMPOUND:
return "COMPOUND";
case Element.DYNAMIC:
return "DYNAMIC";
case Element.ELEMENT:
return "ELEMENT";
case Element.ELEMENT_LIST:
return "ELEMENT_LIST";
case Element.FBOX:
return "FBOX";
case Element.FERMATA:
return "FERMATA";
case Element.FIGURED_BASS:
return "FIGURED_BASS";
case Element.FINGERING:
return "FINGERING";
case Element.FRET_DIAGRAM:
return "FRET_DIAGRAM";
case Element.FSYMBOL:
return "FSYMBOL";
case Element.GLISSANDO:
return "GLISSANDO";
case Element.GLISSANDO_SEGMENT:
return "GLISSANDO_SEGMENT";
case Element.HAIRPIN:
return "HAIRPIN";
case Element.HAIRPIN_SEGMENT:
return "HAIRPIN_SEGMENT";
case Element.HARMONY:
return "HARMONY";
case Element.HBOX:
return "HBOX";
case Element.HOOK:
return "HOOK";
case Element.ICON:
return "ICON";
case Element.IMAGE:
return "IMAGE";
case Element.INSTRUMENT_CHANGE:
return "INSTRUMENT_CHANGE";
case Element.INSTRUMENT_NAME:
return "INSTRUMENT_NAME";
case Element.JUMP:
return "JUMP";
case Element.KEYSIG:
return "KEYSIG";
case Element.LASSO:
return "LASSO";
case Element.LAYOUT_BREAK:
return "LAYOUT_BREAK";
case Element.LEDGER_LINE:
return "LEDGER_LINE";
case Element.LET_RING:
return "LET_RING";
case Element.LET_RING_SEGMENT:
return "LET_RING_SEGMENT";
case Element.LYRICS:
return "LYRICS";
case Element.LYRICSLINE:
return "LYRICSLINE";
case Element.LYRICSLINE_SEGMENT:
return "LYRICSLINE_SEGMENT";
case Element.MARKER:
return "MARKER";
case Element.MEASURE:
return "MEASURE";
case Element.MEASURE_LIST:
return "MEASURE_LIST";
case Element.MEASURE_NUMBER:
return "MEASURE_NUMBER";
case Element.NOTE:
return "NOTE";
case Element.NOTEDOT:
return "NOTEDOT";
case Element.NOTEHEAD:
return "NOTEHEAD";
case Element.NOTELINE:
return "NOTELINE";
case Element.OSSIA:
return "OSSIA";
case Element.OTTAVA:
return "OTTAVA";
case Element.OTTAVA_SEGMENT:
return "OTTAVA_SEGMENT";
case Element.PAGE:
return "PAGE";
case Element.PALM_MUTE:
return "PALM_MUTE";
case Element.PALM_MUTE_SEGMENT:
return "PALM_MUTE_SEGMENT";
case Element.PART:
return "PART";
case Element.PEDAL:
return "PEDAL";
case Element.PEDAL_SEGMENT:
return "PEDAL_SEGMENT";
case Element.REHEARSAL_MARK:
return "REHEARSAL_MARK";
case Element.REPEAT_MEASURE:
return "REPEAT_MEASURE";
case Element.REST:
return "REST";
case Element.SCORE:
return "SCORE";
case Element.SEGMENT:
return "SEGMENT";
case Element.SELECTION:
return "SELECTION";
case Element.SHADOW_NOTE:
return "SHADOW_NOTE";
case Element.SLUR:
return "SLUR";
case Element.SLUR_SEGMENT:
return "SLUR_SEGMENT";
case Element.SPACER:
return "SPACER";
case Element.STAFF:
return "STAFF";
case Element.STAFFTYPE_CHANGE:
return "STAFFTYPE_CHANGE";
case Element.STAFF_LINES:
return "STAFF_LINES";
case Element.STAFF_LIST:
return "STAFF_LIST";
case Element.STAFF_STATE:
return "STAFF_STATE";
case Element.STAFF_TEXT:
return "STAFF_TEXT";
case Element.STEM:
return "STEM";
case Element.STEM_SLASH:
return "STEM_SLASH";
case Element.STICKING:
return "STICKING";
case Element.SYMBOL:
return "SYMBOL";
case Element.SYSTEM:
return "SYSTEM";
case Element.SYSTEM_DIVIDER:
return "SYSTEM_DIVIDER";
case Element.SYSTEM_TEXT:
return "SYSTEM_TEXT";
case Element.TAB_DURATION_SYMBOL:
return "TAB_DURATION_SYMBOL";
case Element.TBOX:
return "TBOX";
case Element.TEMPO_TEXT:
return "TEMPO_TEXT";
case Element.TEXT:
return "TEXT";
case Element.TEXTLINE:
return "TEXTLINE";
case Element.TEXTLINE_BASE:
return "TEXTLINE_BASE";
case Element.TEXTLINE_SEGMENT:
return "TEXTLINE_SEGMENT";
case Element.TIE:
return "TIE";
case Element.TIE_SEGMENT:
return "TIE_SEGMENT";
case Element.TIMESIG:
return "TIMESIG";
case Element.TREMOLO:
return "TREMOLO";
case Element.TREMOLOBAR:
return "TREMOLOBAR";
case Element.TRILL:
return "TRILL";
case Element.TRILL_SEGMENT:
return "TRILL_SEGMENT";
case Element.TUPLET:
return "TUPLET";
case Element.VBOX:
return "VBOX";
case Element.VIBRATO:
return "VIBRATO";
case Element.VIBRATO_SEGMENT:
return "VIBRATO_SEGMENT";
case Element.VOLTA:
return "VOLTA";
case Element.VOLTA_SEGMENT:
return "VOLTA_SEGMENT";
default:
return "(Element." + (elementType + 0) + ")";
}
}
/** signature: applyToSelectionOrScore(cb, ...args) */
function applyToSelectionOrScore(cb) {
var args = Array.prototype.slice.call(arguments, 1);
var staveBeg;
var staveEnd;
var tickEnd;
var rewindMode;
var toEOF;
var cursor = curScore.newCursor();
cursor.rewind(1);
if (cursor.segment) {
staveBeg = cursor.staffIdx;
cursor.rewind(2);
staveEnd = cursor.staffIdx;
if (!cursor.tick) {
/*
* This happens when the selection goes to the
* end of the score — rewind() jumps behind the
* last segment, setting tick = 0.
*/
toEOF = true;
} else {
toEOF = false;
tickEnd = cursor.tick;
}
rewindMode = 1;
} else {
/* no selection */
staveBeg = 0;
staveEnd = curScore.nstaves - 1;
toEOF = true;
rewindMode = 0;
}
for (var stave = staveBeg; stave <= staveEnd; ++stave) {
for (var voice = 0; voice < 4; ++voice) {
cursor.staffIdx = stave;
cursor.voice = voice;
cursor.rewind(rewindMode);
/*XXX https://musescore.org/en/node/301846 */
cursor.staffIdx = stave;
cursor.voice = voice;
while (cursor.segment &&
(toEOF || cursor.tick < tickEnd)) {
if (cursor.element)
cb.apply(null,
[cursor].concat(args));
cursor.next();
}
}
}
}
function labelBeat(cursor, measureMap, doneMap) {
//console.log(showPos(cursor, measureMap) + ": " +
// nameElementType(cursor.element.type));
if (cursor.element.type !== Element.CHORD)
return;
var t = cursor.segment.tick;
if (doneMap[t])
return;
doneMap[t] = true;
var m = measureMap[cursor.measure.firstSegment.tick];
var text = newElement(Element.STAFF_TEXT);
if (m && t >= m.tick && t < m.past) {
var b = 1 + (t - m.tick) / m.ticksB;
text.text = "" + b;
} else {
text.text = "?";
}
if (text.text == "")
return;
text.placement = Element.ABOVE;
cursor.add(text);
}
onRun: {
var measureMap = buildMeasureMap(curScore);
var doneMap = {};
applyToSelectionOrScore(labelBeat, measureMap, doneMap);
}
}