关闭

二进制音频流文件解码成数值文件遇到的问题

标签: audio音频解码
767人阅读 评论(0) 收藏 举报
分类:
这几天一直在弄音频文件的解码问题
纠结我好久
public class AudioSampleReader {
    
    private AudioInputStream audioInputStream;
    private AudioFormat format;
    
    public AudioSampleReader(File file)
            throws UnsupportedAudioFileException, IOException {
        audioInputStream = AudioSystem.getAudioInputStream(file);
        format = audioInputStream.getFormat();
    }


    public AudioFormat getFormat() {
        return format;
    }
    
//获取样本数目
    public long getSampleCount() {
        long total = (audioInputStream.getFrameLength() *
            format.getFrameSize() * 8) / format.getSampleSizeInBits();
      return total / format.getChannels();   
        
    }
 //获取十进制样本值   
    public void getInterleavedSamples(long begin, long end,
            double[] samples) throws IOException,
                IllegalArgumentException {
        long nbSamples = end - begin;
        long nbBytes = nbSamples * (format.getSampleSizeInBits() / 8) *format.getChannels();
        if (nbBytes > Integer.MAX_VALUE)
            throw new IllegalArgumentException("too many samples");
        byte[] inBuffer = new byte[(int)nbBytes];
        // read bytes from audio file
        audioInputStream.read(inBuffer, 0, inBuffer.length);
        // decode bytes into samples. Supported encodings are:
        // PCM-SIGNED, PCM-UNSIGNED, A-LAW, U-LAW
        double ratio = Math.pow(2., format.getSampleSizeInBits() - 1);  
        decodeBytes(inBuffer, samples,ratio);
    }
    //获取特定声道的样本值
    public void getChannelSamples(int channel,
            double[] interleavedSamples, double[] channelSamples) {
        int nbChannels = format.getChannels();
        for (int i = 0; i < channelSamples.length; i++) {
            channelSamples[i] = interleavedSamples[nbChannels*i + channel];
        }
    }
    
//分别获取左右声道
    public void getStereoSamples(double[] leftSamples, double[] rightSamples)
            throws IOException {
        long sampleCount = getSampleCount();
        double[] interleavedSamples = new double[(int)sampleCount*2];
        getInterleavedSamples(0, sampleCount, interleavedSamples);


        for (int i = 0; i < leftSamples.length; i++) {
            leftSamples[i] = interleavedSamples[2*i];
            rightSamples[i] = interleavedSamples[2*i+1];
        }        
    }


//解码函数
private void decodeBytes(byte[] audioBytes, double[] audioSamples,double ratio) {
        int sampleSizeInBytes = format.getSampleSizeInBits() / 8;
        int[] sampleBytes = new int[sampleSizeInBytes];
        int k = 0; // index in audioBytes
        for (int i = 0; i < audioSamples.length; i++) {
            // collect sample byte in big-endian order
            if (format.isBigEndian()) {
                // bytes start with MSB
                for (int j = 0; j < sampleSizeInBytes; j++) {
                    sampleBytes[j] = audioBytes[k++];
                   
                }
            } else {
                // bytes start with LSB
                for (int j = sampleSizeInBytes - 1; j >= 0; j--) {
                    sampleBytes[j] = audioBytes[k++];
                    if (sampleBytes[j] != 0)
                        j = j + 0;
                }
            }
            int ival = 0;
            
            for (int j = 0; j < sampleSizeInBytes; j++) {
                ival += sampleBytes[j];
if (j < sampleSizeInBytes - 1) ival <<= 8;  //左移8位相当于乘以2的8次方(一个字节占8位),高8位*2^8+低8位既为十进制解码
            }
           
            // decode value
            //但是结果这样子算结果是不对的!!!!
//与matlabwavread函数结果不一样!!!!
            
           double val = ((double) ival+256) / ratio; // 然后,我发现给ival加上256,结果就与matlab一致了,这是为什么,为什么!!!!!!!!
            audioSamples[i] = val;
        }
    }
    
}


给ival加上256,结果就与matlab一致了,这是为什么,为什么!!!!!!!!
给ival加上256,结果就与matlab一致了,这是为什么,为什么!!!!!!!!
给ival加上256,结果就与matlab一致了,这是为什么,为什么!!!!!!!!
给ival加上256,结果就与matlab一致了,这是为什么,为什么!!!!!!!!
给ival加上256,结果就与matlab一致了,这是为什么,为什么!!!!!!!!




求高手解答!!!!!
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1115次
    • 积分:30
    • 等级:
    • 排名:千里之外
    • 原创:2篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档