-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConductor.java
More file actions
235 lines (206 loc) · 6.72 KB
/
Conductor.java
File metadata and controls
235 lines (206 loc) · 6.72 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package bells;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
public class Conductor
{
private final AudioFormat af;
Member[] members;
Scanner readLine;
Scanner readNote;
int numMembers = 7;
int SeqNum;
int[][] Member_Bell_Length = new int[50][3];
ArrayList<Note> Notes = new ArrayList<Note>(
Arrays.asList(Note.REST,
Note.A4,
Note.A4S,
Note.B4,
Note.C4,
Note.C4S,
Note.D4,
Note.D4S,
Note.E4,
Note.F4,
Note.F4S,
Note.G4,
Note.G4S,
Note.A5));
ArrayList<NoteLength> NoteLengths = new ArrayList<NoteLength>(
Arrays.asList(NoteLength.WHOLE,
NoteLength.HALF,
NoteLength.QUARTER,
NoteLength.WHOLE));
Conductor(AudioFormat af) throws FileNotFoundException, LineUnavailableException
{
this.af = af;
readSong("MaryHadALittleLamb");
members = new Member[numMembers];
for (int currentNum = 0; currentNum < numMembers; currentNum++)
{
System.out.println("Creating Member:" + currentNum + " with notes "
+ Notes.get(currentNum*2) + " and " + Notes.get(currentNum*2+1));
Member member = new Member(currentNum,Notes.get(currentNum*2), Notes.get(currentNum*2+1));
members[currentNum] = member;
}
System.out.println("\n");
}
public static void main(String[] args) throws FileNotFoundException, LineUnavailableException
{
final AudioFormat af =
new AudioFormat(Note.SAMPLE_RATE, 8, 1, true, false);
Conductor c = new Conductor(af);
c.playSong();
}
void playSong() throws LineUnavailableException {
try (final SourceDataLine line = AudioSystem.getSourceDataLine(af)) {
line.open();
line.start();
boolean free = true;
//Make members play
for(int seq = 0; seq <= SeqNum; seq++)
{
members[Member_Bell_Length[seq][0]].play(line,Member_Bell_Length[seq][1], NoteLengths.get(Member_Bell_Length[seq][2]));
members[Member_Bell_Length[seq][0]].occupy();
while(free)
{
free = !members[Member_Bell_Length[seq][0]].getOccupied();
}
}
line.drain();
}
}
enum Note {
// REST Must be the first 'Note'
REST,
A4,
A4S,
B4,
C4,
C4S,
D4,
D4S,
E4,
F4,
F4S,
G4,
G4S,
A5;
public static final int SAMPLE_RATE = 48 * 1024; // ~48KHz
public static final int MEASURE_LENGTH_SEC = 1;
// Circumference of a circle divided by # of samples
private static final double step_alpha = (2.0 * Math.PI) / SAMPLE_RATE;
private final double FREQUENCY_A_HZ = 440.0;
private final double MAX_VOLUME = 127.0;
private final byte[] sinSample = new byte[MEASURE_LENGTH_SEC * SAMPLE_RATE];
private Note() {
int n = this.ordinal();
if (n > 0) {
// Calculate the frequency!
final double halfStepUpFromA = n - 1;
final double exp = halfStepUpFromA / 12.0;
final double freq = FREQUENCY_A_HZ * Math.pow(2.0, exp);
// Create sinusoidal data sample for the desired frequency
final double sinStep = freq * step_alpha;
for (int i = 0; i < sinSample.length; i++) {
sinSample[i] = (byte)(Math.sin(i * sinStep) * MAX_VOLUME);
}
}
}
public byte[] sample() {
return sinSample;
}
}
enum NoteLength {
WHOLE(1.0f),
HALF(0.5f),
QUARTER(0.25f),
EIGHTH(0.125f);
private final int timeMs;
private NoteLength(float length) {
timeMs = (int)(length * Note.MEASURE_LENGTH_SEC * 1000);
}
public int timeMs() {
return timeMs;
}
}
public void readSong(String fileName) throws FileNotFoundException
{
SeqNum = 0;
readLine = new Scanner (new File(fileName));
readLine.useDelimiter(";");
Note note;
String songName = readLine.next();
System.out.println("Reading in the song " + songName + "\n");
readLine.nextLine();
while (readLine.hasNext())
{
String noteString = readLine.next();
switch (noteString)
{
case "Rest": note = Note.REST;
break;
case "A4": note = Note.A4;
break;
case "A4S": note = Note.A4S;
break;
case "B4": note = Note.B4;
break;
case "C4": note = Note.C4;
break;
case "C4S": note = Note.C4S;
break;
case "D4": note = Note.D4;
break;
case "D4S": note = Note.D4S;
break;
case "E4": note = Note.E4;
break;
case "F4": note = Note.F4;
break;
case "F4S": note = Note.F4S;
break;
case "G4": note = Note.G4;
break;
case "G4S": note = Note.G4S;
break;
case "A5": note = Note.A5;
break;
default: note = Note.REST;
break;
}
Member_Bell_Length[SeqNum][0] = Notes.indexOf(note)/2;
Member_Bell_Length[SeqNum][1] = Notes.indexOf(note)%2;
String lengthString = readLine.next();
int lengthNum;
switch (lengthString) {
case "WHOLE": lengthNum = 0;
break;
case "HALF": lengthNum = 1;
break;
case "QUARTER": lengthNum = 2;
break;
case "EIGTH": lengthNum = 3;
break;
default: lengthNum = 0;
break;
}
Member_Bell_Length[SeqNum][2] = lengthNum;
System.out.println("Reading in Bellnote Number" + SeqNum + " is " + note + " which has an index of " +
Notes.indexOf(note) + " and so player number " + Member_Bell_Length[SeqNum][0] +
" will play it from hand number " + Member_Bell_Length[SeqNum][1] + " for a " +
lengthString + " note.");
SeqNum++;
readLine.nextLine();
}
System.out.println("\n");
readLine.close();
}
}