def3f3d5a0752c79e37ca1422f4199356459fd40
[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 import com.google.common.eventbus.EventBus;
28
29 /**
30  * {@link Filter} implementation that can reduce the stereo width of a signal,
31  * or even reverse the channels.
32  *
33  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34  */
35 public class StereoSeparationFilter extends AudioProcessingFilter {
36
37         /** The separation knob. */
38         private final Knob separationKnob;
39
40         /**
41          * Creates a new stereo separation filter.
42          *
43          * @param eventBus
44          *              The event bus
45          */
46         public StereoSeparationFilter(EventBus eventBus) {
47                 super(eventBus, "Stereo Separation");
48                 separationKnob = new Knob("Separation", 1.0);
49         }
50
51         //
52         // CONTROLLED METHODS
53         //
54
55         @Override
56         public List<Controller<?>> controllers() {
57                 return Arrays.<Controller<?>>asList(separationKnob);
58         }
59
60         //
61         // AUDIOPROCESSINGFILTER METHODS
62         //
63
64         @Override
65         protected int[] processSamples(int[] samples) {
66                 if (samples.length == 1) {
67                         return samples;
68                 }
69                 int[] processedSamples = new int[samples.length];
70                 double a = (separationKnob.value() + 1) / 2.0;
71                 processedSamples[0] = (int) (samples[0] * a + samples[1] * (1 - a));
72                 processedSamples[1] = (int) (samples[1] * a + samples[0] * (1 - a));
73                 return processedSamples;
74         }
75
76 }