Assists music production by grouping standalone programs into sessions. Community version of "Non Session Manager".
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.

261 lines
6.9KB

  1. /*******************************************************************************/
  2. /* Copyright (C) 2008 Jonathan Moore Liles */
  3. /* */
  4. /* This program is free software; you can redistribute it and/or modify it */
  5. /* under the terms of the GNU General Public License as published by the */
  6. /* Free Software Foundation; either version 2 of the License, or (at your */
  7. /* option) any later version. */
  8. /* */
  9. /* This program is distributed in the hope that it will be useful, but WITHOUT */
  10. /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
  11. /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
  12. /* more details. */
  13. /* */
  14. /* You should have received a copy of the GNU General Public License along */
  15. /* with This program; see the file COPYING. If not,write to the Free Software */
  16. /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  17. /*******************************************************************************/
  18. /* General DSP related functions. */
  19. #include "dsp.h"
  20. #include "string.h" // for memset.
  21. #include <stdlib.h>
  22. static const int ALIGNMENT = 16;
  23. #ifdef HAS_BUILTIN_ASSUME_ALIGNED
  24. #define assume_aligned(x) __builtin_assume_aligned(x,ALIGNMENT)
  25. #else
  26. #define assume_aligned(x) (x)
  27. #endif
  28. sample_t *
  29. buffer_alloc ( nframes_t size )
  30. {
  31. void *p;
  32. posix_memalign( &p, ALIGNMENT, size * sizeof( sample_t ) );
  33. return (sample_t*)p;
  34. }
  35. void
  36. buffer_apply_gain ( sample_t * __restrict__ buf, nframes_t nframes, float g )
  37. {
  38. sample_t * buf_ = (sample_t*) assume_aligned(buf);
  39. if ( g != 1.0f )
  40. while ( nframes-- )
  41. *(buf_++) *= g;
  42. }
  43. void
  44. buffer_apply_gain_unaligned ( sample_t * __restrict__ buf, nframes_t nframes, float g )
  45. {
  46. if ( g != 1.0f )
  47. while ( nframes-- )
  48. *(buf++) *= g;
  49. }
  50. void
  51. buffer_apply_gain_buffer ( sample_t * __restrict__ buf, const sample_t * __restrict__ gainbuf, nframes_t nframes )
  52. {
  53. sample_t * buf_ = (sample_t*) assume_aligned(buf);
  54. const sample_t * gainbuf_ = (const sample_t*) assume_aligned(gainbuf);
  55. while ( nframes-- )
  56. *(buf_++) *= *(gainbuf_++);
  57. }
  58. void
  59. buffer_copy_and_apply_gain_buffer ( sample_t * __restrict__ dst, const sample_t * __restrict__ src, const sample_t * __restrict__ gainbuf, nframes_t nframes )
  60. {
  61. sample_t * dst_ = (sample_t*) assume_aligned(dst);
  62. const sample_t * src_ = (const sample_t*) assume_aligned(src);
  63. const sample_t * gainbuf_ = (const sample_t*) assume_aligned(gainbuf);
  64. while ( nframes-- )
  65. *(dst_++) = *(src_++) * *(gainbuf_++);
  66. }
  67. void
  68. buffer_mix ( sample_t * __restrict__ dst, const sample_t * __restrict__ src, nframes_t nframes )
  69. {
  70. sample_t * dst_ = (sample_t*) assume_aligned(dst);
  71. const sample_t * src_ = (const sample_t*) assume_aligned(src);
  72. while ( nframes-- )
  73. *(dst_++) += *(src_++);
  74. }
  75. void
  76. buffer_mix_with_gain ( sample_t * __restrict__ dst, const sample_t * __restrict__ src, nframes_t nframes, float g )
  77. {
  78. sample_t * dst_ = (sample_t*) assume_aligned(dst);
  79. const sample_t * src_ = (const sample_t*) assume_aligned(src);
  80. while ( nframes-- )
  81. *(dst_++) += *(src_++) * g;
  82. }
  83. void
  84. buffer_interleave_one_channel ( sample_t * __restrict__ dst, const sample_t * __restrict__ src, int channel, int channels, nframes_t nframes )
  85. {
  86. dst += channel;
  87. while ( nframes-- )
  88. {
  89. *dst = *(src++);
  90. dst += channels;
  91. }
  92. }
  93. void
  94. buffer_interleave_one_channel_and_mix ( sample_t *__restrict__ dst, const sample_t * __restrict__ src, int channel, int channels, nframes_t nframes )
  95. {
  96. dst += channel;
  97. while ( nframes-- )
  98. {
  99. *dst += *(src++);
  100. dst += channels;
  101. }
  102. }
  103. void
  104. buffer_deinterleave_one_channel ( sample_t * __restrict__ dst, const sample_t * __restrict__ src, int channel, int channels, nframes_t nframes )
  105. {
  106. src += channel;
  107. while ( nframes-- )
  108. {
  109. *(dst++) = *src;
  110. src += channels;
  111. }
  112. }
  113. void
  114. buffer_interleaved_mix ( sample_t *__restrict__ dst, const sample_t * __restrict__ src, int dst_channel, int src_channel, int dst_channels, int src_channels, nframes_t nframes )
  115. {
  116. sample_t * dst_ = (sample_t*) assume_aligned(dst);
  117. const sample_t * src_ = (const sample_t*) assume_aligned(src);
  118. dst_ += dst_channel;
  119. src_ += src_channel;
  120. while ( nframes-- )
  121. {
  122. *dst_ += *src_;
  123. dst_ += dst_channels;
  124. src_ += src_channels;
  125. }
  126. }
  127. void
  128. buffer_interleaved_copy ( sample_t *__restrict__ dst, const sample_t * __restrict__ src, int dst_channel, int src_channel, int dst_channels, int src_channels, nframes_t nframes )
  129. {
  130. sample_t * dst_ = (sample_t*) assume_aligned(dst);
  131. const sample_t * src_ = (const sample_t*) assume_aligned(src);
  132. dst_ += dst_channel;
  133. src_ += src_channel;
  134. while ( nframes-- )
  135. {
  136. *dst_ = *src_;
  137. dst_ += dst_channels;
  138. src_ += src_channels;
  139. }
  140. }
  141. void
  142. buffer_fill_with_silence ( sample_t *buf, nframes_t nframes )
  143. {
  144. memset( buf, 0, nframes * sizeof( sample_t ) );
  145. }
  146. bool
  147. buffer_is_digital_black ( sample_t *buf, nframes_t nframes )
  148. {
  149. while ( nframes-- )
  150. {
  151. if ( 0 != buf[nframes] )
  152. return false;
  153. }
  154. return true;
  155. }
  156. float
  157. buffer_get_peak ( const sample_t * __restrict__ buf, nframes_t nframes )
  158. {
  159. const sample_t * buf_ = (const sample_t*) assume_aligned(buf);
  160. float p = 0.0f;
  161. while ( nframes-- )
  162. {
  163. const float s = fabs(*(buf_++));
  164. p = s > p ? s : p;
  165. }
  166. return p;
  167. }
  168. void
  169. buffer_copy ( sample_t * __restrict__ dst, const sample_t * __restrict__ src, nframes_t nframes )
  170. {
  171. memcpy( dst, src, nframes * sizeof( sample_t ) );
  172. }
  173. void
  174. buffer_copy_and_apply_gain ( sample_t * __restrict__ dst, const sample_t * __restrict__ src, nframes_t nframes, float gain )
  175. {
  176. memcpy( dst, src, nframes * sizeof( sample_t ) );
  177. buffer_apply_gain( dst, nframes, gain );
  178. }
  179. void
  180. Value_Smoothing_Filter::sample_rate ( nframes_t n )
  181. {
  182. const float FS = n;
  183. const float T = 0.05f;
  184. w = _cutoff / (FS * T);
  185. }
  186. bool
  187. Value_Smoothing_Filter::apply( sample_t * __restrict__ dst, nframes_t nframes, float gt )
  188. {
  189. sample_t * dst_ = (sample_t*) assume_aligned(dst);
  190. const float a = 0.07f;
  191. const float b = 1 + a;
  192. const float gm = b * gt;
  193. float g1 = this->g1;
  194. float g2 = this->g2;
  195. if ( target_reached(gt) )
  196. return false;
  197. for (nframes_t i = 0; i < nframes; i++)
  198. {
  199. g1 += w * (gm - g1 - a * g2);
  200. g2 += w * (g1 - g2);
  201. dst_[i] = g2;
  202. }
  203. if ( fabsf( gt - g2 ) < 0.0001f )
  204. g2 = gt;
  205. this->g1 = g1;
  206. this->g2 = g2;
  207. return true;
  208. }