jdk/src/share/classes/com/sun/media/sound/SunFileReader.java
author amenkov
Mon, 28 Feb 2011 18:36:33 +0300
changeset 8525 08f98f5a11df
parent 5506 202f599c92aa
child 18215 b2afd66ce6db
permissions -rw-r--r--
7013521: AudioSystem.write for AIFF files closes source audio stream Reviewed-by: dav
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.media.sound;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.DataInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.sound.sampled.AudioFileFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.sound.sampled.AudioInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.sound.sampled.UnsupportedAudioFileException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.sound.sampled.spi.AudioFileReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Abstract File Reader class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author Jan Borgersen
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
abstract class SunFileReader extends AudioFileReader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    // buffer size for temporary input streams
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    protected static final int bisBufferSize = 4096;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * Constructs a new SunFileReader object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public SunFileReader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // METHODS TO IMPLEMENT AudioFileReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Obtains the audio file format of the input stream provided.  The stream must
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * point to valid audio file data.  In general, audio file providers may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * need to read some data from the stream before determining whether they
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * support it.  These parsers must
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * be able to mark the stream, read enough data to determine whether they
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * support the stream, and, if not, reset the stream's read pointer to its original
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * position.  If the input stream does not support this, this method may fail
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * with an IOException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @param stream the input stream from which file format information should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * extracted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @return an <code>AudioFileFormat</code> object describing the audio file format
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @throws UnsupportedAudioFileException if the stream does not point to valid audio
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * file data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @throws IOException if an I/O exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @see InputStream#markSupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @see InputStream#mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    abstract public AudioFileFormat getAudioFileFormat(InputStream stream) throws UnsupportedAudioFileException, IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Obtains the audio file format of the URL provided.  The URL must
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * point to valid audio file data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param url the URL from which file format information should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * extracted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @return an <code>AudioFileFormat</code> object describing the audio file format
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @throws UnsupportedAudioFileException if the URL does not point to valid audio
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * file data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @throws IOException if an I/O exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    abstract public AudioFileFormat getAudioFileFormat(URL url) throws UnsupportedAudioFileException, IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Obtains the audio file format of the File provided.  The File must
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * point to valid audio file data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param file the File from which file format information should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * extracted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @return an <code>AudioFileFormat</code> object describing the audio file format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @throws UnsupportedAudioFileException if the File does not point to valid audio
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * file data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @throws IOException if an I/O exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    abstract public AudioFileFormat getAudioFileFormat(File file) throws UnsupportedAudioFileException, IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Obtains an audio stream from the input stream provided.  The stream must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * point to valid audio file data.  In general, audio file providers may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * need to read some data from the stream before determining whether they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * support it.  These parsers must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * be able to mark the stream, read enough data to determine whether they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * support the stream, and, if not, reset the stream's read pointer to its original
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * position.  If the input stream does not support this, this method may fail
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * with an IOException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param stream the input stream from which the <code>AudioInputStream</code> should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @return an <code>AudioInputStream</code> object based on the audio file data contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * in the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @throws UnsupportedAudioFileException if the stream does not point to valid audio
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * file data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @throws IOException if an I/O exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @see InputStream#markSupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @see InputStream#mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    abstract public AudioInputStream getAudioInputStream(InputStream stream) throws UnsupportedAudioFileException, IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Obtains an audio stream from the URL provided.  The URL must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * point to valid audio file data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param url the URL for which the <code>AudioInputStream</code> should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @return an <code>AudioInputStream</code> object based on the audio file data pointed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * to by the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @throws UnsupportedAudioFileException if the URL does not point to valid audio
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * file data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @throws IOException if an I/O exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    abstract public AudioInputStream getAudioInputStream(URL url) throws UnsupportedAudioFileException, IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Obtains an audio stream from the File provided.  The File must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * point to valid audio file data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param file the File for which the <code>AudioInputStream</code> should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @return an <code>AudioInputStream</code> object based on the audio file data pointed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * to by the File
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @throws UnsupportedAudioFileException if the File does not point to valid audio
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * file data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @throws IOException if an I/O exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    abstract public AudioInputStream getAudioInputStream(File file) throws UnsupportedAudioFileException, IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    // HELPER METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * rllong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Protected helper method to read 64 bits and changing the order of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * each bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @param DataInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @return 32 bits swapped value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @exception IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    protected int rllong(DataInputStream dis) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        int b1, b2, b3, b4 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        i = dis.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        b1 = ( i & 0xFF ) << 24 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        b2 = ( i & 0xFF00 ) << 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        b3 = ( i & 0xFF0000 ) >> 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        b4 = ( i & 0xFF000000 ) >>> 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        i = ( b1 | b2 | b3 | b4 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * big2little
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Protected helper method to swap the order of bytes in a 32 bit int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @param int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @return 32 bits swapped value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    protected int big2little(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        int b1, b2, b3, b4 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        b1 = ( i & 0xFF ) << 24 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        b2 = ( i & 0xFF00 ) << 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        b3 = ( i & 0xFF0000 ) >> 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        b4 = ( i & 0xFF000000 ) >>> 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        i = ( b1 | b2 | b3 | b4 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * rlshort
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Protected helper method to read 16 bits value. Swap high with low byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @param DataInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @return the swapped value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @exception IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    protected short rlshort(DataInputStream dis)  throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        short s=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        short high, low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        s = dis.readShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        high = (short)(( s & 0xFF ) << 8) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        low = (short)(( s & 0xFF00 ) >>> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        s = (short)( high | low );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * big2little
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Protected helper method to swap the order of bytes in a 16 bit short
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @param int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @return 16 bits swapped value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    protected short big2littleShort(short i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        short high, low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        high = (short)(( i & 0xFF ) << 8) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        low = (short)(( i & 0xFF00 ) >>> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        i = (short)( high | low );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        /** Calculates the frame size for PCM frames.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
         * Note that this method is appropriate for non-packed samples.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
         * For instance, 12 bit, 2 channels will return 4 bytes, not 3.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
         * @param sampleSizeInBits the size of a single sample in bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
         * @param channels the number of channels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
         * @return the size of a PCM frame in bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        protected static int calculatePCMFrameSize(int sampleSizeInBits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                                                                                        int channels) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                return ((sampleSizeInBits + 7) / 8) * channels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
}