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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1143 by iliev, Mon Apr 2 21:18:31 2007 UTC revision 1864 by iliev, Sat Mar 14 20:44:58 2009 UTC
# Line 1  Line 1 
1  /*  /*
2   *   JSampler - a java front-end for LinuxSampler   *   JSampler - a java front-end for LinuxSampler
3   *   *
4   *   Copyright (C) 2005-2007 Grigor Iliev <grigor@grigoriliev.com>   *   Copyright (C) 2005-2009 Grigor Iliev <grigor@grigoriliev.com>
5   *   *
6   *   This file is part of JSampler.   *   This file is part of JSampler.
7   *   *
# Line 25  package org.jsampler; Line 25  package org.jsampler;
25  import java.awt.event.ActionEvent;  import java.awt.event.ActionEvent;
26  import java.awt.event.ActionListener;  import java.awt.event.ActionListener;
27    
 import java.io.ByteArrayInputStream;  
28  import java.io.ByteArrayOutputStream;  import java.io.ByteArrayOutputStream;
29  import java.io.File;  import java.io.File;
30  import java.io.FileInputStream;  import java.io.FileInputStream;
# Line 40  import java.util.logging.Logger; Line 39  import java.util.logging.Logger;
39  import java.util.logging.SimpleFormatter;  import java.util.logging.SimpleFormatter;
40  import java.util.logging.StreamHandler;  import java.util.logging.StreamHandler;
41    
42    import javax.swing.SwingUtilities;
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;  import net.sf.juife.Task;
49  import net.sf.juife.TaskQueue;  import net.sf.juife.TaskQueue;
50    
# Line 59  import org.jsampler.task.*; Line 62  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;  import org.linuxsampler.lscp.AudioOutputChannel;
69  import org.linuxsampler.lscp.AudioOutputDevice;  import org.linuxsampler.lscp.AudioOutputDevice;
# Line 86  public class CC { Line 91  public class CC {
91          private static Handler handler;          private static Handler handler;
92          private static FileOutputStream fos;          private static FileOutputStream fos;
93                    
94            private static JSViewConfig viewConfig = null;
95          private static JSMainFrame mainFrame = null;          private static JSMainFrame mainFrame = null;
96          private static JSProgress progress = null;          private static JSProgress progress = null;
97                    
# Line 96  public class CC { Line 102  public class CC {
102          private final static TaskQueue taskQueue = new TaskQueue();          private final static TaskQueue taskQueue = new TaskQueue();
103          private final static Timer timer = new Timer(2000, null);          private final static Timer timer = new Timer(2000, null);
104                    
105            private static int connectionFailureCount = 0;
106            
107          /** Forbits the instantiation of this class. */          /** Forbits the instantiation of this class. */
108          private          private
109          CC() { }          CC() { }
# Line 118  public class CC { Line 126  public class CC {
126           * @return The task queue to be used for scheduling tasks           * @return The task queue to be used for scheduling tasks
127           * for execution out of the event-dispatching thread.           * for execution out of the event-dispatching thread.
128           */           */
129          public static TaskQueue          public static synchronized TaskQueue
130          getTaskQueue() { return taskQueue; }          getTaskQueue() { return taskQueue; }
131                    
132          /**          /**
133             * Adds the specified task to the task queue. All task in the
134             * queue equal to the specified task are removed from the queue.
135             */
136            public static synchronized void
137            scheduleTask(Task t) {
138                    while(getTaskQueue().removeTask(t)) { }
139                    
140                    getTaskQueue().add(t);
141            }
142            
143            /**
144             * Adds the specified task to the task queue only if the last
145             * task in the queue is not equal to <code>t</code>.
146             */
147            public static synchronized void
148            addTask(Task t) {
149                    Task[] tasks = getTaskQueue().getPendingTasks();
150                    if(tasks.length > 0 && tasks[tasks.length - 1].equals(t)) return;
151                    getTaskQueue().add(t);
152            }
153            
154            /**
155             * Gets the configuration of the current view.
156             */
157            public static JSViewConfig
158            getViewConfig() { return viewConfig; }
159            
160            public static JSPrefs
161            preferences() { return getViewConfig().preferences(); }
162            
163            /**
164             * Sets the configuration of the current view.
165             */
166            public static void
167            setViewConfig(JSViewConfig viewConfig) { CC.viewConfig = viewConfig; }
168            
169            /**
170           * Returns the main window of this application.           * Returns the main window of this application.
171           * @return The main window of this application.           * @return The main window of this application.
172           */           */
# Line 190  public class CC { Line 235  public class CC {
235                  handler.setLevel(Level.FINE);                  handler.setLevel(Level.FINE);
236                  getLogger().addHandler(handler);                  getLogger().addHandler(handler);
237                  getLogger().setLevel(Level.FINE);                  getLogger().setLevel(Level.FINE);
                 Logger.getLogger("org.linuxsampler.lscp").addHandler(handler);  
238                  Logger.getLogger("org.linuxsampler.lscp").setLevel(Level.FINE);                  Logger.getLogger("org.linuxsampler.lscp").setLevel(Level.FINE);
239                    Logger.getLogger("org.linuxsampler.lscp").addHandler(handler);
240                                    
241                  // Flushing logs on every second                  // Flushing logs on every second
242                  new java.util.Timer().schedule(new java.util.TimerTask() {                  new java.util.Timer().schedule(new java.util.TimerTask() {
# Line 199  public class CC { Line 244  public class CC {
244                          run() { if(handler != null) handler.flush(); }                          run() { if(handler != null) handler.flush(); }
245                  }, 1000, 1000);                  }, 1000, 1000);
246                                    
247                  CC.getLogger().fine("CC.jsStarted");                  getLogger().fine("CC.jsStarted");
248                                    
249                  HF.setUIDefaultFont(Prefs.getInterfaceFont());                  HF.setUIDefaultFont(Prefs.getInterfaceFont());
250                                    
                 getClient().setServerAddress(Prefs.getLSAddress());  
                 getClient().setServerPort(Prefs.getLSPort());  
                   
251                  timer.setRepeats(false);                  timer.setRepeats(false);
252                                    
253                  timer.addActionListener(new ActionListener() {                  timer.addActionListener(new ActionListener() {
# Line 213  public class CC { Line 255  public class CC {
255                          actionPerformed(ActionEvent e) { CC.getProgressIndicator().start(); }                          actionPerformed(ActionEvent e) { CC.getProgressIndicator().start(); }
256                  });                  });
257                                    
258                  taskQueue.addTaskQueueListener(getHandler());                  getTaskQueue().addTaskQueueListener(getHandler());
259                                    
260                  taskQueue.start();                  getTaskQueue().start();
261                                    
262                  getClient().removeChannelCountListener(getHandler());                  getClient().removeChannelCountListener(getHandler());
263                  getClient().addChannelCountListener(getHandler());                  getClient().addChannelCountListener(getHandler());
# Line 235  public class CC { Line 277  public class CC {
277                  getClient().removeVoiceCountListener(getHandler());                  getClient().removeVoiceCountListener(getHandler());
278                  getClient().addVoiceCountListener(getHandler());                  getClient().addVoiceCountListener(getHandler());
279                                    
280                    getClient().removeTotalStreamCountListener(getHandler());
281                    getClient().addTotalStreamCountListener(getHandler());
282                    
283                  getClient().removeTotalVoiceCountListener(getHandler());                  getClient().removeTotalVoiceCountListener(getHandler());
284                  getClient().addTotalVoiceCountListener(getHandler());                  getClient().addTotalVoiceCountListener(getHandler());
285                                    
# Line 264  public class CC { Line 309  public class CC {
309                                    
310                  getClient().removeGlobalInfoListener(getHandler());                  getClient().removeGlobalInfoListener(getHandler());
311                  getClient().addGlobalInfoListener(getHandler());                  getClient().addGlobalInfoListener(getHandler());
312                    
313                    getClient().removeChannelMidiDataListener(getHandler());
314                    getClient().addChannelMidiDataListener(getHandler());
315                    
316                    CC.addConnectionEstablishedListener(new ActionListener() {
317                            public void
318                            actionPerformed(ActionEvent e) {
319                                    connectionFailureCount = 0;
320                            }
321                    });
322          }          }
323                    
324          /**          /**
# Line 277  public class CC { Line 332  public class CC {
332          checkJSamplerHome() {          checkJSamplerHome() {
333                  if(getJSamplerHome() != null) {                  if(getJSamplerHome() != null) {
334                          File f = new File(getJSamplerHome());                          File f = new File(getJSamplerHome());
335                          if(f.isDirectory()) return;                          if(f.exists() && f.isDirectory()) {
336                                    return;
337                            }
338                  }                  }
339                                    
340                  CC.getMainFrame().installJSamplerHome();                  getMainFrame().installJSamplerHome();
341          }          }
342                    
343          /**          /**
# Line 293  public class CC { Line 350  public class CC {
350          public static void          public static void
351          changeJSamplerHome(String path) {          changeJSamplerHome(String path) {
352                  File fNew = new File(path);                  File fNew = new File(path);
353                  if(fNew.isFile()) {                  if(fNew.exists() && fNew.isFile()) {
354                          HF.showErrorMessage(i18n.getError("CC.JSamplerHomeIsNotDir!"));                          HF.showErrorMessage(i18n.getError("CC.JSamplerHomeIsNotDir!"));
355                          return;                          return;
356                  }                  }
357                                    
358                  if(!fNew.isDirectory()) {                  if(!fNew.exists()) {
359                          if(!fNew.mkdir()) {                          if(!fNew.mkdir()) {
360                                  String s = fNew.getAbsolutePath();                                  String s = fNew.getAbsolutePath();
361                                  HF.showErrorMessage(i18n.getError("CC.mkdirFailed", s));                                  HF.showErrorMessage(i18n.getError("CC.mkdirFailed", s));
# Line 306  public class CC { Line 363  public class CC {
363                          }                          }
364                  }                  }
365                                    
366                  if(getJSamplerHome() == null) {                  if(getJSamplerHome() == null || path.equals(getJSamplerHome())) {
367                          setJSamplerHome(fNew.getAbsolutePath());                          setJSamplerHome(fNew.getAbsolutePath());
368                          return;                          return;
369                  }                  }
370                                    
371                  File fOld = new File(getJSamplerHome());                  File fOld = new File(getJSamplerHome());
372                  if(!fOld.isDirectory()) {                  if(!fOld.exists() || !fOld.isDirectory()) {
373                          setJSamplerHome(fNew.getAbsolutePath());                          setJSamplerHome(fNew.getAbsolutePath());
374                          return;                          return;
375                  }                  }
# Line 337  public class CC { Line 394  public class CC {
394          public static OrchestraListModel          public static OrchestraListModel
395          getOrchestras() { return orchestras; }          getOrchestras() { return orchestras; }
396                    
397            private final static ServerList servers = new ServerList();
398            
399            /** Returns the server list. */
400            public static ServerList
401            getServerList() { return servers; }
402            
403            private static ServerListListener serverListListener = new ServerListListener();
404            
405            private static class ServerListListener implements ChangeListener {
406                    @Override
407                    public void
408                    stateChanged(ChangeEvent e) {
409                            saveServerList();
410                    }
411            }
412            
413            private static final Vector<ChangeListener> idtmListeners = new Vector<ChangeListener>();
414            private static InstrumentsDbTreeModel instrumentsDbTreeModel = null;
415            
416            /**
417             * Gets the tree model of the instruments database.
418             * If the currently used view doesn't have instruments
419             * database support the tree model is initialized on first use.
420             * @return The tree model of the instruments database or
421             * <code>null</code> if the backend doesn't have instruments database support.
422             * @see org.jsampler.view.JSViewConfig#getInstrumentsDbSupport
423             */
424            public static InstrumentsDbTreeModel
425            getInstrumentsDbTreeModel() {
426                    if(getSamplerModel().getServerInfo() == null) return null;
427                    if(!getSamplerModel().getServerInfo().hasInstrumentsDbSupport()) return null;
428                    
429                    if(instrumentsDbTreeModel == null) {
430                            instrumentsDbTreeModel = new InstrumentsDbTreeModel();
431                            for(ChangeListener l : idtmListeners) l.stateChanged(null);
432                    }
433                    
434                    return instrumentsDbTreeModel;
435            }
436            
437            public static void
438            addInstrumentsDbChangeListener(ChangeListener l) {
439                    idtmListeners.add(l);
440            }
441            
442            public static void
443            removeInstrumentsDbChangeListener(ChangeListener l) {
444                    idtmListeners.remove(l);
445            }
446            
447            private static final LostFilesModel lostFilesModel = new LostFilesModel();
448            
449            public static LostFilesModel
450            getLostFilesModel() { return lostFilesModel; }
451            
452          /**          /**
453           * Loads the orchestras described in <code>&lt;jsampler_home&gt;/orchestras.xml</code>.           * Loads the orchestras described in <code>&lt;jsampler_home&gt;/orchestras.xml</code>.
454           * If file with name <code>orchestras.xml.bkp</code> exist in the JSampler's home           * If file with name <code>orchestras.xml.bkp</code> exist in the JSampler's home
# Line 348  public class CC { Line 460  public class CC {
460          loadOrchestras() {          loadOrchestras() {
461                  if(getJSamplerHome() == null) return;                  if(getJSamplerHome() == null) return;
462                                    
                 //TODO: This should be removed in the next release (including loadOrchestras0())  
                 File f2 = new File(getJSamplerHome() + File.separator + "orchestras.xml");  
                 if(!f2.isFile()) {  
                         loadOrchestras0();  
                         saveOrchestras();  
                         return;  
                 }  
                 ///////  
                   
463                  try {                  try {
464                          String s = getJSamplerHome();                          String s = getJSamplerHome();
                         if(s == null) return;  
465                                                    
466                          File f = new File(s + File.separator + "orchestras.xml.bkp");                          File f = new File(s + File.separator + "orchestras.xml.bkp");
467                          if(f.isFile()) HF.createBackup("orchestras.xml.bkp", "orchestras.xml.rec");                          if(f.isFile()) HF.createBackup("orchestras.xml.bkp", "orchestras.xml.rec");
# Line 372  public class CC { Line 474  public class CC {
474                  } catch(Exception x) {                  } catch(Exception x) {
475                          getLogger().log(Level.INFO, HF.getErrorMessage(x), x);                          getLogger().log(Level.INFO, HF.getErrorMessage(x), x);
476                  }                  }
477                    
478                    getOrchestras().addOrchestraListListener(getHandler());
479          }          }
480                    
481                    
# Line 388  public class CC { Line 492  public class CC {
492                  for(int i = 0; i < getOrchestras().getOrchestraCount(); i++) {                  for(int i = 0; i < getOrchestras().getOrchestraCount(); i++) {
493                          getOrchestras().getOrchestra(i).addOrchestraListener(getHandler());                          getOrchestras().getOrchestra(i).addOrchestraListener(getHandler());
494                  }                  }
                 getOrchestras().addOrchestraListListener(getHandler());  
         }  
           
         private static void  
         loadOrchestras0() {  
                 String s = Prefs.getOrchestras();  
                 if(s == null) return;  
                   
                 ByteArrayInputStream bais = new ByteArrayInputStream(s.getBytes());  
                 Document doc = DOMUtils.readObject(bais);  
                   
                 try { getOrchestras().readObject(doc.getDocumentElement()); }  
                 catch(Exception x) { HF.showErrorMessage(x, "Loading orchestras: "); }  
495          }          }
496                    
497          private static void          private static void
# Line 411  public class CC { Line 502  public class CC {
502                                                    
503                          HF.createBackup("orchestras.xml", "orchestras.xml.bkp");                          HF.createBackup("orchestras.xml", "orchestras.xml.bkp");
504                                                    
505                          FileOutputStream fos;                          FileOutputStream fos2;
506                          fos = new FileOutputStream(s + File.separator + "orchestras.xml", false);                          fos2 = new FileOutputStream(s + File.separator + "orchestras.xml", false);
507                                                    
508                          Document doc = DOMUtils.createEmptyDocument();                          Document doc = DOMUtils.createEmptyDocument();
509                                    
# Line 423  public class CC { Line 514  public class CC {
514                                                    
515                          doc.replaceChild(node.getFirstChild(), node);                          doc.replaceChild(node.getFirstChild(), node);
516                                    
517                          DOMUtils.writeObject(doc, fos);                          DOMUtils.writeObject(doc, fos2);
518                                                    
519                          fos.close();                          fos2.close();
520                                                    
521                          HF.deleteFile("orchestras.xml.bkp");                          HF.deleteFile("orchestras.xml.bkp");
522                  } catch(Exception x) {                  } catch(Exception x) {
# Line 435  public class CC { Line 526  public class CC {
526          }          }
527                    
528          /**          /**
529             * Loads the servers' info described in <code>&lt;jsampler_home&gt;/servers.xml</code>.
530             * If file with name <code>servers.xml.bkp</code> exist in the JSampler's home
531             * directory, this means that the last save has failed. In that case a recovery file
532             * <code>servers.xml.rec</code> is created and a recovery procedure
533             * will be initiated.
534             */
535            public static void
536            loadServerList() {
537                    if(getJSamplerHome() == null) return;
538                    
539                    try {
540                            String s = getJSamplerHome();
541                            
542                            File f = new File(s + File.separator + "servers.xml.bkp");
543                            if(f.isFile()) HF.createBackup("servers.xml.bkp", "servers.xml.rec");
544                            
545                            FileInputStream fis;
546                            fis = new FileInputStream(s + File.separator + "servers.xml");
547                            
548                            loadServerList(fis);
549                            fis.close();
550                    } catch(Exception x) {
551                            getLogger().log(Level.INFO, HF.getErrorMessage(x), x);
552                    }
553                    
554                    getServerList().addChangeListener(serverListListener);
555                    
556                    /* We should have at least one server to connect. */
557                    if(getServerList().getServerCount() == 0) {
558                            Server server = new Server();
559                            server.setName("127.0.0.1:8888");
560                            server.setAddress("127.0.0.1");
561                            server.setPort(8888);
562                            getServerList().addServer(server);
563                    }
564            }
565            
566            
567            private static void
568            loadServerList(InputStream in) {
569                    Document doc = DOMUtils.readObject(in);
570                    
571                    try { getServerList().readObject(doc.getDocumentElement()); }
572                    catch(Exception x) {
573                            HF.showErrorMessage(x, "Loading server list: ");
574                            return;
575                    }
576            }
577            
578            private static void
579            saveServerList() {
580                    try {
581                            String s = getJSamplerHome();
582                            if(s == null) return;
583                            
584                            HF.createBackup("servers.xml", "servers.xml.bkp");
585                            
586                            FileOutputStream fos2;
587                            fos2 = new FileOutputStream(s + File.separator + "servers.xml", false);
588                            
589                            Document doc = DOMUtils.createEmptyDocument();
590                    
591                            Node node = doc.createElement("temp");
592                            doc.appendChild(node);
593                            
594                            getServerList().writeObject(doc, doc.getDocumentElement());
595                            
596                            doc.replaceChild(node.getFirstChild(), node);
597                    
598                            DOMUtils.writeObject(doc, fos2);
599                            
600                            fos2.close();
601                            
602                            HF.deleteFile("servers.xml.bkp");
603                    } catch(Exception x) {
604                            HF.showErrorMessage(x, "Saving server list: ");
605                            return;
606                    }
607            }
608            
609            /**
610           * The exit point of the application which ensures clean exit with default exit status 0.           * The exit point of the application which ensures clean exit with default exit status 0.
611           *  @see #cleanExit(int i)           *  @see #cleanExit(int i)
612           */           */
# Line 447  public class CC { Line 619  public class CC {
619           */           */
620          public static void          public static void
621          cleanExit(int i) {          cleanExit(int i) {
622                  CC.getLogger().fine("CC.jsEnded");                  getLogger().fine("CC.jsEnded");
623                    try { getClient().disconnect(); } // FIXME: this might block the EDT
624                    catch(Exception x) { x.printStackTrace(); }
625                    if(backendProcess != null) backendProcess.destroy();
626                    backendProcess = null;
627                    fireBackendProcessEvent();
628                  System.exit(i);                  System.exit(i);
629          }          }
630                    
# Line 480  public class CC { Line 657  public class CC {
657                  for(ActionListener l : listeners) l.actionPerformed(e);                  for(ActionListener l : listeners) l.actionPerformed(e);
658          }          }
659                    
660            private static final Vector<ActionListener> ceListeners = new Vector<ActionListener>();
661            
662            /**
663             * Registers the specified listener to be notified when
664             * jsampler is connected successfully to LinuxSampler.
665             * @param l The <code>ActionListener</code> to register.
666             */
667            public static void
668            addConnectionEstablishedListener(ActionListener l) { ceListeners.add(l); }
669            
670            /**
671             * Removes the specified listener.
672             * @param l The <code>ActionListener</code> to remove.
673             */
674            public static void
675            removeConnectionEstablishedListener(ActionListener l) { ceListeners.remove(l); }
676            
677            private static void
678            fireConnectionEstablishedEvent() {
679                    ActionEvent e = new ActionEvent(CC.class, ActionEvent.ACTION_PERFORMED, null);
680                    for(ActionListener l : ceListeners) l.actionPerformed(e);
681            }
682            
683          private static final SamplerModel samplerModel = new DefaultSamplerModel();          private static final SamplerModel samplerModel = new DefaultSamplerModel();
684                    
685          /**          /**
# Line 490  public class CC { Line 690  public class CC {
690          getSamplerModel() { return samplerModel; }          getSamplerModel() { return samplerModel; }
691                    
692          /**          /**
693             * Connects to LinuxSampler.
694             */
695            public static void
696            connect() { initSamplerModel(); }
697            
698            /**
699           * Reconnects to LinuxSampler.           * Reconnects to LinuxSampler.
700           */           */
701          public static void          public static void
702          reconnect() {          reconnect() { initSamplerModel(getCurrentServer()); }
703                  initSamplerModel();          
704                  fireReconnectEvent();          private static Server currentServer = null;
705            
706            /**
707             * Gets the server, to which the frontend is going to connect
708             * or is already connected.
709             */
710            public static Server
711            getCurrentServer() { return currentServer; }
712            
713            /**
714             * Sets the current server.
715             */
716            public static void
717            setCurrentServer(Server server) {
718                    if(server == currentServer) return;
719                    connectionFailureCount = 0;
720                    currentServer = server;
721          }          }
722                    
723          /**          /**
724           * This method updates the information about the backend state.           * Sets the LSCP client's read timeout.
725             * @param timeout The new timeout value (in seconds).
726           */           */
727          public static void          public static void
728            setClientReadTimeout(int timeout) {
729                    getTaskQueue().add(new Global.SetClientReadTimeout(timeout));
730            }
731            
732            /**
733             * This method updates the information about the backend state.
734             */
735            private static void
736          initSamplerModel() {          initSamplerModel() {
737                    Server srv = getMainFrame().getServer();
738                    if(srv == null) return;
739                    initSamplerModel(srv);
740            }
741            
742            /**
743             * This method updates the information about the backend state.
744             */
745            private static void
746            initSamplerModel(Server srv) {
747                    setCurrentServer(srv);
748                    final SetServerAddress ssa = new SetServerAddress(srv.getAddress(), srv.getPort());
749                    
750                  final DefaultSamplerModel model = (DefaultSamplerModel)getSamplerModel();                  final DefaultSamplerModel model = (DefaultSamplerModel)getSamplerModel();
751                                    
752                  final GetServerInfo gsi = new GetServerInfo();                  final Global.GetServerInfo gsi = new Global.GetServerInfo();
753                  gsi.addTaskListener(new TaskListener() {                  gsi.addTaskListener(new TaskListener() {
754                          public void                          public void
755                          taskPerformed(TaskEvent e) {                          taskPerformed(TaskEvent e) {
756                                  if(!gsi.doneWithErrors()) model.setServerInfo(gsi.getResult());                                  if(gsi.doneWithErrors()) return;
757                                    
758                                    model.setServerInfo(gsi.getResult());
759                                    
760                                    if(CC.getViewConfig().getInstrumentsDbSupport()) {
761                                            getInstrumentsDbTreeModel();
762                                    }
763                          }                          }
764                  });                  });
765                                    
# Line 565  public class CC { Line 815  public class CC {
815                  uc.addTaskListener(new TaskListener() {                  uc.addTaskListener(new TaskListener() {
816                          public void                          public void
817                          taskPerformed(TaskEvent e) {                          taskPerformed(TaskEvent e) {
818                                  for(SamplerChannelModel c : model.getChannelModels()) {                                  for(SamplerChannelModel c : model.getChannels()) {
819                                          if(c.getChannelInfo().getEngine() == null) continue;                                          if(c.getChannelInfo().getEngine() == null) continue;
820                                                                                    
821                                          Channel.GetFxSends gfs = new Channel.GetFxSends();                                          Channel.GetFxSends gfs = new Channel.GetFxSends();
# Line 573  public class CC { Line 823  public class CC {
823                                          gfs.addTaskListener(new GetFxSendsListener());                                          gfs.addTaskListener(new GetFxSendsListener());
824                                          getTaskQueue().add(gfs);                                          getTaskQueue().add(gfs);
825                                  }                                  }
826                                    
827                                    // TODO: This should be done after the fx sends are set
828                                    //CC.getSamplerModel().setModified(false);
829                          }                          }
830                  });                  });
831                                    
832                                    
833                  final Connect cnt = new Connect();                  final Connect cnt = new Connect();
834                    boolean b = preferences().getBoolProperty(JSPrefs.LAUNCH_BACKEND_LOCALLY);
835                    if(b && srv.isLocal() && backendProcess == null) cnt.setSilent(true);
836                  cnt.addTaskListener(new TaskListener() {                  cnt.addTaskListener(new TaskListener() {
837                          public void                          public void
838                          taskPerformed(TaskEvent e) {                          taskPerformed(TaskEvent e) {
839                                  if(cnt.doneWithErrors()) return;                                  if(cnt.doneWithErrors()) {
840                                            onConnectFailure();
841                                            return;
842                                    }
843                                                                    
844                                  getTaskQueue().add(gsi);                                  getTaskQueue().add(gsi);
845                                  getTaskQueue().add(gaod);                                  getTaskQueue().add(gaod);
# Line 591  public class CC { Line 849  public class CC {
849                                  getTaskQueue().add(mgim);                                  getTaskQueue().add(mgim);
850                                  getTaskQueue().add(new Midi.UpdateDevices());                                  getTaskQueue().add(new Midi.UpdateDevices());
851                                  getTaskQueue().add(new Audio.UpdateDevices());                                  getTaskQueue().add(new Audio.UpdateDevices());
852                                  getTaskQueue().add(uc);                                  addTask(uc);
853                                    
854                                    int vl = preferences().getIntProperty(JSPrefs.GLOBAL_VOICE_LIMIT);
855                                    int sl = preferences().getIntProperty(JSPrefs.GLOBAL_STREAM_LIMIT);
856                                    
857                                    getTaskQueue().add(new Global.SetPolyphony(vl, sl));
858                                    
859                                    fireConnectionEstablishedEvent();
860                            }
861                    });
862                    
863                    ssa.addTaskListener(new TaskListener() {
864                            public void
865                            taskPerformed(TaskEvent e) {
866                                    int t = preferences().getIntProperty(JSPrefs.SOCKET_READ_TIMEOUT);
867                                    CC.setClientReadTimeout(t * 1000);
868                                    CC.getTaskQueue().add(cnt);
869                            }
870                    });
871                    
872                    getSamplerModel().reset();
873                    if(instrumentsDbTreeModel != null) {
874                            instrumentsDbTreeModel.reset();
875                            instrumentsDbTreeModel = null;
876                    }
877                    
878                    getTaskQueue().removePendingTasks();
879                    getTaskQueue().add(ssa);
880                    
881                    fireReconnectEvent();
882            }
883            
884            private static void
885            onConnectFailure() {
886                    connectionFailureCount++;
887                    if(connectionFailureCount > 50) { // to prevent eventual infinite loop
888                            getLogger().warning("Reached maximum number of connection failures");
889                            return;
890                    }
891                    
892                    try {
893                            if(launchBackend()) {
894                                    int i = preferences().getIntProperty(JSPrefs.BACKEND_LAUNCH_DELAY);
895                                    if(i < 1) {
896                                            initSamplerModel(getCurrentServer());
897                                            return;
898                                    }
899                                    
900                                    LaunchBackend lb = new LaunchBackend(i, getBackendMonitor());
901                                    //CC.getTaskQueue().add(lb);
902                                    new Thread(lb).start();
903                                    return;
904                          }                          }
905                    } catch(Exception x) {
906                            final String s = JSI18n.i18n.getError("CC.failedToLaunchBackend");
907                            CC.getLogger().log(Level.INFO, s, x);
908                            
909                            SwingUtilities.invokeLater(new Runnable() {
910                                    public void
911                                    run() { HF.showErrorMessage(s); }
912                            });
913                            return;
914                    }
915                    
916                    retryToConnect();
917            }
918            
919            private static void
920            retryToConnect() {
921                    javax.swing.SwingUtilities.invokeLater(new Runnable() {
922                            public void
923                            run() { changeBackend(); }
924                  });                  });
925                  getTaskQueue().add(cnt);          }
926            
927            public static void
928            changeBackend() {
929                    Server s = getMainFrame().getServer(true);
930                    if(s != null) {
931                            connectionFailureCount = 0; // cleared because this change due to user interaction
932                            initSamplerModel(s);
933                    }
934            }
935            
936            private static final Vector<ActionListener> pListeners = new Vector<ActionListener>();
937            
938            /**
939             * Registers the specified listener to be notified when
940             * backend process is created/terminated.
941             * @param l The <code>ActionListener</code> to register.
942             */
943            public static void
944            addBackendProcessListener(ActionListener l) { pListeners.add(l); }
945            
946            /**
947             * Removes the specified listener.
948             * @param l The <code>ActionListener</code> to remove.
949             */
950            public static void
951            removeBackendProcessListener(ActionListener l) { pListeners.remove(l); }
952            
953            private static void
954            fireBackendProcessEvent() {
955                    ActionEvent e = new ActionEvent(CC.class, ActionEvent.ACTION_PERFORMED, null);
956                    for(ActionListener l : pListeners) l.actionPerformed(e);
957            }
958            
959            private static Process backendProcess = null;
960            
961            public static Process
962            getBackendProcess() { return backendProcess; }
963            
964            private static final Object backendMonitor = new Object();
965            
966            public static Object
967            getBackendMonitor() { return backendMonitor; }
968            
969            private static boolean
970            launchBackend() throws Exception {
971                    if(backendProcess != null) {
972                            try {
973                                    int i = backendProcess.exitValue();
974                                    getLogger().info("Backend exited with exit value " + i);
975                                    backendProcess = null;
976                                    fireBackendProcessEvent();
977                            } catch(IllegalThreadStateException x) { return false; }
978                    }
979                    
980                    if(!preferences().getBoolProperty(JSPrefs.LAUNCH_BACKEND_LOCALLY)) return false;
981                    if(connectionFailureCount > 1) return false;
982                    
983                    Server  s = getCurrentServer();
984                    if(s != null && s.isLocal()) {
985                            String cmd = preferences().getStringProperty(JSPrefs.BACKEND_LAUNCH_COMMAND);
986                            backendProcess = Runtime.getRuntime().exec(cmd);
987                            fireBackendProcessEvent();
988                            return true;
989                    }
990                    
991                    return false;
992          }          }
993                    
994          private static class GetFxSendsListener implements TaskListener {          private static class GetFxSendsListener implements TaskListener {
995                    @Override
996                  public void                  public void
997                  taskPerformed(TaskEvent e) {                  taskPerformed(TaskEvent e) {
998                          Channel.GetFxSends gfs = (Channel.GetFxSends)e.getSource();                          Channel.GetFxSends gfs = (Channel.GetFxSends)e.getSource();
999                          if(gfs.doneWithErrors()) return;                          if(gfs.doneWithErrors()) return;
1000                          SamplerChannelModel m = getSamplerModel().getChannelModel(gfs.getChannel());                          SamplerChannelModel m = getSamplerModel().getChannelById(gfs.getChannel());
1001                          m.removeAllFxSends();                          m.removeAllFxSends();
1002                                                    
1003                          for(FxSend fxs : gfs.getResult()) m.addFxSend(fxs);                          for(FxSend fxs : gfs.getResult()) m.addFxSend(fxs);
# Line 631  public class CC { Line 1026  public class CC {
1026          exportInstrMapsToLscpScript(Client lscpClient) {          exportInstrMapsToLscpScript(Client lscpClient) {
1027                  try {                  try {
1028                          lscpClient.removeAllMidiInstrumentMaps();                          lscpClient.removeAllMidiInstrumentMaps();
1029                          MidiInstrumentMap[] maps = CC.getSamplerModel().getMidiInstrumentMaps();                          MidiInstrumentMap[] maps = getSamplerModel().getMidiInstrumentMaps();
1030                          for(int i = 0; i < maps.length; i++) {                          for(int i = 0; i < maps.length; i++) {
1031                                  lscpClient.addMidiInstrumentMap(maps[i].getName());                                  lscpClient.addMidiInstrumentMap(maps[i].getName());
1032                                  exportInstrumentsToLscpScript(i, maps[i], lscpClient);                                  exportInstrumentsToLscpScript(i, maps[i], lscpClient);
1033                          }                          }
1034                  } catch(Exception e) {                  } catch(Exception e) {
1035                          CC.getLogger().log(Level.FINE, HF.getErrorMessage(e), e);                          getLogger().log(Level.FINE, HF.getErrorMessage(e), e);
1036                          HF.showErrorMessage(e);                          HF.showErrorMessage(e);
1037                  }                  }
1038          }          }
# Line 646  public class CC { Line 1041  public class CC {
1041          exportInstrumentsToLscpScript(int mapId, MidiInstrumentMap map, Client lscpClient)          exportInstrumentsToLscpScript(int mapId, MidiInstrumentMap map, Client lscpClient)
1042                                                                                  throws Exception {                                                                                  throws Exception {
1043                    
1044                    boolean b = preferences().getBoolProperty(JSPrefs.LOAD_MIDI_INSTRUMENTS_IN_BACKGROUND);
1045                    
1046                  for(MidiInstrument i : map.getAllMidiInstruments()) {                  for(MidiInstrument i : map.getAllMidiInstruments()) {
1047                          lscpClient.mapMidiInstrument(mapId, i.getInfo().getEntry(), i.getInfo());                          lscpClient.mapMidiInstrument(mapId, i.getInfo().getEntry(), i.getInfo(), b);
1048                  }                  }
1049          }          }
1050                    
1051          public static String          public static String
1052          exportSessionToLscpScript() {          exportSessionToLscpScript() {
1053                    getSamplerModel().setModified(false);
1054                    
1055                  StringBuffer sb = new StringBuffer("# Exported by: ");                  StringBuffer sb = new StringBuffer("# Exported by: ");
1056                  sb.append("JSampler - a java front-end for LinuxSampler\r\n# Version: ");                  sb.append("JSampler - a java front-end for LinuxSampler\r\n# Version: ");
1057                  sb.append(JSampler.VERSION).append("\r\n");                  sb.append(JSampler.VERSION).append("\r\n");
# Line 667  public class CC { Line 1066  public class CC {
1066                          sb.append(out.toString());                          sb.append(out.toString());
1067                          out.reset();                          out.reset();
1068                          sb.append("\r\n");                          sb.append("\r\n");
1069                          lscpClient.setVolume(CC.getSamplerModel().getVolume());                          lscpClient.setVolume(getSamplerModel().getVolume());
1070                          sb.append(out.toString());                          sb.append(out.toString());
1071                          out.reset();                          out.reset();
1072                          sb.append("\r\n");                          sb.append("\r\n");
1073                  } catch(Exception e) { CC.getLogger().log(Level.FINE, HF.getErrorMessage(e), e); }                  } catch(Exception e) { getLogger().log(Level.FINE, HF.getErrorMessage(e), e); }
1074                                                                    
1075                  MidiDeviceModel[] mDevs = getSamplerModel().getMidiDeviceModels();                  MidiDeviceModel[] mDevs = getSamplerModel().getMidiDevices();
1076                  for(int i = 0; i < mDevs.length; i++) {                  for(int i = 0; i < mDevs.length; i++) {
1077                          exportMidiDeviceToLscpScript(mDevs[i].getDeviceInfo(), i, lscpClient);                          exportMidiDeviceToLscpScript(mDevs[i].getDeviceInfo(), i, lscpClient);
1078                          sb.append(out.toString());                          sb.append(out.toString());
# Line 681  public class CC { Line 1080  public class CC {
1080                          sb.append("\r\n");                          sb.append("\r\n");
1081                  }                  }
1082                                    
1083                  AudioDeviceModel[] aDevs = getSamplerModel().getAudioDeviceModels();                  AudioDeviceModel[] aDevs = getSamplerModel().getAudioDevices();
1084                  for(int i = 0; i < aDevs.length; i++) {                  for(int i = 0; i < aDevs.length; i++) {
1085                          exportAudioDeviceToLscpScript(aDevs[i].getDeviceInfo(), i, lscpClient);                          exportAudioDeviceToLscpScript(aDevs[i].getDeviceInfo(), i, lscpClient);
1086                          sb.append(out.toString());                          sb.append(out.toString());
# Line 689  public class CC { Line 1088  public class CC {
1088                          sb.append("\r\n");                          sb.append("\r\n");
1089                  }                  }
1090                                    
1091                  SamplerChannelModel[] channels = getSamplerModel().getChannelModels();                  boolean b = preferences().getBoolProperty(JSPrefs.EXPORT_MIDI_MAPS_TO_SESSION_SCRIPT);
1092                    if(b) {
1093                            exportInstrMapsToLscpScript(lscpClient);
1094                            sb.append(out.toString());
1095                            out.reset();
1096                            sb.append("\r\n");
1097                    }
1098                    
1099                    SamplerChannelModel[] channels = getSamplerModel().getChannels();
1100                                    
1101                  for(int i = 0; i < channels.length; i++) {                  for(int i = 0; i < channels.length; i++) {
1102                          SamplerChannelModel scm = getSamplerModel().getChannelModel(i);                          SamplerChannelModel scm = channels[i];
1103                          exportChannelToLscpScript(scm.getChannelInfo(), i, lscpClient);                          exportChannelToLscpScript(scm.getChannelInfo(), i, lscpClient);
1104                          sb.append(out.toString());                          sb.append(out.toString());
1105                          out.reset();                          out.reset();
# Line 706  public class CC { Line 1113  public class CC {
1113                          sb.append("\r\n");                          sb.append("\r\n");
1114                  }                  }
1115                                    
1116                  exportInstrMapsToLscpScript(lscpClient);                  sb.append(getViewConfig().exportSessionViewConfig());
                 sb.append(out.toString());  
                 out.reset();  
1117                                    
1118                  return sb.toString();                  return sb.toString();
1119          }          }
# Line 731  public class CC { Line 1136  public class CC {
1136                                  }                                  }
1137                          }                          }
1138                  } catch(Exception e) {                  } catch(Exception e) {
1139                          CC.getLogger().log(Level.FINE, HF.getErrorMessage(e), e);                          getLogger().log(Level.FINE, HF.getErrorMessage(e), e);
1140                  }                  }
1141          }          }
1142                    
# Line 751  public class CC { Line 1156  public class CC {
1156                                  }                                  }
1157                          }                          }
1158                  } catch(Exception e) {                  } catch(Exception e) {
1159                          CC.getLogger().log(Level.FINE, HF.getErrorMessage(e), e);                          getLogger().log(Level.FINE, HF.getErrorMessage(e), e);
1160                  }                  }
1161          }          }
1162                    
# Line 760  public class CC { Line 1165  public class CC {
1165                  try {                  try {
1166                          lscpCLient.addSamplerChannel();                          lscpCLient.addSamplerChannel();
1167                                                    
1168                          int i = chn.getMidiInputDevice();                          SamplerModel sm = getSamplerModel();
1169                          if(i != -1) lscpCLient.setChannelMidiInputDevice(chnId, i);                          int id = chn.getMidiInputDevice();
1170                          lscpCLient.setChannelMidiInputPort(chnId, chn.getMidiInputPort());                          if(id != -1) {
1171                          lscpCLient.setChannelMidiInputChannel(chnId, chn.getMidiInputChannel());                                  for(int i = 0; i < sm.getMidiDeviceCount(); i++) {
1172                                                                    if(sm.getMidiDevice(i).getDeviceId() == id) {
1173                          i = chn.getAudioOutputDevice();                                                  lscpCLient.setChannelMidiInputDevice(chnId, i);
1174                          if(i != -1) {                                                  break;
1175                                  lscpCLient.setChannelAudioOutputDevice(chnId, i);                                          }
1176                                    }
1177                                    lscpCLient.setChannelMidiInputPort(chnId, chn.getMidiInputPort());
1178                                    lscpCLient.setChannelMidiInputChannel(chnId, chn.getMidiInputChannel());
1179                            }
1180                            
1181                            if(chn.getEngine() != null) {
1182                                    lscpCLient.loadSamplerEngine(chn.getEngine().getName(), chnId);
1183                                    lscpCLient.setChannelVolume(chnId, chn.getVolume());
1184                                    int mapId = chn.getMidiInstrumentMapId();
1185                                    lscpCLient.setChannelMidiInstrumentMap(chnId, mapId);
1186                            }
1187                            
1188                            id = chn.getAudioOutputDevice();
1189                            if(id != -1) {
1190                                    for(int i = 0; i < sm.getAudioDeviceCount(); i++) {
1191                                            if(sm.getAudioDevice(i).getDeviceId() == id) {
1192                                                    lscpCLient.setChannelAudioOutputDevice(chnId, i);
1193                                                    break;
1194                                            }
1195                                    }
1196                                    
1197                                  Integer[] routing = chn.getAudioOutputRouting();                                  Integer[] routing = chn.getAudioOutputRouting();
1198                                                                    
1199                                  for(int j = 0; j < routing.length; j++) {                                  for(int j = 0; j < routing.length; j++) {
# Line 778  public class CC { Line 1204  public class CC {
1204                                  }                                  }
1205                          }                          }
1206                                                    
                         if(chn.getEngine() != null) {  
                                 lscpCLient.loadSamplerEngine(chn.getEngine().getName(), chnId);  
                                 lscpCLient.setChannelVolume(chnId, chn.getVolume());  
                         }  
                           
1207                          String s = chn.getInstrumentFile();                          String s = chn.getInstrumentFile();
1208                          i = chn.getInstrumentIndex();                          int i = chn.getInstrumentIndex();
1209                          if(s != null) lscpCLient.loadInstrument(s, i, chnId, true);                          if(s != null) lscpCLient.loadInstrument(s, i, chnId, true);
1210                                                    
1211                          if(chn.isMuted()) lscpCLient.setChannelMute(chnId, true);                          if(chn.isMuted()) lscpCLient.setChannelMute(chnId, true);
1212                          if(chn.isSoloChannel()) lscpCLient.setChannelSolo(chnId, true);                          if(chn.isSoloChannel()) lscpCLient.setChannelSolo(chnId, true);
1213                  } catch(Exception e) {                  } catch(Exception e) {
1214                          CC.getLogger().log(Level.FINE, HF.getErrorMessage(e), e);                          getLogger().log(Level.FINE, HF.getErrorMessage(e), e);
1215                  }                  }
1216          }          }
1217                    
# Line 809  public class CC { Line 1230  public class CC {
1230                                  }                                  }
1231                          }                          }
1232                  } catch(Exception e) {                  } catch(Exception e) {
1233                          CC.getLogger().log(Level.FINE, HF.getErrorMessage(e), e);                          getLogger().log(Level.FINE, HF.getErrorMessage(e), e);
1234                  }                  }
1235          }          }
1236                    
1237            public static void
1238            scheduleInTaskQueue(final Runnable r) {
1239                    Task dummy = new Global.DummyTask();
1240                    dummy.addTaskListener(new TaskListener() {
1241                            public void
1242                            taskPerformed(TaskEvent e) {
1243                                    javax.swing.SwingUtilities.invokeLater(r);
1244                            }
1245                    });
1246                    
1247                    getTaskQueue().add(dummy);
1248            }
1249            
1250            public static boolean
1251            verifyConnection() {
1252                    if(getCurrentServer() == null) {
1253                            HF.showErrorMessage(i18n.getError("CC.notConnected"));
1254                            return false;
1255                    }
1256                    
1257                    return true;
1258            }
1259    
1260            public static boolean
1261            isMacOS() {
1262                    return System.getProperty("os.name").toLowerCase().startsWith("mac os x");
1263            }
1264            
1265                    
1266          private final static EventHandler eventHandler = new EventHandler();          private final static EventHandler eventHandler = new EventHandler();
1267                    
# Line 821  public class CC { Line 1270  public class CC {
1270                    
1271          private static class EventHandler implements ChannelCountListener, ChannelInfoListener,          private static class EventHandler implements ChannelCountListener, ChannelInfoListener,
1272                  FxSendCountListener, FxSendInfoListener, StreamCountListener, VoiceCountListener,                  FxSendCountListener, FxSendInfoListener, StreamCountListener, VoiceCountListener,
1273                  TotalVoiceCountListener, TaskQueueListener, OrchestraListener,                  TotalStreamCountListener, TotalVoiceCountListener, TaskQueueListener,
1274                  ListListener<OrchestraModel>, MidiInstrumentCountListener,                  OrchestraListener, ListListener<OrchestraModel>, MidiInstrumentCountListener,
1275                  MidiInstrumentInfoListener, GlobalInfoListener {                  MidiInstrumentInfoListener, GlobalInfoListener, ChannelMidiDataListener {
1276                                    
1277                  /** Invoked when the number of channels has changed. */                  /** Invoked when the number of channels has changed. */
1278                    @Override
1279                  public void                  public void
1280                  channelCountChanged( ChannelCountEvent e) {                  channelCountChanged( ChannelCountEvent e) {
1281                          getTaskQueue().add(new UpdateChannels());                          if(e.getChannelCount() == 0) {
1282                                    /*
1283                                     * This special case is handled because this might be due to
1284                                     * loading a lscp script containing sampler view configuration.
1285                                     */
1286                                    CC.getSamplerModel().removeAllChannels();
1287                                    return;
1288                            }
1289                            addTask(new UpdateChannels());
1290                  }                  }
1291                                    
1292                  /** Invoked when changes to the sampler channel has occured. */                  /** Invoked when changes to the sampler channel has occured. */
1293                    @Override
1294                  public void                  public void
1295                  channelInfoChanged(ChannelInfoEvent e) {                  channelInfoChanged(ChannelInfoEvent e) {
1296                          /*                          /*
# Line 869  public class CC { Line 1328  public class CC {
1328                   * Invoked when the number of effect sends                   * Invoked when the number of effect sends
1329                   * on a particular sampler channel has changed.                   * on a particular sampler channel has changed.
1330                   */                   */
1331                    @Override
1332                  public void                  public void
1333                  fxSendCountChanged(FxSendCountEvent e) {                  fxSendCountChanged(FxSendCountEvent e) {
1334                          getTaskQueue().add(new Channel.UpdateFxSends(e.getChannel()));                          getTaskQueue().add(new Channel.UpdateFxSends(e.getChannel()));
# Line 877  public class CC { Line 1337  public class CC {
1337                  /**                  /**
1338                   * Invoked when the settings of an effect sends are changed.                   * Invoked when the settings of an effect sends are changed.
1339                   */                   */
1340                    @Override
1341                  public void                  public void
1342                  fxSendInfoChanged(FxSendInfoEvent e) {                  fxSendInfoChanged(FxSendInfoEvent e) {
1343                          Task t = new Channel.UpdateFxSendInfo(e.getChannel(), e.getFxSend());                          Task t = new Channel.UpdateFxSendInfo(e.getChannel(), e.getFxSend());
# Line 887  public class CC { Line 1348  public class CC {
1348                   * Invoked when the number of active disk                   * Invoked when the number of active disk
1349                   * streams in a specific sampler channel has changed.                   * streams in a specific sampler channel has changed.
1350                   */                   */
1351                    @Override
1352                  public void                  public void
1353                  streamCountChanged(StreamCountEvent e) {                  streamCountChanged(StreamCountEvent e) {
1354                          SamplerChannelModel scm =                          SamplerChannelModel scm =
1355                                  getSamplerModel().getChannelModel(e.getSamplerChannel());                                  getSamplerModel().getChannelById(e.getSamplerChannel());
1356                                                    
1357                          if(scm == null) {                          if(scm == null) {
1358                                  CC.getLogger().log (                                  CC.getLogger().log (
# Line 909  public class CC { Line 1371  public class CC {
1371                   * Invoked when the number of active voices                   * Invoked when the number of active voices
1372                   * in a specific sampler channel has changed.                   * in a specific sampler channel has changed.
1373                   */                   */
1374                    @Override
1375                  public void                  public void
1376                  voiceCountChanged(VoiceCountEvent e) {                  voiceCountChanged(VoiceCountEvent e) {
1377                          SamplerChannelModel scm =                          SamplerChannelModel scm =
1378                                  getSamplerModel().getChannelModel(e.getSamplerChannel());                                  getSamplerModel().getChannelById(e.getSamplerChannel());
1379                                                    
1380                          if(scm == null) {                          if(scm == null) {
1381                                  CC.getLogger().log (                                  CC.getLogger().log (
# Line 927  public class CC { Line 1390  public class CC {
1390                          scm.setVoiceCount(e.getVoiceCount());                          scm.setVoiceCount(e.getVoiceCount());
1391                  }                  }
1392                                    
1393                    /** Invoked when the total number of active streams has changed. */
1394                    @Override
1395                    public void
1396                    totalStreamCountChanged(TotalStreamCountEvent e) {
1397                            getSamplerModel().updateActiveStreamsInfo(e.getTotalStreamCount());
1398                    }
1399                    
1400                  /** Invoked when the total number of active voices has changed. */                  /** Invoked when the total number of active voices has changed. */
1401                    @Override
1402                  public void                  public void
1403                  totalVoiceCountChanged(TotalVoiceCountEvent e) {                  totalVoiceCountChanged(TotalVoiceCountEvent e) {
1404                          getTaskQueue().add(new UpdateTotalVoiceCount());                          scheduleTask(new UpdateTotalVoiceCount());
1405                  }                  }
1406                                    
1407                  /** Invoked when the number of MIDI instruments in a MIDI instrument map is changed. */                  /** Invoked when the number of MIDI instruments in a MIDI instrument map is changed. */
1408                    @Override
1409                  public void                  public void
1410                  instrumentCountChanged(MidiInstrumentCountEvent e) {                  instrumentCountChanged(MidiInstrumentCountEvent e) {
1411                          getTaskQueue().add(new Midi.UpdateInstruments(e.getMapId()));                          scheduleTask(new Midi.UpdateInstruments(e.getMapId()));
1412                  }                  }
1413                                    
1414                  /** Invoked when a MIDI instrument in a MIDI instrument map is changed. */                  /** Invoked when a MIDI instrument in a MIDI instrument map is changed. */
1415                    @Override
1416                  public void                  public void
1417                  instrumentInfoChanged(MidiInstrumentInfoEvent e) {                  instrumentInfoChanged(MidiInstrumentInfoEvent e) {
1418                          Task t = new Midi.UpdateInstrumentInfo (                          Task t = new Midi.UpdateInstrumentInfo (
# Line 950  public class CC { Line 1423  public class CC {
1423                  }                  }
1424                                    
1425                  /** Invoked when the global volume of the sampler is changed. */                  /** Invoked when the global volume of the sampler is changed. */
1426                    @Override
1427                  public void                  public void
1428                  volumeChanged(GlobalInfoEvent e) {                  volumeChanged(GlobalInfoEvent e) {
1429                          getSamplerModel().setVolume(e.getVolume());                          getSamplerModel().setVolume(e.getVolume());
1430                  }                  }
1431                                    
1432                    @Override
1433                    public void
1434                    voiceLimitChanged(GlobalInfoEvent e) { }
1435                    
1436                    @Override
1437                    public void
1438                    streamLimitChanged(GlobalInfoEvent e) { }
1439                    
1440                  /**                  /**
1441                   * Invoked to indicate that the state of a task queue is changed.                   * Invoked to indicate that the state of a task queue is changed.
1442                   * This method is invoked only from the event-dispatching thread.                   * This method is invoked only from the event-dispatching thread.
1443                   */                   */
1444                    @Override
1445                  public void                  public void
1446                  stateChanged(TaskQueueEvent e) {                  stateChanged(TaskQueueEvent e) {
1447                          switch(e.getEventID()) {                          switch(e.getEventID()) {
# Line 969  public class CC { Line 1452  public class CC {
1452                                  break;                                  break;
1453                          case TASK_DONE:                          case TASK_DONE:
1454                                  EnhancedTask t = (EnhancedTask)e.getSource();                                  EnhancedTask t = (EnhancedTask)e.getSource();
1455                                  if(t.doneWithErrors() && !t.isStopped())                                  if(t.doneWithErrors() && !t.isStopped() && !t.isSilent()) {
1456                                          HF.showErrorMessage(t.getErrorMessage());                                          showError(t);
1457                                    }
1458                                  break;                                  break;
1459                          case NOT_IDLE:                          case NOT_IDLE:
1460                                  timer.start();                                  timer.start();
# Line 982  public class CC { Line 1466  public class CC {
1466                          }                          }
1467                  }                  }
1468                                    
1469                    private void
1470                    showError(final Task t) {
1471                            javax.swing.SwingUtilities.invokeLater(new Runnable() {
1472                                    public void
1473                                    run() {
1474                                            if(t.getErrorDetails() == null) {
1475                                                    HF.showErrorMessage(t.getErrorMessage());
1476                                            } else {
1477                                                    getMainFrame().showDetailedErrorMessage (
1478                                                            getMainFrame(),
1479                                                            t.getErrorMessage(),
1480                                                            t.getErrorDetails()
1481                                                    );
1482                                            }
1483                                    }
1484                            });
1485                    }
1486                    
1487                  /** Invoked when the name of orchestra is changed. */                  /** Invoked when the name of orchestra is changed. */
1488                    @Override
1489                  public void                  public void
1490                  nameChanged(OrchestraEvent e) { saveOrchestras(); }                  nameChanged(OrchestraEvent e) { saveOrchestras(); }
1491                    
1492                  /** Invoked when the description of orchestra is changed. */                  /** Invoked when the description of orchestra is changed. */
1493                    @Override
1494                  public void                  public void
1495                  descriptionChanged(OrchestraEvent e) { saveOrchestras(); }                  descriptionChanged(OrchestraEvent e) { saveOrchestras(); }
1496                    
1497                  /** Invoked when an instrument is added to the orchestra. */                  /** Invoked when an instrument is added to the orchestra. */
1498                    @Override
1499                  public void                  public void
1500                  instrumentAdded(OrchestraEvent e) { saveOrchestras(); }                  instrumentAdded(OrchestraEvent e) { saveOrchestras(); }
1501                    
1502                  /** Invoked when an instrument is removed from the orchestra. */                  /** Invoked when an instrument is removed from the orchestra. */
1503                    @Override
1504                  public void                  public void
1505                  instrumentRemoved(OrchestraEvent e) { saveOrchestras(); }                  instrumentRemoved(OrchestraEvent e) { saveOrchestras(); }
1506                    
1507                  /** Invoked when the settings of an instrument are changed. */                  /** Invoked when the settings of an instrument are changed. */
1508                    @Override
1509                  public void                  public void
1510                  instrumentChanged(OrchestraEvent e) { saveOrchestras(); }                  instrumentChanged(OrchestraEvent e) { saveOrchestras(); }
1511                                    
1512                  /** Invoked when an orchestra is added to the orchestra list. */                  /** Invoked when an orchestra is added to the orchestra list. */
1513                    @Override
1514                  public void                  public void
1515                  entryAdded(ListEvent<OrchestraModel> e) {                  entryAdded(ListEvent<OrchestraModel> e) {
1516                          e.getEntry().addOrchestraListener(getHandler());                          e.getEntry().addOrchestraListener(getHandler());
# Line 1010  public class CC { Line 1518  public class CC {
1518                  }                  }
1519                    
1520                  /** Invoked when an orchestra is removed from the orchestra list. */                  /** Invoked when an orchestra is removed from the orchestra list. */
1521                    @Override
1522                  public void                  public void
1523                  entryRemoved(ListEvent<OrchestraModel> e) {                  entryRemoved(ListEvent<OrchestraModel> e) {
1524                          e.getEntry().removeOrchestraListener(getHandler());                          e.getEntry().removeOrchestraListener(getHandler());
1525                          saveOrchestras();                          saveOrchestras();
1526                  }                  }
1527                    
1528                    /**
1529                     * Invoked when MIDI data arrives.
1530                     */
1531                    @Override
1532                    public void
1533                    midiDataArrived(final ChannelMidiDataEvent e) {
1534                            try {
1535                                    javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
1536                                            public void
1537                                            run() { fireChannelMidiDataEvent(e); }
1538                                    });
1539                            } catch(Exception x) {
1540                                    CC.getLogger().log(Level.INFO, "Failed!", x);
1541                            }
1542                    }
1543            }
1544            
1545            private static void
1546            fireChannelMidiDataEvent(ChannelMidiDataEvent e) {
1547                    SamplerChannelModel chn;
1548                    chn = CC.getSamplerModel().getChannelById(e.getChannelId());
1549                    if(chn == null) {
1550                            CC.getLogger().info("Unknown channel ID: " + e.getChannelId());
1551                    }
1552                    
1553                    ((DefaultSamplerChannelModel)chn).fireMidiDataEvent(e);
1554          }          }
1555                    
1556          private static final AudioDeviceCountListener audioDeviceCountListener =          private static final AudioDeviceCountListener audioDeviceCountListener =
# Line 1022  public class CC { Line 1558  public class CC {
1558                    
1559          private static class AudioDeviceCountListener implements ItemCountListener {          private static class AudioDeviceCountListener implements ItemCountListener {
1560                  /** Invoked when the number of audio output devices has changed. */                  /** Invoked when the number of audio output devices has changed. */
1561                    @Override
1562                  public void                  public void
1563                  itemCountChanged(ItemCountEvent e) {                  itemCountChanged(ItemCountEvent e) {
1564                          getTaskQueue().add(new Audio.UpdateDevices());                          getTaskQueue().add(new Audio.UpdateDevices());
# Line 1033  public class CC { Line 1570  public class CC {
1570                    
1571          private static class AudioDeviceInfoListener implements ItemInfoListener {          private static class AudioDeviceInfoListener implements ItemInfoListener {
1572                  /** Invoked when the audio output device's settings are changed. */                  /** Invoked when the audio output device's settings are changed. */
1573                    @Override
1574                  public void                  public void
1575                  itemInfoChanged(ItemInfoEvent e) {                  itemInfoChanged(ItemInfoEvent e) {
1576                          getTaskQueue().add(new Audio.UpdateDeviceInfo(e.getItemID()));                          getTaskQueue().add(new Audio.UpdateDeviceInfo(e.getItemID()));
# Line 1044  public class CC { Line 1582  public class CC {
1582                    
1583          private static class MidiDeviceCountListener implements ItemCountListener {          private static class MidiDeviceCountListener implements ItemCountListener {
1584                  /** Invoked when the number of MIDI input devices has changed. */                  /** Invoked when the number of MIDI input devices has changed. */
1585                    @Override
1586                  public void                  public void
1587                  itemCountChanged(ItemCountEvent e) {                  itemCountChanged(ItemCountEvent e) {
1588                          getTaskQueue().add(new Midi.UpdateDevices());                          getTaskQueue().add(new Midi.UpdateDevices());
# Line 1055  public class CC { Line 1594  public class CC {
1594                    
1595          private static class MidiDeviceInfoListener implements ItemInfoListener {          private static class MidiDeviceInfoListener implements ItemInfoListener {
1596                  /** Invoked when the MIDI input device's settings are changed. */                  /** Invoked when the MIDI input device's settings are changed. */
1597                    @Override
1598                  public void                  public void
1599                  itemInfoChanged(ItemInfoEvent e) {                  itemInfoChanged(ItemInfoEvent e) {
1600                          getTaskQueue().add(new Midi.UpdateDeviceInfo(e.getItemID()));                          getTaskQueue().add(new Midi.UpdateDeviceInfo(e.getItemID()));
# Line 1066  public class CC { Line 1606  public class CC {
1606                    
1607          private static class MidiInstrMapCountListener implements ItemCountListener {          private static class MidiInstrMapCountListener implements ItemCountListener {
1608                  /** Invoked when the number of MIDI instrument maps is changed. */                  /** Invoked when the number of MIDI instrument maps is changed. */
1609                    @Override
1610                  public void                  public void
1611                  itemCountChanged(ItemCountEvent e) {                  itemCountChanged(ItemCountEvent e) {
1612                          getTaskQueue().add(new Midi.UpdateInstrumentMaps());                          getTaskQueue().add(new Midi.UpdateInstrumentMaps());
# Line 1077  public class CC { Line 1618  public class CC {
1618                    
1619          private static class MidiInstrMapInfoListener implements ItemInfoListener {          private static class MidiInstrMapInfoListener implements ItemInfoListener {
1620                  /** Invoked when the MIDI instrument map's settings are changed. */                  /** Invoked when the MIDI instrument map's settings are changed. */
1621                    @Override
1622                  public void                  public void
1623                  itemInfoChanged(ItemInfoEvent e) {                  itemInfoChanged(ItemInfoEvent e) {
1624                          getTaskQueue().add(new Midi.UpdateInstrumentMapInfo(e.getItemID()));                          getTaskQueue().add(new Midi.UpdateInstrumentMapInfo(e.getItemID()));

Legend:
Removed from v.1143  
changed lines
  Added in v.1864

  ViewVC Help
Powered by ViewVC