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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2192 - (show annotations) (download)
Fri Jun 24 21:34:51 2011 UTC (12 years, 9 months ago) by iliev
File size: 9412 byte(s)
* Initial implementation of Sampler Browser
  (choose Window/Sampler Browser) - another way to view/edit
  the sampler configuration (work in progress - for now only
  support for viewing/editing send effects)

1 /*
2 * JSampler - a java front-end for LinuxSampler
3 *
4 * Copyright (C) 2005-2011 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.ArrayList;
26 import java.util.Vector;
27
28 import javax.swing.SwingUtilities;
29
30 import org.jsampler.event.AudioDeviceEvent;
31 import org.jsampler.event.AudioDeviceListener;
32
33 import org.jsampler.task.Audio;
34
35 import org.linuxsampler.lscp.AudioOutputDevice;
36 import org.linuxsampler.lscp.Effect;
37 import org.linuxsampler.lscp.Parameter;
38
39
40 /**
41 * This class provides default implementation of the <code>AudioDeviceModel</code> interface.
42 * @author Grigor Iliev
43 */
44 public class DefaultAudioDeviceModel implements AudioDeviceModel {
45 private AudioOutputDevice audioDevice;
46
47 private final ArrayList<AudioDeviceListener> listeners = new ArrayList<AudioDeviceListener>();
48 private final ArrayList<EffectChain> effectChains = new ArrayList<EffectChain>();
49
50 /**
51 * Creates a new instance of <code>DefaultAudioDeviceModel</code> using the
52 * specified non-null audio device.
53 * @param audioDevice An <code>AudioOutputDevice</code> instance providing the current
54 * settings of the audio device which will be represented by this model.
55 * @throws IllegalArgumentException If <code>audioDevice</code> is <code>null</code>.
56 */
57 public
58 DefaultAudioDeviceModel(AudioOutputDevice audioDevice) {
59 if(audioDevice == null)
60 throw new IllegalArgumentException("audioDevice must be non null");
61
62 this.audioDevice = audioDevice;
63 }
64
65 /**
66 * Registers the specified listener to be notified when
67 * the settings of the audio device are changed.
68 * @param l The <code>AudioDeviceListener</code> to register.
69 */
70 @Override
71 public void
72 addAudioDeviceListener(AudioDeviceListener l) { listeners.add(l); }
73
74 /**
75 * Removes the specified listener.
76 * @param l The <code>AudioDeviceListener</code> to remove.
77 */
78 @Override
79 public void
80 removeAudioDeviceListener(AudioDeviceListener l) { listeners.remove(l); }
81
82 /**
83 * Gets the numerical ID of this audio device.
84 * @return The numerical ID of this audio device or
85 * -1 if the device number is not set.
86 */
87 @Override
88 public int
89 getDeviceId() { return audioDevice.getDeviceId(); }
90
91 /**
92 * Gets the current settings of the audio device represented by this model.
93 * @return <code>AudioOutputDevice</code> instance providing
94 * the current settings of the audio device represented by this model.
95 */
96 @Override
97 public AudioOutputDevice
98 getDeviceInfo() { return audioDevice; }
99
100 /**
101 * Updates the settings of the audio device represented by this model.
102 * @param device The new audio device settings.
103 */
104 @Override
105 public void
106 setDeviceInfo(AudioOutputDevice device) {
107 audioDevice = device;
108 fireSettingsChanged();
109 }
110
111 /**
112 * Sets whether the audio device is enabled or disabled.
113 * @param active If <code>true</code> the audio device is enabled,
114 * else the device is disabled.
115 */
116 @Override
117 public void
118 setActive(boolean active) {
119 if(active == getDeviceInfo().isActive()) return;
120
121 audioDevice.setActive(active);
122 fireSettingsChanged();
123 }
124
125 /**
126 * Determines whether the audio device is active.
127 * @return <code>true</code> if the device is enabled and <code>false</code> otherwise.
128 */
129 @Override
130 public boolean
131 isActive() { return audioDevice.isActive(); }
132
133 /**
134 * Schedules a new task for enabling/disabling the audio device.
135 * @param active If <code>true</code> the audio device is enabled,
136 * else the device is disabled.
137 */
138 @Override
139 public void
140 setBackendActive(boolean active) {
141 CC.getTaskQueue().add(new Audio.EnableDevice(getDeviceId(), active));
142 }
143
144 /**
145 * Schedules a new task for altering
146 * a specific setting of the audio output device.
147 * @param prm The parameter to be set.
148 */
149 @Override
150 public void
151 setBackendDeviceParameter(Parameter prm) {
152 CC.getTaskQueue().add(new Audio.SetDeviceParameter(getDeviceId(), prm));
153 }
154
155 /**
156 * Schedules a new task for changing the channel number of the audio device.
157 * @param channels The new number of audio channels.
158 */
159 @Override
160 public void
161 setBackendChannelCount(int channels) {
162 CC.getTaskQueue().add(new Audio.SetChannelCount(getDeviceId(), channels));
163 }
164
165 /**
166 * Schedules a new task for altering a specific
167 * setting of the specified audio output channel.
168 * @param channel The channel number.
169 * @param prm The parameter to be set.
170 */
171 @Override
172 public void
173 setBackendChannelParameter(int channel, Parameter prm) {
174 CC.getTaskQueue().add(new Audio.SetChannelParameter(getDeviceId(), channel, prm));
175 }
176
177 /** Gets the current number of send effect chains. */
178 @Override
179 public int
180 getSendEffectChainCount() { return effectChains.size(); }
181
182 /** Gets the effect chain at the specified position. */
183 @Override
184 public EffectChain
185 getSendEffectChain(int chainIdx) { return effectChains.get(chainIdx); }
186
187 @Override
188 public EffectChain
189 getSendEffectChainById(int chainId) {
190 for(int i = 0; i < getSendEffectChainCount(); i++) {
191 EffectChain chain = getSendEffectChain(i);
192 if(chain.getChainId() == chainId) return chain;
193 }
194
195 return null;
196 }
197
198 /**
199 * Adds the specified send effect chain to the specified audio output device.
200 */
201 @Override
202 public void
203 addSendEffectChain(EffectChain chain) {
204 effectChains.add(chain);
205 fireSendEffectChainAdded(chain);
206 }
207
208 /**
209 * Removes the specified send effect chain from the audio output device.
210 */
211 @Override
212 public void
213 removeSendEffectChain(int chainId) {
214 for(int i = 0; i < effectChains.size(); i++) {
215 if(effectChains.get(i).getChainId() == chainId) {
216 fireSendEffectChainRemoved(effectChains.remove(i));
217 return;
218 }
219 }
220 }
221
222 public void
223 removeAllSendEffectChains() {
224 for(int i = effectChains.size() - 1; i >= 0; i--) {
225 fireSendEffectChainRemoved(effectChains.remove(i));
226 }
227 }
228
229 /**
230 * Schedules a new task for adding a new send effect chain and
231 * assigning it to the specified audio output device.
232 */
233 @Override
234 public void
235 addBackendSendEffectChain() {
236 CC.getTaskQueue().add(new Audio.AddSendEffectChain(getDeviceId()));
237 }
238
239 /** Schedules a new task for removing the specified send effect chain. */
240 @Override
241 public void
242 removeBackendSendEffectChain(int chainId) {
243 CC.getTaskQueue().add(new Audio.RemoveSendEffectChain(getDeviceId(), chainId));
244 }
245
246 /**
247 * Schedules a new task for creating new effect instances and inserting them
248 * in the specified send effect chain at the specified position.
249 */
250 @Override
251 public void
252 addBackendEffectInstances(Effect[] effects, int chainId, int index) {
253 CC.getTaskQueue().add (
254 new Audio.AddNewEffectInstances(effects, getDeviceId(), chainId, index)
255 );
256 }
257
258 /**
259 * Schedules a new task for removing the specified
260 * effect instance from the specified send effect chain.
261 */
262 @Override
263 public void
264 removeBackendEffectInstance(int chainId, int instanceId) {
265 CC.getTaskQueue().add(new Audio.RemoveEffectInstance(getDeviceId(), chainId, instanceId));
266 }
267
268 /**
269 * Notifies listeners that the settings of the audio device are changed.
270 */
271 private void
272 fireSettingsChanged() {
273 SwingUtilities.invokeLater(new Runnable() {
274 public void
275 run() {
276 AudioDeviceModel model = DefaultAudioDeviceModel.this;
277 fireSettingsChanged(new AudioDeviceEvent(model, model));
278 }
279 });
280 }
281
282 /**
283 * Notifies listeners that the settings of the audio device are changed.
284 * This method should be invoked from the event-dispatching thread.
285 */
286 private void
287 fireSettingsChanged(final AudioDeviceEvent e) {
288 CC.getSamplerModel().setModified(true);
289 for(AudioDeviceListener l : listeners) l.settingsChanged(e);
290 }
291
292 private void
293 fireSendEffectChainAdded(final EffectChain chain) {
294 SwingUtilities.invokeLater(new Runnable() {
295 public void
296 run() {
297 AudioDeviceModel m = DefaultAudioDeviceModel.this;
298 fireSendEffectChainAdded(new AudioDeviceEvent(m, m, chain));
299 }
300 });
301 }
302
303 /** This method should be invoked from the event-dispatching thread. */
304 private void
305 fireSendEffectChainAdded(final AudioDeviceEvent e) {
306 CC.getSamplerModel().setModified(true);
307 for(AudioDeviceListener l : listeners) l.sendEffectChainAdded(e);
308 }
309
310 private void
311 fireSendEffectChainRemoved(final EffectChain chain) {
312 SwingUtilities.invokeLater(new Runnable() {
313 public void
314 run() {
315 AudioDeviceModel m = DefaultAudioDeviceModel.this;
316 fireSendEffectChainRemoved(new AudioDeviceEvent(m, m, chain));
317 }
318 });
319 }
320
321 /** This method should be invoked from the event-dispatching thread. */
322 private void
323 fireSendEffectChainRemoved(final AudioDeviceEvent e) {
324 CC.getSamplerModel().setModified(true);
325 for(AudioDeviceListener l : listeners) l.sendEffectChainRemoved(e);
326 }
327 }

  ViewVC Help
Powered by ViewVC