jdk/src/java.desktop/share/classes/com/sun/media/sound/AlawCodec.java
author serb
Thu, 18 Feb 2016 22:11:29 +0300
changeset 36454 d2853d1fc614
parent 35685 f405bbd07cc6
child 40444 afabcfc2f3ef
permissions -rw-r--r--
8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV Reviewed-by: prr, amenkov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 25859
diff changeset
     2
 * Copyright (c) 1999, 2015, 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;
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 25859
diff changeset
    29
import java.util.Objects;
2
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;
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 9035
diff changeset
    33
import javax.sound.sampled.AudioInputStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.sound.sampled.AudioSystem;
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
 * A-law encodes linear data, and decodes a-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: 9035
diff changeset
    42
public final class AlawCodec extends SunCodec {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /* Tables used for A-law decoding */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 9035
diff changeset
    46
    private static final byte[] ALAW_TABH = new byte[256];
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 9035
diff changeset
    47
    private static final byte[] ALAW_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[] alawEncodings = { AudioFormat.Encoding.ALAW, AudioFormat.Encoding.PCM_SIGNED };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final short seg_end [] = {0xFF, 0x1FF, 0x3FF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                                             0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Initializes the decode tables
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        for (int i=0;i<256;i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            int input    = i ^ 0x55;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            int mantissa = (input & 0xf ) << 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            int segment  = (input & 0x70) >> 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            int value    = mantissa+8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            if(segment>=1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                value+=0x100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            if(segment>1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                value <<= (segment -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            if( (input & 0x80)==0 )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                value = -value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            ALAW_TABL[i] = (byte)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            ALAW_TABH[i] = (byte)(value>>8);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Constructs a new ALAW codec object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public AlawCodec() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        super(alawEncodings, alawEncodings);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    // NEW CODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public AudioFormat.Encoding[] getTargetEncodings(AudioFormat sourceFormat){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if( sourceFormat.getEncoding().equals( AudioFormat.Encoding.PCM_SIGNED )) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            if( sourceFormat.getSampleSizeInBits() == 16 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                AudioFormat.Encoding enc[] = new AudioFormat.Encoding[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                enc[0] = AudioFormat.Encoding.ALAW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                return enc;
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
        } else if( sourceFormat.getEncoding().equals( AudioFormat.Encoding.ALAW ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            if( sourceFormat.getSampleSizeInBits() == 8 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                AudioFormat.Encoding enc[] = new AudioFormat.Encoding[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                enc[0] = AudioFormat.Encoding.PCM_SIGNED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                return enc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                return new AudioFormat.Encoding[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            return new AudioFormat.Encoding[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 AudioFormat[] getTargetFormats(AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat){
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 25859
diff changeset
   123
        Objects.requireNonNull(sourceFormat);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if( (targetEncoding.equals( AudioFormat.Encoding.PCM_SIGNED ) && sourceFormat.getEncoding().equals( AudioFormat.Encoding.ALAW)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            (targetEncoding.equals( AudioFormat.Encoding.ALAW) && sourceFormat.getEncoding().equals( AudioFormat.Encoding.PCM_SIGNED)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                return getOutputFormats( sourceFormat );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                return new AudioFormat[0];
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public AudioInputStream getAudioInputStream(AudioFormat.Encoding targetEncoding, AudioInputStream sourceStream){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        AudioFormat sourceFormat = sourceStream.getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        AudioFormat.Encoding sourceEncoding = sourceFormat.getEncoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
35684
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   138
        if( !isConversionSupported(targetEncoding,sourceStream.getFormat()) ) {
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   139
            throw new IllegalArgumentException("Unsupported conversion: " + sourceStream.getFormat().toString() + " to " + targetEncoding.toString());
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   140
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if( sourceEncoding.equals( targetEncoding ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            return sourceStream;
35684
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   143
        }
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   144
        AudioFormat targetFormat = null;
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   145
        if( sourceEncoding.equals( AudioFormat.Encoding.ALAW ) &&
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   146
            targetEncoding.equals( AudioFormat.Encoding.PCM_SIGNED ) ) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
35684
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   148
            targetFormat = new AudioFormat( targetEncoding,
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   149
                                            sourceFormat.getSampleRate(),
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   150
                                            16,
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   151
                                            sourceFormat.getChannels(),
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   152
                                            2*sourceFormat.getChannels(),
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   153
                                            sourceFormat.getSampleRate(),
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   154
                                            sourceFormat.isBigEndian());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
35684
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   156
        } else if( sourceEncoding.equals( AudioFormat.Encoding.PCM_SIGNED ) &&
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   157
                   targetEncoding.equals( AudioFormat.Encoding.ALAW ) ) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
35684
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   159
            targetFormat = new AudioFormat( targetEncoding,
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   160
                                            sourceFormat.getSampleRate(),
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   161
                                            8,
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   162
                                            sourceFormat.getChannels(),
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   163
                                            sourceFormat.getChannels(),
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   164
                                            sourceFormat.getSampleRate(),
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   165
                                            false);
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   166
        } else {
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   167
            throw new IllegalArgumentException("Unsupported conversion: " + sourceStream.getFormat().toString() + " to " + targetEncoding.toString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
35684
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   169
        return getConvertedStream(targetFormat, sourceStream);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * use old code...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public AudioInputStream getAudioInputStream(AudioFormat targetFormat, AudioInputStream sourceStream){
35684
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   176
        if (!isConversionSupported(targetFormat, sourceStream.getFormat()))
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   177
            throw new IllegalArgumentException("Unsupported conversion: "
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   178
                                               + sourceStream.getFormat().toString() + " to "
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   179
                                               + targetFormat.toString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return getConvertedStream( targetFormat, sourceStream );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    // OLD CODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Opens the codec with the specified parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @param stream stream from which data to be processed should be read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @param outputFormat desired data format of the stream after processing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @return stream from which processed data may be read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @throws IllegalArgumentException if the format combination supplied is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * not supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /*  public AudioInputStream getConvertedStream(AudioFormat outputFormat, AudioInputStream stream) { */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    private AudioInputStream getConvertedStream(AudioFormat outputFormat, AudioInputStream stream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        AudioInputStream cs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        AudioFormat inputFormat = stream.getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        if( inputFormat.matches(outputFormat) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            cs = stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            cs = (AudioInputStream) (new AlawCodecStream(stream, outputFormat));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Obtains the set of output formats supported by the codec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * given a particular input format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * If no output formats are supported for this input format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * returns an array of length 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @return array of supported output formats.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /*  public AudioFormat[] getOutputFormats(AudioFormat inputFormat) { */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    private AudioFormat[] getOutputFormats(AudioFormat inputFormat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
24548
9c007a986347 8042256: Fix raw and unchecked lint warnings in com.sun.media.sound
darcy
parents: 22584
diff changeset
   221
        Vector<AudioFormat> formats = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        AudioFormat format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
35684
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   224
        if (inputFormat.getSampleSizeInBits() == 16
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   225
                && AudioFormat.Encoding.PCM_SIGNED.equals(inputFormat.getEncoding())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            format = new AudioFormat(AudioFormat.Encoding.ALAW,
35684
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   227
                                     inputFormat.getSampleRate(), 8,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                                     inputFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                                     inputFormat.getChannels(),
35684
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   230
                                     inputFormat.getSampleRate(), false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            formats.addElement(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
35684
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   233
        if (inputFormat.getSampleSizeInBits() == 8
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   234
                && AudioFormat.Encoding.ALAW.equals(inputFormat.getEncoding())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
35684
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   236
                                     inputFormat.getSampleRate(), 16,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                                     inputFormat.getChannels(),
35684
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   238
                                     inputFormat.getChannels() * 2,
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   239
                                     inputFormat.getSampleRate(), false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            formats.addElement(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
35684
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   242
                                     inputFormat.getSampleRate(), 16,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                                     inputFormat.getChannels(),
35684
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   244
                                     inputFormat.getChannels() * 2,
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 34412
diff changeset
   245
                                     inputFormat.getSampleRate(), true);
2
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
35685
f405bbd07cc6 6459818: Audio A-law and law decoder skip() method not implemented
serb
parents: 35684
diff changeset
   257
    private 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
        }
35685
f405bbd07cc6 6459818: Audio A-law and law decoder skip() method not implemented
serb
parents: 35684
diff changeset
   447
f405bbd07cc6 6459818: Audio A-law and law decoder skip() method not implemented
serb
parents: 35684
diff changeset
   448
        @Override
f405bbd07cc6 6459818: Audio A-law and law decoder skip() method not implemented
serb
parents: 35684
diff changeset
   449
        public long skip(final long n) throws IOException {
f405bbd07cc6 6459818: Audio A-law and law decoder skip() method not implemented
serb
parents: 35684
diff changeset
   450
            // Implementation of this method assumes that we support
f405bbd07cc6 6459818: Audio A-law and law decoder skip() method not implemented
serb
parents: 35684
diff changeset
   451
            // encoding/decoding from/to 8/16 bits only
f405bbd07cc6 6459818: Audio A-law and law decoder skip() method not implemented
serb
parents: 35684
diff changeset
   452
            return encode ? super.skip(n * 2) / 2 : super.skip(n / 2) * 2;
f405bbd07cc6 6459818: Audio A-law and law decoder skip() method not implemented
serb
parents: 35684
diff changeset
   453
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    } // end class AlawCodecStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
} // end class ALAW