-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBeatBox.java
More file actions
348 lines (307 loc) · 12.6 KB
/
BeatBox.java
File metadata and controls
348 lines (307 loc) · 12.6 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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import java.awt.*;
import javax.swing.*;
import javax.sound.midi.*;
import java.util.*; //for ArrayList really...
import java.awt.event.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.File;
public class BeatBox {
JPanel mainPanel;
ArrayList<JCheckBox> checkboxList;
Sequencer sequencer;
Sequence sequence;
Track track;
JFrame theFrame;
JPanel buttonPanel;
JLabel tempoLabel;
JPanel tempoPanel;
String[] instrumentNames = {"Bass Drum", "Closed Hi-Hat",
"Open Hi-Hat","Acoustic Snare", "Crash Cymbal", "Hand Clap",
"High Tom", "Hi Bongo", "Maracas", "Whistle", "Low Conga",
"Cowbell", "Vibraslap", "Low-mid Tom", "High Agogo",
"Open Hi Conga"};
int[] instruments = {35,42,46,38,49,39,50,60,70,72,64,56,58,47,67,63};
public static void main(String[] args){
new BeatBox().buildGUI();
}
public void buildGUI(){
theFrame = new JFrame("Cyber BeatBox");
theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BorderLayout layout = new BorderLayout();
JPanel background = new JPanel(layout);
background.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
background.setBackground(Color.ORANGE);
checkboxList = new ArrayList<JCheckBox>();
//Box buttonBox = new Box(BoxLayout.Y_AXIS);
GridLayout buttonBox = new GridLayout(8,1);
buttonBox.setVgap(1);
buttonBox.setHgap(2);
buttonPanel = new JPanel(buttonBox);
buttonPanel.setBackground(Color.WHITE);
JButton start = new JButton("Start");
start.setBackground(Color.GREEN.darker());
start.setForeground(Color.WHITE);
start.addActionListener(new MyStartListener());
start.setFont(new Font("Serif", Font.BOLD, 28));
buttonPanel.add(start);
JButton stop = new JButton("Stop");
stop.setBackground(Color.RED);
stop.setForeground(Color.WHITE);
stop.addActionListener(new MyStopListener());
stop.setFont(new Font("Serif", Font.BOLD, 32));
buttonPanel.add(stop);
JButton upTempo = new JButton("Tempo Up");
upTempo.addActionListener(new MyUpTempoListener());
upTempo.setForeground(Color.WHITE);
upTempo.setBackground(Color.BLACK);
upTempo.setFont(new Font("Serif", Font.BOLD, 28));
buttonPanel.add(upTempo);
JButton downTempo = new JButton("Tempo Down");
downTempo.addActionListener(new MyDownTempoListener());
downTempo.setForeground(Color.WHITE);
downTempo.setBackground(Color.BLACK);
downTempo.setFont(new Font("Serif", Font.BOLD, 28));
buttonPanel.add(downTempo);
JButton randomNotes = new JButton("Random Notes");
randomNotes.addActionListener(new MyRandomNotesListener());
randomNotes.setForeground(Color.BLACK);
randomNotes.setBackground(Color.WHITE);
randomNotes.setFont(new Font("Serif", Font.BOLD, 28));
buttonPanel.add(randomNotes);
JButton selectAll = new JButton("Select All");
selectAll.addActionListener(new MySelectAllListener());
selectAll.setForeground(Color.BLACK);
selectAll.setBackground(Color.WHITE);
selectAll.setFont(new Font("Serif", Font.BOLD, 28));
buttonPanel.add(selectAll);
JButton saveBeat = new JButton("Save Beat");
saveBeat.addActionListener(new MySendListener());
saveBeat.setForeground(Color.PINK);
saveBeat.setBackground(Color.magenta);
saveBeat.setFont(new Font("Serif", Font.BOLD, 28));
buttonPanel.add(saveBeat);
JButton loadBeat = new JButton("load Beat");
loadBeat.addActionListener(new MyReadInListener());
loadBeat.setForeground(Color.PINK);
loadBeat.setBackground(Color.magenta);
loadBeat.setFont(new Font("Serif", Font.BOLD, 28));
buttonPanel.add(loadBeat);
Box nameBox = new Box(BoxLayout.Y_AXIS);
for (int i = 0; i < 16; i++){
Label label = new Label(instrumentNames[i]);
label.setFont(new Font("Serif", Font.BOLD, 28));
nameBox.add(label);
}
background.add(BorderLayout.EAST, buttonPanel);
background.add(BorderLayout.WEST, nameBox);
GridLayout grid = new GridLayout(16,16);
grid.setVgap(1);
grid.setHgap(2);
mainPanel = new JPanel(grid);
mainPanel.setBackground(Color.ORANGE.darker());
background.add(BorderLayout.CENTER, mainPanel);
for (int i = 0; i < 256; i++){
JCheckBox c = new JCheckBox();
c.setSelected(false);
c.setAlignmentX(10);
c.setAlignmentY(10);
c.setBackground(Color.YELLOW);
c.setForeground(Color.PINK);
checkboxList.add(c);
mainPanel.add(c);
}
setUpMidi();
JButton reset = new JButton("Reset All");
reset.addActionListener(new MyResetListener());
reset.setFont(new Font("Arial", Font.BOLD, 20));
reset.setBackground(new Color(192, 0, 255)); //Violet
reset.setForeground(Color.WHITE);
GridLayout tempoGrid = new GridLayout(1,3);
tempoGrid.setHgap(20);
float tempoFactor = sequencer.getTempoFactor();
tempoLabel = new JLabel("Current Tempo : " + tempoFactor);
tempoLabel.setFont(new Font("Arial", Font.BOLD, 20));
tempoPanel = new JPanel(tempoGrid);
tempoPanel.add(reset);
tempoPanel.add(tempoLabel);
JLabel creator = new JLabel("Yours Truly, Ayush");
creator.setBackground(Color.WHITE); //Violet
creator.setForeground(new Color(192, 0, 255));
tempoPanel.add(creator);
creator.setFont(new Font("Arial", Font.BOLD, 20));
background.add(BorderLayout.SOUTH, tempoPanel);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = screenSize.getWidth();
double height = screenSize.getHeight();
theFrame.pack();
theFrame.getContentPane().add(background);
//theFrame.setBounds(0, 0, (int)height, (int)width);
theFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
//theFrame.setUndecorated(true);
theFrame.setVisible(true);
//System.out.println(theFrame.getSize());
//theFrame.setLocation(dim.width/2-theFrame.getSize().width/2, 0);
//theFrame.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
}
public void setUpMidi(){
try{
sequencer = MidiSystem.getSequencer();
sequencer.open();
sequence = new Sequence(Sequence.PPQ, 4);
track = sequence.createTrack();
sequencer.setTempoInBPM(120);
} catch (Exception e){e.printStackTrace();}
}
public void buildTrackAndStart(){
int[] trackList = null;
sequence.deleteTrack(track);
track = sequence.createTrack();
for (int i = 0; i < 16; i++){
trackList = new int[16];
int key = instruments[i];
for (int j = 0; j < 16; j++){
JCheckBox jc = (JCheckBox)checkboxList.get(j + (16*i));
if (jc.isSelected()){
trackList[j] = key;
} else {
trackList[j] = 0;
}
}
makeTracks(trackList);
track.add(makeEvent(176,1,127,0,16));
}
track.add(makeEvent(192,9,1,0,15));
try{
sequencer.setSequence(sequence);
sequencer.setLoopCount(sequencer.LOOP_CONTINUOUSLY);
sequencer.start();
sequencer.setTempoInBPM(120);
} catch(Exception e) {e.printStackTrace();}
}
public class MyStartListener implements ActionListener{
public void actionPerformed(ActionEvent a){
buildTrackAndStart();
}
}
public class MyStopListener implements ActionListener{
public void actionPerformed(ActionEvent a){
sequencer.stop();
}
}
public class MyUpTempoListener implements ActionListener{
public void actionPerformed(ActionEvent a){
float tempoFactor = sequencer.getTempoFactor();
sequencer.setTempoFactor((float)(tempoFactor + 0.1));
tempoLabel.setText(String.format("Current Tempo : %.3f",(tempoFactor + 0.1)));
tempoLabel.setFont(new Font("Arial", Font.BOLD, 20));
}
}
public class MyDownTempoListener implements ActionListener{
public void actionPerformed(ActionEvent a){
float tempoFactor = sequencer.getTempoFactor();
sequencer.setTempoFactor((float)(tempoFactor - 0.1));
tempoLabel.setText(String.format("Current Tempo : %.3f",(tempoFactor - 0.1)));
tempoLabel.setFont(new Font("Arial", Font.BOLD, 20));
}
}
public class MyRandomNotesListener implements ActionListener{
public void actionPerformed(ActionEvent a){
for (JCheckBox c: checkboxList)
c.setSelected(false);
for (JCheckBox c: checkboxList)
if (Math.random() > 0.5)
c.setSelected(true);
//buildTrackAndStart();
}
}
public class MySelectAllListener implements ActionListener{
public void actionPerformed(ActionEvent a){
for (JCheckBox c: checkboxList)
c.setSelected(true);
}
}
public class MyResetListener implements ActionListener{
public void actionPerformed(ActionEvent a){
for (JCheckBox c: checkboxList)
c.setSelected(false);
sequencer.setTempoFactor(1.0f);
tempoLabel.setText(String.format("Current Tempo : %.3f",sequencer.getTempoFactor()));
tempoLabel.setFont(new Font("Arial", Font.BOLD, 20));
}
}
public class MySendListener implements ActionListener {
public void actionPerformed(ActionEvent ev) {
JFileChooser fileSave = new JFileChooser();
fileSave.showSaveDialog(theFrame);
saveFile(fileSave.getSelectedFile());
}
}
private void saveFile(File file){
boolean[] checkboxState = new boolean[256];
for(int i = 0; i < 256; i++){
JCheckBox check = (JCheckBox) checkboxList.get(i);
if (check.isSelected()){
checkboxState[i] = true;
}
}
try{
FileOutputStream fileStream = new FileOutputStream(file);
ObjectOutputStream os = new ObjectOutputStream(fileStream);
os.writeObject(checkboxState);
os.writeObject(sequencer.getTempoFactor());
os.close();
} catch(Exception ex){ex.printStackTrace();}
}
public class MyReadInListener implements ActionListener {
public void actionPerformed(ActionEvent ev) {
JFileChooser fileOpen = new JFileChooser();
fileOpen.showOpenDialog(theFrame);
loadFile(fileOpen.getSelectedFile());
}
}
private void loadFile(File file){
boolean[] checkboxState = null;
try{
FileInputStream fileIn = new FileInputStream(file);
ObjectInputStream is = new ObjectInputStream(fileIn);
checkboxState = (boolean[]) is.readObject();
float loadTempo = (float)is.readObject();
sequencer.setTempoFactor(loadTempo);
tempoLabel.setText(String.format("Current Tempo : %.3f",loadTempo));
tempoLabel.setFont(new Font("Arial", Font.BOLD, 20));
is.close();
} catch(Exception ex){ex.printStackTrace();}
for(int i = 0; i < 256; i++){
JCheckBox check = (JCheckBox) checkboxList.get(i);
if (checkboxState[i]){
check.setSelected(true);
}
else{
check.setSelected(false);
}
}
sequencer.stop();
buildTrackAndStart();
}
public void makeTracks(int [] list){
for (int i = 0; i < 16; i++){
int key = list[i];
if (key != 0){
track.add(makeEvent(144,9,key,100,i));
track.add(makeEvent(128,9,key,100,i+1));
}
}
}
public MidiEvent makeEvent(int comd, int chan, int one, int two, int tick){
MidiEvent event = null;
try {
ShortMessage a = new ShortMessage();
a.setMessage(comd, chan, one, two);
event = new MidiEvent(a, tick);
} catch(Exception e) {e.printStackTrace(); }
return event;
}
}