-
Notifications
You must be signed in to change notification settings - Fork 0
/
ji_composer.qml
346 lines (313 loc) · 10.5 KB
/
ji_composer.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
import QtQuick 2.2 // TODO : HEJI note name support
import MuseScore 3.0 // TODO : convert note with accidental to note with TEXT accidental
import QtQuick.Controls 1.1
import QtQuick.Controls.Styles 1.3
import QtQuick.Layouts 1.1
import QtQuick.Dialogs 1.1
import "file:///home/akselai/Documents/MuseScore3/Plugins/calc.js" as Calc
MuseScore {
menuPath: "Plugins.ji_composer"
description: "A control panel for composing in just intonation (JI)."
version: "1.0"
requiresScore: false
pluginType: "dialog"
dockArea: "left"
width: 200
height: 75
function applyToNotesInSelection(func, params) {
var cursor = curScore.newCursor();
cursor.rewind(0);
var startStaff;
var endStaff;
var endTick;
var fullScore = false;
if (!cursor.segment) { // no selection
fullScore = true;
startStaff = 0; // start with 1st staff
endStaff = curScore.nstaves - 1; // and end with last
} else {
startStaff = cursor.staffIdx;
cursor.rewind(2);
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;
}
endStaff = cursor.staffIdx;
}
console.log(startStaff + " - " + endStaff + " - " + 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;
if (fullScore)
cursor.rewind(0);
var pointer = 0;
while (cursor.segment && (fullScore || cursor.tick < endTick)) {
if (cursor.element && cursor.element.type === Element.CHORD) {
var curlen = cursor.segment.annotations.length;
var graceChords = cursor.element.graceNotes;
for (var i = 0; i < graceChords.length; i++) {
// iterate through all grace chords
var graceNotes = graceChords[i].notes;
for (var j = 0; j < graceNotes.length; j++) {
/*
if (curlen != 0) {
func(graceNotes[j], cursor.segment.annotations[0].text);
} else func(graceNotes[j], 0);
*/
}
}
var notes = cursor.element.notes;
var stackNotes = [];
var stackAccidental = [];
for (var k = 0; k < notes.length; k++) {
var note = notes[k];
console.log("octave: " + baseOctave(note));
console.log("base: " + k + " " + baseNote(note));
if (k != 0) {
if (baseNote(notes[k - 1]) != baseNote(note) || baseOctave(notes[k - 1]) != baseOctave(note)) {
console.log(k);
}
}
}
for (var k = 0; k < notes.length; k++) {
/*
if (curlen != 0) {
var commaList = cursor.segment.annotations[0].text.split("\n");
func(note, commaList[commaList.length - k - 1]);
} else {
func(note, "*");
}
*/
func(note, "*");
}
}
pointer++;
cursor.next();
}
}
}
}
function getNote(object) {
var acx = object.accidentalType + 0;
var acc = 0;
switch (acx) {
case 0: acc = 0; break; // nothing
case 1: acc = -1; break; // b
case 2: acc = 0; break; // natural
case 3: acc = 1; break; // #
case 4: acc = 2; break; // ##
case 5: acc = -2; break; // bb
case 6: acc = 3; break; // ###
case 7: acc = -3; break; // bbb
default: acc = 0;
}
var midiNum = (object.pitch - acc) % 12;
var nm = "";
switch (midiNum) {
case 9: nm = "A"; break;
case 11: nm = "B"; break;
case 0: nm = "C"; break;
case 2: nm = "D"; break;
case 4: nm = "E"; break;
case 5: nm = "F"; break;
case 7: nm = "G"; break;
default: console.log(midiNum + " cannot get name! " + acc);
}
return {name: nm, accidental: acc, octave: Math.floor((object.pitch - acc) / 12) - 1};
}
function intervalStep(tick, interval, dur_z, dur_n) {
var c = curScore.newCursor();
c.track = 0;
c.rewindToTick(tick);
var note = c.element.notes[0];
var midiNum = note.pitch - accidentalVal(note);
var steps = (interval.charAt(interval.length - 2) == "-" ? "-" : "") + interval.charAt(interval.length - 1);
var val = diatonicKeyVal(midiNum, steps) + interpretIntervalPrefix(interval) + accidentalVal(note);
console.log(midiNum + " " + steps + " " + val);
c.next();
if (dur_z !== undefined && dur_n !== undefined) {
c.setDuration(dur_z, dur_n);
} else {
c.setDuration(1, 4);
}
c.addNote(diatonicStep(midiNum, steps));
c.prev();
var x = c.element.notes[0];
var text = newElement(Element.STAFF_TEXT);
text.placement = Placement.ABOVE;
text.text = mul_r(noteNameInterval(baseNote(x)), pow_r("2187/2048", val));
c.add(text);
x.fixed = true;
x.fixedLine = 100; //noteToLine(x);
x.accidentalType = valAccidental(val);
console.log("a237468 " + x.fixedLine);
/*
var txt = newElement(Element.STAFF_TEXT);
txt.placement = Placement.ABOVE;
txt.text = '<font face="atesting"/>' + '<font size="20"/>' + "333___";
txt.offsetY = 2;
txt.offsetX = -1;
c.add(txt);
*/
}
function noteTick(note) {
var esc = 0;
while (note.type != Element.SEGMENT) {
note = note.parent;
if (note.tick >= 0) {
console.log(note.tick);
return note.tick
};
esc++;
if (esc > 6) {
return undefined;
}
}
}
function addNoteAfter(pitch, tuning, fixedLine, accidental, tick, dur_z, dur_n) {
var c = curScore.newCursor();
c.track = 0;
c.rewindToTick(tick);
console.log("ok");
c.next();
if (dur_z !== undefined && dur_n !== undefined) {
c.setDuration(dur_z, dur_n);
} else {
c.setDuration(1, 4);
}
c.addNote(pitch);
c.prev();
var x = c.element.notes[0];
x.fixed = true;
x.fixedLine = fixedLine;
x.tuning = tuning;
var txt = newElement(Element.STAFF_TEXT);
txt.placement = Placement.ABOVE;
txt.text = '<font face="atesting"/>' + '<font size="20"/>' + accidental;
txt.offsetY = 2;
txt.offsetX = -1;
c.add(txt);
}
onRun: {
atest();
// applyToNotesInSelection(tune, true);
// Qt.quit();
}
GridLayout {
anchors.fill: parent
columns: 3
rowSpacing: 5
Button {
text: "retune!"
Layout.columnSpan: 2
Layout.fillWidth: true
onClicked: {
applyToNotesInSelection(tune, true);
}
}
/*
Button {
text: "m3"
Layout.columnSpan: 1
Layout.fillWidth: false
onClicked: {
curScore.startCmd();
addNoteAfter(60, 50, 0, null, noteTick(curScore.selection.elements[0]), 1, 4);
curScore.endCmd();
}
}
*/
Button {
text: "m3"
Layout.columnSpan: 1
Layout.fillWidth: false
onClicked: {
stepper("m3");
}
}
/*
Button {
text: "M3"
Layout.columnSpan: 1
Layout.fillWidth: false
onClicked: {
stepper("M3");
}
}
Button {
text: "m-7"
Layout.columnSpan: 1
Layout.fillWidth: false
onClicked: {
stepper("m-7");
}
}
Button {
text: "M-6"
Layout.columnSpan: 1
Layout.fillWidth: false
onClicked: {
stepper("M-6");
}
}
Button {
text: "P-5"
Layout.columnSpan: 1
Layout.fillWidth: false
onClicked: {
stepper("P-5");
}
}
Button {
text: "P-4"
Layout.columnSpan: 1
Layout.fillWidth: false
onClicked: {
stepper("P-4");
}
}
Button {
text: "m-3"
Layout.columnSpan: 1
Layout.fillWidth: false
onClicked: {
stepper("m-3");
}
}
Button {
text: "M-2"
Layout.columnSpan: 1
Layout.fillWidth: false
onClicked: {
stepper("M-2");
}
}
*/
}
Item {
id: pressKey
focus: true
Keys.onPressed: {
keyPressEvent(event.key);
}
function keyPressEvent(key) {
console.log(key);
}
}
function atest() {
curScore.startCmd();
console.log(getNote(curScore.selection.elements[0]).name);
}
function stepper(interval) {
curScore.startCmd();
intervalStep(noteTick(curScore.selection.elements[0]), interval, 1, 4);
applyToNotesInSelection(tune, true);
curScore.endCmd();
}}