soloud_basicwave.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. SoLoud audio engine
  3. Copyright (c) 2013-2021 Jari Komppa
  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. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. */
  19. #include "soloud_basicwave.h"
  20. #include "soloud_misc.h"
  21. namespace SoLoud
  22. {
  23. BasicwaveInstance::BasicwaveInstance(Basicwave *aParent)
  24. {
  25. mParent = aParent;
  26. mOffset = 0;
  27. mFreq = aParent->mFreq;
  28. mT = 0;
  29. }
  30. unsigned int BasicwaveInstance::getAudio(float *aBuffer, unsigned int aSamplesToRead, unsigned int aBufferSize)
  31. {
  32. unsigned int i;
  33. int waveform = mParent->mWaveform;
  34. float d = 1.0f / mSamplerate;
  35. if (!mParent->mSuperwave)
  36. {
  37. for (i = 0; i < aSamplesToRead; i++)
  38. {
  39. aBuffer[i] = SoLoud::Misc::generateWaveform(waveform, (float)fmod(mFreq * (float)mOffset, 1.0f)) * mParent->mADSR.val(mT, 10000000000000.0f);
  40. mOffset++;
  41. mT += d;
  42. }
  43. }
  44. else
  45. {
  46. for (i = 0; i < aSamplesToRead; i++)
  47. {
  48. aBuffer[i] = SoLoud::Misc::generateWaveform(waveform, (float)fmod(mFreq * (float)mOffset, 1.0f)) * mParent->mADSR.val(mT, 10000000000000.0f);
  49. float f = mFreq * (float)mOffset;
  50. for (int j = 0; j < 3; j++)
  51. {
  52. f *= 2;
  53. aBuffer[i] += SoLoud::Misc::generateWaveform(waveform, (float)fmod(mParent->mSuperwaveDetune * f, 1.0f)) * mParent->mADSR.val(mT, 10000000000000.0f) * mParent->mSuperwaveScale;
  54. }
  55. mOffset++;
  56. mT += d;
  57. }
  58. }
  59. return aSamplesToRead;
  60. }
  61. bool BasicwaveInstance::hasEnded()
  62. {
  63. // This audio source never ends.
  64. return 0;
  65. }
  66. Basicwave::Basicwave()
  67. {
  68. setSamplerate(44100);
  69. mWaveform = SoLoud::Soloud::WAVE_SQUARE;
  70. mSuperwave = false;
  71. mSuperwaveScale = 0.25f;
  72. mSuperwaveDetune = 1.0f;
  73. }
  74. Basicwave::~Basicwave()
  75. {
  76. stop();
  77. }
  78. void Basicwave::setSamplerate(float aSamplerate)
  79. {
  80. mBaseSamplerate = aSamplerate;
  81. mFreq = (float)(440 / mBaseSamplerate);
  82. }
  83. void Basicwave::setFreq(float aFreq, bool aSuperwave)
  84. {
  85. mFreq = aFreq / mBaseSamplerate;
  86. mSuperwave = aSuperwave;
  87. }
  88. void Basicwave::setWaveform(int aWaveform)
  89. {
  90. mWaveform = aWaveform;
  91. }
  92. AudioSourceInstance * Basicwave::createInstance()
  93. {
  94. return new BasicwaveInstance(this);
  95. }
  96. };