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