安卓系统: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文件中有一些噪音,但为什么?

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

当低有效字节隐式(出于您的目的,不正确)符号扩展为短字节时,将引入错误。

相反,使用

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

以强制它将低位字节视为没有符号位(只有高位字节有符号位)。

最新更新