ModPlugDecoder.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * Copyright (c) 2006-2009 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. #include <set>
  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, int sampleRate)
  30. : Decoder(data, ext, bufferSize, sampleRate), plug(0)
  31. {
  32. // Set some ModPlug settings.
  33. settings.mFlags = MODPLUG_ENABLE_OVERSAMPLING | MODPLUG_ENABLE_NOISE_REDUCTION;
  34. settings.mChannels = 2;
  35. settings.mBits = 16;
  36. settings.mFrequency = sampleRate;
  37. settings.mResamplingMode = MODPLUG_RESAMPLE_LINEAR;
  38. settings.mLoopCount = 0;
  39. ModPlug_SetSettings(&settings);
  40. // Load the module.
  41. plug = ModPlug_Load(data->getData(), data->getSize());
  42. ModPlug_SetMasterVolume(plug, 512);
  43. if(plug == 0)
  44. throw love::Exception("Could not load file with ModPlug.");
  45. }
  46. ModPlugDecoder::~ModPlugDecoder()
  47. {
  48. if(plug != 0)
  49. ModPlug_Unload(plug);
  50. }
  51. bool ModPlugDecoder::accepts(const std::string & ext)
  52. {
  53. static const std::string supported[] = {
  54. "699", "abc", "amf", "ams", "dbm", "dmf",
  55. "dsm", "far", "it", "j2b", "mdl", "med",
  56. "mid", "mod", "mt2", "mtm", "okt", "pat",
  57. "psm", "s3m", "stm", "ult", "umx", "wav",
  58. "xm", ""
  59. };
  60. for(int i = 0; !(supported[i].empty()); i++)
  61. {
  62. if(supported[i].compare(ext) == 0)
  63. return true;
  64. }
  65. return false;
  66. }
  67. love::sound::Decoder * ModPlugDecoder::clone()
  68. {
  69. return new ModPlugDecoder(data, ext, bufferSize, settings.mFrequency);
  70. }
  71. int ModPlugDecoder::decode()
  72. {
  73. int r = ModPlug_Read(plug, buffer, bufferSize);
  74. if(r == 0)
  75. eof = true;
  76. return r;
  77. }
  78. bool ModPlugDecoder::seek(float s)
  79. {
  80. ModPlug_Seek(plug, (int)(s*1000.0f));
  81. return true;
  82. }
  83. bool ModPlugDecoder::rewind()
  84. {
  85. // Let's reload.
  86. ModPlug_Unload(plug);
  87. plug = ModPlug_Load(data->getData(), data->getSize());
  88. ModPlug_SetMasterVolume(plug, 512);
  89. eof = false;
  90. return (plug != 0);
  91. }
  92. bool ModPlugDecoder::isSeekable()
  93. {
  94. return true;
  95. }
  96. int ModPlugDecoder::getChannels() const
  97. {
  98. return 2;
  99. }
  100. int ModPlugDecoder::getBits() const
  101. {
  102. return 16;
  103. }
  104. } // lullaby
  105. } // sound
  106. } // love