5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package com.sun.media.sound;
26
27 /**
28 * A simple look-ahead volume limiter with very fast attack and fast release.
29 * This filter is used for preventing clipping.
30 *
31 * @author Karl Helgason
32 */
33 public final class SoftLimiter implements SoftAudioProcessor {
34
35 float lastmax = 0;
36 float gain = 1;
37 float[] temp_bufferL;
38 float[] temp_bufferR;
39 boolean mix = false;
40 SoftAudioBuffer bufferL;
41 SoftAudioBuffer bufferR;
42 SoftAudioBuffer bufferLout;
43 SoftAudioBuffer bufferRout;
44 float controlrate;
45
46 public void init(float samplerate, float controlrate) {
47 this.controlrate = controlrate;
48 }
49
50 public void setInput(int pin, SoftAudioBuffer input) {
51 if (pin == 0)
52 bufferL = input;
53 if (pin == 1)
54 bufferR = input;
55 }
56
57 public void setOutput(int pin, SoftAudioBuffer output) {
58 if (pin == 0)
59 bufferLout = output;
60 if (pin == 1)
61 bufferRout = output;
62 }
63
64 public void setMixMode(boolean mix) {
65 this.mix = mix;
66 }
67
68 public void globalParameterControlChange(int[] slothpath, long param,
69 long value) {
70 }
71
72 double silentcounter = 0;
73
74 public void processAudio() {
75 if (this.bufferL.isSilent()
76 && (this.bufferR == null || this.bufferR.isSilent())) {
77 silentcounter += 1 / controlrate;
78
79 if (silentcounter > 60) {
80 if (!mix) {
81 bufferLout.clear();
82 if (bufferRout != null) bufferRout.clear();
83 }
84 return;
85 }
86 } else
87 silentcounter = 0;
88
89 float[] bufferL = this.bufferL.array();
90 float[] bufferR = this.bufferR == null ? null : this.bufferR.array();
91 float[] bufferLout = this.bufferLout.array();
92 float[] bufferRout = this.bufferRout == null
93 ? null : this.bufferRout.array();
169 bufferLout[i] = tL * gain;
170 }
171 } else {
172 for (int i = 0; i < len; i++) {
173 gain += gaindelta;
174 float bL = bufferL[i];
175 float bR = bufferR[i];
176 float tL = temp_bufferL[i];
177 float tR = temp_bufferR[i];
178 temp_bufferL[i] = bL;
179 temp_bufferR[i] = bR;
180 bufferLout[i] = tL * gain;
181 bufferRout[i] = tR * gain;
182 }
183 }
184
185 }
186 gain = newgain;
187 }
188
189 public void processControlLogic() {
190 }
191 }
|
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.media.sound;
27
28 /**
29 * A simple look-ahead volume limiter with very fast attack and fast release.
30 * This filter is used for preventing clipping.
31 *
32 * @author Karl Helgason
33 */
34 public final class SoftLimiter implements SoftAudioProcessor {
35
36 float lastmax = 0;
37 float gain = 1;
38 float[] temp_bufferL;
39 float[] temp_bufferR;
40 boolean mix = false;
41 SoftAudioBuffer bufferL;
42 SoftAudioBuffer bufferR;
43 SoftAudioBuffer bufferLout;
44 SoftAudioBuffer bufferRout;
45 float controlrate;
46
47 @Override
48 public void init(float samplerate, float controlrate) {
49 this.controlrate = controlrate;
50 }
51
52 @Override
53 public void setInput(int pin, SoftAudioBuffer input) {
54 if (pin == 0)
55 bufferL = input;
56 if (pin == 1)
57 bufferR = input;
58 }
59
60 @Override
61 public void setOutput(int pin, SoftAudioBuffer output) {
62 if (pin == 0)
63 bufferLout = output;
64 if (pin == 1)
65 bufferRout = output;
66 }
67
68 @Override
69 public void setMixMode(boolean mix) {
70 this.mix = mix;
71 }
72
73 @Override
74 public void globalParameterControlChange(int[] slothpath, long param,
75 long value) {
76 }
77
78 double silentcounter = 0;
79
80 @Override
81 public void processAudio() {
82 if (this.bufferL.isSilent()
83 && (this.bufferR == null || this.bufferR.isSilent())) {
84 silentcounter += 1 / controlrate;
85
86 if (silentcounter > 60) {
87 if (!mix) {
88 bufferLout.clear();
89 if (bufferRout != null) bufferRout.clear();
90 }
91 return;
92 }
93 } else
94 silentcounter = 0;
95
96 float[] bufferL = this.bufferL.array();
97 float[] bufferR = this.bufferR == null ? null : this.bufferR.array();
98 float[] bufferLout = this.bufferLout.array();
99 float[] bufferRout = this.bufferRout == null
100 ? null : this.bufferRout.array();
176 bufferLout[i] = tL * gain;
177 }
178 } else {
179 for (int i = 0; i < len; i++) {
180 gain += gaindelta;
181 float bL = bufferL[i];
182 float bR = bufferR[i];
183 float tL = temp_bufferL[i];
184 float tR = temp_bufferR[i];
185 temp_bufferL[i] = bL;
186 temp_bufferR[i] = bR;
187 bufferLout[i] = tL * gain;
188 bufferRout[i] = tR * gain;
189 }
190 }
191
192 }
193 gain = newgain;
194 }
195
196 @Override
197 public void processControlLogic() {
198 }
199 }
|