1 package org.herac.tuxguitar.player.impl.midiport.fluidsynth;
2 
3 import java.util.ArrayList;
4 import java.util.List;
5 
6 import org.herac.tuxguitar.player.impl.midiport.fluidsynth.type.BooleanRef;
7 import org.herac.tuxguitar.player.impl.midiport.fluidsynth.type.DoubleRef;
8 import org.herac.tuxguitar.player.impl.midiport.fluidsynth.type.IntegerRef;
9 import org.herac.tuxguitar.player.impl.midiport.fluidsynth.type.StringRef;
10 
11 public class MidiSynth {
12 
13 	private static final String JNI_LIBRARY_NAME = new String("tuxguitar-fluidsynth-jni");
14 
15 	static{
16 		System.loadLibrary(JNI_LIBRARY_NAME);
17 	}
18 
19 	private long instance;
20 	private MidiOutputPortImpl loadedPort;
21 
MidiSynth()22 	public MidiSynth(){
23 		this.instance = malloc();
24 		this.loadedPort = null;
25 	}
26 
isInitialized()27 	public boolean isInitialized(){
28 		return (this.instance != 0);
29 	}
30 
finalize()31 	public void finalize(){
32 		if(isInitialized()){
33 			this.free(this.instance);
34 			this.instance = 0;
35 		}
36 	}
37 
isConnected(MidiOutputPortImpl port)38 	public boolean isConnected(MidiOutputPortImpl port){
39 		return (port != null && this.loadedPort != null && this.loadedPort.equals( port ) );
40 	}
41 
connect(MidiOutputPortImpl port)42 	public void connect(MidiOutputPortImpl port){
43 		if(isInitialized()){
44 			this.disconnect( this.loadedPort );
45 			this.open(this.instance);
46 			this.loadFont(this.instance, port.getSoundFont());
47 			this.loadedPort = port;
48 		}
49 	}
50 
disconnect(MidiOutputPortImpl port)51 	public void disconnect(MidiOutputPortImpl port){
52 		if(isInitialized() && isConnected(port)){
53 			this.unloadFont(this.instance);
54 			this.close(this.instance);
55 			this.loadedPort = null;
56 		}
57 	}
58 
reconnect()59 	public void reconnect(){
60 		MidiOutputPortImpl connection = this.loadedPort;
61 		if( isConnected( connection ) ){
62 			this.disconnect( connection );
63 			this.connect( connection );
64 		}
65 	}
66 
sendSystemReset()67 	public void sendSystemReset() {
68 		if(isInitialized()){
69 			this.systemReset(this.instance);
70 		}
71 	}
72 
sendNoteOn(int channel, int key, int velocity)73 	public void sendNoteOn(int channel, int key, int velocity) {
74 		if(isInitialized()){
75 			this.noteOn(this.instance,channel, key, velocity);
76 		}
77 	}
78 
sendNoteOff(int channel, int key, int velocity)79 	public void sendNoteOff(int channel, int key, int velocity) {
80 		if(isInitialized()){
81 			this.noteOff(this.instance,channel, key, velocity);
82 		}
83 	}
84 
sendControlChange(int channel, int controller, int value)85 	public void sendControlChange(int channel, int controller, int value) {
86 		if(isInitialized()){
87 			this.controlChange(this.instance,channel, controller, value);
88 		}
89 	}
90 
sendProgramChange(int channel, int value)91 	public void sendProgramChange(int channel, int value) {
92 		if(isInitialized()){
93 			this.programChange(this.instance,channel, value);
94 		}
95 	}
96 
sendPitchBend(int channel, int value)97 	public void sendPitchBend(int channel, int value) {
98 		if(isInitialized()){
99 			this.pitchBend(this.instance,channel, value);
100 		}
101 	}
102 
setDoubleProperty( String key , double value )103 	public void setDoubleProperty( String key , double value ){
104 		if(isInitialized()){
105 			this.setDoubleProperty(this.instance, key, value);
106 		}
107 	}
108 
setIntegerProperty( String key , int value )109 	public void setIntegerProperty( String key , int value ){
110 		if(isInitialized()){
111 			this.setIntegerProperty(this.instance, key, value);
112 		}
113 	}
114 
setStringProperty( String key , String value )115 	public void setStringProperty( String key , String value ){
116 		if(isInitialized()){
117 			this.setStringProperty(this.instance, key, value);
118 		}
119 	}
120 
getDoubleProperty( String key )121 	public double getDoubleProperty( String key ){
122 		DoubleRef value = new DoubleRef();
123 		if(isInitialized()){
124 			this.getDoubleProperty(this.instance, key, value);
125 		}
126 		return value.getValue();
127 	}
128 
getIntegerProperty( String key )129 	public int getIntegerProperty( String key ){
130 		IntegerRef value = new IntegerRef();
131 		if(isInitialized()){
132 			this.getIntegerProperty(this.instance, key, value);
133 		}
134 		return value.getValue();
135 	}
136 
getStringProperty( String key )137 	public String getStringProperty( String key ){
138 		StringRef value = new StringRef();
139 		if(isInitialized()){
140 			this.getStringProperty(this.instance, key, value);
141 		}
142 		return value.getValue();
143 	}
144 
getDoublePropertyDefault( String key )145 	public double getDoublePropertyDefault( String key ){
146 		DoubleRef value = new DoubleRef();
147 		if(isInitialized()){
148 			this.getDoublePropertyDefault(this.instance, key, value);
149 		}
150 		return value.getValue();
151 	}
152 
getIntegerPropertyDefault( String key )153 	public int getIntegerPropertyDefault( String key ){
154 		IntegerRef value = new IntegerRef();
155 		if(isInitialized()){
156 			this.getIntegerPropertyDefault(this.instance, key, value);
157 		}
158 		return value.getValue();
159 	}
160 
getStringPropertyDefault( String key )161 	public String getStringPropertyDefault( String key ){
162 		StringRef value = new StringRef();
163 		if(isInitialized()){
164 			this.getStringPropertyDefault(this.instance, key, value);
165 		}
166 		return value.getValue();
167 	}
168 
getPropertyOptions( String key )169 	public List getPropertyOptions( String key ){
170 		List options = new ArrayList();
171 		if(isInitialized()){
172 			this.getPropertyOptions(instance, key, options);
173 		}
174 		return options;
175 	}
176 
getIntegerPropertyRange( String key )177 	public int[] getIntegerPropertyRange( String key ){
178 		IntegerRef minimum = new IntegerRef();
179 		IntegerRef maximum = new IntegerRef();
180 		if(isInitialized()){
181 			this.getIntegerPropertyRange(this.instance, key, minimum , maximum );
182 		}
183 		return new int[]{ minimum.getValue() , maximum.getValue() };
184 	}
185 
getDoublePropertyRange( String key )186 	public double[] getDoublePropertyRange( String key ){
187 		DoubleRef minimum = new DoubleRef();
188 		DoubleRef maximum = new DoubleRef();
189 		if(isInitialized()){
190 			this.getDoublePropertyRange(this.instance, key, minimum , maximum );
191 		}
192 		return new double[]{ minimum.getValue() , maximum.getValue() };
193 	}
194 
isRealtimeProperty( String key )195 	public boolean isRealtimeProperty( String key ){
196 		BooleanRef value = new BooleanRef();
197 		if(isInitialized()){
198 			this.isRealtimeProperty(this.instance, key, value);
199 		}
200 		return value.getValue();
201 	}
202 
malloc()203 	private native long malloc();
204 
free(long instance)205 	private native void free(long instance);
206 
open(long instance)207 	private native void open(long instance);
208 
close(long instance)209 	private native void close(long instance);
210 
loadFont(long instance, String path)211 	private native void loadFont(long instance, String path);
212 
unloadFont(long instance)213 	private native void unloadFont(long instance);
214 
systemReset(long instance)215 	private native void systemReset(long instance);
216 
noteOn(long instance,int channel,int note,int velocity)217 	private native void noteOn(long instance,int channel,int note,int velocity);
218 
noteOff(long instance,int channel,int note,int velocity)219 	private native void noteOff(long instance,int channel,int note,int velocity);
220 
controlChange(long instance,int channel,int control,int value)221 	private native void controlChange(long instance,int channel,int control,int value);
222 
programChange(long instance,int channel,int program)223 	private native void programChange(long instance,int channel,int program);
224 
pitchBend(long instance,int channel,int value)225 	private native void pitchBend(long instance,int channel,int value);
226 
setDoubleProperty(long instance, String key , double value )227 	private native void setDoubleProperty(long instance, String key , double value );
228 
setIntegerProperty(long instance, String key , int value )229 	private native void setIntegerProperty(long instance, String key , int value );
230 
setStringProperty(long instance, String key , String value )231 	private native void setStringProperty(long instance, String key , String value );
232 
getDoubleProperty(long instance, String key , DoubleRef ref )233 	private native void getDoubleProperty(long instance, String key , DoubleRef ref );
234 
getIntegerProperty(long instance, String key , IntegerRef ref )235 	private native void getIntegerProperty(long instance, String key , IntegerRef ref );
236 
getStringProperty(long instance, String key , StringRef ref )237 	private native void getStringProperty(long instance, String key , StringRef ref );
238 
getDoublePropertyDefault(long instance, String key , DoubleRef ref )239 	private native void getDoublePropertyDefault(long instance, String key , DoubleRef ref );
240 
getIntegerPropertyDefault(long instance, String key , IntegerRef ref )241 	private native void getIntegerPropertyDefault(long instance, String key , IntegerRef ref );
242 
getStringPropertyDefault(long instance, String key , StringRef ref )243 	private native void getStringPropertyDefault(long instance, String key , StringRef ref );
244 
getDoublePropertyRange(long instance, String key , DoubleRef minimum , DoubleRef maximum )245 	private native void getDoublePropertyRange(long instance, String key , DoubleRef minimum , DoubleRef maximum );
246 
getIntegerPropertyRange(long instance, String key , IntegerRef minimum , IntegerRef maximum )247 	private native void getIntegerPropertyRange(long instance, String key , IntegerRef minimum , IntegerRef maximum );
248 
getPropertyOptions(long instance, String key , List options )249 	private native void getPropertyOptions(long instance, String key , List options );
250 
isRealtimeProperty(long instance, String key , BooleanRef ref )251 	private native void isRealtimeProperty(long instance, String key , BooleanRef ref );
252 }
253