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

The following examples show how to use java.nio.ShortBuffer#limit() . 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 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 2
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 3
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 4
Source File: FinalPcmAudioFilter.java    From lavaplayer with Apache License 2.0 6 votes vote down vote up
@Override
public void process(ShortBuffer buffer) throws InterruptedException {
  if (ignoredFrames > 0) {
    long skipped = Math.min(buffer.remaining(), ignoredFrames);
    buffer.position(buffer.position() + (int) skipped);
    ignoredFrames -= skipped;
  }

  ShortBuffer local = buffer.duplicate();

  while (buffer.remaining() > 0) {
    int chunk = Math.min(buffer.remaining(), frameBuffer.remaining());
    local.position(buffer.position());
    local.limit(local.position() + chunk);

    frameBuffer.put(local);
    dispatch();

    buffer.position(buffer.position() + chunk);
  }
}
 
Example 5
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 6
Source File: ByteBufferViews.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "shortViewProvider")
public void testShortGet(String desc, IntFunction<ByteBuffer> fbb,
                         Function<ByteBuffer, ShortBuffer> fbi) {
    ByteBuffer bb = allocate(fbb);
    ShortBuffer vb = fbi.apply(bb);
    int o = bb.position();

    for (int i = 0; i < vb.limit(); i++) {
        short fromBytes = getShortFromBytes(bb, o + i * 2);
        short fromMethodView = bb.getShort(o + i * 2);
        assertValues(i, fromBytes, fromMethodView, bb);

        short fromBufferView = vb.get(i);
        assertValues(i, fromMethodView, fromBufferView, bb, vb);
    }

    for (int i = 0; i < vb.limit(); i++) {
        short v = getShortFromBytes(bb, o + i * 2);
        short a = bb.getShort();
        assertValues(i, v, a, bb);

        short b = vb.get();
        assertValues(i, a, b, bb, vb);
    }

}
 
Example 7
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 8
Source File: AudioChannel.java    From Pix-Art-Messenger with GNU General Public License v3.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 9
Source File: LandscapeTileIndices.java    From tribaltrouble with GNU General Public License v2.0 5 votes vote down vote up
private static void buildTile(LandscapeTileTriangle lower, LandscapeTileTriangle higher, int lod, int border_set, ShortBuffer indices) {
	int pos = indices.position();
	lower.putIndices(lod, border_set, indices);
	higher.putIndices(lod ,border_set, indices);
	int saved_pos = indices.position();
	int saved_limit = indices.limit();
	indices.limit(saved_pos);
	indices.position(pos);
	IndexListOptimizer.optimize(indices);
	indices.limit(saved_limit);
	indices.position(saved_pos);
}
 
Example 10
Source File: PcmVolumeProcessor.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
private void applyCurrentVolume(ShortBuffer buffer) {
  if (currentVolume == 100) {
    return;
  }

  int endOffset = buffer.limit();

  for (int i = buffer.position(); i < endOffset; i++) {
    int value = buffer.get(i) * integerMultiplier / 10000;
    buffer.put(i, (short) Math.max(-32767, Math.min(32767, value)));
  }
}
 
Example 11
Source File: BinaryOutputCapsule.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void write(ShortBuffer value) throws IOException {
    if (value == null) {
        write(NULL_OBJECT);
        return;
    }
    value.rewind();
    int length = value.limit();
    write(length);
    for (int x = 0; x < length; x++) {
        writeForBuffer(value.get());
    }
    value.rewind();
}
 
Example 12
Source File: OBAudioBufferPlayer.java    From GLEXP-Team-onebillion with Apache License 2.0 5 votes vote down vote up
void writeOutputBufferToBuffers(ByteBuffer outputBuffer,long prestimeus,long frameNo)
{
    ShortBuffer ib = outputBuffer.asShortBuffer();
    int noInts = ib.limit();
    if (noInts == 0)
        return;
    //MainActivity.log(String.format("Writing buffers for f %d",frameNo));
    ib.rewind();
    long bufferDurationus = 1024 * 1000000 / sampleRate;
    long framesInBuffer = 1024;
    int i = 0;
    while (noInts > 0)
    {
        int wamt = Math.min(noInts,1024);
        int idx = nextAvailableWriteBuffer();
        SimpleBuffer sb = buffers[idx];
        sb.startWriting();
        sb.writeData(ib,wamt);
        sb.sequence = bufferSequenceNo++;
        sb.presentationTimeus = prestimeus + (long)(i * bufferDurationus);
        sb.frameNo = frameNo + i * framesInBuffer;
        //MainActivity.log(String.format("  writing to buffer %d, us - %d",idx,sb.frameNo));
        sb.stopWriting();
        noInts -= wamt;
        nextBufIdx = (nextBufIdx + 1) % NO_BUFFERS;
        i++;
    }
}
 
Example 13
Source File: AudioDecoder.java    From ssj with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Converts given raw audio file to a byte array.
 * @return Byte array in little-endian byte order.
 * @throws IOException If given file couldn't be read.
 */
private short[] getAudioSample(File file) throws IOException
{
	FileInputStream fileInputStream = new FileInputStream(file);
	byte[] data = new byte[(int) file.length()];
	int bytesRead = fileInputStream.read(data);
	short[] samples = null;
	if (bytesRead != EOF)
	{
		ShortBuffer sb = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer();
		samples = new short[sb.limit()];
		sb.get(samples);
	}
	return samples;
}
 
Example 14
Source File: Recorder.java    From VideoAndroid with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);

    // Audio
    int bufferSize;
    ShortBuffer audioData;
    int bufferReadResult;

    bufferSize = AudioRecord.getMinBufferSize(sampleAudioRateInHz,
            AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
    mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleAudioRateInHz,
            AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize);

    audioData = ShortBuffer.allocate(bufferSize);

    mAudioRecord.startRecording();

    /* ffmpeg_audio encoding loop */
    while (mRunAudioThread) {
        //获取音频数据
        bufferReadResult = mAudioRecord.read(audioData.array(), 0, audioData.capacity());
        audioData.limit(bufferReadResult);
        if (bufferReadResult > 0) {
            if(mFFmpegFrameRecorder != null && mRecording) {
                try {
                    mFFmpegFrameRecorder.recordSamples(audioData);      //写入音频数据
                } catch (FFmpegFrameRecorder.Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /* encoding finish, release recorder */
    if (mAudioRecord != null) {
        mAudioRecord.stop();
        mAudioRecord.release();
    }
}
 
Example 15
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 16
Source File: ShortNioDataBuffer.java    From java with Apache License 2.0 5 votes vote down vote up
@Override
public ShortDataBuffer slice(long index, long size) {
  Validator.sliceArgs(this, index, size);
  ShortBuffer sliceBuf = buf.duplicate();
  sliceBuf.position((int)index);
  sliceBuf.limit((int)index + (int)size);
  return new ShortNioDataBuffer(sliceBuf.slice());
}
 
Example 17
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 18
Source File: IosGL.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static int getLimitBytes(ShortBuffer buffer) {
    checkLimit(buffer);
    return buffer.limit() * 2;
}
 
Example 19
Source File: VertexBuffer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Reduces the capacity of the buffer to the given amount
 * of elements, any elements at the end of the buffer are truncated
 * as necessary.
 *
 * @param numElements The number of elements to reduce to.
 */
public void compact(int numElements){
    int total = components * numElements;
    data.clear();
    switch (format){
        case Byte:
        case UnsignedByte:
        case Half:
            ByteBuffer bbuf = (ByteBuffer) data;
            bbuf.limit(total);
            ByteBuffer bnewBuf = BufferUtils.createByteBuffer(total);
            bnewBuf.put(bbuf);
            data = bnewBuf;
            break;
        case Short:
        case UnsignedShort:
            ShortBuffer sbuf = (ShortBuffer) data;
            sbuf.limit(total);
            ShortBuffer snewBuf = BufferUtils.createShortBuffer(total);
            snewBuf.put(sbuf);
            data = snewBuf;
            break;
        case Int:
        case UnsignedInt:
            IntBuffer ibuf = (IntBuffer) data;
            ibuf.limit(total);
            IntBuffer inewBuf = BufferUtils.createIntBuffer(total);
            inewBuf.put(ibuf);
            data = inewBuf;
            break;
        case Float:
            FloatBuffer fbuf = (FloatBuffer) data;
            fbuf.limit(total);
            FloatBuffer fnewBuf = BufferUtils.createFloatBuffer(total);
            fnewBuf.put(fbuf);
            data = fnewBuf;
            break;
        default:
            throw new UnsupportedOperationException("Unrecognized buffer format: "+format);
    }
    data.clear();
    setUpdateNeeded();
    dataSizeChanged = true;
}
 
Example 20
Source File: BufferUtils.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Create a new ShortBuffer of an appropriate size to hold the specified
 * number of shorts only if the given buffer if not already the right size.
 *
 * @param buf
 *            the buffer to first check and rewind
 * @param size
 *            number of shorts that need to be held by the newly created
 *            buffer
 * @return the requested new ShortBuffer
 */
public static ShortBuffer createShortBuffer(ShortBuffer buf, int size) {
    if (buf != null && buf.limit() == size) {
        buf.rewind();
        return buf;
    }

    buf = createShortBuffer(size);
    return buf;
}