jdk/src/share/classes/com/sun/media/sound/AlawCodec.java
author darcy
Mon, 05 May 2014 23:19:00 -0700
changeset 24548 9c007a986347
parent 22584 eed64ee05369
permissions -rw-r--r--
8042256: Fix raw and unchecked lint warnings in com.sun.media.sound Reviewed-by: prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 18215
diff changeset
     2
 * Copyright (c) 1999, 2014, 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.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.sound.sampled.AudioFormat;
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 9035
diff changeset
    32
import javax.sound.sampled.AudioInputStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.sound.sampled.AudioSystem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * A-law encodes linear data, and decodes a-law data to linear data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author Kara Kytle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 9035
diff changeset
    41
public final class AlawCodec extends SunCodec {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    /* Tables used for A-law decoding */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 9035
diff changeset
    45
    private static final byte[] ALAW_TABH = new byte[256];
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 9035
diff changeset
    46
    private static final byte[] ALAW_TABL = new byte[256];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static final AudioFormat.Encoding[] alawEncodings = { AudioFormat.Encoding.ALAW, AudioFormat.Encoding.PCM_SIGNED };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final short seg_end [] = {0xFF, 0x1FF, 0x3FF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                                             0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * Initializes the decode tables
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        for (int i=0;i<256;i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            int input    = i ^ 0x55;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            int mantissa = (input & 0xf ) << 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            int segment  = (input & 0x70) >> 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            int value    = mantissa+8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            if(segment>=1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                value+=0x100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            if(segment>1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                value <<= (segment -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            if( (input & 0x80)==0 )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                value = -value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            ALAW_TABL[i] = (byte)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            ALAW_TABH[i] = (byte)(value>>8);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Constructs a new ALAW codec object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public AlawCodec() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        super(alawEncodings, alawEncodings);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // NEW CODE
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
    public AudioFormat.Encoding[] getTargetEncodings(AudioFormat sourceFormat){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if( sourceFormat.getEncoding().equals( AudioFormat.Encoding.PCM_SIGNED )) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            if( sourceFormat.getSampleSizeInBits() == 16 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
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.ALAW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                return enc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                return new AudioFormat.Encoding[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        } else if( sourceFormat.getEncoding().equals( AudioFormat.Encoding.ALAW ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if( sourceFormat.getSampleSizeInBits() == 8 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                AudioFormat.Encoding enc[] = new AudioFormat.Encoding[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                enc[0] = AudioFormat.Encoding.PCM_SIGNED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                return enc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                return new AudioFormat.Encoding[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            return new AudioFormat.Encoding[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
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
    public AudioFormat[] getTargetFormats(AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if( (targetEncoding.equals( AudioFormat.Encoding.PCM_SIGNED ) && sourceFormat.getEncoding().equals( AudioFormat.Encoding.ALAW)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            (targetEncoding.equals( AudioFormat.Encoding.ALAW) && sourceFormat.getEncoding().equals( AudioFormat.Encoding.PCM_SIGNED)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                return getOutputFormats( sourceFormat );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                return new AudioFormat[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public AudioInputStream getAudioInputStream(AudioFormat.Encoding targetEncoding, AudioInputStream sourceStream){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        AudioFormat sourceFormat = sourceStream.getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        AudioFormat.Encoding sourceEncoding = sourceFormat.getEncoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if( sourceEncoding.equals( targetEncoding ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            return sourceStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            AudioFormat targetFormat = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            if( !isConversionSupported(targetEncoding,sourceStream.getFormat()) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                throw new IllegalArgumentException("Unsupported conversion: " + sourceStream.getFormat().toString() + " to " + targetEncoding.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            if( sourceEncoding.equals( AudioFormat.Encoding.ALAW ) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                targetEncoding.equals( AudioFormat.Encoding.PCM_SIGNED ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                targetFormat = new AudioFormat( targetEncoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                                                sourceFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                                                16,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                                                sourceFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                                                2*sourceFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                                                sourceFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                                                sourceFormat.isBigEndian());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            } else if( sourceEncoding.equals( AudioFormat.Encoding.PCM_SIGNED ) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                       targetEncoding.equals( AudioFormat.Encoding.ALAW ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                targetFormat = new AudioFormat( targetEncoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                                                sourceFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                                                8,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                                                sourceFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                                                sourceFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                                                sourceFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                                                false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                throw new IllegalArgumentException("Unsupported conversion: " + sourceStream.getFormat().toString() + " to " + targetEncoding.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            return getAudioInputStream( targetFormat, sourceStream );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * use old code...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public AudioInputStream getAudioInputStream(AudioFormat targetFormat, AudioInputStream sourceStream){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return getConvertedStream( targetFormat, sourceStream );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    // OLD CODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Opens the codec with the specified parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @param stream stream from which data to be processed should be read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @param outputFormat desired data format of the stream after processing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @return stream from which processed data may be read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @throws IllegalArgumentException if the format combination supplied is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * not supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    /*  public AudioInputStream getConvertedStream(AudioFormat outputFormat, AudioInputStream stream) { */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    private AudioInputStream getConvertedStream(AudioFormat outputFormat, AudioInputStream stream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        AudioInputStream cs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        AudioFormat inputFormat = stream.getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if( inputFormat.matches(outputFormat) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            cs = stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            cs = (AudioInputStream) (new AlawCodecStream(stream, outputFormat));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        return cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Obtains the set of output formats supported by the codec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * given a particular input format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * If no output formats are supported for this input format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * returns an array of length 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @return array of supported output formats.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /*  public AudioFormat[] getOutputFormats(AudioFormat inputFormat) { */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    private AudioFormat[] getOutputFormats(AudioFormat inputFormat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
24548
9c007a986347 8042256: Fix raw and unchecked lint warnings in com.sun.media.sound
darcy
parents: 22584
diff changeset
   216
        Vector<AudioFormat> formats = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        AudioFormat format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        if ( AudioFormat.Encoding.PCM_SIGNED.equals(inputFormat.getEncoding())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            format = new AudioFormat(AudioFormat.Encoding.ALAW,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                                     inputFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                                     8,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                                     inputFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                                     inputFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                                     inputFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                                     false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            formats.addElement(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (AudioFormat.Encoding.ALAW.equals(inputFormat.getEncoding())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                                     inputFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                                     16,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                                     inputFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                                     inputFormat.getChannels()*2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                                     inputFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                                     false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            formats.addElement(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                                     inputFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                                     16,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                                     inputFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                                     inputFormat.getChannels()*2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                                     inputFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                                     true );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            formats.addElement(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        AudioFormat[] formatArray = new AudioFormat[formats.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        for (int i = 0; i < formatArray.length; i++) {
24548
9c007a986347 8042256: Fix raw and unchecked lint warnings in com.sun.media.sound
darcy
parents: 22584
diff changeset
   251
            formatArray[i] = formats.elementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        return formatArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 9035
diff changeset
   257
    final class AlawCodecStream extends AudioInputStream {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
8526
398796acdbd5 6938426: Concurrency bug in ALAW encoder causes random bursts of static/noise in output.
amenkov
parents: 5506
diff changeset
   259
        // tempBuffer required only for encoding (when encode is true)
398796acdbd5 6938426: Concurrency bug in ALAW encoder causes random bursts of static/noise in output.
amenkov
parents: 5506
diff changeset
   260
        private static final int tempBufferSize = 64;
398796acdbd5 6938426: Concurrency bug in ALAW encoder causes random bursts of static/noise in output.
amenkov
parents: 5506
diff changeset
   261
        private byte tempBuffer [] = null;
398796acdbd5 6938426: Concurrency bug in ALAW encoder causes random bursts of static/noise in output.
amenkov
parents: 5506
diff changeset
   262
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
         * True to encode to a-law, false to decode to linear
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        boolean encode = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        AudioFormat encodeFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        AudioFormat decodeFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        byte tabByte1[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        byte tabByte2[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        int highByte = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        int lowByte  = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        AlawCodecStream(AudioInputStream stream, AudioFormat outputFormat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            super(stream, outputFormat, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            AudioFormat inputFormat = stream.getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            // throw an IllegalArgumentException if not ok
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            if ( ! (isConversionSupported(outputFormat, inputFormat)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                throw new IllegalArgumentException("Unsupported conversion: " + inputFormat.toString() + " to " + outputFormat.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            //$$fb 2002-07-18: fix for 4714846: JavaSound ULAW (8-bit) encoder erroneously depends on endian-ness
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            boolean PCMIsBigEndian;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            // determine whether we are encoding or decoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            if (AudioFormat.Encoding.ALAW.equals(inputFormat.getEncoding())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                encode = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                encodeFormat = inputFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                decodeFormat = outputFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                PCMIsBigEndian = outputFormat.isBigEndian();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                encode = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                encodeFormat = outputFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                decodeFormat = inputFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                PCMIsBigEndian = inputFormat.isBigEndian();
8526
398796acdbd5 6938426: Concurrency bug in ALAW encoder causes random bursts of static/noise in output.
amenkov
parents: 5506
diff changeset
   302
                tempBuffer = new byte[tempBufferSize];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            if (PCMIsBigEndian) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                tabByte1 = ALAW_TABH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                tabByte2 = ALAW_TABL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                highByte = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                lowByte  = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                tabByte1 = ALAW_TABL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                tabByte2 = ALAW_TABH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                highByte = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                lowByte  = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            // set the AudioInputStream length in frames if we know it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            if (stream instanceof AudioInputStream) {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 18215
diff changeset
   319
                frameLength = stream.getFrameLength();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            // set framePos to zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            framePos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            frameSize = inputFormat.getFrameSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            if( frameSize==AudioSystem.NOT_SPECIFIED ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                frameSize=1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
         * $$jb 2/23/99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
         * Used to determine segment number in aLaw encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        private short search(short val, short table[], short size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            for(short i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                if (val <= table[i]) { return i; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
         * Note that this won't actually read anything; must read in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
         * two-byte units.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            byte[] b = new byte[1];
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 18215
diff changeset
   349
            return read(b, 0, b.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        public int read(byte[] b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            return read(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        public int read(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            // don't read fractional frames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            if( len%frameSize != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                len -= (len%frameSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            if (encode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                short QUANT_MASK = 0xF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                short SEG_SHIFT = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                short mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                short seg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                int adj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                short sample;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                byte enc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                int readCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                int currentPos = off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                int readLeft = len*2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                int readLen = ( (readLeft>tempBufferSize) ? tempBufferSize : readLeft );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                while ((readCount = super.read(tempBuffer,0,readLen))>0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    for (i = 0; i < readCount; i+=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                        /* Get the sample from the tempBuffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                        sample = (short)(( (tempBuffer[i + highByte]) << 8) & 0xFF00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                        sample |= (short)( (tempBuffer[i + lowByte]) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                        if(sample >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                            mask = 0xD5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                            mask = 0x55;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                            sample = (short)(-sample - 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                        /* Convert the scaled magnitude to segment number. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                        seg = search(sample, seg_end, (short) 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                         * Combine the sign, segment, quantization bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                        if (seg >= 8) {  /* out of range, return maximum value. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                            enc = (byte) (0x7F ^ mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                            enc = (byte) (seg << SEG_SHIFT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                            if(seg < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                                enc |= (byte) ( (sample >> 4) & QUANT_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                                enc |= (byte) ( (sample >> (seg + 3)) & QUANT_MASK );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                            enc ^= mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                        /* Now put the encoded sample where it belongs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                        b[currentPos] = enc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                        currentPos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    /* And update pointers and counters for next iteration */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    readLeft -= readCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    readLen = ( (readLeft>tempBufferSize) ? tempBufferSize : readLeft );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                if( currentPos==off && readCount<0 ) {  // EOF or error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    return readCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                return (currentPos - off);  /* Number of bytes written to new buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                int readLen = len/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                int readOffset = off + len/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                int readCount = super.read(b, readOffset, readLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                for (i = off; i < (off + (readCount*2)); i+=2) {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 18215
diff changeset
   435
                    b[i]        = tabByte1[b[readOffset] & 0xFF];
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 18215
diff changeset
   436
                    b[i+1]      = tabByte2[b[readOffset] & 0xFF];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                    readOffset++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                if( readCount<0 ) {             // EOF or error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                    return readCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                return (i - off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    } // end class AlawCodecStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
} // end class ALAW