-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToneMatrix.java
287 lines (271 loc) · 7.01 KB
/
ToneMatrix.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/**
* Tone Matrix By David Ruan.
*/
import java.io.File;
import java.awt.event.KeyEvent;
public class ToneMatrix {
/**
* Declares variables the ToneMatrix will use
*/
private ToneMatrixModel model;
private int mousex;
private int mousey;
private boolean click;
private int instrument;
public int bpm;
private Stopwatch timer;
private int time;
private int col;
private TonePlayer[] saws;
private TonePlayer[] pianos;
private TonePlayer[] plucks;
public static void main(String[] args) {
/**
* Sets the screen size and runs the program
*/
StdDraw.setCanvasSize(640, 640);
new ToneMatrix().run();
}
public void run() {
/**
* Sets the default values for variables the program needs to function.
* Creates a new ToneMatrixModel that we will be using for our matrix.
* Initializes the timer that will be used to calculate BPM Creates a
* TonePlayer array for each instrument and fills it with a TonePlayer
* for each note.
*/
instrument = 1;
click = false;
bpm = 120;
time = 15000 / bpm;
col = 0;
model = new ToneMatrixModel();
timer = new Stopwatch();
saws = new TonePlayer[16];
/**
* Creates TonePlayer for each sound
*/
for (int i = 0; i < 16; i++) {
saws[i] = new TonePlayer(new File("source" + java.io.File.separator
+ "saw - Marker #" + i + ".wav"));
}
pianos = new TonePlayer[16];
for (int i = 0; i < 16; i++) {
pianos[i] = new TonePlayer(new File("source"
+ java.io.File.separator + "piano - Marker #" + i + ".wav"));
}
plucks = new TonePlayer[16];
for (int i = 0; i < 16; i++) {
plucks[i] = new TonePlayer(new File("source"
+ java.io.File.separator + "pluck - Marker #" + i + ".wav"));
}
/**
* Draws the screen and then runs the main program in a while loop.
*/
draw();
drawInfo();
while (true) {
checkMouse();
checkKey();
if ((timer.elapsedTime() > (time))) {
time += (15000 / bpm);
cycleArray();
}
}
}
/**
* This method checks to see if certain keys are pressed. This is needed to
* change instrument as well as bring up the load/save window.
*/
public void checkKey() {
if (StdDraw.isKeyPressed(KeyEvent.VK_1)) {
instrument = 1;
}
if (StdDraw.isKeyPressed(KeyEvent.VK_2)) {
instrument = 2;
}
if (StdDraw.isKeyPressed(KeyEvent.VK_3)) {
instrument = 3;
}
}
/**
* this method makes sure clicks work correctly by calling mouseClick() and
* mouseRelease()
*/
public void checkMouse() {
if (!click) {
mouseClick();
}
if (click) {
mouseRelease();
StdDraw.show(0);
}
}
public void mouseClick() {
if (StdDraw.mousePressed()) {
click = true;
}
}
public void mouseRelease() {
if (!StdDraw.mousePressed()) {
click = false;
clickSquare();
}
}
public void clickSquare() {
/**
* Detects which square the user has clicked and then toggles the value
* of the array.
*/
double mx = StdDraw.mouseX();
double my = StdDraw.mouseY();
if (mx > .1 && mx < .9 && my > .1 && my < .9) {
mousex = 0;
mousey = 0;
double c = .1;
while (mx > c) {
if (mx > c) {
mousex += 1;
c += .05;
} else {
break;
}
}
c = .1;
while (my > c) {
if (my > c) {
mousey += 1;
c += .05;
} else {
break;
}
}
mousex = mousex - 1;
mousey = mousey - 1;
/**
* Draws appropriate color square in correct spot
*/
if (mousex >= 0 && mousex < 16 && mousey >= 0 && mousey < 16) {
toggle(mousex, mousey);
int color = model.getColor(mousex, mousey);
if (color == 0) {
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.filledSquare(.125 + (mousex * .05),
.125 + (mousey * .05), .024);
StdDraw.setPenColor(StdDraw.GRAY);
StdDraw.square(.125 + (mousex * .05),
.125 + (mousey * .05), .024);
}
if (color == 1) {
StdDraw.picture(.125 + (mousex*.05), .125 + (mousey * .05), "source" + java.io.File.separator + "Grey.jpg");
}
if (color == 2) {
StdDraw.picture(.125 + (mousex*.05), .125 + (mousey * .05), "source" + java.io.File.separator + "Yellow.jpg");
}
if (color == 3) {
StdDraw.picture(.125 + (mousex*.05), .125 + (mousey * .05), "source" + java.io.File.separator + "Blue.jpg");
}
}
}
if (mx > .875 && mx < .925 && my > 0 && my < .05){
if (bpm<160){
bpm++;
drawInfo();
}
}
if (mx > .075 && mx < .125 && my > 0 && my < .05){
if (bpm>80){
bpm--;
drawInfo();
}
}
}
/**
* Toggles the value of the ToneMatrixModel at index [x][y]
*/
public void toggle(int x, int y) {
if (model.getOn(x, y)) {
model.setColor(x, y, 0);
} else {
model.setColor(x, y, instrument);
}
}
/**
* Draws the UI and instructions
*/
public void draw() {
StdDraw.clear();
StdDraw.picture(.5, .5, "source" + java.io.File.separator + "ToneMatrixBG.jpg");
StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.text(.5, 1, "Tone Matrix by David Ruan");
StdDraw.text(.5, .95, "Click squares to toggle notes");
StdDraw.text(.5, .05, "Press 1 for saw, 2 for piano, 3 for pluck");
StdDraw.picture(.9, .025, "source" + java.io.File.separator + "Right.jpg");
StdDraw.picture(.1, .025, "source" + java.io.File.separator + "Left.jpg");
}
/**
* Draws the current value of BPM
*/
public void drawInfo() {
StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.filledRectangle(.5, .01, .1, .02);
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.text(.5, 0.01, "BPM: "+bpm);
}
/**
* No longer used. Just use default image that is drawn at start
* Would be necessary if I was to include the load function
*/
// public void drawSquares() {
// int color;
// for (int x = 0; x < 16; x++) {
// for (int y = 0; y < 16; y++) {
// color = model.getColor(x, y);
// if (color == 0) {
// StdDraw.setPenColor(StdDraw.BLACK);
// }
// if (color == 1) {
// StdDraw.setPenColor(StdDraw.WHITE);
// }
// if (color == 2) {
// StdDraw.setPenColor(StdDraw.YELLOW);
// }
// if (color == 2) {
// StdDraw.setPenColor(StdDraw.BLUE);
// }
// StdDraw.filledSquare(.125 + (x * .05), .125 + (y * .05), .0245);
// StdDraw.setPenColor(StdDraw.WHITE);
// }
// }
// }
public void cycleArray() {
for (int y = 0; y < 16; y++) {
if (model.getOn(col, y)) {
int c = model.getColor(col, y);
play(c, y);
}
}
if (col < 15) {
col++;
} else {
col = 0;
}
}
public void play(int i, int y) {
/**
* Starts a new TonePlayer thread from the appropriate TonePlayer array
* This use of multithreading is what allows this program to work The
* StdAudio library was far too inefficient to use as it led to constant
* freezes rendering the program unusable.
*/
if (i == 1) {
new Thread(saws[y]).start();
}
if (i == 2) {
new Thread(pianos[y]).start();
}
if (i == 3) {
new Thread(plucks[y]).start();
}
}
}