ModPlugDecoder.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. "it", "xm", "mod", "wav", ""
  55. };
  56. for(int i = 0; !(supported[i].empty()); i++)
  57. {
  58. if(supported[i].compare(ext) == 0)
  59. return true;
  60. }
  61. return false;
  62. }
  63. love::sound::Decoder * ModPlugDecoder::clone()
  64. {
  65. return new ModPlugDecoder(data, ext, bufferSize, settings.mFrequency);
  66. }
  67. int ModPlugDecoder::decode()
  68. {
  69. int r = ModPlug_Read(plug, buffer, bufferSize);
  70. if(r == 0)
  71. eof = true;
  72. return r;
  73. }
  74. bool ModPlugDecoder::seek(float s)
  75. {
  76. ModPlug_Seek(plug, (int)(s*1000.0f));
  77. return true;
  78. }
  79. bool ModPlugDecoder::rewind()
  80. {
  81. // Let's reload.
  82. ModPlug_Unload(plug);
  83. plug = ModPlug_Load(data->getData(), data->getSize());
  84. ModPlug_SetMasterVolume(plug, 512);
  85. eof = false;
  86. return (plug != 0);
  87. }
  88. bool ModPlugDecoder::isSeekable()
  89. {
  90. return true;
  91. }
  92. int ModPlugDecoder::getChannels() const
  93. {
  94. return 2;
  95. }
  96. int ModPlugDecoder::getBits() const
  97. {
  98. return 16;
  99. }
  100. } // lullaby
  101. } // sound
  102. } // love