forked from BernardGreenberg/MuseScorePlugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
articulate.qml
377 lines (340 loc) · 11 KB
/
articulate.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
//=============================================================================
// MuseScore
// Music Composition & Notation
//
// Copyright (C) 2012 Werner Schweer
// Copyright (C) 2013-2017 Nicolas Froment, Joachim Schmitz
// Copyright (C) 2019 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
//=============================================================================
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.1
/* This plugin replaces the Piano Roll Editor as the most convenient means of
adjusting the on-times and off-times (not "len"s) of notes on a one-by-one
basis. This allows you to phrase without the yellow bar jungle (which is
always there for hard cases). This even works on appoggiature, but does
not work on notes with real "ornaments" (not well-defined how to edit).
Extensions to impose duple- and triple- phrasing patterns are under
consideration.
If a nonempty region-selection exists when this invoked, all the notes in it
will get the off-time you set; grace notes/appogg not affected.
If there is no region selection but a single note has been clicked on,
the pitch and on an off times are displayed and you can change the last two.
Return and ESC are accepted as Apply and Cancel.
*/
MuseScore {
version: "3.2"
description: "This plugin adjusts the on/off times of a note."
menuPath: "Plugins.Articulation"
pluginType: "dialog"
requiresScore: true
property int margin: 10
property var range_mode : false
property var the_note : null
width: 240
height: 160
onRun: {
if ((mscoreMajorVersion < 3) || (mscoreMinorVersion < 3)) {
versionError.open()
Qt.quit();
return;
}
console.log("hello adjust articulation: onRun");
curScore.createPlayEvents();
var note_count = 0;
applyToNotesInSelection(function(note, cursor) { note_count += 1;});
console.log("sel note count", note_count);
if (note_count > 0) {
range_mode = true;
} else {
the_note = find_usable_note();
if (!the_note) {
console.log("onRun didn't find usable notes")
complaintDialog.open()
Qt.quit();
}
range_mode = false;
}
if (range_mode) {
noteField.text = note_count + " Notes"
onTime.visible = false;
onTimeLabel.visible = false;
pitchLabel.text = "Selection"
offTime.text = "";
showButton.visible = true;
} else {
if (the_note) {
var events = the_note.playEvents;
var pe0 = events[0];
onTime.text = pe0.ontime + "";
offTime.text = (pe0.ontime + pe0.len) + "";
var tpc = get_tpc_name(the_note.tpc1)
var octave = get_octave(tpc, the_note.pitch)
noteField.text = tpc + octave
showButton.visible = false;
}
}
}
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", "", "#", "##"];
result = result + appenda[divergence]
return result
}
function get_octave(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...
console.log(elements.length, "selections")
// Loop a bit silly at this point.
for (var idx = 0; idx < elements.length; idx++) {
var element = elements[idx]
console.log("element.type=" + element.type)
if (element.type == Element.NOTE) {
var note = element;
var events = note.playEvents;
if (events.length == 1) {
var mpe0 = events[0];
dump_play_ev(mpe0);
return note;
}
}
}
}
return false;
}
function dump_play_ev(event) {
console.log("on time", event.ontime, "len", event.len, "off time", event.ontime+event.len);
}
function applyChanges() {
var off_time = parseInt(offTime.text)
if (isNaN(off_time)) {
return false;
}
if (range_mode) {
curScore.startCmd();
applyToNotesInSelection(function(note, cursor) {
var mpe0 = note.playEvents[0];
mpe0.len = off_time - mpe0.ontime;
});
curScore.endCmd();
return true;
}
var note = find_usable_note();
if (!note) {
console.log("No note at Apply time.")
return false;
}
var on_time = parseInt(onTime.text)
if (isNaN(on_time)) {
return false;
}
curScore.startCmd();
var mpe0 = note.playEvents[0];
mpe0.ontime = on_time;
mpe0.len = off_time - on_time;
curScore.endCmd();
dump_play_ev(mpe0);
console.log("Did it!", on_time, off_time);
return true;
}
function applyToNotesInSelection(func) {
var cursor = curScore.newCursor();
cursor.rewind(1);
var endStaff;
if (!cursor.segment) { // no selection
console.log("No region selection.")
return false;
}
cursor.rewind(1);
var startStaff = cursor.staffIdx;
cursor.rewind(2)
var endStaff = cursor.staffIdx;
var endTick;
if (cursor.tick === 0) {
// this happens when the selection includes
// the last measure of the score.
// rewind(2) goes behind the last segment (where
// there's none) and sets tick=0
endTick = curScore.lastSegment.tick + 1;
} else {
endTick = cursor.tick;
}
console.log("sel area startStaff " + startStaff + " endStaff " + endStaff + " endTick " + endTick);
for (var staff = startStaff; staff <= endStaff; staff++) {
for (var voice = 0; voice < 4; voice++) {
cursor.rewind(1); // sets voice to 0
cursor.voice = voice; //voice has to be set after goTo
cursor.staffIdx = staff;
while (cursor.segment && (cursor.tick < endTick)) {
if (cursor.element && cursor.element.type === Element.CHORD) {
// gratia vacua....
var notes = cursor.element.notes;
for (var k = 0; k < notes.length; k++) {
var note = notes[k];
func(note, cursor);
}
}
cursor.next();
}
}
}
return true;
}
function showTimeInScore(note, cursor) {
var npe0 = note.playEvents[0];
var on_time = npe0.ontime;
var off_time = on_time + npe0.len;
if (on_time == 0 && off_time == 1000) {
return;
}
var timeText = off_time; //attempt to use nbsp crashed app at readback
if (on_time != 0) {
timeText += "\n@" + on_time;
}
var staffText = newElement(Element.STAFF_TEXT);
staffText.text = timeText;
staffText.placement = Placement.BELOW
staffText.fontSize = 7;
cursor.add(staffText);
}
function showTimesInScore (){
applyButton.visible = false;
showButton.visible = false;
undoButton.visible = true;
offTimeLabel.visible = false;
offTime.visible = false;
cancelButton.text = "Leave them";
curScore.startCmd();
applyToNotesInSelection(showTimeInScore);
curScore.endCmd();
}
function undoTimes() {
cmd("undo")
Qt.quit()
}
function maybe_finish() {
if (applyChanges()) {
Qt.quit()
}
}
GridLayout {
id: 'mainLayout'
anchors.fill: parent
anchors.margins: 10
columns: 2
Label {
id: pitchLabel
text: "Pitch"
}
Label{
id: noteField
text: ""
}
Label {
id: onTimeLabel
text: "On Time ‰"
}
TextField {
id: onTime
implicitHeight: 24
placeholderText: "0"
Keys.onReturnPressed : {
maybe_finish()
}
Keys.onEscapePressed : {
Qt.quit()
}
}
Label {
id: offTimeLabel
text: "Off Time ‰"
}
TextField {
id: offTime
implicitHeight: 24
placeholderText: "1000"
focus: true
Keys.onReturnPressed : {
maybe_finish()
}
Keys.onEscapePressed : {
Qt.quit()
}
}
Button {
id: applyButton
Layout.columnSpan:1
text: qsTranslate("PrefsDialogBase", "Apply")
onClicked: {
maybe_finish()
}
}
Button {
id: cancelButton
Layout.columnSpan: 1
text: qsTranslate("InsertMeasuresDialogBase", "Cancel")
onClicked: {
Qt.quit();
}
}
Button {
id: showButton
text: "Show in score"
enabled: true
onClicked: {
showTimesInScore();
}
}
Button {
id: undoButton
visible: false
text: "Undo show"
onClicked: {
undoTimes();
}
}
}
MessageDialog {
id: complaintDialog
icon: StandardIcon.Warning
standardButtons: StandardButton.Ok
modality: Qt.ApplicationModal
title: "Invalid or missing note selection"
text: "No unornamented note is selected."
detailedText: "Either you have not selected a note, or " +
"the note, or one of the notes you have selected, have " +
"multi-sub-note ornamentation."
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()
}
}
}