-
Notifications
You must be signed in to change notification settings - Fork 0
/
TuneWriter.java
124 lines (100 loc) · 3.23 KB
/
TuneWriter.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
import java.util.*;
public class TuneWriter{
private String key = "c";
private int totalLines =3;
private int totalBars = 4;//per line
private int totalNotes = 8;//per bar
private ArrayList<Note> notes= new ArrayList<Note>();
private String style;
public TuneWriter(String s){
style=s;
}
public String makeTune(){
String fullTune="";
if(style.equals("random")){
fullTune = "X:1 \nT:Random \nM:4/4 \nC:Runner.java \nK:"+key+"\n";
notes.clear();
for(int line = 0;line<totalLines;line++){
for(int bar = 0; bar<totalBars;bar++){
int ctr = 1;
while(ctr<=totalNotes){//number of eigth notes in bar
if(ctr==5) fullTune += " ";
notes.add(makeNote(ctr));
//System.out.println(notes.get(notes.size()-1).getValue()+" "+notes.get(notes.size()-1).howLong());
fullTune += notes.get(notes.size()-1).getValue();
ctr+=notes.get(notes.size()-1).howLong();
}
fullTune += "|";
}
if(line==totalLines-1) fullTune+="|";
fullTune += ("\n");
}
}
if(style.equals("matrix")){
//generate two tone rows
//play first starting on each note of second
fullTune = "X:1 \nT:Random \nM:4/4 \nC:Runner.java \nK:"+key+"\n";
notes.clear();
for(int line = 0;line<totalLines;line++){
for(int bar = 0; bar<totalBars;bar++){
int ctr = 1;
while(ctr<=totalNotes){//number of eigth notes in bar
if(ctr==5) fullTune += " ";
notes.add(makeNote(ctr));
//System.out.println(notes.get(notes.size()-1).getValue()+" "+notes.get(notes.size()-1).howLong());
fullTune += notes.get(notes.size()-1).getValue();
ctr+=notes.get(notes.size()-1).howLong();
}
fullTune += "|";
}
if(line==totalLines-1) fullTune+="|";
fullTune += ("\n");
}
}
else{
System.out.println("Usage: java Runner [style]");
System.exit(0);
}
return fullTune;
}
public Note makeNote(int ctr){ //passed in current place in bar
//this is where the algorithm goes
//first+last note of the piece is tonic
if(notes.size()==0)return new Note(key+"2");
if(this.totalDuration()>=totalLines*totalBars*(totalNotes)-2) return new Note(key);
if(Math.random()>.6&&(ctr==3||ctr==7)&&this.totalDuration()<=totalLines*totalBars*(totalNotes-1)) return leap(2);
if(Math.random()>.6&&ctr!=4&&ctr!=8) return step(2);
else return step(1);
}
public Note leap(int dur){//passed in duration
Note ret = notes.get(notes.size()-1);
//System.out.print(ret.getValue()+" ");
if(Math.random()<.5){
for (int i=0;i<3;i++){ ret.stepUp();}
ret.changeDuration(dur);
//System.out.println(ret.getValue());
return ret;}
else {
for (int i=0;i<3;i++){ ret.stepDown();}
ret.changeDuration(dur);
//System.out.println(ret.getValue());
return ret;}
}
public Note step(int dur){//passed in duration
if(Math.random()<.5||(notes.size()>2&&(notes.get(notes.size()-2).getiValue()-notes.get(notes.size()-1).getiValue())<-2)){
Note ret = new Note(notes.get(notes.size()-1).getValue());
ret.stepUp();
ret.changeDuration(dur);
return ret;}
else {
Note ret = new Note(notes.get(notes.size()-1).getValue());
ret.stepDown();
ret.changeDuration(dur);
return ret;}
}
public double totalDuration(){
double i = 0;
for(Note n:notes) i+=n.howLong();
return i;
}
}