J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1999-2002 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package sun.audio; |
| 27 | |
| 28 | import java.io.*; |
| 29 | import javax.sound.sampled.*; |
| 30 | |
| 31 | |
| 32 | /** |
| 33 | * A clip of audio data. This data can be used to construct an |
| 34 | * AudioDataStream, which can be played. <p> |
| 35 | * |
| 36 | * @author Arthur van Hoff |
| 37 | * @author Kara Kytle |
| 38 | * @see AudioDataStream |
| 39 | * @see AudioPlayer |
| 40 | */ |
| 41 | |
| 42 | /* |
| 43 | * the idea here is that the AudioData object encapsulates the |
| 44 | * data you need to play an audio clip based on a defined set |
| 45 | * of data. to do this, you require the audio data (a byte |
| 46 | * array rather than an arbitrary input stream) and a format |
| 47 | * object. |
| 48 | */ |
| 49 | |
| 50 | |
| 51 | public class AudioData { |
| 52 | |
| 53 | private static final AudioFormat DEFAULT_FORMAT = |
| 54 | new AudioFormat(AudioFormat.Encoding.ULAW, |
| 55 | 8000, // sample rate |
| 56 | 8, // sample size in bits |
| 57 | 1, // channels |
| 58 | 1, // frame size in bytes |
| 59 | 8000, // frame rate |
| 60 | true ); // bigendian (irrelevant for 8-bit data) |
| 61 | |
| 62 | AudioFormat format; // carry forth the format array amusement |
| 63 | byte buffer[]; |
| 64 | |
| 65 | /** |
| 66 | * Constructor |
| 67 | */ |
| 68 | public AudioData(byte buffer[]) { |
| 69 | |
| 70 | this.buffer = buffer; |
| 71 | // if we cannot extract valid format information, we resort to assuming the data will be 8k mono u-law |
| 72 | // in order to provide maximal backwards compatibility.... |
| 73 | this.format = DEFAULT_FORMAT; |
| 74 | |
| 75 | // okay, we need to extract the format and the byte buffer of data |
| 76 | try { |
| 77 | AudioInputStream ais = AudioSystem.getAudioInputStream(new ByteArrayInputStream(buffer)); |
| 78 | this.format = ais.getFormat(); |
| 79 | ais.close(); |
| 80 | // $$fb 2002-10-27: buffer contains the file header now! |
| 81 | } catch (IOException e) { |
| 82 | // use default format |
| 83 | } catch (UnsupportedAudioFileException e1 ) { |
| 84 | // use default format |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | |
| 89 | /** |
| 90 | * Non-public constructor; this is the one we use in ADS and CADS |
| 91 | * constructors. |
| 92 | */ |
| 93 | AudioData(AudioFormat format, byte[] buffer) { |
| 94 | |
| 95 | this.format = format; |
| 96 | this.buffer = buffer; |
| 97 | } |
| 98 | } |