bytes To Shorts by Audio Format - Java javax.sound.sampled

Java examples for javax.sound.sampled:Audio

Description

bytes To Shorts by Audio Format

Demo Code


//package com.java2s;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import javax.sound.sampled.AudioFormat;

public class Main {
    public static void bytesToShorts(AudioFormat format, byte[] source,
            int sourcePos, int sourceLen, short[] target, int targetPos) {
        if (format.getSampleSizeInBits() == 16) {
            ByteBuffer bb = ByteBuffer.wrap(source);
            if (format.isBigEndian())
                bb.order(ByteOrder.BIG_ENDIAN);
            else//from   w  ww .  j a va  2s. c  o m
                bb.order(ByteOrder.LITTLE_ENDIAN);
            for (int i = 0; i < sourceLen / 2; i++) {
                target[i + targetPos] = bb.getShort(i * 2 + sourcePos);
            }
        } else if (format.getSampleSizeInBits() == 8) {
            for (int i = 0; i < sourceLen; i++) {
                target[i + targetPos] = (short) (source[i + sourcePos] * 256);
            }
        }
    }
}

Related Tutorials