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

The following examples show how to use java.nio.ShortBuffer#clear() . 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: OpusDecoder.java    From lavaplayer with Apache License 2.0 6 votes vote down vote up
/**
 * Encode the input buffer to output.
 * @param directInput Input byte buffer
 * @param directOutput Output sample buffer
 * @return Number of bytes written to the output
 */
public int decode(ByteBuffer directInput, ShortBuffer directOutput) {
  checkNotReleased();

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

  directOutput.clear();
  int result = library.decode(instance, directInput, directInput.remaining(), directOutput, directOutput.remaining() / channels);

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

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

  return result;
}
 
Example 2
Source File: SonicAudioProcessor.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void queueInput(ByteBuffer inputBuffer) {
  Assertions.checkState(sonic != null);
  if (inputBuffer.hasRemaining()) {
    ShortBuffer shortBuffer = inputBuffer.asShortBuffer();
    int inputSize = inputBuffer.remaining();
    inputBytes += inputSize;
    sonic.queueInput(shortBuffer);
    inputBuffer.position(inputBuffer.position() + inputSize);
  }
  int outputSize = sonic.getFramesAvailable() * channelCount * 2;
  if (outputSize > 0) {
    if (buffer.capacity() < outputSize) {
      buffer = ByteBuffer.allocateDirect(outputSize).order(ByteOrder.nativeOrder());
      shortBuffer = buffer.asShortBuffer();
    } else {
      buffer.clear();
      shortBuffer.clear();
    }
    sonic.getOutput(shortBuffer);
    outputBytes += outputSize;
    buffer.limit(outputSize);
    outputBuffer = buffer;
  }
}
 
Example 3
Source File: AudioChannelWithSP.java    From Mp4Composer-android with MIT License 6 votes vote down vote up
private boolean queueInputBufferInEncoder(final short[] rawData, final int encoderInBuffIndex) {

        final ShortBuffer outBuffer = encoder.getInputBuffer(encoderInBuffIndex).asShortBuffer();

        outBuffer.clear();
        if (rawData != null) {

            outBuffer.put(rawData);
            totalDataAdded += rawData.length;

            long presentationTimeUs = sampleCountToDurationUs(totalDataAdded, inputSampleRate, outputChannelCount);

            encoder.queueInputBuffer(encoderInBuffIndex, 0, rawData.length * BYTES_PER_SHORT,
                    presentationTimeUs, 0);
            return false;
        } else {
            encoder.queueInputBuffer(encoderInBuffIndex, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
            return false;
        }
    }
 
Example 4
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 5
Source File: SonicAudioProcessor.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void queueInput(ByteBuffer inputBuffer) {
  Sonic sonic = Assertions.checkNotNull(this.sonic);
  if (inputBuffer.hasRemaining()) {
    ShortBuffer shortBuffer = inputBuffer.asShortBuffer();
    int inputSize = inputBuffer.remaining();
    inputBytes += inputSize;
    sonic.queueInput(shortBuffer);
    inputBuffer.position(inputBuffer.position() + inputSize);
  }
  int outputSize = sonic.getOutputSize();
  if (outputSize > 0) {
    if (buffer.capacity() < outputSize) {
      buffer = ByteBuffer.allocateDirect(outputSize).order(ByteOrder.nativeOrder());
      shortBuffer = buffer.asShortBuffer();
    } else {
      buffer.clear();
      shortBuffer.clear();
    }
    sonic.getOutput(shortBuffer);
    outputBytes += outputSize;
    buffer.limit(outputSize);
    outputBuffer = buffer;
  }
}
 
Example 6
Source File: SonicAudioProcessor.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void queueInput(ByteBuffer inputBuffer) {
  Assertions.checkState(sonic != null);
  if (inputBuffer.hasRemaining()) {
    ShortBuffer shortBuffer = inputBuffer.asShortBuffer();
    int inputSize = inputBuffer.remaining();
    inputBytes += inputSize;
    sonic.queueInput(shortBuffer);
    inputBuffer.position(inputBuffer.position() + inputSize);
  }
  int outputSize = sonic.getFramesAvailable() * channelCount * 2;
  if (outputSize > 0) {
    if (buffer.capacity() < outputSize) {
      buffer = ByteBuffer.allocateDirect(outputSize).order(ByteOrder.nativeOrder());
      shortBuffer = buffer.asShortBuffer();
    } else {
      buffer.clear();
      shortBuffer.clear();
    }
    sonic.getOutput(shortBuffer);
    outputBytes += outputSize;
    buffer.limit(outputSize);
    outputBuffer = buffer;
  }
}
 
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 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 9
Source File: AudioChannel.java    From android-transcoder with Apache License 2.0 5 votes vote down vote up
private long remixAndMaybeFillOverflow(final AudioBuffer input,
                                       final ShortBuffer outBuff) {
    final ShortBuffer inBuff = input.data;
    final ShortBuffer overflowBuff = mOverflowBuffer.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());
        mRemixer.remix(inBuff, outBuff);

        // 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(), mInputSampleRate, mInputChannelCount);
        mRemixer.remix(inBuff, overflowBuff);

        // Seal off overflowBuff & mark limit
        overflowBuff.flip();
        mOverflowBuffer.presentationTimeUs = input.presentationTimeUs + consumedDurationUs;
    } else {
        // No overflow
        mRemixer.remix(inBuff, outBuff);
    }

    return input.presentationTimeUs;
}
 
Example 10
Source File: PcmChunkDecoder.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
@Override
public void decode(byte[] encoded, ShortBuffer buffer) {
  buffer.clear();

  encodedAsByte.clear();
  encodedAsByte.put(encoded);

  encodedAsShort.clear();
  encodedAsShort.limit(encodedAsByte.position() / 2);

  buffer.put(encodedAsShort);
  buffer.rewind();
}
 
Example 11
Source File: AudioChannel.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private long remixAndMaybeFillOverflow(final AudioBuffer input,
                                       final ShortBuffer outBuff) {
    final ShortBuffer inBuff = input.data;
    final ShortBuffer overflowBuff = mOverflowBuffer.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());
        mRemixer.remix(inBuff, outBuff);

        // 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(), mInputSampleRate, mInputChannelCount);
        mRemixer.remix(inBuff, overflowBuff);

        // Seal off overflowBuff & mark limit
        overflowBuff.flip();
        mOverflowBuffer.presentationTimeUs = input.presentationTimeUs + consumedDurationUs;
    } else {
        // No overflow
        mRemixer.remix(inBuff, outBuff);
    }

    return input.presentationTimeUs;
}
 
Example 12
Source File: AudioChannel.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private long remixAndMaybeFillOverflow(final AudioBuffer input,
                                       final ShortBuffer outBuff) {
    final ShortBuffer inBuff = input.data;
    final ShortBuffer overflowBuff = mOverflowBuffer.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());
        mRemixer.remix(inBuff, outBuff);

        // 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(), mInputSampleRate, mInputChannelCount);
        mRemixer.remix(inBuff, overflowBuff);

        // Seal off overflowBuff & mark limit
        overflowBuff.flip();
        mOverflowBuffer.presentationTimeUs = input.presentationTimeUs + consumedDurationUs;
    } else {
        // No overflow
        mRemixer.remix(inBuff, outBuff);
    }

    return input.presentationTimeUs;
}
 
Example 13
Source File: BufferUtils.java    From Ultraino with MIT 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 14
Source File: AudioChannel.java    From EZFilter 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 = mOverflowBuffer.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());
        mRemixer.remix(inBuff, outBuff);

        // 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(), mInputSampleRate, mInputChannelCount);
        mRemixer.remix(inBuff, overflowBuff);

        // Seal off overflowBuff & mark limit
        overflowBuff.flip();
        mOverflowBuffer.presentationTimeUs = input.presentationTimeUs + consumedDurationUs;
    } else {
        // No overflow
        mRemixer.remix(inBuff, outBuff);
    }

    return input.presentationTimeUs;
}
 
Example 15
Source File: BufferUtils.java    From aion-germany with GNU General Public License v3.0 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 16
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 17
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 18
Source File: HyperRectangleReaderTest.java    From sis with Apache License 2.0 4 votes vote down vote up
/**
 * Creates an hyper-rectangle of random size and initializes the sub-region and subsampling to random values.
 * Sample values are index values encoded in base 10. For example the value at index (4,1,2,3) will be 4123.
 *
 * @param random      the random number generator to use for initializing the test.
 * @param useChannel  {@code true} for fetching the data from channel to a small buffer, or
 *                    {@code false} if the data are expected to be fully contained in the buffer.
 */
private void initialize(final Random random, final boolean useChannel) throws IOException, DataStoreException {
    /*
     * Compute a random hyper-rectangle size, sub-region and subsampling. Each dimension will have a
     * size between 1 to 10, so we will be able to use decimal digits from 0 to 9 in the sample values.
     */
    int length = 1;
    for (int i=0; i<size.length; i++) {
        final int s = random.nextInt(9) + 1;
        int low = random.nextInt(s);
        int up  = random.nextInt(s);
        if (low > up) {
            final int t = low;
            low = up; up = t;
        }
        size [i] = s;
        lower[i] = low;
        upper[i] = up + 1;
        subsampling[i] = random.nextInt(3) + 1;
        length *= s;
    }
    /*
     * Prepare an array of bytes which will contain the short values using native byte order.
     * Put small amout of random value at the array beginning in order to test with an origin
     * different than zero.
     */
    final int origin = random.nextInt(10);
    final byte[] array = new byte[origin + length*Short.BYTES];
    for (int i=0; i<origin; i++) {
        array[i] = (byte) random.nextInt(0x100);
    }
    /*
     * Fill the array with short values using the encoding describes in javadoc.
     * Then wrap the array in a pseudo-channel so we can create the reader to test.
     */
    final ShortBuffer view = ByteBuffer.wrap(array, origin, length*Short.BYTES).order(ByteOrder.nativeOrder()).asShortBuffer();
    for (int i3=0; i3<size[3]; i3++) {
        for (int i2=0; i2<size[2]; i2++) {
            for (int i1=0; i1<size[1]; i1++) {
                for (int i0=0; i0<size[0]; i0++) {
                    view.put((short) sampleValue(i0, i1, i2, i3));
                }
            }
        }
    }
    assertEquals(length, view.position());
    if (useChannel) {
        final ByteArrayChannel channel = new ByteArrayChannel(array, true);
        final ByteBuffer       buffer  = ByteBuffer.allocate(random.nextInt(20) + 20).order(ByteOrder.nativeOrder());
        final ChannelDataInput input   = new ChannelDataInput("HyperRectangle in channel", channel, buffer, false);
        reader = new HyperRectangleReader(Numbers.SHORT, input, origin);
    } else {
        view.clear();
        reader = new HyperRectangleReader("HyperRectangle in buffer", view);
    }
}
 
Example 19
Source File: BufferUtils.java    From Ultraino with MIT License 3 votes vote down vote up
/**
 * Create a new ShortBuffer of the specified size.
 *
 * @param size
 *            required number of shorts to store.
 * @return the new ShortBuffer
 */
public static ShortBuffer createShortBuffer(int size) {
    ShortBuffer buf = allocator.allocate(2 * size).order(ByteOrder.nativeOrder()).asShortBuffer();
    buf.clear();
    onBufferAllocated(buf);
    return buf;
}
 
Example 20
Source File: BufferUtils.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * Create a new ShortBuffer of the specified size.
 *
 * @param size
 *            required number of shorts to store.
 * @return the new ShortBuffer
 */
public static ShortBuffer createShortBuffer(int size) {
    ShortBuffer buf = ByteBuffer.allocateDirect(2 * size).order(ByteOrder.nativeOrder()).asShortBuffer();
    buf.clear();
    onBufferAllocated(buf);
    return buf;
}