Clip.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. Clip.cc
  3. Copyright 2003-7 Tim Goetze <tim@quitte.de>
  4. http://quitte.de/dsp/
  5. simple oversampled hard clipper
  6. */
  7. /*
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License
  10. as published by the Free Software Foundation; either version 2
  11. of the License, or (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  19. 02111-1307, USA or point your web browser to http://www.gnu.org.
  20. */
  21. #include "basics.h"
  22. #include "Clip.h"
  23. #include "Descriptor.h"
  24. void
  25. Clip::init()
  26. {
  27. gain = 1;
  28. threshold[0] = -.9;
  29. threshold[1] = +.9;
  30. /* going a bit lower than nominal with fc */
  31. double f = .5 * M_PI / OVERSAMPLE;
  32. /* construct the upsampler filter kernel */
  33. DSP::sinc (f, up.c, FIR_SIZE);
  34. DSP::kaiser<DSP::apply_window> (up.c, FIR_SIZE, 6.4);
  35. /* copy upsampler filter kernel for downsampler, make sum */
  36. double s = 0;
  37. for (int i = 0; i < up.n; ++i)
  38. down.c[i] = up.c[i],
  39. s += up.c[i];
  40. /* scale downsampler kernel for unity gain */
  41. s = 1 / s;
  42. for (int i = 0; i < down.n; ++i)
  43. down.c[i] *= s;
  44. /* scale upsampler kernel for unity gain */
  45. s *= OVERSAMPLE;
  46. for (int i = 0; i < up.n; ++i)
  47. up.c[i] *= s;
  48. }
  49. inline sample_t
  50. Clip::clip (sample_t a)
  51. {
  52. if (a < threshold[0])
  53. return threshold[0];
  54. if (a > threshold[1])
  55. return threshold[1];
  56. return a;
  57. }
  58. template <sample_func_t F>
  59. void
  60. Clip::one_cycle (int frames)
  61. {
  62. sample_t * s = ports[0];
  63. double g = getport (1);
  64. double gf;
  65. if (g == gain_db)
  66. gf = 1;
  67. else
  68. {
  69. gain_db = g;
  70. sample_t g = DSP::db2lin (gain_db);
  71. gf = pow (g / gain, 1 / (double) frames);
  72. }
  73. sample_t * d = ports[2];
  74. *ports[3] = OVERSAMPLE;
  75. for (int i = 0; i < frames; ++i)
  76. {
  77. register sample_t a = gain * s[i];
  78. a = down.process (clip (up.upsample (a)));
  79. for (int o = 1; o < OVERSAMPLE; ++o)
  80. down.store (clip (up.pad (o)));
  81. F (d, i, a, adding_gain);
  82. gain *= gf;
  83. }
  84. }
  85. /* //////////////////////////////////////////////////////////////////////// */
  86. PortInfo
  87. Clip::port_info [] =
  88. {
  89. {
  90. "in",
  91. INPUT | AUDIO,
  92. {BOUNDED, -1, 1}
  93. }, {
  94. "gain (dB)",
  95. INPUT | CONTROL,
  96. {BOUNDED | DEFAULT_1, -72, 72}
  97. }, {
  98. "out",
  99. OUTPUT | AUDIO,
  100. {0}
  101. }, {
  102. "latency",
  103. OUTPUT | CONTROL,
  104. {0}
  105. }
  106. };
  107. template <> void
  108. Descriptor<Clip>::setup()
  109. {
  110. UniqueID = 1771;
  111. Label = "Clip";
  112. Properties = HARD_RT;
  113. Name = CAPS "Clip - Hard clipper, 8x oversampled";
  114. Maker = "Tim Goetze <tim@quitte.de>";
  115. Copyright = "GPL, 2003-7";
  116. /* fill port info and vtable */
  117. autogen();
  118. }