1 |
/* |
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 java.util.Vector; |
26 |
|
27 |
import net.sf.juife.PDUtils; |
28 |
|
29 |
import org.linuxsampler.lscp.MidiInstrumentInfo; |
30 |
|
31 |
import org.jsampler.event.MidiInstrumentEvent; |
32 |
import org.jsampler.event.MidiInstrumentListener; |
33 |
|
34 |
|
35 |
/** |
36 |
* Represents a MIDI instrument. |
37 |
* @author Grigor Iliev |
38 |
*/ |
39 |
public class MidiInstrument { |
40 |
private MidiInstrumentInfo info; |
41 |
private final Vector<MidiInstrumentListener> listeners = |
42 |
new Vector<MidiInstrumentListener>(); |
43 |
|
44 |
private static int firstProgramNumber = 0; |
45 |
|
46 |
|
47 |
/** Creates a new instance of MidiInstrument */ |
48 |
public |
49 |
MidiInstrument() { } |
50 |
|
51 |
/** |
52 |
* Creates a new instance of <code>MidiInstrument</code>. |
53 |
* @param info Provides mapping information about this instrument. |
54 |
*/ |
55 |
public |
56 |
MidiInstrument(MidiInstrumentInfo info) { this.info = info; } |
57 |
|
58 |
/** |
59 |
* Registers the specified listener for receiving event messages. |
60 |
* @param l The <code>MidiInstrumentListener</code> to register. |
61 |
*/ |
62 |
public void |
63 |
addMidiInstrumentListener(MidiInstrumentListener l) { |
64 |
listeners.add(l); |
65 |
} |
66 |
|
67 |
/** |
68 |
* Removes the specified listener. |
69 |
* @param l The <code>MidiInstrumentListener</code> to remove. |
70 |
*/ |
71 |
public void |
72 |
removeMidiInstrumentListener(MidiInstrumentListener l) { |
73 |
listeners.remove(l); |
74 |
} |
75 |
|
76 |
|
77 |
/** |
78 |
* Gets the name of this MIDI instrument. |
79 |
* @return The name of this MIDI instrument. |
80 |
*/ |
81 |
public String |
82 |
getName() { return info.getName(); } |
83 |
|
84 |
/** |
85 |
* Sets the name of this MIDI instrument. |
86 |
* @param name The new name of this MIDI instrument. |
87 |
*/ |
88 |
public void |
89 |
setName(String name) { |
90 |
info.setName(name); |
91 |
fireInfoChanged(); |
92 |
} |
93 |
|
94 |
/** |
95 |
* Gets the information about this instrument. |
96 |
* @return The information about this instrument. |
97 |
*/ |
98 |
public MidiInstrumentInfo |
99 |
getInfo() { return info; } |
100 |
|
101 |
/** |
102 |
* Sets the information about this MIDI instrument. |
103 |
* @param info The new information about this MIDI instrument. |
104 |
*/ |
105 |
public void |
106 |
setInfo(MidiInstrumentInfo info) { |
107 |
this.info = info; |
108 |
fireInfoChanged(); |
109 |
} |
110 |
|
111 |
/** |
112 |
* Gets the MIDI program numbering, whether |
113 |
* the index of the first MIDI program is 0 or 1. |
114 |
*/ |
115 |
public static int |
116 |
getFirstProgramNumber() { return firstProgramNumber; } |
117 |
|
118 |
/** |
119 |
* Sets the MIDI program numbering, whether |
120 |
* the index of the first MIDI program is 0 or 1. |
121 |
*/ |
122 |
public static void |
123 |
setFirstProgramNumber(int idx) { firstProgramNumber = idx; } |
124 |
|
125 |
/** |
126 |
* Determines whether the specified object is of type |
127 |
* <code>MidiInstrument</code> and has equal map ID, MIDI bank and MIDI program. |
128 |
* @param obj The reference object with which to compare. |
129 |
* @return <code>true</code> if the specified object is of type |
130 |
* <code>MidiInstrument</code> and has equal map ID, MIDI bank and MIDI program. |
131 |
*/ |
132 |
public boolean |
133 |
equals(Object obj) { |
134 |
if(obj == null || getInfo() == null) return false; |
135 |
if(!(obj instanceof MidiInstrument)) return false; |
136 |
MidiInstrument i = (MidiInstrument)obj; |
137 |
return getInfo().equals(i.getInfo()); |
138 |
} |
139 |
|
140 |
/** |
141 |
* Returns the program number and name of this instrument. |
142 |
* @return The program number and name of this instrument. |
143 |
*/ |
144 |
public String |
145 |
toString() { |
146 |
int i = getFirstProgramNumber() + getInfo().getMidiProgram(); |
147 |
return String.valueOf(i) + ". " + getName(); |
148 |
} |
149 |
|
150 |
/** |
151 |
* Notifies listeners that the the MIDI instrument settings are changed. |
152 |
* Note that this method can be invoked outside the event-dispatching thread. |
153 |
*/ |
154 |
private void |
155 |
fireInfoChanged() { |
156 |
final MidiInstrumentEvent e = new MidiInstrumentEvent(this); |
157 |
|
158 |
PDUtils.runOnUiThread(new Runnable() { |
159 |
public void |
160 |
run() { fireInfoChanged(e); } |
161 |
}); |
162 |
} |
163 |
|
164 |
|
165 |
/** Notifies listeners that the MIDI instrument settings are changed. */ |
166 |
private void |
167 |
fireInfoChanged(MidiInstrumentEvent e) { |
168 |
for(MidiInstrumentListener l : listeners) l.instrumentInfoChanged(e); |
169 |
} |
170 |
} |