From 774a3d5b051652a0e04aaa4d85e330a9366d630b Mon Sep 17 00:00:00 2001 From: bethrobson Date: Wed, 16 Sep 2020 10:41:36 -0700 Subject: [PATCH] updated BeatModel.java to fix midi/synth bug that was causing the beat to not work well --- .../combined/djview/BeatModel.java | 71 ++++++------------- 1 file changed, 21 insertions(+), 50 deletions(-) diff --git a/src/headfirst/designpatterns/combined/djview/BeatModel.java b/src/headfirst/designpatterns/combined/djview/BeatModel.java index fc818eea..e1532208 100755 --- a/src/headfirst/designpatterns/combined/djview/BeatModel.java +++ b/src/headfirst/designpatterns/combined/djview/BeatModel.java @@ -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 beatObservers = new ArrayList(); - List bpmObservers = new ArrayList(); + ArrayList beatObservers = new ArrayList(); + ArrayList bpmObservers = new ArrayList(); 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(); } @@ -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(); @@ -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; - } }