Sound.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * Copyright (c) 2006-2019 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "common/config.h"
  21. #include <algorithm>
  22. #include <sstream>
  23. #include "Sound.h"
  24. #include "ModPlugDecoder.h"
  25. #include "VorbisDecoder.h"
  26. #include "GmeDecoder.h"
  27. #include "WaveDecoder.h"
  28. //#include "FLACDecoder.h"
  29. #ifndef LOVE_NOMPG123
  30. # include "Mpg123Decoder.h"
  31. #endif // LOVE_NOMPG123
  32. #ifdef LOVE_SUPPORT_COREAUDIO
  33. # include "CoreAudioDecoder.h"
  34. #endif
  35. struct DecoderImpl
  36. {
  37. love::sound::Decoder *(*create)(love::filesystem::FileData *data, int bufferSize);
  38. bool (*accepts)(const std::string& ext);
  39. };
  40. template<typename DecoderType>
  41. DecoderImpl DecoderImplFor()
  42. {
  43. DecoderImpl decoderImpl;
  44. decoderImpl.create = [](love::filesystem::FileData *data, int bufferSize) -> love::sound::Decoder*
  45. {
  46. return new DecoderType(data, bufferSize);
  47. };
  48. decoderImpl.accepts = [](const std::string& ext) -> bool
  49. {
  50. return DecoderType::accepts(ext);
  51. };
  52. return decoderImpl;
  53. }
  54. namespace love
  55. {
  56. namespace sound
  57. {
  58. namespace lullaby
  59. {
  60. Sound::Sound()
  61. {
  62. }
  63. Sound::~Sound()
  64. {
  65. #ifndef LOVE_NOMPG123
  66. Mpg123Decoder::quit();
  67. #endif // LOVE_NOMPG123
  68. }
  69. const char *Sound::getName() const
  70. {
  71. return "love.sound.lullaby";
  72. }
  73. sound::Decoder *Sound::newDecoder(love::filesystem::FileData *data, int bufferSize)
  74. {
  75. std::string ext = data->getExtension();
  76. std::transform(ext.begin(), ext.end(), ext.begin(), tolower);
  77. std::vector<DecoderImpl> possibleDecoders = {
  78. #ifndef LOVE_NO_MODPLUG
  79. DecoderImplFor<ModPlugDecoder>(),
  80. #endif // LOVE_NO_MODPLUG
  81. #ifndef LOVE_NOMPG123
  82. DecoderImplFor<Mpg123Decoder>(),
  83. #endif // LOVE_NOMPG123
  84. DecoderImplFor<VorbisDecoder>(),
  85. #ifdef LOVE_SUPPORT_GME
  86. DecoderImplFor<GmeDecoder>(),
  87. #endif // LOVE_SUPPORT_GME
  88. #ifdef LOVE_SUPPORT_COREAUDIO
  89. DecoderImplFor<CoreAudioDecoder>(),
  90. #endif
  91. DecoderImplFor<WaveDecoder>(),
  92. // DecoderImplFor<FLACDecoder>(),
  93. // DecoderImplFor<OtherDecoder>(),
  94. };
  95. // First find a matching decoder based on extension
  96. for (DecoderImpl &possibleDecoder : possibleDecoders)
  97. {
  98. if (possibleDecoder.accepts(ext))
  99. return possibleDecoder.create(data, bufferSize);
  100. }
  101. // If that fails, start probing instead
  102. std::stringstream decodingErrors;
  103. decodingErrors << "Failed to determine file type:\n";
  104. for (DecoderImpl &possibleDecoder : possibleDecoders)
  105. {
  106. try
  107. {
  108. sound::Decoder *decoder = possibleDecoder.create(data, bufferSize);
  109. return decoder;
  110. }
  111. catch (love::Exception &e)
  112. {
  113. decodingErrors << e.what() << '\n';
  114. }
  115. }
  116. // Probing failed too, bail with the accumulated errors
  117. throw love::Exception(decodingErrors.str().c_str());
  118. // Unreachable, but here to prevent (possible) warnings
  119. return nullptr;
  120. }
  121. } // lullaby
  122. } // sound
  123. } // love