在android中使用liblame将pcm文件转换为mp3 [英] converting pcm file to mp3 using liblame in android

查看:147
本文介绍了在android中使用liblame将pcm文件转换为mp3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 SimpleLameLibForAndroid 将使用Android中的AudioRecord类创建的pcm文件转换为mp3。我读取了pcm文件并将其编码为mp3,然后将其写入文件中。结果mp3文件,但不正确,它有很多噪音,真的很难理解它是记录pcm文件。
这些是录制的音频规格(pcm文件):

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);

这是我使用liblame编码mp3并将其写入文件的代码:

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;
}

如何修复此代码,bufferSizes是真的吗?使用BufferedInputStream是否正确?和...

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

推荐答案

我昨天为使用lame的应用程序实现了PCM到MP3编码器。我建议不要使用SimpleLameLibForAndroid,而是自己为项目添加lame。如果您使用的是Android Studio,如果您之前没有使用NDK,这里有一个很好的指南可以让您开始使用。

I implemented a PCM to MP3 encoder just yesterday for my application using lame. I suggest not using SimpleLameLibForAndroid and instead adding lame to your project yourself. If you are using Android Studio, here is a good guide to get you started on that if you haven't done NDK before.

http://www.shaneenishry.com/blog/2014/08/17/ndk-with-android-studio /

至于实现跛脚本身,下面是我为了启动和运行我的应用程序而遵循的非常好的指南。使用页面顶部.zip中的 wrapper.c 。这暴露了有用的方法,这样你就可以避免所有令人讨厌的 Stream 缓冲区的东西。

As for implementing lame itself, below is a really good guide that I followed to get my application up and running. Use the wrapper.c from the .zip at the top of the page. This exposes useful methods so that you can avoid all the nasty Stream and Buffer stuff.

http://developer.samsung.com/technical-doc/ view.do?v=T000000090

完成所有操作后,对跛脚编码器的实际调用非常简单,如下所示。

When all is said and done, the actual calls to the lame encoder are super simple as follows.

用于初始化(使用您喜欢的任何设置):

For initializing (use whatever settings you like):

public static final int NUM_CHANNELS = 1;
public static final int SAMPLE_RATE = 16000;
public static final int BITRATE = 64;
public static final int MODE = 1;
public static final int QUALITY = 7;
...

initEncoder(NUM_CHANNELS, SAMPLE_RATE, BITRATE, MODE, QUALITY);

编码(非常简单):

int result = encodeFile(pcm.getPath(), mp3.getPath());
if (result == 0) {
    //success
}

当然使用 destroyEncoder()完成时会破坏编码器。

And of course destroy the encoder when done with destroyEncoder().

这篇关于在android中使用liblame将pcm文件转换为mp3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆