2010-07-13 29 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