[英]Android: LAME converts PCM data from AudioRecoder to MP3, but the MP3 file has some noise
我在我的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文件中有一些噪音,但為什么呢?
shortBufferArray[i] = (bufferArray[index+1] << 8) | bufferArray[index];
當較不重要的字節隱含地(並且為了您的目的,不正確地)將符號擴展為短時,將引入錯誤。
相反,使用
shortBufferArray[i] = (bufferArray[index+1] << 8) | ((unsigned char) bufferArray[index]);
強制它將低位字節視為沒有符號位(只有高位字節)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.