• 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
  • 相关阅读:
    Hbase­优化方案
    ssh 登录
    微软2017校招笔试题3 registration day
    微软2017校招笔试题2 composition
    STL中的查找算法
    leetcode-188 买卖股票4
    leetcode-306 Additive Number
    网络安全(3): 数据完整性校验
    网络安全(2)-数据加解密
    linux共享库
  • 原文地址:https://www.cnblogs.com/passedbylove/p/11888931.html
Copyright © 2020-2023  润新知