Example usage for java.nio ShortBuffer remaining

List of usage examples for java.nio ShortBuffer remaining

Introduction

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

Prototype

public final int remaining() 

Source Link

Document

Returns the number of remaining elements in this buffer, that is limit - position .

Usage

From source file:Main.java

public static ByteBuffer copyShortBufferAsByteBuffer(ShortBuffer buf) {
    ByteBuffer dest = newByteBuffer(buf.remaining() * SIZEOF_SHORT);
    buf.mark();//from   w  w  w  .j a va2  s.c o m
    dest.asShortBuffer().put(buf);
    buf.reset();
    dest.rewind();
    return dest;
}

From source file:Main.java

private static String formatShorts(byte[] data, boolean unsigned) {
    ShortBuffer bb = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer();

    StringBuilder sb = new StringBuilder(bb.capacity() * 3);

    while (bb.remaining() > 0) {
        if (unsigned) {
            sb.append(bb.get() & 0xffff);
        } else {//from  ww w  .  j  av  a 2  s . c  o  m
            sb.append(bb.get());
        }
        sb.append(',');
        sb.append('\n');
    }

    return sb.toString();
}

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

static Bitmap bytesToBitmap(byte[] byteBuffer, int w, int h, int startPosition, int endPosition) {
    final ShortBuffer waveBuffer = ByteBuffer.wrap(byteBuffer).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer();
    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); // 0xAARRGGBB
    paint.setAntiAlias(true);//ww w  .  j a  v  a 2 s  . c o m
    paint.setStyle(Paint.Style.STROKE);
    paint.setAlpha(80);

    final PathEffect effect = new CornerPathEffect(3);
    paint.setPathEffect(effect);

    final int numSamples = waveBuffer.remaining();
    int endIndex;
    if (endPosition == 0) {
        endIndex = numSamples;
    } else {
        endIndex = Math.min(endPosition, numSamples);
    }

    int startIndex = startPosition - 2000; // include 250ms before speech
    if (startIndex < 0) {
        startIndex = 0;
    }
    final int numSamplePerWave = 200; // 8KHz 25ms = 200 samples
    final float scale = 10.0f / 65536.0f;

    final int count = (endIndex - startIndex) / numSamplePerWave;
    final float deltaX = 1.0f * w / count;
    int yMax = h / 2;
    Path path = new Path();
    c.translate(0, yMax);
    float x = 0;
    path.moveTo(x, 0);
    for (int i = 0; i < count; i++) {
        final int avabs = getAverageAbs(waveBuffer, startIndex, i, numSamplePerWave);
        int sign = ((i & 01) == 0) ? -1 : 1;
        final float y = Math.min(yMax, avabs * h * scale) * sign;
        path.lineTo(x, y);
        x += deltaX;
        path.lineTo(x, y);
    }
    if (deltaX > 4) {
        paint.setStrokeWidth(2);
    } else {
        paint.setStrokeWidth(Math.max(0, (int) (deltaX - .05)));
    }
    c.drawPath(path, paint);
    return b;
}

From source file:c.depthchart.ViewerPanel.java

private void updateDepthImage()
/* build a new histogram of 8-bit depth values, and convert it to
   image pixels (as bytes) */// w  w w  .  j a v  a  2  s  .  com
{
    ShortBuffer depthBuf = depthMD.getData().createShortBuffer();
    calcHistogram(depthBuf);
    depthBuf.rewind();

    while (depthBuf.remaining() > 0) {
        int pos = depthBuf.position();
        short depth = depthBuf.get();
        imgbytes[pos] = (byte) histogram[depth];
    }
}

From source file:c.depthchart.ViewerPanel.java

private void calcHistogram(ShortBuffer depthBuf) {
    // reset histogram[]
    for (int i = 0; i <= maxDepth; i++)
        histogram[i] = 0;/*www.  j  a va2s .  co  m*/

    // record number of different depths in histogram[]
    int numPoints = 0;
    maxDepth = 0;
    while (depthBuf.remaining() > 0) {
        short depthVal = depthBuf.get();
        if (depthVal > maxDepth)
            maxDepth = depthVal;
        if ((depthVal != 0) && (depthVal < MAX_DEPTH_SIZE)) { // skip histogram[0]
            histogram[depthVal]++;
            numPoints++;
        }
    }
    // System.out.println("No. of numPoints: " + numPoints);
    // System.out.println("Maximum depth: " + maxDepth);

    if (chartTime > CHART_DELAY) {
        updateChart(histogram, maxDepth);
        chartTime = 0;
    }

    // convert into a cummulative depth count (skipping histogram[0])
    for (int i = 1; i <= maxDepth; i++)
        histogram[i] += histogram[i - 1];

    /* convert cummulative depth into 8-bit range (0-255), which will later become grayscales
        - darker means further away, although black
          means "too close" for a depth value to be calculated).
    */
    if (numPoints > 0) {
        for (int i = 1; i <= maxDepth; i++) // skipping histogram[0]
            histogram[i] = (int) (256 * (1.0f - (histogram[i] / (float) numPoints)));
    }
}

From source file:TrackerPanel.java

private void updateUserDepths() {
    depthMD = depthGen.getMetaData();/*from   ww  w .j av  a2  s .  c o m*/
    ShortBuffer depthBuf = depthMD.getData().createShortBuffer();
    calcHistogram(depthBuf);
    depthBuf.rewind();

    ShortBuffer usersBuf = sceneMD.getData().createShortBuffer();

    while (depthBuf.remaining() > 0) {
        int pos = depthBuf.position();
        short depthVal = depthBuf.get();
        short userID = usersBuf.get();

        imgbytes[3 * pos] = 0; // default colour is black when there's no
        // depth data
        imgbytes[3 * pos + 1] = 0;
        imgbytes[3 * pos + 2] = 0;

        if (depthVal != 0) { // there is depth data
            // convert userID to index into USER_COLORS[]
            int colorIdx = userID % (USER_COLORS.length - 1); // skip last
            // color

            if (userID == 0) // not a user; actually the background
                colorIdx = USER_COLORS.length - 1;
            // use last index: the position of white in USER_COLORS[]

            // convert histogram value (0.0-1.0f) to a RGB color
            float histValue = histogram[depthVal];
            imgbytes[3 * pos] = (byte) (histValue * USER_COLORS[colorIdx].getRed());
            imgbytes[3 * pos + 1] = (byte) (histValue * USER_COLORS[colorIdx].getGreen());
            imgbytes[3 * pos + 2] = (byte) (histValue * USER_COLORS[colorIdx].getBlue());
        }
    }
}

From source file:TrackerPanel.java

private void calcHistogram(ShortBuffer depthBuf) {
    // reset histogram
    for (int i = 0; i <= maxDepth; i++)
        histogram[i] = 0;//from  w ww  .j  a v a2s  .  co m

    // record number of different depths in histogram[]
    int numPoints = 0;
    maxDepth = 0;
    while (depthBuf.remaining() > 0) {
        short depthVal = depthBuf.get();
        if (depthVal > maxDepth)
            maxDepth = depthVal;
        if ((depthVal != 0) && (depthVal < MAX_DEPTH_SIZE)) { // skip
            // histogram[0]
            histogram[depthVal]++;
            numPoints++;
        }
    }
    // System.out.println("No. of numPoints: " + numPoints);
    // System.out.println("Maximum depth: " + maxDepth);

    // convert into a cummulative depth count (skipping histogram[0])
    for (int i = 1; i <= maxDepth; i++)
        histogram[i] += histogram[i - 1];

    /*
     * convert cummulative depth into the range 0.0 - 1.0f which will later
     * be used to modify a color from USER_COLORS[]
     */
    if (numPoints > 0) {
        for (int i = 1; i <= maxDepth; i++)
            // skipping histogram[0]
            histogram[i] = 1.0f - (histogram[i] / (float) numPoints);
    }
}

From source file:TrackerPanel.java

private void hideBackground(int[] cameraPixels) {
    depthMD = depthGen.getMetaData();/*from  ww w.  j av a2 s  . c  o  m*/

    ShortBuffer usersBuf = sceneMD.getData().createShortBuffer();
    int userCount = 0;
    int maxUserCount = 0;
    int userCountMaxRow = 0;
    int maxY = 0;
    int row = 0;
    int rowCount = 0;

    while (usersBuf.remaining() > 0) {
        int pos = usersBuf.position();
        short userID = usersBuf.get();
        if (userID == 0) {
            // if not a user (i.e. is part of the background)
            cameraPixels[pos] = hideBGPixel; // make pixel transparent
        } else {
            userCount++;
            int y = imHeight - row;
            if (y > maxY) {
                maxY = y;
            }
        }
        rowCount++;
        if (rowCount == imWidth) {
            if (userCount > maxUserCount) {
                maxUserCount = userCount;
                userCountMaxRow = row;
            }
            row++;
            rowCount = 0;
            userCount = 0;
        }
    }
    int startPoint = imWidth * (imHeight - maxY);
    int finalPoint = startPoint + imWidth;
    if (maxY != 0) {
        printLine(startPoint, finalPoint, Color.YELLOW.getRGB());
    }
    startPoint = imWidth * (userCountMaxRow);
    finalPoint = startPoint + imWidth;
    if (userCountMaxRow != 0) {
        SimpleHTTPPOSTRequester httpPost = new SimpleHTTPPOSTRequester();
        try {
            httpPost.makeHTTPPOSTRequest(userCountMaxRow);
        } catch (ParseException e1) {
            e1.printStackTrace();
        }
        Util.insertLastPoint(userCountMaxRow);
        int response = Util.analyzeLastFivePoints();

        if (response == Util.NOT_READY) {
            printLine(startPoint, finalPoint, Color.RED.getRGB());
        } else if (response == Util.READY) {
            printLine(startPoint, finalPoint, Color.WHITE.getRGB());
        } else if (response == Util.GOING_UP) {
            printLine(startPoint, finalPoint, Color.GREEN.getRGB());
        } else if (response == Util.GOING_DOWN) {
            printLine(startPoint, finalPoint, Color.PINK.getRGB());
        }

    }
}