-
Notifications
You must be signed in to change notification settings - Fork 0
/
MusicMarkov.java
176 lines (135 loc) · 3.96 KB
/
MusicMarkov.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
//-------------------------------------------------------------------------
// MusicMarkov.java
//
// Takes input of parsed midi input and arranges into markov chains that represent
// a probabilistic copy of the music of the given input.
//
// Alexander Lill and Tyler Batistic
// HackACM 2019
// 4/13/19
//-------------------------------------------------------------------------
import java.util.*;
import java.io.*;
class MusicMarkov {
// Fields
double[][] noteMark;
double[][] lengthMark;
Map<Integer, String> translateNote; // Maps numbers to notes
Map<Integer, String> translateRhythm; // Maps numbers to rhythms
MusicMarkov() {
noteMark = new double[13][13];
lengthMark = new double[8][8];
translateNote = new HashMap<Integer, String>();
translateRhythm = new HashMap<Integer, String>();
for(int i = 0; i < lengthMark.length; i++) {
for(int j = 0; j < lengthMark[1].length; j++) {
lengthMark[i][j] = 0;
}
}
for(int i = 0; i < noteMark.length; i++) {
for(int j = 0; j < noteMark[1].length; j++) {
noteMark[i][j] = 0;
}
}
translateNote.put(0, "C");
translateNote.put(1, "C#");
translateNote.put(2, "D");
translateNote.put(3, "D#");
translateNote.put(4, "E");
translateNote.put(5, "F");
translateNote.put(6, "F#");
translateNote.put(7, "G");
translateNote.put(8, "G#");
translateNote.put(9, "A");
translateNote.put(10, "A#");
translateNote.put(11, "B");
translateNote.put(12, "R");
translateRhythm.put(0, "half");
translateRhythm.put(1, "quarter");
translateRhythm.put(2, "8th");
translateRhythm.put(3, "16th");
translateRhythm.put(4, "triplet quarter");
translateRhythm.put(5, "triplet 8th");
translateRhythm.put(6, "triplet 16th");
translateRhythm.put(7, "triplet 8th again");
}
// Translates midi note number to text
String translateNote(int num) {
return translateNote.get(num % 12);
}
// Translates rhythm number to text
String translateRhythm(int num) {
return translateRhythm.get(num);
}
// Records transition from note1 to note2 in noteMark
void putNote(int note1, int note2) {
noteMark[note1][note2] += 1;
}
// Records transition from rhythm1 to rhythm2 in lengthMark
void putRhythm(int rhythm1, int rhythm2) {
lengthMark[rhythm1][rhythm2] += 1;
}
// Normalizes each column based on weight
void calcAverage(double[][] markov) {
int temp = 0;
for(int i = 0; i < markov.length; i++) {
for(int j = 0; j < markov[0].length; j++) { // Calculates column weight
temp += markov[i][j];
}
for(int j = 0; j < markov[0].length; j++) { // Divides each column by total weight
markov[i][j] /= temp;
}
temp = 0;
}
}
// Exports markov chains to a csv file for transfer and use
void exportMarkov() throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter("markovData"));
for(int i = 0; i < noteMark.length; i++) {
for(int j = 0; j < noteMark[0].length; j++) { // Adds each decimal
writer.write(noteMark[i][j] + ",");
}
writer.write("\n");
}
writer.write("\n");
for(int i = 0; i < lengthMark.length; i++) {
for(int j = 0; j < lengthMark[1].length; j++) {
writer.write(lengthMark[i][j] + ",");
}
writer.write("\n");
}
writer.close();
}
int octaveHelper(int numPrev, int numNext) {
if((numNext % 12) == 0) {
return 12;
}
if((Math.abs(numPrev - numNext) % 12) > 5) {
if(numNext - 6 < 66) {
return numNext;
}
else {
return numNext - 6;
}
}
else {
if(numNext > 88) {
return numNext - 12;
}
}
return numNext;
}
public static void main(String[] args) throws IOException {
MusicMarkov test = new MusicMarkov();
Random a = new Random();
for(int i = 0; i < 2000; i++) {
test.putNote(Math.abs(a.nextInt() % 13), Math.abs(a.nextInt() % 13));
}
for(int i = 0; i < 2000; i++) {
test.putRhythm(Math.abs(a.nextInt() % 7), Math.abs(a.nextInt() % 7));
}
test.calcAverage(test.noteMark);
test.calcAverage(test.lengthMark);
test.exportMarkov();
}
}