2014-10-10 123 views
1

我在Android應用程序上使用LAME以將PCM數據從AudioRecoder轉換爲MP3。我可以生成MP3文件,但它有一些噪音。Android:LAME將PCM數據從AudioRecoder轉換爲MP3,但MP3文件有一些噪音

這是我的部分JNI代碼:

jbyte *bufferArray = (*env)->GetByteArrayElements(env, buffer, NULL); 
//Convert To Short 
int shortLength = length/2; 
short int shortBufferArray[shortLength]; 
int i ; 
for(i=0;i<shortLength;i++){ 
    int index = 2*i; 
    shortBufferArray[i] = (bufferArray[index+1] << 8) | bufferArray[index]; 
} 

int mp3BufferSize = (int)(shortLength*1.5+7200); 
unsigned char output[mp3BufferSize]; 
int encodeResult; 
if(lame_get_num_channels(lame)==2){ 
    encodeResult = lame_encode_buffer_interleaved(lame, shortBufferArray, shortLength/2, output, mp3BufferSize); 
}else{ 
    encodeResult = lame_encode_buffer(lame, shortBufferArray, shortBufferArray, shortLength, output, mp3BufferSize); 
} 

if(encodeResult < 0){ 
    return NULL; 
} 
jbyteArray result = (*env)->NewByteArray(env, encodeResult); 
(*env)->SetByteArrayRegion(env, result, 0, encodeResult, output); 
(*env)->ReleaseByteArrayElements(env, buffer, bufferArray, 0); 
return result; 

我把這個JNI函數來編碼PCM數據,MP3數據,而且比我寫的MP3數據文件生成一個MP3文件。 PCM數據全部編碼後,將生成MP3文件。看起來,播放MP3文件是正常的,但即使使用320kbps的比特率,MP3文件的質量也很差。 MP3文件中有一些噪音,但爲什麼?

回答

3
shortBufferArray[i] = (bufferArray[index+1] << 8) | bufferArray[index]; 

當較不重要的字節隱含地(並且出於您的目的,不正確地)簽名擴展爲短片時將引入錯誤。

相反,使用

shortBufferArray[i] = (bufferArray[index+1] << 8) | ((unsigned char) bufferArray[index]); 

,迫使它治療低字節爲不具有符號位(僅上字節一樣)。