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

The following examples show how to use java.nio.ShortBuffer#hasRemaining() . 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: ToFloatAudioFilter.java    From lavaplayer with Apache License 2.0 6 votes vote down vote up
@Override
public void process(ShortBuffer buffer) throws InterruptedException {
  while (buffer.hasRemaining()) {
    int chunkLength = Math.min(buffer.remaining() / channelCount, BUFFER_SIZE);

    if (chunkLength == 0) {
      break;
    }

    for (int chunkPosition = 0; chunkPosition < chunkLength; chunkPosition++) {
      for (int channel = 0; channel < buffers.length; channel++) {
        buffers[channel][chunkPosition] = shortToFloat(buffer.get());
      }
    }

    downstream.process(buffers, 0, chunkLength);
  }
}
 
Example 2
Source File: ChannelCountPcmAudioFilter.java    From lavaplayer with Apache License 2.0 6 votes vote down vote up
private void processNormalizer(ShortBuffer buffer) throws InterruptedException {
  while (buffer.hasRemaining()) {
    inputSet[inputIndex++] = buffer.get();

    if (inputIndex == inputChannels) {
      outputBuffer.put(inputSet, 0, commonChannels);

      for (int i = 0; i < channelsToAdd; i++) {
        outputBuffer.put(inputSet[0]);
      }

      if (!outputBuffer.hasRemaining()) {
        outputBuffer.flip();
        downstream.process(outputBuffer);
        outputBuffer.clear();
      }

      inputIndex = 0;
    }
  }
}
 
Example 3
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 4
Source File: ImageIndexTexture.java    From settlers-remake with MIT License 6 votes vote down vote up
private void loadTexture(GLDrawContext gl) {
	try {
		final DataInputStream in = new DataInputStream(new BufferedInputStream(file));
		int i = in.available() / 2;
		final int height = nextLowerPOT(Math.sqrt(i));
		final int width = nextLowerPOT(i / height);

		// TODO: Use better buffering.
		final ShortBuffer data = ByteBuffer.allocateDirect(width * height * 2).order(ByteOrder.nativeOrder()).asShortBuffer();
		while (data.hasRemaining()) {
			data.put(in.readShort());
		}
		data.rewind();
		textureIndex = gl.generateTexture(width, height, data, name);
	} catch (final IOException e) {
		e.printStackTrace();
		textureIndex = null;
	}
}
 
Example 5
Source File: OpenALAudioDevice.java    From DareEngine with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private static ByteBuffer toByteBuffer(byte[] data, boolean isStereo,
		boolean isBigEndian) {
	ByteBuffer dest = ByteBuffer.allocateDirect(data.length);
	dest.order(ByteOrder.nativeOrder());
	ByteBuffer src = ByteBuffer.wrap(data);
	src.order(isBigEndian ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN);
	if (isStereo) {
		ShortBuffer destAsShort = dest.asShortBuffer();
		ShortBuffer srcAsShort = src.asShortBuffer();
		while (srcAsShort.hasRemaining()) {
			destAsShort.put(srcAsShort.get());
		}
	} else {
		while (src.hasRemaining()) {
			dest.put(src.get());
		}
	}
	dest.rewind();
	return dest;
}
 
Example 6
Source File: WaveData.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Convert the audio bytes into the stream
 * 
 * @param audio_bytes The audio byts
 * @param two_bytes_data True if we using double byte data
 * @return The byte bufer of data
 */
private static ByteBuffer convertAudioBytes(byte[] audio_bytes, boolean two_bytes_data) {
	ByteBuffer dest = ByteBuffer.allocateDirect(audio_bytes.length);
	dest.order(ByteOrder.nativeOrder());
	ByteBuffer src = ByteBuffer.wrap(audio_bytes);
	src.order(ByteOrder.LITTLE_ENDIAN);
	if (two_bytes_data) {
		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 7
Source File: AiffData.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Convert the audio bytes into the stream
 * 
 * @param format The audio format being decoded
 * @param audio_bytes The audio byts
 * @param two_bytes_data True if we using double byte data
 * @return The byte bufer of data
 */
private static ByteBuffer convertAudioBytes(AudioFormat format, byte[] audio_bytes, boolean two_bytes_data) {
	ByteBuffer dest = ByteBuffer.allocateDirect(audio_bytes.length);
	dest.order(ByteOrder.nativeOrder());
	ByteBuffer src = ByteBuffer.wrap(audio_bytes);
	src.order(ByteOrder.BIG_ENDIAN);
	if (two_bytes_data) {
		ShortBuffer dest_short = dest.asShortBuffer();
		ShortBuffer src_short = src.asShortBuffer();
		while (src_short.hasRemaining())
			dest_short.put(src_short.get());
	} else {
		while (src.hasRemaining()) {
			byte b = src.get();
			if (format.getEncoding() == Encoding.PCM_SIGNED) {
				b = (byte) (b + 127);
			}
			dest.put(b);
		}
	}
	dest.rewind();
	return dest;
}
 
Example 8
Source File: ToSplitShortAudioFilter.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
@Override
public void process(ShortBuffer buffer) throws InterruptedException {
  while (buffer.hasRemaining()) {
    int chunkLength = Math.min(buffer.remaining(), BUFFER_SIZE * channelCount);

    for (int chunkPosition = 0; chunkPosition < chunkLength; chunkPosition++) {
      for (int channel = 0; channel < buffers.length; channel++) {
        buffers[channel][chunkPosition] = floatToShort(buffer.get());
      }
    }

    downstream.process(buffers, 0, chunkLength);
  }
}
 
Example 9
Source File: ChannelCountPcmAudioFilter.java    From lavaplayer with 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 10
Source File: TestUtils.java    From succinct with Apache License 2.0 5 votes vote down vote up
public static FSDataInputStream getStream(ShortBuffer buf) throws IOException {
  File tmpDir = Files.createTempDir();
  Path filePath = new Path(tmpDir.getAbsolutePath() + "/testOut");
  FileSystem fs = FileSystem.get(filePath.toUri(), new Configuration());
  FSDataOutputStream fOut = fs.create(filePath);
  buf.rewind();
  while (buf.hasRemaining()) {
    fOut.writeShort(buf.get());
  }
  fOut.close();
  buf.rewind();
  return fs.open(filePath);
}
 
Example 11
Source File: DOMOutputCapsule.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
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());
    }
    
    if (buf.length() > 0) {
        //remove last space
        buf.setLength(buf.length() - 1);
    }
    
    value.position(pos);
    el.setAttribute(dataAttributeName, buf.toString());
    currentElement = (Element) el.getParentNode();
}
 
Example 12
Source File: TextureFile.java    From settlers-remake with MIT License 5 votes vote down vote up
public TexturePosition addImage(ShortBuffer imageData, int width) {
	imageData.rewind();
	int height = (imageData.remaining() + width - 1) / width;
	int startx;
	int starty;

	// compute start
	if (drawx + width > this.width) {
		drawx = 0;
		linetop = linebottom;
	}
	startx = drawx;
	starty = linetop;
	// update for next image
	drawx += width;
	linebottom = Math.max(linebottom, linetop + height);

	// draw!
	short[] buffer = new short[width];
	for (int y = 0; imageData.hasRemaining(); y++) {
		shortBuffer.position((starty + y) * this.width + startx);
		imageData.get(buffer);
		shortBuffer.put(buffer);
	}

	return new TexturePosition((float) startx / (this.width + 1),
			(float) (starty + height + 1) / (this.height + 1),
			(float) (startx + width + 1) / (this.width + 1),
			(float) starty / (this.height + 1));
}
 
Example 13
Source File: DOMOutputCapsule.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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 14
Source File: ViewBuffers.java    From java-core-learning-example with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[]{0,0,0,0,0,0,0,'a'});
    bb.rewind();
    System.out.print("Byte Buffer ");
    while (bb.hasRemaining())
        System.out.print(bb.position() + " -> " + bb.get() + ", ");
    System.out.println();

    CharBuffer cb = ((ByteBuffer)bb.rewind()).asCharBuffer();
    System.out.print("Char Buffer ");
    while (cb.hasRemaining())
        System.out.print(cb.position() + " -> " + cb.get() + ", ");
    System.out.println();

    ShortBuffer sb = ((ByteBuffer)bb.rewind()).asShortBuffer();
    System.out.print("Short Buffer ");
    while (sb.hasRemaining())
        System.out.print(sb.position() + " -> " + sb.get() + ", ");
    System.out.println();

    IntBuffer ib = ((ByteBuffer)bb.rewind()).asIntBuffer();
    System.out.print("Int Buffer ");
    while (ib.hasRemaining())
        System.out.print(ib.position() + " -> " + ib.get());
    System.out.println();

    FloatBuffer fb = ((ByteBuffer)bb.rewind()).asFloatBuffer();
    System.out.print("Float Buffer ");
    while (fb.hasRemaining())
        System.out.print(fb.position() + " -> " + fb.get() + ", ");
    System.out.println();

    LongBuffer lb = ((ByteBuffer)bb.rewind()).asLongBuffer();
    System.out.print("Long Buffer ");
    while (lb.hasRemaining())
        System.out.print(lb.position() + " -> " + lb.get() + ", ");
    System.out.println();

    DoubleBuffer db = ((ByteBuffer)bb.rewind()).asDoubleBuffer();
    System.out.print("Double Buffer ");
    while (db.hasRemaining())
        System.out.print(db.position() + " -> " + db.get() + ", ");
    System.out.println();
}