1159b3361Sopenharmony_ciThe LAME API 2159b3361Sopenharmony_ci 3159b3361Sopenharmony_ciThis is the simple interface to the encoding part of libmp3lame.so. 4159b3361Sopenharmony_ciThe library also contains routines for adding id3 tags and 5159b3361Sopenharmony_cimp3 decoding. These routines are not fully documented, 6159b3361Sopenharmony_cibut you can figure them out by looking at "include/lame.h" and the 7159b3361Sopenharmony_ciexample frontend encoder/decoder source code in frontend/main.c 8159b3361Sopenharmony_ci 9159b3361Sopenharmony_ciAll of these steps should be done for every MP3 to be encoded. 10159b3361Sopenharmony_ci 11159b3361Sopenharmony_ci 12159b3361Sopenharmony_ci========================================================================= 13159b3361Sopenharmony_ci 14159b3361Sopenharmony_ci1. (optional) Get the version number of the encoder, if you are interested. 15159b3361Sopenharmony_ci void get_lame_version(char *strbuf, size_t buflen, const char *prefix); 16159b3361Sopenharmony_ci 17159b3361Sopenharmony_ci 18159b3361Sopenharmony_ci2. Error messages. By default, LAME will write error messages to 19159b3361Sopenharmony_cistderr using vfprintf(). For GUI applications, this is often a problem 20159b3361Sopenharmony_ciand you need to set your own error message handlers: 21159b3361Sopenharmony_ci 22159b3361Sopenharmony_ci lame_set_errorf(gfp,error_handler_function); 23159b3361Sopenharmony_ci lame_set_debugf(gfp,error_handler_function); 24159b3361Sopenharmony_ci lame_set_msgf(gfp,error_handler_function); 25159b3361Sopenharmony_ci 26159b3361Sopenharmony_ciSee lame.h for details. 27159b3361Sopenharmony_ci 28159b3361Sopenharmony_ci 29159b3361Sopenharmony_ci3. Initialize the encoder. sets default for all encoder parameters. 30159b3361Sopenharmony_ci 31159b3361Sopenharmony_ci #include "lame.h" 32159b3361Sopenharmony_ci lame_global_flags *gfp; 33159b3361Sopenharmony_ci gfp = lame_init(); 34159b3361Sopenharmony_ci 35159b3361Sopenharmony_ciThe default (if you set nothing) is a J-Stereo, 44.1khz 36159b3361Sopenharmony_ci128kbps CBR mp3 file at quality 5. Override various default settings 37159b3361Sopenharmony_cias necessary, for example: 38159b3361Sopenharmony_ci 39159b3361Sopenharmony_ci lame_set_num_channels(gfp,2); 40159b3361Sopenharmony_ci lame_set_in_samplerate(gfp,44100); 41159b3361Sopenharmony_ci lame_set_brate(gfp,128); 42159b3361Sopenharmony_ci lame_set_mode(gfp,1); 43159b3361Sopenharmony_ci lame_set_quality(gfp,2); /* 2=high 5 = medium 7=low */ 44159b3361Sopenharmony_ci 45159b3361Sopenharmony_ci 46159b3361Sopenharmony_ciSee lame.h for the complete list of options. Note that there are 47159b3361Sopenharmony_cisome lame_set_*() calls not documented in lame.h. These functions 48159b3361Sopenharmony_ciare experimental and for testing only. They may be removed in 49159b3361Sopenharmony_cithe future. 50159b3361Sopenharmony_ci 51159b3361Sopenharmony_ci 52159b3361Sopenharmony_ci 53159b3361Sopenharmony_ci4. Set more internal configuration based on data provided above, 54159b3361Sopenharmony_ci as well as checking for problems. Check that ret_code >= 0. 55159b3361Sopenharmony_ci 56159b3361Sopenharmony_ci ret_code = lame_init_params(gfp); 57159b3361Sopenharmony_ci 58159b3361Sopenharmony_ci 59159b3361Sopenharmony_ci 60159b3361Sopenharmony_ci5. Encode some data. input pcm data, output (maybe) mp3 frames. 61159b3361Sopenharmony_ciThis routine handles all buffering, resampling and filtering for you. 62159b3361Sopenharmony_ciThe required mp3buffer_size can be computed from num_samples, 63159b3361Sopenharmony_cisamplerate and encoding rate, but here is a worst case estimate: 64159b3361Sopenharmony_cimp3buffer_size (in bytes) = 1.25*num_samples + 7200. 65159b3361Sopenharmony_cinum_samples = the number of PCM samples in each channel. It is 66159b3361Sopenharmony_cinot the sum of the number of samples in the L and R channels. 67159b3361Sopenharmony_ci 68159b3361Sopenharmony_ciThe return code = number of bytes output in mp3buffer. This can be 0. 69159b3361Sopenharmony_ciIf it is <0, an error occured. 70159b3361Sopenharmony_ci 71159b3361Sopenharmony_ci int lame_encode_buffer(lame_global_flags *gfp, 72159b3361Sopenharmony_ci short int leftpcm[], short int rightpcm[], 73159b3361Sopenharmony_ci int num_samples,char *mp3buffer,int mp3buffer_size); 74159b3361Sopenharmony_ci 75159b3361Sopenharmony_ci 76159b3361Sopenharmony_ciThere are also routines for various types of input 77159b3361Sopenharmony_ci(float, long, interleaved, etc). See lame.h for details. 78159b3361Sopenharmony_ci 79159b3361Sopenharmony_ci 80159b3361Sopenharmony_ci6. lame_encode_flush will flush the buffers and may return a 81159b3361Sopenharmony_cifinal few mp3 frames. mp3buffer should be at least 7200 bytes. 82159b3361Sopenharmony_cireturn code = number of bytes output to mp3buffer. This can be 0. 83159b3361Sopenharmony_ci 84159b3361Sopenharmony_ciint lame_encode_flush(lame_global_flags *,char *mp3buffer, int mp3buffer_size); 85159b3361Sopenharmony_ci 86159b3361Sopenharmony_ci 87159b3361Sopenharmony_ci7. Write the Xing VBR/INFO tag to mp3 file. 88159b3361Sopenharmony_ci 89159b3361Sopenharmony_civoid lame_mp3_tags_fid(lame_global_flags *,FILE* fid); 90159b3361Sopenharmony_ci 91159b3361Sopenharmony_ciThis adds a valid mp3 frame which contains information about the 92159b3361Sopenharmony_cibitstream some players may find usefull. It is used for CBR,ABR and 93159b3361Sopenharmony_ciVBR. The routine will attempt to rewind the output stream to the 94159b3361Sopenharmony_cibeginning. If this is not possible, (for example, you are encoding to 95159b3361Sopenharmony_cistdout) you should specifically disable the tag by calling 96159b3361Sopenharmony_cilame_set_bWriteVbrTag(gfp,0) in step 3 above, and call 97159b3361Sopenharmony_cilame_mp3_tags_fid() with fid=NULL. If the rewind fails and 98159b3361Sopenharmony_cithe tag was not disabled, the first mp3 frame in the bitstream 99159b3361Sopenharmony_ciwill be all 0's. 100159b3361Sopenharmony_ci 101159b3361Sopenharmony_ci 102159b3361Sopenharmony_ci 103159b3361Sopenharmony_ci8. free the internal data structures. 104159b3361Sopenharmony_ci 105159b3361Sopenharmony_civoid lame_close(lame_global_flags *); 106159b3361Sopenharmony_ci 107159b3361Sopenharmony_ci 108