MP3Decoder.cpp 1.2 KB

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