jdk/src/share/classes/com/sun/media/sound/UlawCodec.java
author amenkov
Wed, 06 Apr 2011 15:12:33 +0400
changeset 9215 cab45ca6ab44
parent 5506 202f599c92aa
child 18215 b2afd66ce6db
permissions -rw-r--r--
6992523: FindBugs scan - Malicious code vulnerability Warnings in com.sun.media.sound.* Reviewed-by: alexp
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
 * U-law encodes linear data, and decodes u-law data to linear data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author Kara Kytle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class UlawCodec extends SunCodec {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /* Tables used for U-law decoding */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    final static byte ULAW_TABH[] = new byte[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    final static byte ULAW_TABL[] = new byte[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final AudioFormat.Encoding[] ulawEncodings = {AudioFormat.Encoding.ULAW,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                                                                 AudioFormat.Encoding.PCM_SIGNED};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final short seg_end [] = {0xFF, 0x1FF, 0x3FF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                                             0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Initializes the decode tables
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        for (int i=0;i<256;i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            int ulaw = ~i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            int t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            ulaw &= 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            t = ((ulaw & 0xf)<<3) + 132;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            t <<= ((ulaw & 0x70) >> 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            t = ( (ulaw&0x80) != 0 ) ? (132-t) : (t-132);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            ULAW_TABL[i] = (byte) (t&0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            ULAW_TABH[i] = (byte) ((t>>8) & 0xff);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Constructs a new ULAW codec object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public UlawCodec() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        super(ulawEncodings, ulawEncodings);
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
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public AudioFormat.Encoding[] getTargetEncodings(AudioFormat sourceFormat){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if( AudioFormat.Encoding.PCM_SIGNED.equals(sourceFormat.getEncoding()) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            if( sourceFormat.getSampleSizeInBits() == 16 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                AudioFormat.Encoding enc[] = new AudioFormat.Encoding[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                enc[0] = AudioFormat.Encoding.ULAW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                return enc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                return new AudioFormat.Encoding[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        } else if (AudioFormat.Encoding.ULAW.equals(sourceFormat.getEncoding())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            if (sourceFormat.getSampleSizeInBits() == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                AudioFormat.Encoding enc[] = new AudioFormat.Encoding[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                enc[0] = AudioFormat.Encoding.PCM_SIGNED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                return enc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                return new AudioFormat.Encoding[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            return new AudioFormat.Encoding[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
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
    public AudioFormat[] getTargetFormats(AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if( (AudioFormat.Encoding.PCM_SIGNED.equals(targetEncoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
             && AudioFormat.Encoding.ULAW.equals(sourceFormat.getEncoding()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            (AudioFormat.Encoding.ULAW.equals(targetEncoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
             && AudioFormat.Encoding.PCM_SIGNED.equals(sourceFormat.getEncoding()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                return getOutputFormats(sourceFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                return new AudioFormat[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public AudioInputStream getAudioInputStream(AudioFormat.Encoding targetEncoding, AudioInputStream sourceStream){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        AudioFormat sourceFormat = sourceStream.getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        AudioFormat.Encoding sourceEncoding = sourceFormat.getEncoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (sourceEncoding.equals(targetEncoding)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            return sourceStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            AudioFormat targetFormat = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            if (!isConversionSupported(targetEncoding,sourceStream.getFormat())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                throw new IllegalArgumentException("Unsupported conversion: " + sourceStream.getFormat().toString() + " to " + targetEncoding.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            if (AudioFormat.Encoding.ULAW.equals(sourceEncoding) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                AudioFormat.Encoding.PCM_SIGNED.equals(targetEncoding) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                targetFormat = new AudioFormat( targetEncoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                                                sourceFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                                                16,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                                sourceFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                                                2*sourceFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                                sourceFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                                                sourceFormat.isBigEndian());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            } else if (AudioFormat.Encoding.PCM_SIGNED.equals(sourceEncoding) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                       AudioFormat.Encoding.ULAW.equals(targetEncoding)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                targetFormat = new AudioFormat( targetEncoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                                                sourceFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                                                8,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                                                sourceFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                                                sourceFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                                                sourceFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                                                false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                throw new IllegalArgumentException("Unsupported conversion: " + sourceStream.getFormat().toString() + " to " + targetEncoding.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            return getAudioInputStream( targetFormat, sourceStream );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * use old code...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public AudioInputStream getAudioInputStream(AudioFormat targetFormat, AudioInputStream sourceStream){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return getConvertedStream(targetFormat, sourceStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    // OLD CODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * Opens the codec with the specified parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @param stream stream from which data to be processed should be read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @param outputFormat desired data format of the stream after processing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @return stream from which processed data may be read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @throws IllegalArgumentException if the format combination supplied is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * not supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /*  public AudioInputStream getConvertedStream(AudioFormat outputFormat, AudioInputStream stream) { */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    private AudioInputStream getConvertedStream(AudioFormat outputFormat, AudioInputStream stream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        AudioInputStream cs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        AudioFormat inputFormat = stream.getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if( inputFormat.matches(outputFormat) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            cs = stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            cs = (AudioInputStream) (new UlawCodecStream(stream, outputFormat));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Obtains the set of output formats supported by the codec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * given a particular input format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * If no output formats are supported for this input format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * returns an array of length 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @return array of supported output formats.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /*  public AudioFormat[] getOutputFormats(AudioFormat inputFormat) { */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    private AudioFormat[] getOutputFormats(AudioFormat inputFormat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        Vector formats = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        AudioFormat format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if ((inputFormat.getSampleSizeInBits() == 16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            && AudioFormat.Encoding.PCM_SIGNED.equals(inputFormat.getEncoding())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            format = new AudioFormat(AudioFormat.Encoding.ULAW,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                                     inputFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                                     8,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                                     inputFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                                     inputFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                     inputFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                                     false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            formats.addElement(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (AudioFormat.Encoding.ULAW.equals(inputFormat.getEncoding())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                                     inputFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                                     16,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                                     inputFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                                     inputFormat.getChannels()*2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                                     inputFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                                     false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            formats.addElement(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                                     inputFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                                     16,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                                     inputFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                                     inputFormat.getChannels()*2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                                     inputFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                                     true );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            formats.addElement(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        AudioFormat[] formatArray = new AudioFormat[formats.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        for (int i = 0; i < formatArray.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            formatArray[i] = (AudioFormat)(formats.elementAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        return formatArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    class UlawCodecStream extends AudioInputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        private static final int tempBufferSize = 64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        private byte tempBuffer [] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
         * True to encode to u-law, false to decode to linear
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        boolean encode = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        AudioFormat encodeFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        AudioFormat decodeFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        byte tabByte1[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        byte tabByte2[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        int highByte = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        int lowByte  = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        UlawCodecStream(AudioInputStream stream, AudioFormat outputFormat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            super(stream, outputFormat, AudioSystem.NOT_SPECIFIED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            AudioFormat inputFormat = stream.getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            // throw an IllegalArgumentException if not ok
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            if (!(isConversionSupported(outputFormat, inputFormat))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                throw new IllegalArgumentException("Unsupported conversion: " + inputFormat.toString() + " to " + outputFormat.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            //$$fb 2002-07-18: fix for 4714846: JavaSound ULAW (8-bit) encoder erroneously depends on endian-ness
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            boolean PCMIsBigEndian;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            // determine whether we are encoding or decoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            if (AudioFormat.Encoding.ULAW.equals(inputFormat.getEncoding())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                encode = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                encodeFormat = inputFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                decodeFormat = outputFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                PCMIsBigEndian = outputFormat.isBigEndian();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                encode = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                encodeFormat = outputFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                decodeFormat = inputFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                PCMIsBigEndian = inputFormat.isBigEndian();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                tempBuffer = new byte[tempBufferSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            // setup tables according to byte order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            if (PCMIsBigEndian) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                tabByte1 = ULAW_TABH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                tabByte2 = ULAW_TABL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                highByte = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                lowByte  = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                tabByte1 = ULAW_TABL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                tabByte2 = ULAW_TABH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                highByte = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                lowByte  = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            // set the AudioInputStream length in frames if we know it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            if (stream instanceof AudioInputStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                frameLength = ((AudioInputStream)stream).getFrameLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            // set framePos to zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            framePos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            frameSize = inputFormat.getFrameSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            if (frameSize == AudioSystem.NOT_SPECIFIED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                frameSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
         * $$jb 2/23/99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
         * Used to determine segment number in uLaw encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        private short search(short val, short table[], short size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            for(short i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                if (val <= table[i]) { return i; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
         * Note that this won't actually read anything; must read in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
         * two-byte units.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            byte[] b = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            if (read(b, 0, b.length) == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                return b[1] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        public int read(byte[] b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            return read(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        public int read(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            // don't read fractional frames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            if( len%frameSize != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                len -= (len%frameSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            if (encode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                short BIAS = 0x84;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                short mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                short seg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                short sample;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                byte enc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                int readCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                int currentPos = off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                int readLeft = len*2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                int readLen = ( (readLeft>tempBufferSize) ? tempBufferSize : readLeft );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                while ((readCount = super.read(tempBuffer,0,readLen))>0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    for(i = 0; i < readCount; i+=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                        /* Get the sample from the tempBuffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                        sample = (short)(( (tempBuffer[i + highByte]) << 8) & 0xFF00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                        sample |= (short)( (short) (tempBuffer[i + lowByte]) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                        /* Get the sign and the magnitude of the value. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                        if(sample < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                            sample = (short) (BIAS - sample);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                            mask = 0x7F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                            sample += BIAS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                            mask = 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                        /* Convert the scaled magnitude to segment number. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                        seg = search(sample, seg_end, (short) 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                         * Combine the sign, segment, quantization bits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                         * and complement the code word.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                        if (seg >= 8) {  /* out of range, return maximum value. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                            enc = (byte) (0x7F ^ mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                            enc = (byte) ((seg << 4) | ((sample >> (seg+3)) & 0xF));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                            enc ^= mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                        /* Now put the encoded sample where it belongs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                        b[currentPos] = enc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                        currentPos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                    /* And update pointers and counters for next iteration */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                    readLeft -= readCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                    readLen = ( (readLeft>tempBufferSize) ? tempBufferSize : readLeft );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                if( currentPos==off && readCount<0 ) {  // EOF or error on read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    return readCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                return (currentPos - off);  /* Number of bytes written to new buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                int readLen = len/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                int readOffset = off + len/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                int readCount = super.read(b, readOffset, readLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                if(readCount<0) {               // EOF or error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                    return readCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                for (i = off; i < (off + (readCount*2)); i+=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    b[i]        = (byte)tabByte1[b[readOffset] & 0xFF];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    b[i+1]      = (byte)tabByte2[b[readOffset] & 0xFF];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                    readOffset++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                return (i - off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    } // end class UlawCodecStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
} // end class ULAW