Exemplo n.º 1
0
  void resetToBind() {
    for (Mesh mesh : targets) {
      if (isMeshAnimated(mesh)) {
        FloatBuffer bwBuff = (FloatBuffer) mesh.getBuffer(Type.BoneWeight).getData();
        ByteBuffer biBuff = (ByteBuffer) mesh.getBuffer(Type.BoneIndex).getData();
        if (!biBuff.hasArray() || !bwBuff.hasArray()) {
          mesh.prepareForAnim(true); // prepare for software animation
        }
        VertexBuffer bindPos = mesh.getBuffer(Type.BindPosePosition);
        VertexBuffer bindNorm = mesh.getBuffer(Type.BindPoseNormal);
        VertexBuffer pos = mesh.getBuffer(Type.Position);
        VertexBuffer norm = mesh.getBuffer(Type.Normal);
        FloatBuffer pb = (FloatBuffer) pos.getData();
        FloatBuffer nb = (FloatBuffer) norm.getData();
        FloatBuffer bpb = (FloatBuffer) bindPos.getData();
        FloatBuffer bnb = (FloatBuffer) bindNorm.getData();
        pb.clear();
        nb.clear();
        bpb.clear();
        bnb.clear();

        // reseting bind tangents if there is a bind tangent buffer
        VertexBuffer bindTangents = mesh.getBuffer(Type.BindPoseTangent);
        if (bindTangents != null) {
          VertexBuffer tangents = mesh.getBuffer(Type.Tangent);
          FloatBuffer tb = (FloatBuffer) tangents.getData();
          FloatBuffer btb = (FloatBuffer) bindTangents.getData();
          tb.clear();
          btb.clear();
          tb.put(btb).clear();
        }

        pb.put(bpb).clear();
        nb.put(bnb).clear();
      }
    }
  }
  @Override
  public void record(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;

    int inputSize = samples[0].limit() - samples[0].position();
    int inputFormat = AV_SAMPLE_FMT_NONE;
    int inputChannels = samples.length > 1 ? 1 : audioChannels;
    int inputDepth = 0;
    int outputFormat = audio_c.sample_fmt();
    int outputChannels = samples_out.length > 1 ? 1 : audioChannels;
    int outputDepth = av_get_bytes_per_sample(outputFormat);
    if (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[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[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[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[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 {
      throw new Exception("Audio samples Buffer has unsupported type: " + samples);
    }

    if (samples_convert_ctx == null) {
      samples_convert_ctx =
          swr_alloc_set_opts(
              null,
              audio_c.channel_layout(),
              outputFormat,
              audio_c.sample_rate(),
              audio_c.channel_layout(),
              inputFormat,
              audio_c.sample_rate(),
              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.");
      }
    }

    for (int i = 0; i < samples.length; i++) {
      samples_in[i]
          .position(samples_in[i].position() * inputDepth)
          .limit((samples_in[i].position() + inputSize) * inputDepth);
    }
    while (samples_in[0].position() < samples_in[0].limit()) {
      int inputCount =
          (samples_in[0].limit() - samples_in[0].position()) / (inputChannels * inputDepth);
      int outputCount =
          (samples_out[0].limit() - samples_out[0].position()) / (outputChannels * outputDepth);
      int count = Math.min(inputCount, outputCount);
      for (int i = 0; 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, count, samples_in_ptr, count))
          < 0) {
        throw new Exception("swr_convert() error " + ret + ": Cannot convert audio samples.");
      }
      for (int i = 0; i < samples.length; i++) {
        samples_in[i].position(samples_in[i].position() + ret * inputChannels * inputDepth);
      }
      for (int i = 0; i < samples_out.length; i++) {
        samples_out[i].position(samples_out[i].position() + ret * outputChannels * outputDepth);
      }

      if (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], samples_out[0].limit(), 0);
        for (int i = 0; i < samples_out.length; i++) {
          frame.data(i, samples_out[i].position(0));
          frame.linesize(i, samples_out[i].limit());
        }
        frame.quality(audio_c.global_quality());
        record(frame);
      }
    }
    return;
  }
Exemplo n.º 3
0
  public static void test(int level, final FloatBuffer b, boolean direct) {

    show(level, b);

    if (direct != b.isDirect()) fail("Wrong direction", b);

    // Gets and puts

    relPut(b);
    relGet(b);
    absGet(b);
    bulkGet(b);

    absPut(b);
    relGet(b);
    absGet(b);
    bulkGet(b);

    bulkPutArray(b);
    relGet(b);

    bulkPutBuffer(b);
    relGet(b);

    // Compact

    relPut(b);
    b.position(13);
    b.compact();
    b.flip();
    relGet(b, 13);

    // Exceptions

    relPut(b);
    b.limit(b.capacity() / 2);
    b.position(b.limit());

    tryCatch(
        b,
        BufferUnderflowException.class,
        new Runnable() {
          public void run() {
            b.get();
          }
        });

    tryCatch(
        b,
        BufferOverflowException.class,
        new Runnable() {
          public void run() {
            b.put((float) 42);
          }
        });

    // The index must be non-negative and lesss than the buffer's limit.
    tryCatch(
        b,
        IndexOutOfBoundsException.class,
        new Runnable() {
          public void run() {
            b.get(b.limit());
          }
        });
    tryCatch(
        b,
        IndexOutOfBoundsException.class,
        new Runnable() {
          public void run() {
            b.get(-1);
          }
        });

    tryCatch(
        b,
        IndexOutOfBoundsException.class,
        new Runnable() {
          public void run() {
            b.put(b.limit(), (float) 42);
          }
        });

    tryCatch(
        b,
        InvalidMarkException.class,
        new Runnable() {
          public void run() {
            b.position(0);
            b.mark();
            b.compact();
            b.reset();
          }
        });

    // Values

    b.clear();
    b.put((float) 0);
    b.put((float) -1);
    b.put((float) 1);
    b.put(Float.MAX_VALUE);
    b.put(Float.MIN_VALUE);

    b.put(-Float.MAX_VALUE);
    b.put(-Float.MIN_VALUE);
    b.put(Float.NEGATIVE_INFINITY);
    b.put(Float.POSITIVE_INFINITY);
    b.put(Float.NaN);
    b.put(0.91697687f); // Changes value if incorrectly swapped

    float v;
    b.flip();
    ck(b, b.get(), 0);
    ck(b, b.get(), (float) -1);
    ck(b, b.get(), 1);
    ck(b, b.get(), Float.MAX_VALUE);
    ck(b, b.get(), Float.MIN_VALUE);

    ck(b, b.get(), -Float.MAX_VALUE);
    ck(b, b.get(), -Float.MIN_VALUE);
    ck(b, b.get(), Float.NEGATIVE_INFINITY);
    ck(b, b.get(), Float.POSITIVE_INFINITY);
    if (Float.floatToRawIntBits(v = b.get()) != Float.floatToRawIntBits(Float.NaN))
      fail(b, (long) Float.NaN, (long) v);
    ck(b, b.get(), 0.91697687f);

    // Comparison
    b.rewind();
    FloatBuffer b2 = FloatBuffer.allocate(b.capacity());
    b2.put(b);
    b2.flip();
    b.position(2);
    b2.position(2);
    if (!b.equals(b2)) {
      for (int i = 2; i < b.limit(); i++) {
        float x = b.get(i);
        float y = b2.get(i);
        if (x != y || Float.compare(x, y) != 0) out.println("[" + i + "] " + x + " != " + y);
      }
      fail("Identical buffers not equal", b, b2);
    }
    if (b.compareTo(b2) != 0) fail("Comparison to identical buffer != 0", b, b2);

    b.limit(b.limit() + 1);
    b.position(b.limit() - 1);
    b.put((float) 99);
    b.rewind();
    b2.rewind();
    if (b.equals(b2)) fail("Non-identical buffers equal", b, b2);
    if (b.compareTo(b2) <= 0) fail("Comparison to shorter buffer <= 0", b, b2);
    b.limit(b.limit() - 1);

    b.put(2, (float) 42);
    if (b.equals(b2)) fail("Non-identical buffers equal", b, b2);
    if (b.compareTo(b2) <= 0) fail("Comparison to lesser buffer <= 0", b, b2);

    // Check equals and compareTo with interesting values
    for (float x : VALUES) {
      FloatBuffer xb = FloatBuffer.wrap(new float[] {x});
      if (xb.compareTo(xb) != 0) {
        fail("compareTo not reflexive", xb, xb, x, x);
      }
      if (!xb.equals(xb)) {
        fail("equals not reflexive", xb, xb, x, x);
      }
      for (float y : VALUES) {
        FloatBuffer yb = FloatBuffer.wrap(new float[] {y});
        if (xb.compareTo(yb) != -yb.compareTo(xb)) {
          fail("compareTo not anti-symmetric", xb, yb, x, y);
        }
        if ((xb.compareTo(yb) == 0) != xb.equals(yb)) {
          fail("compareTo inconsistent with equals", xb, yb, x, y);
        }
        if (xb.compareTo(yb) != Float.compare(x, y)) {

          if (x == 0.0 && y == 0.0) continue;

          fail("Incorrect results for FloatBuffer.compareTo", xb, yb, x, y);
        }
        if (xb.equals(yb) != ((x == y) || ((x != x) && (y != y)))) {
          fail("Incorrect results for FloatBuffer.equals", xb, yb, x, y);
        }
      }
    }

    // Sub, dup

    relPut(b);
    relGet(b.duplicate());
    b.position(13);
    relGet(b.duplicate(), 13);
    relGet(b.duplicate().slice(), 13);
    relGet(b.slice(), 13);
    relGet(b.slice().duplicate(), 13);

    // Slice

    b.position(5);
    FloatBuffer sb = b.slice();
    checkSlice(b, sb);
    b.position(0);
    FloatBuffer sb2 = sb.slice();
    checkSlice(sb, sb2);

    if (!sb.equals(sb2)) fail("Sliced slices do not match", sb, sb2);
    if ((sb.hasArray()) && (sb.arrayOffset() != sb2.arrayOffset()))
      fail("Array offsets do not match: " + sb.arrayOffset() + " != " + sb2.arrayOffset(), sb, sb2);

    // Read-only views

    b.rewind();
    final FloatBuffer rb = b.asReadOnlyBuffer();
    if (!b.equals(rb)) fail("Buffer not equal to read-only view", b, rb);
    show(level + 1, rb);

    tryCatch(
        b,
        ReadOnlyBufferException.class,
        new Runnable() {
          public void run() {
            relPut(rb);
          }
        });

    tryCatch(
        b,
        ReadOnlyBufferException.class,
        new Runnable() {
          public void run() {
            absPut(rb);
          }
        });

    tryCatch(
        b,
        ReadOnlyBufferException.class,
        new Runnable() {
          public void run() {
            bulkPutArray(rb);
          }
        });

    tryCatch(
        b,
        ReadOnlyBufferException.class,
        new Runnable() {
          public void run() {
            bulkPutBuffer(rb);
          }
        });

    // put(FloatBuffer) should not change source position
    final FloatBuffer src = FloatBuffer.allocate(1);
    tryCatch(
        b,
        ReadOnlyBufferException.class,
        new Runnable() {
          public void run() {
            rb.put(src);
          }
        });
    ck(src, src.position(), 0);

    tryCatch(
        b,
        ReadOnlyBufferException.class,
        new Runnable() {
          public void run() {
            rb.compact();
          }
        });

    if (rb.getClass().getName().startsWith("java.nio.Heap")) {

      tryCatch(
          b,
          ReadOnlyBufferException.class,
          new Runnable() {
            public void run() {
              rb.array();
            }
          });

      tryCatch(
          b,
          ReadOnlyBufferException.class,
          new Runnable() {
            public void run() {
              rb.arrayOffset();
            }
          });

      if (rb.hasArray()) fail("Read-only heap buffer's backing array is accessible", rb);
    }

    // Bulk puts from read-only buffers

    b.clear();
    rb.rewind();
    b.put(rb);

    relPut(b); // Required by testViews
  }