• Java Sound : audio inputstream from pcm amplitude array


    转载自:http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/java-sound-making-audio-input-stream.html

    In this post, i am going to show the code for creating the AudioInputStream from an PCM - amplitude array.

    It basically converts the int [] array to byte array according to AudioFormat.

    The code for the reverse operation (extract amplitude array from recorded wave file or AudioStream )is in my

    earlier post : http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/java-extract-amplitude-array-from.html

    The code for converting PCM amplitude array to AudioStream is follows :

    public static AudioInputStream getAudioStreamFromPCMArray(int[] audioDataIntArr, AudioFormat format) {
            byte[] data = null;
            int nlengthInSamples = audioDataIntArr.length * 2;
            if (format.getSampleSizeInBits() == 16) {
                // FIXME: debug at full length, signed/unsigned problem
                data = new byte[nlengthInSamples];
                if (format.isBigEndian()) {
                    for (int i = 0; i < audioDataIntArr.length; i++) {
                        int temp = Math.abs((short) (audioDataIntArr[i] * 255));
                        data[2 * i + 1] = (byte) temp;
                        data[2 * i + 0] = (byte) (temp >> 8);
                    }
                } else {
                    for (int i = 0; i < audioDataIntArr.length; i++) {
                        int temp = Math.abs((short) (audioDataIntArr[i] * 255));
                        data[2 * i + 0] = (byte) temp;
                        data[2 * i + 1] = (byte) (temp >> 8);
                    }
                }
            } else if (format.getSampleSizeInBits() == 8) {
                nlengthInSamples = audioDataIntArr.length;
                data = new byte[nlengthInSamples];
                if (format.getEncoding().toString().startsWith("PCM_SIGN")) {
                    // PCM_SIGNED
                    for (int i = 0; i < nlengthInSamples; i++) {
                        data[i] = (byte) audioDataIntArr[i];
                    }
                } else {
                    // PCM_UNSIGNED
                    for (int i = 0; i < nlengthInSamples; i++) {
                        data[i] = (byte) (audioDataIntArr[i] + 128);
                    }
                }
            }// end of if..else
            try {
                ByteArrayInputStream bais = new ByteArrayInputStream(data);
                AudioInputStream ais = new AudioInputStream(bais, format, audioDataIntArr.length);
                return ais;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    //## Required Testing -- Comments are much welcomed ...... Thanks
  • 相关阅读:
    【OI新闻】2016.10.06
    旧博客欢迎莅临
    【NYOJ42】一笔画问题
    LCIS最长公共上升子序列
    LIS最长上升子序列
    LCS最长公共子序列
    T2848 列车调度(二分或dp)
    二分图的最大匹配、完美匹配和匈牙利算法
    高精大水题
    最大0,1子矩阵
  • 原文地址:https://www.cnblogs.com/passedbylove/p/11888931.html
Copyright © 2020-2023  润新知