AudioCodecs.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright (c) Kuba Szczodrzyński 2022-1-12.
  2. #include "AudioCodecs.h"
  3. #include <map>
  4. #ifdef BELL_CODEC_AAC
  5. #include "AACDecoder.h"
  6. static std::shared_ptr<AACDecoder> codecAac;
  7. #endif
  8. #ifdef BELL_CODEC_MP3
  9. #include "MP3Decoder.h"
  10. static std::shared_ptr<MP3Decoder> codecMp3;
  11. #endif
  12. #ifdef BELL_CODEC_VORBIS
  13. #include "VorbisDecoder.h"
  14. static std::shared_ptr<VorbisDecoder> codecVorbis;
  15. #endif
  16. #ifdef BELL_CODEC_OPUS
  17. #include "OPUSDecoder.h"
  18. static std::shared_ptr<OPUSDecoder> codecOpus;
  19. #endif
  20. std::map<AudioCodec, std::shared_ptr<BaseCodec>> customCodecs;
  21. std::shared_ptr<BaseCodec> AudioCodecs::getCodec(AudioCodec type) {
  22. if (customCodecs.find(type) != customCodecs.end())
  23. return customCodecs[type];
  24. switch (type) {
  25. #ifdef BELL_CODEC_AAC
  26. case AudioCodec::AAC:
  27. if (codecAac)
  28. return codecAac;
  29. codecAac = std::make_shared<AACDecoder>();
  30. return codecAac;
  31. #endif
  32. #ifdef BELL_CODEC_MP3
  33. case AudioCodec::MP3:
  34. if (codecMp3)
  35. return codecMp3;
  36. codecMp3 = std::make_shared<MP3Decoder>();
  37. return codecMp3;
  38. #endif
  39. #ifdef BELL_CODEC_VORBIS
  40. case AudioCodec::VORBIS:
  41. if (codecVorbis)
  42. return codecVorbis;
  43. codecVorbis = std::make_shared<VorbisDecoder>();
  44. return codecVorbis;
  45. #endif
  46. #ifdef BELL_CODEC_OPUS
  47. case AudioCodec::OPUS:
  48. if (codecOpus)
  49. return codecOpus;
  50. codecOpus = std::make_shared<OPUSDecoder>();
  51. return codecOpus;
  52. #endif
  53. default:
  54. return nullptr;
  55. }
  56. }
  57. std::shared_ptr<BaseCodec> AudioCodecs::getCodec(BaseContainer *container) {
  58. return getCodec(container->codec);
  59. }
  60. void AudioCodecs::addCodec(AudioCodec type, const std::shared_ptr<BaseCodec> &codec) {
  61. customCodecs[type] = codec;
  62. }