ModPlugDecoder.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * Copyright (c) 2006-2013 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 "ModPlugDecoder.h"
  22. #include "common/Exception.h"
  23. namespace love
  24. {
  25. namespace sound
  26. {
  27. namespace lullaby
  28. {
  29. ModPlugDecoder::ModPlugDecoder(Data *data, const std::string &ext, int bufferSize)
  30. : Decoder(data, ext, bufferSize)
  31. , plug(0)
  32. {
  33. // Set some ModPlug settings.
  34. settings.mFlags = MODPLUG_ENABLE_OVERSAMPLING | MODPLUG_ENABLE_NOISE_REDUCTION;
  35. settings.mChannels = 2;
  36. settings.mBits = 16;
  37. settings.mFrequency = sampleRate;
  38. settings.mResamplingMode = MODPLUG_RESAMPLE_LINEAR;
  39. // fill with modplug defaults (modplug _memsets_, so we could get
  40. // garbage settings when the struct is only partially initialized)
  41. // This does not exist yet on Windows.
  42. // Some settings not supported by some older versions
  43. #ifndef LOVE_OLD_MODPLUG
  44. settings.mStereoSeparation = 128;
  45. settings.mMaxMixChannels = 32;
  46. #endif
  47. settings.mReverbDepth = 0;
  48. settings.mReverbDelay = 0;
  49. settings.mBassAmount = 0;
  50. settings.mBassRange = 0;
  51. settings.mSurroundDepth = 0;
  52. settings.mSurroundDelay = 0;
  53. settings.mLoopCount = -1;
  54. ModPlug_SetSettings(&settings);
  55. // Load the module.
  56. plug = ModPlug_Load(data->getData(), data->getSize());
  57. if (plug == 0)
  58. throw love::Exception("Could not load file with ModPlug.");
  59. // set master volume for delicate ears
  60. ModPlug_SetMasterVolume(plug, 128);
  61. }
  62. ModPlugDecoder::~ModPlugDecoder()
  63. {
  64. if (plug != 0)
  65. ModPlug_Unload(plug);
  66. }
  67. bool ModPlugDecoder::accepts(const std::string &ext)
  68. {
  69. static const std::string supported[] =
  70. {
  71. "699", "abc", "amf", "ams", "dbm", "dmf",
  72. "dsm", "far", "it", "j2b", "mdl", "med",
  73. "mid", "mod", "mt2", "mtm", "okt", "pat",
  74. "psm", "s3m", "stm", "ult", "umx", "wav",
  75. "xm", ""
  76. };
  77. for (int i = 0; !(supported[i].empty()); i++)
  78. {
  79. if (supported[i].compare(ext) == 0)
  80. return true;
  81. }
  82. return false;
  83. }
  84. love::sound::Decoder *ModPlugDecoder::clone()
  85. {
  86. return new ModPlugDecoder(data, ext, bufferSize);
  87. }
  88. int ModPlugDecoder::decode()
  89. {
  90. int r = ModPlug_Read(plug, buffer, bufferSize);
  91. if (r == 0)
  92. eof = true;
  93. return r;
  94. }
  95. bool ModPlugDecoder::seek(float s)
  96. {
  97. ModPlug_Seek(plug, (int)(s*1000.0f));
  98. return true;
  99. }
  100. bool ModPlugDecoder::rewind()
  101. {
  102. // Let's reload.
  103. ModPlug_Unload(plug);
  104. plug = ModPlug_Load(data->getData(), data->getSize());
  105. ModPlug_SetMasterVolume(plug, 128);
  106. eof = false;
  107. return (plug != 0);
  108. }
  109. bool ModPlugDecoder::isSeekable()
  110. {
  111. return true;
  112. }
  113. int ModPlugDecoder::getChannels() const
  114. {
  115. return 2;
  116. }
  117. int ModPlugDecoder::getBitDepth() const
  118. {
  119. return 16;
  120. }
  121. } // lullaby
  122. } // sound
  123. } // love