2010-07-13 28 views
1

我需要你的帮助。 我想使用下面的代码,因为我正在开发一个音频工具(仅适用于.wav文件),其主要功能是显示信号波形。 Big Endian以及Little endian是我无法处理的元素。 我是否正确地认为以下代码以如下方式处理问题: - 如果音频文件样本大小为16位或8位,并且Big endian或Little endian它使用audioData数组重新排列样本?了解大端,小端(再次)

如果我的推理是正确的,重排是否总是从大端到小端?

是否将计算机架构考虑在内? (我的意思是,如果我的计算机使用小尾数,会发生什么?)

感谢您在Java

int[] audioData = null; 

if (format.getSampleSizeInBits() == 16) { 
       int nlengthInSamples = audioBytes.size()/2; 
       audioData = new int[nlengthInSamples]; 
       // se Big Endian 
       if (format.isBigEndian()) { 
        for (int i = 0; i < nlengthInSamples; i++) { 
         // MSB calculation - Most Significant Bit 
         int MSB = (int) audioBytes.get(2 * i); 
         // LSB calculation - Least Significant Bit 
         int LSB = (int) audioBytes.get(2 * i + 1); 
         // (MSB << 8) MSB shift to the left by 8 position 
         // (255 & LSB) LSB masking so that LSB is <255 or =0 
         // putting both together 
         audioData[i] = MSB << 8 | (255 & LSB); 
        } 
       } else { 
        for (int i = 0; i < nlengthInSamples; i++) { 
         int LSB = (int) audioBytes.get(2 * i); 
         int MSB = (int) audioBytes.get(2 * i + 1); 
         audioData[i] = MSB << 8 | (255 & LSB); 
        } 
       } 

回答

2

一切都是大端,所以你不必担心。你有什么看起来是正确的(显然,测试它肯定),但我会建议使用以下模式,以避免代码复制的道路。

int nlengthInSamples = audioBytes.size()/2; 
audioData = new int[nlengthInSamples]; 

// default BigEndian 
int MSB = (int) audioBytes.get(2 * i); 
int LSB = (int) audioBytes.get(2 * i + 1); 

// swap for Little Endian 
if(!format.isBigEndian()) { 
    int temp = MSB; 
    MSB = LSB; 
    LSB = temp; 
} 

audioData[i] = MSB << 8 | (255 & LSB); 
+0

您仍然需要一个循环。 – 2010-07-13 21:19:47