Audio plugin host https://kx.studio/carla
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.

270 lines
6.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCETICE project - Copyright 2008 by Lucio Asnaghi.
  4. JUCETICE is based around the JUCE library - "Jules' Utility Class Extensions"
  5. Copyright 2008 by Julian Storer.
  6. ------------------------------------------------------------------------------
  7. JUCE and JUCETICE can be redistributed and/or modified under the terms of
  8. the GNU Lesser General Public License, as published by the Free Software
  9. Foundation; either version 2 of the License, or (at your option) any later
  10. version.
  11. JUCE and JUCETICE are distributed in the hope that they will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public License
  16. along with JUCE and JUCETICE; if not, visit www.gnu.org/licenses or write to
  17. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  18. Boston, MA 02111-1307 USA
  19. ==============================================================================
  20. @author rockhardbuns
  21. @tweaker Lucio Asnaghi
  22. @tweaker falkTX
  23. ==============================================================================
  24. */
  25. #include "VexVoice.h"
  26. float convertPitch(float pitch)
  27. {
  28. long convert;
  29. float *p = (float*)&convert;
  30. float fl, fr, warp, out;
  31. fl = std::floor(pitch);
  32. fr = pitch - fl;
  33. warp = fr*0.696f + fr*fr*0.225f + fr*fr*fr*0.079f; // chebychev approx
  34. out = fl+warp;
  35. out *= 8388608.0; //2^23;
  36. out += 127.0 * 8388608.0; //2^23;
  37. convert = (long)out; //magic
  38. return *p;
  39. }
  40. float bipolar(const float in)
  41. {
  42. return in * 2.0f - 1.0f;
  43. }
  44. VexVoice::VexVoice(const float* const p, int po, WaveRenderer& w, float sr)
  45. : wr(w),
  46. parameters(p),
  47. poff(po)
  48. {
  49. Ordinal = 1;
  50. SampleRate = sr;
  51. isOn = false;
  52. note = 0;
  53. lfoC = 2.f * (float)std::sin(float_Pi * 5.0f / SampleRate);
  54. lfoS[0] = 0.3f;
  55. lfoS[1] = 0.0f;
  56. lowL = 0.0f;
  57. bandL = 0.0f;
  58. highL = 0.0f;
  59. lowR = 0.0f;
  60. bandR = 0.0f;
  61. highR = 0.0f;
  62. q = 0.0f;
  63. cut = 0.0f;
  64. }
  65. void VexVoice::updateParameterPtr(const float* const p)
  66. {
  67. parameters = p;
  68. }
  69. void VexVoice::doProcess(float* outBufferL, float* outBufferR, int bufferSize)
  70. {
  71. if (outBufferL == nullptr || outBufferR == nullptr || bufferSize == 0)
  72. return;
  73. //float resAmt = 0.0;
  74. float A, B;
  75. float amod;
  76. wr.fillBuffer(outBufferL, bufferSize, oL);
  77. wr.fillBuffer(outBufferR, bufferSize, oR);
  78. for (int i = 0; i < bufferSize; i++)
  79. {
  80. //LFO
  81. lfoS[0] = lfoS[0] - lfoC * lfoS[1];
  82. lfoS[1] = lfoS[1] + lfoC * lfoS[0];
  83. LFOA = lfoS[0] * parameters[20 + poff];
  84. LFOF = lfoS[0] * parameters[21 + poff];
  85. //Filter Mod
  86. q = 1.1f - parameters[6 + poff];
  87. cut = jlimit(0.001f, 0.999f, parameters[5 + poff]
  88. + (fadsr.getSample() * bipolar(parameters[8 + poff]))
  89. + Fvelocity
  90. + LFOF);
  91. amod = LFOA + Avelocity;
  92. //Filter
  93. //Left
  94. lowL = lowL + cut * bandL;
  95. highL = outBufferL[i] - lowL - (q * bandL);
  96. bandL = cut * highL + bandL;
  97. B = (lowL * ((q * 0.5f) + 0.5f));
  98. A = (highL * ((q * 0.5f) + 0.5f));
  99. outBufferL[i] = A + parameters[7 + poff] * ( B - A );
  100. outBufferL[i] += outBufferL[i] * amod;
  101. //Right
  102. lowR = lowR + cut * bandR;
  103. highR = outBufferR[i] - lowR - (q * bandR);
  104. bandR = cut * highR + bandR;
  105. B = (lowR * ((q * 0.5f) + 0.5f));
  106. A = (highR * ((q * 0.5f) + 0.5f));
  107. outBufferR[i] = A + parameters[7 + poff] * ( B - A );
  108. outBufferR[i] += outBufferR[i] * amod;
  109. }
  110. aadsr.doProcess(outBufferL, outBufferR, bufferSize);
  111. isOn = aadsr.getState();
  112. }
  113. void VexVoice::start(float f, float v, int n, int preroll, double s, long o)
  114. {
  115. Ordinal = o;
  116. SampleRate = s;
  117. float oct = (parameters[1 + poff] - 0.5f) * 4.0f;
  118. float cent = (parameters[2 + poff] - 0.5f) * 0.1f;
  119. BaseFrequency = f * convertPitch(cent + oct);
  120. wr.reset(BaseFrequency, SampleRate, oL);
  121. wr.reset(BaseFrequency, SampleRate, oR);
  122. note = n;
  123. lfoS[0] = 0.5f;
  124. lfoS[1] = 0.0f;
  125. isOn = true;
  126. isReleased = false;
  127. v = (v * v) - 1.0f;
  128. Avelocity = (v * bipolar(parameters[18 + poff]));
  129. Fvelocity = (1.0f + v) * bipolar(parameters[13 + poff]);
  130. aadsr.reset(preroll);
  131. fadsr.reset(preroll);
  132. }
  133. void VexVoice::release(const int p)
  134. {
  135. isReleased = true;
  136. aadsr.release(p);
  137. fadsr.release(p);
  138. }
  139. void VexVoice::quickRelease()
  140. {
  141. isReleased = true;
  142. aadsr.quickRelease();
  143. }
  144. void VexVoice::kill()
  145. {
  146. isOn = false;
  147. }
  148. /*
  149. * params:
  150. 1 = oct
  151. 2 = cent
  152. 3 = phaseOffset
  153. 4 = phaseIncOffset
  154. 5,6,7,8 = filter
  155. 9,10,11,12 = F ADSR
  156. 13 = F velocity
  157. 14,15,16,17 = A ADSR
  158. 18 = A velocity
  159. 19 = lfoC
  160. 20 = lfoA
  161. 21 = lfoF
  162. 22,23,24 = fx volumes
  163. *3
  164. 83,84,85 = panning synths
  165. 86,87,88 = volume synths
  166. 89,90,91 = on/off synths
  167. */
  168. void VexVoice::update(const int index)
  169. {
  170. float p;
  171. switch (index-poff)
  172. {
  173. case 14:
  174. case 15:
  175. case 16:
  176. case 17:
  177. aadsr.setADSR(parameters[14+poff], parameters[15+poff], parameters[16+poff], parameters[17+poff], SampleRate);
  178. break;
  179. case 9:
  180. case 10:
  181. case 11:
  182. case 12:
  183. fadsr.setADSR(parameters[9+poff], parameters[10+poff], parameters[11+poff], parameters[12+poff], SampleRate);
  184. break;
  185. case 19:
  186. lfoC = 2.f * (float)sin(float_Pi*(parameters[19 + poff] * 10.0f) / SampleRate);
  187. break;
  188. case 3:
  189. p = bipolar(parameters[3 + poff]);
  190. oL.phaseOffset = p > 0.0f ? p : 0.0f;
  191. oR.phaseOffset = p < 0.0f ? fabs(p) : 0.0f;
  192. break;
  193. case 4:
  194. p = bipolar(parameters[4 + poff]);
  195. oL.phaseIncOffset = p > 0.0f ? p : 0.0f;
  196. oR.phaseIncOffset = p < 0.0f ? fabs(p) : 0.0f;
  197. break;
  198. }
  199. }
  200. long VexVoice::getOrdinal() const
  201. {
  202. return Ordinal;
  203. }
  204. int VexVoice::getNote() const
  205. {
  206. return note;
  207. }
  208. bool VexVoice::getIsOn() const
  209. {
  210. return isOn;
  211. }
  212. bool VexVoice::getIsReleased() const
  213. {
  214. return isReleased;
  215. }