Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/audio/AudioData.java
38829 views
/*1* Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.audio;2627import java.io.*;28import java.util.Arrays;2930import javax.sound.sampled.*;313233/**34* A clip of audio data. This data can be used to construct an35* AudioDataStream, which can be played. <p>36*37* @author Arthur van Hoff38* @author Kara Kytle39* @see AudioDataStream40* @see AudioPlayer41*/4243/*44* the idea here is that the AudioData object encapsulates the45* data you need to play an audio clip based on a defined set46* of data. to do this, you require the audio data (a byte47* array rather than an arbitrary input stream) and a format48* object.49*/505152public final class AudioData {5354private static final AudioFormat DEFAULT_FORMAT =55new AudioFormat(AudioFormat.Encoding.ULAW,568000, // sample rate578, // sample size in bits581, // channels591, // frame size in bytes608000, // frame rate61true ); // bigendian (irrelevant for 8-bit data)6263AudioFormat format; // carry forth the format array amusement64byte buffer[];6566/**67* Constructor68*/69public AudioData(final byte[] buffer) {70// if we cannot extract valid format information, we resort to assuming71// the data will be 8k mono u-law in order to provide maximal backwards72// compatibility....73this(DEFAULT_FORMAT, buffer);7475// okay, we need to extract the format and the byte buffer of data76try {77AudioInputStream ais = AudioSystem.getAudioInputStream(new ByteArrayInputStream(buffer));78this.format = ais.getFormat();79ais.close();80// $$fb 2002-10-27: buffer contains the file header now!81} catch (IOException e) {82// use default format83} catch (UnsupportedAudioFileException e1 ) {84// use default format85}86}878889/**90* Non-public constructor; this is the one we use in ADS and CADS91* constructors.92*/93AudioData(final AudioFormat format, final byte[] buffer) {94this.format = format;95if (buffer != null) {96this.buffer = Arrays.copyOf(buffer, buffer.length);97}98}99}100101102