Search in sources :

Example 91 with DoubleBuffer

use of java.nio.DoubleBuffer in project jna by java-native-access.

the class BufferArgumentsMarshalTest method testWrappedDoubleArrayArguent.

public void testWrappedDoubleArrayArguent() {
    double[] array = new double[1024];
    DoubleBuffer buf = DoubleBuffer.wrap(array, 512, 512);
    final double MAGIC = -118.625;
    lib.fillDoubleBuffer(buf, 512, MAGIC);
    for (int i = 0; i < array.length; i++) {
        assertEquals("Bad value at index " + i, i < 512 ? 0 : MAGIC, array[i]);
    }
}
Also used : DoubleBuffer(java.nio.DoubleBuffer)

Example 92 with DoubleBuffer

use of java.nio.DoubleBuffer in project bigbluebutton by bigbluebutton.

the class FFmpegFrameRecorder method recordSamples.

public boolean recordSamples(int sampleRate, int audioChannels, Buffer... samples) throws Exception {
    if (audio_st == null) {
        throw new Exception("No audio output stream (Is audioChannels > 0 and has start() been called?)");
    }
    int ret;
    if (sampleRate <= 0) {
        sampleRate = audio_c.sample_rate();
    }
    if (audioChannels <= 0) {
        audioChannels = audio_c.channels();
    }
    int inputSize = samples != null ? samples[0].limit() - samples[0].position() : 0;
    int inputFormat = AV_SAMPLE_FMT_NONE;
    int inputChannels = samples != null && samples.length > 1 ? 1 : audioChannels;
    int inputDepth = 0;
    int outputFormat = audio_c.sample_fmt();
    int outputChannels = samples_out.length > 1 ? 1 : audio_c.channels();
    int outputDepth = av_get_bytes_per_sample(outputFormat);
    if (samples != null && samples[0] instanceof ByteBuffer) {
        inputFormat = samples.length > 1 ? AV_SAMPLE_FMT_U8P : AV_SAMPLE_FMT_U8;
        inputDepth = 1;
        for (int i = 0; i < samples.length; i++) {
            ByteBuffer b = (ByteBuffer) samples[i];
            if (samples_in[i] instanceof BytePointer && samples_in[i].capacity() >= inputSize && b.hasArray()) {
                ((BytePointer) samples_in[i]).position(0).put(b.array(), b.position(), inputSize);
            } else {
                samples_in[i] = new BytePointer(b);
            }
        }
    } else if (samples != null && samples[0] instanceof ShortBuffer) {
        inputFormat = samples.length > 1 ? AV_SAMPLE_FMT_S16P : AV_SAMPLE_FMT_S16;
        inputDepth = 2;
        for (int i = 0; i < samples.length; i++) {
            ShortBuffer b = (ShortBuffer) samples[i];
            if (samples_in[i] instanceof ShortPointer && samples_in[i].capacity() >= inputSize && b.hasArray()) {
                ((ShortPointer) samples_in[i]).position(0).put(b.array(), samples[i].position(), inputSize);
            } else {
                samples_in[i] = new ShortPointer(b);
            }
        }
    } else if (samples != null && samples[0] instanceof IntBuffer) {
        inputFormat = samples.length > 1 ? AV_SAMPLE_FMT_S32P : AV_SAMPLE_FMT_S32;
        inputDepth = 4;
        for (int i = 0; i < samples.length; i++) {
            IntBuffer b = (IntBuffer) samples[i];
            if (samples_in[i] instanceof IntPointer && samples_in[i].capacity() >= inputSize && b.hasArray()) {
                ((IntPointer) samples_in[i]).position(0).put(b.array(), samples[i].position(), inputSize);
            } else {
                samples_in[i] = new IntPointer(b);
            }
        }
    } else if (samples != null && samples[0] instanceof FloatBuffer) {
        inputFormat = samples.length > 1 ? AV_SAMPLE_FMT_FLTP : AV_SAMPLE_FMT_FLT;
        inputDepth = 4;
        for (int i = 0; i < samples.length; i++) {
            FloatBuffer b = (FloatBuffer) samples[i];
            if (samples_in[i] instanceof FloatPointer && samples_in[i].capacity() >= inputSize && b.hasArray()) {
                ((FloatPointer) samples_in[i]).position(0).put(b.array(), b.position(), inputSize);
            } else {
                samples_in[i] = new FloatPointer(b);
            }
        }
    } else if (samples != null && samples[0] instanceof DoubleBuffer) {
        inputFormat = samples.length > 1 ? AV_SAMPLE_FMT_DBLP : AV_SAMPLE_FMT_DBL;
        inputDepth = 8;
        for (int i = 0; i < samples.length; i++) {
            DoubleBuffer b = (DoubleBuffer) samples[i];
            if (samples_in[i] instanceof DoublePointer && samples_in[i].capacity() >= inputSize && b.hasArray()) {
                ((DoublePointer) samples_in[i]).position(0).put(b.array(), b.position(), inputSize);
            } else {
                samples_in[i] = new DoublePointer(b);
            }
        }
    } else if (samples != null) {
        throw new Exception("Audio samples Buffer has unsupported type: " + samples);
    }
    if (samples_convert_ctx == null || samples_channels != audioChannels || samples_format != inputFormat || samples_rate != sampleRate) {
        samples_convert_ctx = swr_alloc_set_opts(samples_convert_ctx, audio_c.channel_layout(), outputFormat, audio_c.sample_rate(), av_get_default_channel_layout(audioChannels), inputFormat, sampleRate, 0, null);
        if (samples_convert_ctx == null) {
            throw new Exception("swr_alloc_set_opts() error: Cannot allocate the conversion context.");
        } else if ((ret = swr_init(samples_convert_ctx)) < 0) {
            throw new Exception("swr_init() error " + ret + ": Cannot initialize the conversion context.");
        }
        samples_channels = audioChannels;
        samples_format = inputFormat;
        samples_rate = sampleRate;
    }
    for (int i = 0; samples != null && i < samples.length; i++) {
        samples_in[i].position(samples_in[i].position() * inputDepth).limit((samples_in[i].position() + inputSize) * inputDepth);
    }
    while (true) {
        int inputCount = (int) Math.min(samples != null ? (samples_in[0].limit() - samples_in[0].position()) / (inputChannels * inputDepth) : 0, Integer.MAX_VALUE);
        int outputCount = (int) Math.min((samples_out[0].limit() - samples_out[0].position()) / (outputChannels * outputDepth), Integer.MAX_VALUE);
        inputCount = Math.min(inputCount, (outputCount * sampleRate + audio_c.sample_rate() - 1) / audio_c.sample_rate());
        for (int i = 0; samples != null && i < samples.length; i++) {
            samples_in_ptr.put(i, samples_in[i]);
        }
        for (int i = 0; i < samples_out.length; i++) {
            samples_out_ptr.put(i, samples_out[i]);
        }
        if ((ret = swr_convert(samples_convert_ctx, samples_out_ptr, outputCount, samples_in_ptr, inputCount)) < 0) {
            throw new Exception("swr_convert() error " + ret + ": Cannot convert audio samples.");
        } else if (ret == 0) {
            break;
        }
        for (int i = 0; samples != null && i < samples.length; i++) {
            samples_in[i].position(samples_in[i].position() + inputCount * inputChannels * inputDepth);
        }
        for (int i = 0; i < samples_out.length; i++) {
            samples_out[i].position(samples_out[i].position() + ret * outputChannels * outputDepth);
        }
        if (samples == null || samples_out[0].position() >= samples_out[0].limit()) {
            frame.nb_samples(audio_input_frame_size);
            avcodec_fill_audio_frame(frame, audio_c.channels(), outputFormat, samples_out[0], (int) Math.min(samples_out[0].limit(), Integer.MAX_VALUE), 0);
            for (int i = 0; i < samples_out.length; i++) {
                frame.data(i, samples_out[i].position(0));
                frame.linesize(i, (int) Math.min(samples_out[i].limit(), Integer.MAX_VALUE));
            }
            frame.quality(audio_c.global_quality());
            record(frame);
        }
    }
    return samples != null ? frame.key_frame() != 0 : record((AVFrame) null);
}
Also used : DoubleBuffer(java.nio.DoubleBuffer) BytePointer(org.bytedeco.javacpp.BytePointer) DoublePointer(org.bytedeco.javacpp.DoublePointer) FloatBuffer(java.nio.FloatBuffer) ByteBuffer(java.nio.ByteBuffer) ShortPointer(org.bytedeco.javacpp.ShortPointer) FloatPointer(org.bytedeco.javacpp.FloatPointer) IntBuffer(java.nio.IntBuffer) IntPointer(org.bytedeco.javacpp.IntPointer) ShortBuffer(java.nio.ShortBuffer)

Example 93 with DoubleBuffer

use of java.nio.DoubleBuffer in project jmonkeyengine by jMonkeyEngine.

the class BufferUtils method clone.

/**
     * Creates a new DoubleBuffer with the same contents as the given
     * DoubleBuffer. The new DoubleBuffer is separate from the old one and
     * changes are not reflected across. If you want to reflect changes,
     * consider using Buffer.duplicate().
     * 
     * @param buf
     *            the DoubleBuffer to copy
     * @return the copy
     */
public static DoubleBuffer clone(DoubleBuffer buf) {
    if (buf == null) {
        return null;
    }
    buf.rewind();
    DoubleBuffer copy;
    if (isDirect(buf)) {
        copy = createDoubleBuffer(buf.limit());
    } else {
        copy = DoubleBuffer.allocate(buf.limit());
    }
    copy.put(buf);
    return copy;
}
Also used : DoubleBuffer(java.nio.DoubleBuffer)

Example 94 with DoubleBuffer

use of java.nio.DoubleBuffer in project jmonkeyengine by jMonkeyEngine.

the class BufferUtils method createDoubleBuffer.

//// -- GENERAL DOUBLE ROUTINES -- ////
/**
     * Create a new DoubleBuffer of the specified size.
     * 
     * @param size
     *            required number of double to store.
     * @return the new DoubleBuffer
     */
public static DoubleBuffer createDoubleBuffer(int size) {
    DoubleBuffer buf = allocator.allocate(8 * size).order(ByteOrder.nativeOrder()).asDoubleBuffer();
    buf.clear();
    onBufferAllocated(buf);
    return buf;
}
Also used : DoubleBuffer(java.nio.DoubleBuffer)

Example 95 with DoubleBuffer

use of java.nio.DoubleBuffer in project libgdx by libgdx.

the class BufferUtilsTest method benchDouble.

private void benchDouble() {
    DoubleBuffer db = BufferUtils.newDoubleBuffer(1024 * 1024 / 8);
    double[] doubles = new double[1024 * 1024 / 8];
    int len = doubles.length;
    // relative put
    long start = TimeUtils.nanoTime();
    for (int j = 0; j < NUM_MB; j++) {
        db.clear();
        for (int i = 0; i < len; i++) db.put(doubles[i]);
    }
    Gdx.app.log("BufferUtilsTest", "DoubleBuffer relative put: " + (TimeUtils.nanoTime() - start) / 1000000000.0f);
    // absolute put
    start = TimeUtils.nanoTime();
    for (int j = 0; j < NUM_MB; j++) {
        db.clear();
        for (int i = 0; i < len; i++) db.put(i, doubles[i]);
    }
    Gdx.app.log("BufferUtilsTest", "DoubleBuffer absolute put: " + (TimeUtils.nanoTime() - start) / 1000000000.0f);
    // bulk put
    start = TimeUtils.nanoTime();
    for (int j = 0; j < NUM_MB; j++) {
        db.clear();
        db.put(doubles);
    }
    Gdx.app.log("BufferUtilsTest", "DoubleBuffer bulk put: " + (TimeUtils.nanoTime() - start) / 1000000000.0f);
    // JNI put
    start = TimeUtils.nanoTime();
    for (int j = 0; j < NUM_MB; j++) {
        db.clear();
        BufferUtils.copy(doubles, 0, db, len);
    }
    Gdx.app.log("BufferUtilsTest", "DoubleBuffer bulk put: " + (TimeUtils.nanoTime() - start) / 1000000000.0f);
}
Also used : DoubleBuffer(java.nio.DoubleBuffer)

Aggregations

DoubleBuffer (java.nio.DoubleBuffer)162 ByteBuffer (java.nio.ByteBuffer)39 FloatBuffer (java.nio.FloatBuffer)26 IntBuffer (java.nio.IntBuffer)25 ShortBuffer (java.nio.ShortBuffer)22 LongBuffer (java.nio.LongBuffer)14 CharBuffer (java.nio.CharBuffer)11 BufferOverflowException (java.nio.BufferOverflowException)8 IOException (java.io.IOException)5 BufferUnderflowException (java.nio.BufferUnderflowException)5 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 ServerDenseDoubleRow (com.tencent.angel.ps.impl.matrix.ServerDenseDoubleRow)4 Test (org.junit.Test)4 InvalidMarkException (java.nio.InvalidMarkException)3 Random (java.util.Random)3 BytePointer (org.bytedeco.javacpp.BytePointer)3 DoublePointer (org.bytedeco.javacpp.DoublePointer)3 FloatPointer (org.bytedeco.javacpp.FloatPointer)3 IntPointer (org.bytedeco.javacpp.IntPointer)3