Save This Page
Home » openjdk-7 » com.sun.media » sound » [javadoc | source]
    1   /*
    2    * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
    3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    4    *
    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 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();
   94   
   95           if (temp_bufferL == null || temp_bufferL.length < bufferL.length)
   96               temp_bufferL = new float[bufferL.length];
   97           if (bufferR != null)
   98               if (temp_bufferR == null || temp_bufferR.length < bufferR.length)
   99                   temp_bufferR = new float[bufferR.length];
  100   
  101           float max = 0;
  102           int len = bufferL.length;
  103   
  104           if (bufferR == null) {
  105               for (int i = 0; i < len; i++) {
  106                   if (bufferL[i] > max)
  107                       max = bufferL[i];
  108                   if (-bufferL[i] > max)
  109                       max = -bufferL[i];
  110               }
  111           } else {
  112               for (int i = 0; i < len; i++) {
  113                   if (bufferL[i] > max)
  114                       max = bufferL[i];
  115                   if (bufferR[i] > max)
  116                       max = bufferR[i];
  117                   if (-bufferL[i] > max)
  118                       max = -bufferL[i];
  119                   if (-bufferR[i] > max)
  120                       max = -bufferR[i];
  121               }
  122           }
  123   
  124           float lmax = lastmax;
  125           lastmax = max;
  126           if (lmax > max)
  127               max = lmax;
  128   
  129           float newgain = 1;
  130           if (max > 0.99f)
  131               newgain = 0.99f / max;
  132           else
  133               newgain = 1;
  134   
  135           if (newgain > gain)
  136               newgain = (newgain + gain * 9) / 10f;
  137   
  138           float gaindelta = (newgain - gain) / len;
  139           if (mix) {
  140               if (bufferR == null) {
  141                   for (int i = 0; i < len; i++) {
  142                       gain += gaindelta;
  143                       float bL = bufferL[i];
  144                       float tL = temp_bufferL[i];
  145                       temp_bufferL[i] = bL;
  146                       bufferLout[i] += tL * gain;
  147                   }
  148               } else {
  149                   for (int i = 0; i < len; i++) {
  150                       gain += gaindelta;
  151                       float bL = bufferL[i];
  152                       float bR = bufferR[i];
  153                       float tL = temp_bufferL[i];
  154                       float tR = temp_bufferR[i];
  155                       temp_bufferL[i] = bL;
  156                       temp_bufferR[i] = bR;
  157                       bufferLout[i] += tL * gain;
  158                       bufferRout[i] += tR * gain;
  159                   }
  160               }
  161   
  162           } else {
  163               if (bufferR == null) {
  164                   for (int i = 0; i < len; i++) {
  165                       gain += gaindelta;
  166                       float bL = bufferL[i];
  167                       float tL = temp_bufferL[i];
  168                       temp_bufferL[i] = bL;
  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   }

Save This Page
Home » openjdk-7 » com.sun.media » sound » [javadoc | source]