AndroidLame

repository·master·Indexed 42 days ago

https://github.com/naman14/tandroidlame

A JNI wrapper for the LAME MP3 encoder that allows Android/Java applications to encode PCM audio data into MP3 format using the NDK. It includes a LameBuilder for configuring encoding parameters such as sample rate, bitrate, quality, and VBR modes.

Tokens
1.3K
Snippets
3
Records
4
Agent score
5%

What's inside AndroidLame

  1. Configure encoding with LameBuilder

    master

    The LameBuilder allows you to wrap extra initialization parameters for the LAME encoder.

    Parameter Details

    • inSampleRate: Input sample rate in Hz. Default: 44100hz.
    • numChannels: Number of channels in input stream. Default: 2.
    • bitrate: Set the bitrate of the output stream.
    • outSampleRate: Output sample rate in Hz. Default: 0 (LAME picks best value based on compression).
    • quality: Quality level from 0 (best/slow) to 9 (worst). Default: 5.
    • scaleInput: Scale the input by this amount before encoding. Default: 1.
    • lowpassFrequency: Frequency in Hz to apply lowpass. Default: 0 (LAME chooses). -1 to disable.
    • highpassFrequency: Frequency in Hz to apply highpass. Default: 0 (LAME chooses). -1 to disable.
    • setId3...: Methods to set ID3 tags (Title, Album, Artist, Year, Comment).

    Mode and VbrMode

    Mode (Enum: STEREO, JSTEREO, MONO, DEFAULT)

    VbrMode (Enum: VBR_OFF, VBR_RH, VBR_MTRH, VBR_ABR, VBR_DEFAUT)

    • VBR_OFF: Constant Bit Rate (CBR) - default. Every frame is encoded at the same bitrate.
    • VBR_RH / VBR_MTRH: Variable Bit Rate (VBR) - unpredictable file size, usually better quality.
    • VBR_ABR: Average Bit Rate (ABR) - varies bits around a target. Use setAbrMeanBitrate(kbps) to set the target.
  2. Install AndroidLame via Gradle

    master

    To use AndroidLame in your Android project, add the JitPack repository to your project-level build.gradle and then add the dependency to your app-level build.gradle.

    #### build.gradle (project)
    allprojects {
    		repositories {
    			...
    			maven { url "https://jitpack.io" }
    		}
    }
    
    #### app/build.gradle
    apply plugin: 'com.github.dcendents.android-maven'
    
    dependencies {
    		compile 'com.github.naman14:TAndroidLame:1.1'
    }
  3. Initialize AndroidLame

    master

    You can initialize AndroidLame using default settings or by using a LameBuilder to configure specific encoding parameters.

    // Using default settings
    AndoridLame androidLame = new AndroidLame();
    
    // Using a custom LameBuilder
    LameBuilder builder = new LameBuilder()
                    .setInSampleRate(inSamplerate)
                    .setOutChannels(numChannels)
                    .setOutBitrate(bitrate)
                    .setOutSampleRate(outSamplerate)
                    .setMode(mode)
                    .setQuality(quality)
                    .setVbrMode(vbrMode)
                    .setVbrQuality(vbrQuality)
                    .setScaleInput(scaleInput)
                    .setId3tagTitle(title)
                    .setId3tagAlbum(album)
                    .setId3tagArtist(artist)
                    .setId3tagYear(year)
                    .setId3tagComment(comment)
                    .setLowpassFreqency(freq)
                    .setHighpassFreqency(freq)
                    .setAbrMeanBitrate(meanBitRate);
                  
    AndroidLame androidLame = builder.build();
    // OR
    AndroidLame androidLame = new AndroidLame(builder);
  4. Encode PCM data with AndroidLame

    master

    The AndroidLame class provides methods to perform the actual native encoding of PCM data into MP3 format.

    // Encode using separate Left and Right buffers
    int bytesWritten = androidLame.encode(short[] buffer_l, short[] buffer_r, int samples, byte[] mp3buf);
    
    // Encode using interleaved L & R channel data
    // num_samples = number of samples in the L (or R) channel, not the total number of samples in pcm[]
    int bytesWrittenInterleaved = androidLame.encodeBufferInterleaved(short[] pcm, int samples, byte[] mp3buf);
    
    // Flush internal buffers to get final frames and write ID3v1 tags
    int finalBytes = androidLame.lameFlush(byte[] mp3buf);