Collection of DPF-based plugins for packaging
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
5.7KB

  1. /*
  2. * DISTRHO CycleShifter, a DPF'ied CycleShifter.
  3. * Copyright (C) 2004 Niall Moody
  4. * Copyright (C) 2015 Filipe Coelho <falktx@falktx.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. */
  24. #include "DistrhoPluginCycleShifter.hpp"
  25. START_NAMESPACE_DISTRHO
  26. // -----------------------------------------------------------------------
  27. DistrhoPluginCycleShifter::DistrhoPluginCycleShifter()
  28. : Plugin(kParameterCount, 1, 0), // 1 program, 0 states
  29. fNewCycleVolume(1.0f),
  30. fInputVolume(1.0f),
  31. OutIndex(0),
  32. InCount(0),
  33. ReadWrite(false),
  34. EnvOld(0.0f)
  35. {
  36. std::memset(CycleBuffer, 0, sizeof(float)*BUFFER_SIZE);
  37. }
  38. // -----------------------------------------------------------------------
  39. // Init
  40. void DistrhoPluginCycleShifter::initParameter(uint32_t index, Parameter& parameter)
  41. {
  42. parameter.hints = kParameterIsAutomable;
  43. parameter.ranges.min = 0.0f;
  44. parameter.ranges.max = 1.0f;
  45. switch (index)
  46. {
  47. case kParameterNewCycleVolume:
  48. parameter.name = "New Cycle Vol";
  49. parameter.symbol = "ncvolume";
  50. parameter.ranges.def = 1.0f;
  51. break;
  52. case kParameterInputVolume:
  53. parameter.name = "Input Vol";
  54. parameter.symbol = "ipvolume";
  55. parameter.ranges.def = 1.0f;
  56. break;
  57. }
  58. }
  59. void DistrhoPluginCycleShifter::initProgramName(uint32_t index, String& programName)
  60. {
  61. if (index != 0)
  62. return;
  63. programName = "Default";
  64. }
  65. // -----------------------------------------------------------------------
  66. // Internal data
  67. float DistrhoPluginCycleShifter::getParameterValue(uint32_t index) const
  68. {
  69. switch(index)
  70. {
  71. case kParameterNewCycleVolume:
  72. return fNewCycleVolume;
  73. case kParameterInputVolume:
  74. return fInputVolume;
  75. default:
  76. return 0.0f;
  77. }
  78. }
  79. void DistrhoPluginCycleShifter::setParameterValue(uint32_t index, float value)
  80. {
  81. switch(index)
  82. {
  83. case kParameterNewCycleVolume:
  84. fNewCycleVolume = value;
  85. break;
  86. case kParameterInputVolume:
  87. fInputVolume = value;
  88. break;
  89. }
  90. }
  91. void DistrhoPluginCycleShifter::loadProgram(uint32_t index)
  92. {
  93. if (index != 0)
  94. return;
  95. fNewCycleVolume = 1.0f;
  96. fInputVolume = 1.0f;
  97. }
  98. // -----------------------------------------------------------------------
  99. // Process
  100. void DistrhoPluginCycleShifter::activate()
  101. {
  102. std::memset(CycleBuffer, 0, sizeof(float)*BUFFER_SIZE);
  103. OutIndex = 0;
  104. InCount = 0;
  105. ReadWrite = false;
  106. EnvOld = 0.0f;
  107. }
  108. void DistrhoPluginCycleShifter::run(const float** inputs, float** outputs, uint32_t frames)
  109. {
  110. const float* in = inputs[0];
  111. /**/ float* out = outputs[0];
  112. for (uint32_t i=0; i<frames; ++i)
  113. *out++ = DoProcess(*in++);
  114. }
  115. // -----------------------------------------------------------------------
  116. // Borrowed this from Toby Bear's Delphi template - it maybe adds a bit to cpu
  117. // usage, but it makes things simpler...
  118. // -----------------------------------------------------------------------
  119. float DistrhoPluginCycleShifter::DoProcess(float a)
  120. {
  121. const float tempval = a;
  122. if (! ReadWrite) // if we're in read mode
  123. {
  124. if (InCount == 0) // if we're waiting for the start of a new cycle to read
  125. {
  126. if (EnvOld < 0.0f && tempval >= 0.0f) // as soon as the input goes past 0 we start reading
  127. {
  128. CycleBuffer[InCount++] = tempval;
  129. }
  130. }
  131. else if (! (EnvOld < 0.0f && tempval >= 0.0f)) // if we've not reached the end of the cycle yet
  132. {
  133. CycleBuffer[InCount] = tempval;
  134. if (++InCount >= BUFFER_SIZE) // if we've reached the end of the buffer
  135. {
  136. InCount = BUFFER_SIZE;
  137. ReadWrite = true; // we're in write mode now
  138. }
  139. }
  140. else // we've reached the end of the cycle
  141. {
  142. CycleBuffer[InCount++] = 0.0f;
  143. ReadWrite = true;
  144. }
  145. a *= fInputVolume;
  146. }
  147. else // we're in write mode
  148. {
  149. a = (a*fInputVolume) + (CycleBuffer[OutIndex]*fNewCycleVolume);
  150. if (++OutIndex == InCount) // we've reached the end of our stored cycle
  151. {
  152. InCount = 0;
  153. OutIndex = 0;
  154. ReadWrite = false;
  155. }
  156. }
  157. EnvOld = tempval;
  158. return a;
  159. }
  160. // -----------------------------------------------------------------------
  161. Plugin* createPlugin()
  162. {
  163. return new DistrhoPluginCycleShifter();
  164. }
  165. // -----------------------------------------------------------------------
  166. END_NAMESPACE_DISTRHO