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.gui.system.config.TGConfigManager;
7 import org.herac.tuxguitar.gui.system.plugins.TGPluginConfigManager;
8 
9 public class MidiSettings {
10 
11 	public static final String AUDIO_DRIVER = "audio.driver";
12 	public static final String AUDIO_SAMPLE_FORMAT = "audio.sample-format";
13 	public static final String AUDIO_PERIOD_SIZE = "audio.period-size";
14 	public static final String AUDIO_PERIOD_COUNT = "audio.periods";
15 	public static final String SYNTH_GAIN = "synth.gain";
16 	public static final String SYNTH_POLYPHONY = "synth.polyphony";
17 	public static final String SYNTH_SAMPLE_RATE = "synth.sample-rate";
18 	public static final String SYNTH_REVERB_ACTIVE = "synth.reverb.active";
19 	public static final String SYNTH_CHORUS_ACTIVE = "synth.chorus.active";
20 
21 	public static final String SYNTH_AUDIO_CHANNELS = "synth.audio-channels";
22 	public static final String SYNTH_AUDIO_GROUPS = "synth.audio-groups";
23 
24 	private TGConfigManager config;
25 	private MidiOutputPortProviderImpl provider;
26 
27 	private boolean restartSynth;
28 
MidiSettings(MidiOutputPortProviderImpl provider)29 	public MidiSettings(MidiOutputPortProviderImpl provider){
30 		this.provider = provider;
31 	}
32 
getSynth()33 	public MidiSynth getSynth(){
34 		return this.provider.getSynth();
35 	}
36 
getConfig()37 	public TGConfigManager getConfig(){
38 		if(this.config == null){
39 			this.config = new TGPluginConfigManager("tuxguitar-fluidsynth");
40 			this.config.init();
41 		}
42 		return this.config;
43 	}
44 
getDoubleValue( String property )45 	public double getDoubleValue( String property ){
46 		return getConfig().getDoubleConfigValue(property, this.getSynth().getDoubleProperty( property ));
47 	}
48 
getIntegerValue( String property )49 	public int getIntegerValue( String property ){
50 		return getConfig().getIntConfigValue(property, this.getSynth().getIntegerProperty( property ));
51 	}
52 
getStringValue( String property )53 	public String getStringValue( String property ){
54 		return getConfig().getStringConfigValue(property, this.getSynth().getStringProperty( property ));
55 	}
56 
getBooleanValue( String property )57 	public boolean getBooleanValue( String property ){
58 		String value = this.getStringValue(property);
59 		return (value != null && value.equals("yes"));
60 	}
61 
setDoubleValue( String property , double value )62 	public void setDoubleValue( String property , double value ){
63 		getConfig().setProperty( property , value );
64 	}
65 
setIntegerValue( String property , int value )66 	public void setIntegerValue( String property , int value ){
67 		getConfig().setProperty( property , value );
68 	}
69 
setStringValue( String property , String value )70 	public void setStringValue( String property , String value ){
71 		if( value == null ){
72 			getConfig().removeProperty( property );
73 		}else{
74 			getConfig().setProperty( property , value );
75 		}
76 	}
77 
setBooleanValue( String property , boolean value )78 	public void setBooleanValue( String property , boolean value ){
79 		this.setStringValue(property, ( value ? "yes" : "no" ) );
80 	}
81 
getSoundfonts()82 	public List getSoundfonts(){
83 		List ports = new ArrayList();
84 		TGConfigManager config = getConfig();
85 
86 		int count = config.getIntConfigValue("soundfont.count");
87 		for(int i = 0; i < count;i ++){
88 			String path = config.getStringConfigValue("soundfont.path" + i);
89 			if(path != null && path.length() > 0 ){
90 				ports.add( path );
91 			}
92 		}
93 		return ports;
94 	}
95 
setSoundfonts(List soundfonts)96 	public void setSoundfonts(List soundfonts){
97 		TGConfigManager config = getConfig();
98 		config.setProperty("soundfont.count", soundfonts.size() );
99 		for( int i = 0 ; i < soundfonts.size() ; i ++ ){
100 			String path = (String)soundfonts.get( i );
101 			config.setProperty("soundfont.path" + i, path );
102 		}
103 	}
104 
save()105 	public void save(){
106 		this.getConfig().save();
107 	}
108 
apply()109 	public void apply(){
110 		if(this.getSynth() != null && this.getSynth().isInitialized()){
111 			this.restartSynth = false;
112 			this.applyStringProperty( AUDIO_DRIVER );
113 			this.applyStringProperty( AUDIO_SAMPLE_FORMAT );
114 			this.applyIntegerProperty(AUDIO_PERIOD_SIZE );
115 			this.applyIntegerProperty(AUDIO_PERIOD_COUNT );
116 			this.applyDoubleProperty(SYNTH_GAIN );
117 			this.applyDoubleProperty(SYNTH_SAMPLE_RATE );
118 			this.applyStringProperty(SYNTH_REVERB_ACTIVE );
119 			this.applyStringProperty(SYNTH_CHORUS_ACTIVE );
120 			this.applyIntegerProperty(SYNTH_POLYPHONY );
121 			if( this.restartSynth ){
122 				this.getSynth().reconnect();
123 				this.restartSynth = false;
124 			}
125 		}
126 	}
127 
applyStringProperty( String property )128 	private void applyStringProperty( String property ){
129 		String newValue = this.getStringValue( property );
130 		String oldValue = this.getSynth().getStringProperty( property );
131 		if( newValue != null ){
132 			if( oldValue == null || !newValue.equals( oldValue ) ){
133 				this.getSynth().setStringProperty( property, newValue );
134 				this.restartSynth = (this.restartSynth || !this.getSynth().isRealtimeProperty( property ));
135 			}
136 		}
137 	}
138 
applyDoubleProperty( String property )139 	private void applyDoubleProperty( String property ){
140 		double newValue = this.getDoubleValue( property );
141 		double oldValue = this.getSynth().getDoubleProperty( property );
142 		if( newValue != oldValue ){
143 			this.getSynth().setDoubleProperty( property, newValue );
144 			this.restartSynth = (this.restartSynth || !this.getSynth().isRealtimeProperty( property ));
145 		}
146 	}
147 
applyIntegerProperty( String property )148 	private void applyIntegerProperty( String property ){
149 		int newValue = this.getIntegerValue( property );
150 		int oldValue = this.getSynth().getIntegerProperty( property );
151 		if( newValue != oldValue ){
152 			this.getSynth().setIntegerProperty( property, newValue );
153 			this.restartSynth = (this.restartSynth || !this.getSynth().isRealtimeProperty( property ));
154 		}
155 	}
156 }
157