diff options
Diffstat (limited to 'src/com')
-rwxr-xr-x | src/com/fourisland/fourpuzzle/Audio.java | 1 | ||||
-rw-r--r-- | src/com/fourisland/fourpuzzle/util/MidiParser.java | 83 | ||||
-rwxr-xr-x | src/com/fourisland/fourpuzzle/util/ObjectLoader.java | 7 |
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 | ||
6 | package com.fourisland.fourpuzzle; | 6 | package com.fourisland.fourpuzzle; |
7 | 7 | ||
8 | import com.fourisland.fourpuzzle.util.MidiParser; | ||
8 | import com.fourisland.fourpuzzle.util.ObjectLoader; | 9 | import com.fourisland.fourpuzzle.util.ObjectLoader; |
9 | import java.util.concurrent.Executor; | 10 | import java.util.concurrent.Executor; |
10 | import java.util.concurrent.Executors; | 11 | 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 @@ | |||
1 | /* | ||
2 | * To change this template, choose Tools | Templates | ||
3 | * and open the template in the editor. | ||
4 | */ | ||
5 | |||
6 | package com.fourisland.fourpuzzle.util; | ||
7 | |||
8 | import javax.sound.midi.InvalidMidiDataException; | ||
9 | import javax.sound.midi.MidiEvent; | ||
10 | import javax.sound.midi.MidiMessage; | ||
11 | import javax.sound.midi.Sequence; | ||
12 | import javax.sound.midi.ShortMessage; | ||
13 | import javax.sound.midi.Track; | ||
14 | |||
15 | /** | ||
16 | * | ||
17 | * @author hatkirby | ||
18 | */ | ||
19 | public 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 | ||