1 |
iliev |
1540 |
/* |
2 |
|
|
* JSampler - a java front-end for LinuxSampler |
3 |
|
|
* |
4 |
|
|
* Copyright (C) 2005-2007 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 org.w3c.dom.Document; |
26 |
|
|
import org.w3c.dom.Element; |
27 |
|
|
import org.w3c.dom.NamedNodeMap; |
28 |
|
|
import org.w3c.dom.Node; |
29 |
|
|
import org.w3c.dom.NodeList; |
30 |
|
|
|
31 |
|
|
|
32 |
|
|
/** |
33 |
|
|
* |
34 |
|
|
* @author Grigor Iliev |
35 |
|
|
*/ |
36 |
|
|
public class OrchestraInstrument extends Resource implements org.linuxsampler.lscp.Instrument { |
37 |
|
|
private String path = null; |
38 |
|
|
private int instrumentIndex = 0; |
39 |
|
|
private String engine = "GIG"; |
40 |
|
|
|
41 |
|
|
/** |
42 |
|
|
* Creates a new instance of <code>OrchestraInstrument</code>. |
43 |
|
|
*/ |
44 |
|
|
public OrchestraInstrument() { |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
/** |
48 |
|
|
* Returns the absolute pathname of the instrument location. |
49 |
|
|
* @return The absolute pathname of the instrument location. |
50 |
|
|
*/ |
51 |
|
|
public String |
52 |
|
|
getFilePath() { return path; } |
53 |
|
|
|
54 |
|
|
/** |
55 |
|
|
* Sets the absolute pathname of the instrument location. |
56 |
|
|
* @param path Specifies the absolute pathname of the instrument location. |
57 |
|
|
*/ |
58 |
|
|
public void |
59 |
|
|
setFilePath(String path) { |
60 |
|
|
this.path = path; |
61 |
|
|
fireChangeEvent(); |
62 |
|
|
} |
63 |
|
|
|
64 |
|
|
/** |
65 |
|
|
* Returns the index of the instrument in the instrument file. |
66 |
|
|
* @return The index of the instrument in the instrument file. |
67 |
|
|
*/ |
68 |
|
|
public int |
69 |
|
|
getInstrumentIndex() { return instrumentIndex; } |
70 |
|
|
|
71 |
|
|
/** |
72 |
|
|
* Sets the index of the instrument in the instrument file. |
73 |
|
|
* @param idx The index of the instrument in the instrument file. |
74 |
|
|
*/ |
75 |
|
|
public void |
76 |
|
|
setInstrumentIndex(int idx) { |
77 |
|
|
instrumentIndex = idx; |
78 |
|
|
fireChangeEvent(); |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
/** |
82 |
|
|
* Gets the engine to be used for loading this instrument. |
83 |
|
|
* @return The engine to be used for loading this instrument. |
84 |
|
|
*/ |
85 |
|
|
public String |
86 |
|
|
getEngine() { return engine; } |
87 |
|
|
|
88 |
|
|
/** |
89 |
|
|
* Sets the engine to be used for loading this instrument. |
90 |
|
|
*/ |
91 |
|
|
public void |
92 |
|
|
setEngine(String engine) { this.engine = engine; } |
93 |
|
|
|
94 |
|
|
public String |
95 |
|
|
getFormatFamily() { return null; } |
96 |
|
|
|
97 |
|
|
public String |
98 |
|
|
getFormatVersion() { return null; } |
99 |
|
|
|
100 |
|
|
/** |
101 |
|
|
* Returns the name of this instrument. |
102 |
|
|
* @return The name of this instrument. |
103 |
|
|
*/ |
104 |
|
|
public String |
105 |
|
|
toString() { return getName(); } |
106 |
|
|
|
107 |
|
|
private final StringBuffer sb = new StringBuffer(); |
108 |
|
|
|
109 |
|
|
/** |
110 |
|
|
* Gets a string representation of this |
111 |
|
|
* instrument appropriate for Drag & Drop operations. |
112 |
|
|
* @return A string representation of this |
113 |
|
|
* instrument appropriate for Drag & Drop operations. |
114 |
|
|
* @see #isDnDString |
115 |
|
|
*/ |
116 |
|
|
public String |
117 |
|
|
getDnDString() { |
118 |
|
|
sb.setLength(0); |
119 |
|
|
sb.append("[Instrument Definition]\n"); |
120 |
|
|
sb.append(getName()).append("\n"); |
121 |
|
|
sb.append("\n"); |
122 |
|
|
sb.append(getDescription()).append("\n"); |
123 |
|
|
sb.append(getFilePath()).append("\n"); |
124 |
|
|
sb.append(getInstrumentIndex()).append("\n"); |
125 |
|
|
|
126 |
|
|
return sb.toString(); |
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
/** |
130 |
|
|
* Sets the instrument properties provided by the specified |
131 |
|
|
* Drag & Drop string representation. |
132 |
|
|
* @param s String providing Drag & Drop string representation of an instrument. |
133 |
|
|
* @throws IllegalArgumentException If the specified string is not |
134 |
|
|
* a Drag & Drop string representation of an instrument. |
135 |
|
|
* @see #getDnDString |
136 |
|
|
*/ |
137 |
|
|
public void |
138 |
|
|
setDnDString(String s) { |
139 |
|
|
if(!isDnDString(s)) throw new IllegalArgumentException("Not a DnD string"); |
140 |
|
|
|
141 |
|
|
String[] args = s.split("\n"); |
142 |
|
|
if(args.length < 6) throw new IllegalArgumentException("Not a DnD string"); |
143 |
|
|
|
144 |
|
|
setName(args[1]); |
145 |
|
|
setDescription(args[3]); |
146 |
|
|
setFilePath(args[4]); |
147 |
|
|
|
148 |
|
|
try { setInstrumentIndex(Integer.parseInt(args[5])); } |
149 |
|
|
catch(Exception x) { |
150 |
|
|
throw new IllegalArgumentException("Not a DnD string", x); |
151 |
|
|
} |
152 |
|
|
} |
153 |
|
|
|
154 |
|
|
/** |
155 |
|
|
* Determines whether the specified string is |
156 |
|
|
* a Drag & Drop representation of an instrument. |
157 |
|
|
* @param s The string to be checked. |
158 |
|
|
* @return <code>true</code> if the specified string is |
159 |
|
|
* a Drag & Drop representation of an instrument, <code>false</code> otherwise. |
160 |
|
|
*/ |
161 |
|
|
public static boolean |
162 |
|
|
isDnDString(String s) { |
163 |
|
|
if(s == null) return false; |
164 |
|
|
return s.startsWith("[Instrument Definition]\n"); |
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
/** |
168 |
|
|
* Reads and sets the instrument properties by the supplied <code>node</code>. |
169 |
|
|
* @param node The node providing the instrument properties. |
170 |
|
|
* @throws IllegalArgumentException If an error occurs while |
171 |
|
|
* reading the instrument properties. |
172 |
|
|
*/ |
173 |
|
|
public void |
174 |
|
|
readObject(Node node) { |
175 |
|
|
if( |
176 |
|
|
node.getNodeType() != Node.ELEMENT_NODE || |
177 |
|
|
!(node.getNodeName().equals("instrument")) |
178 |
|
|
) { |
179 |
|
|
throw new IllegalArgumentException("Not an instrument node!"); |
180 |
|
|
} |
181 |
|
|
|
182 |
|
|
NamedNodeMap nnm = node.getAttributes(); |
183 |
|
|
Node n = nnm.getNamedItem("name"); |
184 |
|
|
if(n == null) { |
185 |
|
|
throw new IllegalArgumentException("The instrument name is undefined!"); |
186 |
|
|
} |
187 |
|
|
DOMUtils.validateTextContent(n); |
188 |
|
|
setName(n.getFirstChild().getNodeValue()); |
189 |
|
|
|
190 |
|
|
String s = null; |
191 |
|
|
NodeList nl = node.getChildNodes(); |
192 |
|
|
|
193 |
|
|
for(int i = 0; i < nl.getLength(); i++) { |
194 |
|
|
node = nl.item(i); |
195 |
|
|
if(node.getNodeType() != Node.ELEMENT_NODE) continue; |
196 |
|
|
|
197 |
|
|
s = node.getNodeName(); |
198 |
|
|
if(s.equals("description")) { |
199 |
|
|
if(node.hasChildNodes()) { |
200 |
|
|
DOMUtils.validateTextContent(node); |
201 |
|
|
setDescription(node.getFirstChild().getNodeValue()); |
202 |
|
|
} |
203 |
|
|
} else if(s.equals("path")) { |
204 |
|
|
DOMUtils.validateTextContent(node); |
205 |
|
|
setFilePath(node.getFirstChild().getNodeValue()); |
206 |
|
|
} else if(s.equals("instrument-index")) { |
207 |
|
|
DOMUtils.validateTextContent(node); |
208 |
|
|
try { |
209 |
|
|
int j; |
210 |
|
|
j = Integer.parseInt(node.getFirstChild().getNodeValue()); |
211 |
|
|
setInstrumentIndex(j); |
212 |
|
|
} catch(NumberFormatException x) { |
213 |
|
|
throw new IllegalArgumentException("Not a number"); |
214 |
|
|
} |
215 |
|
|
} else { // Unknown content |
216 |
|
|
CC.getLogger().info ("Unknown field: " + s); |
217 |
|
|
} |
218 |
|
|
} |
219 |
|
|
} |
220 |
|
|
|
221 |
|
|
/** |
222 |
|
|
* Writes the instrument properties to the |
223 |
|
|
* specified node of document <code>doc</code>. |
224 |
|
|
* @param doc The document containing <code>node</code>. |
225 |
|
|
* @param node Specifies the node where the instrument properties |
226 |
|
|
* should be written. |
227 |
|
|
*/ |
228 |
|
|
public void |
229 |
|
|
writeObject(Document doc, Node node) { |
230 |
|
|
Element el = doc.createElement("instrument"); |
231 |
|
|
el.setAttribute("name", getName()); |
232 |
|
|
node.appendChild(el); |
233 |
|
|
|
234 |
|
|
node = el; |
235 |
|
|
|
236 |
|
|
el = doc.createElement("description"); |
237 |
|
|
el.appendChild(doc.createTextNode(getDescription())); |
238 |
|
|
node.appendChild(el); |
239 |
|
|
|
240 |
|
|
el = doc.createElement("path"); |
241 |
|
|
el.appendChild(doc.createTextNode(getFilePath())); |
242 |
|
|
node.appendChild(el); |
243 |
|
|
|
244 |
|
|
el = doc.createElement("instrument-index"); |
245 |
|
|
el.appendChild(doc.createTextNode(String.valueOf(getInstrumentIndex()))); |
246 |
|
|
node.appendChild(el); |
247 |
|
|
} |
248 |
|
|
} |