add source code that does not have location annotations.
[IRC.git] / Robust / src / Benchmarks / SSJava / MP3DecoderInfer / Equalizer.java
diff --git a/Robust/src/Benchmarks/SSJava/MP3DecoderInfer/Equalizer.java b/Robust/src/Benchmarks/SSJava/MP3DecoderInfer/Equalizer.java
new file mode 100644 (file)
index 0000000..1fe7fb9
--- /dev/null
@@ -0,0 +1,200 @@
+/*\r
+ * 11/19/04            1.0 moved to LGPL.\r
+ * 12/12/99            Initial version.        mdm@techie.com\r
+ *-----------------------------------------------------------------------\r
+ *   This program is free software; you can redistribute it and/or modify\r
+ *   it under the terms of the GNU Library General Public License as published\r
+ *   by the Free Software Foundation; either version 2 of the License, or\r
+ *   (at your option) any later version.\r
+ *\r
+ *   This program is distributed in the hope that it will be useful,\r
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ *   GNU Library General Public License for more details.\r
+ *\r
+ *   You should have received a copy of the GNU Library General Public\r
+ *   License along with this program; if not, write to the Free Software\r
+ *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\r
+ *----------------------------------------------------------------------\r
+ */\r
+\r
+/**\r
+ * The <code>Equalizer</code> class can be used to specify equalization settings\r
+ * for the MPEG audio decoder.\r
+ * <p>\r
+ * The equalizer consists of 32 band-pass filters. Each band of the equalizer\r
+ * can take on a fractional value between -1.0 and +1.0. At -1.0, the input\r
+ * signal is attenuated by 6dB, at +1.0 the signal is amplified by 6dB.\r
+ * \r
+ * @see Decoder\r
+ * \r
+ * @author MDM\r
+ */\r
+\r
+\r
+public final class Equalizer {\r
+  /**\r
+   * Equalizer setting to denote that a given band will not be present in the\r
+   * output signal.\r
+   */\r
+  \r
+  static public final float BAND_NOT_PRESENT = Float.NEGATIVE_INFINITY;\r
+\r
+  \r
+  static public final Equalizer PASS_THRU_EQ = new Equalizer();\r
+\r
+  \r
+  private static final int BANDS = 32;\r
+\r
+  \r
+  private final float[] settings = new float[BANDS];\r
+\r
+  /**\r
+   * Creates a new <code>Equalizer</code> instance.\r
+   */\r
+  public Equalizer() {\r
+  }\r
+\r
+  // private Equalizer(float b1, float b2, float b3, float b4, float b5,\r
+  // float b6, float b7, float b8, float b9, float b10, float b11,\r
+  // float b12, float b13, float b14, float b15, float b16,\r
+  // float b17, float b18, float b19, float b20);\r
+\r
+  public Equalizer(float[] settings) {\r
+    setFrom(settings);\r
+  }\r
+\r
+  public Equalizer(EQFunction eq) {\r
+    setFrom(eq);\r
+  }\r
+\r
+  public void setFrom(float[] eq) {\r
+    reset();\r
+    int max = (eq.length > BANDS) ? BANDS : eq.length;\r
+\r
+    for (int i = 0; i < max; i++) {\r
+      settings[i] = limit(eq[i]);\r
+    }\r
+  }\r
+\r
+  public void setFrom(EQFunction eq) {\r
+    reset();\r
+    int max = BANDS;\r
+\r
+    for (int i = 0; i < max; i++) {\r
+      settings[i] = limit(eq.getBand(i));\r
+    }\r
+  }\r
+\r
+  /**\r
+   * Sets the bands of this equalizer to the value the bands of another\r
+   * equalizer. Bands that are not present in both equalizers are ignored.\r
+   */\r
+  public void setFrom(Equalizer eq) {\r
+    if (eq != this) {\r
+      setFrom(eq.settings);\r
+    }\r
+  }\r
+\r
+  /**\r
+   * Sets all bands to 0.0\r
+   */\r
+  public void reset() {\r
+    for (int i = 0; i < BANDS; i++) {\r
+      settings[i] = 0.0f;\r
+    }\r
+  }\r
+\r
+  /**\r
+   * Retrieves the number of bands present in this equalizer.\r
+   */\r
+  public int getBandCount() {\r
+    return settings.length;\r
+  }\r
+\r
+  public float setBand(int band, float neweq) {\r
+    float eq = 0.0f;\r
+\r
+    if ((band >= 0) && (band < BANDS)) {\r
+      eq = settings[band];\r
+      settings[band] = limit(neweq);\r
+    }\r
+\r
+    return eq;\r
+  }\r
+\r
+  /**\r
+   * Retrieves the eq setting for a given band.\r
+   */\r
+  public float getBand(int band) {\r
+    float eq = 0.0f;\r
+\r
+    if ((band >= 0) && (band < BANDS)) {\r
+      eq = settings[band];\r
+    }\r
+\r
+    return eq;\r
+  }\r
+\r
+  private float limit(float eq) {\r
+    if (eq == BAND_NOT_PRESENT)\r
+      return eq;\r
+    if (eq > 1.0f)\r
+      return 1.0f;\r
+    if (eq < -1.0f)\r
+      return -1.0f;\r
+\r
+    return eq;\r
+  }\r
+\r
+  /**\r
+   * Retrieves an array of floats whose values represent a scaling factor that\r
+   * can be applied to linear samples in each band to provide the equalization\r
+   * represented by this instance.\r
+   * \r
+   * @return an array of factors that can be applied to the subbands.\r
+   */\r
+  \r
+  \r
+  float[] getBandFactors() {\r
+     float[] factors = new float[BANDS];\r
+     int maxCount = BANDS;\r
+    for ( int i = 0; i < maxCount; i++) {\r
+      factors[i] = getBandFactor(settings[i]);\r
+    }\r
+\r
+    return factors;\r
+  }\r
+\r
+  /**\r
+   * Converts an equalizer band setting to a sample factor. The factor is\r
+   * determined by the function f = 2^n where n is the equalizer band setting in\r
+   * the range [-1.0,1.0].\r
+   * \r
+   */\r
+  \r
+  float getBandFactor( float eq) {\r
+    if (eq == BAND_NOT_PRESENT)\r
+      return 0.0f;\r
+\r
+     float f = (float) Math.pow(2.0, eq);\r
+    return f;\r
+  }\r
+\r
+  static abstract public class EQFunction {\r
+    /**\r
+     * Returns the setting of a band in the equalizer.\r
+     * \r
+     * @param band\r
+     *          The index of the band to retrieve the setting for.\r
+     * \r
+     * @return the setting of the specified band. This is a value between -1 and\r
+     *         +1.\r
+     */\r
+    public float getBand(int band) {\r
+      return 0.0f;\r
+    }\r
+\r
+  }\r
+\r
+}\r