ModPlugDecoder.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * Copyright (c) 2006-2022 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 "ModPlugDecoder.h"
  21. #ifndef LOVE_NO_MODPLUG
  22. #include "common/Exception.h"
  23. #include "common/Data.h"
  24. namespace love
  25. {
  26. namespace sound
  27. {
  28. namespace lullaby
  29. {
  30. ModPlugDecoder::ModPlugDecoder(Stream *stream, int bufferSize)
  31. : Decoder(stream, bufferSize)
  32. , plug(0)
  33. , duration(-2.0)
  34. {
  35. // Set some ModPlug settings.
  36. settings.mFlags = MODPLUG_ENABLE_OVERSAMPLING | MODPLUG_ENABLE_NOISE_REDUCTION;
  37. settings.mChannels = 2;
  38. settings.mBits = 16;
  39. settings.mFrequency = sampleRate;
  40. settings.mResamplingMode = MODPLUG_RESAMPLE_LINEAR;
  41. // fill with modplug defaults (modplug _memsets_, so we could get
  42. // garbage settings when the struct is only partially initialized)
  43. // This does not exist yet on Windows.
  44. settings.mStereoSeparation = 128;
  45. settings.mMaxMixChannels = 32;
  46. settings.mReverbDepth = 0;
  47. settings.mReverbDelay = 0;
  48. settings.mBassAmount = 0;
  49. settings.mBassRange = 0;
  50. settings.mSurroundDepth = 0;
  51. settings.mSurroundDelay = 0;
  52. settings.mLoopCount = -1;
  53. ModPlug_SetSettings(&settings);
  54. // ModPlug has no streaming API. Miserable.
  55. // We don't want to load the entire stream immediately if it's big, because
  56. // it might not be compatible with ModPlug. So we just try to load 4MB and
  57. // see if that works, and then load the whole thing if it does.
  58. if (stream->getSize() > 1024 * 1024 * 4)
  59. {
  60. data.set(stream->read(1024 * 1024 * 4), Acquire::NORETAIN);
  61. plug = ModPlug_Load(data->getData(), (int)data->getSize());
  62. if (plug == nullptr)
  63. throw love::Exception("Could not load file with ModPlug.");
  64. stream->seek(0);
  65. ModPlug_Unload(plug);
  66. }
  67. data.set(stream->read(stream->getSize()), Acquire::NORETAIN);
  68. // Load the module.
  69. plug = ModPlug_Load(data->getData(), (int)data->getSize());
  70. if (plug == nullptr)
  71. throw love::Exception("Could not load file with ModPlug.");
  72. // set master volume for delicate ears
  73. ModPlug_SetMasterVolume(plug, 128);
  74. }
  75. ModPlugDecoder::~ModPlugDecoder()
  76. {
  77. if (plug != nullptr)
  78. ModPlug_Unload(plug);
  79. }
  80. int ModPlugDecoder::probe(Stream* stream)
  81. {
  82. // Ideally we want to probe every single format that ModPlug supports.
  83. Data *data = stream->read(1024 * 1024 * 4);
  84. ModPlugFile *plug = ModPlug_Load(data->getData(), (int)data->getSize());
  85. if (plug)
  86. ModPlug_Unload(plug);
  87. data->release();
  88. return plug ? 80 : 0;
  89. }
  90. love::sound::Decoder *ModPlugDecoder::clone()
  91. {
  92. StrongRef<Stream> s(stream->clone(), Acquire::NORETAIN);
  93. return new ModPlugDecoder(s, bufferSize);
  94. }
  95. int ModPlugDecoder::decode()
  96. {
  97. int r = ModPlug_Read(plug, buffer, bufferSize);
  98. if (r == 0)
  99. eof = true;
  100. return r;
  101. }
  102. bool ModPlugDecoder::seek(double s)
  103. {
  104. ModPlug_Seek(plug, (int)(s*1000.0));
  105. return true;
  106. }
  107. bool ModPlugDecoder::rewind()
  108. {
  109. // Let's reload.
  110. ModPlug_Unload(plug);
  111. plug = ModPlug_Load(data->getData(), (int) data->getSize());
  112. ModPlug_SetMasterVolume(plug, 128);
  113. eof = false;
  114. return (plug != 0);
  115. }
  116. bool ModPlugDecoder::isSeekable()
  117. {
  118. return true;
  119. }
  120. int ModPlugDecoder::getChannelCount() const
  121. {
  122. return 2;
  123. }
  124. int ModPlugDecoder::getBitDepth() const
  125. {
  126. return 16;
  127. }
  128. double ModPlugDecoder::getDuration()
  129. {
  130. // Only calculate the duration if we haven't done so already.
  131. if (duration == -2.0)
  132. {
  133. int lengthms = ModPlug_GetLength(plug);
  134. if (lengthms < 0)
  135. duration = -1.0;
  136. else
  137. duration = (double) lengthms / 1000.0;
  138. }
  139. return duration;
  140. }
  141. } // lullaby
  142. } // sound
  143. } // love
  144. #endif // LOVE_NO_MODPLUG