From 7fbf2187cf557f75c3f63ba36d2563592b066536 Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Tue, 10 Feb 2009 17:37:14 -0500 Subject: Engine: Removed Music starting delay This annoying bug was finally removed by creating a seperate MidiParser class that takes a Sequence and parses out the non-emitting messages. This is run in ObjectLoader so the result is cached. Fixes #3 --- src/com/fourisland/fourpuzzle/Audio.java | 1 + src/com/fourisland/fourpuzzle/util/MidiParser.java | 83 ++++++++++++++++++++++ .../fourisland/fourpuzzle/util/ObjectLoader.java | 7 ++ 3 files changed, 91 insertions(+) create mode 100644 src/com/fourisland/fourpuzzle/util/MidiParser.java (limited to 'src') diff --git a/src/com/fourisland/fourpuzzle/Audio.java b/src/com/fourisland/fourpuzzle/Audio.java index c8cd75b..5fe0dfc 100755 --- a/src/com/fourisland/fourpuzzle/Audio.java +++ b/src/com/fourisland/fourpuzzle/Audio.java @@ -5,6 +5,7 @@ package com.fourisland.fourpuzzle; +import com.fourisland.fourpuzzle.util.MidiParser; import com.fourisland.fourpuzzle.util.ObjectLoader; import java.util.concurrent.Executor; import java.util.concurrent.Executors; diff --git a/src/com/fourisland/fourpuzzle/util/MidiParser.java b/src/com/fourisland/fourpuzzle/util/MidiParser.java new file mode 100644 index 0000000..84dea26 --- /dev/null +++ b/src/com/fourisland/fourpuzzle/util/MidiParser.java @@ -0,0 +1,83 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package com.fourisland.fourpuzzle.util; + +import javax.sound.midi.InvalidMidiDataException; +import javax.sound.midi.MidiEvent; +import javax.sound.midi.MidiMessage; +import javax.sound.midi.Sequence; +import javax.sound.midi.ShortMessage; +import javax.sound.midi.Track; + +/** + * + * @author hatkirby + */ +public class MidiParser { + + private Sequence seq; + private Track currentTrack; + public MidiParser(Sequence seq) + { + this.seq = seq; + } + + public Sequence parse() throws InvalidMidiDataException + { + Sequence temp = temp = new Sequence(seq.getDivisionType(), seq.getResolution()); + + for (Track t : seq.getTracks()) + { + parseTrack(t, temp.createTrack()); + } + + return temp; + } + + private void parseTrack(Track t, Track nt) + { + currentTrack = nt; + + for (int i = 0; i < t.size(); i++) + { + MidiEvent mi = t.get(i); + + parseEvent(mi.getMessage(), mi.getTick()); + } + } + + private void parseEvent(MidiMessage mm, long tick) + { + if (mm instanceof ShortMessage) + { + parseShortMessage((ShortMessage) mm,tick); + } else { + currentTrack.add(new MidiEvent(mm, tick)); + } + } + + private long firstReal = 0; + private boolean foundFirstReal = false; + private void parseShortMessage(ShortMessage mm, long tick) + { + if ((mm.getCommand() == ShortMessage.NOTE_ON) || (mm.getCommand() == ShortMessage.NOTE_OFF)) + { + if (!foundFirstReal) + { + if (mm.getData2() != 0) + { + foundFirstReal = true; + firstReal = tick; + } else { + return; + } + } + } + + currentTrack.add(new MidiEvent(mm, tick-firstReal)); + } + +} diff --git a/src/com/fourisland/fourpuzzle/util/ObjectLoader.java b/src/com/fourisland/fourpuzzle/util/ObjectLoader.java index 2000a1c..3f518cf 100755 --- a/src/com/fourisland/fourpuzzle/util/ObjectLoader.java +++ b/src/com/fourisland/fourpuzzle/util/ObjectLoader.java @@ -119,6 +119,13 @@ public class ObjectLoader { Logger.getLogger(ObjectLoader.class.getName()).log(Level.SEVERE, null, ex); } + MidiParser mp = new MidiParser(seq); + try { + seq = mp.parse(); + } catch (InvalidMidiDataException ex) { + Logger.getLogger(ObjectLoader.class.getName()).log(Level.SEVERE, null, ex); + } + addToObjectCache("Music", name, seq); } -- cgit 1.4.1