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

The following examples show how to use java.nio.ShortBuffer#remaining() . 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: AudioChannel.java    From EZFilter with 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 2
Source File: AudioChannel.java    From phoenix with Apache License 2.0 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 3
Source File: ArrayUtils.java    From pixymeta-android with Eclipse Public License 1.0 6 votes vote down vote up
public static short[] toShortArray(byte[] data, int offset, int len, boolean bigEndian) {
	
	ByteBuffer byteBuffer = ByteBuffer.wrap(data, offset, len);
	
	if (bigEndian) {
		byteBuffer.order(ByteOrder.BIG_ENDIAN);
	} else {
		byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
	}
	
	ShortBuffer shortBuf = byteBuffer.asShortBuffer();
	short[] array = new short[shortBuf.remaining()];
	shortBuf.get(array);
	
	return array;
}
 
Example 4
Source File: BufferUtils.java    From Ultraino with MIT License 6 votes vote down vote up
public static ShortBuffer ensureLargeEnough(ShortBuffer buffer, int required) {
    if (buffer != null) {
        buffer.limit(buffer.capacity());
    }
    if (buffer == null || (buffer.remaining() < required)) {
        int position = (buffer != null ? buffer.position() : 0);
        ShortBuffer newVerts = createShortBuffer(position + required);
        if (buffer != null) {
            buffer.flip();
            newVerts.put(buffer);
            newVerts.position(position);
        }
        buffer = newVerts;
    }
    return buffer;
}
 
Example 5
Source File: BufferUtils.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static ShortBuffer ensureLargeEnough(ShortBuffer buffer, int required) {
    if (buffer != null) {
        buffer.limit(buffer.capacity());
    }
    if (buffer == null || (buffer.remaining() < required)) {
        int position = (buffer != null ? buffer.position() : 0);
        ShortBuffer newVerts = createShortBuffer(position + required);
        if (buffer != null) {
            buffer.flip();
            newVerts.put(buffer);
            newVerts.position(position);
        }
        buffer = newVerts;
    }
    return buffer;
}
 
Example 6
Source File: IndexListACMR.java    From tribaltrouble with GNU General Public License v2.0 6 votes vote down vote up
public static float computeACMR(ShortBuffer indices, int fifo_size) {
		int misses = 0;
		short[] fifo = new short[fifo_size];
		int current_fifo_size = 0;
outer:
		for (int i = 0; i < indices.remaining(); i++) {
			short index = indices.get(indices.position() + i);
			for (int j = 0; j < current_fifo_size; j++)
				if (fifo[j] == index)
					continue outer;
			if (current_fifo_size == fifo.length) {
				for (int j = 1; j < current_fifo_size; j++)
					fifo[j - 1] = fifo[j];
			} else
				current_fifo_size++;
			fifo[current_fifo_size - 1] = index;
			misses++;
		}
		return (float)misses*3/indices.remaining();
	}
 
Example 7
Source File: AudioChannel.java    From Pix-Art-Messenger with GNU General Public License v3.0 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 8
Source File: AudioChannel.java    From android-transcoder with Apache License 2.0 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 9
Source File: ArrayUtils.java    From icafe with Eclipse Public License 1.0 6 votes vote down vote up
public static short[] toShortArray(byte[] data, int offset, int len, boolean bigEndian) {
	
	ByteBuffer byteBuffer = ByteBuffer.wrap(data, offset, len);
	
	if (bigEndian) {
		byteBuffer.order(ByteOrder.BIG_ENDIAN);
	} else {
		byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
	}
	
	ShortBuffer shortBuf = byteBuffer.asShortBuffer();
	short[] array = new short[shortBuf.remaining()];
	shortBuf.get(array);
	
	return array;
}
 
Example 10
Source File: AudioChannel.java    From Mp4Composer-android with MIT License 5 votes vote down vote up
private long remixAndMaybeFillOverflow(final AudioBuffer input,
                                       final ShortBuffer outBuff) {
    final ShortBuffer inBuff = input.data;
    final ShortBuffer overflowBuff = overflowBuffer.data;

    outBuff.clear();

    // Reset position to 0, and set limit to capacity (Since MediaCodec doesn't do that for us)
    inBuff.clear();

    if (inBuff.remaining() > outBuff.remaining()) {
        // Overflow
        // Limit inBuff to outBuff's capacity
        inBuff.limit(outBuff.capacity());
        outBuff.put(inBuff);

        // Reset limit to its own capacity & Keep position
        inBuff.limit(inBuff.capacity());

        // Remix the rest onto overflowBuffer
        // NOTE: We should only reach this point when overflow buffer is empty
        final long consumedDurationUs =
                sampleCountToDurationUs(inBuff.position(), inputSampleRate, inputChannelCount);
        overflowBuff.put(inBuff);

        // Seal off overflowBuff & mark limit
        overflowBuff.flip();
        overflowBuffer.presentationTimeUs = input.presentationTimeUs + consumedDurationUs;
    } else {
        // No overflow
        outBuff.put(inBuff);
    }
    return input.presentationTimeUs;
}
 
Example 11
Source File: AudioRemixer.java    From android-transcoder 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 12
Source File: AudioRemixer.java    From EZFilter with MIT License 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 13
Source File: AudioRemixer.java    From EZFilter with MIT License 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 14
Source File: Sonic.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
/**
 * Queues remaining data from {@code buffer}, and advances its position by the number of bytes
 * consumed.
 *
 * @param buffer A {@link ShortBuffer} containing input data between its position and limit.
 */
public void queueInput(ShortBuffer buffer) {
  int framesToWrite = buffer.remaining() / channelCount;
  int bytesToWrite = framesToWrite * channelCount * 2;
  inputBuffer = ensureSpaceForAdditionalFrames(inputBuffer, inputFrameCount, framesToWrite);
  buffer.get(inputBuffer, inputFrameCount * channelCount, bytesToWrite / 2);
  inputFrameCount += framesToWrite;
  processStreamInput();
}
 
Example 15
Source File: Sonic.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Queues remaining data from {@code buffer}, and advances its position by the number of bytes
 * consumed.
 *
 * @param buffer A {@link ShortBuffer} containing input data between its position and limit.
 */
public void queueInput(ShortBuffer buffer) {
  int framesToWrite = buffer.remaining() / channelCount;
  int bytesToWrite = framesToWrite * channelCount * 2;
  inputBuffer = ensureSpaceForAdditionalFrames(inputBuffer, inputFrameCount, framesToWrite);
  buffer.get(inputBuffer, inputFrameCount * channelCount, bytesToWrite / 2);
  inputFrameCount += framesToWrite;
  processStreamInput();
}
 
Example 16
Source File: AudioRemixer.java    From android-transcoder with Apache License 2.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 17
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) {
    // 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 18
Source File: LandscapeRenderer.java    From tribaltrouble with GNU General Public License v2.0 5 votes vote down vote up
public LandscapeRenderer(World world, WorldInfo world_info, GUIRoot gui_root, AnimationManager manager) {
	ShortBuffer indices = world.getLandscapeIndices().getIndices();
	this.indices_vbo = new ShortVBO(ARBBufferObject.GL_STATIC_DRAW_ARB, indices.remaining());
	this.indices_vbo.put(indices);

	this.landscape_vertices = new LandscapeTileVertices(world.getHeightMap(), HeightMap.GRID_UNITS_PER_PATCH_EXP, world.getHeightMap().getPatchesPerWorld());
	this.patch_levels = new PatchLevel[world.getHeightMap().getPatchesPerWorld()][world.getHeightMap().getPatchesPerWorld()];
	for (int y = 0; y < patch_levels.length; y++)
		for (int x = 0; x < patch_levels.length; x++)
			patch_levels[y][x] = new PatchLevel();
	for (int y = 0; y < patch_levels.length; y++)
		for (int x = 0; x < patch_levels.length; x++) {
			PatchLevel right = getPatchWrapped(x + 1, y);
			PatchLevel top = getPatchWrapped(x, y + 1);
			patch_levels[y][x].init(right, top);
		}
	this.detail = world_info.detail;
	this.colormaps = world_info.colormaps;
	this.gui_root = gui_root;
	this.world = world;
	this.manager = manager;
	int levels = LandscapeTileIndices.getNumLOD(HeightMap.GRID_UNITS_PER_PATCH_EXP);
	patch_lists = new ArrayList[levels];
	for (int i = 0; i < patch_lists.length; i++)
		patch_lists[i] = new ArrayList();
	manager.registerAnimation(this);
	this.shadow_indices_buffer = BufferUtils.createShortBuffer(LandscapeTileIndices.getNumTriangles(world.getLandscapeIndices().getNumLOD() - 1)*3);
	resetEditing();
}
 
Example 19
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 20
Source File: IndexListOptimizer.java    From tribaltrouble with GNU General Public License v2.0 4 votes vote down vote up
private static void dumpBuffer(ShortBuffer buffer) {
	for (int i = 0; i < buffer.remaining(); i++)
		System.out.print(buffer.get(buffer.position() + i) + " ");
	System.out.println();
}