API 3.6 KB

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