jdk/src/share/classes/com/sun/media/sound/PCMtoPCMCodec.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.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.sound.sampled.AudioFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.sound.sampled.AudioSystem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.sound.sampled.AudioInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Converts among signed/unsigned and little/big endianness of sampled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author Jan Borgersen
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class PCMtoPCMCodec extends SunCodec {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static final AudioFormat.Encoding[] inputEncodings = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        AudioFormat.Encoding.PCM_SIGNED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        AudioFormat.Encoding.PCM_UNSIGNED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final AudioFormat.Encoding[] outputEncodings = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        AudioFormat.Encoding.PCM_SIGNED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        AudioFormat.Encoding.PCM_UNSIGNED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static final int tempBufferSize = 64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private byte tempBuffer [] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Constructs a new PCMtoPCM codec object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public PCMtoPCMCodec() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        super( inputEncodings, outputEncodings);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // NEW CODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public AudioFormat.Encoding[] getTargetEncodings(AudioFormat sourceFormat){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if( sourceFormat.getEncoding().equals( AudioFormat.Encoding.PCM_SIGNED ) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            sourceFormat.getEncoding().equals( AudioFormat.Encoding.PCM_UNSIGNED ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                AudioFormat.Encoding encs[] = new AudioFormat.Encoding[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                encs[0] = AudioFormat.Encoding.PCM_SIGNED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                encs[1] = AudioFormat.Encoding.PCM_UNSIGNED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                return encs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                return new AudioFormat.Encoding[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public AudioFormat[] getTargetFormats(AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        // filter out targetEncoding from the old getOutputFormats( sourceFormat ) method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        AudioFormat[] formats = getOutputFormats( sourceFormat );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        Vector newFormats = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        for(int i=0; i<formats.length; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            if( formats[i].getEncoding().equals( targetEncoding ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                newFormats.addElement( formats[i] );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        AudioFormat[] formatArray = new AudioFormat[newFormats.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        for (int i = 0; i < formatArray.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            formatArray[i] = (AudioFormat)(newFormats.elementAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        return formatArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public AudioInputStream getAudioInputStream(AudioFormat.Encoding targetEncoding, AudioInputStream sourceStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if( isConversionSupported(targetEncoding, sourceStream.getFormat()) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            AudioFormat sourceFormat = sourceStream.getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            AudioFormat targetFormat = new AudioFormat( targetEncoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                                                        sourceFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                                                        sourceFormat.getSampleSizeInBits(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                                                        sourceFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                                                        sourceFormat.getFrameSize(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                                                        sourceFormat.getFrameRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                                        sourceFormat.isBigEndian() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            return getAudioInputStream( targetFormat, sourceStream );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            throw new IllegalArgumentException("Unsupported conversion: " + sourceStream.getFormat().toString() + " to " + targetEncoding.toString() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * use old code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public AudioInputStream getAudioInputStream(AudioFormat targetFormat, AudioInputStream sourceStream){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return getConvertedStream( targetFormat, sourceStream );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    // OLD CODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Opens the codec with the specified parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param stream stream from which data to be processed should be read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param outputFormat desired data format of the stream after processing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @return stream from which processed data may be read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @throws IllegalArgumentException if the format combination supplied is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * not supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /*  public AudioInputStream getConvertedStream(AudioFormat outputFormat, AudioInputStream stream) {*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private AudioInputStream getConvertedStream(AudioFormat outputFormat, AudioInputStream stream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        AudioInputStream cs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        AudioFormat inputFormat = stream.getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if( inputFormat.matches(outputFormat) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            cs = stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            cs = (AudioInputStream) (new PCMtoPCMCodecStream(stream, outputFormat));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            tempBuffer = new byte[tempBufferSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Obtains the set of output formats supported by the codec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * given a particular input format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * If no output formats are supported for this input format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * returns an array of length 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @return array of supported output formats.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /*  public AudioFormat[] getOutputFormats(AudioFormat inputFormat) { */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    private AudioFormat[] getOutputFormats(AudioFormat inputFormat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        Vector formats = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        AudioFormat format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        int sampleSize = inputFormat.getSampleSizeInBits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        boolean isBigEndian = inputFormat.isBigEndian();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if ( sampleSize==8 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            if ( AudioFormat.Encoding.PCM_SIGNED.equals(inputFormat.getEncoding()) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                format = new AudioFormat(AudioFormat.Encoding.PCM_UNSIGNED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                                         inputFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                                         inputFormat.getSampleSizeInBits(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                                         inputFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                                         inputFormat.getFrameSize(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                                         inputFormat.getFrameRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                                         false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                formats.addElement(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            if ( AudioFormat.Encoding.PCM_UNSIGNED.equals(inputFormat.getEncoding()) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                                         inputFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                                         inputFormat.getSampleSizeInBits(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                                         inputFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                         inputFormat.getFrameSize(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                                         inputFormat.getFrameRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                         false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                formats.addElement(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        } else if ( sampleSize==16 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            if ( AudioFormat.Encoding.PCM_SIGNED.equals(inputFormat.getEncoding()) && isBigEndian ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                format = new AudioFormat(AudioFormat.Encoding.PCM_UNSIGNED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                                         inputFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                                         inputFormat.getSampleSizeInBits(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                                         inputFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                                         inputFormat.getFrameSize(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                                         inputFormat.getFrameRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                                         true );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                formats.addElement(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                                         inputFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                                         inputFormat.getSampleSizeInBits(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                                         inputFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                                         inputFormat.getFrameSize(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                                         inputFormat.getFrameRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                                         false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                formats.addElement(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                format = new AudioFormat(AudioFormat.Encoding.PCM_UNSIGNED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                                         inputFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                                         inputFormat.getSampleSizeInBits(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                                         inputFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                                         inputFormat.getFrameSize(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                                         inputFormat.getFrameRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                                         false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                formats.addElement(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            if ( AudioFormat.Encoding.PCM_UNSIGNED.equals(inputFormat.getEncoding()) && isBigEndian ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                                         inputFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                                         inputFormat.getSampleSizeInBits(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                                         inputFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                                         inputFormat.getFrameSize(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                                         inputFormat.getFrameRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                                         true );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                formats.addElement(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                format = new AudioFormat(AudioFormat.Encoding.PCM_UNSIGNED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                                         inputFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                                         inputFormat.getSampleSizeInBits(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                                         inputFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                                         inputFormat.getFrameSize(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                                         inputFormat.getFrameRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                                         false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                formats.addElement(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                                         inputFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                                         inputFormat.getSampleSizeInBits(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                                         inputFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                                         inputFormat.getFrameSize(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                                         inputFormat.getFrameRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                                         false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                formats.addElement(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            if ( AudioFormat.Encoding.PCM_SIGNED.equals(inputFormat.getEncoding()) && !isBigEndian ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                format = new AudioFormat(AudioFormat.Encoding.PCM_UNSIGNED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                                         inputFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                                         inputFormat.getSampleSizeInBits(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                                         inputFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                                         inputFormat.getFrameSize(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                                         inputFormat.getFrameRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                                         false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                formats.addElement(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                                         inputFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                                         inputFormat.getSampleSizeInBits(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                                         inputFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                                         inputFormat.getFrameSize(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                                         inputFormat.getFrameRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                                         true );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                formats.addElement(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                format = new AudioFormat(AudioFormat.Encoding.PCM_UNSIGNED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                                         inputFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                                         inputFormat.getSampleSizeInBits(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                                         inputFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                                         inputFormat.getFrameSize(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                                         inputFormat.getFrameRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                                         true );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                formats.addElement(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            if ( AudioFormat.Encoding.PCM_UNSIGNED.equals(inputFormat.getEncoding()) && !isBigEndian ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                                         inputFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                                         inputFormat.getSampleSizeInBits(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                                         inputFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                                         inputFormat.getFrameSize(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                                         inputFormat.getFrameRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                                         false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                formats.addElement(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                format = new AudioFormat(AudioFormat.Encoding.PCM_UNSIGNED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                                         inputFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                                         inputFormat.getSampleSizeInBits(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                                         inputFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                                         inputFormat.getFrameSize(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                                         inputFormat.getFrameRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                                         true );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                formats.addElement(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                                         inputFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                                         inputFormat.getSampleSizeInBits(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                                         inputFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                                         inputFormat.getFrameSize(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                                         inputFormat.getFrameRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                                         true );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                formats.addElement(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        AudioFormat[] formatArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        synchronized(formats) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            formatArray = new AudioFormat[formats.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            for (int i = 0; i < formatArray.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                formatArray[i] = (AudioFormat)(formats.elementAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        return formatArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    class PCMtoPCMCodecStream extends AudioInputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        private final int PCM_SWITCH_SIGNED_8BIT                = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        private final int PCM_SWITCH_ENDIAN                             = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        private final int PCM_SWITCH_SIGNED_LE                  = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        private final int PCM_SWITCH_SIGNED_BE                  = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        private final int PCM_UNSIGNED_LE2SIGNED_BE             = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        private final int PCM_SIGNED_LE2UNSIGNED_BE             = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        private final int PCM_UNSIGNED_BE2SIGNED_LE             = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        private final int PCM_SIGNED_BE2UNSIGNED_LE             = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        private int sampleSizeInBytes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        private int conversionType = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        PCMtoPCMCodecStream(AudioInputStream stream, AudioFormat outputFormat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            super(stream, outputFormat, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            int sampleSizeInBits = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            AudioFormat.Encoding inputEncoding = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            AudioFormat.Encoding outputEncoding = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            boolean inputIsBigEndian;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            boolean outputIsBigEndian;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            AudioFormat inputFormat = stream.getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            // throw an IllegalArgumentException if not ok
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            if ( ! (isConversionSupported(inputFormat, outputFormat)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                throw new IllegalArgumentException("Unsupported conversion: " + inputFormat.toString() + " to " + outputFormat.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            inputEncoding = inputFormat.getEncoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            outputEncoding = outputFormat.getEncoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            inputIsBigEndian = inputFormat.isBigEndian();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            outputIsBigEndian = outputFormat.isBigEndian();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            sampleSizeInBits = inputFormat.getSampleSizeInBits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            sampleSizeInBytes = sampleSizeInBits/8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            // determine conversion to perform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            if( sampleSizeInBits==8 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                if( AudioFormat.Encoding.PCM_UNSIGNED.equals(inputEncoding) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                    AudioFormat.Encoding.PCM_SIGNED.equals(outputEncoding) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                    conversionType = PCM_SWITCH_SIGNED_8BIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                    if(Printer.debug) Printer.debug("PCMtoPCMCodecStream: conversionType = PCM_SWITCH_SIGNED_8BIT");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                } else if( AudioFormat.Encoding.PCM_SIGNED.equals(inputEncoding) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                           AudioFormat.Encoding.PCM_UNSIGNED.equals(outputEncoding) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                    conversionType = PCM_SWITCH_SIGNED_8BIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    if(Printer.debug) Printer.debug("PCMtoPCMCodecStream: conversionType = PCM_SWITCH_SIGNED_8BIT");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                if( inputEncoding.equals(outputEncoding) && (inputIsBigEndian != outputIsBigEndian) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    conversionType = PCM_SWITCH_ENDIAN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    if(Printer.debug) Printer.debug("PCMtoPCMCodecStream: conversionType = PCM_SWITCH_ENDIAN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                } else if (AudioFormat.Encoding.PCM_UNSIGNED.equals(inputEncoding) && !inputIsBigEndian &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                            AudioFormat.Encoding.PCM_SIGNED.equals(outputEncoding) && outputIsBigEndian) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                    conversionType = PCM_UNSIGNED_LE2SIGNED_BE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                    if(Printer.debug) Printer.debug("PCMtoPCMCodecStream: conversionType = PCM_UNSIGNED_LE2SIGNED_BE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                } else if (AudioFormat.Encoding.PCM_SIGNED.equals(inputEncoding) && !inputIsBigEndian &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                           AudioFormat.Encoding.PCM_UNSIGNED.equals(outputEncoding) && outputIsBigEndian) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    conversionType = PCM_SIGNED_LE2UNSIGNED_BE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    if(Printer.debug) Printer.debug("PCMtoPCMCodecStream: conversionType = PCM_SIGNED_LE2UNSIGNED_BE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                } else if (AudioFormat.Encoding.PCM_UNSIGNED.equals(inputEncoding) && inputIsBigEndian &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                           AudioFormat.Encoding.PCM_SIGNED.equals(outputEncoding) && !outputIsBigEndian) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                    conversionType = PCM_UNSIGNED_BE2SIGNED_LE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                    if(Printer.debug) Printer.debug("PCMtoPCMCodecStream: conversionType = PCM_UNSIGNED_BE2SIGNED_LE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                } else if (AudioFormat.Encoding.PCM_SIGNED.equals(inputEncoding) && inputIsBigEndian &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                           AudioFormat.Encoding.PCM_UNSIGNED.equals(outputEncoding) && !outputIsBigEndian) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                    conversionType = PCM_SIGNED_BE2UNSIGNED_LE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                    if(Printer.debug) Printer.debug("PCMtoPCMCodecStream: conversionType = PCM_SIGNED_BE2UNSIGNED_LE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            // set the audio stream length in frames if we know it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            frameSize = inputFormat.getFrameSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            if( frameSize == AudioSystem.NOT_SPECIFIED ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                frameSize=1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            if( stream instanceof AudioInputStream ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                frameLength = stream.getFrameLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                frameLength = AudioSystem.NOT_SPECIFIED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            // set framePos to zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            framePos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
         * Note that this only works for sign conversions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
         * Other conversions require a read of at least 2 bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            // $$jb: do we want to implement this function?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            int temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            byte tempbyte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            if( frameSize==1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                if( conversionType == PCM_SWITCH_SIGNED_8BIT ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                    temp = super.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    if( temp < 0 ) return temp;         // EOF or error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                    tempbyte = (byte) (temp & 0xf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                    tempbyte = (tempbyte >= 0) ? (byte)(0x80 | tempbyte) : (byte)(0x7F & tempbyte);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                    temp = (int) tempbyte & 0xf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                    return temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                    // $$jb: what to return here?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                    throw new IOException("cannot read a single byte if frame size > 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                throw new IOException("cannot read a single byte if frame size > 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        public int read(byte[] b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            return read(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        public int read(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            // don't read fractional frames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            if ( len%frameSize != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                len -= (len%frameSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            // don't read past our own set length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            if( (frameLength!=AudioSystem.NOT_SPECIFIED) && ( (len/frameSize) >(frameLength-framePos)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                len = (int)(frameLength-framePos) * frameSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            int readCount = super.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            byte tempByte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            if(readCount<0) {   // EOF or error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                return readCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            // now do the conversions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            switch( conversionType ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            case PCM_SWITCH_SIGNED_8BIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                switchSigned8bit(b,off,len,readCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            case PCM_SWITCH_ENDIAN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                switchEndian(b,off,len,readCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            case PCM_SWITCH_SIGNED_LE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                switchSignedLE(b,off,len,readCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            case PCM_SWITCH_SIGNED_BE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                switchSignedBE(b,off,len,readCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            case PCM_UNSIGNED_LE2SIGNED_BE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            case PCM_SIGNED_LE2UNSIGNED_BE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                switchSignedLE(b,off,len,readCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                switchEndian(b,off,len,readCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            case PCM_UNSIGNED_BE2SIGNED_LE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            case PCM_SIGNED_BE2UNSIGNED_LE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                switchSignedBE(b,off,len,readCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                switchEndian(b,off,len,readCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                                // do nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            // we've done the conversion, just return the readCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            return readCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        private void switchSigned8bit(byte[] b, int off, int len, int readCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            for(int i=off; i < (off+readCount); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                b[i] = (b[i] >= 0) ? (byte)(0x80 | b[i]) : (byte)(0x7F & b[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        private void switchSignedBE(byte[] b, int off, int len, int readCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            for(int i=off; i < (off+readCount); i+= sampleSizeInBytes ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                b[i] = (b[i] >= 0) ? (byte)(0x80 | b[i]) : (byte)(0x7F & b[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        private void switchSignedLE(byte[] b, int off, int len, int readCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            for(int i=(off+sampleSizeInBytes-1); i < (off+readCount); i+= sampleSizeInBytes ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                b[i] = (b[i] >= 0) ? (byte)(0x80 | b[i]) : (byte)(0x7F & b[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        private void switchEndian(byte[] b, int off, int len, int readCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            if(sampleSizeInBytes == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                for(int i=off; i < (off+readCount); i += sampleSizeInBytes ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                    byte temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                    temp = b[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                    b[i] = b[i+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                    b[i+1] = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    } // end class PCMtoPCMCodecStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
} // end class PCMtoPCMCodec