Delay.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * Delay.h - Delay effect objects to use as building blocks in DSP
  3. *
  4. * Copyright (c) 2014 Vesa Kivimäki <contact/dot/diizy/at/nbl/dot/fi>
  5. * Copyright (c) 2006-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  6. *
  7. * This file is part of LMMS - https://lmms.io
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2 of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public
  20. * License along with this program (see COPYING); if not, write to the
  21. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  22. * Boston, MA 02110-1301 USA.
  23. *
  24. */
  25. #ifndef DELAY_H
  26. #define DELAY_H
  27. #include "lmms_basics.h"
  28. #include "lmms_math.h"
  29. #include "interpolation.h"
  30. #include "MemoryManager.h"
  31. // brief usage
  32. // Classes:
  33. // CombFeedback: a feedback comb filter - basically a simple delay line, makes a comb shape in the freq response
  34. // CombFeedfwd: a feed-forward comb filter - an "inverted" comb filter, can be combined with CombFeedback to create a net allpass if negative gain is used
  35. // CombFeedbackDualtap: same as CombFeedback but takes two delay values
  36. // AllpassDelay: an allpass delay - combines feedback and feed-forward - has flat frequency response
  37. // all classes are templated with channel count, any arbitrary channel count can be used for each fx
  38. // Methods (for all classes):
  39. // setDelay sets delay amount in frames. It's up to you to make this samplerate-agnostic.
  40. // Fractions are allowed - linear interpolation is used to deal with them
  41. // CombFeedbackDualTap is a special case: it requires 2 delay times
  42. // setMaxDelay (re)sets the maximum allowed delay, in frames
  43. // NOTE: for performance reasons, there's no bounds checking at setDelay, so make sure you set maxDelay >= delay!
  44. // clearHistory clears the delay buffer
  45. // setGain sets the feedback/feed-forward gain, in linear amplitude, negative values are allowed
  46. // 1.0 is full feedback/feed-forward, -1.0 is full negative feedback/feed-forward
  47. // update runs the fx for one frame - takes as arguments input and number of channel to run, returns output
  48. template<ch_cnt_t CHANNELS>
  49. class CombFeedback
  50. {
  51. public:
  52. typedef double frame[CHANNELS];
  53. CombFeedback( int maxDelay ) :
  54. m_size( maxDelay ),
  55. m_position( 0 ),
  56. m_feedBack( 0.0 ),
  57. m_delay( 0 ),
  58. m_fraction( 0.0 )
  59. {
  60. m_buffer = MM_ALLOC<frame>(maxDelay );
  61. memset( m_buffer, 0, sizeof( frame ) * maxDelay );
  62. }
  63. virtual ~CombFeedback()
  64. {
  65. MM_FREE( m_buffer );
  66. }
  67. inline void setMaxDelay( int maxDelay )
  68. {
  69. if( maxDelay > m_size )
  70. {
  71. MM_FREE( m_buffer );
  72. m_buffer = MM_ALLOC<frame>( maxDelay );
  73. memset( m_buffer, 0, sizeof( frame ) * maxDelay );
  74. }
  75. m_size = maxDelay;
  76. m_position %= m_size;
  77. }
  78. inline void clearHistory()
  79. {
  80. memset( m_buffer, 0, sizeof( frame ) * m_size );
  81. }
  82. inline void setDelay( double delay )
  83. {
  84. m_delay = static_cast<int>( ceil( delay ) );
  85. m_fraction = 1.0 - ( delay - floor( delay ) );
  86. }
  87. inline void setGain( double gain )
  88. {
  89. m_gain = gain;
  90. }
  91. inline double update( double in, ch_cnt_t ch )
  92. {
  93. int readPos = m_position - m_delay;
  94. if( readPos < 0 ) { readPos += m_size; }
  95. const double y = linearInterpolate( m_buffer[readPos][ch], m_buffer[( readPos + 1 ) % m_size][ch], m_fraction );
  96. ++m_position %= m_size;
  97. m_buffer[m_position][ch] = in + m_gain * y;
  98. return y;
  99. }
  100. private:
  101. frame * m_buffer;
  102. int m_size;
  103. int m_position;
  104. double m_gain;
  105. int m_delay;
  106. double m_fraction;
  107. };
  108. template<ch_cnt_t CHANNELS>
  109. class CombFeedfwd
  110. {
  111. typedef double frame[CHANNELS];
  112. CombFeedfwd( int maxDelay ) :
  113. m_size( maxDelay ),
  114. m_position( 0 ),
  115. m_feedBack( 0.0 ),
  116. m_delay( 0 ),
  117. m_fraction( 0.0 )
  118. {
  119. m_buffer = MM_ALLOC<frame>( maxDelay );
  120. memset( m_buffer, 0, sizeof( frame ) * maxDelay );
  121. }
  122. virtual ~CombFeedfwd()
  123. {
  124. MM_FREE( m_buffer );
  125. }
  126. inline void setMaxDelay( int maxDelay )
  127. {
  128. if( maxDelay > m_size )
  129. {
  130. MM_FREE( m_buffer );
  131. m_buffer = MM_ALLOC<frame>( maxDelay );
  132. memset( m_buffer, 0, sizeof( frame ) * maxDelay );
  133. }
  134. m_size = maxDelay;
  135. m_position %= m_size;
  136. }
  137. inline void clearHistory()
  138. {
  139. memset( m_buffer, 0, sizeof( frame ) * m_size );
  140. }
  141. inline void setDelay( double delay )
  142. {
  143. m_delay = static_cast<int>( ceil( delay ) );
  144. m_fraction = 1.0 - ( delay - floor( delay ) );
  145. }
  146. inline void setGain( double gain )
  147. {
  148. m_gain = gain;
  149. }
  150. inline double update( double in, ch_cnt_t ch )
  151. {
  152. int readPos = m_position - m_delay;
  153. if( readPos < 0 ) { readPos += m_size; }
  154. const double y = linearInterpolate( m_buffer[readPos][ch], m_buffer[( readPos + 1 ) % m_size][ch], m_fraction ) + in * m_gain;
  155. ++m_position %= m_size;
  156. m_buffer[m_position][ch] = in;
  157. return y;
  158. }
  159. private:
  160. frame * m_buffer;
  161. int m_size;
  162. int m_position;
  163. double m_gain;
  164. int m_delay;
  165. double m_fraction;
  166. };
  167. template<ch_cnt_t CHANNELS>
  168. class CombFeedbackDualtap
  169. {
  170. typedef double frame[CHANNELS];
  171. CombFeedbackDualtap( int maxDelay ) :
  172. m_size( maxDelay ),
  173. m_position( 0 ),
  174. m_feedBack( 0.0 ),
  175. m_delay( 0 ),
  176. m_fraction( 0.0 )
  177. {
  178. m_buffer = MM_ALLOC<frame>( maxDelay );
  179. memset( m_buffer, 0, sizeof( frame ) * maxDelay );
  180. }
  181. virtual ~CombFeedbackDualtap()
  182. {
  183. MM_FREE( m_buffer );
  184. }
  185. inline void setMaxDelay( int maxDelay )
  186. {
  187. if( maxDelay > m_size )
  188. {
  189. MM_FREE( m_buffer );
  190. m_buffer = MM_ALLOC<frame>( maxDelay );
  191. memset( m_buffer, 0, sizeof( frame ) * maxDelay );
  192. }
  193. m_size = maxDelay;
  194. m_position %= m_size;
  195. }
  196. inline void clearHistory()
  197. {
  198. memset( m_buffer, 0, sizeof( frame ) * m_size );
  199. }
  200. inline void setDelays( double delay1, double delay2 )
  201. {
  202. m_delay1 = static_cast<int>( ceil( delay1 ) );
  203. m_fraction1 = 1.0 - ( delay1 - floor( delay1 ) );
  204. m_delay2 = static_cast<int>( ceil( delay2 ) );
  205. m_fraction2 = 1.0 - ( delay2 - floor( delay2 ) );
  206. }
  207. inline void setGain( double gain )
  208. {
  209. m_gain = gain;
  210. }
  211. inline double update( double in, ch_cnt_t ch )
  212. {
  213. int readPos1 = m_position - m_delay1;
  214. if( readPos1 < 0 ) { readPos1 += m_size; }
  215. int readPos2 = m_position - m_delay2;
  216. if( readPos2 < 0 ) { readPos2 += m_size; }
  217. const double y = linearInterpolate( m_buffer[readPos1][ch], m_buffer[( readPos1 + 1 ) % m_size][ch], m_fraction1 ) +
  218. linearInterpolate( m_buffer[readPos2][ch], m_buffer[( readPos2 + 1 ) % m_size][ch], m_fraction2 );
  219. ++m_position %= m_size;
  220. m_buffer[m_position][ch] = in + m_gain * y;
  221. return y;
  222. }
  223. private:
  224. frame * m_buffer;
  225. int m_size;
  226. int m_position;
  227. double m_gain;
  228. int m_delay1;
  229. int m_delay2;
  230. double m_fraction1;
  231. double m_fraction2;
  232. };
  233. template<ch_cnt_t CHANNELS>
  234. class AllpassDelay
  235. {
  236. public:
  237. typedef double frame[CHANNELS];
  238. AllpassDelay( int maxDelay ) :
  239. m_size( maxDelay ),
  240. m_position( 0 ),
  241. m_feedBack( 0.0 ),
  242. m_delay( 0 ),
  243. m_fraction( 0.0 )
  244. {
  245. m_buffer = MM_ALLOC<frame>( maxDelay );
  246. memset( m_buffer, 0, sizeof( frame ) * maxDelay );
  247. }
  248. virtual ~AllpassDelay()
  249. {
  250. MM_FREE( m_buffer );
  251. }
  252. inline void setMaxDelay( int maxDelay )
  253. {
  254. if( maxDelay > m_size )
  255. {
  256. MM_FREE( m_buffer );
  257. m_buffer = MM_ALLOC<frame>( maxDelay );
  258. memset( m_buffer, 0, sizeof( frame ) * maxDelay );
  259. }
  260. m_size = maxDelay;
  261. m_position %= m_size;
  262. }
  263. inline void clearHistory()
  264. {
  265. memset( m_buffer, 0, sizeof( frame ) * m_size );
  266. }
  267. inline void setDelay( double delay )
  268. {
  269. m_delay = static_cast<int>( ceil( delay ) );
  270. m_fraction = 1.0 - ( delay - floor( delay ) );
  271. }
  272. inline void setGain( double gain )
  273. {
  274. m_gain = gain;
  275. }
  276. inline double update( double in, ch_cnt_t ch )
  277. {
  278. int readPos = m_position - m_delay;
  279. if( readPos < 0 ) { readPos += m_size; }
  280. const double y = linearInterpolate( m_buffer[readPos][ch], m_buffer[( readPos + 1 ) % m_size][ch], m_fraction ) + in * -m_gain;
  281. const double x = in + m_gain * y;
  282. ++m_position %= m_size;
  283. m_buffer[m_position][ch] = x;
  284. return y;
  285. }
  286. private:
  287. frame * m_buffer;
  288. int m_size;
  289. int m_position;
  290. double m_gain;
  291. int m_delay;
  292. double m_fraction;
  293. };
  294. // convenience typedefs for stereo effects
  295. typedef CombFeedback<2> StereoCombFeedback;
  296. typedef CombFeedfwd<2> StereoCombFeedfwd;
  297. typedef CombFeedbackDualtap<2> StereoCombFeedbackDualtap;
  298. typedef AllpassDelay<2> StereoAllpassDelay;
  299. #endif