-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.java
176 lines (162 loc) · 5.2 KB
/
Matrix.java
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
import javax.sound.midi.*;
public class Matrix {
private static int[] row = new int[12];
private static int[][] matrix = new int[12][12];
public static void main(String[] args) {
generateRow();
transposeRowToZero();
copyPZeroToMatrix();
copyInvertedRow();
fillMatrix();
printMatrix();
generateMeasures();
}
private static void generateMeasures() {
int measures = (int)(Math.random()*(36-5+1)+5);
System.out.println(measures + " measures of music");
for (int i = 0; i < measures; i++) {
int row = (int)(Math.random()*(11));
makeMusic(row);
}
}
private static void fillMatrix() {
for (int i = 1; i < 12; i++) {
int diff = matrix[i][0];
for (int j = 1; j < 12; j++) {
matrix[i][j] = matrix[0][j];
matrix[i][j] += diff;
matrix[i][j] = mod(matrix[i][j]);
}
}
}
private static void transposeRowToZero() {
if (row[0] != 0) {
int diff = row[0];
for (int i = 0; i< 12; i++) {
row[i] = row[i] - diff;
row[i] = mod(row[i]);
}
}
}
private static void copyInvertedRow() {
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 12; j++) {
matrix[i][0] = invert(row[i]);
}
}
}
private static void copyPZeroToMatrix() {
for (int i = 0; i < 12; i++) {
matrix[0][i] = row[i];
}
}
private static void generateRow() {
int min = 0;
int max = 11;
for (int i = 0; i < 12; i++) {
boolean repeat = true;
while (repeat) {
repeat = false;
row[i] = (int)(Math.random()*(max-min+1)+min);
for (int j = 0; j < 12; j++) {
if (j == i) {
break;
}
if (row[i] == row[j]) {
repeat = true;
break;
}
}
}
}
}
private static void makeMusic(int row) {
int numOfNotes = 12;
try {
Sequencer sequencer = MidiSystem.getSequencer();
sequencer.open();
Sequence sequence = new Sequence(Sequence.PPQ, 4);
Track track = sequence.createTrack();
for (int i = 0; i < 12; i++) {
int octave = (int)(Math.random()*(3)+1);
int rowType = (int)(Math.random()*(2));
if (octave == 3) {
if (rowType == 1) {
track.add(makeEvent(144, 1, matrix[row][i] + 50 + 24, 100, i));
} else {
track.add(makeEvent(144, 1, matrix[i][row] + 50 + 24, 100, i));
}
} else if (octave == 2) {
if (rowType == 1) {
track.add(makeEvent(144, 1, matrix[row][i] + 50 + 12, 100, i));
} else {
track.add(makeEvent(144, 1, matrix[i][row] + 50 + 12, 100, i));
}
} else {
if (rowType == 1) {
track.add(makeEvent(144, 1, matrix[row][i] + 50, 100, i));
} else {
track.add(makeEvent(144, 1, matrix[i][row] + 50, 100, i));
}
}
track.add(makeEvent(128, 1, i, 100, i + 4));
}
sequencer.setSequence(sequence);
sequencer.setTempoInBPM(60);
sequencer.start();
while (true) {
if (!sequencer.isRunning()) {
sequencer.close();
return;
}
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public static MidiEvent makeEvent(int command, int channel, int note, int velocity, int tick) {
MidiEvent event = null;
try {
ShortMessage a = new ShortMessage();
a.setMessage(command, channel, note, velocity);
event = new MidiEvent(a, tick);
}
catch (Exception ex) {
ex.printStackTrace();
}
return event;
}
private static int mod(int num) {
if (num > 11) {
num -= 12;
} else if (num < 0) {
num += 12;
}
return num;
}
private static int invert(int num) {
if (num == 0) {
return 0;
} else if (num <= 6) {
return 6 + (6 - num);
} else {
return 6 - (num - 6);
}
}
private static void printMatrix() {
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 12; j++) {
if (matrix[i][j] == 10) {
System.out.print("t ");
continue;
} else if (matrix[i][j] == 11) {
System.out.print("e ");
continue;
}
System.out.print(matrix[i][j] + " ");
}
System.out.println("");
}
}
}