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