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