add source code that does not have location annotations.
[IRC.git] / Robust / src / Benchmarks / SSJava / MP3DecoderInfer / Decoder.java
diff --git a/Robust/src/Benchmarks/SSJava/MP3DecoderInfer/Decoder.java b/Robust/src/Benchmarks/SSJava/MP3DecoderInfer/Decoder.java
new file mode 100644 (file)
index 0000000..d7754bb
--- /dev/null
@@ -0,0 +1,300 @@
+/*\r
+ * 11/19/04            1.0 moved to LGPL.\r
+ * 01/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>Decoder</code> class encapsulates the details of decoding an MPEG\r
+ * audio frame.\r
+ * \r
+ * @author MDM\r
+ * @version 0.0.7 12/12/99\r
+ * @since 0.0.5\r
+ */\r
+\r
+\r
+public class Decoder implements DecoderErrors {\r
+\r
+  static private final Params DEFAULT_PARAMS = new Params();\r
+\r
+  /**\r
+   * The Bistream from which the MPEG audio frames are read.\r
+   */\r
+  // \r
+  // private Bitstream stream;\r
+\r
+  /**\r
+   * The Obuffer instance that will receive the decoded PCM samples.\r
+   */\r
+  // \r
+  // private Obuffer output;\r
+\r
+  /**\r
+   * Synthesis filter for the left channel.\r
+   */\r
+  // \r
+  // private SynthesisFilter filter1;\r
+\r
+  /**\r
+   * Sythesis filter for the right channel.\r
+   */\r
+  // \r
+  // private SynthesisFilter filter2;\r
+\r
+  /**\r
+   * The decoder used to decode layer III frames.\r
+   */\r
+  \r
+  private LayerIIIDecoder l3decoder;\r
+  // \r
+  // private LayerIIDecoder l2decoder;\r
+  // \r
+  // private LayerIDecoder l1decoder;\r
+\r
+  \r
+  private int outputFrequency;\r
+  \r
+  private int outputChannels;\r
+\r
+  \r
+  private Equalizer equalizer = new Equalizer();\r
+\r
+  \r
+  private Params params;\r
+\r
+  \r
+  private boolean initialized;\r
+\r
+  /**\r
+   * Creates a new <code>Decoder</code> instance with default parameters.\r
+   */\r
+\r
+  public Decoder() {\r
+    this(null);\r
+  }\r
+\r
+  /**\r
+   * Creates a new <code>Decoder</code> instance with default parameters.\r
+   * \r
+   * @param params\r
+   *          The <code>Params</code> instance that describes the customizable\r
+   *          aspects of the decoder.\r
+   */\r
+  public Decoder(@DELEGATE Params params0) {\r
+\r
+    if (params0 == null) {\r
+      params0 = getDefaultParams();\r
+    }\r
+\r
+    params = params0;\r
+\r
+    Equalizer eq = params.getInitialEqualizerSettings();\r
+    if (eq != null) {\r
+      equalizer.setFrom(eq);\r
+    }\r
+  }\r
+\r
+  static public Params getDefaultParams() {\r
+    return (Params) DEFAULT_PARAMS.clone();\r
+  }\r
+\r
+  // public void setEqualizer(Equalizer eq) {\r
+  // if (eq == null)\r
+  // eq = Equalizer.PASS_THRU_EQ;\r
+  //\r
+  // equalizer.setFrom(eq);\r
+  //\r
+  // float[] factors = equalizer.getBandFactors();\r
+  //\r
+  // if (filter1 != null)\r
+  // filter1.setEQ(factors);\r
+  //\r
+  // if (filter2 != null)\r
+  // filter2.setEQ(factors);\r
+  // }\r
+  \r
+  public void init(  Header header) {\r
+     float scalefactor = 32700.0f;\r
+\r
+     int mode = header.mode();\r
+     int layer = header.layer();\r
+     int channels = mode == Header.SINGLE_CHANNEL ? 1 : 2;\r
+\r
+    // set up output buffer if not set up by client.\r
+    // if (output == null)\r
+    // output = new SampleBuffer(header.frequency(), channels);\r
+    SampleBufferWrapper.init(header.frequency(), channels);\r
+\r
+     float[] factors = equalizer.getBandFactors();\r
+     SynthesisFilter filter1 =\r
+        new SynthesisFilter(0, scalefactor, factors);\r
+\r
+    // REVIEW: allow mono output for stereo\r
+     SynthesisFilter filter2 = null;\r
+    if (channels == 2) {\r
+      filter2 = new SynthesisFilter(1, scalefactor, factors);\r
+    }\r
+\r
+    outputChannels = channels;\r
+    outputFrequency = header.frequency();\r
+\r
+    l3decoder = new LayerIIIDecoder(header,filter1, filter2, OutputChannels.BOTH_CHANNELS);\r
+\r
+  }\r
+\r
+  /**\r
+   * Decodes one frame from an MPEG audio bitstream.\r
+   * \r
+   * @param header\r
+   *          The header describing the frame to decode.\r
+   * @param bitstream\r
+   *          The bistream that provides the bits for te body of the frame.\r
+   * \r
+   * @return A SampleBuffer containing the decoded samples.\r
+   */\r
+  \r
+  public void decodeFrame( Header header) throws DecoderException {\r
+\r
+    SampleBufferWrapper.clear_buffer();\r
+    l3decoder.decode(header);\r
+    // SampleBufferWrapper.getOutput().write_buffer(1);\r
+\r
+  }\r
+\r
+  /**\r
+   * Changes the output buffer. This will take effect the next time\r
+   * decodeFrame() is called.\r
+   */\r
+  // public void setOutputBuffer(Obuffer out) {\r
+  // output = out;\r
+  // }\r
+\r
+  /**\r
+   * Retrieves the sample frequency of the PCM samples output by this decoder.\r
+   * This typically corresponds to the sample rate encoded in the MPEG audio\r
+   * stream.\r
+   * \r
+   * @param the\r
+   *          sample rate (in Hz) of the samples written to the output buffer\r
+   *          when decoding.\r
+   */\r
+  public int getOutputFrequency() {\r
+    return outputFrequency;\r
+  }\r
+\r
+  /**\r
+   * Retrieves the number of channels of PCM samples output by this decoder.\r
+   * This usually corresponds to the number of channels in the MPEG audio\r
+   * stream, although it may differ.\r
+   * \r
+   * @return The number of output channels in the decoded samples: 1 for mono,\r
+   *         or 2 for stereo.\r
+   * \r
+   */\r
+  public int getOutputChannels() {\r
+    return outputChannels;\r
+  }\r
+\r
+  /**\r
+   * Retrieves the maximum number of samples that will be written to the output\r
+   * buffer when one frame is decoded. This can be used to help calculate the\r
+   * size of other buffers whose size is based upon the number of samples\r
+   * written to the output buffer. NB: this is an upper bound and fewer samples\r
+   * may actually be written, depending upon the sample rate and number of\r
+   * channels.\r
+   * \r
+   * @return The maximum number of samples that are written to the output buffer\r
+   *         when decoding a single frame of MPEG audio.\r
+   */\r
+  public int getOutputBlockSize() {\r
+    return Obuffer.OBUFFERSIZE;\r
+  }\r
+\r
+  protected DecoderException newDecoderException(int errorcode) {\r
+    return new DecoderException(errorcode, null);\r
+  }\r
+\r
+  protected DecoderException newDecoderException(int errorcode, Throwable throwable) {\r
+    return new DecoderException(errorcode, throwable);\r
+  }\r
+}\r
+\r
+\r
+\r
+\r
+\r
+  /**\r
+   * The <code>Params</code> class presents the customizable aspects of the\r
+   * decoder.\r
+   * <p>\r
+   * Instances of this class are not thread safe.\r
+   */\r
+  public class Params implements Cloneable {\r
+\r
+    // private OutputChannels outputChannels = OutputChannels.BOTH;\r
+    private OutputChannels outputChannels = new OutputChannels(0);\r
+\r
+    private Equalizer equalizer = new Equalizer();\r
+\r
+    public Params() {\r
+    }\r
+\r
+    public Object clone() {\r
+      // TODO: need to have better clone method\r
+      Params clone = new Params();\r
+      clone.outputChannels = new OutputChannels(outputChannels.getChannelsOutputCode());\r
+      clone.equalizer = new Equalizer();\r
+      return clone;\r
+      // try\r
+      // {\r
+      // return super.clone();\r
+      // }\r
+      // catch (CloneNotSupportedException ex)\r
+      // {\r
+      // throw new InternalError(this+": "+ex);\r
+      // }\r
+    }\r
+\r
+    public void setOutputChannels(OutputChannels out) {\r
+      if (out == null)\r
+        throw new NullPointerException("out");\r
+\r
+      outputChannels = out;\r
+    }\r
+\r
+    public OutputChannels getOutputChannels() {\r
+      return outputChannels;\r
+    }\r
+\r
+    /**\r
+     * Retrieves the equalizer settings that the decoder's equalizer will be\r
+     * initialized from.\r
+     * <p>\r
+     * The <code>Equalizer</code> instance returned cannot be changed in real\r
+     * time to affect the decoder output as it is used only to initialize the\r
+     * decoders EQ settings. To affect the decoder's output in realtime, use the\r
+     * Equalizer returned from the getEqualizer() method on the decoder.\r
+     * \r
+     * @return The <code>Equalizer</code> used to initialize the EQ settings of\r
+     *         the decoder.\r
+     */\r
+    public Equalizer getInitialEqualizerSettings() {\r
+      return equalizer;\r
+    }\r
+\r
+  }
\ No newline at end of file