/[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 1285 by iliev, Fri Aug 10 19:55:03 2007 UTC revision 1719 by iliev, Wed Mar 19 10:07:36 2008 UTC
# Line 42  import java.util.logging.StreamHandler; Line 42  import java.util.logging.StreamHandler;
42    
43  import javax.swing.Timer;  import javax.swing.Timer;
44    
45    import javax.swing.event.ChangeEvent;
46    import javax.swing.event.ChangeListener;
47    
48  import net.sf.juife.Task;  import net.sf.juife.Task;
49  import net.sf.juife.TaskQueue;  import net.sf.juife.TaskQueue;
50    
# Line 77  import org.w3c.dom.Document; Line 80  import org.w3c.dom.Document;
80  import org.w3c.dom.Node;  import org.w3c.dom.Node;
81    
82  import static org.jsampler.JSI18n.i18n;  import static org.jsampler.JSI18n.i18n;
83    import static org.jsampler.JSPrefs.MANUAL_SERVER_SELECT_ON_STARTUP;
84    
85    
86  /**  /**
# Line 132  public class CC { Line 136  public class CC {
136          scheduleTask(Task t) {          scheduleTask(Task t) {
137                  while(getTaskQueue().removeTask(t)) { }                  while(getTaskQueue().removeTask(t)) { }
138                                    
                 if(getTaskQueue().getPendingTaskCount() == 0) {  
                         if(t.equals(getTaskQueue().getRunningTask())) return;  
                 }  
                   
139                  getTaskQueue().add(t);                  getTaskQueue().add(t);
140          }          }
141                    
# Line 145  public class CC { Line 145  public class CC {
145          public static JSViewConfig          public static JSViewConfig
146          getViewConfig() { return viewConfig; }          getViewConfig() { return viewConfig; }
147                    
148            private static JSPrefs
149            preferences() { return getViewConfig().preferences(); }
150            
151          /**          /**
152           * Sets the configuration of the current view.           * Sets the configuration of the current view.
153           */           */
# Line 233  public class CC { Line 236  public class CC {
236                                    
237                  HF.setUIDefaultFont(Prefs.getInterfaceFont());                  HF.setUIDefaultFont(Prefs.getInterfaceFont());
238                                    
                 getClient().setServerAddress(Prefs.getLSAddress());  
                 getClient().setServerPort(Prefs.getLSPort());  
                   
239                  timer.setRepeats(false);                  timer.setRepeats(false);
240                                    
241                  timer.addActionListener(new ActionListener() {                  timer.addActionListener(new ActionListener() {
# Line 265  public class CC { Line 265  public class CC {
265                  getClient().removeVoiceCountListener(getHandler());                  getClient().removeVoiceCountListener(getHandler());
266                  getClient().addVoiceCountListener(getHandler());                  getClient().addVoiceCountListener(getHandler());
267                                    
268                    getClient().removeTotalStreamCountListener(getHandler());
269                    getClient().addTotalStreamCountListener(getHandler());
270                    
271                  getClient().removeTotalVoiceCountListener(getHandler());                  getClient().removeTotalVoiceCountListener(getHandler());
272                  getClient().addTotalVoiceCountListener(getHandler());                  getClient().addTotalVoiceCountListener(getHandler());
273                                    
# Line 369  public class CC { Line 372  public class CC {
372          public static OrchestraListModel          public static OrchestraListModel
373          getOrchestras() { return orchestras; }          getOrchestras() { return orchestras; }
374                    
375            private final static ServerList servers = new ServerList();
376            
377            /** Returns the server list. */
378            public static ServerList
379            getServerList() { return servers; }
380            
381            private static ServerListListener serverListListener = new ServerListListener();
382            
383            private static class ServerListListener implements ChangeListener {
384                    public void
385                    stateChanged(ChangeEvent e) {
386                            saveServerList();
387                    }
388            }
389            
390            private static final Vector<ChangeListener> idtmListeners = new Vector<ChangeListener>();
391          private static InstrumentsDbTreeModel instrumentsDbTreeModel = null;          private static InstrumentsDbTreeModel instrumentsDbTreeModel = null;
392            
393          /**          /**
394           * Gets the tree model of the instruments database.           * Gets the tree model of the instruments database.
395           * If the currently used view doesn't have instruments           * If the currently used view doesn't have instruments
396           * database support the tree model is initialized on first use.           * database support the tree model is initialized on first use.
397           * @return The tree model of the instruments database or           * @return The tree model of the instruments database or
398           * <code>null</code> if the backend doesn't have instruments database support.           * <code>null</code> if the backend doesn't have instruments database support.
399           * @see org.jsampler.view.JSMainFrame#getInstrumentsDbSupport           * @see org.jsampler.view.JSViewConfig#getInstrumentsDbSupport
400           */           */
401          public static InstrumentsDbTreeModel          public static InstrumentsDbTreeModel
402          getInstrumentsDbTreeModel() {          getInstrumentsDbTreeModel() {
# Line 384  public class CC { Line 404  public class CC {
404                                    
405                  if(instrumentsDbTreeModel == null) {                  if(instrumentsDbTreeModel == null) {
406                          instrumentsDbTreeModel = new InstrumentsDbTreeModel();                          instrumentsDbTreeModel = new InstrumentsDbTreeModel();
407                            for(ChangeListener l : idtmListeners) l.stateChanged(null);
408                  }                  }
409                                    
410                  return instrumentsDbTreeModel;                  return instrumentsDbTreeModel;
411          }          }
412                    
413            public static void
414            addInstrumentsDbChangeListener(ChangeListener l) {
415                    idtmListeners.add(l);
416            }
417            
418            public static void
419            removeInstrumentsDbChangeListener(ChangeListener l) {
420                    idtmListeners.remove(l);
421            }
422            
423          /**          /**
424           * 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>.
425           * 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 400  public class CC { Line 431  public class CC {
431          loadOrchestras() {          loadOrchestras() {
432                  if(getJSamplerHome() == null) return;                  if(getJSamplerHome() == null) return;
433                                    
                 //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;  
                 }  
                 ///////  
                   
434                  try {                  try {
435                          String s = getJSamplerHome();                          String s = getJSamplerHome();
                         if(s == null) return;  
436                                                    
437                          File f = new File(s + File.separator + "orchestras.xml.bkp");                          File f = new File(s + File.separator + "orchestras.xml.bkp");
438                          if(f.isFile()) HF.createBackup("orchestras.xml.bkp", "orchestras.xml.rec");                          if(f.isFile()) HF.createBackup("orchestras.xml.bkp", "orchestras.xml.rec");
# Line 424  public class CC { Line 445  public class CC {
445                  } catch(Exception x) {                  } catch(Exception x) {
446                          getLogger().log(Level.INFO, HF.getErrorMessage(x), x);                          getLogger().log(Level.INFO, HF.getErrorMessage(x), x);
447                  }                  }
448                    
449                    getOrchestras().addOrchestraListListener(getHandler());
450          }          }
451                    
452                    
# Line 440  public class CC { Line 463  public class CC {
463                  for(int i = 0; i < getOrchestras().getOrchestraCount(); i++) {                  for(int i = 0; i < getOrchestras().getOrchestraCount(); i++) {
464                          getOrchestras().getOrchestra(i).addOrchestraListener(getHandler());                          getOrchestras().getOrchestra(i).addOrchestraListener(getHandler());
465                  }                  }
                 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: "); }  
466          }          }
467                    
468          private static void          private static void
# Line 487  public class CC { Line 497  public class CC {
497          }          }
498                    
499          /**          /**
500             * Loads the servers' info described in <code>&lt;jsampler_home&gt;/servers.xml</code>.
501             * If file with name <code>servers.xml.bkp</code> exist in the JSampler's home
502             * directory, this means that the last save has failed. In that case a recovery file
503             * <code>servers.xml.rec</code> is created and a recovery procedure
504             * will be initiated.
505             */
506            public static void
507            loadServerList() {
508                    if(getJSamplerHome() == null) return;
509                    
510                    try {
511                            String s = getJSamplerHome();
512                            
513                            File f = new File(s + File.separator + "servers.xml.bkp");
514                            if(f.isFile()) HF.createBackup("servers.xml.bkp", "servers.xml.rec");
515                            
516                            FileInputStream fis;
517                            fis = new FileInputStream(s + File.separator + "servers.xml");
518                            
519                            loadServerList(fis);
520                            fis.close();
521                    } catch(Exception x) {
522                            getLogger().log(Level.INFO, HF.getErrorMessage(x), x);
523                    }
524                    
525                    getServerList().addChangeListener(serverListListener);
526                    
527                    /* We should have at least one server to connect. */
528                    if(getServerList().getServerCount() == 0) {
529                            Server server = new Server();
530                            server.setName("127.0.0.1:8888");
531                            server.setAddress("127.0.0.1");
532                            server.setPort(8888);
533                            getServerList().addServer(server);
534                    }
535            }
536            
537            
538            private static void
539            loadServerList(InputStream in) {
540                    Document doc = DOMUtils.readObject(in);
541                    
542                    try { getServerList().readObject(doc.getDocumentElement()); }
543                    catch(Exception x) {
544                            HF.showErrorMessage(x, "Loading server list: ");
545                            return;
546                    }
547            }
548            
549            private static void
550            saveServerList() {
551                    try {
552                            String s = getJSamplerHome();
553                            if(s == null) return;
554                            
555                            HF.createBackup("servers.xml", "servers.xml.bkp");
556                            
557                            FileOutputStream fos;
558                            fos = new FileOutputStream(s + File.separator + "servers.xml", false);
559                            
560                            Document doc = DOMUtils.createEmptyDocument();
561                    
562                            Node node = doc.createElement("temp");
563                            doc.appendChild(node);
564                            
565                            getServerList().writeObject(doc, doc.getDocumentElement());
566                            
567                            doc.replaceChild(node.getFirstChild(), node);
568                    
569                            DOMUtils.writeObject(doc, fos);
570                            
571                            fos.close();
572                            
573                            HF.deleteFile("servers.xml.bkp");
574                    } catch(Exception x) {
575                            HF.showErrorMessage(x, "Saving server list: ");
576                            return;
577                    }
578            }
579            
580            /**
581           * 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.
582           *  @see #cleanExit(int i)           *  @see #cleanExit(int i)
583           */           */
# Line 542  public class CC { Line 633  public class CC {
633          getSamplerModel() { return samplerModel; }          getSamplerModel() { return samplerModel; }
634                    
635          /**          /**
636             * Connects to LinuxSampler.
637             */
638            public static void
639            connect() { initSamplerModel(); }
640            
641            /**
642           * Reconnects to LinuxSampler.           * Reconnects to LinuxSampler.
643           */           */
644          public static void          public static void
645          reconnect() {          reconnect() { initSamplerModel(getCurrentServer()); }
646                  initSamplerModel();          
647                  fireReconnectEvent();          private static Server currentServer = null;
         }  
648                    
649          /**          /**
650           * This method updates the information about the backend state.           * Gets the server, to which the frontend is going to connect
651             * or is already connected.
652             */
653            public static Server
654            getCurrentServer() { return currentServer; }
655            
656            /**
657             * Sets the current server.
658           */           */
659          public static void          public static void
660            setCurrentServer(Server server) { currentServer = server; }
661            
662            /**
663             * This method updates the information about the backend state.
664             */
665            private static void
666          initSamplerModel() {          initSamplerModel() {
667                    Server srv = getMainFrame().getServer();
668                    if(srv == null) return;
669                    initSamplerModel(srv);
670            }
671            
672            /**
673             * This method updates the information about the backend state.
674             */
675            private static void
676            initSamplerModel(Server srv) {
677                    setCurrentServer(srv);
678                    final SetServerAddress ssa = new SetServerAddress(srv.getAddress(), srv.getPort());
679                    
680                  final DefaultSamplerModel model = (DefaultSamplerModel)getSamplerModel();                  final DefaultSamplerModel model = (DefaultSamplerModel)getSamplerModel();
681                                    
682                  final Global.GetServerInfo gsi = new Global.GetServerInfo();                  final Global.GetServerInfo gsi = new Global.GetServerInfo();
# Line 631  public class CC { Line 753  public class CC {
753                                          gfs.addTaskListener(new GetFxSendsListener());                                          gfs.addTaskListener(new GetFxSendsListener());
754                                          getTaskQueue().add(gfs);                                          getTaskQueue().add(gfs);
755                                  }                                  }
756                                    
757                                    // TODO: This should be done after the fx sends are set
758                                    //CC.getSamplerModel().setModified(false);
759                          }                          }
760                  });                  });
761                                    
# Line 639  public class CC { Line 764  public class CC {
764                  cnt.addTaskListener(new TaskListener() {                  cnt.addTaskListener(new TaskListener() {
765                          public void                          public void
766                          taskPerformed(TaskEvent e) {                          taskPerformed(TaskEvent e) {
767                                  if(cnt.doneWithErrors()) return;                                  if(cnt.doneWithErrors()) {
768                                            setCurrentServer(null);
769                                            retryToConnect();
770                                            return;
771                                    }
772                                                                    
773                                  getTaskQueue().add(gsi);                                  getTaskQueue().add(gsi);
774                                  getTaskQueue().add(gaod);                                  getTaskQueue().add(gaod);
# Line 652  public class CC { Line 781  public class CC {
781                                  getTaskQueue().add(uc);                                  getTaskQueue().add(uc);
782                          }                          }
783                  });                  });
784                  getTaskQueue().add(cnt);                  
785                    ssa.addTaskListener(new TaskListener() {
786                            public void
787                            taskPerformed(TaskEvent e) {
788                                    CC.getTaskQueue().add(cnt);
789                            }
790                    });
791                    
792                    getSamplerModel().reset();
793                    if(instrumentsDbTreeModel != null) {
794                            instrumentsDbTreeModel.reset();
795                            instrumentsDbTreeModel = null;
796                    }
797                    
798                    getTaskQueue().removePendingTasks();
799                    getTaskQueue().add(ssa);
800                    
801                    fireReconnectEvent();
802            }
803            
804            private static void
805            retryToConnect() {
806                    javax.swing.SwingUtilities.invokeLater(new Runnable() {
807                            public void
808                            run() { changeBackend(); }
809                    });
810            }
811            
812            public static void
813            changeBackend() {
814                    Server s = getMainFrame().getServer(true);
815                    if(s != null) initSamplerModel(s);
816          }          }
817                    
818          private static class GetFxSendsListener implements TaskListener {          private static class GetFxSendsListener implements TaskListener {
# Line 711  public class CC { Line 871  public class CC {
871                    
872          public static String          public static String
873          exportSessionToLscpScript() {          exportSessionToLscpScript() {
874                    CC.getSamplerModel().setModified(false);
875                    
876                  StringBuffer sb = new StringBuffer("# Exported by: ");                  StringBuffer sb = new StringBuffer("# Exported by: ");
877                  sb.append("JSampler - a java front-end for LinuxSampler\r\n# Version: ");                  sb.append("JSampler - a java front-end for LinuxSampler\r\n# Version: ");
878                  sb.append(JSampler.VERSION).append("\r\n");                  sb.append(JSampler.VERSION).append("\r\n");
# Line 747  public class CC { Line 909  public class CC {
909                          sb.append("\r\n");                          sb.append("\r\n");
910                  }                  }
911                                    
912                    exportInstrMapsToLscpScript(lscpClient);
913                    sb.append(out.toString());
914                    out.reset();
915                    sb.append("\r\n");
916                    
917                  SamplerChannelModel[] channels = getSamplerModel().getChannels();                  SamplerChannelModel[] channels = getSamplerModel().getChannels();
918                                    
919                  for(int i = 0; i < channels.length; i++) {                  for(int i = 0; i < channels.length; i++) {
920                          SamplerChannelModel scm = getSamplerModel().getChannelById(i);                          SamplerChannelModel scm = channels[i];
921                          exportChannelToLscpScript(scm.getChannelInfo(), i, lscpClient);                          exportChannelToLscpScript(scm.getChannelInfo(), i, lscpClient);
922                          sb.append(out.toString());                          sb.append(out.toString());
923                          out.reset();                          out.reset();
# Line 764  public class CC { Line 931  public class CC {
931                          sb.append("\r\n");                          sb.append("\r\n");
932                  }                  }
933                                    
                 exportInstrMapsToLscpScript(lscpClient);  
                 sb.append(out.toString());  
                 out.reset();  
                   
934                  return sb.toString();                  return sb.toString();
935          }          }
936                    
# Line 818  public class CC { Line 981  public class CC {
981                  try {                  try {
982                          lscpCLient.addSamplerChannel();                          lscpCLient.addSamplerChannel();
983                                                    
984                          int i = chn.getMidiInputDevice();                          SamplerModel sm = CC.getSamplerModel();
985                          if(i != -1) lscpCLient.setChannelMidiInputDevice(chnId, i);                          int id = chn.getMidiInputDevice();
986                          lscpCLient.setChannelMidiInputPort(chnId, chn.getMidiInputPort());                          if(id != -1) {
987                          lscpCLient.setChannelMidiInputChannel(chnId, chn.getMidiInputChannel());                                  for(int i = 0; i < sm.getMidiDeviceCount(); i++) {
988                                                                    if(sm.getMidiDevice(i).getDeviceId() == id) {
989                          i = chn.getAudioOutputDevice();                                                  lscpCLient.setChannelMidiInputDevice(chnId, i);
990                          if(i != -1) {                                                  break;
991                                  lscpCLient.setChannelAudioOutputDevice(chnId, i);                                          }
992                                    }
993                                    lscpCLient.setChannelMidiInputPort(chnId, chn.getMidiInputPort());
994                                    lscpCLient.setChannelMidiInputChannel(chnId, chn.getMidiInputChannel());
995                            }
996                            
997                            if(chn.getEngine() != null) {
998                                    lscpCLient.loadSamplerEngine(chn.getEngine().getName(), chnId);
999                                    lscpCLient.setChannelVolume(chnId, chn.getVolume());
1000                                    int mapId = chn.getMidiInstrumentMapId();
1001                                    lscpCLient.setChannelMidiInstrumentMap(chnId, mapId);
1002                            }
1003                            
1004                            id = chn.getAudioOutputDevice();
1005                            if(id != -1) {
1006                                    for(int i = 0; i < sm.getAudioDeviceCount(); i++) {
1007                                            if(sm.getAudioDevice(i).getDeviceId() == id) {
1008                                                    lscpCLient.setChannelAudioOutputDevice(chnId, i);
1009                                                    break;
1010                                            }
1011                                    }
1012                                    
1013                                  Integer[] routing = chn.getAudioOutputRouting();                                  Integer[] routing = chn.getAudioOutputRouting();
1014                                                                    
1015                                  for(int j = 0; j < routing.length; j++) {                                  for(int j = 0; j < routing.length; j++) {
# Line 836  public class CC { Line 1020  public class CC {
1020                                  }                                  }
1021                          }                          }
1022                                                    
                         if(chn.getEngine() != null) {  
                                 lscpCLient.loadSamplerEngine(chn.getEngine().getName(), chnId);  
                                 lscpCLient.setChannelVolume(chnId, chn.getVolume());  
                         }  
                           
1023                          String s = chn.getInstrumentFile();                          String s = chn.getInstrumentFile();
1024                          i = chn.getInstrumentIndex();                          int i = chn.getInstrumentIndex();
1025                          if(s != null) lscpCLient.loadInstrument(s, i, chnId, true);                          if(s != null) lscpCLient.loadInstrument(s, i, chnId, true);
1026                                                    
1027                          if(chn.isMuted()) lscpCLient.setChannelMute(chnId, true);                          if(chn.isMuted()) lscpCLient.setChannelMute(chnId, true);
# Line 871  public class CC { Line 1050  public class CC {
1050                  }                  }
1051          }          }
1052                    
1053            public static void
1054            scheduleInTaskQueue(final Runnable r) {
1055                    Task dummy = new Global.DummyTask();
1056                    dummy.addTaskListener(new TaskListener() {
1057                            public void
1058                            taskPerformed(TaskEvent e) {
1059                                    javax.swing.SwingUtilities.invokeLater(r);
1060                            }
1061                    });
1062                    
1063                    CC.getTaskQueue().add(dummy);
1064            }
1065            
1066            public static boolean
1067            verifyConnection() {
1068                    if(getCurrentServer() == null) {
1069                            HF.showErrorMessage(i18n.getError("CC.notConnected"));
1070                            return false;
1071                    }
1072                    
1073                    return true;
1074            }
1075            
1076                    
1077          private final static EventHandler eventHandler = new EventHandler();          private final static EventHandler eventHandler = new EventHandler();
1078                    
# Line 879  public class CC { Line 1081  public class CC {
1081                    
1082          private static class EventHandler implements ChannelCountListener, ChannelInfoListener,          private static class EventHandler implements ChannelCountListener, ChannelInfoListener,
1083                  FxSendCountListener, FxSendInfoListener, StreamCountListener, VoiceCountListener,                  FxSendCountListener, FxSendInfoListener, StreamCountListener, VoiceCountListener,
1084                  TotalVoiceCountListener, TaskQueueListener, OrchestraListener,                  TotalStreamCountListener, TotalVoiceCountListener, TaskQueueListener,
1085                  ListListener<OrchestraModel>, MidiInstrumentCountListener,                  OrchestraListener, ListListener<OrchestraModel>, MidiInstrumentCountListener,
1086                  MidiInstrumentInfoListener, GlobalInfoListener {                  MidiInstrumentInfoListener, GlobalInfoListener {
1087                                    
1088                  /** Invoked when the number of channels has changed. */                  /** Invoked when the number of channels has changed. */
# Line 985  public class CC { Line 1187  public class CC {
1187                          scm.setVoiceCount(e.getVoiceCount());                          scm.setVoiceCount(e.getVoiceCount());
1188                  }                  }
1189                                    
1190                    /** Invoked when the total number of active streams has changed. */
1191                    public void
1192                    totalStreamCountChanged(TotalStreamCountEvent e) {
1193                            getSamplerModel().updateActiveStreamsInfo(e.getTotalStreamCount());
1194                    }
1195                    
1196                  /** Invoked when the total number of active voices has changed. */                  /** Invoked when the total number of active voices has changed. */
1197                  public void                  public void
1198                  totalVoiceCountChanged(TotalVoiceCountEvent e) {                  totalVoiceCountChanged(TotalVoiceCountEvent e) {
# Line 994  public class CC { Line 1202  public class CC {
1202                  /** 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. */
1203                  public void                  public void
1204                  instrumentCountChanged(MidiInstrumentCountEvent e) {                  instrumentCountChanged(MidiInstrumentCountEvent e) {
1205                          getTaskQueue().add(new Midi.UpdateInstruments(e.getMapId()));                          scheduleTask(new Midi.UpdateInstruments(e.getMapId()));
1206                  }                  }
1207                                    
1208                  /** Invoked when a MIDI instrument in a MIDI instrument map is changed. */                  /** Invoked when a MIDI instrument in a MIDI instrument map is changed. */

Legend:
Removed from v.1285  
changed lines
  Added in v.1719

  ViewVC Help
Powered by ViewVC