mp3 pcm lame android,java - converting pcm file to mp3 using liblame in android - Stack Overflow

在尝试使用SimpleLameLib将使用AudioRecord类录制的8000Hz、单声道、16位PCM文件转换为MP3时,发现生成的MP3文件存在大量噪声且难以理解。代码中设置了正确的参数,但似乎缓冲区大小和处理流程可能存在问题。为了解决这个问题,需要检查编码和写入过程,确保PCM数据正确读取和转换。
摘要由CSDN通过智能技术生成

I am using SimpleLameLibForAndroid to convert a pcm file that created using AudioRecord class in android,to mp3. I read the pcm file and encoded it into mp3 and then I write it in the file. the result mp3 file but is not correct and it has a lot of noise on it and really hard to understand that it was recorded pcm file.

these are recorded audio specifications(pcm file):

private static final int RECORDER_SAMPLERATE = 8000;

private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;

private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;

int BufferElements2Rec = 1024; // want to play 2048 (2K) since 2 bytes we use only 1024

int BytesPerElement = 2; // 2 bytes in 16bit format

recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,

RECORDER_SAMPLERATE, RECORDER_CHANNELS,

RECORDER_AUDIO_ENCODING, BufferElements2Rec * BytesPerElement);

and this is my code that uses liblame for encode mp3 and write it to file:

//Encoder.Builder(int inSamplerate,int outChannel,int outSampleRate,int outBitrate)

Encoder en = new Encoder.Builder(8000, 1,8000,128).quality(7).create();

private int PCM_BUF_SIZE = 8192;

private int MP3_SIZE = 8192;

private void readFile() {

File pcm = new File("/sdcard/voice8K16bitmono.pcm");

File mp3 = new File("/sdcard/BOOOB.mp3");

pcm.setReadable(true);

mp3.setWritable(true);

try {

InputStream is = new FileInputStream(pcm);

BufferedInputStream bis = new BufferedInputStream(is);

bis.skip(44);//skip pcm header

OutputStream os = new FileOutputStream(mp3);

FileOutputStream fos = new FileOutputStream(mp3);

int n_bytes_read ;

int n_bytes_write;

int i;

byte mp3_buffer[] = new byte[MP3_SIZE];

byte pcm_buffer1[] = new byte[PCM_BUF_SIZE * 2];

do {

n_bytes_read = bis.read(pcm_buffer1 , 0 , PCM_BUF_SIZE);

if (n_bytes_read == 0){

n_bytes_write = en.flush(mp3_buffer);

}

else{

n_bytes_write = en.encodeBufferInterleaved(byte2short(pcm_buffer1) ,n_bytes_read , mp3_buffer);

}

bof.write(mp3_buffer, 0, PCM_BUF_SIZE);

} while (n_bytes_read > 0);

bis.close();

fos.close();

is.close();

en.close();

}catch (IOException e) {

e.printStackTrace();

}

}

private short[] byte2short(byte[] pcm_buffer1) {

short[] shorts = new short[pcm_buffer1.length/2];

ByteBuffer.wrap(pcm_buffer1).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shorts);

return shorts;

}

how can i fix this code, is the bufferSizes true? using BufferedInputStream is correct? and...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值