Commit fe236d0f authored by Trisha Gee's avatar Trisha Gee
Browse files

[appA] Bringing in some of the changes from the final Chapter 16 code

parent 88f19a56
Loading
Loading
Loading
Loading
+28 −45
Changes for src/main/java/appA/BeatBoxFinal.java: 28 added lines, 45 removed lines.
Original line number Diff line number Diff line
@@ -2,16 +2,15 @@ package appA;

import javax.sound.midi.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;
import java.util.*;

import static javax.sound.midi.ShortMessage.CONTROL_CHANGE;
import static javax.sound.midi.ShortMessage.PROGRAM_CHANGE;

public class BeatBoxFinal {
  private JFrame theFrame;
@@ -30,8 +29,12 @@ public class BeatBoxFinal {
  private Sequence sequence;
  private Track track;

  private 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"};
  private final int[] instruments = {35, 42, 46, 38, 49, 39, 50, 60, 70, 72, 64, 56, 58, 47, 67, 63};
  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 BeatBoxFinal().startUp(args[0]);  // args[0] is your user ID/screen name
@@ -62,20 +65,21 @@ public class BeatBoxFinal {
    checkboxList = new ArrayList<>();

    Box buttonBox = new Box(BoxLayout.Y_AXIS);

    JButton start = new JButton("Start");
    start.addActionListener(new MyStartListener());
    start.addActionListener(e -> buildTrackAndStart());
    buttonBox.add(start);

    JButton stop = new JButton("Stop");
    stop.addActionListener(new MyStopListener());
    stop.addActionListener(e -> sequencer.stop());
    buttonBox.add(stop);

    JButton upTempo = new JButton("Tempo Up");
    upTempo.addActionListener(new MyUpTempoListener());
    upTempo.addActionListener(e -> changeTempo(1.03f));
    buttonBox.add(upTempo);

    JButton downTempo = new JButton("Tempo Down");
    downTempo.addActionListener(new MyDownTempoListener());
    downTempo.addActionListener(e -> changeTempo(0.97f));
    buttonBox.add(downTempo);

    JButton sendIt = new JButton("sendIt");
@@ -120,7 +124,7 @@ public class BeatBoxFinal {
    theFrame.setVisible(true);
  }

  public void setUpMidi() {
  private void setUpMidi() {
    try {
      sequencer = MidiSystem.getSequencer();
      sequencer.open();
@@ -136,73 +140,52 @@ public class BeatBoxFinal {
    ArrayList<Integer> trackList; // this will hold the instruments for each
    sequence.deleteTrack(track);
    track = sequence.createTrack();

    for (int i = 0; i < 16; i++) {
      trackList = new ArrayList<>();
      int key = instruments[i];
      for (int j = 0; j < 16; j++) {
        JCheckBox jc = checkboxList.get(j + (16 * i));
        if (jc.isSelected()) {
          int key = instruments[i];
          trackList.add(key);
        } else {
          trackList.add(null);  // because this slot should be empty in the track
        }
      }
      makeTracks(trackList);
      track.add(makeEvent(CONTROL_CHANGE, 1, 127, 0, 16));
    }
    track.add(makeEvent(192, 9, 1, 0, 15)); // - so we always go to full 16 beats
    track.add(makeEvent(PROGRAM_CHANGE, 9, 1, 0, 15)); // - so we always go to full 16 beats
    try {
      sequencer.setSequence(sequence);
      sequencer.setLoopCount(sequencer.LOOP_CONTINUOUSLY);
      sequencer.start();
      sequencer.setTempoInBPM(120);
      sequencer.start();
    } 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) {
  private void changeTempo(float tempoMultiplier) {
    float tempoFactor = sequencer.getTempoFactor();
      sequencer.setTempoFactor((float) (tempoFactor * 1.03));
    }
  }

  public class MyDownTempoListener implements ActionListener {
    public void actionPerformed(ActionEvent a) {
      float tempoFactor = sequencer.getTempoFactor();
      sequencer.setTempoFactor((float) (tempoFactor * .97));
    }
    sequencer.setTempoFactor(tempoFactor * tempoMultiplier);
  }

  public class MySendListener implements ActionListener {
    public void actionPerformed(ActionEvent a) {
      // make an arraylist of just the STATE of the checkboxes
      // make an array of just the STATE of the checkboxes
      boolean[] checkboxState = new boolean[256];
      for (int i = 0; i < 256; i++) {
        JCheckBox check = (JCheckBox) checkboxList.get(i);
        JCheckBox check = checkboxList.get(i);
        if (check.isSelected()) {
          checkboxState[i] = true;
        }
      }
      String messageToSend = null;
      try {
        out.writeObject(userName + nextNum++ + ": " + userMessage.getText());
        out.writeObject(checkboxState);
      } catch (Exception ex) {
        System.out.println("Sorry dude. Could not send it to the server.");
      } catch (IOException e) {
        System.out.println("Terribly sorry. Could not send it to the server.");
        e.printStackTrace();
      }
      userMessage.setText("");
    }