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

The following examples show how to use java.nio.ShortBuffer#isDirect() . 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: BufferUtils.java    From aion-germany with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Creates a new ShortBuffer with the same contents as the given ShortBuffer. The new ShortBuffer is seperate 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 (buf.isDirect()) {
		copy = createShortBuffer(buf.limit());
	}
	else {
		copy = ShortBuffer.allocate(buf.limit());
	}
	copy.put(buf);

	return copy;
}
 
Example 2
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 3
Source File: OpusEncoder.java    From lavaplayer with 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 4
Source File: Mp3Decoder.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 samples 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.");
  }

  int result = library.decode(instance, directInput, directInput.remaining(), directOutput, directOutput.remaining() * 2);

  while (result == ERROR_NEW_FORMAT) {
    result = library.decode(instance, directInput, 0, directOutput, directOutput.remaining() * 2);
  }

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

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

  return result / 2;
}
 
Example 5
Source File: BufferUtils.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Creates a new ShortBuffer with the same contents as the given ShortBuffer.
 * The new ShortBuffer is seperate 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 (buf.isDirect()) {
        copy = createShortBuffer(buf.limit());
    } else {
        copy = ShortBuffer.allocate(buf.limit());
    }
    copy.put(buf);

    return copy;
}
 
Example 6
Source File: AacDecoder.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
/**
 * Decode a frame of audio into the given buffer.
 *
 * @param buffer DirectBuffer of signed PCM samples where the decoded frame will be stored. The buffer size must be at
 *               least of size <code>frameSize * channels * 2</code>. Buffer position and limit are ignored and not
 *               updated.
 * @param flush Whether all the buffered data should be flushed, set to true if no more input is expected.
 * @return True if the frame buffer was filled, false if there was not enough input for decoding a full frame.
 *
 * @throws IllegalArgumentException If the buffer is not a DirectBuffer.
 * @throws IllegalStateException If the decoding library returns an error other than running out of input data.
 * @throws IllegalStateException If the decoder has already been closed.
 */
public synchronized boolean decode(ShortBuffer buffer, boolean flush) {
  checkNotReleased();

  if (!buffer.isDirect()) {
    throw new IllegalArgumentException("Buffer argument must be a direct buffer.");
  }

  int result = library.decode(instance, buffer, buffer.capacity(), flush);
  if (result != 0 && result != ERROR_NOT_ENOUGH_BITS) {
    throw new IllegalStateException("Error from decoder " + result);
  }

  return result == 0;
}