Last active
September 25, 2018 08:16
-
-
Save lasconic/4ac255e2f1aaef0d1a2a97d9dc70e91e to your computer and use it in GitHub Desktop.
This plugin for MuseScore 2 randomizes the velocity of notes between minRandom and maxRandom
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//============================================================================= | |
// MuseScore | |
// Music Composition & Notation | |
// | |
// Copyright (C) 2018 Nicolas Froment | |
// | |
// This program is free software; you can redistribute it and/or modify | |
// it under the terms of the GNU General Public License version 2 | |
// as published by the Free Software Foundation and appearing in | |
// the file LICENCE.GPL | |
//============================================================================= | |
import QtQuick 2.0 | |
import MuseScore 1.0 | |
MuseScore { | |
version: "1.0" | |
description: qsTr("This plugin randomizes the velocity of notes") | |
menuPath: "Plugins.Notes.RandomVelocity" | |
property int minRandom : -10 | |
property int maxRandom : 10 | |
// Apply the given function to all notes in selection | |
// or, if nothing is selected, in the entire score | |
function applyToNotesInSelection(func) { | |
var cursor = curScore.newCursor(); | |
cursor.rewind(1); | |
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) // if no selection, beginning of score | |
while (cursor.segment && (fullScore || cursor.tick < endTick)) { | |
if (cursor.element && cursor.element.type === Element.CHORD) { | |
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++) | |
func(graceNotes[j]); | |
} | |
var notes = cursor.element.notes; | |
for (var k = 0; k < notes.length; k++) { | |
var note = notes[k]; | |
func(note); | |
} | |
} | |
cursor.next(); | |
} | |
} | |
} | |
} | |
function getRandomIntInclusive(min, max) { | |
min = Math.ceil(min); | |
max = Math.floor(max); | |
return Math.floor(Math.random() * (max - min +1)) + min; | |
} | |
function randomVelocity(note) { | |
note.veloType = Note.OFFSET_VAL | |
note.veloOffset = getRandomIntInclusive(minRandom, maxRandom) | |
} | |
onRun: { | |
console.log("hello random velocity"); | |
if (typeof curScore === 'undefined') | |
Qt.quit(); | |
applyToNotesInSelection(randomVelocity) | |
Qt.quit(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment