/
CustomTicker.java
122 lines (101 loc) 路 3.75 KB
/
CustomTicker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Copyright (C) 2021 Jacob Wysko
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
package org.wysko.midis2jam2;
import com.jme3.app.SimpleApplication;
import com.jme3.asset.DesktopAssetManager;
import com.jme3.input.FlyByCamera;
import com.jme3.input.InputManager;
import com.jme3.input.dummy.DummyKeyInput;
import com.jme3.input.dummy.DummyMouseInput;
import com.jme3.math.Transform;
import com.jme3.renderer.Camera;
import com.jme3.scene.Node;
import com.jme3.scene.SceneGraphVisitor;
import com.jme3.scene.Spatial;
import org.wysko.midis2jam2.midi.MidiFile;
import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
@SuppressWarnings("all")
public class CustomTicker {
public static void main(String[] args) throws MidiUnavailableException, InvalidMidiDataException, IOException {
Logger.getLogger("com.jme3").setLevel(Level.OFF);
var sequencer = MidiSystem.getSequencer(false);
var file = new File("C:\\Users\\wysko\\Documents\\3 dogs on a couch.mid");
sequencer.setSequence(MidiSystem.getSequence(file));
var midiFile = MidiFile.readMidiFile(file);
var midis2jam2 = new Midis2jam2(sequencer, midiFile, new M2J2Settings(0, M2J2Settings.InstrumentTransition.NORMAL));
var app = new DummySimpleApp();
app.simpleInitApp();
app.getStateManager().attach(midis2jam2);
midis2jam2.initialize(app.getStateManager(), app);
app.getRootNode().attachChild(midis2jam2.getRootNode());
Map<Spatial, Map<Integer, Transform>> keyframesMap = new HashMap<>();
Map<Spatial, Integer> idMap = new HashMap<>();
midis2jam2.getRootNode().breadthFirstTraversal(new SceneGraphVisitor() {
int id = 0;
@Override
public void visit(Spatial spatial) {
if (spatial.getKey() != null) {
keyframesMap.put(spatial, new HashMap<>());
idMap.put(spatial, id++);
}
}
});
final var fps = 60;
final var duration = 10;
for (var i = 0; i < duration * fps; i++) {
midis2jam2.update(1f / fps);
int finalI = i;
midis2jam2.getRootNode().breadthFirstTraversal(spatial -> {
var integerTransformMap = keyframesMap.get(spatial);
if (integerTransformMap != null) {
integerTransformMap.put(finalI, spatial.getLocalTransform());
}
});
}
try (var stream = new FileOutputStream("anim.txt")) {
for (Map.Entry<Spatial, Integer> e : idMap.entrySet()) {
if (e.getKey() instanceof Node) {
stream.write('N');
stream.write(e.getValue());
if (e.getKey() == midis2jam2.getRootNode()) {
stream.write(0);
} else {
stream.write(idMap.getOrDefault(e.getKey().getParent(), Integer.MAX_VALUE));
}
}
}
}
}
}
class DummySimpleApp extends SimpleApplication {
@Override
public void simpleInitApp() {
this.assetManager = new DesktopAssetManager(true);
this.cam = new Camera(800, 600);
this.flyCam = new FlyByCamera(cam);
this.inputManager = new InputManager(new DummyMouseInput(), new DummyKeyInput(), null, null);
}
}