-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReset.qml
341 lines (305 loc) · 10.8 KB
/
Reset.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
// ReBeamer: Beam over rests and/or apply custom beaming rules
// Copyright (C) 2023 XiaoMigros
// Changelog:
// v0.8.0 (20230324): MuseScore 4 support
// minor code improvements
// new loading screen appears if plugin takes longer than 1 second to run
// v0.7.0 (20230313): completely rewritten beaming system
// support of complex time signatures
// support for rebeaming notes
// addition of other plugin tools
// v0.6.2 (20230205): forceBeamM improvements and code restructuring
// v0.6.1 (20230203): Improved 8th beaming rules
// v0.6.0 (20230202): Beta Release
// Beaming rules source:
// Gould, Elaine (2011). Behind Bars: The definitive guide to music notation (1st ed.). Faber Music Ltd.
//
// If any rules are broken (and not due to user input) that is unintentional!
//
// For optimal results, make sure the beaming set by the plugin is in line with what is written in the score
// Additionally, make sure the score is not corrupt.
// The plugin also does not alter or rewrite any existing note values, for that use 'Tools/Regroup Rhythms'.
// Plugin Structure:
// Plugin:
// - mapMeasures function: logs length, start, and time signature of every measure
// - applyBeamingRules: applies beaming rules to a measure based on engraving rules,
// chop, applyCorrections functions: fix mistakes left by applyBeamingRules
// - posBeamRests (in progress): automatically repositions notes and beams
// - onRun function: determines how to run the plugin (dockable or not, etc)
// - beamOverRests function: goes through score/selection and beams
// Presets folder: contains an editable list of rules
// Assets folder: contains other files needed by the plugin
// Reset.qml: Retroactively undoes any changes made by this plugin
// Simplify Tuplets.qml: A lightweight edit of the plugin that only focuses on tuplets
import QtQuick 2.0;
import MuseScore 3.0;
import QtQuick.Dialogs 1.2
import QtQuick.Controls 1.0
import QtQuick.Layouts 1.0
import Qt.labs.settings 1.0
MuseScore {
description: (qsTr("This plugin resets changes made by 'Rebeam Music'.") + "\n" +
qsTr("Requires MuseScore 3.5 or later"))
requiresScore: true;
version: "1.0";
menuPath: "Plugins." + qsTr("ReBeamer") + "." + qsTr("Reset")
//settings vars
property var beamType: beamNotes.checked ? (beamRests.checked ? 3 : 2) : (beamRests.checked ? 1 : 0)
//function vars
property var mstart: []; //start tick of each measure
property var mlen: []; //length of each measure in ticks
property var maTsN: []; //only use to compare ts to previous one, or customising to each time change will be impossible
property var maTsD: [];
property var mnTsN: [];
property var mnTsD: [];
property var mno: 0; //measure number - 1
property var lm; //the last measure (start tick), not counting the appended one
property var startStaff; //used to track selection
property var endStaff;
property var fullScore: false;
property var staff;
property var voice;
Component.onCompleted : {
if (mscoreMajorVersion >= 4) {
title = qsTr("ReBeamer: Reset")
//thumbnailName = "logo.png"
categoryCode = "notes-rests"
} //if
}//component
function mapMeasures() {
//this function maps every measure in the score, their start ticks, their length, and their time signature.
var cursor = curScore.newCursor();
if (fullScore) {
cursor.rewind(Cursor.SCORE_START);
} else {
cursor.rewind(Cursor.SELECTION_START);
}
while ((cursor.measure) && (cursor.measure.firstSegment.tick <= lm)) {
var m = cursor.measure;
var aTsN = m.timesigActual.numerator;
var aTsD = m.timesigActual.denominator;
var nTsN = m.timesigNominal.numerator;
var nTsD = m.timesigNominal.denominator;
var mTicks = division * 4.0 * aTsN / aTsD;
mstart.push(m.firstSegment.tick);
mlen.push(mTicks);
maTsN.push(aTsN);
maTsD.push(aTsD);
mnTsN.push(nTsN);
mnTsD.push(nTsD);
if (aTsN == nTsN && aTsD == nTsD) {
console.log(mlen[mno] + " tick long measure at tick " + mstart[mno] +
". TS: " + maTsN[mno] + "/" + maTsD[mno] + ", measure no. " + (mno+1));
} else {
console.log(mlen[mno] + " tick long measure at tick " + mstart[mno] +
". TS: " + mnTsN[mno] + "/" + mnTsD[mno] + " (actual: " + maTsN[mno] + "/" + maTsD[mno] + "), measure no. " + (mno+1));
}
cursor.nextMeasure();
mno += 1;
} //while
console.log("built measure map")
} //function
function smartRewind(cursorrrr, tick) {
//rewinds to the correct place
cursorrrr.rewindToTick(tick)
cursorrrr.staffIdx = staff
cursorrrr.voice = voice
}
function smartTick(tick) {
//converts ticks into human readable beats, fit for the debug console
var places = 3 //max number of decimal places
var places2 = Math.pow(10, places)
var beat = ((tick - mstart[mno]) / division) + 1
if (countDecimals(beat) > 6) {
beat = Math.round(beat * places2) / places2
}
return ("beat " + beat);
}
function countDecimals(value) {
if (Math.floor(value) !== value) {
return value.toString().split(".")[1].length || 0;
} else {
return 0;
}
}
function getDuration(element) {
//returns an element's duration as a number, outside of the fraction wrapper
return (element.duration.numerator / element.duration.denominator);
}//getDuration
function validType(element) {
if (element && (element.type == Element.REST || element.type == Element.NOTE || element.type == Element.CHORD)) {
return true;
} else {
return false;
}
}//validType
function validRest(element) {
return (element && element.type == Element.REST)
}//validRest
function validNote(element) {
return (element && (element.type == Element.NOTE || element.type == Element.CHORD))
}//validNote
function validTuplet(element) {
return (element && element.tuplet)
}
function beamMode(tick, e, mode, type) {
if (validType(e)) {
//type variables: whether to expose the subdivisions in rests (1), notes (2), none (0), or both (3).
//this cursor is needed to compare the element to beam with its preceding element
//beam type settings otherwise give undesired results
var cursorr = curScore.newCursor()
smartRewind(cursorr, tick)
while (cursorr.tick >= tick && tick != 0) {//cursor.prev() alone yields mixed results
cursorr.prev()
}
switch (type) {
case 0: {
console.log("not beaming " + smartTick(tick))
break;
}
case 1: {
if (validRest(e) || validRest(cursorr.element)) {
e.beamMode = mode
console.log("beamed rest " + smartTick(tick) + " to " + mode)
}
break;
}
case 2: {
if (validNote(e) && validNote(cursorr.element)) {
e.beamMode = mode
console.log("beamed note " + smartTick(tick) + " to " + mode)
}
break;
}
case 3: {
e.beamMode = mode
console.log("beamed " + smartTick(tick) + " to " + mode)
break;
}
default: {
console.log("error beaming")
break;
}
}//switch
}
}//beamMode
function applyReset() {
var cursor = curScore.newCursor();
smartRewind(cursor, mstart[mno]); //start of measure number mno, works for all voices
//set all beaming to beammode 0
while (cursor.next() && cursor.tick < mstart[mno] + mlen[mno]) {
beamMode(cursor.tick, cursor.element, 0, beamType)
if (validTuplet(cursor.element) && tupletShape.checked) {
cursor.element.tuplet.bracketType = 0;
}
}
}//applybeamingrules
onRun: {
if ((mscoreMajorVersion < 4 && mscoreMinorVersion < 3) || mscoreMajorVersion < 3) {
Qt.quit()
} else {
resetDialog.open()
}
} //onrun
Dialog {
id: resetDialog
title: qsTr("ReBeamer: Reset")
ColumnLayout {
spacing: 10
anchors.margins: 10
CheckBox {
id: beamNotes
text: qsTr("Reset Notes")
}
CheckBox {
id: beamRests
text: qsTr("Reset Rests")
}
CheckBox {
id: tupletShape
text: qsTr("Reset Tuplet Brackets")
}
}
standardButtons: StandardButton.Cancel | StandardButton.Ok
onAccepted: {
curScore.startCmd()
beamOverRests()
curScore.endCmd()
resetDialog.close()
smartQuit()
}
onRejected: {
resetDialog.close()
smartQuit()
}
}
function beamOverRests() {
var c = curScore.newCursor()
if (curScore.selection.isRange) {
console.log(curScore.selection.startSegment, curScore.selection.endSegment, curScore.selection.startStaff, curScore.selection.endStaff)
startStaff = curScore.selection.startStaff
endStaff = curScore.selection.endStaff
c.rewind(Cursor.SELECTION_END)
if (c.tick == 0) {
lm = curScore.lastMeasure.firstSegment.tick;
} else {
lm = c.measure.firstSegment.tick;
}//else
} else {
fullScore = true;
startStaff = 0; // start with 1st staff
endStaff = curScore.nstaves; // and end with last
lm = curScore.lastMeasure.firstSegment.tick;
}
mapMeasures()
curScore.appendMeasures(1); //safety against weird ticks with end of score stuff, removed later on
//error message, visible to end user in the score
var msg = newElement(Element.SYSTEM_TEXT);
msg.text = qsTr("An error with the\nplugin has occured.")
msg.size = 12;
msg.placement = Placement.ABOVE;
c.rewindToTick(curScore.lastMeasure.firstSegment.tick);
c.add(msg);
for (staff = startStaff; staff < endStaff; staff++) {
console.log("In staff " + (staff+1))
for (voice = 0; voice < 4; voice++) {
console.log("In voice " + (voice+1))
//add backup rest in last measure, safety against bad voice calculations
smartRewind(c, curScore.lastMeasure.firstSegment.tick)
c.addRest(c.duration)
mno = 0; //used by other functions to count through score
while (mno < mstart.length) {
console.log("At measure " + (mno+1))
applyReset();
c.nextMeasure();
mno += 1;
} //while
} //for voice
} // for staff
removeElement(curScore.lastMeasure);
console.log("end");
}//beamOverRests
function smartQuit() {
if (mscoreMajorVersion < 4) {
Qt.quit()
}
else {
quit()
}
}//smartQuit
function addError(error) {
var errtext = newElement(Element.SYSTEM_TEXT);
errtext.text = error
errtext.size = 12;
errtext.placement = Placement.BELOW;
var cursor5 = curScore.newCursor()
cursor5.rewindToTick(curScore.lastMeasure.firstSegment.tick);
cursor5.add(errtext);
}
Settings {
id: settings
property alias beamNotes: beamNotes.checked
property alias beamRests: beamRests.checked
property alias tupletShape: tupletShape.checked
}
} //MuseScore