Java ByteBuffer to Float toFloat(AudioFormat format, ByteBuffer buf, float[] floatBuf)

Here you can find the source of toFloat(AudioFormat format, ByteBuffer buf, float[] floatBuf)

Description

to Float

License

BSD License

Declaration

public static int toFloat(AudioFormat format, ByteBuffer buf, float[] floatBuf) 

Method Source Code

//package com.java2s;
/**/*from  w  ww  . j  a  v  a2  s  .  c  o m*/
 * This class is part of JCodec ( www.jcodec.org ) This software is distributed
 * under FreeBSD License
 * 
 * @author Jay Codec
 * 
 */

import java.nio.ByteBuffer;
import javax.sound.sampled.AudioFormat;

public class Main {
    public final static float r16 = 1f / 32768f;
    public final static float r24 = 1f / 8388608f;

    public static int toFloat(AudioFormat format, ByteBuffer buf, float[] floatBuf) {
        if (format.isBigEndian()) {
            if (format.getSampleSizeInBits() == 16) {
                return toFloat16BE(buf, floatBuf);
            } else {
                return toFloat24BE(buf, floatBuf);
            }
        } else {
            if (format.getSampleSizeInBits() == 16) {
                return toFloat16LE(buf, floatBuf);
            } else {
                return toFloat24LE(buf, floatBuf);
            }
        }
    }

    private static int toFloat16BE(ByteBuffer buf, float[] out) {
        int samples = 0;
        while (buf.remaining() >= 2 && samples < out.length) {
            out[samples++] = r16 * (short) (((buf.get() & 0xff) << 8) | (buf.get() & 0xff));
        }
        return samples;
    }

    private static int toFloat24BE(ByteBuffer buf, float[] out) {
        int samples = 0;
        while (buf.remaining() >= 3 && samples < out.length) {
            out[samples++] = r24
                    * ((((buf.get() & 0xff) << 24) | ((buf.get() & 0xff) << 16) | ((buf.get() & 0xff) << 8)) >> 8);
        }
        return samples;
    }

    private static int toFloat16LE(ByteBuffer buf, float[] out) {
        int samples = 0;
        while (buf.remaining() >= 2 && samples < out.length) {
            out[samples++] = r16 * (short) ((buf.get() & 0xff) | ((buf.get() & 0xff) << 8));
        }
        return samples;
    }

    private static int toFloat24LE(ByteBuffer buf, float[] out) {
        int samples = 0;
        while (buf.remaining() >= 3 && samples < out.length) {
            out[samples++] = r24
                    * ((((buf.get() & 0xff) << 8) | ((buf.get() & 0xff) << 16) | ((buf.get() & 0xff) << 24)) >> 8);
        }
        return samples;
    }
}

Related

  1. readFloat(ByteBuffer buffer)
  2. readFloatArray(ByteBuffer bb, int length)
  3. readFloats(final ByteBuffer bb, final int length)
  4. toFloat(ByteBuffer buffer)
  5. toFloat(ByteBuffer bytes)
  6. toFloat(ByteBuffer value)
  7. toFloat16BE(ByteBuffer buf, FloatBuffer out)