1 |
/* |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
* |
4 |
* Copyright (C) 2005 Grigor Kirilov Iliev |
* Copyright (C) 2005-2007 Grigor Iliev <grigor@grigoriliev.com> |
5 |
* |
* |
6 |
* This file is part of JSampler. |
* This file is part of JSampler. |
7 |
* |
* |
25 |
import java.awt.event.ActionEvent; |
import java.awt.event.ActionEvent; |
26 |
import java.awt.event.ActionListener; |
import java.awt.event.ActionListener; |
27 |
|
|
28 |
|
import java.io.ByteArrayInputStream; |
29 |
|
import java.io.ByteArrayOutputStream; |
30 |
|
import java.io.File; |
31 |
|
import java.io.FileInputStream; |
32 |
import java.io.FileOutputStream; |
import java.io.FileOutputStream; |
33 |
|
import java.io.InputStream; |
34 |
|
|
35 |
|
import java.util.Vector; |
36 |
|
|
37 |
import java.util.logging.Handler; |
import java.util.logging.Handler; |
38 |
import java.util.logging.Level; |
import java.util.logging.Level; |
42 |
|
|
43 |
import javax.swing.Timer; |
import javax.swing.Timer; |
44 |
|
|
45 |
|
import javax.swing.event.ChangeEvent; |
46 |
|
import javax.swing.event.ChangeListener; |
47 |
|
|
48 |
|
import net.sf.juife.Task; |
49 |
|
import net.sf.juife.TaskQueue; |
50 |
|
|
51 |
|
import net.sf.juife.event.TaskEvent; |
52 |
|
import net.sf.juife.event.TaskListener; |
53 |
|
import net.sf.juife.event.TaskQueueEvent; |
54 |
|
import net.sf.juife.event.TaskQueueListener; |
55 |
|
|
56 |
|
import org.jsampler.event.ListEvent; |
57 |
|
import org.jsampler.event.ListListener; |
58 |
|
import org.jsampler.event.OrchestraEvent; |
59 |
|
import org.jsampler.event.OrchestraListener; |
60 |
|
|
61 |
import org.jsampler.task.*; |
import org.jsampler.task.*; |
62 |
|
|
63 |
import org.jsampler.view.JSMainFrame; |
import org.jsampler.view.JSMainFrame; |
64 |
import org.jsampler.view.JSProgress; |
import org.jsampler.view.JSProgress; |
65 |
|
import org.jsampler.view.JSViewConfig; |
66 |
|
import org.jsampler.view.InstrumentsDbTreeModel; |
67 |
|
|
68 |
|
import org.linuxsampler.lscp.AudioOutputChannel; |
69 |
|
import org.linuxsampler.lscp.AudioOutputDevice; |
70 |
import org.linuxsampler.lscp.Client; |
import org.linuxsampler.lscp.Client; |
71 |
|
import org.linuxsampler.lscp.FxSend; |
72 |
|
import org.linuxsampler.lscp.MidiInputDevice; |
73 |
|
import org.linuxsampler.lscp.MidiPort; |
74 |
|
import org.linuxsampler.lscp.Parameter; |
75 |
|
import org.linuxsampler.lscp.SamplerChannel; |
76 |
|
|
77 |
import org.linuxsampler.lscp.event.*; |
import org.linuxsampler.lscp.event.*; |
78 |
|
|
79 |
import net.sf.juife.Task; |
import org.w3c.dom.Document; |
80 |
import net.sf.juife.TaskQueue; |
import org.w3c.dom.Node; |
81 |
|
|
82 |
import net.sf.juife.event.TaskEvent; |
import static org.jsampler.JSI18n.i18n; |
83 |
import net.sf.juife.event.TaskListener; |
import static org.jsampler.JSPrefs.MANUAL_SERVER_SELECT_ON_STARTUP; |
|
import net.sf.juife.event.TaskQueueEvent; |
|
|
import net.sf.juife.event.TaskQueueListener; |
|
84 |
|
|
85 |
|
|
86 |
/** |
/** |
87 |
* |
* This class serves as a 'Control Center' of the application. |
88 |
|
* It also provides some fundamental routines and access to most used objects. |
89 |
* @author Grigor Iliev |
* @author Grigor Iliev |
90 |
*/ |
*/ |
91 |
public class CC { |
public class CC { |
92 |
private static Handler handler; |
private static Handler handler; |
93 |
public static FileOutputStream fos; |
private static FileOutputStream fos; |
94 |
|
|
95 |
|
private static JSViewConfig viewConfig = null; |
96 |
private static JSMainFrame mainFrame = null; |
private static JSMainFrame mainFrame = null; |
97 |
private static JSProgress progress = null; |
private static JSProgress progress = null; |
98 |
|
|
99 |
private final static Client lsClient = new Client(); |
private final static Client lsClient = new Client(); |
100 |
|
|
101 |
|
private static String jSamplerHome = null; |
102 |
|
|
103 |
private final static TaskQueue taskQueue = new TaskQueue(); |
private final static TaskQueue taskQueue = new TaskQueue(); |
104 |
private final static Timer timer = new Timer(1000, null); |
private final static Timer timer = new Timer(2000, null); |
105 |
|
|
106 |
private final static EventHandler eventHandler = new EventHandler(); |
/** Forbits the instantiation of this class. */ |
107 |
|
private |
108 |
|
CC() { } |
109 |
|
|
110 |
|
/** |
111 |
|
* Returns the logger to be used for logging events. |
112 |
|
* @return The logger to be used for logging events. |
113 |
|
*/ |
114 |
public static Logger |
public static Logger |
115 |
getLogger() { |
getLogger() { |
116 |
return Logger.getLogger ( |
return Logger.getLogger ( |
119 |
); |
); |
120 |
} |
} |
121 |
|
|
122 |
|
/** |
123 |
|
* Returns the task queue to be used for scheduling tasks |
124 |
|
* for execution out of the event-dispatching thread. |
125 |
|
* @return The task queue to be used for scheduling tasks |
126 |
|
* for execution out of the event-dispatching thread. |
127 |
|
*/ |
128 |
public static TaskQueue |
public static TaskQueue |
129 |
getTaskQueue() { return taskQueue; } |
getTaskQueue() { return taskQueue; } |
130 |
|
|
131 |
|
/** |
132 |
|
* Adds the specified task to the task queue. All task in the |
133 |
|
* queue equal to the specified task are removed from the queue. |
134 |
|
*/ |
135 |
|
public static void |
136 |
|
scheduleTask(Task t) { |
137 |
|
while(getTaskQueue().removeTask(t)) { } |
138 |
|
|
139 |
|
if(getTaskQueue().getPendingTaskCount() == 0) { |
140 |
|
if(t.equals(getTaskQueue().getRunningTask())) return; |
141 |
|
} |
142 |
|
|
143 |
|
getTaskQueue().add(t); |
144 |
|
} |
145 |
|
|
146 |
|
/** |
147 |
|
* Gets the configuration of the current view. |
148 |
|
*/ |
149 |
|
public static JSViewConfig |
150 |
|
getViewConfig() { return viewConfig; } |
151 |
|
|
152 |
|
private static JSPrefs |
153 |
|
preferences() { return getViewConfig().preferences(); } |
154 |
|
|
155 |
|
/** |
156 |
|
* Sets the configuration of the current view. |
157 |
|
*/ |
158 |
|
public static void |
159 |
|
setViewConfig(JSViewConfig viewConfig) { CC.viewConfig = viewConfig; } |
160 |
|
|
161 |
|
/** |
162 |
|
* Returns the main window of this application. |
163 |
|
* @return The main window of this application. |
164 |
|
*/ |
165 |
public static JSMainFrame |
public static JSMainFrame |
166 |
getMainFrame() { return mainFrame; } |
getMainFrame() { return mainFrame; } |
167 |
|
|
168 |
|
/** |
169 |
|
* Sets the main window of this application. |
170 |
|
* @param mainFrame The main window of this application. |
171 |
|
*/ |
172 |
public static void |
public static void |
173 |
setMainFrame(JSMainFrame mainFrame) { CC.mainFrame = mainFrame; } |
setMainFrame(JSMainFrame mainFrame) { CC.mainFrame = mainFrame; } |
174 |
|
|
175 |
|
/** |
176 |
|
* Gets the progress indicator of this application. |
177 |
|
* @return The progress indicator of this application. |
178 |
|
*/ |
179 |
public static JSProgress |
public static JSProgress |
180 |
getProgressIndicator() { return progress; } |
getProgressIndicator() { return progress; } |
181 |
|
|
182 |
|
/** |
183 |
|
* Sets the progress indicator to be used by this application. |
184 |
|
* @param progress The progress indicator to be used by this application. |
185 |
|
*/ |
186 |
public static void |
public static void |
187 |
setProgressIndicator(JSProgress progress) { CC.progress = progress; } |
setProgressIndicator(JSProgress progress) { CC.progress = progress; } |
188 |
|
|
189 |
|
/** |
190 |
|
* Gets the absolute path to the JSampler's home location. |
191 |
|
* @return The absolute path to the JSampler's home location |
192 |
|
* or <code>null</code> if the JSampler's home location is not specified yet. |
193 |
|
*/ |
194 |
|
public static String |
195 |
|
getJSamplerHome() { return jSamplerHome; } |
196 |
|
|
197 |
|
/** |
198 |
|
* Sets the location of the JSampler's home. |
199 |
|
* @param path The new absolute path to the JSampler's home location. |
200 |
|
*/ |
201 |
|
public static void |
202 |
|
setJSamplerHome(String path) { |
203 |
|
jSamplerHome = path; |
204 |
|
Prefs.setJSamplerHome(jSamplerHome); |
205 |
|
} |
206 |
|
|
207 |
|
/** |
208 |
|
* This method does the initial preparation of the application. |
209 |
|
*/ |
210 |
protected static void |
protected static void |
211 |
initJSampler() { |
initJSampler() { |
212 |
fos = null; |
fos = null; |
213 |
|
setJSamplerHome(Prefs.getJSamplerHome()); |
214 |
try { fos = new FileOutputStream("JSampler.log"); } |
String s = getJSamplerHome(); |
215 |
catch(Exception x) { x.printStackTrace(); } |
try { |
216 |
|
if(s != null) { |
217 |
|
s += File.separator + "jsampler.log"; |
218 |
|
File f = new File(s); |
219 |
|
if(f.isFile()) HF.createBackup("jsampler.log", "jsampler.log.0"); |
220 |
|
fos = new FileOutputStream(s); |
221 |
|
} |
222 |
|
} catch(Exception x) { x.printStackTrace(); } |
223 |
|
|
224 |
if(fos == null) handler = new StreamHandler(System.out, new SimpleFormatter()); |
if(fos == null) handler = new StreamHandler(System.out, new SimpleFormatter()); |
225 |
else handler = new StreamHandler(fos, new SimpleFormatter()); |
else handler = new StreamHandler(fos, new SimpleFormatter()); |
227 |
handler.setLevel(Level.FINE); |
handler.setLevel(Level.FINE); |
228 |
getLogger().addHandler(handler); |
getLogger().addHandler(handler); |
229 |
getLogger().setLevel(Level.FINE); |
getLogger().setLevel(Level.FINE); |
|
Logger.getLogger("org.linuxsampler.lscp").addHandler(handler); |
|
230 |
Logger.getLogger("org.linuxsampler.lscp").setLevel(Level.FINE); |
Logger.getLogger("org.linuxsampler.lscp").setLevel(Level.FINE); |
231 |
|
Logger.getLogger("org.linuxsampler.lscp").addHandler(handler); |
232 |
|
|
233 |
// Flushing logs on every second |
// Flushing logs on every second |
234 |
new java.util.Timer().schedule(new java.util.TimerTask() { |
new java.util.Timer().schedule(new java.util.TimerTask() { |
240 |
|
|
241 |
HF.setUIDefaultFont(Prefs.getInterfaceFont()); |
HF.setUIDefaultFont(Prefs.getInterfaceFont()); |
242 |
|
|
|
|
|
|
|
|
|
getClient().setServerAddress(Prefs.getLSAddress()); |
|
|
getClient().setServerPort(Prefs.getLSPort()); |
|
|
|
|
243 |
timer.setRepeats(false); |
timer.setRepeats(false); |
244 |
|
|
245 |
timer.addActionListener(new ActionListener() { |
timer.addActionListener(new ActionListener() { |
247 |
actionPerformed(ActionEvent e) { CC.getProgressIndicator().start(); } |
actionPerformed(ActionEvent e) { CC.getProgressIndicator().start(); } |
248 |
}); |
}); |
249 |
|
|
250 |
taskQueue.addTaskQueueListener(new TaskQueueListener() { |
taskQueue.addTaskQueueListener(getHandler()); |
|
public void |
|
|
stateChanged(TaskQueueEvent e) { |
|
|
switch(e.getEventID()) { |
|
|
case TASK_FETCHED: |
|
|
CC.getProgressIndicator().setString ( |
|
|
((Task)e.getSource()).getDescription() |
|
|
); |
|
|
break; |
|
|
case TASK_DONE: |
|
|
EnhancedTask t = (EnhancedTask)e.getSource(); |
|
|
if(t.doneWithErrors() && !t.isStopped()) |
|
|
HF.showErrorMessage(t.getErrorMessage()); |
|
|
break; |
|
|
case NOT_IDLE: |
|
|
timer.start(); |
|
|
break; |
|
|
case IDLE: |
|
|
timer.stop(); |
|
|
CC.getProgressIndicator().stop(); |
|
|
break; |
|
|
} |
|
|
} |
|
|
}); |
|
251 |
|
|
252 |
taskQueue.start(); |
taskQueue.start(); |
253 |
|
|
254 |
getClient().addChannelCountListener(eventHandler); |
getClient().removeChannelCountListener(getHandler()); |
255 |
getClient().addChannelInfoListener(eventHandler); |
getClient().addChannelCountListener(getHandler()); |
256 |
getClient().addStreamCountListener(eventHandler); |
|
257 |
getClient().addVoiceCountListener(eventHandler); |
getClient().removeChannelInfoListener(getHandler()); |
258 |
getClient().addTotalVoiceCountListener(eventHandler); |
getClient().addChannelInfoListener(getHandler()); |
259 |
|
|
260 |
|
getClient().removeFxSendCountListener(getHandler()); |
261 |
|
getClient().addFxSendCountListener(getHandler()); |
262 |
|
|
263 |
|
getClient().removeFxSendInfoListener(getHandler()); |
264 |
|
getClient().addFxSendInfoListener(getHandler()); |
265 |
|
|
266 |
|
getClient().removeStreamCountListener(getHandler()); |
267 |
|
getClient().addStreamCountListener(getHandler()); |
268 |
|
|
269 |
|
getClient().removeVoiceCountListener(getHandler()); |
270 |
|
getClient().addVoiceCountListener(getHandler()); |
271 |
|
|
272 |
|
getClient().removeTotalStreamCountListener(getHandler()); |
273 |
|
getClient().addTotalStreamCountListener(getHandler()); |
274 |
|
|
275 |
|
getClient().removeTotalVoiceCountListener(getHandler()); |
276 |
|
getClient().addTotalVoiceCountListener(getHandler()); |
277 |
|
|
278 |
|
getClient().removeAudioDeviceCountListener(audioDeviceCountListener); |
279 |
|
getClient().addAudioDeviceCountListener(audioDeviceCountListener); |
280 |
|
|
281 |
|
getClient().removeAudioDeviceInfoListener(audioDeviceInfoListener); |
282 |
|
getClient().addAudioDeviceInfoListener(audioDeviceInfoListener); |
283 |
|
|
284 |
|
getClient().removeMidiDeviceCountListener(midiDeviceCountListener); |
285 |
|
getClient().addMidiDeviceCountListener(midiDeviceCountListener); |
286 |
|
|
287 |
|
getClient().removeMidiDeviceInfoListener(midiDeviceInfoListener); |
288 |
|
getClient().addMidiDeviceInfoListener(midiDeviceInfoListener); |
289 |
|
|
290 |
|
getClient().removeMidiInstrumentMapCountListener(midiInstrMapCountListener); |
291 |
|
getClient().addMidiInstrumentMapCountListener(midiInstrMapCountListener); |
292 |
|
|
293 |
|
getClient().removeMidiInstrumentMapInfoListener(midiInstrMapInfoListener); |
294 |
|
getClient().addMidiInstrumentMapInfoListener(midiInstrMapInfoListener); |
295 |
|
|
296 |
|
getClient().removeMidiInstrumentCountListener(getHandler()); |
297 |
|
getClient().addMidiInstrumentCountListener(getHandler()); |
298 |
|
|
299 |
|
getClient().removeMidiInstrumentInfoListener(getHandler()); |
300 |
|
getClient().addMidiInstrumentInfoListener(getHandler()); |
301 |
|
|
302 |
|
getClient().removeGlobalInfoListener(getHandler()); |
303 |
|
getClient().addGlobalInfoListener(getHandler()); |
304 |
|
} |
305 |
|
|
306 |
|
/** |
307 |
|
* Checks whether the JSampler home directory is specified and exist. |
308 |
|
* If the JSampler home directory is not specifed, or is specified |
309 |
|
* but doesn't exist, a procedure of specifying a JSampler home |
310 |
|
* directory is initiated. |
311 |
|
* @see org.jsampler.view.JSMainFrame#installJSamplerHome |
312 |
|
*/ |
313 |
|
public static void |
314 |
|
checkJSamplerHome() { |
315 |
|
if(getJSamplerHome() != null) { |
316 |
|
File f = new File(getJSamplerHome()); |
317 |
|
if(f.exists() && f.isDirectory()) { |
318 |
|
return; |
319 |
|
} |
320 |
|
} |
321 |
|
|
322 |
|
CC.getMainFrame().installJSamplerHome(); |
323 |
|
} |
324 |
|
|
325 |
|
/** |
326 |
|
* Changes the JSampler's home directory and moves all files from |
327 |
|
* the old JSampler's home directory to the new one. If all files are |
328 |
|
* moved succesfully, the old directory is deleted. |
329 |
|
* @param path The location of the new JSampler's home directory. If |
330 |
|
* the last directory in the path doesn't exist, it is created. |
331 |
|
*/ |
332 |
|
public static void |
333 |
|
changeJSamplerHome(String path) { |
334 |
|
File fNew = new File(path); |
335 |
|
if(fNew.exists() && fNew.isFile()) { |
336 |
|
HF.showErrorMessage(i18n.getError("CC.JSamplerHomeIsNotDir!")); |
337 |
|
return; |
338 |
|
} |
339 |
|
|
340 |
|
if(!fNew.exists()) { |
341 |
|
if(!fNew.mkdir()) { |
342 |
|
String s = fNew.getAbsolutePath(); |
343 |
|
HF.showErrorMessage(i18n.getError("CC.mkdirFailed", s)); |
344 |
|
return; |
345 |
|
} |
346 |
|
} |
347 |
|
|
348 |
|
if(getJSamplerHome() == null || path.equals(getJSamplerHome())) { |
349 |
|
setJSamplerHome(fNew.getAbsolutePath()); |
350 |
|
return; |
351 |
|
} |
352 |
|
|
353 |
|
File fOld = new File(getJSamplerHome()); |
354 |
|
if(!fOld.exists() || !fOld.isDirectory()) { |
355 |
|
setJSamplerHome(fNew.getAbsolutePath()); |
356 |
|
return; |
357 |
|
} |
358 |
|
|
359 |
|
File[] files = fOld.listFiles(); |
360 |
|
boolean b = true; |
361 |
|
if(files != null) { |
362 |
|
String s = fNew.getAbsolutePath() + File.separator; |
363 |
|
for(File f : files) if(!f.renameTo(new File(s + f.getName()))) b = false; |
364 |
|
} |
365 |
|
|
366 |
|
if(b) fOld.delete(); |
367 |
|
setJSamplerHome(fNew.getAbsolutePath()); |
368 |
|
} |
369 |
|
|
370 |
|
private final static OrchestraListModel orchestras = new DefaultOrchestraListModel(); |
371 |
|
|
372 |
|
/** |
373 |
|
* Returns a list containing all available orchestras. |
374 |
|
* @return A list containing all available orchestras. |
375 |
|
*/ |
376 |
|
public static OrchestraListModel |
377 |
|
getOrchestras() { return orchestras; } |
378 |
|
|
379 |
|
private final static ServerList servers = new ServerList(); |
380 |
|
|
381 |
|
/** Returns the server list. */ |
382 |
|
public static ServerList |
383 |
|
getServerList() { return servers; } |
384 |
|
|
385 |
|
private static ServerListListener serverListListener = new ServerListListener(); |
386 |
|
|
387 |
|
private static class ServerListListener implements ChangeListener { |
388 |
|
public void |
389 |
|
stateChanged(ChangeEvent e) { |
390 |
|
saveServerList(); |
391 |
|
} |
392 |
|
} |
393 |
|
|
394 |
|
private static final Vector<ChangeListener> idtmListeners = new Vector<ChangeListener>(); |
395 |
|
private static InstrumentsDbTreeModel instrumentsDbTreeModel = null; |
396 |
|
|
397 |
|
/** |
398 |
|
* Gets the tree model of the instruments database. |
399 |
|
* If the currently used view doesn't have instruments |
400 |
|
* database support the tree model is initialized on first use. |
401 |
|
* @return The tree model of the instruments database or |
402 |
|
* <code>null</code> if the backend doesn't have instruments database support. |
403 |
|
* @see org.jsampler.view.JSViewConfig#getInstrumentsDbSupport |
404 |
|
*/ |
405 |
|
public static InstrumentsDbTreeModel |
406 |
|
getInstrumentsDbTreeModel() { |
407 |
|
if(!CC.getSamplerModel().getServerInfo().hasInstrumentsDbSupport()) return null; |
408 |
|
|
409 |
|
if(instrumentsDbTreeModel == null) { |
410 |
|
instrumentsDbTreeModel = new InstrumentsDbTreeModel(); |
411 |
|
for(ChangeListener l : idtmListeners) l.stateChanged(null); |
412 |
|
} |
413 |
|
|
414 |
|
return instrumentsDbTreeModel; |
415 |
|
} |
416 |
|
|
417 |
|
public static void |
418 |
|
addInstrumentsDbChangeListener(ChangeListener l) { |
419 |
|
idtmListeners.add(l); |
420 |
|
} |
421 |
|
|
422 |
|
public static void |
423 |
|
removeInstrumentsDbChangeListener(ChangeListener l) { |
424 |
|
idtmListeners.remove(l); |
425 |
|
} |
426 |
|
|
427 |
|
/** |
428 |
|
* Loads the orchestras described in <code><jsampler_home>/orchestras.xml</code>. |
429 |
|
* If file with name <code>orchestras.xml.bkp</code> exist in the JSampler's home |
430 |
|
* directory, this means that the last save has failed. In that case a recovery file |
431 |
|
* <code>orchestras.xml.rec</code> is created and a recovery procedure |
432 |
|
* will be initiated. |
433 |
|
*/ |
434 |
|
public static void |
435 |
|
loadOrchestras() { |
436 |
|
if(getJSamplerHome() == null) return; |
437 |
|
|
438 |
|
try { |
439 |
|
String s = getJSamplerHome(); |
440 |
|
|
441 |
|
File f = new File(s + File.separator + "orchestras.xml.bkp"); |
442 |
|
if(f.isFile()) HF.createBackup("orchestras.xml.bkp", "orchestras.xml.rec"); |
443 |
|
|
444 |
|
FileInputStream fis; |
445 |
|
fis = new FileInputStream(s + File.separator + "orchestras.xml"); |
446 |
|
|
447 |
|
loadOrchestras(fis); |
448 |
|
fis.close(); |
449 |
|
} catch(Exception x) { |
450 |
|
getLogger().log(Level.INFO, HF.getErrorMessage(x), x); |
451 |
|
} |
452 |
|
|
453 |
|
getOrchestras().addOrchestraListListener(getHandler()); |
454 |
|
} |
455 |
|
|
456 |
|
|
457 |
|
private static void |
458 |
|
loadOrchestras(InputStream in) { |
459 |
|
Document doc = DOMUtils.readObject(in); |
460 |
|
|
461 |
|
try { getOrchestras().readObject(doc.getDocumentElement()); } |
462 |
|
catch(Exception x) { |
463 |
|
HF.showErrorMessage(x, "Loading orchestras: "); |
464 |
|
return; |
465 |
|
} |
466 |
|
|
467 |
|
for(int i = 0; i < getOrchestras().getOrchestraCount(); i++) { |
468 |
|
getOrchestras().getOrchestra(i).addOrchestraListener(getHandler()); |
469 |
|
} |
470 |
|
} |
471 |
|
|
472 |
|
private static void |
473 |
|
saveOrchestras() { |
474 |
|
try { |
475 |
|
String s = getJSamplerHome(); |
476 |
|
if(s == null) return; |
477 |
|
|
478 |
|
HF.createBackup("orchestras.xml", "orchestras.xml.bkp"); |
479 |
|
|
480 |
|
FileOutputStream fos; |
481 |
|
fos = new FileOutputStream(s + File.separator + "orchestras.xml", false); |
482 |
|
|
483 |
|
Document doc = DOMUtils.createEmptyDocument(); |
484 |
|
|
485 |
|
Node node = doc.createElement("temp"); |
486 |
|
doc.appendChild(node); |
487 |
|
|
488 |
|
getOrchestras().writeObject(doc, doc.getDocumentElement()); |
489 |
|
|
490 |
|
doc.replaceChild(node.getFirstChild(), node); |
491 |
|
|
492 |
|
DOMUtils.writeObject(doc, fos); |
493 |
|
|
494 |
|
fos.close(); |
495 |
|
|
496 |
|
HF.deleteFile("orchestras.xml.bkp"); |
497 |
|
} catch(Exception x) { |
498 |
|
HF.showErrorMessage(x, "Saving orchestras: "); |
499 |
|
return; |
500 |
|
} |
501 |
} |
} |
502 |
|
|
503 |
|
/** |
504 |
|
* Loads the servers' info described in <code><jsampler_home>/servers.xml</code>. |
505 |
|
* If file with name <code>servers.xml.bkp</code> exist in the JSampler's home |
506 |
|
* directory, this means that the last save has failed. In that case a recovery file |
507 |
|
* <code>servers.xml.rec</code> is created and a recovery procedure |
508 |
|
* will be initiated. |
509 |
|
*/ |
510 |
|
public static void |
511 |
|
loadServerList() { |
512 |
|
if(getJSamplerHome() == null) return; |
513 |
|
|
514 |
|
try { |
515 |
|
String s = getJSamplerHome(); |
516 |
|
|
517 |
|
File f = new File(s + File.separator + "servers.xml.bkp"); |
518 |
|
if(f.isFile()) HF.createBackup("servers.xml.bkp", "servers.xml.rec"); |
519 |
|
|
520 |
|
FileInputStream fis; |
521 |
|
fis = new FileInputStream(s + File.separator + "servers.xml"); |
522 |
|
|
523 |
|
loadServerList(fis); |
524 |
|
fis.close(); |
525 |
|
} catch(Exception x) { |
526 |
|
getLogger().log(Level.INFO, HF.getErrorMessage(x), x); |
527 |
|
} |
528 |
|
|
529 |
|
getServerList().addChangeListener(serverListListener); |
530 |
|
|
531 |
|
/* We should have at least one server to connect. */ |
532 |
|
if(getServerList().getServerCount() == 0) { |
533 |
|
Server server = new Server(); |
534 |
|
server.setName("127.0.0.1:8888"); |
535 |
|
server.setAddress("127.0.0.1"); |
536 |
|
server.setPort(8888); |
537 |
|
getServerList().addServer(server); |
538 |
|
} |
539 |
|
} |
540 |
|
|
541 |
|
|
542 |
|
private static void |
543 |
|
loadServerList(InputStream in) { |
544 |
|
Document doc = DOMUtils.readObject(in); |
545 |
|
|
546 |
|
try { getServerList().readObject(doc.getDocumentElement()); } |
547 |
|
catch(Exception x) { |
548 |
|
HF.showErrorMessage(x, "Loading server list: "); |
549 |
|
return; |
550 |
|
} |
551 |
|
} |
552 |
|
|
553 |
|
private static void |
554 |
|
saveServerList() { |
555 |
|
try { |
556 |
|
String s = getJSamplerHome(); |
557 |
|
if(s == null) return; |
558 |
|
|
559 |
|
HF.createBackup("servers.xml", "servers.xml.bkp"); |
560 |
|
|
561 |
|
FileOutputStream fos; |
562 |
|
fos = new FileOutputStream(s + File.separator + "servers.xml", false); |
563 |
|
|
564 |
|
Document doc = DOMUtils.createEmptyDocument(); |
565 |
|
|
566 |
|
Node node = doc.createElement("temp"); |
567 |
|
doc.appendChild(node); |
568 |
|
|
569 |
|
getServerList().writeObject(doc, doc.getDocumentElement()); |
570 |
|
|
571 |
|
doc.replaceChild(node.getFirstChild(), node); |
572 |
|
|
573 |
|
DOMUtils.writeObject(doc, fos); |
574 |
|
|
575 |
|
fos.close(); |
576 |
|
|
577 |
|
HF.deleteFile("servers.xml.bkp"); |
578 |
|
} catch(Exception x) { |
579 |
|
HF.showErrorMessage(x, "Saving server list: "); |
580 |
|
return; |
581 |
|
} |
582 |
|
} |
583 |
|
|
584 |
|
/** |
585 |
|
* The exit point of the application which ensures clean exit with default exit status 0. |
586 |
|
* @see #cleanExit(int i) |
587 |
|
*/ |
588 |
public static void |
public static void |
589 |
cleanExit() { cleanExit(0); } |
cleanExit() { cleanExit(0); } |
590 |
|
|
591 |
|
/** |
592 |
|
* The exit point of the application which ensures clean exit. |
593 |
|
* @param i The exit status. |
594 |
|
*/ |
595 |
public static void |
public static void |
596 |
cleanExit(int i) { |
cleanExit(int i) { |
597 |
CC.getLogger().fine("CC.jsEnded"); |
CC.getLogger().fine("CC.jsEnded"); |
598 |
System.exit(i); |
System.exit(i); |
599 |
} |
} |
600 |
|
|
601 |
|
/** |
602 |
|
* Gets the <code>Client</code> object that is used to communicate with the backend. |
603 |
|
* @return The <code>Client</code> object that is used to communicate with the backend. |
604 |
|
*/ |
605 |
public static Client |
public static Client |
606 |
getClient() { return lsClient; } |
getClient() { return lsClient; } |
607 |
|
|
608 |
|
private static final Vector<ActionListener> listeners = new Vector<ActionListener>(); |
609 |
|
|
610 |
|
/** |
611 |
|
* Registers the specified listener to be notified when reconnecting to LinuxSampler. |
612 |
|
* @param l The <code>ActionListener</code> to register. |
613 |
|
*/ |
614 |
|
public static void |
615 |
|
addReconnectListener(ActionListener l) { listeners.add(l); } |
616 |
|
|
617 |
|
/** |
618 |
|
* Removes the specified listener. |
619 |
|
* @param l The <code>ActionListener</code> to remove. |
620 |
|
*/ |
621 |
|
public static void |
622 |
|
removeReconnectListener(ActionListener l) { listeners.remove(l); } |
623 |
|
|
624 |
|
private static void |
625 |
|
fireReconnectEvent() { |
626 |
|
ActionEvent e = new ActionEvent(CC.class, ActionEvent.ACTION_PERFORMED, null); |
627 |
|
for(ActionListener l : listeners) l.actionPerformed(e); |
628 |
|
} |
629 |
|
|
630 |
private static final SamplerModel samplerModel = new DefaultSamplerModel(); |
private static final SamplerModel samplerModel = new DefaultSamplerModel(); |
631 |
|
|
636 |
public static SamplerModel |
public static SamplerModel |
637 |
getSamplerModel() { return samplerModel; } |
getSamplerModel() { return samplerModel; } |
638 |
|
|
639 |
|
/** |
640 |
|
* Connects to LinuxSampler. |
641 |
|
*/ |
642 |
public static void |
public static void |
643 |
|
connect() { initSamplerModel(); } |
644 |
|
|
645 |
|
/** |
646 |
|
* Reconnects to LinuxSampler. |
647 |
|
*/ |
648 |
|
public static void |
649 |
|
reconnect() { initSamplerModel(getCurrentServer()); } |
650 |
|
|
651 |
|
private static Server currentServer = null; |
652 |
|
|
653 |
|
/** |
654 |
|
* Gets the server, to which the frontend is going to connect |
655 |
|
* or is already connected. |
656 |
|
*/ |
657 |
|
public static Server |
658 |
|
getCurrentServer() { return currentServer; } |
659 |
|
|
660 |
|
/** |
661 |
|
* Sets the current server. |
662 |
|
*/ |
663 |
|
public static void |
664 |
|
setCurrentServer(Server server) { currentServer = server; } |
665 |
|
|
666 |
|
/** |
667 |
|
* This method updates the information about the backend state. |
668 |
|
*/ |
669 |
|
private static void |
670 |
initSamplerModel() { |
initSamplerModel() { |
671 |
|
Server srv = getMainFrame().getServer(); |
672 |
|
if(srv == null) return; |
673 |
|
initSamplerModel(srv); |
674 |
|
} |
675 |
|
|
676 |
|
/** |
677 |
|
* This method updates the information about the backend state. |
678 |
|
*/ |
679 |
|
private static void |
680 |
|
initSamplerModel(Server srv) { |
681 |
|
setCurrentServer(srv); |
682 |
|
final SetServerAddress ssa = new SetServerAddress(srv.getAddress(), srv.getPort()); |
683 |
|
|
684 |
final DefaultSamplerModel model = (DefaultSamplerModel)getSamplerModel(); |
final DefaultSamplerModel model = (DefaultSamplerModel)getSamplerModel(); |
685 |
|
|
686 |
final GetServerInfo gsi = new GetServerInfo(); |
final Global.GetServerInfo gsi = new Global.GetServerInfo(); |
687 |
gsi.addTaskListener(new TaskListener() { |
gsi.addTaskListener(new TaskListener() { |
688 |
public void |
public void |
689 |
taskPerformed(TaskEvent e) { |
taskPerformed(TaskEvent e) { |
690 |
if(!gsi.doneWithErrors()) model.setServerInfo(gsi.getResult()); |
if(gsi.doneWithErrors()) return; |
691 |
|
|
692 |
|
model.setServerInfo(gsi.getResult()); |
693 |
|
|
694 |
|
if(CC.getViewConfig().getInstrumentsDbSupport()) { |
695 |
|
getInstrumentsDbTreeModel(); |
696 |
|
} |
697 |
} |
} |
698 |
}); |
}); |
699 |
|
|
700 |
final GetAODrivers gaod = new GetAODrivers(); |
final Audio.GetDrivers gaod = new Audio.GetDrivers(); |
701 |
gaod.addTaskListener(new TaskListener() { |
gaod.addTaskListener(new TaskListener() { |
702 |
public void |
public void |
703 |
taskPerformed(TaskEvent e) { |
taskPerformed(TaskEvent e) { |
714 |
} |
} |
715 |
}); |
}); |
716 |
|
|
717 |
final GetMIDrivers gmid = new GetMIDrivers(); |
final Midi.GetDrivers gmid = new Midi.GetDrivers(); |
718 |
gmid.addTaskListener(new TaskListener() { |
gmid.addTaskListener(new TaskListener() { |
719 |
public void |
public void |
720 |
taskPerformed(TaskEvent e) { |
taskPerformed(TaskEvent e) { |
723 |
} |
} |
724 |
}); |
}); |
725 |
|
|
726 |
|
final Global.GetVolume gv = new Global.GetVolume(); |
727 |
|
gv.addTaskListener(new TaskListener() { |
728 |
|
public void |
729 |
|
taskPerformed(TaskEvent e) { |
730 |
|
if(!gv.doneWithErrors()) |
731 |
|
model.setVolume(gv.getResult()); |
732 |
|
} |
733 |
|
}); |
734 |
|
|
735 |
|
final Midi.GetInstrumentMaps mgim = new Midi.GetInstrumentMaps(); |
736 |
|
mgim.addTaskListener(new TaskListener() { |
737 |
|
public void |
738 |
|
taskPerformed(TaskEvent e) { |
739 |
|
if(mgim.doneWithErrors()) return; |
740 |
|
model.removeAllMidiInstrumentMaps(); |
741 |
|
|
742 |
|
for(MidiInstrumentMap map : mgim.getResult()) { |
743 |
|
model.addMidiInstrumentMap(map); |
744 |
|
} |
745 |
|
} |
746 |
|
}); |
747 |
|
|
748 |
|
final UpdateChannels uc = new UpdateChannels(); |
749 |
|
uc.addTaskListener(new TaskListener() { |
750 |
|
public void |
751 |
|
taskPerformed(TaskEvent e) { |
752 |
|
for(SamplerChannelModel c : model.getChannels()) { |
753 |
|
if(c.getChannelInfo().getEngine() == null) continue; |
754 |
|
|
755 |
|
Channel.GetFxSends gfs = new Channel.GetFxSends(); |
756 |
|
gfs.setChannel(c.getChannelId()); |
757 |
|
gfs.addTaskListener(new GetFxSendsListener()); |
758 |
|
getTaskQueue().add(gfs); |
759 |
|
} |
760 |
|
|
761 |
|
// TODO: This should be done after the fx sends are set |
762 |
|
//CC.getSamplerModel().setModified(false); |
763 |
|
} |
764 |
|
}); |
765 |
|
|
766 |
|
|
767 |
final Connect cnt = new Connect(); |
final Connect cnt = new Connect(); |
768 |
cnt.addTaskListener(new TaskListener() { |
cnt.addTaskListener(new TaskListener() { |
769 |
public void |
public void |
770 |
taskPerformed(TaskEvent e) { |
taskPerformed(TaskEvent e) { |
771 |
if(cnt.doneWithErrors()) return; |
if(cnt.doneWithErrors()) { |
772 |
|
setCurrentServer(null); |
773 |
|
retryToConnect(); |
774 |
|
return; |
775 |
|
} |
776 |
|
|
777 |
getTaskQueue().add(gsi); |
getTaskQueue().add(gsi); |
778 |
getTaskQueue().add(gaod); |
getTaskQueue().add(gaod); |
779 |
getTaskQueue().add(gmid); |
getTaskQueue().add(gmid); |
780 |
getTaskQueue().add(ge); |
getTaskQueue().add(ge); |
781 |
getTaskQueue().add(new UpdateMidiDevices()); |
getTaskQueue().add(gv); |
782 |
getTaskQueue().add(new UpdateAudioDevices()); |
getTaskQueue().add(mgim); |
783 |
getTaskQueue().add(new UpdateChannels()); |
getTaskQueue().add(new Midi.UpdateDevices()); |
784 |
|
getTaskQueue().add(new Audio.UpdateDevices()); |
785 |
|
getTaskQueue().add(uc); |
786 |
} |
} |
787 |
}); |
}); |
788 |
getTaskQueue().add(cnt); |
|
789 |
|
ssa.addTaskListener(new TaskListener() { |
790 |
|
public void |
791 |
|
taskPerformed(TaskEvent e) { |
792 |
|
CC.getTaskQueue().add(cnt); |
793 |
|
} |
794 |
|
}); |
795 |
|
|
796 |
|
getSamplerModel().reset(); |
797 |
|
if(instrumentsDbTreeModel != null) { |
798 |
|
instrumentsDbTreeModel.reset(); |
799 |
|
instrumentsDbTreeModel = null; |
800 |
|
} |
801 |
|
|
802 |
|
getTaskQueue().removePendingTasks(); |
803 |
|
getTaskQueue().add(ssa); |
804 |
|
|
805 |
|
fireReconnectEvent(); |
806 |
|
} |
807 |
|
|
808 |
|
private static void |
809 |
|
retryToConnect() { |
810 |
|
javax.swing.SwingUtilities.invokeLater(new Runnable() { |
811 |
|
public void |
812 |
|
run() { changeBackend(); } |
813 |
|
}); |
814 |
|
} |
815 |
|
|
816 |
|
public static void |
817 |
|
changeBackend() { |
818 |
|
Server s = getMainFrame().getServer(true); |
819 |
|
if(s != null) initSamplerModel(s); |
820 |
|
} |
821 |
|
|
822 |
|
private static class GetFxSendsListener implements TaskListener { |
823 |
|
public void |
824 |
|
taskPerformed(TaskEvent e) { |
825 |
|
Channel.GetFxSends gfs = (Channel.GetFxSends)e.getSource(); |
826 |
|
if(gfs.doneWithErrors()) return; |
827 |
|
SamplerChannelModel m = getSamplerModel().getChannelById(gfs.getChannel()); |
828 |
|
m.removeAllFxSends(); |
829 |
|
|
830 |
|
for(FxSend fxs : gfs.getResult()) m.addFxSend(fxs); |
831 |
|
} |
832 |
|
} |
833 |
|
|
834 |
|
public static String |
835 |
|
exportInstrMapsToLscpScript() { |
836 |
|
StringBuffer sb = new StringBuffer("# Exported by: "); |
837 |
|
sb.append("JSampler - a java front-end for LinuxSampler\r\n# Version: "); |
838 |
|
sb.append(JSampler.VERSION).append("\r\n"); |
839 |
|
sb.append("# Date: ").append(new java.util.Date().toString()).append("\r\n\r\n"); |
840 |
|
|
841 |
|
Client lscpClient = new Client(true); |
842 |
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
843 |
|
lscpClient.setPrintOnlyModeOutputStream(out); |
844 |
|
|
845 |
|
exportInstrMapsToLscpScript(lscpClient); |
846 |
|
sb.append(out.toString()); |
847 |
|
out.reset(); |
848 |
|
|
849 |
|
return sb.toString(); |
850 |
|
} |
851 |
|
|
852 |
|
private static void |
853 |
|
exportInstrMapsToLscpScript(Client lscpClient) { |
854 |
|
try { |
855 |
|
lscpClient.removeAllMidiInstrumentMaps(); |
856 |
|
MidiInstrumentMap[] maps = CC.getSamplerModel().getMidiInstrumentMaps(); |
857 |
|
for(int i = 0; i < maps.length; i++) { |
858 |
|
lscpClient.addMidiInstrumentMap(maps[i].getName()); |
859 |
|
exportInstrumentsToLscpScript(i, maps[i], lscpClient); |
860 |
|
} |
861 |
|
} catch(Exception e) { |
862 |
|
CC.getLogger().log(Level.FINE, HF.getErrorMessage(e), e); |
863 |
|
HF.showErrorMessage(e); |
864 |
|
} |
865 |
} |
} |
866 |
|
|
867 |
|
private static void |
868 |
|
exportInstrumentsToLscpScript(int mapId, MidiInstrumentMap map, Client lscpClient) |
869 |
|
throws Exception { |
870 |
|
|
871 |
|
for(MidiInstrument i : map.getAllMidiInstruments()) { |
872 |
|
lscpClient.mapMidiInstrument(mapId, i.getInfo().getEntry(), i.getInfo()); |
873 |
|
} |
874 |
|
} |
875 |
|
|
876 |
|
public static String |
877 |
|
exportSessionToLscpScript() { |
878 |
|
CC.getSamplerModel().setModified(false); |
879 |
|
|
880 |
|
StringBuffer sb = new StringBuffer("# Exported by: "); |
881 |
|
sb.append("JSampler - a java front-end for LinuxSampler\r\n# Version: "); |
882 |
|
sb.append(JSampler.VERSION).append("\r\n"); |
883 |
|
sb.append("# Date: ").append(new java.util.Date().toString()).append("\r\n\r\n"); |
884 |
|
|
885 |
|
Client lscpClient = new Client(true); |
886 |
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
887 |
|
lscpClient.setPrintOnlyModeOutputStream(out); |
888 |
|
|
889 |
|
try { |
890 |
|
lscpClient.resetSampler(); |
891 |
|
sb.append(out.toString()); |
892 |
|
out.reset(); |
893 |
|
sb.append("\r\n"); |
894 |
|
lscpClient.setVolume(CC.getSamplerModel().getVolume()); |
895 |
|
sb.append(out.toString()); |
896 |
|
out.reset(); |
897 |
|
sb.append("\r\n"); |
898 |
|
} catch(Exception e) { CC.getLogger().log(Level.FINE, HF.getErrorMessage(e), e); } |
899 |
|
|
900 |
|
MidiDeviceModel[] mDevs = getSamplerModel().getMidiDevices(); |
901 |
|
for(int i = 0; i < mDevs.length; i++) { |
902 |
|
exportMidiDeviceToLscpScript(mDevs[i].getDeviceInfo(), i, lscpClient); |
903 |
|
sb.append(out.toString()); |
904 |
|
out.reset(); |
905 |
|
sb.append("\r\n"); |
906 |
|
} |
907 |
|
|
908 |
|
AudioDeviceModel[] aDevs = getSamplerModel().getAudioDevices(); |
909 |
|
for(int i = 0; i < aDevs.length; i++) { |
910 |
|
exportAudioDeviceToLscpScript(aDevs[i].getDeviceInfo(), i, lscpClient); |
911 |
|
sb.append(out.toString()); |
912 |
|
out.reset(); |
913 |
|
sb.append("\r\n"); |
914 |
|
} |
915 |
|
|
916 |
|
exportInstrMapsToLscpScript(lscpClient); |
917 |
|
sb.append(out.toString()); |
918 |
|
out.reset(); |
919 |
|
|
920 |
|
SamplerChannelModel[] channels = getSamplerModel().getChannels(); |
921 |
|
|
922 |
|
for(int i = 0; i < channels.length; i++) { |
923 |
|
SamplerChannelModel scm = channels[i]; |
924 |
|
exportChannelToLscpScript(scm.getChannelInfo(), i, lscpClient); |
925 |
|
sb.append(out.toString()); |
926 |
|
out.reset(); |
927 |
|
|
928 |
|
sb.append("\r\n"); |
929 |
|
|
930 |
|
exportFxSendsToLscpScript(scm, i, lscpClient); |
931 |
|
sb.append(out.toString()); |
932 |
|
out.reset(); |
933 |
|
|
934 |
|
sb.append("\r\n"); |
935 |
|
} |
936 |
|
|
937 |
|
return sb.toString(); |
938 |
|
} |
939 |
|
|
940 |
|
private static void |
941 |
|
exportMidiDeviceToLscpScript(MidiInputDevice mid, int devId, Client lscpCLient) { |
942 |
|
try { |
943 |
|
String s = mid.getDriverName(); |
944 |
|
lscpCLient.createMidiInputDevice(s, mid.getAdditionalParameters()); |
945 |
|
|
946 |
|
MidiPort[] mPorts = mid.getMidiPorts(); |
947 |
|
int l = mPorts.length; |
948 |
|
if(l != 1) lscpCLient.setMidiInputPortCount(devId, l); |
949 |
|
|
950 |
|
for(int i = 0; i < l; i++) { |
951 |
|
Parameter[] prms = mPorts[i].getAllParameters(); |
952 |
|
for(Parameter p : prms) { |
953 |
|
if(!p.isFixed() && p.getStringValue().length() > 0) |
954 |
|
lscpCLient.setMidiInputPortParameter(devId, i, p); |
955 |
|
} |
956 |
|
} |
957 |
|
} catch(Exception e) { |
958 |
|
CC.getLogger().log(Level.FINE, HF.getErrorMessage(e), e); |
959 |
|
} |
960 |
|
} |
961 |
|
|
962 |
|
private static void |
963 |
|
exportAudioDeviceToLscpScript(AudioOutputDevice aod, int devId, Client lscpCLient) { |
964 |
|
try { |
965 |
|
String s = aod.getDriverName(); |
966 |
|
lscpCLient.createAudioOutputDevice(s, aod.getAllParameters()); |
967 |
|
|
968 |
|
AudioOutputChannel[] chns = aod.getAudioChannels(); |
969 |
|
|
970 |
|
for(int i = 0; i < chns.length; i++) { |
971 |
|
Parameter[] prms = chns[i].getAllParameters(); |
972 |
|
for(Parameter p : prms) { |
973 |
|
if(p.isFixed() || p.getStringValue().length() == 0); |
974 |
|
else lscpCLient.setAudioOutputChannelParameter(devId, i, p); |
975 |
|
} |
976 |
|
} |
977 |
|
} catch(Exception e) { |
978 |
|
CC.getLogger().log(Level.FINE, HF.getErrorMessage(e), e); |
979 |
|
} |
980 |
|
} |
981 |
|
|
982 |
|
private static void |
983 |
|
exportChannelToLscpScript(SamplerChannel chn, int chnId, Client lscpCLient) { |
984 |
|
try { |
985 |
|
lscpCLient.addSamplerChannel(); |
986 |
|
|
987 |
|
SamplerModel sm = CC.getSamplerModel(); |
988 |
|
int id = chn.getMidiInputDevice(); |
989 |
|
if(id != -1) { |
990 |
|
for(int i = 0; i < sm.getMidiDeviceCount(); i++) { |
991 |
|
if(sm.getMidiDevice(i).getDeviceId() == id) { |
992 |
|
lscpCLient.setChannelMidiInputDevice(chnId, i); |
993 |
|
break; |
994 |
|
} |
995 |
|
} |
996 |
|
lscpCLient.setChannelMidiInputPort(chnId, chn.getMidiInputPort()); |
997 |
|
lscpCLient.setChannelMidiInputChannel(chnId, chn.getMidiInputChannel()); |
998 |
|
} |
999 |
|
|
1000 |
|
if(chn.getEngine() != null) { |
1001 |
|
lscpCLient.loadSamplerEngine(chn.getEngine().getName(), chnId); |
1002 |
|
lscpCLient.setChannelVolume(chnId, chn.getVolume()); |
1003 |
|
int mapId = chn.getMidiInstrumentMapId(); |
1004 |
|
lscpCLient.setChannelMidiInstrumentMap(chnId, mapId); |
1005 |
|
} |
1006 |
|
|
1007 |
|
id = chn.getAudioOutputDevice(); |
1008 |
|
if(id != -1) { |
1009 |
|
for(int i = 0; i < sm.getAudioDeviceCount(); i++) { |
1010 |
|
if(sm.getAudioDevice(i).getDeviceId() == id) { |
1011 |
|
lscpCLient.setChannelAudioOutputDevice(chnId, i); |
1012 |
|
break; |
1013 |
|
} |
1014 |
|
} |
1015 |
|
|
1016 |
|
Integer[] routing = chn.getAudioOutputRouting(); |
1017 |
|
|
1018 |
|
for(int j = 0; j < routing.length; j++) { |
1019 |
|
int k = routing[j]; |
1020 |
|
if(k == j) continue; |
1021 |
|
|
1022 |
|
lscpCLient.setChannelAudioOutputChannel(chnId, j, k); |
1023 |
|
} |
1024 |
|
} |
1025 |
|
|
1026 |
|
String s = chn.getInstrumentFile(); |
1027 |
|
int i = chn.getInstrumentIndex(); |
1028 |
|
if(s != null) lscpCLient.loadInstrument(s, i, chnId, true); |
1029 |
|
|
1030 |
|
if(chn.isMuted()) lscpCLient.setChannelMute(chnId, true); |
1031 |
|
if(chn.isSoloChannel()) lscpCLient.setChannelSolo(chnId, true); |
1032 |
|
} catch(Exception e) { |
1033 |
|
CC.getLogger().log(Level.FINE, HF.getErrorMessage(e), e); |
1034 |
|
} |
1035 |
|
} |
1036 |
|
|
1037 |
|
private static void |
1038 |
|
exportFxSendsToLscpScript(SamplerChannelModel scm, int chnId, Client lscpClient) { |
1039 |
|
try { |
1040 |
|
FxSend[] fxSends = scm.getFxSends(); |
1041 |
|
|
1042 |
|
for(int i = 0; i < fxSends.length; i++) { |
1043 |
|
FxSend f = fxSends[i]; |
1044 |
|
lscpClient.createFxSend(chnId, f.getMidiController(), f.getName()); |
1045 |
|
|
1046 |
|
Integer[] r = f.getAudioOutputRouting(); |
1047 |
|
for(int j = 0; j < r.length; j++) { |
1048 |
|
lscpClient.setFxSendAudioOutputChannel(chnId, i, j, r[j]); |
1049 |
|
} |
1050 |
|
} |
1051 |
|
} catch(Exception e) { |
1052 |
|
CC.getLogger().log(Level.FINE, HF.getErrorMessage(e), e); |
1053 |
|
} |
1054 |
|
} |
1055 |
|
|
1056 |
|
public static void |
1057 |
|
scheduleInTaskQueue(final Runnable r) { |
1058 |
|
Task dummy = new Global.DummyTask(); |
1059 |
|
dummy.addTaskListener(new TaskListener() { |
1060 |
|
public void |
1061 |
|
taskPerformed(TaskEvent e) { |
1062 |
|
javax.swing.SwingUtilities.invokeLater(r); |
1063 |
|
} |
1064 |
|
}); |
1065 |
|
|
1066 |
|
CC.getTaskQueue().add(dummy); |
1067 |
|
} |
1068 |
|
|
1069 |
|
public static boolean |
1070 |
|
verifyConnection() { |
1071 |
|
if(getCurrentServer() == null) { |
1072 |
|
HF.showErrorMessage(i18n.getError("CC.notConnected")); |
1073 |
|
return false; |
1074 |
|
} |
1075 |
|
|
1076 |
|
return true; |
1077 |
|
} |
1078 |
|
|
1079 |
|
|
1080 |
|
private final static EventHandler eventHandler = new EventHandler(); |
1081 |
|
|
1082 |
|
private static EventHandler |
1083 |
|
getHandler() { return eventHandler; } |
1084 |
|
|
1085 |
private static class EventHandler implements ChannelCountListener, ChannelInfoListener, |
private static class EventHandler implements ChannelCountListener, ChannelInfoListener, |
1086 |
StreamCountListener, VoiceCountListener, TotalVoiceCountListener { |
FxSendCountListener, FxSendInfoListener, StreamCountListener, VoiceCountListener, |
1087 |
|
TotalStreamCountListener, TotalVoiceCountListener, TaskQueueListener, |
1088 |
|
OrchestraListener, ListListener<OrchestraModel>, MidiInstrumentCountListener, |
1089 |
|
MidiInstrumentInfoListener, GlobalInfoListener { |
1090 |
|
|
1091 |
/** Invoked when the number of channels has changed. */ |
/** Invoked when the number of channels has changed. */ |
1092 |
public void |
public void |
1107 |
for(int i = tS.length - 1; i >= 0; i--) { |
for(int i = tS.length - 1; i >= 0; i--) { |
1108 |
Task t = tS[i]; |
Task t = tS[i]; |
1109 |
|
|
1110 |
if(t instanceof UpdateChannelInfo) { |
if(t instanceof Channel.UpdateInfo) { |
1111 |
UpdateChannelInfo uci = (UpdateChannelInfo)t; |
Channel.UpdateInfo cui = (Channel.UpdateInfo)t; |
1112 |
if(uci.getChannelID() == e.getSamplerChannel()) return; |
if(cui.getChannelId() == e.getSamplerChannel()) return; |
1113 |
} else { |
} else { |
1114 |
b = false; |
b = false; |
1115 |
break; |
break; |
1118 |
|
|
1119 |
if(b) { |
if(b) { |
1120 |
Task t = getTaskQueue().getRunningTask(); |
Task t = getTaskQueue().getRunningTask(); |
1121 |
if(t instanceof UpdateChannelInfo) { |
if(t instanceof Channel.UpdateInfo) { |
1122 |
UpdateChannelInfo uci = (UpdateChannelInfo)t; |
Channel.UpdateInfo cui = (Channel.UpdateInfo)t; |
1123 |
if(uci.getChannelID() == e.getSamplerChannel()) return; |
if(cui.getChannelId() == e.getSamplerChannel()) return; |
1124 |
} |
} |
1125 |
} |
} |
1126 |
|
|
1127 |
|
|
1128 |
getTaskQueue().add(new UpdateChannelInfo(e.getSamplerChannel())); |
getTaskQueue().add(new Channel.UpdateInfo(e.getSamplerChannel())); |
1129 |
|
} |
1130 |
|
|
1131 |
|
/** |
1132 |
|
* Invoked when the number of effect sends |
1133 |
|
* on a particular sampler channel has changed. |
1134 |
|
*/ |
1135 |
|
public void |
1136 |
|
fxSendCountChanged(FxSendCountEvent e) { |
1137 |
|
getTaskQueue().add(new Channel.UpdateFxSends(e.getChannel())); |
1138 |
|
} |
1139 |
|
|
1140 |
|
/** |
1141 |
|
* Invoked when the settings of an effect sends are changed. |
1142 |
|
*/ |
1143 |
|
public void |
1144 |
|
fxSendInfoChanged(FxSendInfoEvent e) { |
1145 |
|
Task t = new Channel.UpdateFxSendInfo(e.getChannel(), e.getFxSend()); |
1146 |
|
getTaskQueue().add(t); |
1147 |
} |
} |
1148 |
|
|
1149 |
/** |
/** |
1153 |
public void |
public void |
1154 |
streamCountChanged(StreamCountEvent e) { |
streamCountChanged(StreamCountEvent e) { |
1155 |
SamplerChannelModel scm = |
SamplerChannelModel scm = |
1156 |
getSamplerModel().getChannelModel(e.getSamplerChannel()); |
getSamplerModel().getChannelById(e.getSamplerChannel()); |
1157 |
|
|
1158 |
if(scm == null) { |
if(scm == null) { |
1159 |
CC.getLogger().log ( |
CC.getLogger().log ( |
1175 |
public void |
public void |
1176 |
voiceCountChanged(VoiceCountEvent e) { |
voiceCountChanged(VoiceCountEvent e) { |
1177 |
SamplerChannelModel scm = |
SamplerChannelModel scm = |
1178 |
getSamplerModel().getChannelModel(e.getSamplerChannel()); |
getSamplerModel().getChannelById(e.getSamplerChannel()); |
1179 |
|
|
1180 |
if(scm == null) { |
if(scm == null) { |
1181 |
CC.getLogger().log ( |
CC.getLogger().log ( |
1190 |
scm.setVoiceCount(e.getVoiceCount()); |
scm.setVoiceCount(e.getVoiceCount()); |
1191 |
} |
} |
1192 |
|
|
1193 |
|
/** Invoked when the total number of active streams has changed. */ |
1194 |
|
public void |
1195 |
|
totalStreamCountChanged(TotalStreamCountEvent e) { |
1196 |
|
getSamplerModel().updateActiveStreamsInfo(e.getTotalStreamCount()); |
1197 |
|
} |
1198 |
|
|
1199 |
/** Invoked when the total number of active voices has changed. */ |
/** Invoked when the total number of active voices has changed. */ |
1200 |
public void |
public void |
1201 |
totalVoiceCountChanged(TotalVoiceCountEvent e) { |
totalVoiceCountChanged(TotalVoiceCountEvent e) { |
1202 |
getTaskQueue().add(new UpdateTotalVoiceCount()); |
getTaskQueue().add(new UpdateTotalVoiceCount()); |
1203 |
} |
} |
1204 |
|
|
1205 |
|
/** Invoked when the number of MIDI instruments in a MIDI instrument map is changed. */ |
1206 |
|
public void |
1207 |
|
instrumentCountChanged(MidiInstrumentCountEvent e) { |
1208 |
|
getTaskQueue().add(new Midi.UpdateInstruments(e.getMapId())); |
1209 |
|
} |
1210 |
|
|
1211 |
|
/** Invoked when a MIDI instrument in a MIDI instrument map is changed. */ |
1212 |
|
public void |
1213 |
|
instrumentInfoChanged(MidiInstrumentInfoEvent e) { |
1214 |
|
Task t = new Midi.UpdateInstrumentInfo ( |
1215 |
|
e.getMapId(), e.getMidiBank(), e.getMidiProgram() |
1216 |
|
); |
1217 |
|
getTaskQueue().add(t); |
1218 |
|
|
1219 |
|
} |
1220 |
|
|
1221 |
|
/** Invoked when the global volume of the sampler is changed. */ |
1222 |
|
public void |
1223 |
|
volumeChanged(GlobalInfoEvent e) { |
1224 |
|
getSamplerModel().setVolume(e.getVolume()); |
1225 |
|
} |
1226 |
|
|
1227 |
|
/** |
1228 |
|
* Invoked to indicate that the state of a task queue is changed. |
1229 |
|
* This method is invoked only from the event-dispatching thread. |
1230 |
|
*/ |
1231 |
|
public void |
1232 |
|
stateChanged(TaskQueueEvent e) { |
1233 |
|
switch(e.getEventID()) { |
1234 |
|
case TASK_FETCHED: |
1235 |
|
getProgressIndicator().setString ( |
1236 |
|
((Task)e.getSource()).getDescription() |
1237 |
|
); |
1238 |
|
break; |
1239 |
|
case TASK_DONE: |
1240 |
|
EnhancedTask t = (EnhancedTask)e.getSource(); |
1241 |
|
if(t.doneWithErrors() && !t.isStopped()) { |
1242 |
|
showError(t); |
1243 |
|
} |
1244 |
|
break; |
1245 |
|
case NOT_IDLE: |
1246 |
|
timer.start(); |
1247 |
|
break; |
1248 |
|
case IDLE: |
1249 |
|
timer.stop(); |
1250 |
|
getProgressIndicator().stop(); |
1251 |
|
break; |
1252 |
|
} |
1253 |
|
} |
1254 |
|
|
1255 |
|
private void |
1256 |
|
showError(final Task t) { |
1257 |
|
javax.swing.SwingUtilities.invokeLater(new Runnable() { |
1258 |
|
public void |
1259 |
|
run() { |
1260 |
|
if(t.getErrorDetails() == null) { |
1261 |
|
HF.showErrorMessage(t.getErrorMessage()); |
1262 |
|
} else { |
1263 |
|
getMainFrame().showDetailedErrorMessage ( |
1264 |
|
getMainFrame(), |
1265 |
|
t.getErrorMessage(), |
1266 |
|
t.getErrorDetails() |
1267 |
|
); |
1268 |
|
} |
1269 |
|
} |
1270 |
|
}); |
1271 |
|
} |
1272 |
|
|
1273 |
|
/** Invoked when the name of orchestra is changed. */ |
1274 |
|
public void |
1275 |
|
nameChanged(OrchestraEvent e) { saveOrchestras(); } |
1276 |
|
|
1277 |
|
/** Invoked when the description of orchestra is changed. */ |
1278 |
|
public void |
1279 |
|
descriptionChanged(OrchestraEvent e) { saveOrchestras(); } |
1280 |
|
|
1281 |
|
/** Invoked when an instrument is added to the orchestra. */ |
1282 |
|
public void |
1283 |
|
instrumentAdded(OrchestraEvent e) { saveOrchestras(); } |
1284 |
|
|
1285 |
|
/** Invoked when an instrument is removed from the orchestra. */ |
1286 |
|
public void |
1287 |
|
instrumentRemoved(OrchestraEvent e) { saveOrchestras(); } |
1288 |
|
|
1289 |
|
/** Invoked when the settings of an instrument are changed. */ |
1290 |
|
public void |
1291 |
|
instrumentChanged(OrchestraEvent e) { saveOrchestras(); } |
1292 |
|
|
1293 |
|
/** Invoked when an orchestra is added to the orchestra list. */ |
1294 |
|
public void |
1295 |
|
entryAdded(ListEvent<OrchestraModel> e) { |
1296 |
|
e.getEntry().addOrchestraListener(getHandler()); |
1297 |
|
saveOrchestras(); |
1298 |
|
} |
1299 |
|
|
1300 |
|
/** Invoked when an orchestra is removed from the orchestra list. */ |
1301 |
|
public void |
1302 |
|
entryRemoved(ListEvent<OrchestraModel> e) { |
1303 |
|
e.getEntry().removeOrchestraListener(getHandler()); |
1304 |
|
saveOrchestras(); |
1305 |
|
} |
1306 |
|
} |
1307 |
|
|
1308 |
|
private static final AudioDeviceCountListener audioDeviceCountListener = |
1309 |
|
new AudioDeviceCountListener(); |
1310 |
|
|
1311 |
|
private static class AudioDeviceCountListener implements ItemCountListener { |
1312 |
|
/** Invoked when the number of audio output devices has changed. */ |
1313 |
|
public void |
1314 |
|
itemCountChanged(ItemCountEvent e) { |
1315 |
|
getTaskQueue().add(new Audio.UpdateDevices()); |
1316 |
|
} |
1317 |
|
} |
1318 |
|
|
1319 |
|
private static final AudioDeviceInfoListener audioDeviceInfoListener = |
1320 |
|
new AudioDeviceInfoListener(); |
1321 |
|
|
1322 |
|
private static class AudioDeviceInfoListener implements ItemInfoListener { |
1323 |
|
/** Invoked when the audio output device's settings are changed. */ |
1324 |
|
public void |
1325 |
|
itemInfoChanged(ItemInfoEvent e) { |
1326 |
|
getTaskQueue().add(new Audio.UpdateDeviceInfo(e.getItemID())); |
1327 |
|
} |
1328 |
|
} |
1329 |
|
|
1330 |
|
private static final MidiDeviceCountListener midiDeviceCountListener = |
1331 |
|
new MidiDeviceCountListener(); |
1332 |
|
|
1333 |
|
private static class MidiDeviceCountListener implements ItemCountListener { |
1334 |
|
/** Invoked when the number of MIDI input devices has changed. */ |
1335 |
|
public void |
1336 |
|
itemCountChanged(ItemCountEvent e) { |
1337 |
|
getTaskQueue().add(new Midi.UpdateDevices()); |
1338 |
|
} |
1339 |
|
} |
1340 |
|
|
1341 |
|
private static final MidiDeviceInfoListener midiDeviceInfoListener = |
1342 |
|
new MidiDeviceInfoListener(); |
1343 |
|
|
1344 |
|
private static class MidiDeviceInfoListener implements ItemInfoListener { |
1345 |
|
/** Invoked when the MIDI input device's settings are changed. */ |
1346 |
|
public void |
1347 |
|
itemInfoChanged(ItemInfoEvent e) { |
1348 |
|
getTaskQueue().add(new Midi.UpdateDeviceInfo(e.getItemID())); |
1349 |
|
} |
1350 |
|
} |
1351 |
|
|
1352 |
|
private static final MidiInstrMapCountListener midiInstrMapCountListener = |
1353 |
|
new MidiInstrMapCountListener(); |
1354 |
|
|
1355 |
|
private static class MidiInstrMapCountListener implements ItemCountListener { |
1356 |
|
/** Invoked when the number of MIDI instrument maps is changed. */ |
1357 |
|
public void |
1358 |
|
itemCountChanged(ItemCountEvent e) { |
1359 |
|
getTaskQueue().add(new Midi.UpdateInstrumentMaps()); |
1360 |
|
} |
1361 |
|
} |
1362 |
|
|
1363 |
|
private static final MidiInstrMapInfoListener midiInstrMapInfoListener = |
1364 |
|
new MidiInstrMapInfoListener(); |
1365 |
|
|
1366 |
|
private static class MidiInstrMapInfoListener implements ItemInfoListener { |
1367 |
|
/** Invoked when the MIDI instrument map's settings are changed. */ |
1368 |
|
public void |
1369 |
|
itemInfoChanged(ItemInfoEvent e) { |
1370 |
|
getTaskQueue().add(new Midi.UpdateInstrumentMapInfo(e.getItemID())); |
1371 |
|
} |
1372 |
} |
} |
1373 |
} |
} |