Skip to content

Instantly share code, notes, and snippets.

@lgvr123
Created Jul 22, 2021
Embed
What would you like to do?
import QtQuick 2.9
import QtQuick.Controls 2.2
import MuseScore 3.0
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2
import QtQuick.Controls.Styles 1.4
import QtQuick.Layouts 1.1
import FileIO 3.0
import "zparkingb/notehelper.js" as NoteHelper
MuseScore {
menuPath : "Plugins.Mcve B"
description : "Description goes here"
version : "1.0"
requiresScore : false
onRun : {
printWorkout();
}
property int _Cpitch : 48
function printWorkout() {
var patts = [];
patts[[0]] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; // One basic pattern
var roots = [0]; // C
// Les notes
var pages = [];
pages[0] = [];
// We sort by roots. By root, repeat every pattern
var root = roots[0];
var notes = [];
var basesteps = patts[0];
for (var j = 0; j < basesteps.length; j++) {
notes.push(root + basesteps[j]);
}
pages[0].push({
"root" : root,
"mode" : "major",
"notes" : notes
});
// Debug
for (var i = 0; i < pages.length; i++) {
for (var j = 0; j < pages[i].length; j++) {
for (var k = 0; k < pages[i][j].notes.length; k++) {
console.log(i + ") [" + pages[i][j].root + "/" + pages[i][j].mode + "] " + pages[i][j].notes[k]);
}
}
}
// To Score
//var score = newScore("Workout", "saxophone", 20);
var score = newScore("library", "saxophone", 99);
var numerator = 4;
var denominator = 4;
score.addText("title", "Workouts");
score.startCmd();
var cursor = score.newCursor();
cursor.track = 0;
cursor.rewind(0);
var ts = newElement(Element.TIMESIG);
ts.timesig = fraction(numerator, denominator);
cursor.add(ts);
cursor.rewind(0);
var cur_time = cursor.segment.tick;
var counter = 0;
var prevRoot = '';
var preferredTpcs = NoteHelper.tpcs;
for (var i = 0; i < pages.length; i++) {
for (var j = 0; j < pages[i].length; j++) {
var root = pages[i][j].root;
var mode = pages[i][j].mode;
if (root !== prevRoot) {
preferredTpcs = filterTpcs(root, mode);
prevRoot = root;
}
for (var k = 0; k < pages[i][j].notes.length; k++, counter++) {
if (counter > 0) {
cursor.rewindToTick(cur_time); // be sure to move to the next rest, as now defined
cursor.next();
}
cursor.setDuration(1, 4); // quarter
var note = cursor.element;
var delta = pages[i][j].notes[k];
var pitch = _Cpitch + delta;
var tpc = 14; // One default value. The one of the C natural.
for (var t = 0; t < preferredTpcs.length; t++) {
if (preferredTpcs[t].pitch == (delta % 12)) {
tpc = preferredTpcs[t].tpc;
break;
}
}
var target = {
"pitch" : pitch,
"tpc1" : tpc,
"tpc2" : tpc
};
note = NoteHelper.restToNote(note, target);
//cur_time = note.parent.tick; // getting note's segment's tick
cur_time = cursor.segment.tick;
debugNote(delta, note);
}
// Fill with rests until end of measure
var fill = pages[i][j].notes.length % 4;
if (fill > 0) {
fill = 4 - fill;
//console.log("Going to fill for :"+fill);
for (var f = 0; f < fill; f++) {}
cursor.rewindToTick(cur_time); // be sure to move to the next rest, as now defined
cursor.next();
cursor.setDuration(1, 4); // quarter
cursor.addRest();
cur_time = cursor.segment.tick;
}
}
}
score.endCmd();
}
// Filter the TPCS list according to the mode ("major"/"minor")
function filterTpcs(root, mode) {
var sharp_mode = true;
var accidentals = sharp_mode ? ['NONE', 'SHARP', 'SHARP2'] : ['NONE', 'FLAT', 'FLAT2']
var preferredTpcs;
// On ne garde que les tpcs correspondant au type d'accord et trié par type d'altération
var preferredTpcs = NoteHelper.tpcs.filter(function (e) {
return accidentals.indexOf(e.accidental) >= 0;
});
preferredTpcs = preferredTpcs.sort(function (a, b) {
var acca = accidentals.indexOf(a.accidental);
var accb = accidentals.indexOf(b.accidental);
if (acca != accb)
return acca - accb;
return a.pitch - b.pitch;
});
for (var i = 0; i < preferredTpcs.length; i++) {
if (preferredTpcs[i].pitch < 0)
preferredTpcs[i].pitch += 12;
//console.log(root + " (" + mode + ") => " + sharp_mode + ": " + preferredTpcs[i].name + "/" + preferredTpcs[i].pitch);
}
return preferredTpcs;
}
// Debug one note as added to the score
function debugNote(delta, n) {
NoteHelper.enrichNote(n);
console.log("delta: " + delta + ", pitch: " + n.pitch + ", tpc: " + n.tpc + ", name: " + n.extname.fullname);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment