Add name to all controlled components.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / filter / StereoSeparationFilter.java
1 /*
2  * Sonitus - StereoSeparationFilter.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sonitus.data.filter;
19
20 import java.util.Arrays;
21 import java.util.List;
22
23 import net.pterodactylus.sonitus.data.Controller;
24 import net.pterodactylus.sonitus.data.Filter;
25 import net.pterodactylus.sonitus.data.controller.Knob;
26
27 /**
28  * {@link Filter} implementation that can reduce the stereo width of a signal,
29  * or even reverse the channels.
30  *
31  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
32  */
33 public class StereoSeparationFilter extends AudioProcessingFilter {
34
35         /** The separation knob. */
36         private final Knob separationKnob;
37
38         /** Creates a new stereo separation filter. */
39         public StereoSeparationFilter() {
40                 super("Stereo Separation");
41                 separationKnob = new Knob("Separation", 1.0);
42         }
43
44         //
45         // CONTROLLED METHODS
46         //
47
48         @Override
49         public List<Controller<?>> controllers() {
50                 return Arrays.<Controller<?>>asList(separationKnob);
51         }
52
53         //
54         // AUDIOPROCESSINGFILTER METHODS
55         //
56
57         @Override
58         protected int[] processSamples(int[] samples) {
59                 if (samples.length == 1) {
60                         return samples;
61                 }
62                 int[] processedSamples = new int[samples.length];
63                 double a = (separationKnob.value() + 1) / 2.0;
64                 processedSamples[0] = (int) (samples[0] * a + samples[1] * (1 - a));
65                 processedSamples[1] = (int) (samples[1] * a + samples[0] * (1 - a));
66                 return processedSamples;
67         }
68
69 }