jdk/src/share/classes/sun/audio/AudioData.java
changeset 25838 02b8ba539ccb
parent 25837 38b0d0abca49
parent 25831 3fce8370cad4
child 25842 f322f27b47d2
equal deleted inserted replaced
25837:38b0d0abca49 25838:02b8ba539ccb
     1 /*
       
     2  * Copyright (c) 1999, 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 
       
    26 package sun.audio;
       
    27 
       
    28 import java.io.*;
       
    29 import java.util.Arrays;
       
    30 
       
    31 import javax.sound.sampled.*;
       
    32 
       
    33 
       
    34 /**
       
    35  * A clip of audio data. This data can be used to construct an
       
    36  * AudioDataStream, which can be played. <p>
       
    37  *
       
    38  * @author  Arthur van Hoff
       
    39  * @author  Kara Kytle
       
    40  * @see     AudioDataStream
       
    41  * @see     AudioPlayer
       
    42  */
       
    43 
       
    44  /*
       
    45   * the idea here is that the AudioData object encapsulates the
       
    46   * data you need to play an audio clip based on a defined set
       
    47   * of data.  to do this, you require the audio data (a byte
       
    48   * array rather than an arbitrary input stream) and a format
       
    49   * object.
       
    50   */
       
    51 
       
    52 
       
    53 public final class AudioData {
       
    54 
       
    55     private static final AudioFormat DEFAULT_FORMAT =
       
    56         new AudioFormat(AudioFormat.Encoding.ULAW,
       
    57                         8000,   // sample rate
       
    58                         8,      // sample size in bits
       
    59                         1,      // channels
       
    60                         1,      // frame size in bytes
       
    61                         8000,   // frame rate
       
    62                         true ); // bigendian (irrelevant for 8-bit data)
       
    63 
       
    64     AudioFormat format;   // carry forth the format array amusement
       
    65     byte buffer[];
       
    66 
       
    67     /**
       
    68      * Constructor
       
    69      */
       
    70     public AudioData(final byte[] buffer) {
       
    71         // if we cannot extract valid format information, we resort to assuming
       
    72         // the data will be 8k mono u-law in order to provide maximal backwards
       
    73         // compatibility....
       
    74         this(DEFAULT_FORMAT, buffer);
       
    75 
       
    76         // okay, we need to extract the format and the byte buffer of data
       
    77         try {
       
    78             AudioInputStream ais = AudioSystem.getAudioInputStream(new ByteArrayInputStream(buffer));
       
    79             this.format = ais.getFormat();
       
    80             ais.close();
       
    81             // $$fb 2002-10-27: buffer contains the file header now!
       
    82         } catch (IOException e) {
       
    83             // use default format
       
    84         } catch (UnsupportedAudioFileException e1 ) {
       
    85             // use default format
       
    86         }
       
    87     }
       
    88 
       
    89 
       
    90     /**
       
    91      * Non-public constructor; this is the one we use in ADS and CADS
       
    92      * constructors.
       
    93      */
       
    94     AudioData(final AudioFormat format, final byte[] buffer) {
       
    95         this.format = format;
       
    96         if (buffer != null) {
       
    97             this.buffer = Arrays.copyOf(buffer, buffer.length);
       
    98         }
       
    99     }
       
   100 }