-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdurationeditor.qml
2567 lines (2143 loc) · 90 KB
/
durationeditor.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
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import QtQuick 2.9
import QtQuick.Controls 2.2
import MuseScore 3.0
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2
import QtQuick.Layouts 1.1
import FileIO 3.0
import "zparkingb/selectionhelper.js" as SelHelper
import "zparkingb/notehelper.js" as NoteHelper
import "durationeditor"
/**********************
/* Parking B - DurationEditor - New approach for note duration edition
/* v1.3.0
/* ChangeLog:
/* - 1.1.0: Initial releasee
/* - 1.3.0.alpha1: New Tuplet functionality
/* - 1.3.0.beta1: Bugfix for measures <> 4/4
/* - 1.3.0.beta1: Improved rest augmentation
/* - 1.3.0.beta1: AddTuplet finalization
/* - 1.3.0.beta1: RemoveTuplet
/* - 1.3.0.beta2: Paste correct accidentals
/* - 1.3.0.beta2: SetDurationXyz fonctionne correctement en multi-voix: toutes les voix sont mises à jour dans tous les cas
/* - 1.3.0.beta2: Tie on previous rest too
/* - 1.3.0.beta2: Better count available the remaining duration when a tuplet is involved
/* - 1.3.0.beta2: Correction for the 5:4 case
/* - 1.3.0.beta2: Forbid setDuration and setDot within tuplets
/* - 1.3.0.beta2: log to file
/* - 1.3.0.beta2: Single Voice edition
/* - 1.3.0.beta2: Tuplet multivoice edition
/* - 1.3.0.beta2: Single voice copy/paste (incl. tuplets) copies all the annotations (incl. harmonies)
/* - 1.3.0.beta3: Correction in x/8 cases
/* - 1.3.0.beta3: consider segments with rests but annotations as non empty
/* - 1.3.0.beta3: new selectionhelper.js version
/* - 1.3.0.beta3: Qt.quit issue
/**********************************************/
MuseScore {
menuPath: "Plugins." + pluginName
description: "Edit the notes and rests length by moving the next notes in the measure, instead of eating them."
version: "1.3.0"
readonly property var pluginName: "Duration Editor"
readonly property var selHelperVersion: "1.3.0"
readonly property var noteHelperVersion: "1.0.5"
readonly property var debug: false
pluginType: "dock"
dockArea: "left"
requiresScore: false
width: 450
height: 200
id: mainWindow
readonly property var imgHeight: 32
readonly property var imgPadding: 8
ColumnLayout {
// anchors.fill: parent
width: parent.width
y: parent.y
Flow {
id: layButtons
Layout.fillWidth: true
// anchors.fill: parent
anchors.margins: 4
spacing: 10
ImageButton {
imageSource: "ronde.svg"
imageHeight: imgHeight
imagePadding: imgPadding
ToolTip.text: "Change to Whole/Semibreve\nSHIFT: insert a rest"
MouseArea {
anchors.fill: parent
onPressed: {
if ((mouse.button == Qt.LeftButton) && (mouse.modifiers & Qt.ShiftModifier)) {
insertRest(64);
} else {
setDuration(64);
}
}
}
}
ImageButton {
imageSource: "blanche.svg"
imageHeight: imgHeight
imagePadding: imgPadding
ToolTip.text: "Change to Half/Minim\nSHIFT: insert a rest"
MouseArea {
anchors.fill: parent
onPressed: {
if ((mouse.button == Qt.LeftButton) && (mouse.modifiers & Qt.ShiftModifier)) {
insertRest(32);
} else {
setDuration(32);
}
}
}
}
ImageButton {
imageSource: "noire.svg"
imageHeight: imgHeight
imagePadding: imgPadding
ToolTip.text: "Change to Quarter/Crotchet\nSHIFT: insert a rest"
MouseArea {
anchors.fill: parent
onPressed: {
if ((mouse.button == Qt.LeftButton) && (mouse.modifiers & Qt.ShiftModifier)) {
insertRest(16);
} else {
setDuration(16);
}
}
}
}
ImageButton {
imageSource: "croche.svg"
imageHeight: imgHeight
imagePadding: imgPadding
ToolTip.text: "Change to Eighth/Quaver\nSHIFT: insert a rest"
MouseArea {
anchors.fill: parent
onPressed: {
if ((mouse.button == Qt.LeftButton) && (mouse.modifiers & Qt.ShiftModifier)) {
insertRest(8);
} else {
setDuration(8);
}
}
}
}
ImageButton {
imageSource: "double.svg"
imageHeight: imgHeight
imagePadding: imgPadding
ToolTip.text: "Change to Sixteenth/Semiquaver\nSHIFT: insert a rest"
MouseArea {
anchors.fill: parent
onPressed: {
if ((mouse.button == Qt.LeftButton) && (mouse.modifiers & Qt.ShiftModifier)) {
insertRest(4);
} else {
setDuration(4);
}
}
}
}
ImageButton {
imageSource: "triple.svg"
imageHeight: imgHeight
imagePadding: imgPadding
ToolTip.text: "Change to Thirty-second/Demisemiquaver\nSHIFT: insert a rest"
MouseArea {
anchors.fill: parent
onPressed: {
if ((mouse.button == Qt.LeftButton) && (mouse.modifiers & Qt.ShiftModifier)) {
insertRest(2);
} else {
setDuration(2);
}
}
}
}
ImageButton {
imageSource: "quadruple.svg"
imageHeight: imgHeight
imagePadding: imgPadding
ToolTip.text: "Change to Sixty-fourth/Hemidemisemiquaver\nSHIFT: insert a rest"
MouseArea {
anchors.fill: parent
onPressed: {
if ((mouse.button == Qt.LeftButton) && (mouse.modifiers & Qt.ShiftModifier)) {
insertRest(1);
} else {
setDuration(1);
}
}
}
}
ImageButton {
imageSource: "dot.svg"
imageHeight: imgHeight
imagePadding: imgPadding
ToolTip.text: "Dot"
onClicked: setDot(1.5)
}
ImageButton {
imageSource: "dot2.svg"
imageHeight: imgHeight
imagePadding: imgPadding
ToolTip.text: "Dubble dot"
onClicked: setDot(1.75)
}
ImageButton {
imageSource: "dot3.svg"
imageHeight: imgHeight
imagePadding: imgPadding
ToolTip.text: "Triple dot"
onClicked: setDot(1.875)
}
ImageButton {
imageSource: "dot4.svg"
imageHeight: imgHeight
imagePadding: imgPadding
ToolTip.text: "Quadruple dot"
onClicked: setDot(1.9375)
}
ImageButton {
imageSource: "remove.svg"
imageHeight: imgHeight
imagePadding: imgPadding
ToolTip.text: "Delete"
onClicked: setDuration(0)
}
ImageButton {
id: btnTie
// imageSource: "qrc:///data/icons/note-tie.svg"
imageSource: "tie.svg"
imageHeight: imgHeight
imagePadding: imgPadding
// fillMode: Image.PreserveAspectCrop
ToolTip.text: "Tie rest to preceding note/Tie note to following rest."
onClicked: addTie()
}
Item {
// height: btnTuplet.height
height: imgHeight
// width: btnTuplet.width+btnManualTuplet.width
width: 50
ImageButton {
id: btnTuplet
imageSource: "tuplets.svg"
imageHeight: imgHeight
imagePadding: imgPadding
// fillMode: Image.PreserveAspectCrop
ToolTip.text: "Convert consecutive notes/rests to and from tuplets."
onClicked: addRemoveTupplet(false)
}
ImageButton {
id: btnManualTuplet
x: btnTuplet.x + btnTuplet.width
y: btnTuplet.y
imageSource: "arrowbutton.svg"
imageHeight: 16
implicitHeight: btnTuplet.height
imagePadding: 2
//fillMode: Image.PreserveAspectCrop
ToolTip.text: "Manual conversion to tuplets."
onClicked: addRemoveTupplet(true)
}
}
} // flow
/* SmallCheckBox {
id: chkCrossVoice
boxWidth: 20
text: "Cross voices edition"
ToolTip.text: "Treat all the voices at oncee."
checked: true
enabled: false;
}
*/
RowLayout {
Item {
Layout.fillWidth: true
Layout.preferredHeight: 1
}
Label {
text: "v: " + version
font.italic: true
font.pointSize: 8
}
}
} // ColumnLayout
onRun: {
// Versionning
if ((typeof(SelHelper.checktVersion) !== 'function') || !SelHelper.checktVersion(selHelperVersion) ||
(typeof(NoteHelper.checktVersion) !== 'function') || !NoteHelper.checktVersion(noteHelperVersion)) {
console.log("Invalid zparkingb/selectionhelper.js or zparkingb/notehelper.js versions. Expecting "
+ selHelperVersion + " and " + noteHelperVersion + ".");
invalidLibraryDialog.open();
return;
}
// auto-run test
if (debug) {
addRemoveTupplet();
}
}
function setDuration(newDuration) {
// log to file
if (debug) {
console.log("Logfile: " + logfile.source);
openLog(logfile.source);
}
var res = getSelection();
var chords = res.chords;
if (!chords || (chords.length == 0))
return;
if (verifySelection(chords)<0)
return;
setElementDuration(chords[0], newDuration, res.multivoice);
if (debug) {
closeLog();
}
}
function insertRest(newDuration) {
// log to file
if (debug) {
console.log("Logfile: " + logfile.source);
openLog(logfile.source);
}
var res = getSelection();
var chords = res.chords;
if (!chords || (chords.length == 0))
return;
if (verifySelection(chords,["staff","tick"])<0)
return;
setElementDuration(chords[0], newDuration * (-1), res.multivoice);
if (debug) {
closeLog();
}
}
function setDot(newDuration) {
// log to file
if (debug) {
console.log("Logfile: " + logfile.source);
openLog(logfile.source);
}
var res = getSelection();
var chords = res.chords;
if (!chords || (chords.length == 0))
return;
if (verifySelection(chords)<0)
return;
setElementDot(chords[0], newDuration, res.multivoice);
if (debug) {
closeLog();
}
}
function setElementDot(element, dot, multivoice) {
if (element.tuplet !== null) {
warningDialog.text = "Cannot perform this action for elements within tuplets";
warningDialog.open();
return;
}
var current = durationTo64(element.duration);
var analyze = analyzeDuration(current);
logThis(current + " => " + analyze.base + "/" + analyze.ratio);
var newDuration;
if (dot == analyze.ratio) {
// Same "dot", removing it
newDuration = analyze.base;
} else {
newDuration = analyze.base * dot;
}
setElementDuration(element, newDuration, multivoice);
}
/**
* newDuration>0: change the element to that duration
* newDuration==0: delete the element
* newDuration<0: insert a rest before the element with that duration
*/
function setElementDuration(element, newDuration, multivoice) {
if (element.tuplet !== null && newDuration>=0) {
warningDialog.text = "Cannot perform this action for elements within tuplets";
warningDialog.open();
return;
}
var insertmode = (newDuration < 0);
newDuration = Math.abs(newDuration);
var score = curScore;
var cur_time = element.parent.tick;
var cursor = score.newCursor();
cursor.track = element.track;
cursor.rewindToTick(cur_time);
var current = durationTo64(element.duration);
var increment = (insertmode) ? newDuration : newDuration - current;
logThis("setElementDuration: from " + current + " to " + newDuration + " in "+((typeof multivoice==="undefined")?"undefined":(multivoice?"multi":"single"))+" voice context");
if (increment > 0) {
// 0) On compte ce qu'on a comme buffer en fin de mesure
var buffer = computeRemainingRest(cursor.measure, cursor.track, cur_time);
logThis("Required increment is : " + increment + ", current buffer is : " + buffer);
// 1) on coupe ce qu'on va déplacer
var localCopy;
var doCutPaste = selectRemaingInMeasure(cursor, insertmode, multivoice);
switch (doCutPaste) {
case 1:
if (multivoice) {
logThis("CMD: cmd(\"cut\")");
cmd("cut");
} else {
logThis("SingleVoice cut");
localCopy = copySelection(score.selection.elements);
// logThis("CMD: cmd(\"delete\")");
// cmd("delete");
logThis("CMD: cmd(\"pad-rest\")");
cmd("pad-rest");
}
break;
case -1:
logThis("!! cannot perform a duration modification of a single voice context with tuplets after.");
warningDialog.subtitle = "Duration editor";
warningDialog.text = "The limit of the plugin have been reached.\nIt cannot perform a duration modification of a single voice context with tuplets after.";
warningDialog.open();
return;
}
// 2) on adapte la longueur de la mesure (si on n'a pas assez de buffer)
if (buffer < increment) {
var delta = increment - buffer;
logThis("buffer is < increment => adding a rest of " + delta);
appendRest(cursor, delta, multivoice);
} else {
logThis("buffer is >= increment => no need to add a rest");
}
// 3) on adapte la durée de la note
cursor.rewindToTick(cur_time);
debugCursor(cursor, "before cursorToDuration");
cursorToDuration(cursor, newDuration);
debugCursor(cursor, "after cursorToDuration");
var lastRestTick = cursor.tick;
// 4) s'occuper des autres voix (uniquement quand la durée augmente)
// 1.3.0
if(multivoice) {
var t_cursor = cursor.score.newCursor();
t_cursor.rewindToTick(cur_time);
var seg = t_cursor.segment;
logThis("About to adapt others for tracks " + (cursor.staffIdx * 4) + " to " + ((cursor.staffIdx + 1) * 4));
for (var t = cursor.staffIdx * 4; t < (cursor.staffIdx + 1) * 4; t++) {
logThis("adapting others at track " + t);
if (t === element.track)
continue; // on a déjà traité celui-là
var t_el = seg.elementAt(t);
if (t_el === null)
continue; // rien sur la voie t à ce tick
var t_dur = durationTo64(t_el.duration);
var t_newdur = t_dur + increment;
logThis("adapting others from " + t_dur + " to " + t_newdur);
t_cursor.track = t;
cursorToDuration(t_cursor, t_newdur);
}
}
// 5) On fait le paste
if (doCutPaste===1) {
// cursor.rewindToTick(cur_time);
cursor.rewindToTick(lastRestTick); //1.3.0
debugCursor(cursor, "rewind for paste");
cursor.next();
debugCursor(cursor, "paste position");
selectCursor(cursor);
if (multivoice) {
logThis("CMD: cmd(\"paste\")");
cmd("paste");
} else {
logThis("SingleVoice paste");
pasteSelection(cursor, localCopy);
}
}
//endCmd(score);
}
if (increment < 0) {
// 1) on coupe ce qu'on va déplacer
var localCopy;
var doCutPaste = selectRemaingInMeasure(cursor, false, multivoice);
switch (doCutPaste) {
case 1:
if (multivoice) {
logThis("CMD: cmd(\"cut\")");
cmd("cut");
} else {
logThis("SingleVoice cut");
localCopy = copySelection(score.selection.elements);
logThis("CMD: cmd(\"delete\")");
cmd("delete");
}
break;
case -1:
warningDialog.subtitle = "Duration editor";
warningDialog.text = "The limit of the plugin have been reached.\nIt cannot perform a duration modification of a single voice context with tuplets after.";
warningDialog.open();
return;
}
// 2) on adapte la durée de la note
cursor.rewindToTick(cur_time);
if (newDuration != 0)
cursorToDuration(cursor, newDuration);
else {
// duration 0, so replace the element by a rest
startCmd(score);
logThis("CMD: replace element by rest (removeElement)");
removeElement(element);
endCmd(score);
}
//cursor.element.duration=fraction(1, 2) // KO
// 3) On fait le paste
if (doCutPaste===1) {
cursor.rewindToTick(cur_time);
if (newDuration != 0)
cursor.next();
selectCursor(cursor);
if (multivoice) {
logThis("CMD: cmd(\"paste\")");
cmd("paste");
} else {
logThis("SingleVoice paste");
pasteSelection(cursor, localCopy);
}
}
// 4) On réduit la durée de la mesure si nécessaire
var measure = cursor.measure;
debugMeasureLength(measure);
removeRest(cursor, increment);
debugMeasureLength(measure);
}
cursor.rewindToTick(cur_time);
selectCursor(cursor);
}
function addTie() {
// log to file
if (debug) {
logThis("Logfile: " + logfile.source);
openLog(logfile.source);
}
var res = getSelection();
var rests = res.chords;
if (rests.length == 0) {
logThis("NO SELECTION. QUIT HERE.");
return;
}
var rest = rests[0];
var source = null;
var cur_time = rest.parent.tick; // getting rest's segment's tick
var cursor = curScore.newCursor();
cursor.track = rest.track;
cursor.rewindToTick(cur_time);
// current is rest
if (rest.type === Element.REST) {
logThis("REST ==> looking behind for a CHORD");
logThis("REST ==> looking backward for a CHORD");
cursor.rewindToTick(cur_time);
if (movePrev(cursor)) {
logThis("got a backward element");
var candidate = cursor.element;
if (candidate !== null) {
logThis("which ain't null");
if (candidate.type === Element.CHORD) {
logThis("and is a chord ==> ok");
source = candidate;
} else {
logThis("which ain't a chord (" + candidate.userName() + ")");
}
} else {
logThis("which is null");
}
} else {
logThis("no backward element found");
}
if (source == null) {
logThis("REST ==> looking forward for a CHORD");
cursor.rewindToTick(cur_time);
if (moveNext(cursor)) {
logThis("got a next element");
var candidate = cursor.element;
if (candidate !== null) {
logThis("which ain't null");
if (candidate.type === Element.CHORD) {
logThis("and is a chord ==> ok");
source = candidate;
} else {
logThis("which ain't a chord (" + candidate.userName() + ")");
}
} else {
logThis("which is null");
}
} else {
logThis("no next element found");
}
}
if (source === null) {
warningDialog.text = "Failed to tie the rest.\nThe selected note must be preceeded or followed by a chord.";
warningDialog.open();
return;
}
} // current is rest
// current is chord
else {
source = rest;
rest = null;
logThis("CHORD ==> looking backward for a REST");
cursor.rewindToTick(cur_time);
if (movePrev(cursor)) {
logThis("got a backward element");
var candidate = cursor.element;
if (candidate !== null) {
logThis("which ain't null");
if (candidate.type === Element.REST) {
logThis("and is a rest ==> ok");
rest = candidate;
} else {
logThis("which ain't a rest (" + candidate.userName() + ")");
}
} else {
logThis("which is null");
}
} else {
logThis("no backward element found");
}
if (rest == null) {
logThis("CHORD ==> looking forward for a REST");
cursor.rewindToTick(cur_time);
if (moveNext(cursor)) {
logThis("got a next element");
var candidate = cursor.element;
if (candidate !== null) {
logThis("which ain't null");
if (candidate.type === Element.REST) {
logThis("and is a rest ==> ok");
rest = candidate;
} else {
logThis("which ain't a rest (" + candidate.userName() + ")");
}
} else {
logThis("which is null");
}
} else {
logThis("no next element found");
}
}
if (rest === null) {
warningDialog.text = "Failed to tie the note.\nThe selected note must be preceeded or followed by a rest.";
warningDialog.open();
return;
}
} // current is chord
// A source is found.
// var note = source.notes[0];
logThis("Got a source at " + source.parent.tick + " and a dest at " + rest.parent.tick);
var notes = [];
for (var i = 0; i < source.notes.length; i++) {
// notes.push(source.notes[i].pitch);
var n = {
"pitch": source.notes[i].pitch,
"tpc1": source.notes[i].tpc1,
"tpc2": source.notes[i].tpc2
};
notes.push(n);
}
// Transforming the rest into the notes
startCmd(curScore);
NoteHelper.restToChord(rest, notes, true); // with keepRestDuration=true
if (source.parent.tick < rest.parent.tick) {
cursor.rewindToTick(source.parent.tick);
} else {
cursor.rewindToTick(rest.parent.tick);
}
selectCursor(cursor);
cmd("chord-tie");
cursor.rewindToTick(rest.parent.tick);
// cursor.rewindToTick(cur_time);
selectCursor(cursor);
endCmd(curScore);
if (debug) {
closeLog();
}
}
function addRemoveTupplet(ask) {
// log to file
if (debug) {
logThis("Logfile: " + logfile.source);
openLog(logfile.source);
}
var selection = getTupletsFromSelection();
if (selection && (selection.tracks.length > 0)) {
// Tuplets selected - Converting to regular chords
logThis("TUPLETS FOUND FROM SELECTION");
if (ask) {
warningDialog.subtitle = "Tuplet conversion";
warningDialog.text = "Cannot perform a manual tuplet conversion for this selection.\nThe manual conversion only applies to non-tuplet consecutive notes/rests.";
warningDialog.open();
return;
}
convertChordsFromTuplets(selection);
} else if (selection && (selection.tracks.length === 0)) {
logThis("No TUPLETS FOUND FROM SELECTION");
selection = getTupletsCandidates();
// console.log(tupletChords);
if (selection && (selection.tracks.length > 0)) {
// Regular chords selected - Converting to tuplets chords
logThis("THE SELECTION CAN BE CONVERTED to TUPLETS");
convertChordsToTuplets(selection, ask);
} else {
warningDialog.subtitle = "Tuplet conversion";
warningDialog.text = "Invalid selection.\nExpecting a selection of consecutive notes/rests.";
warningDialog.open();
// return;
}
} else {
warningDialog.subtitle = "Tuplet conversion";
warningDialog.text = "Invalid selection.\nExpecting a selection of consecutive notes/rests or of a tuplet.";
warningDialog.open();
// return;
}
if (debug) {
closeLog();
}
}
/**
* Determines how to convert the selection to tuplets.
* Computes the target duration, target tuplet ratio, .... Possibly ask for it it cannot be automatically determined.
* @see getTupletsCandidates for the `selection` object structure.
*/
function convertChordsToTuplets(selection, ask) {
// Transforming regular notes into tuplets
var track1=selection.tracks[0];
var measure = track1.chords[0].parent.parent;
var duration=track1.duration;
// var measureType = measure.timesigActual.denominator;
var measureType = measure.timesigNominal.denominator;
logThis("Selection: " + track1.chords.length + "element, total duration: " + track1.duration);
logThis("Measure: x/" + measureType + ", total duration: " + sigTo64(measure.timesigActual));
var tupletN = null;
var tupletD = null;
var nums = [3, 5, 7, 9, 4, 6, 8];
// En x/8 si j'ai 2 notes sélectionnées, de même durée, j'autorise aussi à faire des 2:3
if (measureType === 8) {
// on garde les tracks qui ont 2 accords et de même durée
var tsamedur=selection.tracks.filter(function(e) {
return e.samedur && e.chords.length==2;
});
// si au 1
if (tsamedur.length>0)
// if ((chords.length == 2) && (measureType == 8) && (samedur > 0))
nums.unshift(2);
}
for (var i = 0; i < nums.length; i++) {
var n = nums[i];
if (isTupletCandidate(duration, n)) {
tupletN = n;
break;
}
}
if (tupletN != null) {
if (measureType == 4) {
// 2/4, 3/4, 4/4, ...
tupletD = Math.pow(2, Math.sqrt(tupletN) | 0); // (trunc) 3/2, 5/4, 6/4, .. 9/8, ...
} else if (measureType == 8) {
// 3/8, 6/8, 9/8, ...
tupletD = 3 * (Math.max((tupletN / 3) | 0, 1));
}
}
logThis("Heading to " + ((tupletN != null) ? (tupletN + ":" + tupletD + " of " + (duration / tupletN)) : "???"));
if (ask || (tupletN == null) || (tupletD == null) || (tupletN == tupletD)) {
manualTupletDefinitionDialog.selection = selection;
manualTupletDefinitionDialog.duration = duration;
manualTupletDefinitionDialog.measureType = measureType;
manualTupletDefinitionDialog.tupletN = (tupletN != null) ? tupletN : "";
manualTupletDefinitionDialog.tupletD = (tupletD != null) ? tupletD : "";
manualTupletDefinitionDialog.open();
return;
} else {
convertChordsToTuplets_Exectute(selection, duration, measureType, tupletN, tupletD);
}
}
/**
* Executing the conversion of the selection to tuplets.
* Everything must be known (target duration, target tuplet ratio, ...).
* @see getTupletsCandidates for the `selection` object structure.
*/
function convertChordsToTuplets_Exectute(selection, duration, measureType, tupletN, tupletD) {
if (tupletN == null || tupletD == null) {
warningDialog.subtitle = "Tuplet conversion";
warningDialog.text = "Wrong tuplet definition: --" + ((tupletN) ? tupletN : "/") + ":" + ((tupletD) ? tupletD : "/") + "--";
warningDialog.open();
return;
}
var targets=[];
// Storing track by track what will be pasted later
// Rational: Dealing with the track 0's duration may alter what's on track 1 and therefor the copy we would take if we did by the time we handle the track 1.
// So we copy everything *before* modifying anything.
for (var t = 0; t < selection.tracks.length; t++) {
var track = selection.tracks[t];
logThis("** Copying tuplet "+t+" (track "+track.track+")");
targets[t] = copySelection(track.chords);
}
// Working track by track
for (var t = 0; t < selection.tracks.length; t++) {
var track = selection.tracks[t];
logThis("** Dealing with tuplet "+t+" (track "+track.track+")");
// Inserting the triplet
// 1) deleting/reducing the last elements duration until we get the right total dur
var targetdur = duration * tupletD / tupletN;
if (t === 0 || !selection.multivoice) {
// must only be done one time if in multivoice context
logThis("Adapting the duration of the selection from " + duration + " to " + targetdur);
var delta = duration - targetdur;
// startCmd(curScore, "adapt last note duration");
for (var i = track.chords.length - 1; i >= 0; i--) {
var last = track.chords[i];
var newDuration = durationTo64(last.duration) - delta;
if (newDuration >= 0) {
setElementDuration(last, newDuration, selection.multivoice);
break;
} else {
// Il faut supprimer plus que le dernier
setElementDuration(last, 0, selection.multivoice);
delta = newDuration * (-1);
}
}
}
// endCmd(curScore, "adapt last note duration"); // DEBUG
// 2) adding a tuplet
var cur_time = track.first_tick;
var cursor = curScore.newCursor();
cursor.track = track.track;
cursor.rewindToTick(cur_time);
startCmd(curScore, "addTuplet");
var actual = fraction(targetdur, 64);
var ratio = fraction(tupletN, tupletD);
var tuplet = cursor.addTuplet(ratio, actual);
// tuplet.bracketType=TupletBracketType.SHOW_BRACKET; // les crochets
// tuplet.bracketType=1; // les crochets
//tuplet.numberType=TupletNumberType.SHOW_RELATION; //le ratio
endCmd(curScore, "addTuplet");
// 3) re-adding the notes
pasteSelection(cursor, targets[t]);
logThis("** Done with tuplet "+t+" (track "+track.track+")");
}
// if (!debug)endCmd(curScore);
}
function isTupletCandidate(duration, check) {
var res = duration / check;
return ((res | 0) == res); // trunc
}
/**
* Executing the conversion of the selected tuplets to regular chords.
* @see getTupletsFromSelection for the `selection` object structure.
*/
function convertChordsFromTuplets(selection) {
// Transforming a tuplet into regular notes
var targets=[];
// Storing track by track what will be pasted later
// Rational: Dealing with the track 0's duration may alter what's on track 1 and therefor the copy we would take if we did by the time we handle the track 1.
// So we copy everything *before* modifying anything.
for (var t = 0; t < selection.tracks.length; t++) {
var track = selection.tracks[t];
logThis("** Copying tuplet "+t+" (track "+track.track+")");
targets[t] = copySelection(track.chords);
}
// computing the new duration
var tuplet = selection.tracks[0].chords[0].tuplet;
var tuplet_ratio = tuplet.actualNotes / tuplet.normalNotes;
var duration = selection.tracks[0].duration;
var origduration = duration / tuplet_ratio;
// for (var i = 0; i < chords.length; i++) {
// logThis("Tuplets chords: " + chords[i].userName() + " with duration " + durationTo64(chords[i].duration));
// }