-
Notifications
You must be signed in to change notification settings - Fork 1
/
checkHarmonyRules.qml
459 lines (407 loc) · 20.4 KB
/
checkHarmonyRules.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
//==============================================
// check harmony rules
//
// Copyright (C)2015 Jörn Eichler (heuchi)
//
// This plugin aims at providing the functionality of the
// harmonyRules plugin for v1.3 created by Yvonne Cliff
//
// This plugin is not a port of the original version,
// but has been completely rewritten.
//
// Results of both plugins might therefore sometimes be different.
//
// 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.1
import MuseScore 1.0
MuseScore {
menuPath: "Plugins.Proof Reading.Check Harmony Rules"
description: "Check harmony rules.\nBased on the textbook 'First Year Harmony' by William Lovelock and aiming at providing the functionality of the plugin harmonyRules by Yvonne Cliff for MuseScore v1.x"
version: "0.1"
// colors taken from harmonyRules plugin
property var colorFifth: "#e21c48";
property var colorOctave: "#ff6a07";
property var colorLargeInt: "#7b0e7f";
property var colorError: "#ff6a64";
property bool processAll: false;
property bool errorChords: false;
MessageDialog {
id: msgMoreNotes
title: "Warning"
text: "This plugin only checks the top note of each voice.\nChords are ignored."
onAccepted: {
Qt.quit();
}
visible: false;
}
function sgn(x) {
if (x > 0) return(1);
else if (x == 0) return(0);
else return(-1);
}
function isBetween(note1,note2,n) {
// test if pitch of note n is between note1 and note2
if (note1.pitch > note2.pitch) {
if (n.pitch < note1.pitch && n.pitch > note2.pitch)
return true;
} else {
if (n.pitch < note2.pitch && n.pitch > note1.pitch)
return true;
}
return false;
}
function markColor(note1, note2, color) {
note1.color = color;
note2.color = color;
}
function markText(note1, note2, msg, color, trck, tick) {
markColor(note1, note2, color);
var myText = newElement(Element.STAFF_TEXT);
myText.text = msg;
//myText.pos.x = 0;
myText.pos.y = 1;
var cursor = curScore.newCursor();
cursor.rewind(0);
cursor.track = trck;
while (cursor.tick < tick) {
cursor.next();
}
cursor.add(myText);
}
function isAugmentedInt(note1, note2) {
var dtpc = note2.tpc - note1.tpc;
var dpitch = note2.pitch - note1.pitch;
// augmented intervals have same sgn for dtpc and dpitch
if (sgn(dtpc) != sgn(dpitch))
return false;
dtpc = Math.abs(dtpc);
dpitch = Math.abs(dpitch) % 12;
// augmented intervalls have tpc diff > 5
if (dtpc < 6)
return false;
if (dtpc == 7 && dpitch == 1) // aug. Unison / Octave
return true;
if (dtpc == 9 && dpitch == 3) // aug. Second / Ninth
return true;
if (dtpc == 11 && dpitch == 5) // aug. Third / ...
return true;
if (dtpc == 6 && dpitch == 6) // aug. Fourth
return true;
if (dtpc == 8 && dpitch == 8) // aug. Fifth
return true;
if (dtpc == 10 && dpitch == 10) // aug. Sixth
return true;
if (dtpc == 12 && dpitch == 0) // aug. Seventh
return true;
// not augmented
return false;
}
function checkDim47(note1, note2, track, tick) {
var dtpc = note2.tpc - note1.tpc;
var dpitch = note2.pitch - note1.pitch;
// diminished intervals have opposite sgn for dtpc and dpitch
if (sgn(dtpc) == sgn(dpitch)) {
return;
}
dtpc = Math.abs(dtpc);
dpitch = Math.abs(dpitch) % 12;
if (dtpc == 8 && dpitch == 4) { // dim. Fourth
markText(note1, note2, "dim. 4th, avoid for now",
colorError,track,tick);
} else if (dtpc == 9 && dpitch == 9) { // dim. Seventh
markText(note1, note2, "dim. 7th, avoid for now",
colorError,track,tick);
}
}
function checkDim5(note1, note2, note3, track, tick) {
var dtpc = note2.tpc - note1.tpc;
var dpitch = note2.pitch - note1.pitch;
// diminished intervals have opposite sgn for dtpc and dpitch
if (sgn(dtpc) == sgn(dpitch)) {
return;
}
dtpc = Math.abs(dtpc);
dpitch = Math.abs(dpitch) % 12;
if (dtpc == 6 && dpitch == 6) {
// check if note3 is inbetween
if (!isBetween(note1,note2,note3)) {
note3.color = colorError;
markText(note1,note2,
"dim. 5th should be followed by\nnote within interval",
colorError,track,tick);
}
}
}
function check6NextNote(note1, note2, note3, track, tick) {
var dtpc = note2.tpc - note1.tpc;
var dpitch = note2.pitch - note1.pitch;
var sameSgn = (sgn(dtpc) == sgn(dpitch));
dtpc = Math.abs(dtpc);
dpitch = Math.abs(dpitch) % 12;
// check dim6th, min. 6th or maj. 6th
if ((dtpc == 11 && dpitch == 7 && !sameSgn)
|| (dtpc == 4 && dpitch == 8 && !sameSgn)
|| (dtpc == 3 && dpitch == 9 && sameSgn)) {
// check if note3 is inbetween
if (!isBetween(note1,note2,note3)) {
note3.color = colorError;
markText(note1,note2,
"6th better avoided, but should be followed by\nnote within interval",
colorError,track,tick);
} else {
markText(note1,note2,
"6th better avoided",
colorError,track,tick);
}
}
}
function check7AndLarger(note1, note2, track, tick, flag) {
var dtpc = Math.abs(note2.tpc - note1.tpc);
var dpitch = Math.abs(note2.pitch - note1.pitch);
if (dpitch > 9 && dpitch != 12 && dtpc < 6) {
if (flag) {
markText(note1,note2,
"No 7ths, 9ths or larger\nnor with 1 note in between",
colorLargeInt,track,tick);
} else {
markText(note1, note2,
"No 7ths, 9ths or larger",colorLargeInt,track,tick);
}
}
}
function isOctave(note1, note2) {
var dtpc = Math.abs(note2.tpc - note1.tpc);
var dpitch = Math.abs(note2.pitch - note1.pitch);
if (dpitch == 12 && dtpc == 0)
return true;
else
return false;
}
function check8(note1, note2, note3, track, tick) {
// check if note2 and note3 form an octave
// and note1 is not inbetween
if (isOctave(note2,note3) && !isBetween(note2,note3,note1)) {
note3.color = colorError;
markText(note1,note2,
"Octave should be preceeded by note within compass",
colorError,track,tick);
}
// check if note1 and note2 form an octave
// and note3 is not inbetween
if (isOctave(note1,note2) && !isBetween(note1,note2,note3)) {
note3.color = colorError;
markText(note1,note2,
"Octave should be followed by note within compass",
colorError,track,tick);
}
}
onRun: {
console.log("start")
if (typeof curScore == 'undefined' || curScore == null) {
console.log("no score found");
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+")");
}
// initialize data structure
var changed = [];
var curNote = [];
var prevNote = [];
var curRest = [];
var prevRest = [];
var curTick = [];
var prevTick = [];
var foundParallels = 0;
var track;
var startTrack = startStaff * 4;
var endTrack = endStaff * 4;
for (track = startTrack; track < endTrack; track++) {
curRest[track] = true;
prevRest[track] = true;
changed[track] = false;
curNote[track] = 0;
prevNote[track] = 0;
curTick[track] = 0;
prevTick[track] = 0;
}
// go through all staves/voices simultaneously
if(processAll) {
cursor.track = 0;
cursor.rewind(0);
} else {
cursor.rewind(1);
}
var segment = cursor.segment;
while (segment && (processAll || segment.tick < endTick)) {
// Pass 1: read notes
for (track = startTrack; track < endTrack; track++) {
if (segment.elementAt(track)) {
if (segment.elementAt(track).type == Element.CHORD) {
// we ignore grace notes for now
var notes = segment.elementAt(track).notes;
if (notes.length > 1) {
console.log("found chord with more than one note!");
errorChords = true;
}
var note = notes[notes.length-1];
// check for some voice rules
// if we have a new pitch
if ((! curRest[track])
&& curNote[track].pitch != note.pitch) {
// previous note present
// check for augmented interval
if (isAugmentedInt(note, curNote[track])) {
markText(curNote[track],note,
"augmented interval",colorError,
track,curTick[track]);
}
// check for diminished 4th and 7th
checkDim47(curNote[track], note,
track,curTick[track]);
check7AndLarger(curNote[track],note,
track,curTick[track],false);
// have three notes?
if (! prevRest[track]) {
// check for diminished 5th
checkDim5(prevNote[track],curNote[track],
note, track, prevTick[track]);
// check for 6th
check6NextNote(prevNote[track],curNote[track],
note, track, prevTick[track]);
if(!isOctave(prevNote[track],curNote[track]) &&
!isOctave(curNote[track],note))
check7AndLarger(prevNote[track],note,
track,prevTick[track],true);
check8(prevNote[track],curNote[track],note,
track, prevTick[track]);
}
}
// remember note
if (curNote[track].pitch != note.pitch) {
prevTick[track]=curTick[track];
prevRest[track]=curRest[track];
prevNote[track]=curNote[track];
changed[track]=true;
} else {
changed[track]=false;
}
curRest[track]=false;
curNote[track]=note;
curTick[track]=segment.tick;
} else if (segment.elementAt(track).type == Element.REST) {
if (!curRest[track]) {
// was note
prevRest[track]=curRest[track];
prevNote[track]=curNote[track];
curRest[track]=true;
changed[track]=false; // no need to check against a rest
}
} else {
changed[track] = false;
}
} else {
changed[track] = false;
}
}
// Pass 2: find paralleles
for (track=startTrack; track < endTrack; track++) {
var i;
// compare to other tracks
if (changed[track] && (!prevRest[track])) {
var dir1 = sgn(curNote[track].pitch - prevNote[track].pitch);
if (dir1 == 0) continue; // voice didn't move
for (i=track+1; i < endTrack; i++) {
if (changed[i] && (!prevRest[i])) {
var dir2 = sgn(curNote[i].pitch-prevNote[i].pitch);
if (dir1 == dir2) { // both voices moving in the same direction
var cint = curNote[track].pitch - curNote[i].pitch;
var pint = prevNote[track].pitch-prevNote[i].pitch;
// test for 5th
if (Math.abs(cint%12) == 7) {
// test for open parallel
if (cint == pint) {
foundParallels++;
console.log ("P5:"+cint+", "+pint);
markText(prevNote[track],prevNote[i],"parallel 5th",
colorFifth,track,prevTick[track]);
markColor(curNote[track],curNote[i],colorFifth);
} else if (dir1 == 1 && Math.abs(pint) < Math.abs(cint)) {
// hidden parallel (only when moving up)
foundParallels++;
console.log ("H5:"+cint+", "+pint);
markText(prevNote[track],prevNote[i],"hidden 5th",
colorFifth,track,prevTick[track]);
markColor(curNote[track],curNote[i],colorFifth);
}
}
// test for 8th
if (Math.abs(cint%12) == 0) {
// test for open parallel
if (cint == pint) {
foundParallels++;
console.log ("P8:"+cint+", "+pint+"Tracks "+track+","+i+" Tick="+segment.tick);
markText(prevNote[track],prevNote[i],"parallel 8th",
colorOctave,track,prevTick[track]);
markColor(curNote[track],curNote[i],colorOctave);
} else if (dir1 == 1 && Math.abs(pint) < Math.abs(cint)) {
// hidden parallel (only when moving up)
foundParallels++;
console.log ("H8:"+cint+", "+pint);
markText(prevNote[track],prevNote[i],"hidden 8th",
colorOctave,track,prevTick[track]);
markColor(curNote[track],curNote[i],colorOctave);
}
}
}
}
}
}
}
segment = segment.next;
}
// set result dialog
if (errorChords) {
console.log("finished with error");
msgMoreNotes.visible = true;
} else {
console.log("finished");
Qt.quit();
}
}
}