File: random2.qml

package info (click to toggle)
musescore2 2.3.2%2Bdfsg4-15
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 168,212 kB
  • sloc: cpp: 262,317; xml: 176,707; sh: 3,377; ansic: 1,384; python: 356; makefile: 227; perl: 82; pascal: 78
file content (106 lines) | stat: -rw-r--r-- 3,256 bytes parent folder | download | duplicates (5)
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
import QtQuick 2.1
import MuseScore 1.0
import QtQuick.Controls 1.0
import QtQuick.Layouts 1.0

MuseScore {
      version:  "2.1"
      description: "Create random score."
      menuPath: "Plugins.random2"
      pluginType: "dock"
      dockArea:   "left"
      width:  150
      height: 75

      onRun: { }

      function addNote(key, cursor) {
            var cdur = [ 0, 2, 4, 5, 7, 9, 11 ];
            //           c  g  d  e
            var keyo = [ 0, 7, 2, 4 ];

            var idx    = Math.random() * 6;
            var octave = Math.floor(Math.random() * octaves.value);
            var pitch  = cdur[Math.floor(idx)] + octave * 12 + 60 + keyo[key];
            console.log("Add note pitch "+pitch);
            cursor.addNote(pitch);
            }

      function createScore() {
            var measures    = 18; //in 4/4 default time signature
            var numerator   = 3;
            var denominator = 4;
            var key         = 2; //index in keyo from addNote function above

            var score = newScore("Random2.mscz", "piano", measures);

            score.startCmd();
            score.addText("title", "==Random2==");
            score.addText("subtitle", "Another subtitle");

            var cursor = score.newCursor();
            cursor.track = 0;

            cursor.rewind(0);

            var ts = newElement(Element.TIMESIG);
            ts.setSig(numerator, denominator);
            cursor.add(ts);

            var realMeasures = Math.ceil(measures * denominator / numerator);
            console.log(realMeasures);
            var notes = realMeasures * 4; //number of 1/4th notes

            for (var staff = 0; staff < 2; ++staff) { //piano has two staves to fill
                  cursor.track = staff * 4; //4 voice tracks per staff
                  cursor.rewind(0); //go to the start of the score
                  //add notes
                  for (var i = 0; i < notes; ++i) {
                        if (Math.random() < 0.4) {
                              console.log("Adding two notes at ", i);
                              cursor.setDuration(1, 8);
                              addNote(key, cursor);
                              addNote(key, cursor);
                              }
                        else {
                              console.log("Adding note at ", i);
                              cursor.setDuration(1, 4);
                              addNote(key, cursor);
                              }
                        } //done adding notes to this staff
                  }
            score.endCmd();
            Qt.quit();
            }

    GridLayout {
        anchors.fill: parent
        columns: 2
        rowSpacing: 5


        Text {
            text: "Octaves"
            color: "white"
            }

        SpinBox {
            id: octaves
            minimumValue: 1
            maximumValue: 3
            stepSize:     1
            Layout.fillWidth: true
            Layout.preferredHeight: 25
            value: 1
            }

        Button {
            text: "create"
            Layout.columnSpan: 2
            Layout.fillWidth: true
            onClicked: {
                createScore()
                }
            }
        }
    }