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 |
|
|
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 |
/** |
/** |
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 |
|
|
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 |
*/ |
*/ |
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() { |
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 |
400 |
*/ |
*/ |
401 |
public static InstrumentsDbTreeModel |
public static InstrumentsDbTreeModel |
402 |
getInstrumentsDbTreeModel() { |
getInstrumentsDbTreeModel() { |
403 |
|
if(CC.getSamplerModel().getServerInfo() == null) return null; |
404 |
if(!CC.getSamplerModel().getServerInfo().hasInstrumentsDbSupport()) return null; |
if(!CC.getSamplerModel().getServerInfo().hasInstrumentsDbSupport()) return null; |
405 |
|
|
406 |
if(instrumentsDbTreeModel == null) { |
if(instrumentsDbTreeModel == null) { |
407 |
instrumentsDbTreeModel = new InstrumentsDbTreeModel(); |
instrumentsDbTreeModel = new InstrumentsDbTreeModel(); |
408 |
|
for(ChangeListener l : idtmListeners) l.stateChanged(null); |
409 |
} |
} |
410 |
|
|
411 |
return instrumentsDbTreeModel; |
return instrumentsDbTreeModel; |
412 |
} |
} |
413 |
|
|
414 |
|
public static void |
415 |
|
addInstrumentsDbChangeListener(ChangeListener l) { |
416 |
|
idtmListeners.add(l); |
417 |
|
} |
418 |
|
|
419 |
|
public static void |
420 |
|
removeInstrumentsDbChangeListener(ChangeListener l) { |
421 |
|
idtmListeners.remove(l); |
422 |
|
} |
423 |
|
|
424 |
|
private static final LostFilesModel lostFilesModel = new LostFilesModel(); |
425 |
|
|
426 |
|
public static LostFilesModel |
427 |
|
getLostFilesModel() { return lostFilesModel; } |
428 |
|
|
429 |
/** |
/** |
430 |
* Loads the orchestras described in <code><jsampler_home>/orchestras.xml</code>. |
* Loads the orchestras described in <code><jsampler_home>/orchestras.xml</code>. |
431 |
* 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 |
439 |
|
|
440 |
try { |
try { |
441 |
String s = getJSamplerHome(); |
String s = getJSamplerHome(); |
|
if(s == null) return; |
|
|
getOrchestras().addOrchestraListListener(getHandler()); |
|
442 |
|
|
443 |
File f = new File(s + File.separator + "orchestras.xml.bkp"); |
File f = new File(s + File.separator + "orchestras.xml.bkp"); |
444 |
if(f.isFile()) HF.createBackup("orchestras.xml.bkp", "orchestras.xml.rec"); |
if(f.isFile()) HF.createBackup("orchestras.xml.bkp", "orchestras.xml.rec"); |
451 |
} catch(Exception x) { |
} catch(Exception x) { |
452 |
getLogger().log(Level.INFO, HF.getErrorMessage(x), x); |
getLogger().log(Level.INFO, HF.getErrorMessage(x), x); |
453 |
} |
} |
454 |
|
|
455 |
|
getOrchestras().addOrchestraListListener(getHandler()); |
456 |
} |
} |
457 |
|
|
458 |
|
|
503 |
} |
} |
504 |
|
|
505 |
/** |
/** |
506 |
|
* Loads the servers' info described in <code><jsampler_home>/servers.xml</code>. |
507 |
|
* If file with name <code>servers.xml.bkp</code> exist in the JSampler's home |
508 |
|
* directory, this means that the last save has failed. In that case a recovery file |
509 |
|
* <code>servers.xml.rec</code> is created and a recovery procedure |
510 |
|
* will be initiated. |
511 |
|
*/ |
512 |
|
public static void |
513 |
|
loadServerList() { |
514 |
|
if(getJSamplerHome() == null) return; |
515 |
|
|
516 |
|
try { |
517 |
|
String s = getJSamplerHome(); |
518 |
|
|
519 |
|
File f = new File(s + File.separator + "servers.xml.bkp"); |
520 |
|
if(f.isFile()) HF.createBackup("servers.xml.bkp", "servers.xml.rec"); |
521 |
|
|
522 |
|
FileInputStream fis; |
523 |
|
fis = new FileInputStream(s + File.separator + "servers.xml"); |
524 |
|
|
525 |
|
loadServerList(fis); |
526 |
|
fis.close(); |
527 |
|
} catch(Exception x) { |
528 |
|
getLogger().log(Level.INFO, HF.getErrorMessage(x), x); |
529 |
|
} |
530 |
|
|
531 |
|
getServerList().addChangeListener(serverListListener); |
532 |
|
|
533 |
|
/* We should have at least one server to connect. */ |
534 |
|
if(getServerList().getServerCount() == 0) { |
535 |
|
Server server = new Server(); |
536 |
|
server.setName("127.0.0.1:8888"); |
537 |
|
server.setAddress("127.0.0.1"); |
538 |
|
server.setPort(8888); |
539 |
|
getServerList().addServer(server); |
540 |
|
} |
541 |
|
} |
542 |
|
|
543 |
|
|
544 |
|
private static void |
545 |
|
loadServerList(InputStream in) { |
546 |
|
Document doc = DOMUtils.readObject(in); |
547 |
|
|
548 |
|
try { getServerList().readObject(doc.getDocumentElement()); } |
549 |
|
catch(Exception x) { |
550 |
|
HF.showErrorMessage(x, "Loading server list: "); |
551 |
|
return; |
552 |
|
} |
553 |
|
} |
554 |
|
|
555 |
|
private static void |
556 |
|
saveServerList() { |
557 |
|
try { |
558 |
|
String s = getJSamplerHome(); |
559 |
|
if(s == null) return; |
560 |
|
|
561 |
|
HF.createBackup("servers.xml", "servers.xml.bkp"); |
562 |
|
|
563 |
|
FileOutputStream fos; |
564 |
|
fos = new FileOutputStream(s + File.separator + "servers.xml", false); |
565 |
|
|
566 |
|
Document doc = DOMUtils.createEmptyDocument(); |
567 |
|
|
568 |
|
Node node = doc.createElement("temp"); |
569 |
|
doc.appendChild(node); |
570 |
|
|
571 |
|
getServerList().writeObject(doc, doc.getDocumentElement()); |
572 |
|
|
573 |
|
doc.replaceChild(node.getFirstChild(), node); |
574 |
|
|
575 |
|
DOMUtils.writeObject(doc, fos); |
576 |
|
|
577 |
|
fos.close(); |
578 |
|
|
579 |
|
HF.deleteFile("servers.xml.bkp"); |
580 |
|
} catch(Exception x) { |
581 |
|
HF.showErrorMessage(x, "Saving server list: "); |
582 |
|
return; |
583 |
|
} |
584 |
|
} |
585 |
|
|
586 |
|
/** |
587 |
* 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. |
588 |
* @see #cleanExit(int i) |
* @see #cleanExit(int i) |
589 |
*/ |
*/ |
639 |
getSamplerModel() { return samplerModel; } |
getSamplerModel() { return samplerModel; } |
640 |
|
|
641 |
/** |
/** |
642 |
|
* Connects to LinuxSampler. |
643 |
|
*/ |
644 |
|
public static void |
645 |
|
connect() { initSamplerModel(); } |
646 |
|
|
647 |
|
/** |
648 |
* Reconnects to LinuxSampler. |
* Reconnects to LinuxSampler. |
649 |
*/ |
*/ |
650 |
public static void |
public static void |
651 |
reconnect() { |
reconnect() { initSamplerModel(getCurrentServer()); } |
652 |
initSamplerModel(); |
|
653 |
fireReconnectEvent(); |
private static Server currentServer = null; |
|
} |
|
654 |
|
|
655 |
/** |
/** |
656 |
* This method updates the information about the backend state. |
* Gets the server, to which the frontend is going to connect |
657 |
|
* or is already connected. |
658 |
|
*/ |
659 |
|
public static Server |
660 |
|
getCurrentServer() { return currentServer; } |
661 |
|
|
662 |
|
/** |
663 |
|
* Sets the current server. |
664 |
*/ |
*/ |
665 |
public static void |
public static void |
666 |
|
setCurrentServer(Server server) { currentServer = server; } |
667 |
|
|
668 |
|
/** |
669 |
|
* This method updates the information about the backend state. |
670 |
|
*/ |
671 |
|
private static void |
672 |
initSamplerModel() { |
initSamplerModel() { |
673 |
|
Server srv = getMainFrame().getServer(); |
674 |
|
if(srv == null) return; |
675 |
|
initSamplerModel(srv); |
676 |
|
} |
677 |
|
|
678 |
|
/** |
679 |
|
* This method updates the information about the backend state. |
680 |
|
*/ |
681 |
|
private static void |
682 |
|
initSamplerModel(Server srv) { |
683 |
|
setCurrentServer(srv); |
684 |
|
final SetServerAddress ssa = new SetServerAddress(srv.getAddress(), srv.getPort()); |
685 |
|
|
686 |
final DefaultSamplerModel model = (DefaultSamplerModel)getSamplerModel(); |
final DefaultSamplerModel model = (DefaultSamplerModel)getSamplerModel(); |
687 |
|
|
688 |
final Global.GetServerInfo gsi = new Global.GetServerInfo(); |
final Global.GetServerInfo gsi = new Global.GetServerInfo(); |
759 |
gfs.addTaskListener(new GetFxSendsListener()); |
gfs.addTaskListener(new GetFxSendsListener()); |
760 |
getTaskQueue().add(gfs); |
getTaskQueue().add(gfs); |
761 |
} |
} |
762 |
|
|
763 |
|
// TODO: This should be done after the fx sends are set |
764 |
|
//CC.getSamplerModel().setModified(false); |
765 |
} |
} |
766 |
}); |
}); |
767 |
|
|
770 |
cnt.addTaskListener(new TaskListener() { |
cnt.addTaskListener(new TaskListener() { |
771 |
public void |
public void |
772 |
taskPerformed(TaskEvent e) { |
taskPerformed(TaskEvent e) { |
773 |
if(cnt.doneWithErrors()) return; |
if(cnt.doneWithErrors()) { |
774 |
|
setCurrentServer(null); |
775 |
|
retryToConnect(); |
776 |
|
return; |
777 |
|
} |
778 |
|
|
779 |
getTaskQueue().add(gsi); |
getTaskQueue().add(gsi); |
780 |
getTaskQueue().add(gaod); |
getTaskQueue().add(gaod); |
787 |
getTaskQueue().add(uc); |
getTaskQueue().add(uc); |
788 |
} |
} |
789 |
}); |
}); |
790 |
getTaskQueue().add(cnt); |
|
791 |
|
ssa.addTaskListener(new TaskListener() { |
792 |
|
public void |
793 |
|
taskPerformed(TaskEvent e) { |
794 |
|
CC.getTaskQueue().add(cnt); |
795 |
|
} |
796 |
|
}); |
797 |
|
|
798 |
|
getSamplerModel().reset(); |
799 |
|
if(instrumentsDbTreeModel != null) { |
800 |
|
instrumentsDbTreeModel.reset(); |
801 |
|
instrumentsDbTreeModel = null; |
802 |
|
} |
803 |
|
|
804 |
|
getTaskQueue().removePendingTasks(); |
805 |
|
getTaskQueue().add(ssa); |
806 |
|
|
807 |
|
fireReconnectEvent(); |
808 |
|
} |
809 |
|
|
810 |
|
private static void |
811 |
|
retryToConnect() { |
812 |
|
javax.swing.SwingUtilities.invokeLater(new Runnable() { |
813 |
|
public void |
814 |
|
run() { changeBackend(); } |
815 |
|
}); |
816 |
|
} |
817 |
|
|
818 |
|
public static void |
819 |
|
changeBackend() { |
820 |
|
Server s = getMainFrame().getServer(true); |
821 |
|
if(s != null) initSamplerModel(s); |
822 |
} |
} |
823 |
|
|
824 |
private static class GetFxSendsListener implements TaskListener { |
private static class GetFxSendsListener implements TaskListener { |
877 |
|
|
878 |
public static String |
public static String |
879 |
exportSessionToLscpScript() { |
exportSessionToLscpScript() { |
880 |
|
CC.getSamplerModel().setModified(false); |
881 |
|
|
882 |
StringBuffer sb = new StringBuffer("# Exported by: "); |
StringBuffer sb = new StringBuffer("# Exported by: "); |
883 |
sb.append("JSampler - a java front-end for LinuxSampler\r\n# Version: "); |
sb.append("JSampler - a java front-end for LinuxSampler\r\n# Version: "); |
884 |
sb.append(JSampler.VERSION).append("\r\n"); |
sb.append(JSampler.VERSION).append("\r\n"); |
915 |
sb.append("\r\n"); |
sb.append("\r\n"); |
916 |
} |
} |
917 |
|
|
918 |
|
exportInstrMapsToLscpScript(lscpClient); |
919 |
|
sb.append(out.toString()); |
920 |
|
out.reset(); |
921 |
|
sb.append("\r\n"); |
922 |
|
|
923 |
SamplerChannelModel[] channels = getSamplerModel().getChannels(); |
SamplerChannelModel[] channels = getSamplerModel().getChannels(); |
924 |
|
|
925 |
for(int i = 0; i < channels.length; i++) { |
for(int i = 0; i < channels.length; i++) { |
937 |
sb.append("\r\n"); |
sb.append("\r\n"); |
938 |
} |
} |
939 |
|
|
|
exportInstrMapsToLscpScript(lscpClient); |
|
|
sb.append(out.toString()); |
|
|
out.reset(); |
|
|
|
|
940 |
return sb.toString(); |
return sb.toString(); |
941 |
} |
} |
942 |
|
|
1000 |
lscpCLient.setChannelMidiInputChannel(chnId, chn.getMidiInputChannel()); |
lscpCLient.setChannelMidiInputChannel(chnId, chn.getMidiInputChannel()); |
1001 |
} |
} |
1002 |
|
|
1003 |
|
if(chn.getEngine() != null) { |
1004 |
|
lscpCLient.loadSamplerEngine(chn.getEngine().getName(), chnId); |
1005 |
|
lscpCLient.setChannelVolume(chnId, chn.getVolume()); |
1006 |
|
int mapId = chn.getMidiInstrumentMapId(); |
1007 |
|
lscpCLient.setChannelMidiInstrumentMap(chnId, mapId); |
1008 |
|
} |
1009 |
|
|
1010 |
id = chn.getAudioOutputDevice(); |
id = chn.getAudioOutputDevice(); |
1011 |
if(id != -1) { |
if(id != -1) { |
1012 |
for(int i = 0; i < sm.getAudioDeviceCount(); i++) { |
for(int i = 0; i < sm.getAudioDeviceCount(); i++) { |
1026 |
} |
} |
1027 |
} |
} |
1028 |
|
|
|
if(chn.getEngine() != null) { |
|
|
lscpCLient.loadSamplerEngine(chn.getEngine().getName(), chnId); |
|
|
lscpCLient.setChannelVolume(chnId, chn.getVolume()); |
|
|
} |
|
|
|
|
1029 |
String s = chn.getInstrumentFile(); |
String s = chn.getInstrumentFile(); |
1030 |
int i = chn.getInstrumentIndex(); |
int i = chn.getInstrumentIndex(); |
1031 |
if(s != null) lscpCLient.loadInstrument(s, i, chnId, true); |
if(s != null) lscpCLient.loadInstrument(s, i, chnId, true); |
1056 |
} |
} |
1057 |
} |
} |
1058 |
|
|
1059 |
|
public static void |
1060 |
|
scheduleInTaskQueue(final Runnable r) { |
1061 |
|
Task dummy = new Global.DummyTask(); |
1062 |
|
dummy.addTaskListener(new TaskListener() { |
1063 |
|
public void |
1064 |
|
taskPerformed(TaskEvent e) { |
1065 |
|
javax.swing.SwingUtilities.invokeLater(r); |
1066 |
|
} |
1067 |
|
}); |
1068 |
|
|
1069 |
|
CC.getTaskQueue().add(dummy); |
1070 |
|
} |
1071 |
|
|
1072 |
|
public static boolean |
1073 |
|
verifyConnection() { |
1074 |
|
if(getCurrentServer() == null) { |
1075 |
|
HF.showErrorMessage(i18n.getError("CC.notConnected")); |
1076 |
|
return false; |
1077 |
|
} |
1078 |
|
|
1079 |
|
return true; |
1080 |
|
} |
1081 |
|
|
1082 |
|
|
1083 |
private final static EventHandler eventHandler = new EventHandler(); |
private final static EventHandler eventHandler = new EventHandler(); |
1084 |
|
|
1208 |
/** 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. */ |
1209 |
public void |
public void |
1210 |
instrumentCountChanged(MidiInstrumentCountEvent e) { |
instrumentCountChanged(MidiInstrumentCountEvent e) { |
1211 |
getTaskQueue().add(new Midi.UpdateInstruments(e.getMapId())); |
scheduleTask(new Midi.UpdateInstruments(e.getMapId())); |
1212 |
} |
} |
1213 |
|
|
1214 |
/** Invoked when a MIDI instrument in a MIDI instrument map is changed. */ |
/** Invoked when a MIDI instrument in a MIDI instrument map is changed. */ |