Method to convert an amplitude value into Pulse Code Modulation bytes for Sound. - Android Media

Android examples for Media:Sound

Description

Method to convert an amplitude value into Pulse Code Modulation bytes for Sound.

Demo Code


//package com.java2s;

public class Main {
    /**/*ww w . jav  a2  s  .c  o m*/
     * Method to convert an amplitude value into Pulse Code Modulation bytes.
     *
     * @param samples The raw amplitude value.
     * @return An array of Pulse Code Modulation audio bytes.
     */
    public static final byte[] toPCM(final double[] samples) {
        final byte[] pcm = new byte[samples.length * 2];
        int destination = 0;
        for (double sample : samples) {
            final short val = (short) (sample * 32767);
            // in 16 bit wav PCM, first byte is the low order byte
            pcm[destination++] = (byte) (val & 0x00ff);
            pcm[destination++] = (byte) ((val & 0xff00) >>> 8);
        }
        return pcm;
    }
}

Related Tutorials