Java Code Examples for java.nio.ShortBuffer

The following examples show how to use java.nio.ShortBuffer. These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: lwjglbook   Source File: SoundBuffer.java    License: Apache License 2.0 6 votes vote down vote up
private ShortBuffer readVorbis(String resource, int bufferSize, STBVorbisInfo info) throws Exception {
    try (MemoryStack stack = MemoryStack.stackPush()) {
        vorbis = Utils.ioResourceToByteBuffer(resource, bufferSize);
        IntBuffer error = stack.mallocInt(1);
        long decoder = stb_vorbis_open_memory(vorbis, error, null);
        if (decoder == NULL) {
            throw new RuntimeException("Failed to open Ogg Vorbis file. Error: " + error.get(0));
        }

        stb_vorbis_get_info(decoder, info);

        int channels = info.channels();

        int lengthSamples = stb_vorbis_stream_length_in_samples(decoder);

        pcm = MemoryUtil.memAllocShort(lengthSamples);

        pcm.limit(stb_vorbis_get_samples_short_interleaved(decoder, channels, pcm) * channels);
        stb_vorbis_close(decoder);

        return pcm;
    }
}
 
Example 2
Source Project: Ultraino   Source File: BufferUtils.java    License: MIT 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 3
public final void fillCoverIndices(ShortBuffer buffer, int lod, int border_set, int start_x, int start_y, int end_x, int end_y) {
	border_set = adjustBorderSet(lod, border_set);
	int num_quads_exp = getNumQuadsExp(lod);
	int quad_size_exp = patch_exp - num_quads_exp;
	int quad_size = 1 << quad_size_exp;
	int start_quad_x = start_x>>quad_size_exp;
	int start_quad_y = start_y>>quad_size_exp;
	int end_quad_x = end_x>>quad_size_exp;
	int end_quad_y = end_y>>quad_size_exp;
	LandscapeTileTriangle[][] quads = quad_to_planes[lod];
	for (int x = start_quad_x; x <= end_quad_x; x++)
		for (int y = start_quad_y; y <= end_quad_y; y++) {
			LandscapeTileTriangle[] quad = quads[getQuadIndex(num_quads_exp, x, y)];
			LandscapeTileTriangle north = quad[LandscapeTileTriangle.NORTH_EXP];
			LandscapeTileTriangle south = quad[LandscapeTileTriangle.SOUTH_EXP];
			LandscapeTileTriangle east = quad[LandscapeTileTriangle.EAST_EXP];
			LandscapeTileTriangle west = quad[LandscapeTileTriangle.WEST_EXP];
			north.addIndices(buffer, border_set);
			south.addIndices(buffer, border_set);
			if (east != north && east != south) {
				east.addIndices(buffer, border_set);
				west.addIndices(buffer, border_set);
			}
		}
}
 
Example 4
Source Project: lavaplayer   Source File: OpusEncoder.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Encode the input buffer to output.
 * @param directInput Input sample buffer
 * @param frameSize Number of samples per channel
 * @param directOutput Output byte buffer
 * @return Number of bytes written to the output
 */
public int encode(ShortBuffer directInput, int frameSize, ByteBuffer directOutput) {
  checkNotReleased();

  if (!directInput.isDirect() || !directOutput.isDirect()) {
    throw new IllegalArgumentException("Arguments must be direct buffers.");
  }

  directOutput.clear();
  int result = library.encode(instance, directInput, frameSize, directOutput, directOutput.capacity());

  if (result < 0) {
    throw new IllegalStateException("Encoding failed with error " + result);
  }

  directOutput.position(result);
  directOutput.flip();

  return result;
}
 
Example 5
Source Project: EZFilter   Source File: AudioChannel.java    License: MIT License 6 votes vote down vote up
private long drainOverflow(final ShortBuffer outBuff) {
    final ShortBuffer overflowBuff = mOverflowBuffer.data;
    final int overflowLimit = overflowBuff.limit();
    final int overflowSize = overflowBuff.remaining();

    final long beginPresentationTimeUs = mOverflowBuffer.presentationTimeUs + sampleCountToDurationUs(overflowBuff.position(), mInputSampleRate,
            mOutputChannelCount);

    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 Project: WraithEngine   Source File: GLMeshTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void createData()
{
    IOpenGL opengl = opengl();
    OpenGLRenderingEngine renderingEngine = new OpenGLRenderingEngine(opengl);
    renderingEngine.init();

    IMesh mesh = renderingEngine.createMesh();
    mesh.update(vertexData());

    verify(opengl, times(1)).generateVertexArray();
    verify(opengl, times(2)).generateBuffer();

    verify(opengl, times(1)).uploadBufferData((FloatBuffer) any());
    verify(opengl, times(1)).uploadBufferData((ShortBuffer) any());
}
 
Example 7
Source Project: jogl-samples   Source File: BufferUtils.java    License: MIT License 6 votes vote down vote up
private static boolean isDirect(Buffer buf) {
    if (buf instanceof FloatBuffer) {
        return ((FloatBuffer) buf).isDirect();
    }
    if (buf instanceof IntBuffer) {
        return ((IntBuffer) buf).isDirect();
    }
    if (buf instanceof ShortBuffer) {
        return ((ShortBuffer) buf).isDirect();
    }
    if (buf instanceof ByteBuffer) {
        return ((ByteBuffer) buf).isDirect();
    }
    if (buf instanceof DoubleBuffer) {
        return ((DoubleBuffer) buf).isDirect();
    }
    if (buf instanceof LongBuffer) {
        return ((LongBuffer) buf).isDirect();
    }
    throw new UnsupportedOperationException(" BufferUtils.isDirect was called on " + buf.getClass().getName());
}
 
Example 8
Source Project: icafe   Source File: ArrayUtils.java    License: Eclipse Public License 1.0 6 votes vote down vote up
public static byte[] toByteArray(short[] data, boolean bigEndian) {
	
	ByteBuffer byteBuffer = ByteBuffer.allocate(data.length * 2);
	
	if (bigEndian) {
		byteBuffer.order(ByteOrder.BIG_ENDIAN);
	} else {
		byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
	}
       
	ShortBuffer shortBuffer = byteBuffer.asShortBuffer();
       shortBuffer.put(data);

       byte[] array = byteBuffer.array();

	return array;
}
 
Example 9
Source Project: libcommon   Source File: ChannelHelper.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * ByteChannelからshort配列を読み込む
 * @param channel
 * @return
 * @throws IOException
 */
public static short[] readShortArray(@NonNull final ByteChannel channel)
	throws IOException {
	
	final int n = readInt(channel);
	final ByteBuffer buf = ByteBuffer.allocate(n * 2).order(ByteOrder.BIG_ENDIAN);
	final int readBytes = channel.read(buf);
	if (readBytes != n * 2) throw new IOException();
	buf.clear();
	final ShortBuffer result = buf.asShortBuffer();
	if (result.hasArray()) {
		return result.array();
	}  else {
		final short[] b = new short[n];
		result.get(b);
		return b;
	}
}
 
Example 10
Source Project: hello-triangle   Source File: HelloGlobe.java    License: MIT License 6 votes vote down vote up
private ShortBuffer getElementBuffer(float radius, short rings, short sectors) {

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

        ShortBuffer elementBuffer = GLBuffers.newDirectShortBuffer(rings * sectors * 6);

        for (r = 0; r < rings - 1; r++) {

            for (s = 0; s < sectors - 1; s++) {

                elementBuffer.put((short) (r * sectors + s));
                elementBuffer.put((short) (r * sectors + (s + 1)));
                elementBuffer.put((short) ((r + 1) * sectors + (s + 1)));
                elementBuffer.put((short) ((r + 1) * sectors + (s + 1)));
                elementBuffer.put((short) (r * sectors + s));
//                elementBuffer.put((short) (r * sectors + (s + 1)));
                elementBuffer.put((short) ((r + 1) * sectors + s));
            }
        }
        elementBuffer.position(0);

        return elementBuffer;
    }
 
Example 11
Source Project: jogl-samples   Source File: Gl_400_fbo_layered.java    License: MIT License 6 votes vote down vote up
private boolean initBuffer(GL4 gl4) {

        ShortBuffer elementBuffer = GLBuffers.newDirectShortBuffer(elementData);
        FloatBuffer vertexBuffer = GLBuffers.newDirectFloatBuffer(vertexData);

        gl4.glGenBuffers(Buffer.MAX, bufferName);

        gl4.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferName.get(Buffer.ELEMENT));
        gl4.glBufferData(GL_ELEMENT_ARRAY_BUFFER, elementSize, elementBuffer, GL_STATIC_DRAW);
        gl4.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

        gl4.glBindBuffer(GL_ARRAY_BUFFER, bufferName.get(Buffer.VERTEX));
        gl4.glBufferData(GL_ARRAY_BUFFER, vertexSize, vertexBuffer, GL_STATIC_DRAW);
        gl4.glBindBuffer(GL_ARRAY_BUFFER, 0);

        BufferUtils.destroyDirectBuffer(elementBuffer);
        BufferUtils.destroyDirectBuffer(vertexBuffer);

        return checkError(gl4, "initBuffer");
    }
 
Example 12
public ShortBuffer getIndexBuffer() {
	short[] iarray = new short[sides * 3];
	ByteBuffer ibb = ByteBuffer.allocateDirect(sides * 3 * 2);
	ibb.order(ByteOrder.nativeOrder());
	ShortBuffer mIndexBuffer = ibb.asShortBuffer();
	for (int i = 0; i < sides; i++) {
		short index1 = 0;
		short index2 = (short) (i + 1);
		short index3 = (short) (i + 2);
		if (index3 == sides + 1) {
			index3 = 1;
		}
		mIndexBuffer.put(index1);
		mIndexBuffer.put(index2);
		mIndexBuffer.put(index3);

		iarray[i * 3 + 0] = index1;
		iarray[i * 3 + 1] = index2;
		iarray[i * 3 + 2] = index3;
	}
	//this.printShortArray(iarray, "index array");
	return mIndexBuffer;
}
 
Example 13
public static void convertToList(Mesh mesh){
    IndexBuffer inBuf = mesh.getIndicesAsList();
    IndexBuffer outBuf = IndexBuffer.createIndexBuffer(mesh.getVertexCount(),
                                                       inBuf.size());

    for (int i = 0; i < inBuf.size(); i++){
        outBuf.put(i, inBuf.get(i));
    }

    mesh.clearBuffer(Type.Index);
    switch (mesh.getMode()){
        case LineLoop:
        case LineStrip:
            mesh.setMode(Mode.Lines);
            break;
        case TriangleStrip:
        case TriangleFan:
            mesh.setMode(Mode.Triangles);
            break;
        default:
            break;
    }
    if (outBuf instanceof IndexIntBuffer){
        mesh.setBuffer(Type.Index, 3, (IntBuffer)outBuf.getBuffer());
    }else{
        mesh.setBuffer(Type.Index, 3, (ShortBuffer)outBuf.getBuffer());
    }
}
 
Example 14
public void write(ShortBuffer value, String name, ShortBuffer defVal) throws IOException {
    if (value == null) {
        return;
    }
    if (value.equals(defVal)) {
        return;
    }

    Element el = appendElement(name);
    el.setAttribute("size", String.valueOf(value.limit()));
    StringBuilder buf = new StringBuilder();
    int pos = value.position();
    value.rewind();
    int ctr = 0;
    while (value.hasRemaining()) {
        ctr++;
        buf.append(value.get());
        buf.append(" ");
    }
    if (ctr != value.limit())
        throw new IOException("'" + name
            + "' buffer contention resulted in write data consistency.  "
            + ctr + " values written when should have written "
            + value.limit());
    buf.setLength(buf.length() - 1);
    value.position(pos);
    el.setAttribute(dataAttributeName, buf.toString());
    currentElement = (Element) el.getParentNode();
}
 
Example 15
Source Project: speech-android-sdk   Source File: OggOpusEnc.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Encode raw audio data into Opus format then call OpusWriter to write the Ogg packet
 *
 * @param rawAudio
 * @return
 * @throws IOException
 */
public int encodeAndWrite(byte[] rawAudio) throws IOException {
    int uploadedAudioSize = 0;
    ByteArrayInputStream ios = new ByteArrayInputStream(rawAudio);

    byte[] data = new byte[SpeechConfiguration.FRAME_SIZE*2];
    int bufferSize, read;

    while((read = ios.read(data)) > 0){
        bufferSize = read;
        byte[] pcmBuffer = new byte[read];
        System.arraycopy(data, 0, pcmBuffer, 0, read);

        ShortBuffer shortBuffer = ShortBuffer.allocate(bufferSize);
        for (int i = 0; i < read; i += 2) {
            int b1 = pcmBuffer[i] & 0xff;
            int b2 = pcmBuffer[i+1] << 8;
            shortBuffer.put((short) (b1 | b2));
        }
        shortBuffer.flip();
        ByteBuffer opusBuffer = ByteBuffer.allocate(bufferSize);

        int opus_encoded = JNAOpus.INSTANCE.opus_encode(this.opusEncoder, shortBuffer, SpeechConfiguration.FRAME_SIZE, opusBuffer, bufferSize);

        opusBuffer.position(opus_encoded);
        opusBuffer.flip();

        byte[] opusData = new byte[opusBuffer.remaining()];
        opusBuffer.get(opusData, 0, opusData.length);

        if (opus_encoded > 0) {
            uploadedAudioSize += opusData.length;
            writer.writePacket(opusData, 0, opusData.length);
        }
    }

    ios.close();
    return uploadedAudioSize;
}
 
Example 16
Source Project: lavaplayer   Source File: ChannelCountPcmAudioFilter.java    License: Apache License 2.0 5 votes vote down vote up
private void processMonoToStereo(ShortBuffer buffer) throws InterruptedException {
  while (buffer.hasRemaining()) {
    short sample = buffer.get();
    outputBuffer.put(sample);
    outputBuffer.put(sample);

    if (!outputBuffer.hasRemaining()) {
      outputBuffer.flip();
      downstream.process(outputBuffer);
      outputBuffer.clear();
    }
  }
}
 
Example 17
Source Project: Bytecoder   Source File: TrueTypeFont.java    License: Apache License 2.0 5 votes vote down vote up
protected void initAllNames(int requestedID, HashSet<String> names) {

        byte[] name = new byte[256];
        ByteBuffer buffer = getTableBuffer(nameTag);

        if (buffer != null) {
            ShortBuffer sbuffer = buffer.asShortBuffer();
            sbuffer.get(); // format - not needed.
            short numRecords = sbuffer.get();

            /* The name table uses unsigned shorts. Many of these
             * are known small values that fit in a short.
             * The values that are sizes or offsets into the table could be
             * greater than 32767, so read and store those as ints
             */
            int stringPtr = ((int) sbuffer.get()) & 0xffff;
            for (int i=0; i<numRecords; i++) {
                short platformID = sbuffer.get();
                if (platformID != MS_PLATFORM_ID) {
                    sbuffer.position(sbuffer.position()+5);
                    continue; // skip over this record.
                }
                short encodingID = sbuffer.get();
                short langID     = sbuffer.get();
                short nameID     = sbuffer.get();
                int   nameLen    = ((int) sbuffer.get()) & 0xffff;
                int   namePtr    = (((int) sbuffer.get()) & 0xffff) + stringPtr;

                if (nameID == requestedID) {
                    buffer.position(namePtr);
                    buffer.get(name, 0, nameLen);
                    names.add(makeString(name, nameLen, platformID, encodingID));
                }
            }
        }
    }
 
Example 18
protected void initAllNames(int requestedID, HashSet names) {

        byte[] name = new byte[256];
        ByteBuffer buffer = getTableBuffer(nameTag);

        if (buffer != null) {
            ShortBuffer sbuffer = buffer.asShortBuffer();
            sbuffer.get(); // format - not needed.
            short numRecords = sbuffer.get();

            /* The name table uses unsigned shorts. Many of these
             * are known small values that fit in a short.
             * The values that are sizes or offsets into the table could be
             * greater than 32767, so read and store those as ints
             */
            int stringPtr = ((int) sbuffer.get()) & 0xffff;
            for (int i=0; i<numRecords; i++) {
                short platformID = sbuffer.get();
                if (platformID != MS_PLATFORM_ID) {
                    sbuffer.position(sbuffer.position()+5);
                    continue; // skip over this record.
                }
                short encodingID = sbuffer.get();
                short langID     = sbuffer.get();
                short nameID     = sbuffer.get();
                int   nameLen    = ((int) sbuffer.get()) & 0xffff;
                int   namePtr    = (((int) sbuffer.get()) & 0xffff) + stringPtr;

                if (nameID == requestedID) {
                    buffer.position(namePtr);
                    buffer.get(name, 0, nameLen);
                    names.add(makeString(name, nameLen, encodingID));
                }
            }
        }
    }
 
Example 19
Source Project: settlers-remake   Source File: ProvidedImage.java    License: MIT License 5 votes vote down vote up
public ShortBuffer getData() {
	ShortBuffer data = ShortBuffer.allocate(image.getWidth() * image.getHeight());
	for (int y = 0; y < image.getHeight(); y++) {
		for (int x = 0; x < image.getWidth(); x++) {
			Color color = new Color(image.getRGB(x, y));
			data.put(color.toShortColor(1));
		}
	}
	data.rewind();
	return data;
}
 
Example 20
Source Project: lwjglbook   Source File: SoundBuffer.java    License: Apache License 2.0 5 votes vote down vote up
public SoundBuffer(String file) throws Exception {
    this.bufferId = alGenBuffers();
    try (STBVorbisInfo info = STBVorbisInfo.malloc()) {
        ShortBuffer pcm = readVorbis(file, 32 * 1024, info);

        // Copy to buffer
        alBufferData(bufferId, info.channels() == 1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16, pcm, info.sample_rate());
    }
}
 
Example 21
Source Project: trekarta   Source File: VertexData.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * @return sum of elements added
 */
public int compile(ShortBuffer sbuf) {
    if (cur == null)
        return 0;

    cur.used = used;

    int size = 0;
    for (Chunk it = head(); it != null; it = it.next) {
        size += it.used;
        sbuf.put(it.vertices, 0, it.used);
    }
    dispose();
    return size;
}
 
Example 22
private static GuardedInvocation linkSetElement(final Object self) {
    MethodHandle method = null;
    MethodHandle guard = null;
    if (self instanceof ByteBuffer) {
        method = BYTEBUFFER_PUT;
        guard = IS_BYTEBUFFER;
    } else if (self instanceof CharBuffer) {
        method = CHARBUFFER_PUT;
        guard = IS_CHARBUFFER;
    } else if (self instanceof ShortBuffer) {
        method = SHORTBUFFER_PUT;
        guard = IS_SHORTBUFFER;
    } else if (self instanceof IntBuffer) {
        method = INTBUFFER_PUT;
        guard = IS_INTBUFFER;
    } else if (self instanceof LongBuffer) {
        method = LONGBUFFER_PUT;
        guard = IS_LONGBUFFER;
    } else if (self instanceof FloatBuffer) {
        method = FLOATBUFFER_PUT;
        guard = IS_FLOATBUFFER;
    } else if (self instanceof DoubleBuffer) {
        method = DOUBLEBUFFER_PUT;
        guard = IS_DOUBLEBUFFER;
    }

    return method != null? new GuardedInvocation(method, guard) : null;
}
 
Example 23
public final void putIndices(int lod, int border_set, ShortBuffer indices) {
//System.out.println("lod = " + lod + " | Integer.toHexString(border_set) = " + Integer.toHexString(border_set) + " | Integer.toHexString(border_direction) = " + Integer.toHexString(border_direction));
		if (lod > 0 || (lod == 0 && containsBorder(border_set))) {
			t0.putIndices(lod - 1, border_set, indices);
			t1.putIndices(lod - 1, border_set, indices);
		} else {
			indices.put(i0);
			indices.put(i1);
			indices.put(i2);
		}
	}
 
Example 24
Source Project: jdk8u-jdk   Source File: TrueTypeFont.java    License: GNU General Public License v2.0 5 votes vote down vote up
protected void initAllNames(int requestedID, HashSet names) {

        byte[] name = new byte[256];
        ByteBuffer buffer = getTableBuffer(nameTag);

        if (buffer != null) {
            ShortBuffer sbuffer = buffer.asShortBuffer();
            sbuffer.get(); // format - not needed.
            short numRecords = sbuffer.get();

            /* The name table uses unsigned shorts. Many of these
             * are known small values that fit in a short.
             * The values that are sizes or offsets into the table could be
             * greater than 32767, so read and store those as ints
             */
            int stringPtr = ((int) sbuffer.get()) & 0xffff;
            for (int i=0; i<numRecords; i++) {
                short platformID = sbuffer.get();
                if (platformID != MS_PLATFORM_ID) {
                    sbuffer.position(sbuffer.position()+5);
                    continue; // skip over this record.
                }
                short encodingID = sbuffer.get();
                short langID     = sbuffer.get();
                short nameID     = sbuffer.get();
                int   nameLen    = ((int) sbuffer.get()) & 0xffff;
                int   namePtr    = (((int) sbuffer.get()) & 0xffff) + stringPtr;

                if (nameID == requestedID) {
                    buffer.position(namePtr);
                    buffer.get(name, 0, nameLen);
                    names.add(makeString(name, nameLen, encodingID));
                }
            }
        }
    }
 
Example 25
Source Project: dragonwell8_jdk   Source File: TrueTypeFont.java    License: GNU General Public License v2.0 5 votes vote down vote up
protected void initAllNames(int requestedID, HashSet names) {

        byte[] name = new byte[256];
        ByteBuffer buffer = getTableBuffer(nameTag);

        if (buffer != null) {
            ShortBuffer sbuffer = buffer.asShortBuffer();
            sbuffer.get(); // format - not needed.
            short numRecords = sbuffer.get();

            /* The name table uses unsigned shorts. Many of these
             * are known small values that fit in a short.
             * The values that are sizes or offsets into the table could be
             * greater than 32767, so read and store those as ints
             */
            int stringPtr = ((int) sbuffer.get()) & 0xffff;
            for (int i=0; i<numRecords; i++) {
                short platformID = sbuffer.get();
                if (platformID != MS_PLATFORM_ID) {
                    sbuffer.position(sbuffer.position()+5);
                    continue; // skip over this record.
                }
                short encodingID = sbuffer.get();
                short langID     = sbuffer.get();
                short nameID     = sbuffer.get();
                int   nameLen    = ((int) sbuffer.get()) & 0xffff;
                int   namePtr    = (((int) sbuffer.get()) & 0xffff) + stringPtr;

                if (nameID == requestedID) {
                    buffer.position(namePtr);
                    buffer.get(name, 0, nameLen);
                    names.add(makeString(name, nameLen, encodingID));
                }
            }
        }
    }
 
Example 26
Source Project: hottub   Source File: TrueTypeFont.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void setStrikethroughMetrics(ByteBuffer os_2Table, int upem) {
    if (os_2Table == null || os_2Table.capacity() < 30 || upem < 0) {
        stSize = .05f;
        stPos = -.4f;
        return;
    }
    ShortBuffer sb = os_2Table.asShortBuffer();
    stSize = sb.get(13) / (float)upem;
    stPos = -sb.get(14) / (float)upem;
}
 
Example 27
Source Project: TencentKona-8   Source File: TrueTypeFont.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void setStrikethroughMetrics(ByteBuffer os_2Table, int upem) {
    if (os_2Table == null || os_2Table.capacity() < 30 || upem < 0) {
        stSize = .05f;
        stPos = -.4f;
        return;
    }
    ShortBuffer sb = os_2Table.asShortBuffer();
    stSize = sb.get(13) / (float)upem;
    stPos = -sb.get(14) / (float)upem;
}
 
Example 28
Source Project: ShapesInOpenGLES2.0   Source File: Quad.java    License: MIT License 5 votes vote down vote up
/**
 * creates buffers for Quad shape object
 * @param pos
 * @param widths
 */
public void createBuffers( float[] pos, float[] widths) {
    createVertexData(pos, widths);

    final FloatBuffer heightMapVertexDataBuffer = ByteBuffer
            .allocateDirect(aQuadVertexData.length * BYTES_PER_FLOAT).order(ByteOrder.nativeOrder())
            .asFloatBuffer();
    heightMapVertexDataBuffer.put(aQuadVertexData).position(0);

    final ShortBuffer heightMapIndexDataBuffer = ByteBuffer
            .allocateDirect(aQuadIndexData.length * BYTES_PER_SHORT).order(ByteOrder.nativeOrder())
            .asShortBuffer();
    heightMapIndexDataBuffer.put(aQuadIndexData).position(0);

    if (qvbo[0] > 0 && qibo[0] > 0) {
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, qvbo[0]);
        GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, heightMapVertexDataBuffer.capacity() * BYTES_PER_FLOAT,
                heightMapVertexDataBuffer, GLES20.GL_STATIC_DRAW);

        GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, qibo[0]);
        GLES20.glBufferData(GLES20.GL_ELEMENT_ARRAY_BUFFER, heightMapIndexDataBuffer.capacity()
                * BYTES_PER_SHORT, heightMapIndexDataBuffer, GLES20.GL_STATIC_DRAW);

        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
        GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0);

    } else {
        GlUtil.checkGlError("glGenBuffers");
    }
}
 
Example 29
Source Project: TencentKona-8   Source File: TrueTypeFont.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public void getStyleMetrics(float pointSize, float[] metrics, int offset) {

    if (ulSize == 0f && ulPos == 0f) {

        ByteBuffer head_Table = getTableBuffer(headTag);
        int upem = -1;
        if (head_Table != null && head_Table.capacity() >= 18) {
            ShortBuffer sb = head_Table.asShortBuffer();
            upem = sb.get(9) & 0xffff;
            if (upem < 16 || upem > 16384) {
                upem = 2048;
            }
        }

        ByteBuffer os2_Table = getTableBuffer(os_2Tag);
        setStrikethroughMetrics(os2_Table, upem);

        ByteBuffer post_Table = getTableBuffer(postTag);
        setUnderlineMetrics(post_Table, upem);
    }

    metrics[offset] = stPos * pointSize;
    metrics[offset+1] = stSize * pointSize;

    metrics[offset+2] = ulPos * pointSize;
    metrics[offset+3] = ulSize * pointSize;
}
 
Example 30
Source Project: es6draft   Source File: TypedArrayFunctions.java    License: MIT License 5 votes vote down vote up
private static final ShortBuffer asShortBuffer(TypedArrayObject typedArray) {
    ByteBuffer data = byteBuffer(typedArray);
    int byteOffset = byteOffset(typedArray);
    int byteLength = byteLength(typedArray);

    data.limit(byteOffset + byteLength).position(byteOffset);
    ShortBuffer view = data.asShortBuffer();
    data.clear();
    return view;
}
 
Example 31
Source Project: jogl-samples   Source File: Gl_320_fbo_shadow.java    License: MIT License 5 votes vote down vote up
private boolean initBuffer(GL3 gl3) {

        ShortBuffer elementBuffer = GLBuffers.newDirectShortBuffer(elementData);
        ByteBuffer vertexBuffer = GLBuffers.newDirectByteBuffer(vertexSize);

        gl3.glGenBuffers(Buffer.MAX, bufferName);

        gl3.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferName.get(Buffer.ELEMENT));
        gl3.glBufferData(GL_ELEMENT_ARRAY_BUFFER, elementSize, elementBuffer, GL_STATIC_DRAW);
        gl3.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

        gl3.glBindBuffer(GL_ARRAY_BUFFER, bufferName.get(Buffer.VERTEX));
        for (int i = 0; i < vertexCount; i++) {
            vertexData[i].toBb(vertexBuffer, i);
        }
        vertexBuffer.rewind();
        gl3.glBufferData(GL_ARRAY_BUFFER, vertexSize, vertexBuffer, GL_STATIC_DRAW);
        gl3.glBindBuffer(GL_ARRAY_BUFFER, 0);

        int[] uniformBufferOffset = {0};
        gl3.glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, uniformBufferOffset, 0);
        int uniformBlockSize = Math.max(Mat4.SIZE * 3, uniformBufferOffset[0]);

        gl3.glBindBuffer(GL_UNIFORM_BUFFER, bufferName.get(Buffer.TRANSFORM));
        gl3.glBufferData(GL_UNIFORM_BUFFER, uniformBlockSize, null, GL_DYNAMIC_DRAW);
        gl3.glBindBuffer(GL_UNIFORM_BUFFER, 0);

        BufferUtils.destroyDirectBuffer(elementBuffer);
        BufferUtils.destroyDirectBuffer(vertexBuffer);

        return true;
    }
 
Example 32
Source Project: jogl-samples   Source File: Gl_320_fbo_srgb_decode_ext.java    License: MIT License 5 votes vote down vote up
private boolean initBuffer(GL3 gl3) {

        ShortBuffer elementBuffer = GLBuffers.newDirectShortBuffer(elementData);
        FloatBuffer vertexBuffer = GLBuffers.newDirectFloatBuffer(vertexData);
        IntBuffer uniformBufferOffset = GLBuffers.newDirectIntBuffer(1);

        gl3.glGenBuffers(Buffer.MAX, bufferName);

        gl3.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferName.get(Buffer.ELEMENT));
        gl3.glBufferData(GL_ELEMENT_ARRAY_BUFFER, elementSize, elementBuffer, GL_STATIC_DRAW);
        gl3.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

        gl3.glBindBuffer(GL_ARRAY_BUFFER, bufferName.get(Buffer.VERTEX));
        gl3.glBufferData(GL_ARRAY_BUFFER, vertexSize, vertexBuffer, GL_STATIC_DRAW);
        gl3.glBindBuffer(GL_ARRAY_BUFFER, 0);

        gl3.glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, uniformBufferOffset);
        int uniformBlockSize = Math.max(Mat4.SIZE, uniformBufferOffset.get(0));

        gl3.glBindBuffer(GL_UNIFORM_BUFFER, bufferName.get(Buffer.TRANSFORM));
        gl3.glBufferData(GL_UNIFORM_BUFFER, uniformBlockSize, null, GL_DYNAMIC_DRAW);
        gl3.glBindBuffer(GL_UNIFORM_BUFFER, 0);

        BufferUtils.destroyDirectBuffer(elementBuffer);
        BufferUtils.destroyDirectBuffer(vertexBuffer);
        BufferUtils.destroyDirectBuffer(uniformBufferOffset);

        return true;
    }
 
Example 33
public Mesh createMesh(Vector3f scale, Vector2f tcScale, Vector2f tcOffset, float offsetAmount, int totalSize, boolean center, int lod, boolean rightLod, boolean topLod, boolean leftLod, boolean bottomLod) {
    FloatBuffer pb = writeVertexArray(null, scale, center);
    FloatBuffer texb = writeTexCoordArray(null, tcOffset, tcScale, offsetAmount, totalSize);
    FloatBuffer nb = writeNormalArray(null, scale);
    IndexBuffer ib = writeIndexArrayLodDiff(lod, rightLod, topLod, leftLod, bottomLod, totalSize);
    FloatBuffer bb = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 3);
    FloatBuffer tanb = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 3);
    writeTangentArray(nb, tanb, bb, texb, scale);
    Mesh m = new Mesh();
    m.setMode(Mode.TriangleStrip);
    m.setBuffer(Type.Position, 3, pb);
    m.setBuffer(Type.Normal, 3, nb);
    m.setBuffer(Type.Tangent, 3, tanb);
    m.setBuffer(Type.Binormal, 3, bb);
    m.setBuffer(Type.TexCoord, 2, texb);
    switch (ib.getFormat()) {
        case UnsignedInt:
            m.setBuffer(Type.Index, 3, (IntBuffer) ib.getBuffer());
            break;
        case UnsignedShort:
            m.setBuffer(Type.Index, 3, (ShortBuffer) ib.getBuffer());
            break;
        case UnsignedByte:
            m.setBuffer(Type.Index, 3, (ByteBuffer) ib.getBuffer());
            break;
    }
    m.setStatic();
    m.updateBound();
    return m;
}
 
Example 34
Source Project: Mp4Composer-android   Source File: AudioChannelWithSP.java    License: MIT License 5 votes vote down vote up
private void writeToSonicSteam(final ShortBuffer data) {

        short[] temBuff = new short[data.capacity()];
        data.get(temBuff);
        data.rewind();
        stream.writeShortToStream(temBuff, temBuff.length / outputChannelCount);
    }
 
Example 35
Source Project: trekarta   Source File: LineTexBucket.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static void init() {

            shader = new Shader("linetex_layer_tex");
            //shader = new Shader("linetex_layer");

            int[] vboIds = GLUtils.glGenBuffers(1);
            mVertexFlipID = vboIds[0];

            /* bytes: 0, 1, 0, 1, 0, ... */
            byte[] flip = new byte[MapRenderer.MAX_QUADS * 4];
            for (int i = 0; i < flip.length; i++)
                flip[i] = (byte) (i % 2);

            ByteBuffer buf = ByteBuffer.allocateDirect(flip.length)
                    .order(ByteOrder.nativeOrder());
            buf.put(flip);
            buf.flip();

            ShortBuffer sbuf = buf.asShortBuffer();

            //GL.bindBuffer(GL20.ARRAY_BUFFER, mVertexFlipID);
            GLState.bindVertexBuffer(mVertexFlipID);
            gl.bufferData(GL.ARRAY_BUFFER, flip.length, sbuf,
                    GL.STATIC_DRAW);
            GLState.bindVertexBuffer(GLState.UNBIND);

            //    mTexID = new int[10];
            //    byte[] stipple = new byte[40];
            //    stipple[0] = 32;
            //    stipple[1] = 32;
            //    mTexID[0] = loadStippleTexture(stipple);

            //tex = new TextureItem(CanvasAdapter.getBitmapAsset("patterns/arrow.png"));
            //tex.mipmap = true;
        }
 
Example 36
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 37
Source Project: jdk8u_jdk   Source File: TrueTypeFont.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void setUnderlineMetrics(ByteBuffer postTable, int upem) {
    if (postTable == null || postTable.capacity() < 12 || upem < 0) {
        ulSize = .05f;
        ulPos = .1f;
        return;
    }
    ShortBuffer sb = postTable.asShortBuffer();
    ulSize = sb.get(5) / (float)upem;
    ulPos = -sb.get(4) / (float)upem;
}
 
Example 38
private void printBuffer(Buffer buffer) {
    StringBuilder sb = new StringBuilder();
    sb.append(ANSI_MAGENTA);
    if (buffer instanceof ByteBuffer) {
        sb.append("byte");
    } else if (buffer instanceof ShortBuffer) {
        sb.append("short");
    } else if (buffer instanceof CharBuffer) { 
        sb.append("char");
    } else if (buffer instanceof FloatBuffer) {
        sb.append("float");
    } else if (buffer instanceof IntBuffer) {
        sb.append("int");
    } else if (buffer instanceof LongBuffer) {
        sb.append("long");
    } else if (buffer instanceof DoubleBuffer) {
        sb.append("double");
    } else {
        throw new UnsupportedOperationException();
    }
    sb.append(ANSI_RESET);
    sb.append("[");
    
    if (buffer.position() == 0
            && buffer.limit() == buffer.capacity()) {
        // Common case. Just print buffer size.
        sb.append(buffer.capacity());
    } else {
        sb.append("pos=").append(buffer.position());
        sb.append(" lim=").append(buffer.limit());
        sb.append(" cap=").append(buffer.capacity());
    }
    
    sb.append("]");
    print(sb.toString());
}
 
Example 39
Source Project: lavaplayer   Source File: VolumePostProcessor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void process(long timecode, ShortBuffer buffer) throws InterruptedException {
  int currentVolume = context.playerOptions.volumeLevel.get();

  if (currentVolume != volumeProcessor.getLastVolume()) {
    AudioFrameVolumeChanger.apply(context);
  }

  // Volume 0 is stored in the frame with volume 100 buffer
  if (currentVolume != 0) {
    volumeProcessor.applyVolume(100, currentVolume, buffer);
  } else {
    volumeProcessor.setLastVolume(0);
  }
}
 
Example 40
@Override
public short[] readField(final byte[] fieldData) {
  if ((fieldData == null) || (fieldData.length < 2)) {
    return null;
  }
  final ShortBuffer buff = ByteBuffer.wrap(fieldData).asShortBuffer();
  final short[] result = new short[buff.remaining()];
  buff.get(result);
  return result;
}