MP3Decoder.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include "MP3Decoder.h"
  2. #include <stdlib.h> // for free, malloc
  3. #include "CodecType.h" // for bell
  4. namespace bell {
  5. class AudioContainer;
  6. } // namespace bell
  7. using namespace bell;
  8. MP3Decoder::MP3Decoder() {
  9. mp3 = MP3InitDecoder();
  10. pcmData =
  11. (int16_t*)malloc(MAX_NSAMP * MAX_NGRAN * MAX_NCHAN * sizeof(int16_t));
  12. }
  13. MP3Decoder::~MP3Decoder() {
  14. MP3FreeDecoder(mp3);
  15. free(pcmData);
  16. }
  17. bool MP3Decoder::setup(uint32_t sampleRate, uint8_t channelCount,
  18. uint8_t bitDepth) {
  19. return true;
  20. }
  21. bool MP3Decoder::setup(AudioContainer* container) {
  22. return true;
  23. }
  24. uint8_t* MP3Decoder::decode(uint8_t* inData, uint32_t& inLen,
  25. uint32_t& outLen) {
  26. if (!inData || inLen == 0)
  27. return nullptr;
  28. int status = MP3Decode(mp3, static_cast<unsigned char**>(&inData),
  29. reinterpret_cast<int*>(&inLen),
  30. static_cast<short*>(this->pcmData),
  31. /* useSize */ 0);
  32. MP3GetLastFrameInfo(mp3, &frame);
  33. if (status != ERR_MP3_NONE) {
  34. lastErrno = status;
  35. return nullptr;
  36. }
  37. if (sampleRate != frame.samprate) {
  38. this->sampleRate = frame.samprate;
  39. }
  40. if (channelCount != frame.nChans) {
  41. this->channelCount = frame.nChans;
  42. }
  43. outLen = frame.outputSamps * sizeof(int16_t);
  44. return (uint8_t*)pcmData;
  45. }