Echo.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Echo.cpp
  3. * --------
  4. * Purpose: Implementation of the DMO Echo DSP (for non-Windows platforms)
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #include "stdafx.h"
  10. #ifndef NO_PLUGINS
  11. #include "../../Sndfile.h"
  12. #include "Echo.h"
  13. #endif // !NO_PLUGINS
  14. OPENMPT_NAMESPACE_BEGIN
  15. #ifndef NO_PLUGINS
  16. namespace DMO
  17. {
  18. IMixPlugin* Echo::Create(VSTPluginLib &factory, CSoundFile &sndFile, SNDMIXPLUGIN *mixStruct)
  19. {
  20. return new (std::nothrow) Echo(factory, sndFile, mixStruct);
  21. }
  22. Echo::Echo(VSTPluginLib &factory, CSoundFile &sndFile, SNDMIXPLUGIN *mixStruct)
  23. : IMixPlugin(factory, sndFile, mixStruct)
  24. , m_bufferSize(0)
  25. , m_writePos(0)
  26. , m_sampleRate(sndFile.GetSampleRate())
  27. , m_initialFeedback(0.0f)
  28. {
  29. m_param[kEchoWetDry] = 0.5f;
  30. m_param[kEchoFeedback] = 0.5f;
  31. m_param[kEchoLeftDelay] = (500.0f - 1.0f) / 1999.0f;
  32. m_param[kEchoRightDelay] = (500.0f - 1.0f) / 1999.0f;
  33. m_param[kEchoPanDelay] = 0.0f;
  34. m_mixBuffer.Initialize(2, 2);
  35. InsertIntoFactoryList();
  36. }
  37. void Echo::Process(float *pOutL, float *pOutR, uint32 numFrames)
  38. {
  39. if(!m_bufferSize || !m_mixBuffer.Ok())
  40. return;
  41. const float wetMix = m_param[kEchoWetDry], dryMix = 1 - wetMix;
  42. const float *in[2] = { m_mixBuffer.GetInputBuffer(0), m_mixBuffer.GetInputBuffer(1) };
  43. float *out[2] = { m_mixBuffer.GetOutputBuffer(0), m_mixBuffer.GetOutputBuffer(1) };
  44. for(uint32 i = numFrames; i != 0; i--)
  45. {
  46. for(uint8 channel = 0; channel < 2; channel++)
  47. {
  48. const uint8 readChannel = (m_crossEcho ? (1 - channel) : channel);
  49. int readPos = m_writePos - m_delayTime[readChannel];
  50. if(readPos < 0)
  51. readPos += m_bufferSize;
  52. float chnInput = *(in[channel])++;
  53. float chnDelay = m_delayLine[readPos * 2 + readChannel];
  54. // Calculate the delay
  55. float chnOutput = chnInput * m_initialFeedback;
  56. chnOutput += chnDelay * m_param[kEchoFeedback];
  57. // Prevent denormals
  58. if(std::abs(chnOutput) < 1e-24f)
  59. chnOutput = 0.0f;
  60. m_delayLine[m_writePos * 2 + channel] = chnOutput;
  61. // Output samples now
  62. *(out[channel])++ = (chnInput * dryMix + chnDelay * wetMix);
  63. }
  64. m_writePos++;
  65. if(m_writePos == m_bufferSize)
  66. m_writePos = 0;
  67. }
  68. ProcessMixOps(pOutL, pOutR, m_mixBuffer.GetOutputBuffer(0), m_mixBuffer.GetOutputBuffer(1), numFrames);
  69. }
  70. PlugParamValue Echo::GetParameter(PlugParamIndex index)
  71. {
  72. if(index < kEchoNumParameters)
  73. {
  74. return m_param[index];
  75. }
  76. return 0;
  77. }
  78. void Echo::SetParameter(PlugParamIndex index, PlugParamValue value)
  79. {
  80. if(index < kEchoNumParameters)
  81. {
  82. value = mpt::safe_clamp(value, 0.0f, 1.0f);
  83. if(index == kEchoPanDelay)
  84. value = mpt::round(value);
  85. m_param[index] = value;
  86. RecalculateEchoParams();
  87. }
  88. }
  89. void Echo::Resume()
  90. {
  91. m_isResumed = true;
  92. m_sampleRate = m_SndFile.GetSampleRate();
  93. RecalculateEchoParams();
  94. PositionChanged();
  95. }
  96. void Echo::PositionChanged()
  97. {
  98. m_bufferSize = m_sampleRate * 2u;
  99. try
  100. {
  101. m_delayLine.assign(m_bufferSize * 2, 0);
  102. } catch(mpt::out_of_memory e)
  103. {
  104. mpt::delete_out_of_memory(e);
  105. m_bufferSize = 0;
  106. }
  107. m_writePos = 0;
  108. }
  109. #ifdef MODPLUG_TRACKER
  110. CString Echo::GetParamName(PlugParamIndex param)
  111. {
  112. switch(param)
  113. {
  114. case kEchoWetDry: return _T("WetDryMix");
  115. case kEchoFeedback: return _T("Feedback");
  116. case kEchoLeftDelay: return _T("LeftDelay");
  117. case kEchoRightDelay: return _T("RightDelay");
  118. case kEchoPanDelay: return _T("PanDelay");
  119. }
  120. return CString();
  121. }
  122. CString Echo::GetParamLabel(PlugParamIndex param)
  123. {
  124. switch(param)
  125. {
  126. case kEchoFeedback:
  127. return _T("%");
  128. case kEchoLeftDelay:
  129. case kEchoRightDelay:
  130. return _T("ms");
  131. default:
  132. return CString{};
  133. }
  134. }
  135. CString Echo::GetParamDisplay(PlugParamIndex param)
  136. {
  137. CString s;
  138. switch(param)
  139. {
  140. case kEchoWetDry:
  141. s.Format(_T("%.1f : %.1f"), m_param[param] * 100.0f, 100.0f - m_param[param] * 100.0f);
  142. break;
  143. case kEchoFeedback:
  144. s.Format(_T("%.2f"), m_param[param] * 100.0f);
  145. break;
  146. case kEchoLeftDelay:
  147. case kEchoRightDelay:
  148. s.Format(_T("%.2f"), 1.0f + m_param[param] * 1999.0f);
  149. break;
  150. case kEchoPanDelay:
  151. s = (m_param[param] <= 0.5) ? _T("No") : _T("Yes");
  152. }
  153. return s;
  154. }
  155. #endif // MODPLUG_TRACKER
  156. void Echo::RecalculateEchoParams()
  157. {
  158. m_initialFeedback = std::sqrt(1.0f - (m_param[kEchoFeedback] * m_param[kEchoFeedback]));
  159. m_delayTime[0] = static_cast<uint32>((1.0f + m_param[kEchoLeftDelay] * 1999.0f) / 1000.0f * m_sampleRate);
  160. m_delayTime[1] = static_cast<uint32>((1.0f + m_param[kEchoRightDelay] * 1999.0f) / 1000.0f * m_sampleRate);
  161. m_crossEcho = (m_param[kEchoPanDelay]) > 0.5f;
  162. }
  163. } // namespace DMO
  164. #else
  165. MPT_MSVC_WORKAROUND_LNK4221(Echo)
  166. #endif // !NO_PLUGINS
  167. OPENMPT_NAMESPACE_END