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.

185 lines
5.6KB

  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. WaveShapeSmps.cpp - Sample Distortion
  4. Copyright (C) 2002-2005 Nasca Octavian Paul
  5. Author: Nasca Octavian Paul
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or (at your option) any later version.
  10. */
  11. #include "WaveShapeSmps.h"
  12. #include <cmath>
  13. namespace zyncarla {
  14. void waveShapeSmps(int n,
  15. float *smps,
  16. unsigned char type,
  17. unsigned char drive)
  18. {
  19. int i;
  20. float ws = drive / 127.0f;
  21. float tmpv;
  22. switch(type) {
  23. case 1:
  24. ws = powf(10, ws * ws * 3.0f) - 1.0f + 0.001f; //Arctangent
  25. for(i = 0; i < n; ++i)
  26. smps[i] = atanf(smps[i] * ws) / atanf(ws);
  27. break;
  28. case 2:
  29. ws = ws * ws * 32.0f + 0.0001f; //Asymmetric
  30. if(ws < 1.0f)
  31. tmpv = sinf(ws) + 0.1f;
  32. else
  33. tmpv = 1.1f;
  34. for(i = 0; i < n; ++i)
  35. smps[i] = sinf(smps[i] * (0.1f + ws - ws * smps[i])) / tmpv;
  36. ;
  37. break;
  38. case 3:
  39. ws = ws * ws * ws * 20.0f + 0.0001f; //Pow
  40. for(i = 0; i < n; ++i) {
  41. smps[i] *= ws;
  42. if(fabs(smps[i]) < 1.0f) {
  43. smps[i] = (smps[i] - powf(smps[i], 3.0f)) * 3.0f;
  44. if(ws < 1.0f)
  45. smps[i] /= ws;
  46. }
  47. else
  48. smps[i] = 0.0f;
  49. }
  50. break;
  51. case 4:
  52. ws = ws * ws * ws * 32.0f + 0.0001f; //Sine
  53. if(ws < 1.57f)
  54. tmpv = sinf(ws);
  55. else
  56. tmpv = 1.0f;
  57. for(i = 0; i < n; ++i)
  58. smps[i] = sinf(smps[i] * ws) / tmpv;
  59. break;
  60. case 5:
  61. ws = ws * ws + 0.000001f; //Quantisize
  62. for(i = 0; i < n; ++i)
  63. smps[i] = floor(smps[i] / ws + 0.5f) * ws;
  64. break;
  65. case 6:
  66. ws = ws * ws * ws * 32 + 0.0001f; //Zigzag
  67. if(ws < 1.0f)
  68. tmpv = sinf(ws);
  69. else
  70. tmpv = 1.0f;
  71. for(i = 0; i < n; ++i)
  72. smps[i] = asinf(sinf(smps[i] * ws)) / tmpv;
  73. break;
  74. case 7:
  75. ws = powf(2.0f, -ws * ws * 8.0f); //Limiter
  76. for(i = 0; i < n; ++i) {
  77. float tmp = smps[i];
  78. if(fabs(tmp) > ws) {
  79. if(tmp >= 0.0f)
  80. smps[i] = 1.0f;
  81. else
  82. smps[i] = -1.0f;
  83. }
  84. else
  85. smps[i] /= ws;
  86. }
  87. break;
  88. case 8:
  89. ws = powf(2.0f, -ws * ws * 8.0f); //Upper Limiter
  90. for(i = 0; i < n; ++i) {
  91. float tmp = smps[i];
  92. if(tmp > ws)
  93. smps[i] = ws;
  94. smps[i] *= 2.0f;
  95. }
  96. break;
  97. case 9:
  98. ws = powf(2.0f, -ws * ws * 8.0f); //Lower Limiter
  99. for(i = 0; i < n; ++i) {
  100. float tmp = smps[i];
  101. if(tmp < -ws)
  102. smps[i] = -ws;
  103. smps[i] *= 2.0f;
  104. }
  105. break;
  106. case 10:
  107. ws = (powf(2.0f, ws * 6.0f) - 1.0f) / powf(2.0f, 6.0f); //Inverse Limiter
  108. for(i = 0; i < n; ++i) {
  109. float tmp = smps[i];
  110. if(fabs(tmp) > ws) {
  111. if(tmp >= 0.0f)
  112. smps[i] = tmp - ws;
  113. else
  114. smps[i] = tmp + ws;
  115. }
  116. else
  117. smps[i] = 0;
  118. }
  119. break;
  120. case 11:
  121. ws = powf(5, ws * ws * 1.0f) - 1.0f; //Clip
  122. for(i = 0; i < n; ++i)
  123. smps[i] = smps[i]
  124. * (ws + 0.5f) * 0.9999f - floor(
  125. 0.5f + smps[i] * (ws + 0.5f) * 0.9999f);
  126. break;
  127. case 12:
  128. ws = ws * ws * ws * 30 + 0.001f; //Asym2
  129. if(ws < 0.3f)
  130. tmpv = ws;
  131. else
  132. tmpv = 1.0f;
  133. for(i = 0; i < n; ++i) {
  134. float tmp = smps[i] * ws;
  135. if((tmp > -2.0f) && (tmp < 1.0f))
  136. smps[i] = tmp * (1.0f - tmp) * (tmp + 2.0f) / tmpv;
  137. else
  138. smps[i] = 0.0f;
  139. }
  140. break;
  141. case 13:
  142. ws = ws * ws * ws * 32.0f + 0.0001f; //Pow2
  143. if(ws < 1.0f)
  144. tmpv = ws * (1 + ws) / 2.0f;
  145. else
  146. tmpv = 1.0f;
  147. for(i = 0; i < n; ++i) {
  148. float tmp = smps[i] * ws;
  149. if((tmp > -1.0f) && (tmp < 1.618034f))
  150. smps[i] = tmp * (1.0f - tmp) / tmpv;
  151. else
  152. if(tmp > 0.0f)
  153. smps[i] = -1.0f;
  154. else
  155. smps[i] = -2.0f;
  156. }
  157. break;
  158. case 14:
  159. ws = powf(ws, 5.0f) * 80.0f + 0.0001f; //sigmoid
  160. if(ws > 10.0f)
  161. tmpv = 0.5f;
  162. else
  163. tmpv = 0.5f - 1.0f / (expf(ws) + 1.0f);
  164. for(i = 0; i < n; ++i) {
  165. float tmp = smps[i] * ws;
  166. if(tmp < -10.0f)
  167. tmp = -10.0f;
  168. else
  169. if(tmp > 10.0f)
  170. tmp = 10.0f;
  171. tmp = 0.5f - 1.0f / (expf(tmp) + 1.0f);
  172. smps[i] = tmp / tmpv;
  173. }
  174. break;
  175. }
  176. }
  177. }