/[svn]/jsampler/trunk/src/org/jsampler/CC.java
ViewVC logotype

Contents of /jsampler/trunk/src/org/jsampler/CC.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1143 - (show annotations) (download)
Mon Apr 2 21:18:31 2007 UTC (17 years ago) by iliev
File size: 32093 byte(s)
* upgrading to version 0.4a

1 /*
2 * JSampler - a java front-end for LinuxSampler
3 *
4 * Copyright (C) 2005-2007 Grigor Iliev <grigor@grigoriliev.com>
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.ByteArrayInputStream;
29 import java.io.ByteArrayOutputStream;
30 import java.io.File;
31 import java.io.FileInputStream;
32 import java.io.FileOutputStream;
33 import java.io.InputStream;
34
35 import java.util.Vector;
36
37 import java.util.logging.Handler;
38 import java.util.logging.Level;
39 import java.util.logging.Logger;
40 import java.util.logging.SimpleFormatter;
41 import java.util.logging.StreamHandler;
42
43 import javax.swing.Timer;
44
45 import net.sf.juife.Task;
46 import net.sf.juife.TaskQueue;
47
48 import net.sf.juife.event.TaskEvent;
49 import net.sf.juife.event.TaskListener;
50 import net.sf.juife.event.TaskQueueEvent;
51 import net.sf.juife.event.TaskQueueListener;
52
53 import org.jsampler.event.ListEvent;
54 import org.jsampler.event.ListListener;
55 import org.jsampler.event.OrchestraEvent;
56 import org.jsampler.event.OrchestraListener;
57
58 import org.jsampler.task.*;
59
60 import org.jsampler.view.JSMainFrame;
61 import org.jsampler.view.JSProgress;
62
63 import org.linuxsampler.lscp.AudioOutputChannel;
64 import org.linuxsampler.lscp.AudioOutputDevice;
65 import org.linuxsampler.lscp.Client;
66 import org.linuxsampler.lscp.FxSend;
67 import org.linuxsampler.lscp.MidiInputDevice;
68 import org.linuxsampler.lscp.MidiPort;
69 import org.linuxsampler.lscp.Parameter;
70 import org.linuxsampler.lscp.SamplerChannel;
71
72 import org.linuxsampler.lscp.event.*;
73
74 import org.w3c.dom.Document;
75 import org.w3c.dom.Node;
76
77 import static org.jsampler.JSI18n.i18n;
78
79
80 /**
81 * This class serves as a 'Control Center' of the application.
82 * It also provides some fundamental routines and access to most used objects.
83 * @author Grigor Iliev
84 */
85 public class CC {
86 private static Handler handler;
87 private static FileOutputStream fos;
88
89 private static JSMainFrame mainFrame = null;
90 private static JSProgress progress = null;
91
92 private final static Client lsClient = new Client();
93
94 private static String jSamplerHome = null;
95
96 private final static TaskQueue taskQueue = new TaskQueue();
97 private final static Timer timer = new Timer(2000, null);
98
99 /** Forbits the instantiation of this class. */
100 private
101 CC() { }
102
103 /**
104 * Returns the logger to be used for logging events.
105 * @return The logger to be used for logging events.
106 */
107 public static Logger
108 getLogger() {
109 return Logger.getLogger (
110 "org.jsampler",
111 "org.jsampler.langprops.LogsBundle"
112 );
113 }
114
115 /**
116 * Returns the task queue to be used for scheduling tasks
117 * for execution out of the event-dispatching thread.
118 * @return The task queue to be used for scheduling tasks
119 * for execution out of the event-dispatching thread.
120 */
121 public static TaskQueue
122 getTaskQueue() { return taskQueue; }
123
124 /**
125 * Returns the main window of this application.
126 * @return The main window of this application.
127 */
128 public static JSMainFrame
129 getMainFrame() { return mainFrame; }
130
131 /**
132 * Sets the main window of this application.
133 * @param mainFrame The main window of this application.
134 */
135 public static void
136 setMainFrame(JSMainFrame mainFrame) { CC.mainFrame = mainFrame; }
137
138 /**
139 * Gets the progress indicator of this application.
140 * @return The progress indicator of this application.
141 */
142 public static JSProgress
143 getProgressIndicator() { return progress; }
144
145 /**
146 * Sets the progress indicator to be used by this application.
147 * @param progress The progress indicator to be used by this application.
148 */
149 public static void
150 setProgressIndicator(JSProgress progress) { CC.progress = progress; }
151
152 /**
153 * Gets the absolute path to the JSampler's home location.
154 * @return The absolute path to the JSampler's home location
155 * or <code>null</code> if the JSampler's home location is not specified yet.
156 */
157 public static String
158 getJSamplerHome() { return jSamplerHome; }
159
160 /**
161 * Sets the location of the JSampler's home.
162 * @param path The new absolute path to the JSampler's home location.
163 */
164 public static void
165 setJSamplerHome(String path) {
166 jSamplerHome = path;
167 Prefs.setJSamplerHome(jSamplerHome);
168 }
169
170 /**
171 * This method does the initial preparation of the application.
172 */
173 protected static void
174 initJSampler() {
175 fos = null;
176 setJSamplerHome(Prefs.getJSamplerHome());
177 String s = getJSamplerHome();
178 try {
179 if(s != null) {
180 s += File.separator + "jsampler.log";
181 File f = new File(s);
182 if(f.isFile()) HF.createBackup("jsampler.log", "jsampler.log.0");
183 fos = new FileOutputStream(s);
184 }
185 } catch(Exception x) { x.printStackTrace(); }
186
187 if(fos == null) handler = new StreamHandler(System.out, new SimpleFormatter());
188 else handler = new StreamHandler(fos, new SimpleFormatter());
189
190 handler.setLevel(Level.FINE);
191 getLogger().addHandler(handler);
192 getLogger().setLevel(Level.FINE);
193 Logger.getLogger("org.linuxsampler.lscp").addHandler(handler);
194 Logger.getLogger("org.linuxsampler.lscp").setLevel(Level.FINE);
195
196 // Flushing logs on every second
197 new java.util.Timer().schedule(new java.util.TimerTask() {
198 public void
199 run() { if(handler != null) handler.flush(); }
200 }, 1000, 1000);
201
202 CC.getLogger().fine("CC.jsStarted");
203
204 HF.setUIDefaultFont(Prefs.getInterfaceFont());
205
206 getClient().setServerAddress(Prefs.getLSAddress());
207 getClient().setServerPort(Prefs.getLSPort());
208
209 timer.setRepeats(false);
210
211 timer.addActionListener(new ActionListener() {
212 public void
213 actionPerformed(ActionEvent e) { CC.getProgressIndicator().start(); }
214 });
215
216 taskQueue.addTaskQueueListener(getHandler());
217
218 taskQueue.start();
219
220 getClient().removeChannelCountListener(getHandler());
221 getClient().addChannelCountListener(getHandler());
222
223 getClient().removeChannelInfoListener(getHandler());
224 getClient().addChannelInfoListener(getHandler());
225
226 getClient().removeFxSendCountListener(getHandler());
227 getClient().addFxSendCountListener(getHandler());
228
229 getClient().removeFxSendInfoListener(getHandler());
230 getClient().addFxSendInfoListener(getHandler());
231
232 getClient().removeStreamCountListener(getHandler());
233 getClient().addStreamCountListener(getHandler());
234
235 getClient().removeVoiceCountListener(getHandler());
236 getClient().addVoiceCountListener(getHandler());
237
238 getClient().removeTotalVoiceCountListener(getHandler());
239 getClient().addTotalVoiceCountListener(getHandler());
240
241 getClient().removeAudioDeviceCountListener(audioDeviceCountListener);
242 getClient().addAudioDeviceCountListener(audioDeviceCountListener);
243
244 getClient().removeAudioDeviceInfoListener(audioDeviceInfoListener);
245 getClient().addAudioDeviceInfoListener(audioDeviceInfoListener);
246
247 getClient().removeMidiDeviceCountListener(midiDeviceCountListener);
248 getClient().addMidiDeviceCountListener(midiDeviceCountListener);
249
250 getClient().removeMidiDeviceInfoListener(midiDeviceInfoListener);
251 getClient().addMidiDeviceInfoListener(midiDeviceInfoListener);
252
253 getClient().removeMidiInstrumentMapCountListener(midiInstrMapCountListener);
254 getClient().addMidiInstrumentMapCountListener(midiInstrMapCountListener);
255
256 getClient().removeMidiInstrumentMapInfoListener(midiInstrMapInfoListener);
257 getClient().addMidiInstrumentMapInfoListener(midiInstrMapInfoListener);
258
259 getClient().removeMidiInstrumentCountListener(getHandler());
260 getClient().addMidiInstrumentCountListener(getHandler());
261
262 getClient().removeMidiInstrumentInfoListener(getHandler());
263 getClient().addMidiInstrumentInfoListener(getHandler());
264
265 getClient().removeGlobalInfoListener(getHandler());
266 getClient().addGlobalInfoListener(getHandler());
267 }
268
269 /**
270 * Checks whether the JSampler home directory is specified and exist.
271 * If the JSampler home directory is not specifed, or is specified
272 * but doesn't exist, a procedure of specifying a JSampler home
273 * directory is initiated.
274 * @see org.jsampler.view.JSMainFrame#installJSamplerHome
275 */
276 public static void
277 checkJSamplerHome() {
278 if(getJSamplerHome() != null) {
279 File f = new File(getJSamplerHome());
280 if(f.isDirectory()) return;
281 }
282
283 CC.getMainFrame().installJSamplerHome();
284 }
285
286 /**
287 * Changes the JSampler's home directory and moves all files from
288 * the old JSampler's home directory to the new one. If all files are
289 * moved succesfully, the old directory is deleted.
290 * @param path The location of the new JSampler's home directory. If
291 * the last directory in the path doesn't exist, it is created.
292 */
293 public static void
294 changeJSamplerHome(String path) {
295 File fNew = new File(path);
296 if(fNew.isFile()) {
297 HF.showErrorMessage(i18n.getError("CC.JSamplerHomeIsNotDir!"));
298 return;
299 }
300
301 if(!fNew.isDirectory()) {
302 if(!fNew.mkdir()) {
303 String s = fNew.getAbsolutePath();
304 HF.showErrorMessage(i18n.getError("CC.mkdirFailed", s));
305 return;
306 }
307 }
308
309 if(getJSamplerHome() == null) {
310 setJSamplerHome(fNew.getAbsolutePath());
311 return;
312 }
313
314 File fOld = new File(getJSamplerHome());
315 if(!fOld.isDirectory()) {
316 setJSamplerHome(fNew.getAbsolutePath());
317 return;
318 }
319
320 File[] files = fOld.listFiles();
321 boolean b = true;
322 if(files != null) {
323 String s = fNew.getAbsolutePath() + File.separator;
324 for(File f : files) if(!f.renameTo(new File(s + f.getName()))) b = false;
325 }
326
327 if(b) fOld.delete();
328 setJSamplerHome(fNew.getAbsolutePath());
329 }
330
331 private final static OrchestraListModel orchestras = new DefaultOrchestraListModel();
332
333 /**
334 * Returns a list containing all available orchestras.
335 * @return A list containing all available orchestras.
336 */
337 public static OrchestraListModel
338 getOrchestras() { return orchestras; }
339
340 /**
341 * Loads the orchestras described in <code>&lt;jsampler_home&gt;/orchestras.xml</code>.
342 * If file with name <code>orchestras.xml.bkp</code> exist in the JSampler's home
343 * directory, this means that the last save has failed. In that case a recovery file
344 * <code>orchestras.xml.rec</code> is created and a recovery procedure
345 * will be initiated.
346 */
347 public static void
348 loadOrchestras() {
349 if(getJSamplerHome() == null) return;
350
351 //TODO: This should be removed in the next release (including loadOrchestras0())
352 File f2 = new File(getJSamplerHome() + File.separator + "orchestras.xml");
353 if(!f2.isFile()) {
354 loadOrchestras0();
355 saveOrchestras();
356 return;
357 }
358 ///////
359
360 try {
361 String s = getJSamplerHome();
362 if(s == null) return;
363
364 File f = new File(s + File.separator + "orchestras.xml.bkp");
365 if(f.isFile()) HF.createBackup("orchestras.xml.bkp", "orchestras.xml.rec");
366
367 FileInputStream fis;
368 fis = new FileInputStream(s + File.separator + "orchestras.xml");
369
370 loadOrchestras(fis);
371 fis.close();
372 } catch(Exception x) {
373 getLogger().log(Level.INFO, HF.getErrorMessage(x), x);
374 }
375 }
376
377
378 private static void
379 loadOrchestras(InputStream in) {
380 Document doc = DOMUtils.readObject(in);
381
382 try { getOrchestras().readObject(doc.getDocumentElement()); }
383 catch(Exception x) {
384 HF.showErrorMessage(x, "Loading orchestras: ");
385 return;
386 }
387
388 for(int i = 0; i < getOrchestras().getOrchestraCount(); i++) {
389 getOrchestras().getOrchestra(i).addOrchestraListener(getHandler());
390 }
391 getOrchestras().addOrchestraListListener(getHandler());
392 }
393
394 private static void
395 loadOrchestras0() {
396 String s = Prefs.getOrchestras();
397 if(s == null) return;
398
399 ByteArrayInputStream bais = new ByteArrayInputStream(s.getBytes());
400 Document doc = DOMUtils.readObject(bais);
401
402 try { getOrchestras().readObject(doc.getDocumentElement()); }
403 catch(Exception x) { HF.showErrorMessage(x, "Loading orchestras: "); }
404 }
405
406 private static void
407 saveOrchestras() {
408 try {
409 String s = getJSamplerHome();
410 if(s == null) return;
411
412 HF.createBackup("orchestras.xml", "orchestras.xml.bkp");
413
414 FileOutputStream fos;
415 fos = new FileOutputStream(s + File.separator + "orchestras.xml", false);
416
417 Document doc = DOMUtils.createEmptyDocument();
418
419 Node node = doc.createElement("temp");
420 doc.appendChild(node);
421
422 getOrchestras().writeObject(doc, doc.getDocumentElement());
423
424 doc.replaceChild(node.getFirstChild(), node);
425
426 DOMUtils.writeObject(doc, fos);
427
428 fos.close();
429
430 HF.deleteFile("orchestras.xml.bkp");
431 } catch(Exception x) {
432 HF.showErrorMessage(x, "Saving orchestras: ");
433 return;
434 }
435 }
436
437 /**
438 * The exit point of the application which ensures clean exit with default exit status 0.
439 * @see #cleanExit(int i)
440 */
441 public static void
442 cleanExit() { cleanExit(0); }
443
444 /**
445 * The exit point of the application which ensures clean exit.
446 * @param i The exit status.
447 */
448 public static void
449 cleanExit(int i) {
450 CC.getLogger().fine("CC.jsEnded");
451 System.exit(i);
452 }
453
454 /**
455 * Gets the <code>Client</code> object that is used to communicate with the backend.
456 * @return The <code>Client</code> object that is used to communicate with the backend.
457 */
458 public static Client
459 getClient() { return lsClient; }
460
461 private static final Vector<ActionListener> listeners = new Vector<ActionListener>();
462
463 /**
464 * Registers the specified listener to be notified when reconnecting to LinuxSampler.
465 * @param l The <code>ActionListener</code> to register.
466 */
467 public static void
468 addReconnectListener(ActionListener l) { listeners.add(l); }
469
470 /**
471 * Removes the specified listener.
472 * @param l The <code>ActionListener</code> to remove.
473 */
474 public static void
475 removeReconnectListener(ActionListener l) { listeners.remove(l); }
476
477 private static void
478 fireReconnectEvent() {
479 ActionEvent e = new ActionEvent(CC.class, ActionEvent.ACTION_PERFORMED, null);
480 for(ActionListener l : listeners) l.actionPerformed(e);
481 }
482
483 private static final SamplerModel samplerModel = new DefaultSamplerModel();
484
485 /**
486 * Gets the sampler model.
487 * @return The sampler model.
488 */
489 public static SamplerModel
490 getSamplerModel() { return samplerModel; }
491
492 /**
493 * Reconnects to LinuxSampler.
494 */
495 public static void
496 reconnect() {
497 initSamplerModel();
498 fireReconnectEvent();
499 }
500
501 /**
502 * This method updates the information about the backend state.
503 */
504 public static void
505 initSamplerModel() {
506 final DefaultSamplerModel model = (DefaultSamplerModel)getSamplerModel();
507
508 final GetServerInfo gsi = new GetServerInfo();
509 gsi.addTaskListener(new TaskListener() {
510 public void
511 taskPerformed(TaskEvent e) {
512 if(!gsi.doneWithErrors()) model.setServerInfo(gsi.getResult());
513 }
514 });
515
516 final Audio.GetDrivers gaod = new Audio.GetDrivers();
517 gaod.addTaskListener(new TaskListener() {
518 public void
519 taskPerformed(TaskEvent e) {
520 if(!gaod.doneWithErrors())
521 model.setAudioOutputDrivers(gaod.getResult());
522 }
523 });
524
525 final GetEngines ge = new GetEngines();
526 ge.addTaskListener(new TaskListener() {
527 public void
528 taskPerformed(TaskEvent e) {
529 if(!ge.doneWithErrors()) model.setEngines(ge.getResult());
530 }
531 });
532
533 final Midi.GetDrivers gmid = new Midi.GetDrivers();
534 gmid.addTaskListener(new TaskListener() {
535 public void
536 taskPerformed(TaskEvent e) {
537 if(!gmid.doneWithErrors())
538 model.setMidiInputDrivers(gmid.getResult());
539 }
540 });
541
542 final Global.GetVolume gv = new Global.GetVolume();
543 gv.addTaskListener(new TaskListener() {
544 public void
545 taskPerformed(TaskEvent e) {
546 if(!gv.doneWithErrors())
547 model.setVolume(gv.getResult());
548 }
549 });
550
551 final Midi.GetInstrumentMaps mgim = new Midi.GetInstrumentMaps();
552 mgim.addTaskListener(new TaskListener() {
553 public void
554 taskPerformed(TaskEvent e) {
555 if(mgim.doneWithErrors()) return;
556 model.removeAllMidiInstrumentMaps();
557
558 for(MidiInstrumentMap map : mgim.getResult()) {
559 model.addMidiInstrumentMap(map);
560 }
561 }
562 });
563
564 final UpdateChannels uc = new UpdateChannels();
565 uc.addTaskListener(new TaskListener() {
566 public void
567 taskPerformed(TaskEvent e) {
568 for(SamplerChannelModel c : model.getChannelModels()) {
569 if(c.getChannelInfo().getEngine() == null) continue;
570
571 Channel.GetFxSends gfs = new Channel.GetFxSends();
572 gfs.setChannel(c.getChannelId());
573 gfs.addTaskListener(new GetFxSendsListener());
574 getTaskQueue().add(gfs);
575 }
576 }
577 });
578
579
580 final Connect cnt = new Connect();
581 cnt.addTaskListener(new TaskListener() {
582 public void
583 taskPerformed(TaskEvent e) {
584 if(cnt.doneWithErrors()) return;
585
586 getTaskQueue().add(gsi);
587 getTaskQueue().add(gaod);
588 getTaskQueue().add(gmid);
589 getTaskQueue().add(ge);
590 getTaskQueue().add(gv);
591 getTaskQueue().add(mgim);
592 getTaskQueue().add(new Midi.UpdateDevices());
593 getTaskQueue().add(new Audio.UpdateDevices());
594 getTaskQueue().add(uc);
595 }
596 });
597 getTaskQueue().add(cnt);
598 }
599
600 private static class GetFxSendsListener implements TaskListener {
601 public void
602 taskPerformed(TaskEvent e) {
603 Channel.GetFxSends gfs = (Channel.GetFxSends)e.getSource();
604 if(gfs.doneWithErrors()) return;
605 SamplerChannelModel m = getSamplerModel().getChannelModel(gfs.getChannel());
606 m.removeAllFxSends();
607
608 for(FxSend fxs : gfs.getResult()) m.addFxSend(fxs);
609 }
610 }
611
612 public static String
613 exportInstrMapsToLscpScript() {
614 StringBuffer sb = new StringBuffer("# Exported by: ");
615 sb.append("JSampler - a java front-end for LinuxSampler\r\n# Version: ");
616 sb.append(JSampler.VERSION).append("\r\n");
617 sb.append("# Date: ").append(new java.util.Date().toString()).append("\r\n\r\n");
618
619 Client lscpClient = new Client(true);
620 ByteArrayOutputStream out = new ByteArrayOutputStream();
621 lscpClient.setPrintOnlyModeOutputStream(out);
622
623 exportInstrMapsToLscpScript(lscpClient);
624 sb.append(out.toString());
625 out.reset();
626
627 return sb.toString();
628 }
629
630 private static void
631 exportInstrMapsToLscpScript(Client lscpClient) {
632 try {
633 lscpClient.removeAllMidiInstrumentMaps();
634 MidiInstrumentMap[] maps = CC.getSamplerModel().getMidiInstrumentMaps();
635 for(int i = 0; i < maps.length; i++) {
636 lscpClient.addMidiInstrumentMap(maps[i].getName());
637 exportInstrumentsToLscpScript(i, maps[i], lscpClient);
638 }
639 } catch(Exception e) {
640 CC.getLogger().log(Level.FINE, HF.getErrorMessage(e), e);
641 HF.showErrorMessage(e);
642 }
643 }
644
645 private static void
646 exportInstrumentsToLscpScript(int mapId, MidiInstrumentMap map, Client lscpClient)
647 throws Exception {
648
649 for(MidiInstrument i : map.getAllMidiInstruments()) {
650 lscpClient.mapMidiInstrument(mapId, i.getInfo().getEntry(), i.getInfo());
651 }
652 }
653
654 public static String
655 exportSessionToLscpScript() {
656 StringBuffer sb = new StringBuffer("# Exported by: ");
657 sb.append("JSampler - a java front-end for LinuxSampler\r\n# Version: ");
658 sb.append(JSampler.VERSION).append("\r\n");
659 sb.append("# Date: ").append(new java.util.Date().toString()).append("\r\n\r\n");
660
661 Client lscpClient = new Client(true);
662 ByteArrayOutputStream out = new ByteArrayOutputStream();
663 lscpClient.setPrintOnlyModeOutputStream(out);
664
665 try {
666 lscpClient.resetSampler();
667 sb.append(out.toString());
668 out.reset();
669 sb.append("\r\n");
670 lscpClient.setVolume(CC.getSamplerModel().getVolume());
671 sb.append(out.toString());
672 out.reset();
673 sb.append("\r\n");
674 } catch(Exception e) { CC.getLogger().log(Level.FINE, HF.getErrorMessage(e), e); }
675
676 MidiDeviceModel[] mDevs = getSamplerModel().getMidiDeviceModels();
677 for(int i = 0; i < mDevs.length; i++) {
678 exportMidiDeviceToLscpScript(mDevs[i].getDeviceInfo(), i, lscpClient);
679 sb.append(out.toString());
680 out.reset();
681 sb.append("\r\n");
682 }
683
684 AudioDeviceModel[] aDevs = getSamplerModel().getAudioDeviceModels();
685 for(int i = 0; i < aDevs.length; i++) {
686 exportAudioDeviceToLscpScript(aDevs[i].getDeviceInfo(), i, lscpClient);
687 sb.append(out.toString());
688 out.reset();
689 sb.append("\r\n");
690 }
691
692 SamplerChannelModel[] channels = getSamplerModel().getChannelModels();
693
694 for(int i = 0; i < channels.length; i++) {
695 SamplerChannelModel scm = getSamplerModel().getChannelModel(i);
696 exportChannelToLscpScript(scm.getChannelInfo(), i, lscpClient);
697 sb.append(out.toString());
698 out.reset();
699
700 sb.append("\r\n");
701
702 exportFxSendsToLscpScript(scm, i, lscpClient);
703 sb.append(out.toString());
704 out.reset();
705
706 sb.append("\r\n");
707 }
708
709 exportInstrMapsToLscpScript(lscpClient);
710 sb.append(out.toString());
711 out.reset();
712
713 return sb.toString();
714 }
715
716 private static void
717 exportMidiDeviceToLscpScript(MidiInputDevice mid, int devId, Client lscpCLient) {
718 try {
719 String s = mid.getDriverName();
720 lscpCLient.createMidiInputDevice(s, mid.getAdditionalParameters());
721
722 MidiPort[] mPorts = mid.getMidiPorts();
723 int l = mPorts.length;
724 if(l != 1) lscpCLient.setMidiInputPortCount(devId, l);
725
726 for(int i = 0; i < l; i++) {
727 Parameter[] prms = mPorts[i].getAllParameters();
728 for(Parameter p : prms) {
729 if(!p.isFixed() && p.getStringValue().length() > 0)
730 lscpCLient.setMidiInputPortParameter(devId, i, p);
731 }
732 }
733 } catch(Exception e) {
734 CC.getLogger().log(Level.FINE, HF.getErrorMessage(e), e);
735 }
736 }
737
738 private static void
739 exportAudioDeviceToLscpScript(AudioOutputDevice aod, int devId, Client lscpCLient) {
740 try {
741 String s = aod.getDriverName();
742 lscpCLient.createAudioOutputDevice(s, aod.getAllParameters());
743
744 AudioOutputChannel[] chns = aod.getAudioChannels();
745
746 for(int i = 0; i < chns.length; i++) {
747 Parameter[] prms = chns[i].getAllParameters();
748 for(Parameter p : prms) {
749 if(p.isFixed() || p.getStringValue().length() == 0);
750 else lscpCLient.setAudioOutputChannelParameter(devId, i, p);
751 }
752 }
753 } catch(Exception e) {
754 CC.getLogger().log(Level.FINE, HF.getErrorMessage(e), e);
755 }
756 }
757
758 private static void
759 exportChannelToLscpScript(SamplerChannel chn, int chnId, Client lscpCLient) {
760 try {
761 lscpCLient.addSamplerChannel();
762
763 int i = chn.getMidiInputDevice();
764 if(i != -1) lscpCLient.setChannelMidiInputDevice(chnId, i);
765 lscpCLient.setChannelMidiInputPort(chnId, chn.getMidiInputPort());
766 lscpCLient.setChannelMidiInputChannel(chnId, chn.getMidiInputChannel());
767
768 i = chn.getAudioOutputDevice();
769 if(i != -1) {
770 lscpCLient.setChannelAudioOutputDevice(chnId, i);
771 Integer[] routing = chn.getAudioOutputRouting();
772
773 for(int j = 0; j < routing.length; j++) {
774 int k = routing[j];
775 if(k == j) continue;
776
777 lscpCLient.setChannelAudioOutputChannel(chnId, j, k);
778 }
779 }
780
781 if(chn.getEngine() != null) {
782 lscpCLient.loadSamplerEngine(chn.getEngine().getName(), chnId);
783 lscpCLient.setChannelVolume(chnId, chn.getVolume());
784 }
785
786 String s = chn.getInstrumentFile();
787 i = chn.getInstrumentIndex();
788 if(s != null) lscpCLient.loadInstrument(s, i, chnId, true);
789
790 if(chn.isMuted()) lscpCLient.setChannelMute(chnId, true);
791 if(chn.isSoloChannel()) lscpCLient.setChannelSolo(chnId, true);
792 } catch(Exception e) {
793 CC.getLogger().log(Level.FINE, HF.getErrorMessage(e), e);
794 }
795 }
796
797 private static void
798 exportFxSendsToLscpScript(SamplerChannelModel scm, int chnId, Client lscpClient) {
799 try {
800 FxSend[] fxSends = scm.getFxSends();
801
802 for(int i = 0; i < fxSends.length; i++) {
803 FxSend f = fxSends[i];
804 lscpClient.createFxSend(chnId, f.getMidiController(), f.getName());
805
806 Integer[] r = f.getAudioOutputRouting();
807 for(int j = 0; j < r.length; j++) {
808 lscpClient.setFxSendAudioOutputChannel(chnId, i, j, r[j]);
809 }
810 }
811 } catch(Exception e) {
812 CC.getLogger().log(Level.FINE, HF.getErrorMessage(e), e);
813 }
814 }
815
816
817 private final static EventHandler eventHandler = new EventHandler();
818
819 private static EventHandler
820 getHandler() { return eventHandler; }
821
822 private static class EventHandler implements ChannelCountListener, ChannelInfoListener,
823 FxSendCountListener, FxSendInfoListener, StreamCountListener, VoiceCountListener,
824 TotalVoiceCountListener, TaskQueueListener, OrchestraListener,
825 ListListener<OrchestraModel>, MidiInstrumentCountListener,
826 MidiInstrumentInfoListener, GlobalInfoListener {
827
828 /** Invoked when the number of channels has changed. */
829 public void
830 channelCountChanged( ChannelCountEvent e) {
831 getTaskQueue().add(new UpdateChannels());
832 }
833
834 /** Invoked when changes to the sampler channel has occured. */
835 public void
836 channelInfoChanged(ChannelInfoEvent e) {
837 /*
838 * Because of the rapid notification flow when instrument is loaded
839 * we need to do some optimization to decrease the traffic.
840 */
841 boolean b = true;
842 Task[] tS = getTaskQueue().getPendingTasks();
843
844 for(int i = tS.length - 1; i >= 0; i--) {
845 Task t = tS[i];
846
847 if(t instanceof Channel.UpdateInfo) {
848 Channel.UpdateInfo cui = (Channel.UpdateInfo)t;
849 if(cui.getChannelId() == e.getSamplerChannel()) return;
850 } else {
851 b = false;
852 break;
853 }
854 }
855
856 if(b) {
857 Task t = getTaskQueue().getRunningTask();
858 if(t instanceof Channel.UpdateInfo) {
859 Channel.UpdateInfo cui = (Channel.UpdateInfo)t;
860 if(cui.getChannelId() == e.getSamplerChannel()) return;
861 }
862 }
863
864
865 getTaskQueue().add(new Channel.UpdateInfo(e.getSamplerChannel()));
866 }
867
868 /**
869 * Invoked when the number of effect sends
870 * on a particular sampler channel has changed.
871 */
872 public void
873 fxSendCountChanged(FxSendCountEvent e) {
874 getTaskQueue().add(new Channel.UpdateFxSends(e.getChannel()));
875 }
876
877 /**
878 * Invoked when the settings of an effect sends are changed.
879 */
880 public void
881 fxSendInfoChanged(FxSendInfoEvent e) {
882 Task t = new Channel.UpdateFxSendInfo(e.getChannel(), e.getFxSend());
883 getTaskQueue().add(t);
884 }
885
886 /**
887 * Invoked when the number of active disk
888 * streams in a specific sampler channel has changed.
889 */
890 public void
891 streamCountChanged(StreamCountEvent e) {
892 SamplerChannelModel scm =
893 getSamplerModel().getChannelModel(e.getSamplerChannel());
894
895 if(scm == null) {
896 CC.getLogger().log (
897 Level.WARNING,
898 "CC.unknownChannel!",
899 e.getSamplerChannel()
900 );
901
902 return;
903 }
904
905 scm.setStreamCount(e.getStreamCount());
906 }
907
908 /**
909 * Invoked when the number of active voices
910 * in a specific sampler channel has changed.
911 */
912 public void
913 voiceCountChanged(VoiceCountEvent e) {
914 SamplerChannelModel scm =
915 getSamplerModel().getChannelModel(e.getSamplerChannel());
916
917 if(scm == null) {
918 CC.getLogger().log (
919 Level.WARNING,
920 "CC.unknownChannel!",
921 e.getSamplerChannel()
922 );
923
924 return;
925 }
926
927 scm.setVoiceCount(e.getVoiceCount());
928 }
929
930 /** Invoked when the total number of active voices has changed. */
931 public void
932 totalVoiceCountChanged(TotalVoiceCountEvent e) {
933 getTaskQueue().add(new UpdateTotalVoiceCount());
934 }
935
936 /** Invoked when the number of MIDI instruments in a MIDI instrument map is changed. */
937 public void
938 instrumentCountChanged(MidiInstrumentCountEvent e) {
939 getTaskQueue().add(new Midi.UpdateInstruments(e.getMapId()));
940 }
941
942 /** Invoked when a MIDI instrument in a MIDI instrument map is changed. */
943 public void
944 instrumentInfoChanged(MidiInstrumentInfoEvent e) {
945 Task t = new Midi.UpdateInstrumentInfo (
946 e.getMapId(), e.getMidiBank(), e.getMidiProgram()
947 );
948 getTaskQueue().add(t);
949
950 }
951
952 /** Invoked when the global volume of the sampler is changed. */
953 public void
954 volumeChanged(GlobalInfoEvent e) {
955 getSamplerModel().setVolume(e.getVolume());
956 }
957
958 /**
959 * Invoked to indicate that the state of a task queue is changed.
960 * This method is invoked only from the event-dispatching thread.
961 */
962 public void
963 stateChanged(TaskQueueEvent e) {
964 switch(e.getEventID()) {
965 case TASK_FETCHED:
966 getProgressIndicator().setString (
967 ((Task)e.getSource()).getDescription()
968 );
969 break;
970 case TASK_DONE:
971 EnhancedTask t = (EnhancedTask)e.getSource();
972 if(t.doneWithErrors() && !t.isStopped())
973 HF.showErrorMessage(t.getErrorMessage());
974 break;
975 case NOT_IDLE:
976 timer.start();
977 break;
978 case IDLE:
979 timer.stop();
980 getProgressIndicator().stop();
981 break;
982 }
983 }
984
985 /** Invoked when the name of orchestra is changed. */
986 public void
987 nameChanged(OrchestraEvent e) { saveOrchestras(); }
988
989 /** Invoked when the description of orchestra is changed. */
990 public void
991 descriptionChanged(OrchestraEvent e) { saveOrchestras(); }
992
993 /** Invoked when an instrument is added to the orchestra. */
994 public void
995 instrumentAdded(OrchestraEvent e) { saveOrchestras(); }
996
997 /** Invoked when an instrument is removed from the orchestra. */
998 public void
999 instrumentRemoved(OrchestraEvent e) { saveOrchestras(); }
1000
1001 /** Invoked when the settings of an instrument are changed. */
1002 public void
1003 instrumentChanged(OrchestraEvent e) { saveOrchestras(); }
1004
1005 /** Invoked when an orchestra is added to the orchestra list. */
1006 public void
1007 entryAdded(ListEvent<OrchestraModel> e) {
1008 e.getEntry().addOrchestraListener(getHandler());
1009 saveOrchestras();
1010 }
1011
1012 /** Invoked when an orchestra is removed from the orchestra list. */
1013 public void
1014 entryRemoved(ListEvent<OrchestraModel> e) {
1015 e.getEntry().removeOrchestraListener(getHandler());
1016 saveOrchestras();
1017 }
1018 }
1019
1020 private static final AudioDeviceCountListener audioDeviceCountListener =
1021 new AudioDeviceCountListener();
1022
1023 private static class AudioDeviceCountListener implements ItemCountListener {
1024 /** Invoked when the number of audio output devices has changed. */
1025 public void
1026 itemCountChanged(ItemCountEvent e) {
1027 getTaskQueue().add(new Audio.UpdateDevices());
1028 }
1029 }
1030
1031 private static final AudioDeviceInfoListener audioDeviceInfoListener =
1032 new AudioDeviceInfoListener();
1033
1034 private static class AudioDeviceInfoListener implements ItemInfoListener {
1035 /** Invoked when the audio output device's settings are changed. */
1036 public void
1037 itemInfoChanged(ItemInfoEvent e) {
1038 getTaskQueue().add(new Audio.UpdateDeviceInfo(e.getItemID()));
1039 }
1040 }
1041
1042 private static final MidiDeviceCountListener midiDeviceCountListener =
1043 new MidiDeviceCountListener();
1044
1045 private static class MidiDeviceCountListener implements ItemCountListener {
1046 /** Invoked when the number of MIDI input devices has changed. */
1047 public void
1048 itemCountChanged(ItemCountEvent e) {
1049 getTaskQueue().add(new Midi.UpdateDevices());
1050 }
1051 }
1052
1053 private static final MidiDeviceInfoListener midiDeviceInfoListener =
1054 new MidiDeviceInfoListener();
1055
1056 private static class MidiDeviceInfoListener implements ItemInfoListener {
1057 /** Invoked when the MIDI input device's settings are changed. */
1058 public void
1059 itemInfoChanged(ItemInfoEvent e) {
1060 getTaskQueue().add(new Midi.UpdateDeviceInfo(e.getItemID()));
1061 }
1062 }
1063
1064 private static final MidiInstrMapCountListener midiInstrMapCountListener =
1065 new MidiInstrMapCountListener();
1066
1067 private static class MidiInstrMapCountListener implements ItemCountListener {
1068 /** Invoked when the number of MIDI instrument maps is changed. */
1069 public void
1070 itemCountChanged(ItemCountEvent e) {
1071 getTaskQueue().add(new Midi.UpdateInstrumentMaps());
1072 }
1073 }
1074
1075 private static final MidiInstrMapInfoListener midiInstrMapInfoListener =
1076 new MidiInstrMapInfoListener();
1077
1078 private static class MidiInstrMapInfoListener implements ItemInfoListener {
1079 /** Invoked when the MIDI instrument map's settings are changed. */
1080 public void
1081 itemInfoChanged(ItemInfoEvent e) {
1082 getTaskQueue().add(new Midi.UpdateInstrumentMapInfo(e.getItemID()));
1083 }
1084 }
1085 }

  ViewVC Help
Powered by ViewVC