MP3Decoder.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include "MP3Decoder.h"
  2. #include <stdlib.h> // for free, malloc
  3. #include <cstdio>
  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. inLen -= 2;
  36. outLen = 0;
  37. return nullptr;
  38. }
  39. if (sampleRate != frame.samprate) {
  40. this->sampleRate = frame.samprate;
  41. }
  42. if (channelCount != frame.nChans) {
  43. this->channelCount = frame.nChans;
  44. }
  45. outLen = frame.outputSamps * sizeof(int16_t);
  46. return (uint8_t*)pcmData;
  47. }