1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005 Grigor Kirilov Iliev |
5 |
* |
6 |
* This file is part of JSampler. |
7 |
* |
8 |
* JSampler is free software; you can redistribute it and/or modify |
9 |
* it under the terms of the GNU General Public License version 2 |
10 |
* as published by the Free Software Foundation. |
11 |
* |
12 |
* JSampler is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with JSampler; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
20 |
* MA 02111-1307 USA |
21 |
*/ |
22 |
|
23 |
package org.jsampler; |
24 |
|
25 |
import java.awt.event.ActionEvent; |
26 |
import java.awt.event.ActionListener; |
27 |
|
28 |
import java.io.FileOutputStream; |
29 |
|
30 |
import java.util.logging.Handler; |
31 |
import java.util.logging.Level; |
32 |
import java.util.logging.Logger; |
33 |
import java.util.logging.SimpleFormatter; |
34 |
import java.util.logging.StreamHandler; |
35 |
|
36 |
import javax.swing.Timer; |
37 |
|
38 |
import org.jsampler.task.*; |
39 |
|
40 |
import org.jsampler.view.JSMainFrame; |
41 |
import org.jsampler.view.JSProgress; |
42 |
|
43 |
import org.linuxsampler.lscp.Client; |
44 |
import org.linuxsampler.lscp.event.*; |
45 |
|
46 |
import net.sf.juife.Task; |
47 |
import net.sf.juife.TaskQueue; |
48 |
|
49 |
import net.sf.juife.event.TaskEvent; |
50 |
import net.sf.juife.event.TaskListener; |
51 |
import net.sf.juife.event.TaskQueueEvent; |
52 |
import net.sf.juife.event.TaskQueueListener; |
53 |
|
54 |
|
55 |
/** |
56 |
* |
57 |
* @author Grigor Iliev |
58 |
*/ |
59 |
public class CC { |
60 |
private static Handler handler; |
61 |
public static FileOutputStream fos; |
62 |
|
63 |
private static JSMainFrame mainFrame = null; |
64 |
private static JSProgress progress = null; |
65 |
|
66 |
private final static Client lsClient = new Client(); |
67 |
|
68 |
private final static TaskQueue taskQueue = new TaskQueue(); |
69 |
private final static Timer timer = new Timer(1000, null); |
70 |
|
71 |
private final static EventHandler eventHandler = new EventHandler(); |
72 |
|
73 |
public static Logger |
74 |
getLogger() { |
75 |
return Logger.getLogger ( |
76 |
"org.jsampler", |
77 |
"org.jsampler.langprops.LogsBundle" |
78 |
); |
79 |
} |
80 |
|
81 |
public static TaskQueue |
82 |
getTaskQueue() { return taskQueue; } |
83 |
|
84 |
public static JSMainFrame |
85 |
getMainFrame() { return mainFrame; } |
86 |
|
87 |
public static void |
88 |
setMainFrame(JSMainFrame mainFrame) { CC.mainFrame = mainFrame; } |
89 |
|
90 |
public static JSProgress |
91 |
getProgressIndicator() { return progress; } |
92 |
|
93 |
public static void |
94 |
setProgressIndicator(JSProgress progress) { CC.progress = progress; } |
95 |
|
96 |
protected static void |
97 |
initJSampler() { |
98 |
fos = null; |
99 |
|
100 |
try { fos = new FileOutputStream("JSampler.log"); } |
101 |
catch(Exception x) { x.printStackTrace(); } |
102 |
|
103 |
if(fos == null) handler = new StreamHandler(System.out, new SimpleFormatter()); |
104 |
else handler = new StreamHandler(fos, new SimpleFormatter()); |
105 |
|
106 |
handler.setLevel(Level.FINE); |
107 |
getLogger().addHandler(handler); |
108 |
getLogger().setLevel(Level.FINE); |
109 |
Logger.getLogger("org.linuxsampler.lscp").addHandler(handler); |
110 |
Logger.getLogger("org.linuxsampler.lscp").setLevel(Level.FINE); |
111 |
|
112 |
// Flushing logs on every second |
113 |
new java.util.Timer().schedule(new java.util.TimerTask() { |
114 |
public void |
115 |
run() { if(handler != null) handler.flush(); } |
116 |
}, 1000, 1000); |
117 |
|
118 |
CC.getLogger().fine("CC.jsStarted"); |
119 |
|
120 |
HF.setUIDefaultFont(Prefs.getInterfaceFont()); |
121 |
|
122 |
|
123 |
|
124 |
getClient().setServerAddress(Prefs.getLSAddress()); |
125 |
getClient().setServerPort(Prefs.getLSPort()); |
126 |
|
127 |
timer.setRepeats(false); |
128 |
|
129 |
timer.addActionListener(new ActionListener() { |
130 |
public void |
131 |
actionPerformed(ActionEvent e) { CC.getProgressIndicator().start(); } |
132 |
}); |
133 |
|
134 |
taskQueue.addTaskQueueListener(new TaskQueueListener() { |
135 |
public void |
136 |
stateChanged(TaskQueueEvent e) { |
137 |
switch(e.getEventID()) { |
138 |
case TASK_FETCHED: |
139 |
CC.getProgressIndicator().setString ( |
140 |
((Task)e.getSource()).getDescription() |
141 |
); |
142 |
break; |
143 |
case TASK_DONE: |
144 |
EnhancedTask t = (EnhancedTask)e.getSource(); |
145 |
if(t.doneWithErrors() && !t.isStopped()) |
146 |
HF.showErrorMessage(t.getErrorMessage()); |
147 |
break; |
148 |
case NOT_IDLE: |
149 |
timer.start(); |
150 |
break; |
151 |
case IDLE: |
152 |
timer.stop(); |
153 |
CC.getProgressIndicator().stop(); |
154 |
break; |
155 |
} |
156 |
} |
157 |
}); |
158 |
|
159 |
taskQueue.start(); |
160 |
|
161 |
getClient().addChannelCountListener(eventHandler); |
162 |
getClient().addChannelInfoListener(eventHandler); |
163 |
getClient().addStreamCountListener(eventHandler); |
164 |
getClient().addVoiceCountListener(eventHandler); |
165 |
getClient().addTotalVoiceCountListener(eventHandler); |
166 |
} |
167 |
|
168 |
public static void |
169 |
cleanExit() { cleanExit(0); } |
170 |
|
171 |
public static void |
172 |
cleanExit(int i) { |
173 |
CC.getLogger().fine("CC.jsEnded"); |
174 |
System.exit(i); |
175 |
} |
176 |
|
177 |
public static Client |
178 |
getClient() { return lsClient; } |
179 |
|
180 |
|
181 |
private static final SamplerModel samplerModel = new DefaultSamplerModel(); |
182 |
|
183 |
/** |
184 |
* Gets the sampler model. |
185 |
* @return The sampler model. |
186 |
*/ |
187 |
public static SamplerModel |
188 |
getSamplerModel() { return samplerModel; } |
189 |
|
190 |
public static void |
191 |
initSamplerModel() { |
192 |
final DefaultSamplerModel model = (DefaultSamplerModel)getSamplerModel(); |
193 |
|
194 |
final GetServerInfo gsi = new GetServerInfo(); |
195 |
gsi.addTaskListener(new TaskListener() { |
196 |
public void |
197 |
taskPerformed(TaskEvent e) { |
198 |
if(!gsi.doneWithErrors()) model.setServerInfo(gsi.getResult()); |
199 |
} |
200 |
}); |
201 |
|
202 |
final GetAODrivers gaod = new GetAODrivers(); |
203 |
gaod.addTaskListener(new TaskListener() { |
204 |
public void |
205 |
taskPerformed(TaskEvent e) { |
206 |
if(!gaod.doneWithErrors()) |
207 |
model.setAudioOutputDrivers(gaod.getResult()); |
208 |
} |
209 |
}); |
210 |
|
211 |
final GetEngines ge = new GetEngines(); |
212 |
ge.addTaskListener(new TaskListener() { |
213 |
public void |
214 |
taskPerformed(TaskEvent e) { |
215 |
if(!ge.doneWithErrors()) model.setEngines(ge.getResult()); |
216 |
} |
217 |
}); |
218 |
|
219 |
final GetMIDrivers gmid = new GetMIDrivers(); |
220 |
gmid.addTaskListener(new TaskListener() { |
221 |
public void |
222 |
taskPerformed(TaskEvent e) { |
223 |
if(!gmid.doneWithErrors()) |
224 |
model.setMidiInputDrivers(gmid.getResult()); |
225 |
} |
226 |
}); |
227 |
|
228 |
final Connect cnt = new Connect(); |
229 |
cnt.addTaskListener(new TaskListener() { |
230 |
public void |
231 |
taskPerformed(TaskEvent e) { |
232 |
if(cnt.doneWithErrors()) return; |
233 |
|
234 |
getTaskQueue().add(gsi); |
235 |
getTaskQueue().add(gaod); |
236 |
getTaskQueue().add(gmid); |
237 |
getTaskQueue().add(ge); |
238 |
getTaskQueue().add(new UpdateMidiDevices()); |
239 |
getTaskQueue().add(new UpdateAudioDevices()); |
240 |
getTaskQueue().add(new UpdateChannels()); |
241 |
} |
242 |
}); |
243 |
getTaskQueue().add(cnt); |
244 |
} |
245 |
|
246 |
private static class EventHandler implements ChannelCountListener, ChannelInfoListener, |
247 |
StreamCountListener, VoiceCountListener, TotalVoiceCountListener { |
248 |
|
249 |
/** Invoked when the number of channels has changed. */ |
250 |
public void |
251 |
channelCountChanged( ChannelCountEvent e) { |
252 |
getTaskQueue().add(new UpdateChannels()); |
253 |
} |
254 |
|
255 |
/** Invoked when changes to the sampler channel has occured. */ |
256 |
public void |
257 |
channelInfoChanged(ChannelInfoEvent e) { |
258 |
/* |
259 |
* Because of the rapid notification flow when instrument is loaded |
260 |
* we need to do some optimization to decrease the traffic. |
261 |
*/ |
262 |
boolean b = true; |
263 |
Task[] tS = getTaskQueue().getPendingTasks(); |
264 |
|
265 |
for(int i = tS.length - 1; i >= 0; i--) { |
266 |
Task t = tS[i]; |
267 |
|
268 |
if(t instanceof UpdateChannelInfo) { |
269 |
UpdateChannelInfo uci = (UpdateChannelInfo)t; |
270 |
if(uci.getChannelID() == e.getSamplerChannel()) return; |
271 |
} else { |
272 |
b = false; |
273 |
break; |
274 |
} |
275 |
} |
276 |
|
277 |
if(b) { |
278 |
Task t = getTaskQueue().getRunningTask(); |
279 |
if(t instanceof UpdateChannelInfo) { |
280 |
UpdateChannelInfo uci = (UpdateChannelInfo)t; |
281 |
if(uci.getChannelID() == e.getSamplerChannel()) return; |
282 |
} |
283 |
} |
284 |
|
285 |
|
286 |
getTaskQueue().add(new UpdateChannelInfo(e.getSamplerChannel())); |
287 |
} |
288 |
|
289 |
/** |
290 |
* Invoked when the number of active disk |
291 |
* streams in a specific sampler channel has changed. |
292 |
*/ |
293 |
public void |
294 |
streamCountChanged(StreamCountEvent e) { |
295 |
SamplerChannelModel scm = |
296 |
getSamplerModel().getChannelModel(e.getSamplerChannel()); |
297 |
|
298 |
if(scm == null) { |
299 |
CC.getLogger().log ( |
300 |
Level.WARNING, |
301 |
"CC.unknownChannel!", |
302 |
e.getSamplerChannel() |
303 |
); |
304 |
|
305 |
return; |
306 |
} |
307 |
|
308 |
scm.setStreamCount(e.getStreamCount()); |
309 |
} |
310 |
|
311 |
/** |
312 |
* Invoked when the number of active voices |
313 |
* in a specific sampler channel has changed. |
314 |
*/ |
315 |
public void |
316 |
voiceCountChanged(VoiceCountEvent e) { |
317 |
SamplerChannelModel scm = |
318 |
getSamplerModel().getChannelModel(e.getSamplerChannel()); |
319 |
|
320 |
if(scm == null) { |
321 |
CC.getLogger().log ( |
322 |
Level.WARNING, |
323 |
"CC.unknownChannel!", |
324 |
e.getSamplerChannel() |
325 |
); |
326 |
|
327 |
return; |
328 |
} |
329 |
|
330 |
scm.setVoiceCount(e.getVoiceCount()); |
331 |
} |
332 |
|
333 |
/** Invoked when the total number of active voices has changed. */ |
334 |
public void |
335 |
totalVoiceCountChanged(TotalVoiceCountEvent e) { |
336 |
getTaskQueue().add(new UpdateTotalVoiceCount()); |
337 |
} |
338 |
} |
339 |
} |