Pan.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. Pan.cc
  3. Copyright 2002-11 Tim Goetze <tim@quitte.de>
  4. http://quitte.de/dsp/
  5. panorama with width control,
  6. stereo image width reduction
  7. */
  8. /*
  9. This program is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU General Public License
  11. as published by the Free Software Foundation; either version 2
  12. of the License, or (at your option) any later version.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  20. 02111-1307, USA or point your web browser to http://www.gnu.org.
  21. */
  22. #include "basics.h"
  23. #include "Pan.h"
  24. #include "Descriptor.h"
  25. void
  26. Pan::init()
  27. {
  28. delay.init ((int) (.040 * fs));
  29. }
  30. void
  31. Pan::activate()
  32. {
  33. delay.reset();
  34. tap.reset (400 / fs);
  35. set_pan (getport (1));
  36. }
  37. inline void
  38. Pan::set_pan (sample_t p)
  39. {
  40. pan = p;
  41. double phi = (pan + 1) * M_PI * .25;
  42. gain_l = cos (phi);
  43. gain_r = sin (phi);
  44. }
  45. template <sample_func_t F>
  46. void
  47. Pan::one_cycle (int frames)
  48. {
  49. sample_t * s = ports[0];
  50. if (pan != *ports[1])
  51. set_pan (getport(1));
  52. sample_t g = getport(2);
  53. sample_t
  54. width_l = g * gain_r,
  55. width_r = g * gain_l;
  56. tap.t = (int) (getport(3) * fs * .001);
  57. bool mono = getport(4);
  58. sample_t * dl = ports[5];
  59. sample_t * dr = ports[6];
  60. sample_t x, xt;
  61. if (mono) for (int i = 0; i < frames; ++i)
  62. {
  63. x = s[i];
  64. xt = tap.get (delay);
  65. delay.put (x + normal);
  66. x = (gain_l * x + gain_r * x + width_l * xt + width_r * xt) * .5;
  67. F (dl, i, x, adding_gain);
  68. F (dr, i, x, adding_gain);
  69. normal = -normal;
  70. }
  71. else /* stereo */ for (int i = 0; i < frames; ++i)
  72. {
  73. x = s[i];
  74. xt = tap.get (delay);
  75. delay.put (x + normal);
  76. F (dl, i, gain_l * x + width_l * xt, adding_gain);
  77. F (dr, i, gain_r * x + width_r * xt, adding_gain);
  78. normal = -normal;
  79. }
  80. }
  81. /* //////////////////////////////////////////////////////////////////////// */
  82. PortInfo
  83. Pan::port_info [] =
  84. {
  85. {
  86. "in",
  87. INPUT | AUDIO,
  88. {0}
  89. }, {
  90. "pan",
  91. INPUT | CONTROL,
  92. {BOUNDED | DEFAULT_0, -1, 1}
  93. }, {
  94. "width",
  95. INPUT | CONTROL,
  96. {BOUNDED | DEFAULT_0, 0, 1}
  97. }, {
  98. "t",
  99. INPUT | CONTROL,
  100. {BOUNDED | DEFAULT_LOW, 0.1, 40}
  101. }, {
  102. "mono",
  103. INPUT | CONTROL,
  104. {BOUNDED | DEFAULT_0 | INTEGER | TOGGLE, 0, 1}
  105. }, {
  106. "out:l",
  107. OUTPUT | AUDIO,
  108. {0}
  109. }, {
  110. "out:r",
  111. OUTPUT | AUDIO,
  112. {0}
  113. }
  114. };
  115. template <> void
  116. Descriptor<Pan>::setup()
  117. {
  118. UniqueID = 1788;
  119. Label = "Pan";
  120. Properties = HARD_RT;
  121. Name = CAPS "Pan - Pan and width";
  122. Maker = "Tim Goetze <tim@quitte.de>";
  123. Copyright = "GPL, 2004-7";
  124. /* fill port info and vtable */
  125. autogen();
  126. }
  127. /* //////////////////////////////////////////////////////////////////////// */
  128. void
  129. Narrower::init()
  130. {
  131. }
  132. void
  133. Narrower::activate()
  134. {
  135. }
  136. template <sample_func_t F>
  137. void
  138. Narrower::one_cycle (int frames)
  139. {
  140. sample_t * sl = ports[0];
  141. sample_t * sr = ports[1];
  142. if (strength != *ports[2])
  143. strength = *ports[2];
  144. sample_t * dl = ports[3];
  145. sample_t * dr = ports[4];
  146. double xl, xr, m;
  147. double dry = 1 - strength, wet = strength;
  148. for (int i = 0; i < frames; ++i)
  149. {
  150. xl = sl[i];
  151. xr = sr[i];
  152. m = wet * (xl + xr) * .5;
  153. xl *= dry;
  154. xr *= dry;
  155. xl += m;
  156. xr += m;
  157. F (dl, i, xl, adding_gain);
  158. F (dr, i, xr, adding_gain);
  159. }
  160. }
  161. /* //////////////////////////////////////////////////////////////////////// */
  162. PortInfo
  163. Narrower::port_info [] =
  164. {
  165. {
  166. "in:l",
  167. INPUT | AUDIO,
  168. {0}
  169. }, {
  170. "in:r",
  171. INPUT | AUDIO,
  172. {0}
  173. }, {
  174. "strength",
  175. INPUT | CONTROL,
  176. {BOUNDED | DEFAULT_LOW, 0, 1}
  177. }, {
  178. "out:l",
  179. OUTPUT | AUDIO,
  180. {0}
  181. }, {
  182. "out:r",
  183. OUTPUT | AUDIO,
  184. {0}
  185. }
  186. };
  187. template <> void
  188. Descriptor<Narrower>::setup()
  189. {
  190. UniqueID = 2595;
  191. Label = "Narrower";
  192. Properties = HARD_RT;
  193. Name = CAPS "Narrower - Stereo image width reduction";
  194. Maker = "Tim Goetze <tim@quitte.de>";
  195. Copyright = "GPL, 2011";
  196. /* fill port info and vtable */
  197. autogen();
  198. }