1 /*
   2  * Copyright (c) 2007, 2013, 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 standard transformer used in connection blocks.
  29  * It expects input values to be between 0 and 1.
  30  *
  31  * The result of the transform is
  32  *   between 0 and 1 if polarity = unipolar and
  33  *   between -1 and 1 if polarity = bipolar.
  34  *
  35  * These constraints only applies to Concave, Convex and Switch transforms.
  36  *
  37  * @author Karl Helgason
  38  */
  39 public final class ModelStandardTransform implements ModelTransform {
  40 
  41     public static final boolean DIRECTION_MIN2MAX = false;
  42     public static final boolean DIRECTION_MAX2MIN = true;
  43     public static final boolean POLARITY_UNIPOLAR = false;
  44     public static final boolean POLARITY_BIPOLAR = true;
  45     public static final int TRANSFORM_LINEAR = 0;
  46     // concave: output = (20*log10(127^2/value^2)) / 96
  47     public static final int TRANSFORM_CONCAVE = 1;
  48     // convex: same as concave except that start and end point are reversed.
  49     public static final int TRANSFORM_CONVEX = 2;
  50     // switch: if value > avg(max,min) then max else min
  51     public static final int TRANSFORM_SWITCH = 3;
  52     public static final int TRANSFORM_ABSOLUTE = 4;
  53     private boolean direction = DIRECTION_MIN2MAX;
  54     private boolean polarity = POLARITY_UNIPOLAR;
  55     private int transform = TRANSFORM_LINEAR;
  56 
  57     public ModelStandardTransform() {
  58     }
  59 
  60     public ModelStandardTransform(boolean direction) {
  61         this.direction = direction;
  62     }
  63 
  64     public ModelStandardTransform(boolean direction, boolean polarity) {
  65         this.direction = direction;
  66         this.polarity = polarity;
  67     }
  68 
  69     public ModelStandardTransform(boolean direction, boolean polarity,
  70             int transform) {
  71         this.direction = direction;
  72         this.polarity = polarity;
  73         this.transform = transform;
  74     }
  75 
  76     public double transform(double value) {
  77         double s;
  78         double a;
  79         if (direction == DIRECTION_MAX2MIN)
  80             value = 1.0 - value;
  81         if (polarity == POLARITY_BIPOLAR)
  82             value = value * 2.0 - 1.0;
  83         switch (transform) {
  84             case TRANSFORM_CONCAVE:
  85                 s = Math.signum(value);
  86                 a = Math.abs(value);
  87                 a = -((5.0 / 12.0) / Math.log(10)) * Math.log(1.0 - a);
  88                 if (a < 0)
  89                     a = 0;
  90                 else if (a > 1)
  91                     a = 1;
  92                 return s * a;
  93             case TRANSFORM_CONVEX:
  94                 s = Math.signum(value);
  95                 a = Math.abs(value);
  96                 a = 1.0 + ((5.0 / 12.0) / Math.log(10)) * Math.log(a);
  97                 if (a < 0)
  98                     a = 0;
  99                 else if (a > 1)
 100                     a = 1;
 101                 return s * a;
 102             case TRANSFORM_SWITCH:
 103                 if (polarity == POLARITY_BIPOLAR)
 104                     return (value > 0) ? 1 : -1;
 105                 else
 106                     return (value > 0.5) ? 1 : 0;
 107             case TRANSFORM_ABSOLUTE:
 108                 return Math.abs(value);
 109             default:
 110                 break;
 111         }
 112 
 113         return value;
 114     }
 115 
 116     public boolean getDirection() {
 117         return direction;
 118     }
 119 
 120     public void setDirection(boolean direction) {
 121         this.direction = direction;
 122     }
 123 
 124     public boolean getPolarity() {
 125         return polarity;
 126     }
 127 
 128     public void setPolarity(boolean polarity) {
 129         this.polarity = polarity;
 130     }
 131 
 132     public int getTransform() {
 133         return transform;
 134     }
 135 
 136     public void setTransform(int transform) {
 137         this.transform = transform;
 138     }
 139 }