forked from BernardGreenberg/MuseScorePlugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriller.qml
794 lines (681 loc) · 23.5 KB
/
triller.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
//=============================================================================
// MuseScore
// Music Composition & Notation
//
// Copyright (C) 2012 Werner Schweer
// Copyright (C) 2013-2017 Nicolas Froment, Joachim Schmitz
// Copyright (C) 2019-2020 Bernard Greenberg
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2
// as published by the Free Software Foundation and appearing in
// the file LICENCE.GPL
//
// Documentation: https://musescore.org/en/project/articulation-and-ornamentation-control
// Version 3.3 13 Jan 2020 decodes ornamentation currently on notes
// Version 3.4 14 Jan 2020 improvements/fixes to 3.3, see CHANGES.md
// Version 3.5 15 Jan 2020 integrate read/write auxiliary handling, report note names.
// Version 3.6 10 Apr 2020 baroque trill is now an option. If unchecked, normal trill is generated.
//=============================================================================
import QtQuick 2.2
import MuseScore 3.0
import QtQuick.Controls 1.1
import QtQuick.Controls.Styles 1.3
import QtQuick.Layouts 1.1
import QtQuick.Dialogs 1.2
import Qt.labs.settings 1.0
MuseScore {
version: "3.5"
description: "This plugin generates custom trills with baroque details."
menuPath: "Plugins.Triller"
id: dlg
pluginType: "dialog"
requiresScore: true
property int margin: 10
property var the_note : null;
width: 640
height: 480
/* Facilitates consistent automatic manipulation of the checkboxes. */
property int n_aux_vals: 3
property var oben_fields: [{box: obenSemi, val: 1, tpc: -5},
{box: obenWhole, val: 2, tpc: 2},
{box: obenSesqui, val: 3, tpc: -3}]
property var unten_fields: [{box: untenSemi, val: -1, tpc: 5},
{box: untenWhole, val: -2, tpc: -2},
{box: untenSesqui, val: -3, tpc: 3}]
onRun: {
if ((mscoreMajorVersion < 3) || (mscoreMinorVersion < 3)) {
versionError.open()
Qt.quit();
return;
}
var note = find_usable_note();
if (note) {
the_note = note;
if (!analyze_current_ornaments(note))
beatsField.text = ""; // "Hoc malum fecit signum..."
noteName.text = get_note_name(note)
augment_auxiliary_names(oben_fields, note.tpc1);
augment_auxiliary_names(unten_fields, note.tpc1);
} else {
complain("No ornamentable note selected.")
Qt.quit();
}
}
Settings {
id: settings
category: "Plugin-Triller"
property alias midpointSlider: midpointSlider.value
property alias curveType: curveType.currentIndex
}
function getFloatFromInput(input)
{
var value = input.text;
if (value == "") {
value = input.placeholderText;
}
return parseFloat(value);
}
function getExpFromInput(input)
{
return getFloatFromInput(input) / 100;
}
function set_auxiliary_check(aux, val, default_val) {
val = val || default_val; //If 0, use default.
for (var i = 0; i < n_aux_vals; i++) {
var field = aux[i];
field.box.checked = (val == field.val);
}
}
function augment_auxiliary_names(aux, tpc) {
for (var i = 0; i < n_aux_vals; i++) {
var field = aux[i];
field.box.text += " (" + get_tpc_name(tpc + field.tpc) + ")"
}
}
function unique_count(arry) {
var ctr = {}
for (var i = 0; i < arry.length; i++) {
var elt = arry[i];
ctr[elt] = (ctr[elt] || 0) + 1;
}
return Object.keys(ctr).length;
}
function compare_pattern(data, start, pattern) {
for (var i = 0; i < pattern.length; i++) {
if (data[start] !== pattern[i]) // overrun end ok
return false;
start += 1;
}
return true;
}
function analyze_current_ornaments(note) {
var events = note.playEvents;
var N = events.length;
console.log("N events", N);
//Assay the "current events", as it were.
//MS non-Baroque trills will be accepted, but confirmation baroquizes.
var lens = new Array(N);
var pitches = new Array(N);
for (var i = 0; i < N; i++) {
lens[i] = events[i].len;
pitches[i] = events[i].pitch;
}
console.log("Lens", lens);
console.log("Pitches", pitches);
var maxlen = Math.max.apply(Math, lens)
var minlen = Math.min.apply(Math, lens)
var nlens = unique_count(lens);
var maxpitch = Math.max.apply(Math, pitches)
var minpitch = Math.min.apply(Math, pitches)
var npitches = unique_count(pitches);
console.log("maxlen", maxlen, "minlen", minlen,
"maxpitch", maxpitch, "minpitch", minpitch,
"npitches", npitches, "nlens", nlens);
// See if we think we understand this ornament. Not too short or too florid,
// and longest note cannot be other than last.
if (N < 3 || npitches > 3 || nlens > 2 || maxlen != lens[N-1]) {
console.log("No extant recognizable ornament.");
return false;
}
var start_main_trill = (npitches == 2 && nlens == 1 && minpitch == 0
&& compare_pattern(pitches, 0, [0 , maxpitch, 0]));
// If it doesn't end on the main note: either it has a Nachschlag or it is not baroque
if (pitches[N-1] != 0) {
if (start_main_trill) {
// MS non-Baroque trills start from lower note (main pitch). This is the case detected here...
console.log("Start-main end-upper MS trill detected.");
} else if (pitches[N-1] == minpitch) { // probably has a Nachschlag
console.log("Start-main end-upper MS trill with Nachschlag detected.");
} else{ // ... but nothing else.
console.log("No valid trill detected.");
return false;
}
}
// we're good to go -- set the beats field "good" ...
beatsField.text = N;
// Set the oben/unten pitch checkboxes correctly
set_auxiliary_check(oben_fields, maxpitch, 2);
set_auxiliary_check(unten_fields, minpitch, -1);
// Check for long final beat.
// pitch==0 already checked, as well as len being greatest or same as all
if (lens[N-1] > minlen+2) { // rounding
finalField.text = lens[N-1] - minlen;
// beats still means the same !!
}
// ToDo: there is no Vorschlaege or Nachschlaege if it's not baroque (baroque means start from upper). Vorschlaege and Nachschlaege only make sense when it starts from lower. Check this and treat it, maybe leaving a message.
// look for Vorschlaege
if (compare_pattern(pitches, 0, [maxpitch, 0, minpitch, 0])) {
vorOben.checked = true;
keinVorschlag.checked = false;
cb_baroque.checked = true;
} else if (compare_pattern(pitches, 0, [minpitch, 0, maxpitch, 0])) {
vorUnten.checked = true;
keinVorschlag.checked = false;
cb_baroque.checked = true;
}
// Look for final mordent
if ( compare_pattern(lens, N-4, [minlen, minlen, minlen, minlen])
&& compare_pattern(pitches, N-4, [maxpitch, 0, minpitch, 0])) {
nachMordent.checked = true;
cb_baroque.checked = true;
} else if ( compare_pattern(lens, N-4, [minlen, minlen, minlen, minlen])
&& compare_pattern(pitches, N-4, [0, maxpitch, 0, minpitch])) {
nachMordent.checked = true;
cb_baroque.checked = false;
} else if (pitches[N-1] == 0) { // Look for baroque if there is no final mordent
cb_baroque.checked = true;
} else {
cb_baroque.checked = false;
}
return true;
}
function get_tpc_name(tpc){
var based_0 = tpc + 1;
var result = "FCGDAEB"[based_0 % 7];
var divergence = Math.floor(based_0 / 7);
var appenda = ["bb", "b", "", "#", "##"];
return result + appenda[divergence]
}
function get_note_name(note) {
if (note == undefined) {
return note;
}
var tpc = get_tpc_name(note.tpc1)
return tpc + get_octave_name(tpc, note.pitch)
function get_octave_name(tpc, pitch) {
var answer = Math.floor(pitch / 12) - 1
if (tpc == "B#" || tpc == "B##") {
answer -= 1;
} else if (tpc == "Cb" || tpc == "Cbb") {
answer += 1;
}
return answer + "";
}
}
function find_usable_note() {
var selection = curScore.selection;
var elements = selection.elements;
if (elements.length == 1) { // We have a selection list to work with...
var element = elements[0]
if (element.type == Element.NOTE) {
return element;
}
}
return false;
}
function get_checked_aux_step(aux) {
for (var i = 0; i < n_aux_vals; i++)
if (aux[i].box.checked)
return aux[i].val;
} //undefined errs out, but "can't happen".
function generateTrill() {
console.log("generateTrill");
var note = the_note;
if (!note) {
console.log("No note at Apply time.")
return false;
}
var oben_steps = get_checked_aux_step(oben_fields);
var unten_steps = get_checked_aux_step(unten_fields);
var vor_von_oben = vorOben.checked;
var vor_von_unten = vorUnten.checked;
var nachschlag_mordent = nachMordent.checked;
var baroque_trill = cb_baroque.checked;
var beats = parseInt(beatsField.text);
if (isNaN(beats)){
return false;
}
if (beats & 1) {
beats += 1
}
var final_milles = parseInt(finalField.text);
if (isNaN(final_milles) || final_milles < 0 || final_milles >=1000) {
return false;
}
var program = []
var trill_beats = beats
function ppush(prog) {
var len = prog.length;
trill_beats -= len;
for (var i = 0; i < len; i++)
program.push(prog[i]);
}
if (nachschlag_mordent) {
trill_beats -= 2;
}
if (vor_von_oben) {
ppush([oben_steps, 0, unten_steps, 0]);
} else if (vor_von_unten){
ppush([unten_steps, 0]);
}
if (trill_beats < 0) {
complain("Trill beats specified too few to cover request.");
return false;
}
var reps = Math.floor(trill_beats / 2);
for (var i = 0; i < reps; i++ ) {
if (baroque_trill) {
ppush([oben_steps, 0]);
} else {
ppush([0, oben_steps]);
}
}
if (nachschlag_mordent) {
if (baroque_trill) {
ppush([unten_steps, 0]);
} else {
ppush([0, unten_steps]);
}
}
return function() {install_trill(note, program, final_milles);};
}
function install_trill(note, program, final_milles) {
var trillable_len = 1000 - final_milles;
var len = Math.floor(trillable_len/program.length);
var time = 0;
var time_lin = 0;
console.log("Final program:", program)
curScore.startCmd();
note.playEvents = []
var playEvents = note.playEvents;
// for expressive expansion:
var midPoint = ((curveType.isLinear) ? 50.0 : midpointSlider.value) / 100; //linear == hit midpoint at 50% tickRange
var p = Math.log(0.5) / Math.log(midPoint);
console.log("Expansion p:", p)
for (var i = 0; i < program.length; i++) {
var pevt = note.createPlayEvent()
pevt.pitch = program[i];
pevt.ontime = time;
if (final_milles && i == program.length - 1) {
len += final_milles;
}
pevt.len = len;
playEvents.push(pevt) // Append to list
time_lin += len;
// expression
time = trillable_len*Math.pow((i + 1)/program.length, p);
}
curScore.endCmd();
}
function maybe_finish() {
var continuation = generateTrill();
if (continuation) {
continuation();
Qt.quit();
}
}
GridLayout {
id: 'mainLayout'
anchors.fill: parent
anchors.margins: 10
columns: 3
focus: true
// Row 0
Label {
text: "Note"
Layout.columnSpan:2
}
Label {
id: noteName
text: " "
}
// Row 1
Label {
text: "Oben ="
}
// These "radio buttons" don't exclude each other.
// We have to teach them how to do their job.
RowLayout {
Layout.columnSpan:2
RadioButton {
id: obenSesqui
//checked: true
text: qsTr("3/2 Ton")
onClicked: {
obenSemi.checked = false
obenWhole.checked = false
}
}
RadioButton {
id: obenWhole
checked: true
text: qsTr("Ton")
onClicked: {
obenSemi.checked = false
obenSesqui.checked = false
}
}
RadioButton {
id: obenSemi
text: qsTr("Halbton")
Layout.columnSpan:2
onClicked: {
obenWhole.checked = false
obenSesqui.checked = false
}
}
}
// Row 2
Label {
text: "Unten ="
}
RowLayout {
Layout.columnSpan:2
RadioButton {
id: untenSesqui
text: qsTr("3/2 Ton")
onClicked: {
untenSemi.checked = false
untenWhole.checked = false
}
}
RadioButton {
id: untenWhole
text: qsTr("Ton")
onClicked: {
untenSemi.checked = false
untenSesqui.checked = false
}
}
RadioButton {
id: untenSemi
checked: true
text: qsTr("Halbton")
Layout.columnSpan:2
onClicked : {
untenWhole.checked = false
untenSesqui.checked = false
}
}
}
// Row 3
Label {
text: "Vorschlag"
}
RowLayout {
Layout.columnSpan:2
RadioButton {
id: vorOben
text: qsTr("von oben")
onClicked: {
vorUnten.checked = false
keinVorschlag.checked = false
}
}
RadioButton {
id: vorUnten
text: qsTr("von unten")
onClicked: {
vorOben.checked = false
keinVorschlag.checked = false
}
}
RadioButton {
id: keinVorschlag
checked: true
text: qsTr("kein")
onClicked: {
vorUnten.checked = false
vorOben.checked = false
}
}
}
// Row 4
Label {
text: " "
}
RowLayout {
Layout.columnSpan:2
CheckBox {
id: nachMordent
checked: false
text: qsTr("Nachschlag-Mordent")
}
CheckBox {
id: cb_baroque
checked: true
text: qsTr("Barock")
}
}
// Row 5
Label {
text: "Schläge"
}
TextField {
id: beatsField
implicitHeight: 24
Layout.columnSpan:2
text: "8"
focus: true
Keys.onReturnPressed : {
maybe_finish()
}
Keys.onEscapePressed : {
Qt.quit()
}
}
// Row 6
Label {
text: "Final ‰"
}
TextField {
id: finalField
implicitHeight: 24
Layout.columnSpan:2
text: "0"
focus: false
Keys.onReturnPressed: {
maybe_finish()
}
Keys.onEscapePressed: {
Qt.quit()
}
}
// Row 7
Label {
text: qsTr("Expressive expansion:")
Layout.columnSpan:2
}
Canvas {
id: canvas
Layout.rowSpan: 4
Layout.minimumWidth: 102
Layout.minimumHeight: 102
Layout.fillWidth: true
Layout.fillHeight: true
onPaint: {
var w = canvas.width;
var h = canvas.height;
var ctx = getContext("2d");
//square plot area
var length = (w > h) ? h : w;
var top = (h - length) / 2;
var left = (w - length) / 2;
ctx.clearRect(0, 0, w, h);
ctx.fillStyle = '#555555';
ctx.fillRect(left, top, length, length);
ctx.strokeStyle = '#000000';
ctx.lineWidth = 1;
ctx.strokeRect(left, top, length, length);
//grid lines
ctx.strokeStyle = '#888888';
ctx.beginPath();
var divisions = 4;
for (var i = divisions - 1; i > 0; --i) {
//vertical
ctx.moveTo(left + ((i*length)/divisions), top);
ctx.lineTo(left + ((i*length)/divisions), top+length);
//horizontal
ctx.moveTo(left , top + ((i*length)/divisions));
ctx.lineTo(left + length, top + ((i*length)/divisions));
}
ctx.stroke();
//graph
ctx.strokeStyle = '#abd3fb';
ctx.lineWidth = 2;
var start = 0; //= getFloatFromInput(startExpValue);
var end = 100;
var midPoint = ((curveType.isLinear) ? 50.0 : midpointSlider.value) / 100;
ctx.beginPath();
ctx.moveTo(left + length, (start > end) ? top + length : top);
for (var x = length; x >= 0; --x) {
var outputPct = Math.pow((x / length), (Math.log(0.5) / Math.log(midPoint)));
var newY = (start > end) ? (top + (outputPct * length)) : (top + length - (outputPct * length));
ctx.lineTo(left + x, newY);
}
ctx.stroke();
}
} //end of Canvas
ComboBox {
id: curveType
model: ListModel {
ListElement { text: qsTr("Linear") }
ListElement { text: qsTr("Curved") }
}
Layout.preferredWidth: 100
Layout.columnSpan:2
property bool isLinear: {
return (curveType.currentText === qsTr("Linear"));
}
onCurrentIndexChanged: {
canvas.requestPaint();
}
}
Label {
text: qsTr("midpoint:")
Layout.alignment: Qt.AlignRight
}
Slider {
id: midpointSlider
Layout.fillWidth: true
minimumValue: 25 //1
maximumValue: 75 //99 -> range reduced to make the centre less sensitive
value: 50.0
stepSize: 0.1
enabled: !curveType.isLinear
style: SliderStyle {
groove: Rectangle { //background
id: grooveRect
implicitHeight: 6
color: (enabled) ? '#555555' : '#565656'
radius: implicitHeight
border {
color: '#888888'
width: 1
}
Rectangle {
//value fill
implicitHeight: grooveRect.implicitHeight
implicitWidth: styleData.handlePosition
color: (enabled) ? '#abd3fb' : '#567186'
radius: grooveRect.radius
border {
color: '#888888'
width: 1
}
}
}
handle: Rectangle {
anchors.centerIn: parent
color: (enabled) ? (control.pressed ? '#ffffff': '#d8d8d8') : '#565656'
border.color: '#666666'
border.width: 1
implicitWidth: 16
implicitHeight: 16
radius: 8
}
}
}
RowLayout {
Layout.alignment: Qt.AlignHCenter
SpinBox {
id: sliderValue
Layout.preferredWidth: 60
minimumValue: midpointSlider.minimumValue
maximumValue: midpointSlider.maximumValue
value: midpointSlider.value
decimals: 1
stepSize: midpointSlider.stepSize
onValueChanged: {
midpointSlider.value = value;
canvas.requestPaint();
}
enabled: !curveType.isLinear
}
Label { text: '%' }
}
// Row 8
Button {
id: applyButton
Layout.columnSpan:2
text: qsTranslate("PrefsDialogBase", "Apply")
onClicked: {
maybe_finish()
}
}
Button {
id: cancelButton
Layout.columnSpan: 1
text: qsTranslate("InsertMeasuresDialogBase", "Cancel")
onClicked: {
Qt.quit()
}
}
} // End of GridLayout
function complain(text) {
complaintDialog.text = text;
complaintDialog.open();
}
MessageDialog {
id: complaintDialog
icon: StandardIcon.Warning
standardButtons: StandardButton.Ok
modality: Qt.ApplicationModal
title: "Trill generator usage error"
text: "Triller usage error."
onAccepted: {
Qt.quit()
}
}
MessageDialog {
id: versionError
visible: false
title: qsTr("Unsupported MuseScore Version")
text: qsTr("This plugin needs MuseScore 3.3 or later")
onAccepted: {
Qt.quit()
}
}
Keys.onEscapePressed: {
dlg.parent.Window.window.close();
}
Keys.onReturnPressed: {
maybe_finish();
dlg.parent.Window.window.close();
}
Keys.onEnterPressed: {
maybe_finish();
dlg.parent.Window.window.close();
}
}