Skip to content

Commit

Permalink
updated BeatModel.java to fix midi/synth bug that was causing the bea…
Browse files Browse the repository at this point in the history
…t to not work well
  • Loading branch information
bethrobson committed Sep 16, 2020
1 parent 8e62df0 commit 774a3d5
Showing 1 changed file with 21 additions and 50 deletions.
71 changes: 21 additions & 50 deletions src/headfirst/designpatterns/combined/djview/BeatModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,37 @@

import javax.sound.midi.*;

import java.io.File;
import java.io.IOException;

import java.util.*;

public class BeatModel implements BeatModelInterface, MetaEventListener {
Sequencer sequencer;
List<BeatObserver> beatObservers = new ArrayList<BeatObserver>();
List<BPMObserver> bpmObservers = new ArrayList<BPMObserver>();
ArrayList<BeatObserver> beatObservers = new ArrayList<BeatObserver>();
ArrayList<BPMObserver> bpmObservers = new ArrayList<BPMObserver>();
int bpm = 90;
Sequence sequence;
Track track;

public void initialize() {
setUpMidi();
buildTrackAndStart();
}

public void on() {
System.out.println("Starting the sequencer");
sequencer.start();
setBPM(90);
sequencer.start();
}

public void off() {
setBPM(0);
sequencer.stop();
setBPM(0);
}

public void setBPM(int bpm) {
this.bpm = bpm;
sequencer.setTempoInBPM(getBPM());
sequencer.setTempoInBPM(bpm);
notifyBPMObservers();
}

Expand Down Expand Up @@ -84,6 +86,7 @@ public void removeObserver(BPMObserver o) {


public void meta(MetaMessage message) {
System.out.println(message.getType());
if (message.getType() == 47) {
beatEvent();
sequencer.start();
Expand All @@ -93,55 +96,23 @@ public void meta(MetaMessage message) {

public void setUpMidi() {
try {
Synthesizer synth = MidiSystem.getSynthesizer();
synth.loadAllInstruments(synth.getDefaultSoundbank());

sequencer = MidiSystem.getSequencer();
this.sequence = MidiSystem.getSequence(new File("test.mid"));
sequencer.open();
sequencer.addMetaEventListener(this);
sequence = new Sequence(Sequence.PPQ,4);
track = sequence.createTrack();
sequencer.setSequence(sequence);
sequencer.setTempoFactor(1f);
sequencer.addMetaEventListener(meta -> {
if (meta.getType() == 47) {
sequencer.setTickPosition(0);
sequencer.start();
}
});
sequencer.setTempoInBPM(getBPM());
sequencer.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);
} catch(Exception e) {
e.printStackTrace();
}
}

public void buildTrackAndStart() {
int[] trackList = {35, 0, 46, 0};

sequence.deleteTrack(null);
track = sequence.createTrack();

makeTracks(trackList);
track.add(makeEvent(192,9,1,0,4));
try {
sequencer.setSequence(sequence);
} catch(Exception e) {
e.printStackTrace();
}
}

public void makeTracks(int[] list) {

for (int i = 0; i < list.length; 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;
}
}

0 comments on commit 774a3d5

Please sign in to comment.