Example usage for java.nio ShortBuffer position

List of usage examples for java.nio ShortBuffer position

Introduction

In this page you can find the example usage for java.nio ShortBuffer position.

Prototype

public final Buffer position(int newPosition) 

Source Link

Document

Sets the position of this buffer.

Usage

From source file:Main.java

public static ShortBuffer setupShortBuffer(ShortBuffer preBuffer, short[] array) {

    if (preBuffer == null || preBuffer.capacity() < array.length) {
        preBuffer = createShortBuffer(array.length * 2);
    } else {//from w w  w.j  a  va2 s.  co m
        preBuffer.clear();
    }

    preBuffer.clear();
    preBuffer.put(array);
    preBuffer.position(0);

    return preBuffer;
}

From source file:Main.java

public static ShortBuffer createShortBuffer(short[] shortData) {
    ShortBuffer drawListBuffer;
    ByteBuffer dlb = ByteBuffer.allocateDirect(
            // (# of coordinate values * 2 bytes per short)
            shortData.length * 2);//from   w  w  w .java 2 s.c  o  m
    dlb.order(ByteOrder.nativeOrder());
    drawListBuffer = dlb.asShortBuffer();
    drawListBuffer.put(shortData);
    drawListBuffer.position(0);
    return drawListBuffer;
}

From source file:ee.ioc.phon.android.speak.Utils.java

/**
 * <p>Returns a bitmap that visualizes the given waveform (byte array),
 * i.e. a sequence of 16-bit integers.</p>
 *
 * TODO: show to high/low points in other color
 * TODO: show end pause data with another color
 *//* ww w  . ja  v  a  2  s.  co m*/
public static Bitmap drawWaveform(byte[] waveBuffer, int w, int h, int start, int end) {
    final Bitmap b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    final Canvas c = new Canvas(b);
    final Paint paint = new Paint();
    paint.setColor(0xFFFFFFFF); // 0xRRGGBBAA
    paint.setAntiAlias(true);
    paint.setStrokeWidth(0);

    final Paint redPaint = new Paint();
    redPaint.setColor(0xFF000080);
    redPaint.setAntiAlias(true);
    redPaint.setStrokeWidth(0);

    final ShortBuffer buf = ByteBuffer.wrap(waveBuffer).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer();
    buf.position(0);

    final int numSamples = waveBuffer.length / 2;
    //final int delay = (SAMPLING_RATE * 100 / 1000);
    final int delay = 0;
    int endIndex = end / 2 + delay;
    if (end == 0 || endIndex >= numSamples) {
        endIndex = numSamples;
    }
    int index = start / 2 - delay;
    if (index < 0) {
        index = 0;
    }
    final int size = endIndex - index;
    int numSamplePerPixel = 32;
    int delta = size / (numSamplePerPixel * w);
    if (delta == 0) {
        numSamplePerPixel = size / w;
        delta = 1;
    }

    final float scale = 3.5f / 65536.0f;
    // do one less column to make sure we won't read past
    // the buffer.
    try {
        for (int i = 0; i < w - 1; i++) {
            final float x = i;
            for (int j = 0; j < numSamplePerPixel; j++) {
                final short s = buf.get(index);
                final float y = (h / 2) - (s * h * scale);
                if (s > Short.MAX_VALUE - 10 || s < Short.MIN_VALUE + 10) {
                    // TODO: make it work
                    c.drawPoint(x, y, redPaint);
                } else {
                    c.drawPoint(x, y, paint);
                }
                index += delta;
            }
        }
    } catch (IndexOutOfBoundsException e) {
        // this can happen, but we don't care
    }

    return b;
}