Advertisement
Guest User

Untitled

a guest
Mar 6th, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.01 KB | None | 0 0
  1. package BeatBox;
  2.  
  3.  
  4. public class BeatBox {
  5. GUI gui;
  6. Midi midi;
  7.  
  8. public BeatBox(){
  9. midi = new Midi();
  10. gui = new GUI(midi);
  11. midi.setUpMidi();
  12. }
  13.  
  14. public static void main(String[] args) {
  15. new BeatBox();
  16. }
  17. }
  18.  
  19. package BeatBox;
  20.  
  21. import java.awt.*;
  22. import java.awt.event.ActionEvent;
  23. import java.awt.event.ActionListener;
  24. import javax.swing.*;
  25.  
  26. public class GUI {
  27. private Midi midi;
  28.  
  29. public GUI(Midi midi){
  30. this.midi = midi;
  31. buildGUI();
  32. }
  33.  
  34. // buildGUI - creates the GUI for the beat box program
  35. void buildGUI(){
  36. JFrame frame = new JFrame("Cyber BeatBox");
  37. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  38. frame.setMinimumSize(new Dimension(600, 350));
  39. JPanel panel = new JPanel(new BorderLayout());
  40. panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
  41. frame.setContentPane(panel);
  42.  
  43. // Instrument names
  44. Box nameBox = new Box(BoxLayout.Y_AXIS);
  45. for(int i = 0; i < BeatBoxConstants.NUM_INSTRUMENTS; i++){
  46. nameBox.add(new Label(BeatBoxConstants.instrumentNames[i]));
  47. }
  48.  
  49. // Check box
  50. GridLayout grid = new GridLayout(16, 16);
  51. grid.setVgap(0);
  52. grid.setHgap(2);
  53. JPanel checkBoxPanel = new JPanel(grid);
  54. for(int i = 0; i < BeatBoxConstants.NUM_INSTRUMENTS; i++){
  55. for(int j = 0; j < BeatBoxConstants.NUM_BEATS; j++) {
  56. JCheckBox checkBox = new JCheckBox();
  57. checkBoxPanel.add(checkBox);
  58. midi.checkBoxArray[i][j] = checkBox;
  59. }
  60. }
  61.  
  62. // Buttons
  63. JPanel buttonsPanel = new JPanel();
  64. buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.Y_AXIS));
  65. JButton startButton = new JButton("Start");
  66. JButton pauseButton = new JButton("Pause");
  67.  
  68. startButton.addActionListener(new StartButtonListener());
  69. pauseButton.addActionListener(new GUI.PauseButtonListener());
  70.  
  71. buttonsPanel.add(startButton);
  72. buttonsPanel.add(pauseButton);
  73.  
  74. // Add everything to the frame
  75. frame.add(BorderLayout.WEST, nameBox);
  76. frame.add(BorderLayout.CENTER, checkBoxPanel);
  77. frame.add(BorderLayout.EAST, buttonsPanel);
  78. frame.pack();
  79. frame.setLocationRelativeTo(null);
  80. frame.setVisible(true);
  81. }
  82.  
  83. // LISTENERS
  84. private class StartButtonListener implements ActionListener{
  85. @Override
  86. public void actionPerformed(ActionEvent ev){
  87. midi.buildTrackAndStart();
  88. midi.startSequencer();
  89. }
  90. }
  91.  
  92. private class PauseButtonListener implements ActionListener{
  93. @Override
  94. public void actionPerformed(ActionEvent ev){
  95. midi.pauseSequencer();
  96. }
  97. }
  98. }
  99.  
  100. package BeatBox;
  101.  
  102. import javax.sound.midi.*;
  103. import javax.swing.JCheckBox;
  104.  
  105. public class Midi {
  106. private Sequencer sequencer;
  107. private Sequence seq;
  108. private Track track;
  109. JCheckBox[][] checkBoxArray = new JCheckBox[BeatBoxConstants.NUM_INSTRUMENTS][BeatBoxConstants.NUM_BEATS];
  110.  
  111. // buildTrackAndStart - used to create and begin playing the newly created track
  112. void buildTrackAndStart(){
  113. try {
  114. seq.deleteTrack(track);
  115. track = seq.createTrack();
  116. makeTrack();
  117. sequencer.setSequence(seq);
  118. sequencer.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);
  119. }catch(InvalidMidiDataException invalidMidiDataException){
  120. invalidMidiDataException.printStackTrace();
  121. }
  122. }
  123.  
  124. // makeBeat - this creates the noteOn and noteOff beats
  125. private void makeBeat(int key, int tick) throws InvalidMidiDataException{
  126. track.add(makeEvent(144, 9, key, 100, tick));
  127. track.add(makeEvent(128, 9, key, 100, tick + 2));
  128. }
  129.  
  130. // makeEvent - creates the notes for the track
  131. private MidiEvent makeEvent(int comd, int chan, int one, int two, int tick) throws InvalidMidiDataException{
  132. ShortMessage a = new ShortMessage();
  133. a.setMessage(comd, chan, one, two);
  134. return new MidiEvent(a, tick);
  135. }
  136.  
  137. // makeTrack - creates the sound track based on the check boxes selected by the user
  138. private void makeTrack() throws InvalidMidiDataException{
  139. for(int i = 0; i < BeatBoxConstants.NUM_INSTRUMENTS; i++){
  140. for(int j = 0; j < BeatBoxConstants.NUM_BEATS; j++){
  141. if(checkBoxArray[i][j].isSelected()){
  142. makeBeat(BeatBoxConstants.instruments[i], j);
  143. }
  144. }
  145. }
  146. // Added to ensure that beat box completes the whole 16 beats before it loops again
  147. track.add(makeEvent(192, 9, 1, 0, BeatBoxConstants.NUM_BEATS - 1));
  148. }
  149.  
  150. // setUpMidi - this sets up the sound system for the user to edit
  151. void setUpMidi(){
  152. try {
  153. sequencer = MidiSystem.getSequencer();
  154. sequencer.open();
  155. seq = new Sequence(Sequence.PPQ, 4);
  156. track = seq.createTrack();
  157. }catch(InvalidMidiDataException invalidMidiDataException){
  158. invalidMidiDataException.printStackTrace();
  159. }catch(MidiUnavailableException midiUnavailableException){
  160. midiUnavailableException.printStackTrace();
  161. }
  162. }
  163.  
  164. // startSequencer - starts playing the sequencer
  165. void startSequencer(){
  166. sequencer.start();
  167. }
  168.  
  169. // pauseSequencer - pauses the sequencer. This does not stop the sound, rather it pauses it.
  170. void pauseSequencer(){
  171. sequencer.stop();
  172. }
  173. }
  174.  
  175. package BeatBox;
  176.  
  177. public class BeatBoxConstants {
  178. static final String[] instrumentNames = {"Bass Drum", "Closed Hi-Hat", "Open Hi-Hat", "Acoustic Snare", "Crash Cymbal",
  179. "Hand Clap", "High Tom", "Hi Bongo", "Maracas", "Whistle", "Low Conga", "Cowbell", "Vibraslap", "Low-mid Tom",
  180. "High Agogo", "Open Hi Conga"};
  181. static final int[] instruments = {35, 42, 46, 38, 49, 39, 50, 60, 70, 72, 64, 56, 58, 47, 67, 63};
  182.  
  183. static final int NUM_BEATS = 16;
  184. static final int NUM_INSTRUMENTS = instrumentNames.length;
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement