Example usage for java.nio ByteBuffer asFloatBuffer

List of usage examples for java.nio ByteBuffer asFloatBuffer

Introduction

In this page you can find the example usage for java.nio ByteBuffer asFloatBuffer.

Prototype

public abstract FloatBuffer asFloatBuffer();

Source Link

Document

Returns a float buffer which is based on the remaining content of this byte buffer.

Usage

From source file:Main.java

public static FloatBuffer toFloatBufferPositionZero(float[] values) {
    ByteBuffer vbb = ByteBuffer.allocateDirect(values.length * 4);
    vbb.order(ByteOrder.nativeOrder());
    FloatBuffer buffer = vbb.asFloatBuffer();
    buffer.put(values);/*from   w ww.  ja v a  2  s  . c  om*/
    buffer.position(0);
    return buffer;
}

From source file:Main.java

private static final FloatBuffer createDebugColors(int drawMode, int size) {
    ByteBuffer bb = ByteBuffer.allocateDirect(size * 4 * 4);
    bb.order(ByteOrder.nativeOrder());
    FloatBuffer colors = bb.asFloatBuffer();
    if (drawMode == GL10.GL_TRIANGLES) {
        for (int i = 0; i < size * 4; i++) {
            colors.put(triangleColors[i % triangleColors.length]);
        }/*from w  w  w  .  ja  v  a  2  s . c o m*/
    } else {
        for (int i = 0; i < size; i++) {
            colors.put(1f);
            colors.put(0f);
            colors.put(1f);
            colors.put(1f);
        }
    }
    colors.rewind();
    return colors;
}

From source file:Main.java

public static FloatBuffer floatArrayToBuffer(float[] floatArray) {
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(floatArray.length * 4);
    byteBuffer.order(ByteOrder.nativeOrder());
    FloatBuffer floatBuffer = byteBuffer.asFloatBuffer();
    floatBuffer.put(floatArray);//from   ww  w. ja v a 2 s .  com
    floatBuffer.position(0);
    return floatBuffer;
}

From source file:Main.java

public static FloatBuffer transportArrayToNativeBuffer(float[] fArray) {
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(fArray.length * 4);
    byteBuffer.order(ByteOrder.nativeOrder());
    return (FloatBuffer) byteBuffer.asFloatBuffer().put(fArray).position(0);
}

From source file:Main.java

/**
 * Creates a {@link FloatBuffer} based on the given data.
 *
 * @param data the data for the buffer//  w w  w .java2  s . c  om
 * @return the float buffer
 */
public static FloatBuffer createFloatBuffer(final float[] data) {
    ByteBuffer bbVertices = ByteBuffer.allocateDirect(data.length * 4);
    bbVertices.order(ByteOrder.nativeOrder());
    final FloatBuffer fBuffer = bbVertices.asFloatBuffer();
    fBuffer.put(data);
    fBuffer.position(0);
    return fBuffer;
}

From source file:Main.java

public static FloatBuffer toFloatBufferPositionZero(float[] values) {
    final ByteBuffer vbb = ByteBuffer.allocateDirect(values.length * 4);
    vbb.order(ByteOrder.nativeOrder());
    final FloatBuffer buffer = vbb.asFloatBuffer();
    buffer.put(values);/*from   w ww  . j  av a2 s .c o m*/
    buffer.position(0);
    return buffer;
}

From source file:Main.java

/**
 * Allocates a direct float buffer, and populates it with the float array data.
 *///from  w w w . ja  va  2  s.  c  o m
public static FloatBuffer createFloatBuffer(float[] coords) {
    // Allocate a direct ByteBuffer, using 4 bytes per float, and copy coords into it.
    ByteBuffer bb = ByteBuffer.allocateDirect(coords.length * SIZEOF_FLOAT);
    bb.order(ByteOrder.nativeOrder());
    FloatBuffer fb = bb.asFloatBuffer();
    fb.put(coords);
    fb.position(0);
    return fb;
}

From source file:Main.java

/**
 * Convert from singed 16-bit PCM to 32-bit float PCM.
 * @param byteArray byte array/*from  w w  w.ja  v a2  s.  c  o m*/
 * @return byte array
 */
public static byte[] convert16BitTo32Bit(final byte[] byteArray) {
    float[] audioDataF = shortToFloat(byteToShort(byteArray));
    for (int i = 0; i < audioDataF.length; i++) {
        audioDataF[i] /= 32768.0;
    }

    FloatBuffer fb = FloatBuffer.wrap(audioDataF);
    ByteBuffer byteBuffer = ByteBuffer.allocate(fb.capacity() * 4);
    byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
    byteBuffer.asFloatBuffer().put(fb);
    return byteBuffer.array();
}

From source file:Main.java

/**
 * create a Floatbuffer for a given Array
 * @param array/*from  w ww. j a va  2s .  c om*/
 * @return
 */
public static FloatBuffer createFloatBuffer(float[] array) {
    final int floatSize = Float.SIZE / 8;
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(array.length * floatSize);
    byteBuffer.order(ByteOrder.nativeOrder());
    FloatBuffer floatBuffer = byteBuffer.asFloatBuffer();
    floatBuffer.put(array);
    floatBuffer.position(0);
    return floatBuffer;
}

From source file:Main.java

/**
 * Make a FloatBuffer from an array of floats
 * /*from  w  w w  .j  a v  a 2s. c  om*/
 * @param f
 *            The array
 * @return the FloatBuffer
 */
public static FloatBuffer makeFloatBuffer(float[] f) {
    ByteBuffer bytBuffer = ByteBuffer.allocateDirect(f.length * 4);
    bytBuffer.order(ByteOrder.nativeOrder());

    FloatBuffer floatBuffer = bytBuffer.asFloatBuffer();
    floatBuffer.put(f);
    floatBuffer.position(0);

    return floatBuffer;
}