Java Code Examples for java.nio.ShortBuffer#put()

The following examples show how to use java.nio.ShortBuffer#put() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BoundingSphereDebug.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * sets the indices for rendering the sphere.
 */
private void setIndexData() {

    // allocate connectivity
    int nbSegments = (radialSamples) * 3;

    ShortBuffer idxBuf = BufferUtils.createShortBuffer(2 * nbSegments);
    setBuffer(Type.Index, 2, idxBuf);

    int idx = 0;
    int segDone = 0;
    while (segDone < nbSegments) {
        idxBuf.put((short) idx);
        idxBuf.put((short) (idx + 1));
        idx++;
        segDone++;
        if (segDone == radialSamples || segDone == radialSamples * 2) {
            idx++;
        }

    }

}
 
Example 2
Source File: Wave.java    From tribaltrouble with GNU General Public License v2.0 6 votes vote down vote up
private final ByteBuffer directWaveOrder(byte[] buffer, int bits) {
	ByteBuffer src = ByteBuffer.wrap(buffer);
	src.order(ByteOrder.LITTLE_ENDIAN);
	ByteBuffer dest = ByteBuffer.allocateDirect(buffer.length);
	dest.order(ByteOrder.nativeOrder());

	if (bits == 16) {
		ShortBuffer dest_short = dest.asShortBuffer();
		ShortBuffer src_short = src.asShortBuffer();
		while (src_short.hasRemaining())
			dest_short.put(src_short.get());
	} else {
		while (src.hasRemaining())
			dest.put(src.get());
	}
	dest.rewind();
	return dest;
}
 
Example 3
Source File: AudioChannel.java    From Mp4Composer-android with MIT License 6 votes vote down vote up
private long drainOverflow(final ShortBuffer outBuff) {
    final ShortBuffer overflowBuff = overflowBuffer.data;
    final int overflowLimit = overflowBuff.limit();
    final int overflowSize = overflowBuff.remaining();

    final long beginPresentationTimeUs = overflowBuffer.presentationTimeUs +
            sampleCountToDurationUs(overflowBuff.position(), inputSampleRate, outputChannelCount);

    outBuff.clear();
    // Limit overflowBuff to outBuff's capacity
    overflowBuff.limit(outBuff.capacity());
    // Load overflowBuff onto outBuff
    outBuff.put(overflowBuff);

    if (overflowSize >= outBuff.capacity()) {
        // Overflow fully consumed - Reset
        overflowBuff.clear().limit(0);
    } else {
        // Only partially consumed - Keep position & restore previous limit
        overflowBuff.limit(overflowLimit);
    }

    return beginPresentationTimeUs;
}
 
Example 4
Source File: BufferUtils.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Creates a new ShortBuffer with the same contents as the given
 * ShortBuffer. The new ShortBuffer is separate from the old one and changes
 * are not reflected across. If you want to reflect changes, consider using
 * Buffer.duplicate().
 *
 * @param buf
 *            the ShortBuffer to copy
 * @return the copy
 */
public static ShortBuffer clone(ShortBuffer buf) {
    if (buf == null) {
        return null;
    }
    buf.rewind();

    ShortBuffer copy;
    if (isDirect(buf)) {
        copy = createShortBuffer(buf.limit());
    } else {
        copy = ShortBuffer.allocate(buf.limit());
    }
    copy.put(buf);

    return copy;
}
 
Example 5
Source File: AudioChannel.java    From GPUVideo-android with MIT License 6 votes vote down vote up
private long drainOverflow(final ShortBuffer outBuff) {
    final ShortBuffer overflowBuff = overflowBuffer.data;
    final int overflowLimit = overflowBuff.limit();
    final int overflowSize = overflowBuff.remaining();

    final long beginPresentationTimeUs = overflowBuffer.presentationTimeUs +
            sampleCountToDurationUs(overflowBuff.position(), inputSampleRate, outputChannelCount);

    outBuff.clear();
    // Limit overflowBuff to outBuff's capacity
    overflowBuff.limit(outBuff.capacity());
    // Load overflowBuff onto outBuff
    outBuff.put(overflowBuff);

    if (overflowSize >= outBuff.capacity()) {
        // Overflow fully consumed - Reset
        overflowBuff.clear().limit(0);
    } else {
        // Only partially consumed - Keep position & restore previous limit
        overflowBuff.limit(overflowLimit);
    }

    return beginPresentationTimeUs;
}
 
Example 6
Source File: Buffers.java    From JglTF with MIT License 5 votes vote down vote up
/**
 * Create a new direct byte buffer with native byte order that has the
 * same contents as the given short buffer.
 *  
 * @param buffer The input buffer
 * @return The new byte buffer
 */
public static ByteBuffer createByteBufferFrom(ShortBuffer buffer)
{
    ByteBuffer byteBuffer = 
        ByteBuffer.allocateDirect(buffer.capacity() * Short.BYTES);
    ShortBuffer shortBuffer = 
        byteBuffer.order(ByteOrder.nativeOrder()).asShortBuffer();
    shortBuffer.put(buffer.slice());
    return byteBuffer;
}
 
Example 7
Source File: PMDLoaderGLSLSkinning2.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
PMDSkinMesh createSkinMesh(PMDMaterial pmdMaterial) {
        boolean textureFlag = true;
        if (pmdMaterial.getTextureFileName().length() == 0) {
            textureFlag = false;
        }
        PMDSkinMesh mesh = new PMDSkinMesh();
        mesh.setMode(Mesh.Mode.Triangles);
        mesh.setBuffer(skinvb);
        mesh.setSkinvb2(skinvb2);
        mesh.setBuffer(skinnb);
//        mesh.setSkinnb2(skinnb2);
        if (textureFlag) {
            mesh.setBuffer(skintb);
        }
        mesh.setBuffer(skinbib);
        mesh.setBuffer(skinwb);
        VertexBuffer ib = new VertexBuffer(VertexBuffer.Type.Index);
        ShortBuffer isb = meshConverter.getSkinMeshData().indexShortBufferMap.get(pmdMaterial);
        ib.setupData(VertexBuffer.Usage.Static, 1, VertexBuffer.Format.UnsignedShort, isb);
        mesh.setBuffer(ib);
        mesh.setBoneIndexArray(skinIndexArray);
        ShortBuffer boneIndexBuffer = BufferUtils.createShortBuffer(skinIndexArray.length);
        for(int i=0;i<skinIndexArray.length;i++) {
            boneIndexBuffer.put((short)skinIndexArray[i]);
        }
        mesh.setBoneIndexBuffer(boneIndexBuffer);
        mesh.setBoneMatrixArray(new Matrix4f[skinIndexArray.length]);
        for (int i = 0; i < mesh.getBoneMatrixArray().length; i++) {
            mesh.getBoneMatrixArray()[i] = new Matrix4f();
            mesh.getBoneMatrixArray()[i].loadIdentity();
        }
        FloatBuffer boneMatrixBuffer = BufferUtils.createFloatBuffer(skinIndexArray.length * 16);
        mesh.setBoneMatrixBuffer(boneMatrixBuffer);
        return mesh;
    }
 
Example 8
Source File: BufferTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private void testBuffersIndependentLimit(ByteBuffer b) {
    CharBuffer c = b.asCharBuffer();
    b.limit(b.capacity()); c.put(1, (char)1);
    b.limit(0);  c.put(1, (char)1);

    b.limit(b.capacity());
    ShortBuffer s = b.asShortBuffer();
    b.limit(b.capacity()); s.put(1, (short)1);
    b.limit(0);  s.put(1, (short)1);

    b.limit(b.capacity());
    IntBuffer i = b.asIntBuffer();
    i.put(1, (int)1);
    b.limit(0);  i.put(1, (int)1);

    b.limit(b.capacity());
    LongBuffer l = b.asLongBuffer();
    l.put(1, (long)1);
    b.limit(0);  l.put(1, (long)1);

    b.limit(b.capacity());
    FloatBuffer f = b.asFloatBuffer();
    f.put(1, (float)1);
    b.limit(0);  f.put(1, (float)1);

    b.limit(b.capacity());
    DoubleBuffer d = b.asDoubleBuffer();
    d.put(1, (double)1);
    b.limit(0);  d.put(1, (double)1);
}
 
Example 9
Source File: GLFace.java    From codeexamples-android with Eclipse Public License 1.0 5 votes vote down vote up
public void putIndices(ShortBuffer buffer) {
	int last = mVertexList.size() - 1;

	GLVertex v0 = mVertexList.get(0);
	GLVertex vn = mVertexList.get(last);
	
	// push triangles into the buffer
	for (int i = 1; i < last; i++) {
		GLVertex v1 = mVertexList.get(i);
		buffer.put(v0.index);
		buffer.put(v1.index);
		buffer.put(vn.index);
		v0 = v1;
	}
}
 
Example 10
Source File: PcmVolumeProcessor.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
private void applyCurrentVolume(ShortBuffer buffer) {
  if (currentVolume == 100) {
    return;
  }

  int endOffset = buffer.limit();

  for (int i = buffer.position(); i < endOffset; i++) {
    int value = buffer.get(i) * integerMultiplier / 10000;
    buffer.put(i, (short) Math.max(-32767, Math.min(32767, value)));
  }
}
 
Example 11
Source File: BufferUtils.java    From aion-germany with GNU General Public License v3.0 5 votes vote down vote up
public static ShortBuffer ensureLargeEnough(ShortBuffer buffer, int required) {
	if (buffer == null || (buffer.remaining() < required)) {
		int position = (buffer != null ? buffer.position() : 0);
		ShortBuffer newVerts = createShortBuffer(position + required);
		if (buffer != null) {
			buffer.rewind();
			newVerts.put(buffer);
			newVerts.position(position);
		}
		buffer = newVerts;
	}
	return buffer;
}
 
Example 12
Source File: BufferUtils.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static ShortBuffer createShortBuffer(short... data) {
    if (data == null) {
        return null;
    }
    ShortBuffer buff = createShortBuffer(data.length);
    buff.clear();
    buff.put(data);
    buff.flip();
    return buff;
}
 
Example 13
Source File: AudioRemixer.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public void remix(final ShortBuffer inSBuff, final ShortBuffer outSBuff) {
    // Down-mix stereo to mono
    // Viktor Toth's algorithm -
    // See: http://www.vttoth.com/CMS/index.php/technical-notes/68
    //      http://stackoverflow.com/a/25102339
    final int inRemaining = inSBuff.remaining() / 2;
    final int outSpace = outSBuff.remaining();

    final int samplesToBeProcessed = Math.min(inRemaining, outSpace);
    for (int i = 0; i < samplesToBeProcessed; ++i) {
        // Convert to unsigned
        final int a = inSBuff.get() + SIGNED_SHORT_LIMIT;
        final int b = inSBuff.get() + SIGNED_SHORT_LIMIT;
        int m;
        // Pick the equation
        if ((a < SIGNED_SHORT_LIMIT) || (b < SIGNED_SHORT_LIMIT)) {
            // Viktor's first equation when both sources are "quiet"
            // (i.e. less than middle of the dynamic range)
            m = a * b / SIGNED_SHORT_LIMIT;
        } else {
            // Viktor's second equation when one or both sources are loud
            m = 2 * (a + b) - (a * b) / SIGNED_SHORT_LIMIT - UNSIGNED_SHORT_MAX;
        }
        // Convert output back to signed short
        if (m == UNSIGNED_SHORT_MAX + 1) m = UNSIGNED_SHORT_MAX;
        outSBuff.put((short) (m - SIGNED_SHORT_LIMIT));
    }
}
 
Example 14
Source File: AudioRemixer.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void remix(final ShortBuffer inSBuff, final ShortBuffer outSBuff) {
    // Up-mix mono to stereo
    final int inRemaining = inSBuff.remaining();
    final int outSpace = outSBuff.remaining() / 2;

    final int samplesToBeProcessed = Math.min(inRemaining, outSpace);
    for (int i = 0; i < samplesToBeProcessed; ++i) {
        final short inSample = inSBuff.get();
        outSBuff.put(inSample);
        outSBuff.put(inSample);
    }
}
 
Example 15
Source File: AudioRemixer.java    From EZFilter with MIT License 4 votes vote down vote up
@Override
public void remix(final ShortBuffer inSBuff, final ShortBuffer outSBuff) {
    // Passthrough
    outSBuff.put(inSBuff);
}
 
Example 16
Source File: VertexBuffer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Copies a sequence of elements of data from this <code>VertexBuffer</code>
 * to the given output VertexBuffer.
 * 
 * @param inIndex The input element index
 * @param outVb The buffer to copy to
 * @param outIndex The output element index
 * @param len The number of elements to copy
 * 
 * @throws IllegalArgumentException If the formats of the buffers do not
 * match.
 */
public void copyElements(int inIndex, VertexBuffer outVb, int outIndex, int len){
    if (outVb.format != format || outVb.components != components)
        throw new IllegalArgumentException("Buffer format mismatch. Cannot copy");

    int inPos  = inIndex  * components;
    int outPos = outIndex * components;
    int elementSz = components;
    if (format == Format.Half){
        // because half is stored as bytebuf but its 2 bytes long
        inPos *= 2;
        outPos *= 2;
        elementSz *= 2;
    }

    data.clear();
    outVb.data.clear();

    switch (format){
        case Byte:
        case UnsignedByte:
        case Half:
            ByteBuffer bin = (ByteBuffer) data;
            ByteBuffer bout = (ByteBuffer) outVb.data;
            bin.position(inPos).limit(inPos + elementSz);
            bout.position(outPos).limit(outPos + elementSz);
            bout.put(bin);
            break;
        case Short:
        case UnsignedShort:
            ShortBuffer sin = (ShortBuffer) data;
            ShortBuffer sout = (ShortBuffer) outVb.data;
            sin.position(inPos).limit(inPos + elementSz);
            sout.position(outPos).limit(outPos + elementSz);
            sout.put(sin);
            break;
        case Int:
        case UnsignedInt:
            IntBuffer iin = (IntBuffer) data;
            IntBuffer iout = (IntBuffer) outVb.data;
            iin.position(inPos).limit(inPos + elementSz);
            iout.position(outPos).limit(outPos + elementSz);
            iout.put(iin);
            break;
        case Float:
            FloatBuffer fin = (FloatBuffer) data;
            FloatBuffer fout = (FloatBuffer) outVb.data;
            fin.position(inPos).limit(inPos + elementSz);
            fout.position(outPos).limit(outPos + elementSz);
            fout.put(fin);
            break;
        default:
            throw new UnsupportedOperationException("Unrecognized buffer format: "+format);
    }

    data.clear();
    outVb.data.clear();
}
 
Example 17
Source File: GLMesh.java    From WraithEngine with Apache License 2.0 4 votes vote down vote up
@Override
public void update(VertexData vertexData)
{
    if (isDisposed())
        throw new IllegalStateException("Mesh already disposed!");

    FloatBuffer vertexBuffer = BufferUtils.createFloatBuffer(vertexData.getDataArray().length);
    vertexBuffer.put(vertexData.getDataArray());
    vertexBuffer.flip();

    ShortBuffer indexBuffer = BufferUtils.createShortBuffer(vertexData.getTriangles().length);
    indexBuffer.put(vertexData.getTriangles());
    indexBuffer.flip();

    if (!created)
    {
        vaoId = opengl.generateVertexArray();
        vboId = opengl.generateBuffer();
        indexId = opengl.generateBuffer();
    }

    bindStates.bindVao(vaoId);

    bindStates.bindBuffer(false, vboId);
    opengl.uploadBufferData(vertexBuffer);

    int stride = vertexData.getVertexByteSize();
    int offset = 0;

    ShaderAttributes attributes = vertexData.getAttributeSizes();
    for (int i = 0; i < attributes.getCount(); i++)
    {
        opengl.setVertexAttributePointer(i, attributes.getAttributeSize(i), stride, offset);
        offset += attributes.getAttributeSize(i) * 4;
    }

    bindStates.bindBuffer(true, indexId);
    opengl.uploadBufferData(indexBuffer);

    created = true;
    indexCount = vertexData.getTriangles().length;
}
 
Example 18
Source File: MDDome3D.java    From MD360Player4Android with Apache License 2.0 4 votes vote down vote up
public static void generateDome(float radius, int sectors, float degreeY, boolean isUpper, MDDome3D object3D) {
    final float PI = (float) Math.PI;
    final float PI_2 = (float) (Math.PI / 2);

    float percent = degreeY / 360;
    int rings = sectors >> 1;

    float R = 1f/rings;
    float S = 1f/sectors;
    short r, s;
    float x, y, z;

    int lenRings = (int) (rings * percent) + 1;
    int lenSectors = sectors + 1;
    int numPoint = lenRings * lenSectors;

    float[] vertexs = new float[numPoint * 3];
    float[] texcoords = new float[numPoint * 2];
    short[] indices = new short[numPoint * 6];

    int upper = isUpper ? 1 : -1;

    int t = 0, v = 0;
    for(r = 0; r < lenRings; r++) {
        for(s = 0; s < lenSectors; s++) {
            x = (float) (Math.cos( 2 * PI * s * S ) * Math.sin( PI * r * R )) * upper;
            y = (float) Math.sin( -PI_2 + PI * r * R ) * -upper;
            z = (float) (Math.sin( 2 * PI * s * S ) * Math.sin( PI * r * R ));

            float a = (float) (Math.cos( 2 * PI * s * S) * r * R / percent)/2.0f + 0.5f;
            float b = (float) (Math.sin( 2 * PI * s * S) * r * R / percent)/2.0f + 0.5f;

            texcoords[t++] = b;
            texcoords[t++] = a;

            vertexs[v++] = x * radius;
            vertexs[v++] = y * radius;
            vertexs[v++] = z * radius;
        }
    }

    int counter = 0;
    for(r = 0; r < lenRings - 1; r++){
        for(s = 0; s < lenSectors - 1; s++) {
            indices[counter++] = (short) (r * lenSectors + s);       //(a)
            indices[counter++] = (short) ((r+1) * lenSectors + (s));    //(b)
            indices[counter++] = (short) ((r) * lenSectors + (s+1));  // (c)
            indices[counter++] = (short) ((r) * lenSectors + (s+1));  // (c)
            indices[counter++] = (short) ((r+1) * lenSectors + (s));    //(b)
            indices[counter++] = (short) ((r+1) * lenSectors + (s+1));  // (d)
        }
    }

    // initialize vertex byte buffer for shape coordinates
    ByteBuffer bb = ByteBuffer.allocateDirect(
            // (# of coordinate values * 4 bytes per float)
            vertexs.length * 4);
    bb.order(ByteOrder.nativeOrder());
    FloatBuffer vertexBuffer = bb.asFloatBuffer();
    vertexBuffer.put(vertexs);
    vertexBuffer.position(0);

    // initialize vertex byte buffer for shape coordinates
    ByteBuffer cc = ByteBuffer.allocateDirect(
            texcoords.length * 4);
    cc.order(ByteOrder.nativeOrder());
    FloatBuffer texBuffer = cc.asFloatBuffer();
    texBuffer.put(texcoords);
    texBuffer.position(0);

    // initialize byte buffer for the draw list
    ByteBuffer dlb = ByteBuffer.allocateDirect(
            // (# of coordinate values * 2 bytes per short)
            indices.length * 2);
    dlb.order(ByteOrder.nativeOrder());
    ShortBuffer indexBuffer = dlb.asShortBuffer();
    indexBuffer.put(indices);
    indexBuffer.position(0);

    object3D.setIndicesBuffer(indexBuffer);
    object3D.setTexCoordinateBuffer(0,texBuffer);
    object3D.setTexCoordinateBuffer(1,texBuffer);
    object3D.setVerticesBuffer(0,vertexBuffer);
    object3D.setVerticesBuffer(1,vertexBuffer);
    object3D.setNumIndices(indices.length);

    object3D.texCoordinates = texcoords;
}
 
Example 19
Source File: MapRenderer.java    From trekarta with GNU General Public License v3.0 4 votes vote down vote up
public void onSurfaceChanged(int width, int height) {
    //log.debug("onSurfaceChanged: new={}, {}x{}", mNewSurface, width, height);

    if (width <= 0 || height <= 0)
        return;

    GLState.viewport(width, height);

    //GL.scissor(0, 0, width, height);
    //GL.enable(GL20.SCISSOR_TEST);

    gl.clearStencil(0x00);

    gl.disable(GL.CULL_FACE);
    gl.blendFunc(GL.ONE, GL.ONE_MINUS_SRC_ALPHA);

    gl.frontFace(GL.CW);
    gl.cullFace(GL.BACK);

    if (!mNewSurface) {
        mMap.updateMap(false);
        return;
    }

    mNewSurface = false;

    /* initialize quad indices used by Texture- and LineTexRenderer */
    int[] vboIds = GLUtils.glGenBuffers(2);

    mQuadIndicesID = vboIds[0];

    short[] indices = new short[MAX_INDICES];
    for (int i = 0, j = 0; i < MAX_INDICES; i += 6, j += 4) {
        indices[i + 0] = (short) (j + 0);
        indices[i + 1] = (short) (j + 1);
        indices[i + 2] = (short) (j + 2);

        indices[i + 3] = (short) (j + 2);
        indices[i + 4] = (short) (j + 1);
        indices[i + 5] = (short) (j + 3);
    }
    ShortBuffer buf = MapRenderer.getShortBuffer(indices.length);
    buf.put(indices);
    buf.flip();

    GLState.bindElementBuffer(mQuadIndicesID);
    gl.bufferData(GL.ELEMENT_ARRAY_BUFFER,
            indices.length * 2, buf,
            GL.STATIC_DRAW);
    GLState.bindElementBuffer(GLState.UNBIND);

    /* initialize default quad */
    FloatBuffer floatBuffer = MapRenderer.getFloatBuffer(8);
    float[] quad = new float[]{-1, -1, -1, 1, 1, -1, 1, 1};
    floatBuffer.put(quad);
    floatBuffer.flip();
    mQuadVerticesID = vboIds[1];

    GLState.bindVertexBuffer(mQuadVerticesID);
    gl.bufferData(GL.ARRAY_BUFFER,
            quad.length * 4, floatBuffer,
            GL.STATIC_DRAW);
    GLState.bindVertexBuffer(GLState.UNBIND);

    GLState.init();

    mMap.updateMap(true);
}
 
Example 20
Source File: VertexBuffer.java    From aion-germany with GNU General Public License v3.0 4 votes vote down vote up
public void compact(int numElements) {
	int total = components * numElements;
	data.clear();
	switch (format) {
		case Byte:
		case UnsignedByte:
		case Half:
			ByteBuffer bbuf = (ByteBuffer) data;
			bbuf.limit(total);
			ByteBuffer bnewBuf = BufferUtils.createByteBuffer(total);
			bnewBuf.put(bbuf);
			data = bnewBuf;
			break;
		case Short:
		case UnsignedShort:
			ShortBuffer sbuf = (ShortBuffer) data;
			sbuf.limit(total);
			ShortBuffer snewBuf = BufferUtils.createShortBuffer(total);
			snewBuf.put(sbuf);
			data = snewBuf;
			break;
		case Int:
		case UnsignedInt:
			IntBuffer ibuf = (IntBuffer) data;
			ibuf.limit(total);
			IntBuffer inewBuf = BufferUtils.createIntBuffer(total);
			inewBuf.put(ibuf);
			data = inewBuf;
			break;
		case Float:
			FloatBuffer fbuf = (FloatBuffer) data;
			fbuf.limit(total);
			FloatBuffer fnewBuf = BufferUtils.createFloatBuffer(total);
			fnewBuf.put(fbuf);
			data = fnewBuf;
			break;
		default:
			throw new UnsupportedOperationException("Unrecognized buffer format: " + format);
	}
	data.clear();
	setUpdateNeeded();
	dataSizeChanged = true;
}