jdk/src/share/classes/com/sun/media/sound/AlawCodec.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8526 398796acdbd5
child 18215 b2afd66ce6db
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8526
diff changeset
     2
 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.media.sound;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.sound.sampled.AudioFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.sound.sampled.AudioSystem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.sound.sampled.AudioInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * A-law encodes linear data, and decodes a-law data to linear data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author Kara Kytle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class AlawCodec extends SunCodec {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /* Tables used for A-law decoding */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    final static byte ALAW_TABH[] = new byte[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    final static byte ALAW_TABL[] = new byte[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final AudioFormat.Encoding[] alawEncodings = { AudioFormat.Encoding.ALAW, 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 input    = i ^ 0x55;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            int mantissa = (input & 0xf ) << 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            int segment  = (input & 0x70) >> 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            int value    = mantissa+8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            if(segment>=1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                value+=0x100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            if(segment>1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                value <<= (segment -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            if( (input & 0x80)==0 )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                value = -value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            ALAW_TABL[i] = (byte)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            ALAW_TABH[i] = (byte)(value>>8);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Constructs a new ALAW codec object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public AlawCodec() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        super(alawEncodings, alawEncodings);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    // NEW CODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public AudioFormat.Encoding[] getTargetEncodings(AudioFormat sourceFormat){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if( sourceFormat.getEncoding().equals( AudioFormat.Encoding.PCM_SIGNED )) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            if( sourceFormat.getSampleSizeInBits() == 16 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                AudioFormat.Encoding enc[] = new AudioFormat.Encoding[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                enc[0] = AudioFormat.Encoding.ALAW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                return enc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                return new AudioFormat.Encoding[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        } else if( sourceFormat.getEncoding().equals( AudioFormat.Encoding.ALAW ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            if( sourceFormat.getSampleSizeInBits() == 8 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                AudioFormat.Encoding enc[] = new AudioFormat.Encoding[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                enc[0] = AudioFormat.Encoding.PCM_SIGNED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                return enc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                return new AudioFormat.Encoding[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            return new AudioFormat.Encoding[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public AudioFormat[] getTargetFormats(AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat){
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if( sourceEncoding.equals( targetEncoding ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            return sourceStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            AudioFormat targetFormat = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            if( !isConversionSupported(targetEncoding,sourceStream.getFormat()) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                throw new IllegalArgumentException("Unsupported conversion: " + sourceStream.getFormat().toString() + " to " + targetEncoding.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            if( sourceEncoding.equals( AudioFormat.Encoding.ALAW ) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                targetEncoding.equals( AudioFormat.Encoding.PCM_SIGNED ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                targetFormat = new AudioFormat( targetEncoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                                                sourceFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                                                16,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                                                sourceFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                                                2*sourceFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                                                sourceFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                                                sourceFormat.isBigEndian());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            } else if( sourceEncoding.equals( AudioFormat.Encoding.PCM_SIGNED ) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                       targetEncoding.equals( AudioFormat.Encoding.ALAW ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                targetFormat = new AudioFormat( targetEncoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                                                sourceFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                                                8,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                                                sourceFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                                                sourceFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                                                sourceFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                                                false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                throw new IllegalArgumentException("Unsupported conversion: " + sourceStream.getFormat().toString() + " to " + targetEncoding.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            return getAudioInputStream( targetFormat, sourceStream );
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * use old code...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public AudioInputStream getAudioInputStream(AudioFormat targetFormat, AudioInputStream sourceStream){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        return getConvertedStream( targetFormat, sourceStream );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    // OLD CODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Opens the codec with the specified parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @param stream stream from which data to be processed should be read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @param outputFormat desired data format of the stream after processing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @return stream from which processed data may be read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @throws IllegalArgumentException if the format combination supplied is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * not supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /*  public AudioInputStream getConvertedStream(AudioFormat outputFormat, AudioInputStream stream) { */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    private AudioInputStream getConvertedStream(AudioFormat outputFormat, AudioInputStream stream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        AudioInputStream cs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        AudioFormat inputFormat = stream.getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if( inputFormat.matches(outputFormat) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            cs = stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            cs = (AudioInputStream) (new AlawCodecStream(stream, outputFormat));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        return cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Obtains the set of output formats supported by the codec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * given a particular input format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * If no output formats are supported for this input format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * returns an array of length 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @return array of supported output formats.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /*  public AudioFormat[] getOutputFormats(AudioFormat inputFormat) { */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    private AudioFormat[] getOutputFormats(AudioFormat inputFormat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        Vector formats = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        AudioFormat format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if ( AudioFormat.Encoding.PCM_SIGNED.equals(inputFormat.getEncoding())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            format = new AudioFormat(AudioFormat.Encoding.ALAW,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                                     inputFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                                     8,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                                     inputFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                                     inputFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                                     inputFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                                     false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            formats.addElement(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (AudioFormat.Encoding.ALAW.equals(inputFormat.getEncoding())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                                     inputFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                                     16,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                                     inputFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                                     inputFormat.getChannels()*2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                                     inputFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                                     false );
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,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                                     inputFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                                     16,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                                     inputFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                                     inputFormat.getChannels()*2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                                     inputFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                                     true );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            formats.addElement(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        AudioFormat[] formatArray = new AudioFormat[formats.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        for (int i = 0; i < formatArray.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            formatArray[i] = (AudioFormat)(formats.elementAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        return formatArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    class AlawCodecStream extends AudioInputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
8526
398796acdbd5 6938426: Concurrency bug in ALAW encoder causes random bursts of static/noise in output.
amenkov
parents: 5506
diff changeset
   261
        // 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
   262
        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
   263
        private byte tempBuffer [] = null;
398796acdbd5 6938426: Concurrency bug in ALAW encoder causes random bursts of static/noise in output.
amenkov
parents: 5506
diff changeset
   264
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
         * True to encode to a-law, false to decode to linear
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        boolean encode = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        AudioFormat encodeFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        AudioFormat decodeFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        byte tabByte1[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        byte tabByte2[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        int highByte = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        int lowByte  = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        AlawCodecStream(AudioInputStream stream, AudioFormat outputFormat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            super(stream, outputFormat, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            AudioFormat inputFormat = stream.getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            // throw an IllegalArgumentException if not ok
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            if ( ! (isConversionSupported(outputFormat, inputFormat)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                throw new IllegalArgumentException("Unsupported conversion: " + inputFormat.toString() + " to " + outputFormat.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            //$$fb 2002-07-18: fix for 4714846: JavaSound ULAW (8-bit) encoder erroneously depends on endian-ness
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            boolean PCMIsBigEndian;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            // determine whether we are encoding or decoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            if (AudioFormat.Encoding.ALAW.equals(inputFormat.getEncoding())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                encode = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                encodeFormat = inputFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                decodeFormat = outputFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                PCMIsBigEndian = outputFormat.isBigEndian();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                encode = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                encodeFormat = outputFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                decodeFormat = inputFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                PCMIsBigEndian = inputFormat.isBigEndian();
8526
398796acdbd5 6938426: Concurrency bug in ALAW encoder causes random bursts of static/noise in output.
amenkov
parents: 5506
diff changeset
   304
                tempBuffer = new byte[tempBufferSize];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            if (PCMIsBigEndian) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                tabByte1 = ALAW_TABH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                tabByte2 = ALAW_TABL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                highByte = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                lowByte  = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                tabByte1 = ALAW_TABL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                tabByte2 = ALAW_TABH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                highByte = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                lowByte  = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            // set the AudioInputStream length in frames if we know it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            if (stream instanceof AudioInputStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                frameLength = ((AudioInputStream)stream).getFrameLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            // set framePos to zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            framePos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            frameSize = inputFormat.getFrameSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            if( frameSize==AudioSystem.NOT_SPECIFIED ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                frameSize=1;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
         * $$jb 2/23/99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
         * Used to determine segment number in aLaw encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        private short search(short val, short table[], short size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            for(short i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                if (val <= table[i]) { return i; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
         * Note that this won't actually read anything; must read in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
         * two-byte units.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            byte[] b = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            return (int)read(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        public int read(byte[] b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            return read(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        public int read(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            // don't read fractional frames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            if( len%frameSize != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                len -= (len%frameSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            if (encode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                short QUANT_MASK = 0xF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                short SEG_SHIFT = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                short mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                short seg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                int adj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                short sample;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                byte enc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                int readCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                int currentPos = off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                int readLeft = len*2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                int readLen = ( (readLeft>tempBufferSize) ? tempBufferSize : readLeft );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                while ((readCount = super.read(tempBuffer,0,readLen))>0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    for (i = 0; i < readCount; i+=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                        /* Get the sample from the tempBuffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                        sample = (short)(( (tempBuffer[i + highByte]) << 8) & 0xFF00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                        sample |= (short)( (tempBuffer[i + lowByte]) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                        if(sample >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                            mask = 0xD5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                            mask = 0x55;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                            sample = (short)(-sample - 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                        /* Convert the scaled magnitude to segment number. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                        seg = search(sample, seg_end, (short) 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                         * Combine the sign, segment, quantization bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                        if (seg >= 8) {  /* out of range, return maximum value. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                            enc = (byte) (0x7F ^ mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                            enc = (byte) (seg << SEG_SHIFT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                            if(seg < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                                enc |= (byte) ( (sample >> 4) & QUANT_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                                enc |= (byte) ( (sample >> (seg + 3)) & QUANT_MASK );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                            enc ^= mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                        /* Now put the encoded sample where it belongs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                        b[currentPos] = enc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                        currentPos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    /* And update pointers and counters for next iteration */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    readLeft -= readCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    readLen = ( (readLeft>tempBufferSize) ? tempBufferSize : readLeft );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                if( currentPos==off && readCount<0 ) {  // EOF or error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                    return readCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                return (currentPos - off);  /* Number of bytes written to new buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                int readLen = len/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                int readOffset = off + len/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                int readCount = super.read(b, readOffset, readLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                for (i = off; i < (off + (readCount*2)); i+=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                    b[i]        = (byte)tabByte1[b[readOffset] & 0xFF];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    b[i+1]      = (byte)tabByte2[b[readOffset] & 0xFF];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                    readOffset++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                if( readCount<0 ) {             // EOF or error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    return readCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                return (i - off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    } // end class AlawCodecStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
} // end class ALAW