c_encoder_mp3dll.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "c_encoder_mp3dll.h"
  2. #include "../../utils.h"
  3. T_ENCODER_MP3_INFO formatlist[] = {
  4. {8, 22050, 1, 44100, 2, 8}, {16, 22050, 1, 44100, 2, 8}, {24, 22050, 1, 44100, 2, 8},
  5. {32, 22050, 1, 44100, 2, 8}, {40, 22050, 1, 44100, 2, 8}, {48, 22050, 1, 44100, 2, 8},
  6. {48, 44100, 1, 44100, 2, 8}, {56, 22050, 1, 44100, 2, 8}, {56, 44100, 1, 44100, 2, 8},
  7. {64, 44100, 1, 44100, 2, 8}, {80, 44100, 1, 44100, 2, 8}, {96, 44100, 1, 44100, 2, 8},
  8. {112, 44100, 1, 44100, 2, 8}, {128, 44100, 1, 44100, 2, 8}, {40, 22050, 2, 44100, 2, 8},
  9. {48, 22050, 2, 44100, 2, 8}, {56, 22050, 2, 44100, 2, 8}, {64, 22050, 2, 44100, 2, 8},
  10. {80, 22050, 2, 44100, 2, 8}, {56, 44100, 2, 44100, 2, 8}, {64, 44100, 2, 44100, 2, 8},
  11. {80, 44100, 2, 44100, 2, 8}, {96, 44100, 2, 44100, 2, 8}, {112, 44100, 2, 44100, 2, 8},
  12. {128, 44100, 2, 44100, 2, 8}, {160, 44100, 2, 44100, 2, 8}, {192, 44100, 2, 44100, 2, 8},
  13. {224, 44100, 2, 44100, 2, 8}, {256, 44100, 2, 44100, 2, 8}, {320, 44100, 2, 44100, 2, 8}
  14. };
  15. static unsigned int formatlist_numEntries = sizeof(formatlist) / sizeof(T_ENCODER_MP3_INFO);
  16. C_ENCODER_MP3::C_ENCODER_MP3(void *init, void *params, void *encode, void *finish) : C_ENCODER(sizeof(T_ENCODER_MP3_INFO)) { //sizeof(T_ENCODER_LAMEMP3_INFO)
  17. SetName("MP3 Encoder");
  18. T_ENCODER_MP3_INFO &EncInfo = *((T_ENCODER_MP3_INFO *)ExtendedInfoPtr);
  19. Handle = NULL;
  20. has_encoded = 0;
  21. EncInfo = formatlist[MP3_DEFAULT_ATTRIBNUM];
  22. hMutex = CreateMutex(NULL,TRUE,NULL);
  23. ReleaseMutex(hMutex);
  24. lame_init = (lame_t (__cdecl *)(void))init;
  25. lame_init_params = (int (__cdecl *)(lame_global_flags *))params;
  26. lame_encode_buffer_interleaved = (int (__cdecl *)(lame_global_flags *, short int pcm[], int num_samples, char *mp3buffer, int mp3buffer_size))encode;
  27. lame_encode_flush = (int (__cdecl *)(lame_global_flags *, char *mp3buffer, int size))finish;
  28. }
  29. C_ENCODER_MP3::~C_ENCODER_MP3() {
  30. WaitForSingleObject(hMutex,INFINITE);
  31. CloseHandle(hMutex);
  32. hMutex = NULL;
  33. C_ENCODER::~C_ENCODER();
  34. }
  35. void C_ENCODER_MP3::Close() {
  36. C_ENCODER::Close();
  37. if(lame_init != NULL) {
  38. if(has_encoded && lame_encode_flush) {
  39. char buf[1024] = {0};
  40. lame_encode_flush(Handle,(char *)buf,sizeof(buf));
  41. }
  42. //delete Handle; caused crash !! needs looking at
  43. Handle = NULL;
  44. has_encoded = 0;
  45. }
  46. }
  47. void C_ENCODER_MP3::Reset() {
  48. T_ENCODER_MP3_INFO &EncInfo = *(T_ENCODER_MP3_INFO *)ExtendedInfoPtr;
  49. if(WaitForSingleObject(hMutex,INFINITE) != WAIT_OBJECT_0) return;
  50. Close();
  51. if(lame_init != NULL && EncInfo.input_sampleRate != 0 && EncInfo.input_numChannels != 0) {
  52. if(EncInfo.output_sampleRate != 0 && EncInfo.output_bitRate != 0 && Handle == NULL) {
  53. has_encoded = 0;
  54. Handle = lame_init();
  55. Handle->samplerate_in = EncInfo.input_sampleRate;
  56. Handle->num_channels = 2; // always process as 2 channels as it resolves issues with soundcard input in mono mode (which is padded to stereo)
  57. Handle->samplerate_out = EncInfo.output_sampleRate;
  58. Handle->mode = EncInfo.output_numChannels == 2 ? (MPEG_mode)0 : (MPEG_mode)3;
  59. Handle->brate = EncInfo.output_bitRate;
  60. Handle->VBR = vbr_off;
  61. Handle->write_lame_tag = 0;
  62. Handle->write_id3tag_automatic = 0;
  63. Handle->quality = EncInfo.QualityMode;
  64. if(Handle->quality < 0 || Handle->quality > 9) Handle->quality = 8;
  65. lame_init_params(Handle);
  66. } else {
  67. Handle = NULL;
  68. }
  69. for(unsigned int i = 0; i < formatlist_numEntries; i++) {
  70. char textbuf[256];
  71. formatlist[i].QualityMode = EncInfo.QualityMode;
  72. snprintf(textbuf,sizeof(textbuf),"%dkbps, %dHz, %s",formatlist[i].output_bitRate,formatlist[i].output_sampleRate,(formatlist[i].output_numChannels == 1 ? "Mono" : "Stereo"));
  73. T_ENCODER_MP3_INFO *attribs = new T_ENCODER_MP3_INFO;
  74. *attribs = formatlist[i];
  75. AddAttrib((char *)&textbuf,attribs);
  76. }
  77. }
  78. ReleaseMutex(hMutex);
  79. }
  80. int C_ENCODER_MP3::Encode(const void *inputbuf, const unsigned int inputbufsize, void *outputbuf, const unsigned int outputbufsize, int *inputamtused) {
  81. if((inputbuf != NULL) && (outputbuf != NULL) && (inputbufsize != 0) && (outputbufsize != 0) && (inputamtused != NULL) && (Handle != NULL)) {
  82. if(WaitForSingleObject(hMutex,INFINITE) != WAIT_OBJECT_0) return 0;
  83. int outputamtused = 0;
  84. if(lame_encode_buffer_interleaved) {
  85. outputamtused = lame_encode_buffer_interleaved(Handle, (short *)inputbuf, inputbufsize / (2 * sizeof(short)), (char *)outputbuf, outputbufsize);
  86. if(outputamtused < 0) {
  87. ReleaseMutex(hMutex);
  88. return 0;
  89. }
  90. has_encoded = 1;
  91. }
  92. *inputamtused = inputbufsize;
  93. ReleaseMutex(hMutex);
  94. return outputamtused;
  95. }
  96. return 0;
  97. }