NorthernCaptain/TAndroidLame


Android wrapper around Lame mp3 encoder

Download


Step 1. Add the JitPack repository to your build file

Add it in your root settings.gradle at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url 'https://jitpack.io' }
		}
	}

Add it in your settings.gradle.kts at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url = uri("https://jitpack.io") }
		}
	}

Add to pom.xml

	<repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>

Add it in your build.sbt at the end of resolvers:

 
    resolvers += "jitpack" at "https://jitpack.io"
        
    

Add it in your project.clj at the end of repositories:

 
    :repositories [["jitpack" "https://jitpack.io"]]
        
    

Step 2. Add the dependency

	dependencies {
		implementation 'com.github.NorthernCaptain:TAndroidLame:1.1'
	}
	dependencies {
		implementation("com.github.NorthernCaptain:TAndroidLame:1.1")
	}
	<dependency>
	    <groupId>com.github.NorthernCaptain</groupId>
	    <artifactId>TAndroidLame</artifactId>
	    <version>1.1</version>
	</dependency>

                            
    libraryDependencies += "com.github.NorthernCaptain" % "TAndroidLame" % "1.1"
        
        

                            
    :dependencies [[com.github.NorthernCaptain/TAndroidLame "1.1"]]
        
        

Readme


AndroidLame

AndroidLame is a wrapper Library for Android/Java around Lame MP3 encoder (http://lame.sourceforge.net/)
Built using NDK, cmake and Andorid studio with gradle 2.3.0.

Based on naman14 work.

Gradle dependency

build.gradle (project)

allprojects {
		repositories {
			...
			maven { url "https://jitpack.io" }
		}
	}

app/build.gradle

dependencies {
	        compile 'com.github.NorthernCaptain:TAndroidLame:1.1'
	}

Usage

AndoridLame androidLame = new AndroidLame(); //everything set to defaults
or
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(); //use this
AndroidLame androidLame = new AndroidLame(builder); //or this

Building with Android studio

Import the project and use gradle version 2.3.0 or higher.

Documentation

LameBuilder

LameBuilder is a wrapper around the extra initialisation parameters in Lame.

inSampleRate - input sample rate in Hz. default = 44100hz
numChannels - number of channels in input stream. default=2
bitrate - set the bitrate of out stream
outSampleRate - output sample rate in Hz. default = 0, which means LAME picks best value
based on the amount of compression
quality - quality = 0 to 9. 0=best (very slow). 9=worst. default = 5
scaleInput - scale the input by this amount before encoding. default=1

Mode - sets a preset mode

public enum Mode {
        STEREO, JSTEREO, MONO, DEFAULT
    }

vbrMode There are 3 bitrate modes in Lame - CBR, VBR, ABR
CBR Constant Bit Rate (default) - CBR encodes every frame at the same bitrate.
VBR Variable Bit Rate - The final file size of a VBR encode is less predictable, but the quality is usually better.
ABR Average Bit rate - A compromise between VBR and CBR modes, ABR encoding varies bits around a specified target bitrate. use setAbrBitrate to set the mean bitrate to be used for encoding

setVbrMode default = VBR_OFF = CBR

public enum VbrMode {
        VBR_OFF, VBR_RH, VBR_MTRH, VBR_ABR, VBR_DEFAUT
    }

If using ABR, use setAbrBitrate to set the mean bitrate in kbps, value is ignored if used with other vbr modes

vbrQuality VBR quality level. 0=highest 9=lowest, Range [0,...,10[
lowpassFrequency freq in Hz to apply lowpass. Default = 0 = lame chooses. -1 = disabled
highpassFrequency freq in Hz to apply highpass. Default = 0 = lame chooses. -1 = disabled

setId3... - to set id3 tags

AndroidLame

A wrapper class for actual native implementation and encoding

encode(short[] buffer_l, short[] buffer_r, int samples, byte[] mp3buf)
input pcm data
returns number of bytes output in mp3buf

encodeBufferInterleaved(short[] pcm, int samples, byte[] mp3buf);
as above, but input has L & R channel data interleaved.
num_samples = number of samples in the L (or R) channel, not the total number of samples in pcm[]

lameFlush(byte[] mp3buf);
flushes the intenal PCM buffers, and returns the final mp3 frames, will also write id3v1 tags (if any) into the bitstream returns number of bytes output to mp3buf

Mp3Recorder

Class provides simplified use of AndroidLame and AudioRecord together for recording audio from mic into MP3 file.

recorder = Mp3Recorder(File);

Will use default 44100 Hz sample rate and 128k bitrate

recorder.start();

Starts recording from the mic in background thread.

recorder.stop();

Initiate stopping procedure. This means not immediate stopping but stopping in background. If you want to be notified when actual stop occurs then register listener:

recorder.setOnChangeListener(listener)

Listener methods will be called on UI thread.

Demo

A sample apk is avilable in releases.
(Remember to grant permissions from settings on Marshmallow devices or app will crash)

Sample apk has two demos -

  • Encoding .wav to mp3 and
  • Recording audio using AudioRecorder and encoding in real time to mp3
<img src="https://raw.githubusercontent.com/NorthernCaptain/TAndroidLame/master/app/Screenshot1.png" alt="alt text" width="250" height="500"> <img src="https://raw.githubusercontent.com/NorthernCaptain/TAndroidLame/master/app/Screenshot2.png" alt="alt text" width="250" height="500">

License

(c) 2015 Naman Dwivedi

(c) 2017 Northern Captain

This is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this app. If not, see https://www.gnu.org/licenses/.