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.

190 lines
6.0KB

  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 modify
  7. it under the terms of version 2 of the GNU General Public License
  8. as published by the Free Software Foundation.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License (version 2 or later) for more details.
  13. You should have received a copy of the GNU General Public License (version 2)
  14. along with this program; if not, write to the Free Software Foundation,
  15. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. #include "WaveShapeSmps.h"
  18. #include <cmath>
  19. void waveShapeSmps(int n,
  20. float *smps,
  21. unsigned char type,
  22. unsigned char drive)
  23. {
  24. int i;
  25. float ws = drive / 127.0f;
  26. float tmpv;
  27. switch(type) {
  28. case 1:
  29. ws = powf(10, ws * ws * 3.0f) - 1.0f + 0.001f; //Arctangent
  30. for(i = 0; i < n; ++i)
  31. smps[i] = atanf(smps[i] * ws) / atanf(ws);
  32. break;
  33. case 2:
  34. ws = ws * ws * 32.0f + 0.0001f; //Asymmetric
  35. if(ws < 1.0f)
  36. tmpv = sinf(ws) + 0.1f;
  37. else
  38. tmpv = 1.1f;
  39. for(i = 0; i < n; ++i)
  40. smps[i] = sinf(smps[i] * (0.1f + ws - ws * smps[i])) / tmpv;
  41. ;
  42. break;
  43. case 3:
  44. ws = ws * ws * ws * 20.0f + 0.0001f; //Pow
  45. for(i = 0; i < n; ++i) {
  46. smps[i] *= ws;
  47. if(fabs(smps[i]) < 1.0f) {
  48. smps[i] = (smps[i] - powf(smps[i], 3.0f)) * 3.0f;
  49. if(ws < 1.0f)
  50. smps[i] /= ws;
  51. }
  52. else
  53. smps[i] = 0.0f;
  54. }
  55. break;
  56. case 4:
  57. ws = ws * ws * ws * 32.0f + 0.0001f; //Sine
  58. if(ws < 1.57f)
  59. tmpv = sinf(ws);
  60. else
  61. tmpv = 1.0f;
  62. for(i = 0; i < n; ++i)
  63. smps[i] = sinf(smps[i] * ws) / tmpv;
  64. break;
  65. case 5:
  66. ws = ws * ws + 0.000001f; //Quantisize
  67. for(i = 0; i < n; ++i)
  68. smps[i] = floor(smps[i] / ws + 0.5f) * ws;
  69. break;
  70. case 6:
  71. ws = ws * ws * ws * 32 + 0.0001f; //Zigzag
  72. if(ws < 1.0f)
  73. tmpv = sinf(ws);
  74. else
  75. tmpv = 1.0f;
  76. for(i = 0; i < n; ++i)
  77. smps[i] = asinf(sinf(smps[i] * ws)) / tmpv;
  78. break;
  79. case 7:
  80. ws = powf(2.0f, -ws * ws * 8.0f); //Limiter
  81. for(i = 0; i < n; ++i) {
  82. float tmp = smps[i];
  83. if(fabs(tmp) > ws) {
  84. if(tmp >= 0.0f)
  85. smps[i] = 1.0f;
  86. else
  87. smps[i] = -1.0f;
  88. }
  89. else
  90. smps[i] /= ws;
  91. }
  92. break;
  93. case 8:
  94. ws = powf(2.0f, -ws * ws * 8.0f); //Upper Limiter
  95. for(i = 0; i < n; ++i) {
  96. float tmp = smps[i];
  97. if(tmp > ws)
  98. smps[i] = ws;
  99. smps[i] *= 2.0f;
  100. }
  101. break;
  102. case 9:
  103. ws = powf(2.0f, -ws * ws * 8.0f); //Lower Limiter
  104. for(i = 0; i < n; ++i) {
  105. float tmp = smps[i];
  106. if(tmp < -ws)
  107. smps[i] = -ws;
  108. smps[i] *= 2.0f;
  109. }
  110. break;
  111. case 10:
  112. ws = (powf(2.0f, ws * 6.0f) - 1.0f) / powf(2.0f, 6.0f); //Inverse Limiter
  113. for(i = 0; i < n; ++i) {
  114. float tmp = smps[i];
  115. if(fabs(tmp) > ws) {
  116. if(tmp >= 0.0f)
  117. smps[i] = tmp - ws;
  118. else
  119. smps[i] = tmp + ws;
  120. }
  121. else
  122. smps[i] = 0;
  123. }
  124. break;
  125. case 11:
  126. ws = powf(5, ws * ws * 1.0f) - 1.0f; //Clip
  127. for(i = 0; i < n; ++i)
  128. smps[i] = smps[i]
  129. * (ws + 0.5f) * 0.9999f - floor(
  130. 0.5f + smps[i] * (ws + 0.5f) * 0.9999f);
  131. break;
  132. case 12:
  133. ws = ws * ws * ws * 30 + 0.001f; //Asym2
  134. if(ws < 0.3f)
  135. tmpv = ws;
  136. else
  137. tmpv = 1.0f;
  138. for(i = 0; i < n; ++i) {
  139. float tmp = smps[i] * ws;
  140. if((tmp > -2.0f) && (tmp < 1.0f))
  141. smps[i] = tmp * (1.0f - tmp) * (tmp + 2.0f) / tmpv;
  142. else
  143. smps[i] = 0.0f;
  144. }
  145. break;
  146. case 13:
  147. ws = ws * ws * ws * 32.0f + 0.0001f; //Pow2
  148. if(ws < 1.0f)
  149. tmpv = ws * (1 + ws) / 2.0f;
  150. else
  151. tmpv = 1.0f;
  152. for(i = 0; i < n; ++i) {
  153. float tmp = smps[i] * ws;
  154. if((tmp > -1.0f) && (tmp < 1.618034f))
  155. smps[i] = tmp * (1.0f - tmp) / tmpv;
  156. else
  157. if(tmp > 0.0f)
  158. smps[i] = -1.0f;
  159. else
  160. smps[i] = -2.0f;
  161. }
  162. break;
  163. case 14:
  164. ws = powf(ws, 5.0f) * 80.0f + 0.0001f; //sigmoid
  165. if(ws > 10.0f)
  166. tmpv = 0.5f;
  167. else
  168. tmpv = 0.5f - 1.0f / (expf(ws) + 1.0f);
  169. for(i = 0; i < n; ++i) {
  170. float tmp = smps[i] * ws;
  171. if(tmp < -10.0f)
  172. tmp = -10.0f;
  173. else
  174. if(tmp > 10.0f)
  175. tmp = 10.0f;
  176. tmp = 0.5f - 1.0f / (expf(tmp) + 1.0f);
  177. smps[i] = tmp / tmpv;
  178. }
  179. break;
  180. }
  181. }