Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package BeatBox;
- public class BeatBox {
- GUI gui;
- Midi midi;
- public BeatBox(){
- midi = new Midi();
- gui = new GUI(midi);
- midi.setUpMidi();
- }
- public static void main(String[] args) {
- new BeatBox();
- }
- }
- package BeatBox;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.*;
- public class GUI {
- private Midi midi;
- public GUI(Midi midi){
- this.midi = midi;
- buildGUI();
- }
- // buildGUI - creates the GUI for the beat box program
- void buildGUI(){
- JFrame frame = new JFrame("Cyber BeatBox");
- frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- frame.setMinimumSize(new Dimension(600, 350));
- JPanel panel = new JPanel(new BorderLayout());
- panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
- frame.setContentPane(panel);
- // Instrument names
- Box nameBox = new Box(BoxLayout.Y_AXIS);
- for(int i = 0; i < BeatBoxConstants.NUM_INSTRUMENTS; i++){
- nameBox.add(new Label(BeatBoxConstants.instrumentNames[i]));
- }
- // Check box
- GridLayout grid = new GridLayout(16, 16);
- grid.setVgap(0);
- grid.setHgap(2);
- JPanel checkBoxPanel = new JPanel(grid);
- for(int i = 0; i < BeatBoxConstants.NUM_INSTRUMENTS; i++){
- for(int j = 0; j < BeatBoxConstants.NUM_BEATS; j++) {
- JCheckBox checkBox = new JCheckBox();
- checkBoxPanel.add(checkBox);
- midi.checkBoxArray[i][j] = checkBox;
- }
- }
- // Buttons
- JPanel buttonsPanel = new JPanel();
- buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.Y_AXIS));
- JButton startButton = new JButton("Start");
- JButton pauseButton = new JButton("Pause");
- startButton.addActionListener(new StartButtonListener());
- pauseButton.addActionListener(new GUI.PauseButtonListener());
- buttonsPanel.add(startButton);
- buttonsPanel.add(pauseButton);
- // Add everything to the frame
- frame.add(BorderLayout.WEST, nameBox);
- frame.add(BorderLayout.CENTER, checkBoxPanel);
- frame.add(BorderLayout.EAST, buttonsPanel);
- frame.pack();
- frame.setLocationRelativeTo(null);
- frame.setVisible(true);
- }
- // LISTENERS
- private class StartButtonListener implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent ev){
- midi.buildTrackAndStart();
- midi.startSequencer();
- }
- }
- private class PauseButtonListener implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent ev){
- midi.pauseSequencer();
- }
- }
- }
- package BeatBox;
- import javax.sound.midi.*;
- import javax.swing.JCheckBox;
- public class Midi {
- private Sequencer sequencer;
- private Sequence seq;
- private Track track;
- JCheckBox[][] checkBoxArray = new JCheckBox[BeatBoxConstants.NUM_INSTRUMENTS][BeatBoxConstants.NUM_BEATS];
- // buildTrackAndStart - used to create and begin playing the newly created track
- void buildTrackAndStart(){
- try {
- seq.deleteTrack(track);
- track = seq.createTrack();
- makeTrack();
- sequencer.setSequence(seq);
- sequencer.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);
- }catch(InvalidMidiDataException invalidMidiDataException){
- invalidMidiDataException.printStackTrace();
- }
- }
- // makeBeat - this creates the noteOn and noteOff beats
- private void makeBeat(int key, int tick) throws InvalidMidiDataException{
- track.add(makeEvent(144, 9, key, 100, tick));
- track.add(makeEvent(128, 9, key, 100, tick + 2));
- }
- // makeEvent - creates the notes for the track
- private MidiEvent makeEvent(int comd, int chan, int one, int two, int tick) throws InvalidMidiDataException{
- ShortMessage a = new ShortMessage();
- a.setMessage(comd, chan, one, two);
- return new MidiEvent(a, tick);
- }
- // makeTrack - creates the sound track based on the check boxes selected by the user
- private void makeTrack() throws InvalidMidiDataException{
- for(int i = 0; i < BeatBoxConstants.NUM_INSTRUMENTS; i++){
- for(int j = 0; j < BeatBoxConstants.NUM_BEATS; j++){
- if(checkBoxArray[i][j].isSelected()){
- makeBeat(BeatBoxConstants.instruments[i], j);
- }
- }
- }
- // Added to ensure that beat box completes the whole 16 beats before it loops again
- track.add(makeEvent(192, 9, 1, 0, BeatBoxConstants.NUM_BEATS - 1));
- }
- // setUpMidi - this sets up the sound system for the user to edit
- void setUpMidi(){
- try {
- sequencer = MidiSystem.getSequencer();
- sequencer.open();
- seq = new Sequence(Sequence.PPQ, 4);
- track = seq.createTrack();
- }catch(InvalidMidiDataException invalidMidiDataException){
- invalidMidiDataException.printStackTrace();
- }catch(MidiUnavailableException midiUnavailableException){
- midiUnavailableException.printStackTrace();
- }
- }
- // startSequencer - starts playing the sequencer
- void startSequencer(){
- sequencer.start();
- }
- // pauseSequencer - pauses the sequencer. This does not stop the sound, rather it pauses it.
- void pauseSequencer(){
- sequencer.stop();
- }
- }
- package BeatBox;
- public class BeatBoxConstants {
- static final 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"};
- static final int[] instruments = {35, 42, 46, 38, 49, 39, 50, 60, 70, 72, 64, 56, 58, 47, 67, 63};
- static final int NUM_BEATS = 16;
- static final int NUM_INSTRUMENTS = instrumentNames.length;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement