All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sun.media.sound.PCMtoPCMCodec Maven / Gradle / Ivy

/*
 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package com.sun.media.sound;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import java.io.IOException;
import java.util.Vector;


/**
 * Converts among signed/unsigned and little/big endianness of sampled.
 *
 * @author Jan Borgersen
 */
public final class PCMtoPCMCodec extends SunCodec {


    private static final AudioFormat.Encoding[] inputEncodings = {
        AudioFormat.Encoding.PCM_SIGNED,
        AudioFormat.Encoding.PCM_UNSIGNED,
    };

    private static final AudioFormat.Encoding[] outputEncodings = {
        AudioFormat.Encoding.PCM_SIGNED,
        AudioFormat.Encoding.PCM_UNSIGNED,
    };



    private static final int tempBufferSize = 64;
    private byte tempBuffer [] = null;

    /**
     * Constructs a new PCMtoPCM codec object.
     */
    public PCMtoPCMCodec() {

        super( inputEncodings, outputEncodings);
    }

    // NEW CODE


    /**
     */
    public AudioFormat.Encoding[] getTargetEncodings(AudioFormat sourceFormat){

        if( sourceFormat.getEncoding().equals( AudioFormat.Encoding.PCM_SIGNED ) ||
            sourceFormat.getEncoding().equals( AudioFormat.Encoding.PCM_UNSIGNED ) ) {

                AudioFormat.Encoding encs[] = new AudioFormat.Encoding[2];
                encs[0] = AudioFormat.Encoding.PCM_SIGNED;
                encs[1] = AudioFormat.Encoding.PCM_UNSIGNED;
                return encs;
            } else {
                return new AudioFormat.Encoding[0];
            }
    }


    /**
     */
    public AudioFormat[] getTargetFormats(AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat){

        // filter out targetEncoding from the old getOutputFormats( sourceFormat ) method

        AudioFormat[] formats = getOutputFormats( sourceFormat );
        Vector newFormats = new Vector();
        for(int i=0; i= 0) ? (byte)(0x80 | tempbyte) : (byte)(0x7F & tempbyte);
                    temp = (int) tempbyte & 0xf;

                    return temp;

                } else {
                    // $$jb: what to return here?
                    throw new IOException("cannot read a single byte if frame size > 1");
                }
            } else {
                throw new IOException("cannot read a single byte if frame size > 1");
            }
        }


        public int read(byte[] b) throws IOException {

            return read(b, 0, b.length);
        }

        public int read(byte[] b, int off, int len) throws IOException {


            int i;

            // don't read fractional frames
            if ( len%frameSize != 0 ) {
                len -= (len%frameSize);
            }
            // don't read past our own set length
            if( (frameLength!=AudioSystem.NOT_SPECIFIED) && ( (len/frameSize) >(frameLength-framePos)) ) {
                len = (int)(frameLength-framePos) * frameSize;
            }

            int readCount = super.read(b, off, len);
            byte tempByte;

            if(readCount<0) {   // EOF or error
                return readCount;
            }

            // now do the conversions

            switch( conversionType ) {

            case PCM_SWITCH_SIGNED_8BIT:
                switchSigned8bit(b,off,len,readCount);
                break;

            case PCM_SWITCH_ENDIAN:
                switchEndian(b,off,len,readCount);
                break;

            case PCM_SWITCH_SIGNED_LE:
                switchSignedLE(b,off,len,readCount);
                break;

            case PCM_SWITCH_SIGNED_BE:
                switchSignedBE(b,off,len,readCount);
                break;

            case PCM_UNSIGNED_LE2SIGNED_BE:
            case PCM_SIGNED_LE2UNSIGNED_BE:
                switchSignedLE(b,off,len,readCount);
                switchEndian(b,off,len,readCount);
                break;

            case PCM_UNSIGNED_BE2SIGNED_LE:
            case PCM_SIGNED_BE2UNSIGNED_LE:
                switchSignedBE(b,off,len,readCount);
                switchEndian(b,off,len,readCount);
                break;

            default:
                                // do nothing
            }

            // we've done the conversion, just return the readCount
            return readCount;

        }

        private void switchSigned8bit(byte[] b, int off, int len, int readCount) {

            for(int i=off; i < (off+readCount); i++) {
                b[i] = (b[i] >= 0) ? (byte)(0x80 | b[i]) : (byte)(0x7F & b[i]);
            }
        }

        private void switchSignedBE(byte[] b, int off, int len, int readCount) {

            for(int i=off; i < (off+readCount); i+= sampleSizeInBytes ) {
                b[i] = (b[i] >= 0) ? (byte)(0x80 | b[i]) : (byte)(0x7F & b[i]);
            }
        }

        private void switchSignedLE(byte[] b, int off, int len, int readCount) {

            for(int i=(off+sampleSizeInBytes-1); i < (off+readCount); i+= sampleSizeInBytes ) {
                b[i] = (b[i] >= 0) ? (byte)(0x80 | b[i]) : (byte)(0x7F & b[i]);
            }
        }

        private void switchEndian(byte[] b, int off, int len, int readCount) {

            if(sampleSizeInBytes == 2) {
                for(int i=off; i < (off+readCount); i += sampleSizeInBytes ) {
                    byte temp;
                    temp = b[i];
                    b[i] = b[i+1];
                    b[i+1] = temp;
                }
            }
        }



    } // end class PCMtoPCMCodecStream

} // end class PCMtoPCMCodec




© 2015 - 2025 Weber Informatics LLC | Privacy Policy