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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1916 - (show annotations) (download)
Fri Jun 12 01:22:41 2009 UTC (14 years, 10 months ago) by iliev
File size: 18283 byte(s)
* added support for exporting the MIDI instrument maps
  as Rosegarden device file

1 /*
2 * JSampler - a java front-end for LinuxSampler
3 *
4 * Copyright (C) 2009 Grigor Iliev <grigor@grigoriliev.com>
5 *
6 * This file is part of JSampler.
7 *
8 * JSampler is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation.
11 *
12 * JSampler is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with JSampler; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20 * MA 02111-1307 USA
21 */
22
23 package org.jsampler;
24
25 import java.io.ByteArrayOutputStream;
26
27 import java.io.File;
28 import java.text.DateFormat;
29 import java.util.logging.Level;
30 import java.util.zip.GZIPOutputStream;
31
32 import org.jsampler.view.JSChannel;
33 import org.jsampler.view.JSChannelsPane;
34
35 import org.linuxsampler.lscp.AudioOutputChannel;
36 import org.linuxsampler.lscp.AudioOutputDevice;
37 import org.linuxsampler.lscp.Client;
38 import org.linuxsampler.lscp.FxSend;
39 import org.linuxsampler.lscp.MidiInputDevice;
40 import org.linuxsampler.lscp.MidiPort;
41 import org.linuxsampler.lscp.Parameter;
42 import org.linuxsampler.lscp.SamplerChannel;
43
44 import org.w3c.dom.Document;
45 import org.w3c.dom.Element;
46
47 import static org.jsampler.CC.preferences;
48 import static org.jsampler.JSI18n.i18n;
49
50 /**
51 *
52 * @author Grigor Iliev
53 */
54 public class JSUtils {
55
56 /** Forbids the instantiation of this class */
57 private
58 JSUtils() { }
59
60 /**
61 * Checks whether the JSampler home directory is specified and exist.
62 * If the JSampler home directory is not specifed, or is specified
63 * but doesn't exist, a procedure of specifying a JSampler home
64 * directory is initiated.
65 * @see org.jsampler.view.JSMainFrame#installJSamplerHome
66 */
67 public static void
68 checkJSamplerHome() {
69 if(CC.getJSamplerHome() != null) {
70 File f = new File(CC.getJSamplerHome());
71 if(f.exists() && f.isDirectory()) {
72 return;
73 }
74 }
75
76 CC.getMainFrame().installJSamplerHome();
77 }
78
79 /**
80 * Changes the JSampler's home directory and moves all files from
81 * the old JSampler's home directory to the new one. If all files are
82 * moved succesfully, the old directory is deleted.
83 * @param path The location of the new JSampler's home directory. If
84 * the last directory in the path doesn't exist, it is created.
85 */
86 public static void
87 changeJSamplerHome(String path) {
88 File fNew = new File(path);
89 if(fNew.exists() && fNew.isFile()) {
90 HF.showErrorMessage(i18n.getError("CC.JSamplerHomeIsNotDir!"));
91 return;
92 }
93
94 if(!fNew.exists()) {
95 if(!fNew.mkdir()) {
96 String s = fNew.getAbsolutePath();
97 HF.showErrorMessage(i18n.getError("CC.mkdirFailed", s));
98 return;
99 }
100 }
101
102 if(CC.getJSamplerHome() == null || path.equals(CC.getJSamplerHome())) {
103 CC.setJSamplerHome(fNew.getAbsolutePath());
104 return;
105 }
106
107 File fOld = new File(CC.getJSamplerHome());
108 if(!fOld.exists() || !fOld.isDirectory()) {
109 CC.setJSamplerHome(fNew.getAbsolutePath());
110 return;
111 }
112
113 File[] files = fOld.listFiles();
114 boolean b = true;
115 if(files != null) {
116 String s = fNew.getAbsolutePath() + File.separator;
117 for(File f : files) if(!f.renameTo(new File(s + f.getName()))) b = false;
118 }
119
120 if(b) fOld.delete();
121 CC.setJSamplerHome(fNew.getAbsolutePath());
122 }
123
124 public static String
125 exportInstrMapsToLscpScript() {
126 StringBuffer sb = new StringBuffer("# Exported by: ");
127 sb.append("JSampler - a java front-end for LinuxSampler\r\n# Version: ");
128 sb.append(JSampler.VERSION).append("\r\n");
129 sb.append("# Date: ").append(new java.util.Date().toString()).append("\r\n\r\n");
130
131 Client lscpClient = new Client(true);
132 ByteArrayOutputStream out = new ByteArrayOutputStream();
133 lscpClient.setPrintOnlyModeOutputStream(out);
134
135 exportInstrMapsToLscpScript(lscpClient);
136 sb.append(out.toString());
137 out.reset();
138
139 return sb.toString();
140 }
141
142 private static void
143 exportInstrMapsToLscpScript(Client lscpClient) {
144 try {
145 lscpClient.removeAllMidiInstrumentMaps();
146 MidiInstrumentMap[] maps = CC.getSamplerModel().getMidiInstrumentMaps();
147 for(int i = 0; i < maps.length; i++) {
148 lscpClient.addMidiInstrumentMap(maps[i].getName());
149 exportInstrumentsToLscpScript(i, maps[i], lscpClient);
150 }
151 } catch(Exception e) {
152 CC.getLogger().log(Level.FINE, HF.getErrorMessage(e), e);
153 HF.showErrorMessage(e);
154 }
155 }
156
157 private static void
158 exportInstrumentsToLscpScript(int mapId, MidiInstrumentMap map, Client lscpClient)
159 throws Exception {
160
161 boolean b = preferences().getBoolProperty(JSPrefs.LOAD_MIDI_INSTRUMENTS_IN_BACKGROUND);
162
163 for(MidiInstrument i : map.getAllMidiInstruments()) {
164 lscpClient.mapMidiInstrument(mapId, i.getInfo().getEntry(), i.getInfo(), b);
165 }
166 }
167
168 public static String
169 exportInstrMapsToText() {
170 String nl = System.getProperty("line.separator");
171 StringBuffer sb = new StringBuffer();
172
173
174 MidiInstrumentMap[] maps = CC.getSamplerModel().getMidiInstrumentMaps();
175 for(int i = 0; i < maps.length; i++) {
176 sb.append("MIDI Instrument Map: ");
177 sb.append(maps[i].getName()).append(nl);
178 exportInstrumentsToText(maps[i], sb);
179 }
180
181 String date = DateFormat.getDateInstance().format(new java.util.Date());
182 sb.append("Date: ").append(date).append(nl);
183 sb.append("Exported by: JSampler - a java front-end for LinuxSampler, Version ");
184 sb.append(JSampler.VERSION).append(nl);
185
186 return sb.toString();
187 }
188
189 private static void
190 exportInstrumentsToText(MidiInstrumentMap map, StringBuffer sb) {
191 int bank = -1;
192 String nl = System.getProperty("line.separator");
193 int bnkOffset = preferences().getIntProperty(JSPrefs.FIRST_MIDI_BANK_NUMBER);
194 int prgOffset = preferences().getIntProperty(JSPrefs.FIRST_MIDI_PROGRAM_NUMBER);
195
196 for(MidiInstrument i : map.getAllMidiInstruments()) {
197 int newBank = i.getInfo().getMidiBank();
198 if(newBank != bank) {
199 bank = newBank;
200 sb.append(nl).append("\tMIDI Bank ");
201 sb.append(bank + bnkOffset).append(nl);
202 }
203 sb.append("\t[").append(bank + bnkOffset).append("] ");
204 sb.append(i.getInfo().getMidiProgram() + prgOffset);
205 sb.append(" - ").append(i.getName()).append(nl);
206 }
207
208 sb.append(nl);
209 }
210
211 public static String
212 exportInstrMapsToHtml() {
213 String nl = System.getProperty("line.separator");
214 StringBuffer sb = new StringBuffer("<html>").append(nl);
215
216 sb.append("<head><title>MIDI Instrument Maps</title></head>");
217
218 sb.append("<body>").append(nl);
219 sb.append("<h1>MIDI Instrument Maps</h1>").append(nl);
220
221 String date = DateFormat.getDateInstance().format(new java.util.Date());
222 sb.append("Date: ").append(date).append("<br>").append(nl);
223 sb.append("Exported by <a href=http://linuxsampler.org/jsampler/manual/html/jsampler.html>");
224 sb.append("JSampler</a> version ");
225 sb.append(JSampler.VERSION).append("<br>").append(nl);
226
227 MidiInstrumentMap[] maps = CC.getSamplerModel().getMidiInstrumentMaps();
228
229 sb.append("<ol>").append(nl);
230 for(int i = 0; i < maps.length; i++) {
231 String name = toHtmlEscapedText(maps[i].getName());
232 sb.append("<li><a href=#map-").append(i + 1).append(">");
233 sb.append(name).append("</a></li>").append(nl);
234 }
235 sb.append("</ol>").append(nl);
236
237 for(int i = 0; i < maps.length; i++) {
238 String s = toHtmlEscapedText(maps[i].getName());
239 sb.append("<h2><a name=map-").append(i + 1).append(">");
240 sb.append(s).append("</a></h2>").append(nl);
241 exportInstrumentsToHtml(i, maps[i], sb);
242 }
243
244 sb.append(nl).append("</body>").append(nl).append("</html>");
245 return sb.toString();
246 }
247
248 private static void
249 exportInstrumentsToHtml(int mapId, MidiInstrumentMap map, StringBuffer sb) {
250 int bank = -1;
251 String nl = System.getProperty("line.separator");
252 int bnkOffset = preferences().getIntProperty(JSPrefs.FIRST_MIDI_BANK_NUMBER);
253 int prgOffset = preferences().getIntProperty(JSPrefs.FIRST_MIDI_PROGRAM_NUMBER);
254
255 sb.append("<ol>").append(nl);
256 for(MidiInstrument i : map.getAllMidiInstruments()) {
257 int newBank = i.getInfo().getMidiBank();
258 if(newBank != bank) {
259 bank = newBank;
260 String s = "map-" + (mapId + 1) + "-bank-" + (bank + bnkOffset);
261 sb.append(nl).append("<li><a href=#").append(s);
262 sb.append(">MIDI Bank ");
263 sb.append(bank + bnkOffset).append("</a></li>").append(nl);
264 }
265 }
266 sb.append("</ol>").append(nl);
267
268 bank = -1;
269 String bankName = "";
270
271 sb.append("<table border=0>").append(nl);
272 for(MidiInstrument i : map.getAllMidiInstruments()) {
273 int newBank = i.getInfo().getMidiBank();
274 if(newBank != bank) {
275 bank = newBank;
276 sb.append("</table>").append(nl);
277
278 bankName = "map-" + (mapId + 1) + "-bank-" + (bank + bnkOffset);
279 sb.append(nl).append("<h4><a name=").append(bankName);
280 sb.append(">MIDI Bank ");
281 sb.append(bank + bnkOffset).append("</a></h4>").append(nl);
282
283 sb.append("<table border=0>").append(nl);
284 }
285
286 sb.append("<tr><td align='right'>");
287 sb.append(i.getInfo().getMidiProgram() + prgOffset).append(" - </td>");
288 String file = i.getInfo().getFilePath();
289 String tooltip = "File: " + file + ", Index: " + i.getInfo().getInstrumentIndex();
290 sb.append("<td><a title='").append(tooltip).append("'>");
291 String s = toHtmlEscapedText(i.getName());
292 sb.append(s).append("</a></td>");
293
294 sb.append("<td>&nbsp;&nbsp;<a href=#").append(bankName).append(">");
295 sb.append("[").append(bank + bnkOffset).append("]</a></td>").append(nl);
296 sb.append("</tr>");
297 }
298 sb.append("</table>").append(nl);
299
300 sb.append(nl);
301 }
302
303 private static String
304 toHtmlEscapedText(String s) {
305 s = s.replaceAll("&", "&amp;");
306 s = s.replaceAll("<", "&lt;");
307 s = s.replaceAll(">", "&gt;");
308
309 return s;
310 }
311
312 public static byte[]
313 exportInstrMapsToRGD() {
314 Document doc = DOMUtils.createEmptyDocument();
315
316 Element rgd = doc.createElement("rosegarden-data");
317 rgd.setAttribute("version", "1.7.2");
318 doc.appendChild(rgd);
319
320 Element studio = doc.createElement("studio");
321 studio.setAttribute("thrufilter", "0");
322 studio.setAttribute("recordfilter", "0");
323 rgd.appendChild(studio);
324
325 MidiInstrumentMap[] maps = CC.getSamplerModel().getMidiInstrumentMaps();
326 for(int i = 0; i < maps.length; i++) {
327 Element dev = doc.createElement("device");
328 dev.setAttribute("id", String.valueOf(i));
329 dev.setAttribute("name", "LinuxSampler: " + maps[i].getName());
330 dev.setAttribute("type", "midi");
331 studio.appendChild(dev);
332
333 Element el = doc.createElement("librarian");
334 el.setAttribute("name", "Grigor Iliev");
335 el.setAttribute("email", "grigor@grigoriliev.com");
336 dev.appendChild(el);
337
338 exportInstrumentsToRGD(maps[i], dev);
339 }
340
341 ByteArrayOutputStream baos = new ByteArrayOutputStream();
342 DOMUtils.writeObject(doc, baos);
343
344 // Hack to insert the file name in the archive
345 byte[] data2 = null;
346 try {
347 ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
348 GZIPOutputStream gzos = new GZIPOutputStream(baos2);
349 gzos.write(baos.toByteArray());
350 gzos.finish();
351 byte[] data = baos2.toByteArray();
352 data[3] = 8; // File name
353 byte[] fn = "x-rosegarden-device".getBytes("US-ASCII");
354 int fnsize = fn.length;
355 data2 = new byte[data.length + fnsize + 1];
356
357 for(int i = 0; i < 10; i++) data2[i] = data[i];
358 for(int i = 0; i < fnsize; i++) data2[i + 10] = fn[i];
359 data2[10 + fnsize] = 0;
360 for(int i = 10; i < data.length; i++) data2[i + fnsize + 1] = data[i];
361
362 } catch(Exception e) {
363 e.printStackTrace();
364 }
365 //////////////
366
367 return data2;
368 }
369
370 private static void
371 exportInstrumentsToRGD(MidiInstrumentMap map, Element el) {
372 int bank = -1;
373 int bnkOffset = preferences().getIntProperty(JSPrefs.FIRST_MIDI_BANK_NUMBER);
374 Element elBank = null;
375
376 for(MidiInstrument i : map.getAllMidiInstruments()) {
377 int newBank = i.getInfo().getMidiBank();
378 if(newBank != bank) {
379 bank = newBank;
380 elBank = el.getOwnerDocument().createElement("bank");
381 elBank.setAttribute("name", "Bank " + (bank + bnkOffset));
382 elBank.setAttribute("msb", String.valueOf((bank >> 7) & 0x7f));
383 elBank.setAttribute("lsb", String.valueOf(bank & 0x7f));
384 el.appendChild(elBank);
385 }
386
387 Element elProgram = el.getOwnerDocument().createElement("program");
388 elProgram.setAttribute("id", String.valueOf(i.getInfo().getMidiProgram()));
389 elProgram.setAttribute("name", i.getName());
390
391 elBank.appendChild(elProgram);
392 }
393 }
394
395 public static String
396 exportSessionToLscpScript() {
397 CC.getSamplerModel().setModified(false);
398
399 StringBuffer sb = new StringBuffer("# Exported by: ");
400 sb.append("JSampler - a java front-end for LinuxSampler\r\n# Version: ");
401 sb.append(JSampler.VERSION).append("\r\n");
402 sb.append("# Date: ").append(new java.util.Date().toString()).append("\r\n\r\n");
403
404 Client lscpClient = new Client(true);
405 ByteArrayOutputStream out = new ByteArrayOutputStream();
406 lscpClient.setPrintOnlyModeOutputStream(out);
407
408 try {
409 lscpClient.resetSampler();
410 sb.append(out.toString());
411 out.reset();
412 sb.append("\r\n");
413 lscpClient.setVolume(CC.getSamplerModel().getVolume());
414 sb.append(out.toString());
415 out.reset();
416 sb.append("\r\n");
417 } catch(Exception e) { CC.getLogger().log(Level.FINE, HF.getErrorMessage(e), e); }
418
419 MidiDeviceModel[] mDevs = CC.getSamplerModel().getMidiDevices();
420 for(int i = 0; i < mDevs.length; i++) {
421 exportMidiDeviceToLscpScript(mDevs[i].getDeviceInfo(), i, lscpClient);
422 sb.append(out.toString());
423 out.reset();
424 sb.append("\r\n");
425 }
426
427 AudioDeviceModel[] aDevs = CC.getSamplerModel().getAudioDevices();
428 for(int i = 0; i < aDevs.length; i++) {
429 exportAudioDeviceToLscpScript(aDevs[i].getDeviceInfo(), i, lscpClient);
430 sb.append(out.toString());
431 out.reset();
432 sb.append("\r\n");
433 }
434
435 boolean b = preferences().getBoolProperty(JSPrefs.EXPORT_MIDI_MAPS_TO_SESSION_SCRIPT);
436 if(b) {
437 exportInstrMapsToLscpScript(lscpClient);
438 sb.append(out.toString());
439 out.reset();
440 sb.append("\r\n");
441 }
442
443 int chnId = 0;
444 for(JSChannelsPane cp : CC.getMainFrame().getChannelsPaneList()) {
445 for(JSChannel chn : cp.getChannels()) {
446 SamplerChannelModel scm;
447 scm = CC.getSamplerModel().getChannelById(chn.getChannelId());
448 exportChannelToLscpScript(scm.getChannelInfo(), chnId, lscpClient);
449 sb.append(out.toString());
450 out.reset();
451
452 sb.append("\r\n");
453
454 exportFxSendsToLscpScript(scm, chnId, lscpClient);
455 sb.append(out.toString());
456 out.reset();
457
458 sb.append("\r\n");
459
460 chnId++;
461 }
462 }
463
464 sb.append(CC.getViewConfig().exportSessionViewConfig());
465
466 return sb.toString();
467 }
468
469 private static void
470 exportMidiDeviceToLscpScript(MidiInputDevice mid, int devId, Client lscpCLient) {
471 try {
472 String s = mid.getDriverName();
473 lscpCLient.createMidiInputDevice(s, mid.getAdditionalParameters());
474
475 MidiPort[] mPorts = mid.getMidiPorts();
476 int l = mPorts.length;
477 if(l != 1) lscpCLient.setMidiInputPortCount(devId, l);
478
479 for(int i = 0; i < l; i++) {
480 Parameter[] prms = mPorts[i].getAllParameters();
481 for(Parameter p : prms) {
482 if(!p.isFixed() && p.getStringValue().length() > 0)
483 lscpCLient.setMidiInputPortParameter(devId, i, p);
484 }
485 }
486 } catch(Exception e) {
487 CC.getLogger().log(Level.FINE, HF.getErrorMessage(e), e);
488 }
489 }
490
491 private static void
492 exportAudioDeviceToLscpScript(AudioOutputDevice aod, int devId, Client lscpCLient) {
493 try {
494 String s = aod.getDriverName();
495 lscpCLient.createAudioOutputDevice(s, aod.getAllParameters());
496
497 AudioOutputChannel[] chns = aod.getAudioChannels();
498
499 for(int i = 0; i < chns.length; i++) {
500 Parameter[] prms = chns[i].getAllParameters();
501 for(Parameter p : prms) {
502 if(p.isFixed() || p.getStringValue().length() == 0);
503 else lscpCLient.setAudioOutputChannelParameter(devId, i, p);
504 }
505 }
506 } catch(Exception e) {
507 CC.getLogger().log(Level.FINE, HF.getErrorMessage(e), e);
508 }
509 }
510
511 private static void
512 exportChannelToLscpScript(SamplerChannel chn, int chnId, Client lscpCLient) {
513 try {
514 lscpCLient.addSamplerChannel();
515
516 SamplerModel sm = CC.getSamplerModel();
517 int id = chn.getMidiInputDevice();
518 if(id != -1) {
519 for(int i = 0; i < sm.getMidiDeviceCount(); i++) {
520 if(sm.getMidiDevice(i).getDeviceId() == id) {
521 lscpCLient.setChannelMidiInputDevice(chnId, i);
522 break;
523 }
524 }
525 lscpCLient.setChannelMidiInputPort(chnId, chn.getMidiInputPort());
526 lscpCLient.setChannelMidiInputChannel(chnId, chn.getMidiInputChannel());
527 }
528
529 if(chn.getEngine() != null) {
530 lscpCLient.loadSamplerEngine(chn.getEngine().getName(), chnId);
531 lscpCLient.setChannelVolume(chnId, chn.getVolume());
532 int mapId = chn.getMidiInstrumentMapId();
533 lscpCLient.setChannelMidiInstrumentMap(chnId, mapId);
534 }
535
536 id = chn.getAudioOutputDevice();
537 if(id != -1) {
538 for(int i = 0; i < sm.getAudioDeviceCount(); i++) {
539 if(sm.getAudioDevice(i).getDeviceId() == id) {
540 lscpCLient.setChannelAudioOutputDevice(chnId, i);
541 break;
542 }
543 }
544
545 Integer[] routing = chn.getAudioOutputRouting();
546
547 for(int j = 0; j < routing.length; j++) {
548 int k = routing[j];
549 if(k == j) continue;
550
551 lscpCLient.setChannelAudioOutputChannel(chnId, j, k);
552 }
553 }
554
555 String s = chn.getInstrumentFile();
556 int i = chn.getInstrumentIndex();
557 if(s != null) lscpCLient.loadInstrument(s, i, chnId, true);
558
559 if(chn.isMuted() && !chn.isMutedBySolo()) lscpCLient.setChannelMute(chnId, true);
560 if(chn.isSoloChannel()) lscpCLient.setChannelSolo(chnId, true);
561 } catch(Exception e) {
562 CC.getLogger().log(Level.FINE, HF.getErrorMessage(e), e);
563 }
564 }
565
566 private static void
567 exportFxSendsToLscpScript(SamplerChannelModel scm, int chnId, Client lscpClient) {
568 try {
569 FxSend[] fxSends = scm.getFxSends();
570
571 for(int i = 0; i < fxSends.length; i++) {
572 FxSend f = fxSends[i];
573 lscpClient.createFxSend(chnId, f.getMidiController(), f.getName());
574 lscpClient.setFxSendLevel(chnId, i, f.getLevel());
575
576 Integer[] r = f.getAudioOutputRouting();
577 for(int j = 0; j < r.length; j++) {
578 lscpClient.setFxSendAudioOutputChannel(chnId, i, j, r[j]);
579 }
580 }
581 } catch(Exception e) {
582 CC.getLogger().log(Level.FINE, HF.getErrorMessage(e), e);
583 }
584 }
585 }

  ViewVC Help
Powered by ViewVC