-
Notifications
You must be signed in to change notification settings - Fork 1
/
addCourtesyAccidentals.qml
315 lines (269 loc) · 12.2 KB
/
addCourtesyAccidentals.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
//==============================================
// add courtesy accidentals v1.0
//
// Copyright (C)2012-2019 Jörn Eichler (heuchi)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//==============================================
import QtQuick 2.0
import QtQuick.Dialogs 1.2
import MuseScore 3.0
MuseScore {
version: "1.0"
description: "This plugin adds courtesy accidentals"
menuPath: "Plugins.Accidentals.Add Courtesy Accidentals"
requiresScore: true
// configuration
// This has changed for MuseScore v3
// 0 = no bracket, 1 = parenthesis, 2 = bracket
property int useBracket: 0
// if nothing is selected process whole score
property bool processAll: false
MessageDialog {
id: versionError
visible: false
title: "Unsupported MuseScore Version"
text: "This plugin needs MuseScore v3.0.5 or higher"
onAccepted: { Qt.quit() }
}
// function tpcName
//
// return name of note
function tpcName(tpc) {
var tpcNames = new Array(
"Fbb", "Cbb", "Gbb", "Dbb", "Abb", "Ebb", "Bbb",
"Fb", "Cb", "Gb", "Db", "Ab", "Eb", "Bb",
"F", "C", "G", "D", "A", "E", "B",
"F#", "C#", "G#", "D#", "A#", "E#", "B#",
"F##", "C##", "G##", "D##", "A##", "E##", "B##"
);
return(tpcNames[tpc+1]);
}
// function getEndStaffOfPart
//
// return the first staff that does not belong to
// the part containing given start staff.
function getEndStaffOfPart(startStaff) {
var startTrack = startStaff * 4;
var parts = curScore.parts;
for(var i = 0; i < parts.length; i++) {
var part = parts[i];
if( (part.startTrack <= startTrack)
&& (part.endTrack > startTrack) ) {
return(part.endTrack/4);
}
}
// not found!
console.log("error: part for " + startStaff + " not found!");
Qt.quit();
}
// function processNote
//
// for each measure we create a table that contains
// the actual 'noteName' of each 'noteClass'
//
// a 'noteClass' is the natural name of a space
// or line of the staff and the octave:
// C5, F6, B3 are 'noteClass'
//
// a 'noteName' would be C, F#, Bb for example
// (we don't need the octave here)
//
// curMeasureArray[<noteClass>] = <noteName>
function processNote(note,prevMeasureArray,curMeasureArray) {
var octave=Math.floor(note.pitch/12);
// use tpc1 instead of tpc for octave correction
// since this will also work for transposing instruments
// correct octave for Cb and Cbb
if(note.tpc1 == 7 || note.tpc1 == 0) {
octave++; // belongs to higher octave
}
// correct octave for B# and B##
if(note.tpc1 == 26 || note.tpc1 == 33) {
octave--; // belongs to lower octave
}
var noteName = tpcName(note.tpc);
var noteClass = noteName.charAt(0)+octave;
// remember note for next measure
curMeasureArray[noteClass]=noteName;
// check if current note needs courtesy acc
if(typeof prevMeasureArray[noteClass] !== 'undefined') {
if(prevMeasureArray[noteClass] != noteName) {
// this note needs an accidental
// if there's none present anyway
if(note.accidental == null) {
// calculate type of needed accidental
var accidental=Accidental.NONE;
if(note.tpc < 6) {
accidental = Accidental.FLAT2;
} else if(note.tpc < 13) {
accidental = Accidental.FLAT;
} else if(note.tpc < 20) {
accidental = Accidental.NATURAL;
} else if(note.tpc < 27) {
accidental = Accidental.SHARP;
} else {
accidental = Accidental.SHARP2;
}
note.accidentalType = accidental;
// put bracket on accidental
note.accidental.accidentalBracket = useBracket;
}
}
// delete entry to make sure we don't create the
// same accidental again in the same measure
delete prevMeasureArray[noteClass];
}
}
// function processPart
//
// do the actual work: process all given tracks in parallel
// add courtesy accidentals where needed.
//
// We go through all tracks simultaneously, because we also want courtesy
// accidentals for notes across different staves when they are in the
// same octave and for notes of different voices in the same octave
function processPart(cursor,endTick,startTrack,endTrack) {
if(processAll) {
// we need to reset track first, otherwise
// rewind(0) doesn't work correctly
cursor.track=0;
cursor.rewind(0);
} else {
cursor.rewind(1);
}
var segment = cursor.segment;
// we use the cursor to know measure boundaries
cursor.nextMeasure();
var curMeasureArray = new Array();
var prevMeasureArray = new Array();
// we use a segment, because the cursor always proceeds to
// the next element in the given track and we don't know
// in which track the element is.
var inLastMeasure=false;
while(segment && (processAll || segment.tick < endTick)) {
// check if still inside same measure
if(!inLastMeasure && !(segment.tick < cursor.tick)) {
// new measure
prevMeasureArray = curMeasureArray;
curMeasureArray = new Array();
if(!cursor.nextMeasure()) {
inLastMeasure=true;
}
}
// we search for key signatures in first voice of
// first staff:
var keySigTrack = startTrack - (startTrack % 4);
for(var track=startTrack; track<endTrack; track++) {
// check for new key signature
// we only do this for the first track of the first staff
// this means we miss the event of having two different
// key signatures in different staves of the same part
// This remains for future version if needed
// we look inside this loop to make sure we don't miss
// any segments. This could be improved for speed.
// A KeySig that has generated == true was created by
// layout, and is probably at the beginning of a new line
// so we don't need it.
if(segment.elementAt(keySigTrack)
&& segment.elementAt(keySigTrack).type == Element.KEYSIG
&& (!segment.elementAt(keySigTrack).generated)) {
//console.log("found KEYSIG");
// just forget the previous measure info
// to not generate any courtesy accidentals
prevMeasureArray = new Array();
}
// look for notes and grace notes
if(segment.elementAt(track) && segment.elementAt(track).type == Element.CHORD) {
// process graceNotes if present
if(segment.elementAt(track).graceNotes.length > 0) {
var graceChords = segment.elementAt(track).graceNotes;
for(var j=0;j<graceChords.length;j++) {
var notes = graceChords[j].notes;
for(var i=0;i<notes.length;i++) {
processNote(notes[i],prevMeasureArray,curMeasureArray);
}
}
}
// process notes
var notes = segment.elementAt(track).notes;
for(var i=0;i<notes.length;i++) {
processNote(notes[i],prevMeasureArray,curMeasureArray);
}
}
}
segment=segment.next;
}
}
function addAcc() {
console.log("start add courtesy accidentals");
//curScore.startCmd();
if (typeof curScore === 'undefined' || curScore == null) {
console.log("error: no score!");
Qt.quit();
}
// find selection
var startStaff;
var endStaff;
var endTick;
var cursor = curScore.newCursor();
cursor.rewind(1);
if(!cursor.segment) {
// no selection
console.log("no selection: processing whole score");
processAll = true;
startStaff = 0;
endStaff = curScore.nstaves;
} else {
startStaff = cursor.staffIdx;
cursor.rewind(2);
endStaff = cursor.staffIdx+1;
endTick = cursor.tick;
if(endTick == 0) {
// selection includes end of score
// calculate tick from last score segment
endTick = curScore.lastSegment.tick + 1;
}
cursor.rewind(1);
console.log("Selection is: Staves("+startStaff+"-"+endStaff+") Ticks("+cursor.tick+"-"+endTick+")");
}
console.log("ProcessAll is "+processAll);
// go through all staves of a part simultaneously
// find staves that belong to the same part
var curStartStaff = startStaff;
while(curStartStaff < endStaff) {
// find end staff for this part
var curEndStaff = getEndStaffOfPart(curStartStaff);
if(curEndStaff > endStaff) {
curEndStaff = endStaff;
}
// do the work
processPart(cursor,endTick,curStartStaff*4,curEndStaff*4);
// next part
curStartStaff = curEndStaff;
}
//curScore.doLayout();
//curScore.endCmd();
console.log("end add courtesy accidentals");
Qt.quit();
}
onRun: {
if(mscoreMajorVersion == 3 && mscoreMinorVersion == 0
&& mscoreUpdateVersion < 5) {
versionError.open();
} else {
addAcc();
}
}
}