//============================================================================= // MuseScore // Linux Music Score Editor // $Id:$ // // Plugin for gernerating random scores for sight reading practise. // Copyright (c) Chris Wilkinson, 2014 // // This file is a derivative of createscore.js, one of MuseScore's // factory-included plugins. Original file Copyright (C)2008-2010 // Werner Schweer and others // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License version 2. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. //============================================================================= // // This is ECMAScript code (ECMA-262 aka "Java Script") // //--------------------------------------------------------- // init // this function will be called on startup of // mscore //--------------------------------------------------------- function init() { // print("test script init"); }; function addNote(cursor, pitch, duration, goNextMeasure) { var chord = new Chord(); chord.tickLen = duration; var note = new Note(); note.pitch = pitch; note.velocity = 127; chord.addNote(note); cursor.add(chord); if (goNextMeasure) cursor.nextMeasure(); else cursor.next(); }; function run() { var score = new Score(); score.name = "Random Practise"; score.title = "Random Practise"; score.appendPart("Piano"); // create two staff piano part score.appendMeasures(32); // append empty measures var cursor = new Cursor(score); // Generate a right-hand part cursor.staff = 0; cursor.voice = 0; var pitches = [60, 62, 64, 65, 67, 69, 71, 72, 74, 76]; addLevel1 (cursor, score, pitches); // Generate left-hand part cursor.staff = 1; cursor.voice = 0; var pitches = [45, 47, 48, 50, 52, 53, 55, 57, 59, 60]; addLevel1 (cursor, score, pitches); }; // ____________________________________________________________ // Generate level 1. function addLevel1(cursor, score, pitches) { cursor.rewind(); var phrases = [[0, 1, 0], [0, 1, 2], [0, 1, -1], [0, 1, 2, 0], [0, 1, -1, 0], [0, 1, 2, 3], [0, 1, 0, -1], [0, 1, -1, -2], [0, 1, 0, 1, 0], [0, 1, 2, 1, 0], [0, 1, 0, 1, 2], [0, 1, 0, -1, 0], [0, 1, 0, 1, 2], [0, 1, 2, 0, 1], [0, 1, -2, -1, 0]]; while (true) { var phraseNum = Math.floor (Math.random() * phrases.length); // Choose a phrase at random from the list above. var phrase = phrases [phraseNum]; var invertPhrase = Math.floor( Math.random() * 2); // 50% chance of playing the phrase upside-down. if (invertPhrase) phrase = invertArray (phrase); var reversePhrase = Math.floor( Math.random() * 2); // 50% chance of playing the phrase backwards. if (reversePhrase) phrase = reverseArray (phrase); var phraseStartPitchMin = -minElement (phrase); // No negative indeces. var phraseStartPitchMax = pitches.length - maxElement (phrase) - 1; // No too-high indeces var phraseStartPitch = phraseStartPitchMin + Math.floor (Math.random() * (phraseStartPitchMax - phraseStartPitchMin + 1)); // Randomly shift the phrase up or down the keyboard. phrase = rearraynge (pitches, phrase, phraseStartPitch); // Convert from 0s, 1s and 2s to MIDI note numbers //// Debug information //var debugInfo = "PhraseNum=" + phraseNum + ", phraseStartPitch=" + phraseStartPitch; //if (invertPhrase) // debugInfo = debugInfo + ", inv"; //if (reversePhrase) // debugInfo = debugInfo + ", rev"; //var debugText = new Text (score); //debugText.text = debugInfo; //cursor.putStaffText (debugText); // Now, draw the phrase on the score and leave rests until the beginning of a new bar is reached. for (var i=0; i greatestValue) greatestValue = array[i]; } return greatestValue; }; // Returns an array whose elements are the inverse of those of the arugument function invertArray (array) { var r = []; for (var i=0; i