1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 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 |
package org.jsampler; |
23 |
|
24 |
import java.util.ArrayList; |
25 |
import java.util.Arrays; |
26 |
|
27 |
import net.sf.juife.PDUtils; |
28 |
|
29 |
import org.jsampler.event.EffectChainEvent; |
30 |
import org.jsampler.event.EffectChainListener; |
31 |
import org.linuxsampler.lscp.EffectChainInfo; |
32 |
import org.linuxsampler.lscp.EffectInstanceInfo; |
33 |
|
34 |
|
35 |
/** |
36 |
* |
37 |
* @author Grigor Iliev |
38 |
*/ |
39 |
public class EffectChain { |
40 |
private int chainId = -1; |
41 |
private final ArrayList<EffectInstance> effectInstances = new ArrayList<EffectInstance>(); |
42 |
|
43 |
private final ArrayList<EffectChainListener> listeners = new ArrayList<EffectChainListener>(); |
44 |
|
45 |
public |
46 |
EffectChain(EffectChainInfo chain) { |
47 |
setChainId(chain.getChainId()); |
48 |
|
49 |
setEffectInstances(chain); |
50 |
} |
51 |
|
52 |
/** |
53 |
* Registers the specified listener to be notified when |
54 |
* the settings of the effect chain are changed. |
55 |
* @param l The <code>EffectChainListener</code> to register. |
56 |
*/ |
57 |
public void |
58 |
addEffectChainListener(EffectChainListener l) { listeners.add(l); } |
59 |
|
60 |
/** |
61 |
* Removes the specified listener. |
62 |
* @param l The <code>EffectChainListener</code> to remove. |
63 |
*/ |
64 |
public void |
65 |
removeEffectChainListener(EffectChainListener l) { listeners.remove(l); } |
66 |
|
67 |
/** |
68 |
* Gets the numerical ID of the chain. |
69 |
* @return The numerical ID of the chain or -1 if the ID is not set. |
70 |
*/ |
71 |
public int |
72 |
getChainId() { return chainId; } |
73 |
|
74 |
/** Sets the numerical ID of the chain. */ |
75 |
public void |
76 |
setChainId(int id) { chainId = id; } |
77 |
|
78 |
public EffectInstance[] |
79 |
getEffectInstances() { |
80 |
return effectInstances.toArray(new EffectInstance[0]); |
81 |
} |
82 |
|
83 |
public void |
84 |
setEffectInstances(EffectChainInfo chain) { |
85 |
effectInstances.clear(); |
86 |
for(int i = 0; i < chain.getEffectInstanceCount(); i++) { |
87 |
effectInstances.add(new EffectInstance(chain.getEffectInstance(i))); |
88 |
} |
89 |
fireEffectInstanceListChanged(); |
90 |
|
91 |
} |
92 |
|
93 |
public int |
94 |
getEffectInstanceCount() { return effectInstances.size(); } |
95 |
|
96 |
public EffectInstance |
97 |
getEffectInstance(int idx) { return effectInstances.get(idx); } |
98 |
|
99 |
public int |
100 |
getIndex(int instanceId) { |
101 |
for(int i = 0; i < effectInstances.size(); i++) { |
102 |
if(effectInstances.get(i).getInstanceId() == instanceId) { |
103 |
return i; |
104 |
} |
105 |
} |
106 |
return -1; |
107 |
} |
108 |
|
109 |
public EffectInstance |
110 |
getEffectInstanceById(int instanceId) { |
111 |
int idx = getIndex(instanceId); |
112 |
if(idx == -1) return null; |
113 |
|
114 |
return effectInstances.get(idx); |
115 |
} |
116 |
|
117 |
private void |
118 |
fireEffectInstanceListChanged() { |
119 |
final EffectChainEvent e = new EffectChainEvent(this, this); |
120 |
PDUtils.runOnUiThread(new Runnable() { |
121 |
public void |
122 |
run() { fireEffectInstanceListChanged(e); } |
123 |
}); |
124 |
} |
125 |
|
126 |
/** |
127 |
* This method should be invoked from the event-dispatching thread. |
128 |
*/ |
129 |
private void |
130 |
fireEffectInstanceListChanged(EffectChainEvent e) { |
131 |
CC.getSamplerModel().setModified(true); |
132 |
for(EffectChainListener l : listeners) l.effectInstanceListChanged(e); |
133 |
} |
134 |
} |