Blame API

Packit 47f805
The LAME API
Packit 47f805
Packit 47f805
This is the simple interface to the encoding part of libmp3lame.so.
Packit 47f805
The library also contains routines for adding id3 tags and 
Packit 47f805
mp3 decoding.  These routines are not fully documented,
Packit 47f805
but you can figure them out by looking at "include/lame.h" and the
Packit 47f805
example frontend encoder/decoder source code in frontend/main.c
Packit 47f805
Packit 47f805
All of these steps should be done for every MP3 to be encoded.
Packit 47f805
Packit 47f805
Packit 47f805
=========================================================================
Packit 47f805
Packit 47f805
1. (optional) Get the version number of the encoder, if you are interested.  
Packit 47f805
   void get_lame_version(char *strbuf, size_t buflen, const char *prefix);
Packit 47f805
Packit 47f805
Packit 47f805
2. Error messages.  By default, LAME will write error messages to
Packit 47f805
stderr using vfprintf().  For GUI applications, this is often a problem
Packit 47f805
and you need to set your own error message handlers:
Packit 47f805
Packit 47f805
   lame_set_errorf(gfp,error_handler_function);
Packit 47f805
   lame_set_debugf(gfp,error_handler_function);
Packit 47f805
   lame_set_msgf(gfp,error_handler_function);
Packit 47f805
  
Packit 47f805
See lame.h for details.
Packit 47f805
Packit 47f805
Packit 47f805
3. Initialize the encoder.  sets default for all encoder parameters.
Packit 47f805
Packit 47f805
   #include "lame.h"
Packit 47f805
   lame_global_flags *gfp;
Packit 47f805
   gfp = lame_init();
Packit 47f805
Packit 47f805
The default (if you set nothing) is a  J-Stereo, 44.1khz
Packit 47f805
128kbps CBR mp3 file at quality 5.  Override various default settings 
Packit 47f805
as necessary, for example:
Packit 47f805
Packit 47f805
   lame_set_num_channels(gfp,2);
Packit 47f805
   lame_set_in_samplerate(gfp,44100);
Packit 47f805
   lame_set_brate(gfp,128);
Packit 47f805
   lame_set_mode(gfp,1);
Packit 47f805
   lame_set_quality(gfp,2);   /* 2=high  5 = medium  7=low */ 
Packit 47f805
Packit 47f805
Packit 47f805
See lame.h for the complete list of options.  Note that there are
Packit 47f805
some lame_set_*() calls not documented in lame.h.  These functions
Packit 47f805
are experimental and for testing only.  They may be removed in
Packit 47f805
the future.
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
4. Set more internal configuration based on data provided above,
Packit 47f805
   as well as checking for problems.  Check that ret_code >= 0.
Packit 47f805
Packit 47f805
   ret_code = lame_init_params(gfp);
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
5. Encode some data.  input pcm data, output (maybe) mp3 frames.
Packit 47f805
This routine handles all buffering, resampling and filtering for you.
Packit 47f805
The required mp3buffer_size can be computed from num_samples, 
Packit 47f805
samplerate and encoding rate, but here is a worst case estimate:
Packit 47f805
mp3buffer_size (in bytes) = 1.25*num_samples + 7200.
Packit 47f805
num_samples = the number of PCM samples in each channel.  It is
Packit 47f805
not the sum of the number of samples in the L and R channels.
Packit 47f805
Packit 47f805
The return code = number of bytes output in mp3buffer.  This can be 0.
Packit 47f805
If it is <0, an error occured.  
Packit 47f805
Packit 47f805
   int lame_encode_buffer(lame_global_flags *gfp,
Packit 47f805
         short int leftpcm[], short int rightpcm[],
Packit 47f805
         int num_samples,char *mp3buffer,int  mp3buffer_size);
Packit 47f805
Packit 47f805
Packit 47f805
There are also routines for various types of input  
Packit 47f805
(float, long, interleaved, etc).  See lame.h for details.
Packit 47f805
Packit 47f805
Packit 47f805
6. lame_encode_flush will flush the buffers and may return a 
Packit 47f805
final few mp3 frames.  mp3buffer should be at least 7200 bytes.
Packit 47f805
return code = number of bytes output to mp3buffer.  This can be 0.
Packit 47f805
Packit 47f805
int lame_encode_flush(lame_global_flags *,char *mp3buffer, int mp3buffer_size);
Packit 47f805
Packit 47f805
Packit 47f805
7.  Write the Xing VBR/INFO tag to mp3 file.  
Packit 47f805
Packit 47f805
void lame_mp3_tags_fid(lame_global_flags *,FILE* fid);
Packit 47f805
Packit 47f805
This adds a valid mp3 frame which contains information about the
Packit 47f805
bitstream some players may find usefull.  It is used for CBR,ABR and
Packit 47f805
VBR.  The routine will attempt to rewind the output stream to the
Packit 47f805
beginning.  If this is not possible, (for example, you are encoding to
Packit 47f805
stdout) you should specifically disable the tag by calling
Packit 47f805
lame_set_bWriteVbrTag(gfp,0) in step 3 above, and call
Packit 47f805
lame_mp3_tags_fid() with fid=NULL.  If the rewind fails and
Packit 47f805
the tag was not disabled, the first mp3 frame in the bitstream
Packit 47f805
will be all 0's.
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
8. free the internal data structures.
Packit 47f805
Packit 47f805
void lame_close(lame_global_flags *); 
Packit 47f805
Packit 47f805