Example usage for java.nio FloatBuffer capacity

List of usage examples for java.nio FloatBuffer capacity

Introduction

In this page you can find the example usage for java.nio FloatBuffer capacity.

Prototype

public final int capacity() 

Source Link

Document

Returns the capacity of this buffer.

Usage

From source file:Main.java

public static FloatBuffer setupFloatBuffer(FloatBuffer preBuffer, float[] array) {

    if (preBuffer == null || preBuffer.capacity() < array.length) {
        preBuffer = createFloatBuffer(array.length * 2);
    } else {/*from  w w  w  .  ja v a  2 s.c o  m*/
        preBuffer.clear();
    }
    preBuffer.put(array);
    preBuffer.position(0);
    return preBuffer;
}

From source file:Main.java

public static FloatBuffer createFloatBuffer(FloatBuffer buffer) {
    FloatBuffer dest = createFloatBuffer(buffer.capacity());
    dest.clear();//from   w w  w .  j  av  a2  s . com
    dest.put(buffer);
    dest.position(0);
    return dest;
}

From source file:Main.java

public static FloatBuffer createFloatBuffer(FloatBuffer buf) {
    FloatBuffer dest = createFloatBuffer(buf.capacity());
    dest.clear();//w  w w .j  av a2 s .  c  om
    dest.put(buf);
    dest.position(0);
    return dest;
}

From source file:Main.java

/**
 * Convert from singed 16-bit PCM to 32-bit float PCM.
 * @param byteArray byte array//ww w.j av  a 2  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

private static String formatFloats(byte[] data) {
    FloatBuffer bb = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN).asFloatBuffer();

    StringBuilder sb = new StringBuilder(bb.capacity() * 3);

    while (bb.remaining() > 0) {
        sb.append(String.format("%.4f", bb.get()));
        sb.append(',');
        sb.append('\n');
    }/* ww w .  j  a v  a 2 s  .c  o  m*/

    return sb.toString();
}

From source file:Main.java

public static int[] arrayToBufferId(float[] arr) {

    int[] bufferId = new int[1];
    // let's generate
    glGenBuffers(1, bufferId, 0);/*from   ww  w.  jav a2s . c om*/

    FloatBuffer fb = arrayToBuffer(arr);
    // bind the two buffer
    glBindBuffer(GL_ARRAY_BUFFER, bufferId[0]);
    glBufferData(GL_ARRAY_BUFFER, fb.capacity() * 4, fb, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    return bufferId;
}

From source file:Main.java

/**
 * Creates a float array from the provided {@link FloatBuffer}.
 * //w  w w . j  a  va  2s .c o m
 * @param buffer {@link FloatBuffer} the data source.
 * @return float array containing the data of the buffer.
 */
public static float[] getFloatArrayFromBuffer(FloatBuffer buffer) {
    float[] array = null;
    if (buffer.hasArray()) {
        array = buffer.array();
    } else {
        buffer.rewind();
        array = new float[buffer.capacity()];
        buffer.get(array);
    }
    return array;
}

From source file:Main.java

public static FloatBuffer createFloatBuffer(final FloatBuffer reuseStore, final float... data) {
    if (data == null) {
        return null;
    }//from  ww  w .j a  v a2s .  co  m
    final FloatBuffer buff;
    if (reuseStore == null || reuseStore.capacity() != data.length) {
        buff = createFloatBuffer(data.length);
    } else {
        buff = reuseStore;
        buff.clear();
    }
    buff.clear();
    buff.put(data);
    buff.flip();
    return buff;
}

From source file:de.ailis.threedee.utils.BufferUtils.java

/**
 * Converts the specified float buffer to native endian and returns this new
 * buffer. If buffer is already in correct endian format then it is returned
 * right away.//from w ww  .  ja  va 2  s  .co m
 *
 * @param buffer
 *            The float buffer to convert
 * @return The converted float buffer or the source buffer if no conversion
 *         is needed
 */

public static FloatBuffer convertToNativeEndian(final FloatBuffer buffer) {
    if (buffer.order() == ByteOrder.nativeOrder())
        return buffer;

    if (log.isTraceEnabled())
        log.trace("Converting endianess of " + buffer.capacity() + " floats");

    final ByteBuffer bytes = ByteBuffer.allocateDirect(buffer.capacity());
    bytes.order(ByteOrder.nativeOrder());
    final FloatBuffer floats = bytes.asFloatBuffer();
    floats.put(buffer).rewind();
    return floats;
}

From source file:org.apache.sysml.runtime.matrix.data.LibMatrixNative.java

private static FloatBuffer toFloatBuffer(double[] input, ThreadLocal<FloatBuffer> buff, boolean copy) {
    //maintain thread-local buffer (resized on demand)
    FloatBuffer ret = buff.get();
    if (ret == null || ret.capacity() < input.length) {
        ret = ByteBuffer.allocateDirect(4 * input.length).order(ByteOrder.nativeOrder()).asFloatBuffer();
        buff.set(ret);/*from   www  . j av a  2s . c om*/
    }
    //copy to direct byte buffer
    final FloatBuffer ret2 = ret;
    if (copy) {
        IntStream.range(0, input.length).parallel().forEach(i -> ret2.put(i, (float) input[i]));
    }
    return ret2;
}