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.

201 lines
5.9KB

  1. /*
  2. * DISTRHO CycleShifter, a DPF'ied CycleShifter.
  3. * Copyright (C) 2004 Niall Moody
  4. * Copyright (C) 2015-2022 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::initAudioPort(bool input, uint32_t index, AudioPort& port)
  41. {
  42. port.groupId = kPortGroupMono;
  43. Plugin::initAudioPort(input, index, port);
  44. }
  45. void DistrhoPluginCycleShifter::initParameter(uint32_t index, Parameter& parameter)
  46. {
  47. parameter.hints = kParameterIsAutomatable;
  48. parameter.ranges.min = 0.0f;
  49. parameter.ranges.max = 1.0f;
  50. switch (index)
  51. {
  52. case kParameterNewCycleVolume:
  53. parameter.name = "New Cycle Vol";
  54. parameter.symbol = "ncvolume";
  55. parameter.ranges.def = 1.0f;
  56. break;
  57. case kParameterInputVolume:
  58. parameter.name = "Input Vol";
  59. parameter.symbol = "ipvolume";
  60. parameter.ranges.def = 1.0f;
  61. break;
  62. }
  63. }
  64. void DistrhoPluginCycleShifter::initProgramName(uint32_t index, String& programName)
  65. {
  66. if (index != 0)
  67. return;
  68. programName = "Default";
  69. }
  70. // -----------------------------------------------------------------------
  71. // Internal data
  72. float DistrhoPluginCycleShifter::getParameterValue(uint32_t index) const
  73. {
  74. switch(index)
  75. {
  76. case kParameterNewCycleVolume:
  77. return fNewCycleVolume;
  78. case kParameterInputVolume:
  79. return fInputVolume;
  80. default:
  81. return 0.0f;
  82. }
  83. }
  84. void DistrhoPluginCycleShifter::setParameterValue(uint32_t index, float value)
  85. {
  86. switch(index)
  87. {
  88. case kParameterNewCycleVolume:
  89. fNewCycleVolume = value;
  90. break;
  91. case kParameterInputVolume:
  92. fInputVolume = value;
  93. break;
  94. }
  95. }
  96. void DistrhoPluginCycleShifter::loadProgram(uint32_t index)
  97. {
  98. if (index != 0)
  99. return;
  100. fNewCycleVolume = 1.0f;
  101. fInputVolume = 1.0f;
  102. }
  103. // -----------------------------------------------------------------------
  104. // Process
  105. void DistrhoPluginCycleShifter::activate()
  106. {
  107. std::memset(CycleBuffer, 0, sizeof(float)*BUFFER_SIZE);
  108. OutIndex = 0;
  109. InCount = 0;
  110. ReadWrite = false;
  111. EnvOld = 0.0f;
  112. }
  113. void DistrhoPluginCycleShifter::run(const float** inputs, float** outputs, uint32_t frames)
  114. {
  115. const float* in = inputs[0];
  116. /**/ float* out = outputs[0];
  117. for (uint32_t i=0; i<frames; ++i)
  118. *out++ = DoProcess(*in++);
  119. }
  120. // -----------------------------------------------------------------------
  121. // Borrowed this from Toby Bear's Delphi template - it maybe adds a bit to cpu
  122. // usage, but it makes things simpler...
  123. // -----------------------------------------------------------------------
  124. float DistrhoPluginCycleShifter::DoProcess(float a)
  125. {
  126. const float tempval = a;
  127. if (! ReadWrite) // if we're in read mode
  128. {
  129. if (InCount == 0) // if we're waiting for the start of a new cycle to read
  130. {
  131. if (EnvOld < 0.0f && tempval >= 0.0f) // as soon as the input goes past 0 we start reading
  132. {
  133. CycleBuffer[InCount++] = tempval;
  134. }
  135. }
  136. else if (! (EnvOld < 0.0f && tempval >= 0.0f)) // if we've not reached the end of the cycle yet
  137. {
  138. CycleBuffer[InCount] = tempval;
  139. if (++InCount >= BUFFER_SIZE) // if we've reached the end of the buffer
  140. {
  141. InCount = BUFFER_SIZE;
  142. ReadWrite = true; // we're in write mode now
  143. }
  144. }
  145. else // we've reached the end of the cycle
  146. {
  147. CycleBuffer[InCount++] = 0.0f;
  148. ReadWrite = true;
  149. }
  150. a *= fInputVolume;
  151. }
  152. else // we're in write mode
  153. {
  154. a = (a*fInputVolume) + (CycleBuffer[OutIndex]*fNewCycleVolume);
  155. if (++OutIndex == InCount) // we've reached the end of our stored cycle
  156. {
  157. InCount = 0;
  158. OutIndex = 0;
  159. ReadWrite = false;
  160. }
  161. }
  162. EnvOld = tempval;
  163. return a;
  164. }
  165. // -----------------------------------------------------------------------
  166. Plugin* createPlugin()
  167. {
  168. return new DistrhoPluginCycleShifter();
  169. }
  170. // -----------------------------------------------------------------------
  171. END_NAMESPACE_DISTRHO