summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorStarla Insigna <hatkirby@fourisland.com>2009-02-10 17:37:14 -0500
committerStarla Insigna <hatkirby@fourisland.com>2009-02-10 17:37:14 -0500
commit7fbf2187cf557f75c3f63ba36d2563592b066536 (patch)
tree3794f956775486fe6d1a6886aadfdbffa92d1417 /src
parent9529954ca65660e976f12ed0a3f81e880581fee7 (diff)
downloadfourpuzzle-7fbf2187cf557f75c3f63ba36d2563592b066536.tar.gz
fourpuzzle-7fbf2187cf557f75c3f63ba36d2563592b066536.tar.bz2
fourpuzzle-7fbf2187cf557f75c3f63ba36d2563592b066536.zip
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
Diffstat (limited to 'src')
-rwxr-xr-xsrc/com/fourisland/fourpuzzle/Audio.java1
-rw-r--r--src/com/fourisland/fourpuzzle/util/MidiParser.java83
-rwxr-xr-xsrc/com/fourisland/fourpuzzle/util/ObjectLoader.java7
3 files changed, 91 insertions, 0 deletions
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 @@
5 5
6package com.fourisland.fourpuzzle; 6package com.fourisland.fourpuzzle;
7 7
8import com.fourisland.fourpuzzle.util.MidiParser;
8import com.fourisland.fourpuzzle.util.ObjectLoader; 9import com.fourisland.fourpuzzle.util.ObjectLoader;
9import java.util.concurrent.Executor; 10import java.util.concurrent.Executor;
10import java.util.concurrent.Executors; 11import 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 @@
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5
6package com.fourisland.fourpuzzle.util;
7
8import javax.sound.midi.InvalidMidiDataException;
9import javax.sound.midi.MidiEvent;
10import javax.sound.midi.MidiMessage;
11import javax.sound.midi.Sequence;
12import javax.sound.midi.ShortMessage;
13import javax.sound.midi.Track;
14
15/**
16 *
17 * @author hatkirby
18 */
19public class MidiParser {
20
21 private Sequence seq;
22 private Track currentTrack;
23 public MidiParser(Sequence seq)
24 {
25 this.seq = seq;
26 }
27
28 public Sequence parse() throws InvalidMidiDataException
29 {
30 Sequence temp = temp = new Sequence(seq.getDivisionType(), seq.getResolution());
31
32 for (Track t : seq.getTracks())
33 {
34 parseTrack(t, temp.createTrack());
35 }
36
37 return temp;
38 }
39
40 private void parseTrack(Track t, Track nt)
41 {
42 currentTrack = nt;
43
44 for (int i = 0; i < t.size(); i++)
45 {
46 MidiEvent mi = t.get(i);
47
48 parseEvent(mi.getMessage(), mi.getTick());
49 }
50 }
51
52 private void parseEvent(MidiMessage mm, long tick)
53 {
54 if (mm instanceof ShortMessage)
55 {
56 parseShortMessage((ShortMessage) mm,tick);
57 } else {
58 currentTrack.add(new MidiEvent(mm, tick));
59 }
60 }
61
62 private long firstReal = 0;
63 private boolean foundFirstReal = false;
64 private void parseShortMessage(ShortMessage mm, long tick)
65 {
66 if ((mm.getCommand() == ShortMessage.NOTE_ON) || (mm.getCommand() == ShortMessage.NOTE_OFF))
67 {
68 if (!foundFirstReal)
69 {
70 if (mm.getData2() != 0)
71 {
72 foundFirstReal = true;
73 firstReal = tick;
74 } else {
75 return;
76 }
77 }
78 }
79
80 currentTrack.add(new MidiEvent(mm, tick-firstReal));
81 }
82
83}
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 {
119 Logger.getLogger(ObjectLoader.class.getName()).log(Level.SEVERE, null, ex); 119 Logger.getLogger(ObjectLoader.class.getName()).log(Level.SEVERE, null, ex);
120 } 120 }
121 121
122 MidiParser mp = new MidiParser(seq);
123 try {
124 seq = mp.parse();
125 } catch (InvalidMidiDataException ex) {
126 Logger.getLogger(ObjectLoader.class.getName()).log(Level.SEVERE, null, ex);
127 }
128
122 addToObjectCache("Music", name, seq); 129 addToObjectCache("Music", name, seq);
123 } 130 }
124 131