J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1999-2007 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 com.sun.media.sound; |
| 27 | |
| 28 | import java.io.File; |
| 29 | import java.io.InputStream; |
| 30 | import java.io.OutputStream; |
| 31 | import java.io.IOException; |
| 32 | import java.io.DataInputStream; |
| 33 | import java.net.URL; |
| 34 | |
| 35 | import javax.sound.sampled.AudioFileFormat; |
| 36 | import javax.sound.sampled.AudioInputStream; |
| 37 | import javax.sound.sampled.UnsupportedAudioFileException; |
| 38 | import javax.sound.sampled.spi.AudioFileReader; |
| 39 | |
| 40 | |
| 41 | |
| 42 | /** |
| 43 | * Abstract File Reader class. |
| 44 | * |
| 45 | * @author Jan Borgersen |
| 46 | */ |
| 47 | abstract class SunFileReader extends AudioFileReader { |
| 48 | |
| 49 | // buffer size for temporary input streams |
| 50 | protected static final int bisBufferSize = 4096; |
| 51 | |
| 52 | /** |
| 53 | * Constructs a new SunFileReader object. |
| 54 | */ |
| 55 | public SunFileReader() { |
| 56 | } |
| 57 | |
| 58 | |
| 59 | // METHODS TO IMPLEMENT AudioFileReader |
| 60 | |
| 61 | /** |
| 62 | * Obtains the audio file format of the input stream provided. The stream must |
| 63 | * point to valid audio file data. In general, audio file providers may |
| 64 | * need to read some data from the stream before determining whether they |
| 65 | * support it. These parsers must |
| 66 | * be able to mark the stream, read enough data to determine whether they |
| 67 | * support the stream, and, if not, reset the stream's read pointer to its original |
| 68 | * position. If the input stream does not support this, this method may fail |
| 69 | * with an IOException. |
| 70 | * @param stream the input stream from which file format information should be |
| 71 | * extracted |
| 72 | * @return an <code>AudioFileFormat</code> object describing the audio file format |
| 73 | * @throws UnsupportedAudioFileException if the stream does not point to valid audio |
| 74 | * file data recognized by the system |
| 75 | * @throws IOException if an I/O exception occurs |
| 76 | * @see InputStream#markSupported |
| 77 | * @see InputStream#mark |
| 78 | */ |
| 79 | abstract public AudioFileFormat getAudioFileFormat(InputStream stream) throws UnsupportedAudioFileException, IOException; |
| 80 | |
| 81 | |
| 82 | /** |
| 83 | * Obtains the audio file format of the URL provided. The URL must |
| 84 | * point to valid audio file data. |
| 85 | * @param url the URL from which file format information should be |
| 86 | * extracted |
| 87 | * @return an <code>AudioFileFormat</code> object describing the audio file format |
| 88 | * @throws UnsupportedAudioFileException if the URL does not point to valid audio |
| 89 | * file data recognized by the system |
| 90 | * @throws IOException if an I/O exception occurs |
| 91 | */ |
| 92 | abstract public AudioFileFormat getAudioFileFormat(URL url) throws UnsupportedAudioFileException, IOException; |
| 93 | |
| 94 | |
| 95 | /** |
| 96 | * Obtains the audio file format of the File provided. The File must |
| 97 | * point to valid audio file data. |
| 98 | * @param file the File from which file format information should be |
| 99 | * extracted |
| 100 | * @return an <code>AudioFileFormat</code> object describing the audio file format |
| 101 | * @throws UnsupportedAudioFileException if the File does not point to valid audio |
| 102 | * file data recognized by the system |
| 103 | * @throws IOException if an I/O exception occurs |
| 104 | */ |
| 105 | abstract public AudioFileFormat getAudioFileFormat(File file) throws UnsupportedAudioFileException, IOException; |
| 106 | |
| 107 | |
| 108 | /** |
| 109 | * Obtains an audio stream from the input stream provided. The stream must |
| 110 | * point to valid audio file data. In general, audio file providers may |
| 111 | * need to read some data from the stream before determining whether they |
| 112 | * support it. These parsers must |
| 113 | * be able to mark the stream, read enough data to determine whether they |
| 114 | * support the stream, and, if not, reset the stream's read pointer to its original |
| 115 | * position. If the input stream does not support this, this method may fail |
| 116 | * with an IOException. |
| 117 | * @param stream the input stream from which the <code>AudioInputStream</code> should be |
| 118 | * constructed |
| 119 | * @return an <code>AudioInputStream</code> object based on the audio file data contained |
| 120 | * in the input stream. |
| 121 | * @throws UnsupportedAudioFileException if the stream does not point to valid audio |
| 122 | * file data recognized by the system |
| 123 | * @throws IOException if an I/O exception occurs |
| 124 | * @see InputStream#markSupported |
| 125 | * @see InputStream#mark |
| 126 | */ |
| 127 | abstract public AudioInputStream getAudioInputStream(InputStream stream) throws UnsupportedAudioFileException, IOException; |
| 128 | |
| 129 | |
| 130 | /** |
| 131 | * Obtains an audio stream from the URL provided. The URL must |
| 132 | * point to valid audio file data. |
| 133 | * @param url the URL for which the <code>AudioInputStream</code> should be |
| 134 | * constructed |
| 135 | * @return an <code>AudioInputStream</code> object based on the audio file data pointed |
| 136 | * to by the URL |
| 137 | * @throws UnsupportedAudioFileException if the URL does not point to valid audio |
| 138 | * file data recognized by the system |
| 139 | * @throws IOException if an I/O exception occurs |
| 140 | */ |
| 141 | abstract public AudioInputStream getAudioInputStream(URL url) throws UnsupportedAudioFileException, IOException; |
| 142 | |
| 143 | |
| 144 | /** |
| 145 | * Obtains an audio stream from the File provided. The File must |
| 146 | * point to valid audio file data. |
| 147 | * @param file the File for which the <code>AudioInputStream</code> should be |
| 148 | * constructed |
| 149 | * @return an <code>AudioInputStream</code> object based on the audio file data pointed |
| 150 | * to by the File |
| 151 | * @throws UnsupportedAudioFileException if the File does not point to valid audio |
| 152 | * file data recognized by the system |
| 153 | * @throws IOException if an I/O exception occurs |
| 154 | */ |
| 155 | abstract public AudioInputStream getAudioInputStream(File file) throws UnsupportedAudioFileException, IOException; |
| 156 | |
| 157 | |
| 158 | // HELPER METHODS |
| 159 | |
| 160 | |
| 161 | |
| 162 | /** |
| 163 | * rllong |
| 164 | * Protected helper method to read 64 bits and changing the order of |
| 165 | * each bytes. |
| 166 | * @param DataInputStream |
| 167 | * @return 32 bits swapped value. |
| 168 | * @exception IOException |
| 169 | */ |
| 170 | protected int rllong(DataInputStream dis) throws IOException { |
| 171 | |
| 172 | int b1, b2, b3, b4 ; |
| 173 | int i = 0; |
| 174 | |
| 175 | i = dis.readInt(); |
| 176 | |
| 177 | b1 = ( i & 0xFF ) << 24 ; |
| 178 | b2 = ( i & 0xFF00 ) << 8; |
| 179 | b3 = ( i & 0xFF0000 ) >> 8; |
| 180 | b4 = ( i & 0xFF000000 ) >>> 24; |
| 181 | |
| 182 | i = ( b1 | b2 | b3 | b4 ); |
| 183 | |
| 184 | return i; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * big2little |
| 189 | * Protected helper method to swap the order of bytes in a 32 bit int |
| 190 | * @param int |
| 191 | * @return 32 bits swapped value |
| 192 | */ |
| 193 | protected int big2little(int i) { |
| 194 | |
| 195 | int b1, b2, b3, b4 ; |
| 196 | |
| 197 | b1 = ( i & 0xFF ) << 24 ; |
| 198 | b2 = ( i & 0xFF00 ) << 8; |
| 199 | b3 = ( i & 0xFF0000 ) >> 8; |
| 200 | b4 = ( i & 0xFF000000 ) >>> 24; |
| 201 | |
| 202 | i = ( b1 | b2 | b3 | b4 ); |
| 203 | |
| 204 | return i; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * rlshort |
| 209 | * Protected helper method to read 16 bits value. Swap high with low byte. |
| 210 | * @param DataInputStream |
| 211 | * @return the swapped value. |
| 212 | * @exception IOException |
| 213 | */ |
| 214 | protected short rlshort(DataInputStream dis) throws IOException { |
| 215 | |
| 216 | short s=0; |
| 217 | short high, low; |
| 218 | |
| 219 | s = dis.readShort(); |
| 220 | |
| 221 | high = (short)(( s & 0xFF ) << 8) ; |
| 222 | low = (short)(( s & 0xFF00 ) >>> 8); |
| 223 | |
| 224 | s = (short)( high | low ); |
| 225 | |
| 226 | return s; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * big2little |
| 231 | * Protected helper method to swap the order of bytes in a 16 bit short |
| 232 | * @param int |
| 233 | * @return 16 bits swapped value |
| 234 | */ |
| 235 | protected short big2littleShort(short i) { |
| 236 | |
| 237 | short high, low; |
| 238 | |
| 239 | high = (short)(( i & 0xFF ) << 8) ; |
| 240 | low = (short)(( i & 0xFF00 ) >>> 8); |
| 241 | |
| 242 | i = (short)( high | low ); |
| 243 | |
| 244 | return i; |
| 245 | } |
| 246 | |
| 247 | |
| 248 | /** Calculates the frame size for PCM frames. |
| 249 | * Note that this method is appropriate for non-packed samples. |
| 250 | * For instance, 12 bit, 2 channels will return 4 bytes, not 3. |
| 251 | * @param sampleSizeInBits the size of a single sample in bits |
| 252 | * @param channels the number of channels |
| 253 | * @return the size of a PCM frame in bytes. |
| 254 | */ |
| 255 | protected static int calculatePCMFrameSize(int sampleSizeInBits, |
| 256 | int channels) { |
| 257 | return ((sampleSizeInBits + 7) / 8) * channels; |
| 258 | } |
| 259 | } |