/[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 1776 by iliev, Thu Sep 11 18:48:36 2008 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-2008 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 42  import java.util.logging.StreamHandler; Line 41  import java.util.logging.StreamHandler;
41    
42  import javax.swing.Timer;  import javax.swing.Timer;
43    
44    import javax.swing.event.ChangeEvent;
45    import javax.swing.event.ChangeListener;
46    
47  import net.sf.juife.Task;  import net.sf.juife.Task;
48  import net.sf.juife.TaskQueue;  import net.sf.juife.TaskQueue;
49    
# Line 59  import org.jsampler.task.*; Line 61  import org.jsampler.task.*;
61    
62  import org.jsampler.view.JSMainFrame;  import org.jsampler.view.JSMainFrame;
63  import org.jsampler.view.JSProgress;  import org.jsampler.view.JSProgress;
64    import org.jsampler.view.JSViewConfig;
65    import org.jsampler.view.InstrumentsDbTreeModel;
66    
67  import org.linuxsampler.lscp.AudioOutputChannel;  import org.linuxsampler.lscp.AudioOutputChannel;
68  import org.linuxsampler.lscp.AudioOutputDevice;  import org.linuxsampler.lscp.AudioOutputDevice;
# Line 86  public class CC { Line 90  public class CC {
90          private static Handler handler;          private static Handler handler;
91          private static FileOutputStream fos;          private static FileOutputStream fos;
92                    
93            private static JSViewConfig viewConfig = null;
94          private static JSMainFrame mainFrame = null;          private static JSMainFrame mainFrame = null;
95          private static JSProgress progress = null;          private static JSProgress progress = null;
96                    
# Line 122  public class CC { Line 127  public class CC {
127          getTaskQueue() { return taskQueue; }          getTaskQueue() { return taskQueue; }
128                    
129          /**          /**
130             * Adds the specified task to the task queue. All task in the
131             * queue equal to the specified task are removed from the queue.
132             */
133            public static void
134            scheduleTask(Task t) {
135                    while(getTaskQueue().removeTask(t)) { }
136                    
137                    getTaskQueue().add(t);
138            }
139            
140            /**
141             * Adds the specified task to the task queue only if the last
142             * task in the is not equal to <code>t</code>.
143             */
144            public static void
145            addTask(Task t) {
146                    Task[] tasks = getTaskQueue().getPendingTasks();
147                    if(tasks.length > 0 && tasks[tasks.length - 1].equals(t)) return;
148                    getTaskQueue().add(t);
149            }
150            
151            /**
152             * Gets the configuration of the current view.
153             */
154            public static JSViewConfig
155            getViewConfig() { return viewConfig; }
156            
157            public static JSPrefs
158            preferences() { return getViewConfig().preferences(); }
159            
160            /**
161             * Sets the configuration of the current view.
162             */
163            public static void
164            setViewConfig(JSViewConfig viewConfig) { CC.viewConfig = viewConfig; }
165            
166            /**
167           * Returns the main window of this application.           * Returns the main window of this application.
168           * @return The main window of this application.           * @return The main window of this application.
169           */           */
# Line 190  public class CC { Line 232  public class CC {
232                  handler.setLevel(Level.FINE);                  handler.setLevel(Level.FINE);
233                  getLogger().addHandler(handler);                  getLogger().addHandler(handler);
234                  getLogger().setLevel(Level.FINE);                  getLogger().setLevel(Level.FINE);
                 Logger.getLogger("org.linuxsampler.lscp").addHandler(handler);  
235                  Logger.getLogger("org.linuxsampler.lscp").setLevel(Level.FINE);                  Logger.getLogger("org.linuxsampler.lscp").setLevel(Level.FINE);
236                    Logger.getLogger("org.linuxsampler.lscp").addHandler(handler);
237                                    
238                  // Flushing logs on every second                  // Flushing logs on every second
239                  new java.util.Timer().schedule(new java.util.TimerTask() {                  new java.util.Timer().schedule(new java.util.TimerTask() {
# Line 199  public class CC { Line 241  public class CC {
241                          run() { if(handler != null) handler.flush(); }                          run() { if(handler != null) handler.flush(); }
242                  }, 1000, 1000);                  }, 1000, 1000);
243                                    
244                  CC.getLogger().fine("CC.jsStarted");                  getLogger().fine("CC.jsStarted");
245                                    
246                  HF.setUIDefaultFont(Prefs.getInterfaceFont());                  HF.setUIDefaultFont(Prefs.getInterfaceFont());
247                                    
                 getClient().setServerAddress(Prefs.getLSAddress());  
                 getClient().setServerPort(Prefs.getLSPort());  
                   
248                  timer.setRepeats(false);                  timer.setRepeats(false);
249                                    
250                  timer.addActionListener(new ActionListener() {                  timer.addActionListener(new ActionListener() {
# Line 235  public class CC { Line 274  public class CC {
274                  getClient().removeVoiceCountListener(getHandler());                  getClient().removeVoiceCountListener(getHandler());
275                  getClient().addVoiceCountListener(getHandler());                  getClient().addVoiceCountListener(getHandler());
276                                    
277                    getClient().removeTotalStreamCountListener(getHandler());
278                    getClient().addTotalStreamCountListener(getHandler());
279                    
280                  getClient().removeTotalVoiceCountListener(getHandler());                  getClient().removeTotalVoiceCountListener(getHandler());
281                  getClient().addTotalVoiceCountListener(getHandler());                  getClient().addTotalVoiceCountListener(getHandler());
282                                    
# Line 264  public class CC { Line 306  public class CC {
306                                    
307                  getClient().removeGlobalInfoListener(getHandler());                  getClient().removeGlobalInfoListener(getHandler());
308                  getClient().addGlobalInfoListener(getHandler());                  getClient().addGlobalInfoListener(getHandler());
309                    
310                    getClient().removeChannelMidiDataListener(getHandler());
311                    getClient().addChannelMidiDataListener(getHandler());
312          }          }
313                    
314          /**          /**
# Line 277  public class CC { Line 322  public class CC {
322          checkJSamplerHome() {          checkJSamplerHome() {
323                  if(getJSamplerHome() != null) {                  if(getJSamplerHome() != null) {
324                          File f = new File(getJSamplerHome());                          File f = new File(getJSamplerHome());
325                          if(f.isDirectory()) return;                          if(f.exists() && f.isDirectory()) {
326                                    return;
327                            }
328                  }                  }
329                                    
330                  CC.getMainFrame().installJSamplerHome();                  getMainFrame().installJSamplerHome();
331          }          }
332                    
333          /**          /**
# Line 293  public class CC { Line 340  public class CC {
340          public static void          public static void
341          changeJSamplerHome(String path) {          changeJSamplerHome(String path) {
342                  File fNew = new File(path);                  File fNew = new File(path);
343                  if(fNew.isFile()) {                  if(fNew.exists() && fNew.isFile()) {
344                          HF.showErrorMessage(i18n.getError("CC.JSamplerHomeIsNotDir!"));                          HF.showErrorMessage(i18n.getError("CC.JSamplerHomeIsNotDir!"));
345                          return;                          return;
346                  }                  }
347                                    
348                  if(!fNew.isDirectory()) {                  if(!fNew.exists()) {
349                          if(!fNew.mkdir()) {                          if(!fNew.mkdir()) {
350                                  String s = fNew.getAbsolutePath();                                  String s = fNew.getAbsolutePath();
351                                  HF.showErrorMessage(i18n.getError("CC.mkdirFailed", s));                                  HF.showErrorMessage(i18n.getError("CC.mkdirFailed", s));
# Line 306  public class CC { Line 353  public class CC {
353                          }                          }
354                  }                  }
355                                    
356                  if(getJSamplerHome() == null) {                  if(getJSamplerHome() == null || path.equals(getJSamplerHome())) {
357                          setJSamplerHome(fNew.getAbsolutePath());                          setJSamplerHome(fNew.getAbsolutePath());
358                          return;                          return;
359                  }                  }
360                                    
361                  File fOld = new File(getJSamplerHome());                  File fOld = new File(getJSamplerHome());
362                  if(!fOld.isDirectory()) {                  if(!fOld.exists() || !fOld.isDirectory()) {
363                          setJSamplerHome(fNew.getAbsolutePath());                          setJSamplerHome(fNew.getAbsolutePath());
364                          return;                          return;
365                  }                  }
# Line 337  public class CC { Line 384  public class CC {
384          public static OrchestraListModel          public static OrchestraListModel
385          getOrchestras() { return orchestras; }          getOrchestras() { return orchestras; }
386                    
387            private final static ServerList servers = new ServerList();
388            
389            /** Returns the server list. */
390            public static ServerList
391            getServerList() { return servers; }
392            
393            private static ServerListListener serverListListener = new ServerListListener();
394            
395            private static class ServerListListener implements ChangeListener {
396                    public void
397                    stateChanged(ChangeEvent e) {
398                            saveServerList();
399                    }
400            }
401            
402            private static final Vector<ChangeListener> idtmListeners = new Vector<ChangeListener>();
403            private static InstrumentsDbTreeModel instrumentsDbTreeModel = null;
404            
405            /**
406             * Gets the tree model of the instruments database.
407             * If the currently used view doesn't have instruments
408             * database support the tree model is initialized on first use.
409             * @return The tree model of the instruments database or
410             * <code>null</code> if the backend doesn't have instruments database support.
411             * @see org.jsampler.view.JSViewConfig#getInstrumentsDbSupport
412             */
413            public static InstrumentsDbTreeModel
414            getInstrumentsDbTreeModel() {
415                    if(getSamplerModel().getServerInfo() == null) return null;
416                    if(!getSamplerModel().getServerInfo().hasInstrumentsDbSupport()) return null;
417                    
418                    if(instrumentsDbTreeModel == null) {
419                            instrumentsDbTreeModel = new InstrumentsDbTreeModel();
420                            for(ChangeListener l : idtmListeners) l.stateChanged(null);
421                    }
422                    
423                    return instrumentsDbTreeModel;
424            }
425            
426            public static void
427            addInstrumentsDbChangeListener(ChangeListener l) {
428                    idtmListeners.add(l);
429            }
430            
431            public static void
432            removeInstrumentsDbChangeListener(ChangeListener l) {
433                    idtmListeners.remove(l);
434            }
435            
436            private static final LostFilesModel lostFilesModel = new LostFilesModel();
437            
438            public static LostFilesModel
439            getLostFilesModel() { return lostFilesModel; }
440            
441          /**          /**
442           * 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>.
443           * 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 449  public class CC {
449          loadOrchestras() {          loadOrchestras() {
450                  if(getJSamplerHome() == null) return;                  if(getJSamplerHome() == null) return;
451                                    
                 //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;  
                 }  
                 ///////  
                   
452                  try {                  try {
453                          String s = getJSamplerHome();                          String s = getJSamplerHome();
                         if(s == null) return;  
454                                                    
455                          File f = new File(s + File.separator + "orchestras.xml.bkp");                          File f = new File(s + File.separator + "orchestras.xml.bkp");
456                          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 463  public class CC {
463                  } catch(Exception x) {                  } catch(Exception x) {
464                          getLogger().log(Level.INFO, HF.getErrorMessage(x), x);                          getLogger().log(Level.INFO, HF.getErrorMessage(x), x);
465                  }                  }
466                    
467                    getOrchestras().addOrchestraListListener(getHandler());
468          }          }
469                    
470                    
# Line 388  public class CC { Line 481  public class CC {
481                  for(int i = 0; i < getOrchestras().getOrchestraCount(); i++) {                  for(int i = 0; i < getOrchestras().getOrchestraCount(); i++) {
482                          getOrchestras().getOrchestra(i).addOrchestraListener(getHandler());                          getOrchestras().getOrchestra(i).addOrchestraListener(getHandler());
483                  }                  }
                 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: "); }  
484          }          }
485                    
486          private static void          private static void
# Line 435  public class CC { Line 515  public class CC {
515          }          }
516                    
517          /**          /**
518             * Loads the servers' info described in <code>&lt;jsampler_home&gt;/servers.xml</code>.
519             * If file with name <code>servers.xml.bkp</code> exist in the JSampler's home
520             * directory, this means that the last save has failed. In that case a recovery file
521             * <code>servers.xml.rec</code> is created and a recovery procedure
522             * will be initiated.
523             */
524            public static void
525            loadServerList() {
526                    if(getJSamplerHome() == null) return;
527                    
528                    try {
529                            String s = getJSamplerHome();
530                            
531                            File f = new File(s + File.separator + "servers.xml.bkp");
532                            if(f.isFile()) HF.createBackup("servers.xml.bkp", "servers.xml.rec");
533                            
534                            FileInputStream fis;
535                            fis = new FileInputStream(s + File.separator + "servers.xml");
536                            
537                            loadServerList(fis);
538                            fis.close();
539                    } catch(Exception x) {
540                            getLogger().log(Level.INFO, HF.getErrorMessage(x), x);
541                    }
542                    
543                    getServerList().addChangeListener(serverListListener);
544                    
545                    /* We should have at least one server to connect. */
546                    if(getServerList().getServerCount() == 0) {
547                            Server server = new Server();
548                            server.setName("127.0.0.1:8888");
549                            server.setAddress("127.0.0.1");
550                            server.setPort(8888);
551                            getServerList().addServer(server);
552                    }
553            }
554            
555            
556            private static void
557            loadServerList(InputStream in) {
558                    Document doc = DOMUtils.readObject(in);
559                    
560                    try { getServerList().readObject(doc.getDocumentElement()); }
561                    catch(Exception x) {
562                            HF.showErrorMessage(x, "Loading server list: ");
563                            return;
564                    }
565            }
566            
567            private static void
568            saveServerList() {
569                    try {
570                            String s = getJSamplerHome();
571                            if(s == null) return;
572                            
573                            HF.createBackup("servers.xml", "servers.xml.bkp");
574                            
575                            FileOutputStream fos;
576                            fos = new FileOutputStream(s + File.separator + "servers.xml", false);
577                            
578                            Document doc = DOMUtils.createEmptyDocument();
579                    
580                            Node node = doc.createElement("temp");
581                            doc.appendChild(node);
582                            
583                            getServerList().writeObject(doc, doc.getDocumentElement());
584                            
585                            doc.replaceChild(node.getFirstChild(), node);
586                    
587                            DOMUtils.writeObject(doc, fos);
588                            
589                            fos.close();
590                            
591                            HF.deleteFile("servers.xml.bkp");
592                    } catch(Exception x) {
593                            HF.showErrorMessage(x, "Saving server list: ");
594                            return;
595                    }
596            }
597            
598            /**
599           * 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.
600           *  @see #cleanExit(int i)           *  @see #cleanExit(int i)
601           */           */
# Line 447  public class CC { Line 608  public class CC {
608           */           */
609          public static void          public static void
610          cleanExit(int i) {          cleanExit(int i) {
611                  CC.getLogger().fine("CC.jsEnded");                  getLogger().fine("CC.jsEnded");
612                  System.exit(i);                  System.exit(i);
613          }          }
614                    
# Line 490  public class CC { Line 651  public class CC {
651          getSamplerModel() { return samplerModel; }          getSamplerModel() { return samplerModel; }
652                    
653          /**          /**
654             * Connects to LinuxSampler.
655             */
656            public static void
657            connect() { initSamplerModel(); }
658            
659            /**
660           * Reconnects to LinuxSampler.           * Reconnects to LinuxSampler.
661           */           */
662          public static void          public static void
663          reconnect() {          reconnect() { initSamplerModel(getCurrentServer()); }
664                  initSamplerModel();          
665                  fireReconnectEvent();          private static Server currentServer = null;
666            
667            /**
668             * Gets the server, to which the frontend is going to connect
669             * or is already connected.
670             */
671            public static Server
672            getCurrentServer() { return currentServer; }
673            
674            /**
675             * Sets the current server.
676             */
677            public static void
678            setCurrentServer(Server server) { currentServer = server; }
679            
680            /**
681             * Sets the LSCP client's read timeout.
682             * @param timeout The new timeout value (in seconds).
683             */
684            public static void
685            setClientReadTimeout(int timeout) {
686                    getTaskQueue().add(new Global.SetClientReadTimeout(timeout));
687          }          }
688                    
689          /**          /**
690           * This method updates the information about the backend state.           * This method updates the information about the backend state.
691           */           */
692          public static void          private static void
693          initSamplerModel() {          initSamplerModel() {
694                    Server srv = getMainFrame().getServer();
695                    if(srv == null) return;
696                    initSamplerModel(srv);
697            }
698            
699            /**
700             * This method updates the information about the backend state.
701             */
702            private static void
703            initSamplerModel(Server srv) {
704                    setCurrentServer(srv);
705                    final SetServerAddress ssa = new SetServerAddress(srv.getAddress(), srv.getPort());
706                    
707                  final DefaultSamplerModel model = (DefaultSamplerModel)getSamplerModel();                  final DefaultSamplerModel model = (DefaultSamplerModel)getSamplerModel();
708                                    
709                  final GetServerInfo gsi = new GetServerInfo();                  final Global.GetServerInfo gsi = new Global.GetServerInfo();
710                  gsi.addTaskListener(new TaskListener() {                  gsi.addTaskListener(new TaskListener() {
711                          public void                          public void
712                          taskPerformed(TaskEvent e) {                          taskPerformed(TaskEvent e) {
713                                  if(!gsi.doneWithErrors()) model.setServerInfo(gsi.getResult());                                  if(gsi.doneWithErrors()) return;
714                                    
715                                    model.setServerInfo(gsi.getResult());
716                                    
717                                    if(CC.getViewConfig().getInstrumentsDbSupport()) {
718                                            getInstrumentsDbTreeModel();
719                                    }
720                          }                          }
721                  });                  });
722                                    
# Line 565  public class CC { Line 772  public class CC {
772                  uc.addTaskListener(new TaskListener() {                  uc.addTaskListener(new TaskListener() {
773                          public void                          public void
774                          taskPerformed(TaskEvent e) {                          taskPerformed(TaskEvent e) {
775                                  for(SamplerChannelModel c : model.getChannelModels()) {                                  for(SamplerChannelModel c : model.getChannels()) {
776                                          if(c.getChannelInfo().getEngine() == null) continue;                                          if(c.getChannelInfo().getEngine() == null) continue;
777                                                                                    
778                                          Channel.GetFxSends gfs = new Channel.GetFxSends();                                          Channel.GetFxSends gfs = new Channel.GetFxSends();
# Line 573  public class CC { Line 780  public class CC {
780                                          gfs.addTaskListener(new GetFxSendsListener());                                          gfs.addTaskListener(new GetFxSendsListener());
781                                          getTaskQueue().add(gfs);                                          getTaskQueue().add(gfs);
782                                  }                                  }
783                                    
784                                    // TODO: This should be done after the fx sends are set
785                                    //CC.getSamplerModel().setModified(false);
786                          }                          }
787                  });                  });
788                                    
# Line 581  public class CC { Line 791  public class CC {
791                  cnt.addTaskListener(new TaskListener() {                  cnt.addTaskListener(new TaskListener() {
792                          public void                          public void
793                          taskPerformed(TaskEvent e) {                          taskPerformed(TaskEvent e) {
794                                  if(cnt.doneWithErrors()) return;                                  if(cnt.doneWithErrors()) {
795                                            setCurrentServer(null);
796                                            retryToConnect();
797                                            return;
798                                    }
799                                                                    
800                                  getTaskQueue().add(gsi);                                  getTaskQueue().add(gsi);
801                                  getTaskQueue().add(gaod);                                  getTaskQueue().add(gaod);
# Line 591  public class CC { Line 805  public class CC {
805                                  getTaskQueue().add(mgim);                                  getTaskQueue().add(mgim);
806                                  getTaskQueue().add(new Midi.UpdateDevices());                                  getTaskQueue().add(new Midi.UpdateDevices());
807                                  getTaskQueue().add(new Audio.UpdateDevices());                                  getTaskQueue().add(new Audio.UpdateDevices());
808                                  getTaskQueue().add(uc);                                  addTask(uc);
809                            }
810                    });
811                    
812                    ssa.addTaskListener(new TaskListener() {
813                            public void
814                            taskPerformed(TaskEvent e) {
815                                    int t = preferences().getIntProperty(JSPrefs.SOCKET_READ_TIMEOUT);
816                                    CC.setClientReadTimeout(t * 1000);
817                                    CC.getTaskQueue().add(cnt);
818                          }                          }
819                  });                  });
820                  getTaskQueue().add(cnt);                  
821                    getSamplerModel().reset();
822                    if(instrumentsDbTreeModel != null) {
823                            instrumentsDbTreeModel.reset();
824                            instrumentsDbTreeModel = null;
825                    }
826                    
827                    getTaskQueue().removePendingTasks();
828                    getTaskQueue().add(ssa);
829                    
830                    fireReconnectEvent();
831            }
832            
833            private static void
834            retryToConnect() {
835                    javax.swing.SwingUtilities.invokeLater(new Runnable() {
836                            public void
837                            run() { changeBackend(); }
838                    });
839            }
840            
841            public static void
842            changeBackend() {
843                    Server s = getMainFrame().getServer(true);
844                    if(s != null) initSamplerModel(s);
845          }          }
846                    
847          private static class GetFxSendsListener implements TaskListener {          private static class GetFxSendsListener implements TaskListener {
# Line 602  public class CC { Line 849  public class CC {
849                  taskPerformed(TaskEvent e) {                  taskPerformed(TaskEvent e) {
850                          Channel.GetFxSends gfs = (Channel.GetFxSends)e.getSource();                          Channel.GetFxSends gfs = (Channel.GetFxSends)e.getSource();
851                          if(gfs.doneWithErrors()) return;                          if(gfs.doneWithErrors()) return;
852                          SamplerChannelModel m = getSamplerModel().getChannelModel(gfs.getChannel());                          SamplerChannelModel m = getSamplerModel().getChannelById(gfs.getChannel());
853                          m.removeAllFxSends();                          m.removeAllFxSends();
854                                                    
855                          for(FxSend fxs : gfs.getResult()) m.addFxSend(fxs);                          for(FxSend fxs : gfs.getResult()) m.addFxSend(fxs);
# Line 631  public class CC { Line 878  public class CC {
878          exportInstrMapsToLscpScript(Client lscpClient) {          exportInstrMapsToLscpScript(Client lscpClient) {
879                  try {                  try {
880                          lscpClient.removeAllMidiInstrumentMaps();                          lscpClient.removeAllMidiInstrumentMaps();
881                          MidiInstrumentMap[] maps = CC.getSamplerModel().getMidiInstrumentMaps();                          MidiInstrumentMap[] maps = getSamplerModel().getMidiInstrumentMaps();
882                          for(int i = 0; i < maps.length; i++) {                          for(int i = 0; i < maps.length; i++) {
883                                  lscpClient.addMidiInstrumentMap(maps[i].getName());                                  lscpClient.addMidiInstrumentMap(maps[i].getName());
884                                  exportInstrumentsToLscpScript(i, maps[i], lscpClient);                                  exportInstrumentsToLscpScript(i, maps[i], lscpClient);
885                          }                          }
886                  } catch(Exception e) {                  } catch(Exception e) {
887                          CC.getLogger().log(Level.FINE, HF.getErrorMessage(e), e);                          getLogger().log(Level.FINE, HF.getErrorMessage(e), e);
888                          HF.showErrorMessage(e);                          HF.showErrorMessage(e);
889                  }                  }
890          }          }
# Line 646  public class CC { Line 893  public class CC {
893          exportInstrumentsToLscpScript(int mapId, MidiInstrumentMap map, Client lscpClient)          exportInstrumentsToLscpScript(int mapId, MidiInstrumentMap map, Client lscpClient)
894                                                                                  throws Exception {                                                                                  throws Exception {
895                    
896                    boolean b = preferences().getBoolProperty(JSPrefs.LOAD_MIDI_INSTRUMENTS_IN_BACKGROUND);
897                    
898                  for(MidiInstrument i : map.getAllMidiInstruments()) {                  for(MidiInstrument i : map.getAllMidiInstruments()) {
899                          lscpClient.mapMidiInstrument(mapId, i.getInfo().getEntry(), i.getInfo());                          lscpClient.mapMidiInstrument(mapId, i.getInfo().getEntry(), i.getInfo(), b);
900                  }                  }
901          }          }
902                    
903          public static String          public static String
904          exportSessionToLscpScript() {          exportSessionToLscpScript() {
905                    getSamplerModel().setModified(false);
906                    
907                  StringBuffer sb = new StringBuffer("# Exported by: ");                  StringBuffer sb = new StringBuffer("# Exported by: ");
908                  sb.append("JSampler - a java front-end for LinuxSampler\r\n# Version: ");                  sb.append("JSampler - a java front-end for LinuxSampler\r\n# Version: ");
909                  sb.append(JSampler.VERSION).append("\r\n");                  sb.append(JSampler.VERSION).append("\r\n");
# Line 667  public class CC { Line 918  public class CC {
918                          sb.append(out.toString());                          sb.append(out.toString());
919                          out.reset();                          out.reset();
920                          sb.append("\r\n");                          sb.append("\r\n");
921                          lscpClient.setVolume(CC.getSamplerModel().getVolume());                          lscpClient.setVolume(getSamplerModel().getVolume());
922                          sb.append(out.toString());                          sb.append(out.toString());
923                          out.reset();                          out.reset();
924                          sb.append("\r\n");                          sb.append("\r\n");
925                  } catch(Exception e) { CC.getLogger().log(Level.FINE, HF.getErrorMessage(e), e); }                  } catch(Exception e) { getLogger().log(Level.FINE, HF.getErrorMessage(e), e); }
926                                                                    
927                  MidiDeviceModel[] mDevs = getSamplerModel().getMidiDeviceModels();                  MidiDeviceModel[] mDevs = getSamplerModel().getMidiDevices();
928                  for(int i = 0; i < mDevs.length; i++) {                  for(int i = 0; i < mDevs.length; i++) {
929                          exportMidiDeviceToLscpScript(mDevs[i].getDeviceInfo(), i, lscpClient);                          exportMidiDeviceToLscpScript(mDevs[i].getDeviceInfo(), i, lscpClient);
930                          sb.append(out.toString());                          sb.append(out.toString());
# Line 681  public class CC { Line 932  public class CC {
932                          sb.append("\r\n");                          sb.append("\r\n");
933                  }                  }
934                                    
935                  AudioDeviceModel[] aDevs = getSamplerModel().getAudioDeviceModels();                  AudioDeviceModel[] aDevs = getSamplerModel().getAudioDevices();
936                  for(int i = 0; i < aDevs.length; i++) {                  for(int i = 0; i < aDevs.length; i++) {
937                          exportAudioDeviceToLscpScript(aDevs[i].getDeviceInfo(), i, lscpClient);                          exportAudioDeviceToLscpScript(aDevs[i].getDeviceInfo(), i, lscpClient);
938                          sb.append(out.toString());                          sb.append(out.toString());
# Line 689  public class CC { Line 940  public class CC {
940                          sb.append("\r\n");                          sb.append("\r\n");
941                  }                  }
942                                    
943                  SamplerChannelModel[] channels = getSamplerModel().getChannelModels();                  boolean b = preferences().getBoolProperty(JSPrefs.EXPORT_MIDI_MAPS_TO_SESSION_SCRIPT);
944                    if(b) {
945                            exportInstrMapsToLscpScript(lscpClient);
946                            sb.append(out.toString());
947                            out.reset();
948                            sb.append("\r\n");
949                    }
950                    
951                    SamplerChannelModel[] channels = getSamplerModel().getChannels();
952                                    
953                  for(int i = 0; i < channels.length; i++) {                  for(int i = 0; i < channels.length; i++) {
954                          SamplerChannelModel scm = getSamplerModel().getChannelModel(i);                          SamplerChannelModel scm = channels[i];
955                          exportChannelToLscpScript(scm.getChannelInfo(), i, lscpClient);                          exportChannelToLscpScript(scm.getChannelInfo(), i, lscpClient);
956                          sb.append(out.toString());                          sb.append(out.toString());
957                          out.reset();                          out.reset();
# Line 706  public class CC { Line 965  public class CC {
965                          sb.append("\r\n");                          sb.append("\r\n");
966                  }                  }
967                                    
968                  exportInstrMapsToLscpScript(lscpClient);                  //sb.append(getViewConfig().exportSessionViewConfig());
                 sb.append(out.toString());  
                 out.reset();  
969                                    
970                  return sb.toString();                  return sb.toString();
971          }          }
# Line 731  public class CC { Line 988  public class CC {
988                                  }                                  }
989                          }                          }
990                  } catch(Exception e) {                  } catch(Exception e) {
991                          CC.getLogger().log(Level.FINE, HF.getErrorMessage(e), e);                          getLogger().log(Level.FINE, HF.getErrorMessage(e), e);
992                  }                  }
993          }          }
994                    
# Line 751  public class CC { Line 1008  public class CC {
1008                                  }                                  }
1009                          }                          }
1010                  } catch(Exception e) {                  } catch(Exception e) {
1011                          CC.getLogger().log(Level.FINE, HF.getErrorMessage(e), e);                          getLogger().log(Level.FINE, HF.getErrorMessage(e), e);
1012                  }                  }
1013          }          }
1014                    
# Line 760  public class CC { Line 1017  public class CC {
1017                  try {                  try {
1018                          lscpCLient.addSamplerChannel();                          lscpCLient.addSamplerChannel();
1019                                                    
1020                          int i = chn.getMidiInputDevice();                          SamplerModel sm = getSamplerModel();
1021                          if(i != -1) lscpCLient.setChannelMidiInputDevice(chnId, i);                          int id = chn.getMidiInputDevice();
1022                          lscpCLient.setChannelMidiInputPort(chnId, chn.getMidiInputPort());                          if(id != -1) {
1023                          lscpCLient.setChannelMidiInputChannel(chnId, chn.getMidiInputChannel());                                  for(int i = 0; i < sm.getMidiDeviceCount(); i++) {
1024                                                                    if(sm.getMidiDevice(i).getDeviceId() == id) {
1025                          i = chn.getAudioOutputDevice();                                                  lscpCLient.setChannelMidiInputDevice(chnId, i);
1026                          if(i != -1) {                                                  break;
1027                                  lscpCLient.setChannelAudioOutputDevice(chnId, i);                                          }
1028                                    }
1029                                    lscpCLient.setChannelMidiInputPort(chnId, chn.getMidiInputPort());
1030                                    lscpCLient.setChannelMidiInputChannel(chnId, chn.getMidiInputChannel());
1031                            }
1032                            
1033                            if(chn.getEngine() != null) {
1034                                    lscpCLient.loadSamplerEngine(chn.getEngine().getName(), chnId);
1035                                    lscpCLient.setChannelVolume(chnId, chn.getVolume());
1036                                    int mapId = chn.getMidiInstrumentMapId();
1037                                    lscpCLient.setChannelMidiInstrumentMap(chnId, mapId);
1038                            }
1039                            
1040                            id = chn.getAudioOutputDevice();
1041                            if(id != -1) {
1042                                    for(int i = 0; i < sm.getAudioDeviceCount(); i++) {
1043                                            if(sm.getAudioDevice(i).getDeviceId() == id) {
1044                                                    lscpCLient.setChannelAudioOutputDevice(chnId, i);
1045                                                    break;
1046                                            }
1047                                    }
1048                                    
1049                                  Integer[] routing = chn.getAudioOutputRouting();                                  Integer[] routing = chn.getAudioOutputRouting();
1050                                                                    
1051                                  for(int j = 0; j < routing.length; j++) {                                  for(int j = 0; j < routing.length; j++) {
# Line 778  public class CC { Line 1056  public class CC {
1056                                  }                                  }
1057                          }                          }
1058                                                    
                         if(chn.getEngine() != null) {  
                                 lscpCLient.loadSamplerEngine(chn.getEngine().getName(), chnId);  
                                 lscpCLient.setChannelVolume(chnId, chn.getVolume());  
                         }  
                           
1059                          String s = chn.getInstrumentFile();                          String s = chn.getInstrumentFile();
1060                          i = chn.getInstrumentIndex();                          int i = chn.getInstrumentIndex();
1061                          if(s != null) lscpCLient.loadInstrument(s, i, chnId, true);                          if(s != null) lscpCLient.loadInstrument(s, i, chnId, true);
1062                                                    
1063                          if(chn.isMuted()) lscpCLient.setChannelMute(chnId, true);                          if(chn.isMuted()) lscpCLient.setChannelMute(chnId, true);
1064                          if(chn.isSoloChannel()) lscpCLient.setChannelSolo(chnId, true);                          if(chn.isSoloChannel()) lscpCLient.setChannelSolo(chnId, true);
1065                  } catch(Exception e) {                  } catch(Exception e) {
1066                          CC.getLogger().log(Level.FINE, HF.getErrorMessage(e), e);                          getLogger().log(Level.FINE, HF.getErrorMessage(e), e);
1067                  }                  }
1068          }          }
1069                    
# Line 809  public class CC { Line 1082  public class CC {
1082                                  }                                  }
1083                          }                          }
1084                  } catch(Exception e) {                  } catch(Exception e) {
1085                          CC.getLogger().log(Level.FINE, HF.getErrorMessage(e), e);                          getLogger().log(Level.FINE, HF.getErrorMessage(e), e);
1086                  }                  }
1087          }          }
1088                    
1089            public static void
1090            scheduleInTaskQueue(final Runnable r) {
1091                    Task dummy = new Global.DummyTask();
1092                    dummy.addTaskListener(new TaskListener() {
1093                            public void
1094                            taskPerformed(TaskEvent e) {
1095                                    javax.swing.SwingUtilities.invokeLater(r);
1096                            }
1097                    });
1098                    
1099                    getTaskQueue().add(dummy);
1100            }
1101            
1102            public static boolean
1103            verifyConnection() {
1104                    if(getCurrentServer() == null) {
1105                            HF.showErrorMessage(i18n.getError("CC.notConnected"));
1106                            return false;
1107                    }
1108                    
1109                    return true;
1110            }
1111            
1112                    
1113          private final static EventHandler eventHandler = new EventHandler();          private final static EventHandler eventHandler = new EventHandler();
1114                    
# Line 821  public class CC { Line 1117  public class CC {
1117                    
1118          private static class EventHandler implements ChannelCountListener, ChannelInfoListener,          private static class EventHandler implements ChannelCountListener, ChannelInfoListener,
1119                  FxSendCountListener, FxSendInfoListener, StreamCountListener, VoiceCountListener,                  FxSendCountListener, FxSendInfoListener, StreamCountListener, VoiceCountListener,
1120                  TotalVoiceCountListener, TaskQueueListener, OrchestraListener,                  TotalStreamCountListener, TotalVoiceCountListener, TaskQueueListener,
1121                  ListListener<OrchestraModel>, MidiInstrumentCountListener,                  OrchestraListener, ListListener<OrchestraModel>, MidiInstrumentCountListener,
1122                  MidiInstrumentInfoListener, GlobalInfoListener {                  MidiInstrumentInfoListener, GlobalInfoListener, ChannelMidiDataListener {
1123                                    
1124                  /** Invoked when the number of channels has changed. */                  /** Invoked when the number of channels has changed. */
1125                  public void                  public void
1126                  channelCountChanged( ChannelCountEvent e) {                  channelCountChanged( ChannelCountEvent e) {
1127                          getTaskQueue().add(new UpdateChannels());                          addTask(new UpdateChannels());
1128                  }                  }
1129                                    
1130                  /** Invoked when changes to the sampler channel has occured. */                  /** Invoked when changes to the sampler channel has occured. */
# Line 890  public class CC { Line 1186  public class CC {
1186                  public void                  public void
1187                  streamCountChanged(StreamCountEvent e) {                  streamCountChanged(StreamCountEvent e) {
1188                          SamplerChannelModel scm =                          SamplerChannelModel scm =
1189                                  getSamplerModel().getChannelModel(e.getSamplerChannel());                                  getSamplerModel().getChannelById(e.getSamplerChannel());
1190                                                    
1191                          if(scm == null) {                          if(scm == null) {
1192                                  CC.getLogger().log (                                  CC.getLogger().log (
# Line 912  public class CC { Line 1208  public class CC {
1208                  public void                  public void
1209                  voiceCountChanged(VoiceCountEvent e) {                  voiceCountChanged(VoiceCountEvent e) {
1210                          SamplerChannelModel scm =                          SamplerChannelModel scm =
1211                                  getSamplerModel().getChannelModel(e.getSamplerChannel());                                  getSamplerModel().getChannelById(e.getSamplerChannel());
1212                                                    
1213                          if(scm == null) {                          if(scm == null) {
1214                                  CC.getLogger().log (                                  CC.getLogger().log (
# Line 927  public class CC { Line 1223  public class CC {
1223                          scm.setVoiceCount(e.getVoiceCount());                          scm.setVoiceCount(e.getVoiceCount());
1224                  }                  }
1225                                    
1226                    /** Invoked when the total number of active streams has changed. */
1227                    public void
1228                    totalStreamCountChanged(TotalStreamCountEvent e) {
1229                            getSamplerModel().updateActiveStreamsInfo(e.getTotalStreamCount());
1230                    }
1231                    
1232                  /** Invoked when the total number of active voices has changed. */                  /** Invoked when the total number of active voices has changed. */
1233                  public void                  public void
1234                  totalVoiceCountChanged(TotalVoiceCountEvent e) {                  totalVoiceCountChanged(TotalVoiceCountEvent e) {
# Line 936  public class CC { Line 1238  public class CC {
1238                  /** 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. */
1239                  public void                  public void
1240                  instrumentCountChanged(MidiInstrumentCountEvent e) {                  instrumentCountChanged(MidiInstrumentCountEvent e) {
1241                          getTaskQueue().add(new Midi.UpdateInstruments(e.getMapId()));                          scheduleTask(new Midi.UpdateInstruments(e.getMapId()));
1242                  }                  }
1243                                    
1244                  /** Invoked when a MIDI instrument in a MIDI instrument map is changed. */                  /** Invoked when a MIDI instrument in a MIDI instrument map is changed. */
# Line 969  public class CC { Line 1271  public class CC {
1271                                  break;                                  break;
1272                          case TASK_DONE:                          case TASK_DONE:
1273                                  EnhancedTask t = (EnhancedTask)e.getSource();                                  EnhancedTask t = (EnhancedTask)e.getSource();
1274                                  if(t.doneWithErrors() && !t.isStopped())                                  if(t.doneWithErrors() && !t.isStopped()) {
1275                                          HF.showErrorMessage(t.getErrorMessage());                                          showError(t);
1276                                    }
1277                                  break;                                  break;
1278                          case NOT_IDLE:                          case NOT_IDLE:
1279                                  timer.start();                                  timer.start();
# Line 982  public class CC { Line 1285  public class CC {
1285                          }                          }
1286                  }                  }
1287                                    
1288                    private void
1289                    showError(final Task t) {
1290                            javax.swing.SwingUtilities.invokeLater(new Runnable() {
1291                                    public void
1292                                    run() {
1293                                            if(t.getErrorDetails() == null) {
1294                                                    HF.showErrorMessage(t.getErrorMessage());
1295                                            } else {
1296                                                    getMainFrame().showDetailedErrorMessage (
1297                                                            getMainFrame(),
1298                                                            t.getErrorMessage(),
1299                                                            t.getErrorDetails()
1300                                                    );
1301                                            }
1302                                    }
1303                            });
1304                    }
1305                    
1306                  /** Invoked when the name of orchestra is changed. */                  /** Invoked when the name of orchestra is changed. */
1307                  public void                  public void
1308                  nameChanged(OrchestraEvent e) { saveOrchestras(); }                  nameChanged(OrchestraEvent e) { saveOrchestras(); }
# Line 1015  public class CC { Line 1336  public class CC {
1336                          e.getEntry().removeOrchestraListener(getHandler());                          e.getEntry().removeOrchestraListener(getHandler());
1337                          saveOrchestras();                          saveOrchestras();
1338                  }                  }
1339                    
1340                    /**
1341                     * Invoked when MIDI data arrives.
1342                     */
1343                    public void
1344                    midiDataArrived(final ChannelMidiDataEvent e) {
1345                            try {
1346                                    javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
1347                                            public void
1348                                            run() { fireChannelMidiDataEvent(e); }
1349                                    });
1350                            } catch(Exception x) {
1351                                    CC.getLogger().log(Level.INFO, "Failed!", x);
1352                            }
1353                    }
1354            }
1355            
1356            private static void
1357            fireChannelMidiDataEvent(ChannelMidiDataEvent e) {
1358                    SamplerChannelModel chn;
1359                    chn = CC.getSamplerModel().getChannelById(e.getChannelId());
1360                    if(chn == null) {
1361                            CC.getLogger().info("Unknown channel ID: " + e.getChannelId());
1362                    }
1363                    
1364                    ((DefaultSamplerChannelModel)chn).fireMidiDataEvent(e);
1365          }          }
1366                    
1367          private static final AudioDeviceCountListener audioDeviceCountListener =          private static final AudioDeviceCountListener audioDeviceCountListener =

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

  ViewVC Help
Powered by ViewVC