28 |
|
|
29 |
import org.jsampler.CC; |
import org.jsampler.CC; |
30 |
import org.jsampler.HF; |
import org.jsampler.HF; |
31 |
|
import org.jsampler.JSPrefs; |
32 |
import org.jsampler.SamplerChannelModel; |
import org.jsampler.SamplerChannelModel; |
33 |
import org.jsampler.SamplerModel; |
import org.jsampler.SamplerModel; |
34 |
|
|
35 |
import org.linuxsampler.lscp.FxSend; |
import org.linuxsampler.lscp.FxSend; |
36 |
|
|
37 |
import static org.jsampler.JSI18n.i18n; |
import static org.jsampler.JSI18n.i18n; |
38 |
|
import static org.jsampler.JSPrefs.*; |
39 |
|
|
40 |
|
|
41 |
/** |
/** |
62 |
/** The entry point of the task. */ |
/** The entry point of the task. */ |
63 |
public void |
public void |
64 |
run() { |
run() { |
65 |
try { setResult(CC.getClient().addSamplerChannel()); } |
try { |
66 |
catch(Exception x) { |
setResult(CC.getClient().addSamplerChannel()); |
67 |
|
|
68 |
|
JSPrefs p = CC.getViewConfig().preferences(); |
69 |
|
if(!p.getBoolProperty(USE_CHANNEL_DEFAULTS)) return; |
70 |
|
|
71 |
|
String s = p.getStringProperty(DEFAULT_ENGINE); |
72 |
|
if(s != null && s.length() > 0) { |
73 |
|
CC.getClient().loadSamplerEngine(s, getResult()); |
74 |
|
} |
75 |
|
|
76 |
|
s = p.getStringProperty(DEFAULT_MIDI_INPUT); |
77 |
|
if(s != null && s.equals("firstDevice")) { |
78 |
|
assignFirstMidiDevice(); |
79 |
|
} else if(s != null && s.equals("firstDeviceNextChannel")) { |
80 |
|
assignFirstMidiDeviceNextChannel(); |
81 |
|
} |
82 |
|
|
83 |
|
s = p.getStringProperty(DEFAULT_AUDIO_OUTPUT); |
84 |
|
if(s != null && s.equals("firstDevice")) { |
85 |
|
assignFirstAudioDevice(); |
86 |
|
} |
87 |
|
} catch(Exception x) { |
88 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
89 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
90 |
} |
} |
91 |
} |
} |
92 |
|
|
93 |
|
private void |
94 |
|
assignFirstMidiDevice() throws Exception { |
95 |
|
if(CC.getSamplerModel().getMidiDeviceCount() < 1) return; |
96 |
|
int id = CC.getSamplerModel().getMidiDevices()[0].getDeviceId(); |
97 |
|
CC.getClient().setChannelMidiInputDevice(getResult(), id); |
98 |
|
} |
99 |
|
|
100 |
|
private void |
101 |
|
assignFirstMidiDeviceNextChannel() throws Exception { |
102 |
|
int channelId = getResult(); |
103 |
|
if(CC.getSamplerModel().getMidiDeviceCount() < 1) return; |
104 |
|
int id = CC.getSamplerModel().getMidiDevices()[0].getDeviceId(); |
105 |
|
CC.getClient().setChannelMidiInputDevice(channelId, id); |
106 |
|
|
107 |
|
boolean[] usedChannels = new boolean[16]; |
108 |
|
for(int i = 0; i < usedChannels.length; i++) usedChannels[i] = false; |
109 |
|
|
110 |
|
for(SamplerChannelModel m : CC.getSamplerModel().getChannels()) { |
111 |
|
if(m.getChannelId() == channelId) continue; |
112 |
|
if(m.getChannelInfo().getMidiInputDevice() != id) continue; |
113 |
|
if(m.getChannelInfo().getMidiInputPort() != 0) continue; |
114 |
|
int chn = m.getChannelInfo().getMidiInputChannel(); |
115 |
|
if(chn >= 0 && chn < 16) usedChannels[chn] = true; |
116 |
|
} |
117 |
|
|
118 |
|
int lastUsed = -1; |
119 |
|
for(int i = 0; i < usedChannels.length; i++) { |
120 |
|
if(usedChannels[i]) lastUsed = i; |
121 |
|
} |
122 |
|
|
123 |
|
if(lastUsed == -1) { |
124 |
|
CC.getClient().setChannelMidiInputChannel(channelId, 0); |
125 |
|
return; |
126 |
|
} |
127 |
|
|
128 |
|
if(lastUsed < 15) { |
129 |
|
CC.getClient().setChannelMidiInputChannel(channelId, lastUsed + 1); |
130 |
|
return; |
131 |
|
} |
132 |
|
|
133 |
|
int firstUnused = -1; |
134 |
|
for(int i = 0; i < usedChannels.length; i++) { |
135 |
|
if(!usedChannels[i]) { |
136 |
|
firstUnused = i; |
137 |
|
break; |
138 |
|
} |
139 |
|
} |
140 |
|
|
141 |
|
if(firstUnused == -1) { |
142 |
|
CC.getClient().setChannelMidiInputChannel(channelId, 0); |
143 |
|
return; |
144 |
|
} |
145 |
|
|
146 |
|
CC.getClient().setChannelMidiInputChannel(channelId, firstUnused); |
147 |
|
} |
148 |
|
|
149 |
|
private void |
150 |
|
assignFirstAudioDevice() throws Exception { |
151 |
|
if(CC.getSamplerModel().getAudioDeviceCount() < 1) return; |
152 |
|
int id = CC.getSamplerModel().getAudioDevices()[0].getDeviceId(); |
153 |
|
CC.getClient().setChannelAudioOutputDevice(getResult(), id); |
154 |
|
} |
155 |
} |
} |
156 |
|
|
157 |
/** |
/** |