-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxentuner.qml
540 lines (498 loc) · 13.3 KB
/
xentuner.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
import QtQuick 2.9
import MuseScore 3.0
import QtQuick.Controls 1.5
import QtQuick.Dialogs 1.2
import FileIO 3.0
MuseScore {
menuPath: "Plugins.Xenharmonic Tuner"
description: "Allows microtonal retuning of both natural notes and accidentals."
version: "2.0"
pluginType: "dialog"
width: 377
height: 233
Button {
id: "loadButton"
text: "Select tuning file"
anchors.centerIn: parent
onClicked: {
fileDialog.open();
}
}
Button {
anchors.bottom: parent.bottom
anchors.left: parent.left
text: "Cancel"
onClicked: {
qtQuit();
}
}
Button {
anchors.bottom: parent.bottom
anchors.right: parent.right
text: "Apply selected tuning to score"
onClicked: {
try {
var tuningData = parseTuningFileContent(tuningFile.read());
debugLog("tuningData: " + JSON.stringify(tuningData));
} catch (e) {
fileLabel.text = "JSON parse error. Make sure your file has correct JSON syntax.";
fileLabel.color = "red";
return;
}
try {
processSelection(tuningData);
qtQuit();
} catch (e) {
fileLabel.text = "Some unknown error happened. Sorry!";
}
}
}
Text {
id: "fileLabel"
width: parent.width
wrapMode: Text.WrapAnywhere
}
function getNatural(pitch, tpc) {
// since which natural (A-G) isn't directly available, need to compute it
// middle C is 0, D above is 1, B below is -1, etc.
var answerMod7 = (4 * tpc + 7) % 7;
var firstOctavePitch = 60 + answerMod7 * 12 / 7;
var numOctaves = Math.round((pitch - firstOctavePitch) / 12);
return answerMod7 + numOctaves * 7;
}
function parseTuningFileContent(fileContent) {
var obj = JSON.parse(fileContent);
if (!obj.naturals) {
throw "No naturals found";
}
if (!Array.isArray(obj.naturals)) {
throw "\"naturals\" property is not an array";
}
if (!obj.accidentals) {
throw "No accidentals found";
}
return obj;
}
function getAccidentalName(accidentalType) {
for (const accidentalName in Accidental) {
if (Accidental[accidentalName] == accidentalType)
return accidentalName;
}
}
function getCentsOffset(accidentalType) {
// Only for MuseScore 4.2 and later
// see /src/engraving/dom/accidental.cpp
const centsOffsets = [
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
-50,
-150,
50,
-50,
150,
50,
250,
150,
-150,
-250,
-50,
50,
-50,
-150,
50,
150,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
-6.8,
6.8,
-3.4,
3.4,
-16.5,
16.5,
-1.7,
1.7,
-10.9,
10.9,
33,
-67,
-167,
167,
-183,
183,
-17,
17,
-33,
33,
-50,
50,
-67,
67,
-83,
83,
0,
0,
-116,
116,
-133,
133,
-150,
150,
-5.8,
5.8,
-21.5,
21.5,
-27.3,
27.3,
-43,
43,
-48.8,
48.8,
-53.3,
53.3,
-60.4,
60.4,
-64.9,
64.9,
-70.7,
70.7,
-86.4,
86.4,
-92.2,
92.2,
-107.9,
107.9,
-113.7,
113.7,
-22.2,
22.2,
-44.4,
44.4,
-66.7,
66.7,
-88.9,
111.1
]
debugLog("centsOffset: " + centsOffsets[0 + accidentalType]);
return centsOffsets[0 + accidentalType];
}
function computeTuning(natural, accidentalType, tuningData) {
// return value in cents relative to middle C
var numNaturals = tuningData["naturals"].length;
var naturalIndex = natural % numNaturals;
if (naturalIndex < 0) naturalIndex += numNaturals;
var numPeriods = (natural - naturalIndex) / numNaturals;
var period = tuningData["naturals"][numNaturals - 1];
var middleCPitch = tuningData["middleCPitch"]
if (!middleCPitch) {
middleCPitch = 0;
}
var base;
if (naturalIndex == 0) {
base = middleCPitch + numPeriods * period;
} else {
base = middleCPitch + numPeriods * period + tuningData["naturals"][naturalIndex - 1];
}
var accidental = getAccidentalName(accidentalType);
if (!tuningData["accidentals"].hasOwnProperty(accidental)) {
return base;
}
var modifier = tuningData["accidentals"][accidental]
return base + modifier;
}
function processNote(note, accidentalMap, tuningData) {
var natural = getNatural(note.pitch, note.tpc);
// 1. if note carries an accidental, we're done
var effectiveAccidental = note.accidentalType;
// 2. if the pitch occurred previously in the same measure with
// an accidental, or it was tied to a note with an accidental, use that
if (effectiveAccidental == Accidental.NONE) {
if (accidentalMap.hasOwnProperty(natural)) {
effectiveAccidental = accidentalMap[natural];
} else {
var tiedNote = note;
while (effectiveAccidental == Accidental.NONE) {
if (tiedNote.tieBack) {
tiedNote = tiedNote.tieBack.startNote;
effectiveAccidental = tiedNote.accidentalType;
} else {
break;
}
}
}
}
// 3. next rely on the standard key signature - custom key signatures
// can't work yet since they are not exposed in the plugin API
if (effectiveAccidental == Accidental.NONE) {
if (note.tpc < 6) {
effectiveAccidental = Accidental.FLAT2;
} else if (note.tpc < 13) {
effectiveAccidental = Accidental.FLAT;
} else if (note.tpc < 20) {
effectiveAccidental = Accidental.NONE;
} else if (note.tpc < 27) {
effectiveAccidental = Accidental.SHARP;
} else {
effectiveAccidental = Accidental.SHARP2;
}
}
debugLog("pitch: " + note.pitch + " tpc: " + note.tpc + " accidentalType: " + note.accidentalType + " effectiveAccidental: " + effectiveAccidental + " natural: " + natural);
const absolute = computeTuning(natural, effectiveAccidental, tuningData);
var base = (note.pitch - 60) * 100;
if ((mscoreMajorVersion == 4 && mscoreMinorVersion >= 2) || mscoreMajorVersion > 4) {
base += getCentsOffset(effectiveAccidental);
}
const relative = absolute - base;
debugLog("absolute: " + absolute + " relative: " + relative);
note.tuning = relative;
// record the accidental of this natural for later unmarked pitches in this measure
accidentalMap[natural] = effectiveAccidental;
}
function loadSettings() {
var fileContent = settingsFile.read();
var obj = JSON.parse(fileContent);
if (obj.hasOwnProperty("tuningFile")) {
tuningFile.source = obj.tuningFile;
fileLabel.text = "Tuning file: " + obj.tuningFile;
fileLabel.color = "black";
}
}
function writeSettings(settings) {
settingsFile.write(JSON.stringify(settings));
}
FileDialog {
id: fileDialog
title: "Please choose a file"
folder: shortcuts.home
onAccepted: {
tuningFile.source = fileDialog.fileUrl;
fileLabel.text = "Tuning file: " + fileDialog.fileUrl;
fileLabel.color = "black";
writeSettings({tuningFile: "" + fileDialog.fileUrl});
}
onRejected: {
}
}
FileIO {
id: "tuningFile"
onError: {
debugLog(msg);
}
}
FileIO {
id: "settingsFile"
source: homePath() + "/.musescore-xentuner-settings.json"
onError: {
debugLog(msg);
}
}
FileIO {
id: "debugFile"
source: homePath() + "/.musescore-xentuner-log.txt"
onError: {
console.log(msg); // can't use debugLog since it won't work
}
}
onRun: {
}
Component.onCompleted: {
if (mscoreMajorVersion >= 4) {
title = qsTr("XenTuner")
categoryCode = "playback"
}
if (settingsFile.exists()) {
loadSettings();
} else {
fileLabel.text = "No file selected";
}
}
// from addCourtesyAccidentals.qml, GPLv3
// if nothing is selected process whole score
property bool processAll: false
// 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!
debugLog("error: part for " + startStaff + " not found!");
qtQuit();
}
// function processPart
//
// do the actual work: process all given tracks in parallel
//
// We go through all tracks simultaneously, because we also want to tune
// accidentals for notes of different voices in the same octave
function processPart(cursor,endTick,startTrack,endTrack,tuningData) {
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();
// 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
curMeasureArray = new Array();
if(!cursor.nextMeasure()) {
inLastMeasure=true;
}
}
for(var track=startTrack; track<endTrack; track++) {
// 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],curMeasureArray,tuningData);
}
}
}
// process notes
var notes = segment.elementAt(track).notes;
for(var i=0;i<notes.length;i++) {
processNote(notes[i],curMeasureArray,tuningData);
}
}
}
segment=segment.next;
}
}
function processSelection(tuningData) {
debugLog("start process selection");
curScore.startCmd();
if (typeof curScore === 'undefined' || curScore == null) {
debugLog("error: no score!");
qtQuit();
}
// find selection
var startStaff;
var endStaff;
var endTick;
var cursor = curScore.newCursor();
cursor.rewind(1);
if(!cursor.segment) {
// no selection
debugLog("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);
debugLog("Selection is: Staves("+startStaff+"-"+endStaff+") Ticks("+cursor.tick+"-"+endTick+")");
}
debugLog("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,tuningData);
// next part
curStartStaff = curEndStaff;
}
//curScore.doLayout();
curScore.endCmd();
debugLog("end process selection");
}
function qtQuit() {
(typeof(quit) === 'undefined' ? Qt.quit : quit)();
}
function debugLog(msg) {
var prevContent = debugFile.read();
debugFile.write(prevContent + msg + '\n');
}
}