Example usage for java.nio ShortBuffer get

List of usage examples for java.nio ShortBuffer get

Introduction

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

Prototype

public abstract short get();

Source Link

Document

Returns the short at the current position and increases the position by 1.

Usage

From source file:Main.java

public static void main(String[] args) {
    ShortBuffer bb = ShortBuffer.allocate(10);
    bb.put((short) 100);

    bb.rewind();// w  w  w  . j av a  2  s  .  c  o m
    System.out.println(bb.get());

}

From source file:MainClass.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, 'a' });
    bb.rewind();//from w ww  .ja  v  a 2s. c  o  m
    ShortBuffer sb = ((ByteBuffer) bb.rewind()).asShortBuffer();
    System.out.println("Short Buffer");
    while (sb.hasRemaining())
        System.out.println(sb.position() + " -> " + sb.get());

}

From source file:Main.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, 'a' });
    bb.rewind();/*from www  . j  av  a2s  . c o  m*/
    System.out.println("Byte Buffer");
    while (bb.hasRemaining())
        System.out.println(bb.position() + " -> " + bb.get());
    CharBuffer cb = ((ByteBuffer) bb.rewind()).asCharBuffer();
    System.out.println("Char Buffer");
    while (cb.hasRemaining())
        System.out.println(cb.position() + " -> " + cb.get());
    FloatBuffer fb = ((ByteBuffer) bb.rewind()).asFloatBuffer();
    System.out.println("Float Buffer");
    while (fb.hasRemaining())
        System.out.println(fb.position() + " -> " + fb.get());
    IntBuffer ib = ((ByteBuffer) bb.rewind()).asIntBuffer();
    System.out.println("Int Buffer");
    while (ib.hasRemaining())
        System.out.println(ib.position() + " -> " + ib.get());
    LongBuffer lb = ((ByteBuffer) bb.rewind()).asLongBuffer();
    System.out.println("Long Buffer");
    while (lb.hasRemaining())
        System.out.println(lb.position() + " -> " + lb.get());
    ShortBuffer sb = ((ByteBuffer) bb.rewind()).asShortBuffer();
    System.out.println("Short Buffer");
    while (sb.hasRemaining())
        System.out.println(sb.position() + " -> " + sb.get());
    DoubleBuffer db = ((ByteBuffer) bb.rewind()).asDoubleBuffer();
    System.out.println("Double Buffer");
    while (db.hasRemaining())
        System.out.println(db.position() + " -> " + db.get());
}

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 {/*ww  w  .ja v  a  2s  . c om*/
            sb.append(bb.get());
        }
        sb.append(',');
        sb.append('\n');
    }

    return sb.toString();
}

From source file:io.github.dsheirer.record.wave.MonoWaveReader.java

/**
 * Opens the file//from  w w  w.  j a  v  a  2s  .c  o m
 */
private void open() throws IOException {
    if (!Files.exists(mPath)) {
        throw new IOException("File not found");
    }

    mInputStream = Files.newInputStream(mPath, StandardOpenOption.READ);

    //Check for RIFF header
    byte[] buffer = new byte[4];
    mInputStream.read(buffer);

    if (!Arrays.equals(buffer, WaveUtils.RIFF_CHUNK)) {
        throw new IOException("File is not .wav format - missing RIFF chunk");
    }

    //Get file size
    mInputStream.read(buffer);
    int fileSize = ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer().get();

    //Check for WAVE format
    mInputStream.read(buffer);

    if (!Arrays.equals(buffer, WaveUtils.WAV_FORMAT)) {
        throw new IOException("File is not .wav format - missing WAVE format");
    }

    //Check for format chunk
    mInputStream.read(buffer);

    if (!Arrays.equals(buffer, WaveUtils.CHUNK_FORMAT)) {
        throw new IOException("File is not .wav format - missing format chunk");
    }

    //Get chunk size
    mInputStream.read(buffer);
    int chunkSize = ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer().get();

    //Get format
    mInputStream.read(buffer);

    ShortBuffer shortBuffer = ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer();
    short format = shortBuffer.get();
    if (format != WaveUtils.PCM_FORMAT) {
        throw new IOException("File format not supported - expecting PCM format");
    }

    //Get number of channels
    short channels = shortBuffer.get();
    if (channels != 1) {
        throw new IOException("Unsupported channel count - mono audio only");
    }

    //Get samples per second
    mInputStream.read(buffer);
    int sampleRate = ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer().get();

    //Get bytes per second
    mInputStream.read(buffer);
    int bytesPerSecond = ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer().get();

    mInputStream.read(buffer);

    //Get frame size
    shortBuffer = ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer();
    short frameSize = shortBuffer.get();
    if (frameSize != 2) {
        throw new IOException("PCM frame size not supported - expecting 2 bytes per frame");
    }

    //Get bits per sample
    short bitsPerSample = shortBuffer.get();
    if (bitsPerSample != 16) {
        throw new IOException("PCM sample size not supported - expecting 16 bits per sample");
    }

    mInputStream.read(buffer);

    if (!Arrays.equals(buffer, WaveUtils.CHUNK_DATA)) {
        throw new IOException("Unexpected chunk - expecting data chunk");
    }

    //Get data chunk size
    mInputStream.read(buffer);
    mDataByteSize = ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer().get();
}

From source file:org.nuras.mcpha.Client.java

/**
 * // w w w . j a v a  2  s  .  c  o  m
 * @param user
 * @param channels
 * @param trigger_mode
 * @param trigger_level
 * @param trigger_slope
 * @param trigger_source
 * @throws IOException 
 */
synchronized public static void acquireOscilloscopeData(Session user, int channels, String trigger_mode,
        int trigger_level, String trigger_slope, int trigger_source) throws IOException {
    mcphaResetOscilloscope();

    // Set number of samples to skip before trigger
    mcphaSetNumberOfSamplesBeforeTrigger(5000);

    // Set total number of samples to acquire for this run
    mcphaSetTotalNumberOfSamplesToAcquire(65536);

    // Start oscilloscope
    mcphaStartOscilloscope();

    // wait for 200 milliseconds
    try {
        Thread.sleep(200);
    } catch (InterruptedException ex) {
        Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
    }

    // Read oscilloscope status
    for (int i = 0; i < 5; i++) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("Oscilloscope status:" + mcphaReadOscilloscopeStatus());
    }

    // get oscillsocope data
    ShortBuffer data = mcphaGetOsilloscopeData();

    boolean channel_1_requested = (channels & 0x01) != 0;
    boolean channel_2_requested = (channels & 0x02) != 0;

    // push data
    JSONObject json = createJSONResponseObject();
    json.put("command", "get_oscilloscope_data");
    json.put("message", "");
    json.put("status", 0);

    JSONArray arr1 = new JSONArray();
    JSONArray arr2 = new JSONArray();
    data.rewind();
    for (int i = 0, n1 = 0, n2 = 0; i < data.capacity(); i += 2) {
        // channel 1 data
        JSONArray xy = new JSONArray();
        short d = data.get();
        if (channel_1_requested) {
            arr1.put(xy.put(n1++).put(d));
        }
        // channel 2 data
        xy = new JSONArray();
        d = data.get();
        if (channel_2_requested) {
            arr2.put(xy.put(n2++).put(d));
        }
    }
    json.put("data1", arr1);
    json.put("label1", channel_1_requested ? "Channel 1" : "");

    json.put("data2", arr2);
    json.put("label2", channel_2_requested ? "Channel 2" : "");

    sendJSONObjectMessage(user.getRemote(), json);

}

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) *///from w w  w .  j  a va 2s .  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;/*from   w  w  w . ja va  2 s .  c o  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 calcHistogram(ShortBuffer depthBuf) {
    // reset histogram
    for (int i = 0; i <= maxDepth; i++)
        histogram[i] = 0;//from w w  w.j a  v a 2s .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 updateUserDepths() {
    depthMD = depthGen.getMetaData();//from   ww w.  ja  v a  2s  . co  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());
        }
    }
}