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.

222 lines
6.1KB

  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. sample_t *
  24. buffer_alloc ( nframes_t size )
  25. {
  26. void *p;
  27. posix_memalign( &p, ALIGNMENT, size * sizeof( sample_t ) );
  28. return (sample_t*)p;
  29. }
  30. void
  31. buffer_apply_gain ( sample_t * __restrict__ buf, nframes_t nframes, float g )
  32. {
  33. sample_t * buf_ = (sample_t*) __builtin_assume_aligned(buf,ALIGNMENT);
  34. if ( g != 1.0f )
  35. while ( nframes-- )
  36. *(buf_++) *= g;
  37. }
  38. void
  39. buffer_apply_gain_unaligned ( sample_t * __restrict__ buf, nframes_t nframes, float g )
  40. {
  41. if ( g != 1.0f )
  42. while ( nframes-- )
  43. *(buf++) *= g;
  44. }
  45. void
  46. buffer_apply_gain_buffer ( sample_t * __restrict__ buf, const sample_t * __restrict__ gainbuf, nframes_t nframes )
  47. {
  48. sample_t * buf_ = (sample_t*) __builtin_assume_aligned(buf,ALIGNMENT);
  49. const sample_t * gainbuf_ = (const sample_t*) __builtin_assume_aligned(gainbuf,ALIGNMENT);
  50. while ( nframes-- )
  51. *(buf_++) *= *(gainbuf_++);
  52. }
  53. void
  54. buffer_copy_and_apply_gain_buffer ( sample_t * __restrict__ dst, const sample_t * __restrict__ src, const sample_t * __restrict__ gainbuf, nframes_t nframes )
  55. {
  56. sample_t * dst_ = (sample_t*) __builtin_assume_aligned(dst,ALIGNMENT);
  57. const sample_t * src_ = (const sample_t*) __builtin_assume_aligned(src,ALIGNMENT);
  58. const sample_t * gainbuf_ = (const sample_t*) __builtin_assume_aligned(gainbuf,ALIGNMENT);
  59. while ( nframes-- )
  60. *(dst_++) = *(src_++) * *(gainbuf_++);
  61. }
  62. void
  63. buffer_mix ( sample_t * __restrict__ dst, const sample_t * __restrict__ src, nframes_t nframes )
  64. {
  65. sample_t * dst_ = (sample_t*) __builtin_assume_aligned(dst,ALIGNMENT);
  66. const sample_t * src_ = (const sample_t*) __builtin_assume_aligned(src,ALIGNMENT);
  67. while ( nframes-- )
  68. *(dst_++) += *(src_++);
  69. }
  70. void
  71. buffer_mix_with_gain ( sample_t * __restrict__ dst, const sample_t * __restrict__ src, nframes_t nframes, float g )
  72. {
  73. sample_t * dst_ = (sample_t*) __builtin_assume_aligned(dst,ALIGNMENT);
  74. const sample_t * src_ = (const sample_t*) __builtin_assume_aligned(src,ALIGNMENT);
  75. while ( nframes-- )
  76. *(dst_++) += *(src_++) * g;
  77. }
  78. void
  79. buffer_interleave_one_channel ( sample_t * __restrict__ dst, const sample_t * __restrict__ src, int channel, int channels, nframes_t nframes )
  80. {
  81. dst += channel;
  82. while ( nframes-- )
  83. {
  84. *dst = *(src++);
  85. dst += channels;
  86. }
  87. }
  88. void
  89. buffer_interleave_one_channel_and_mix ( sample_t *__restrict__ dst, const sample_t * __restrict__ src, int channel, int channels, nframes_t nframes )
  90. {
  91. dst += channel;
  92. while ( nframes-- )
  93. {
  94. *dst += *(src++);
  95. dst += channels;
  96. }
  97. }
  98. void
  99. buffer_deinterleave_one_channel ( sample_t * __restrict__ dst, const sample_t * __restrict__ src, int channel, int channels, nframes_t nframes )
  100. {
  101. src += channel;
  102. while ( nframes-- )
  103. {
  104. *(dst++) = *src;
  105. src += channels;
  106. }
  107. }
  108. void
  109. buffer_fill_with_silence ( sample_t *buf, nframes_t nframes )
  110. {
  111. memset( buf, 0, nframes * sizeof( sample_t ) );
  112. }
  113. bool
  114. buffer_is_digital_black ( sample_t *buf, nframes_t nframes )
  115. {
  116. while ( nframes-- )
  117. {
  118. if ( 0 != buf[nframes] )
  119. return false;
  120. }
  121. return true;
  122. }
  123. float
  124. buffer_get_peak ( const sample_t * __restrict__ buf, nframes_t nframes )
  125. {
  126. const sample_t * buf_ = (const sample_t*) __builtin_assume_aligned(buf,ALIGNMENT);
  127. float p = 0.0f;
  128. while ( nframes-- )
  129. {
  130. const float s = fabs(*(buf_++));
  131. p = s > p ? s : p;
  132. }
  133. return p;
  134. }
  135. void
  136. buffer_copy ( sample_t * __restrict__ dst, const sample_t * __restrict__ src, nframes_t nframes )
  137. {
  138. memcpy( dst, src, nframes * sizeof( sample_t ) );
  139. }
  140. void
  141. buffer_copy_and_apply_gain ( sample_t * __restrict__ dst, const sample_t * __restrict__ src, nframes_t nframes, float gain )
  142. {
  143. memcpy( dst, src, nframes * sizeof( sample_t ) );
  144. buffer_apply_gain( dst, nframes, gain );
  145. }
  146. void
  147. Value_Smoothing_Filter::sample_rate ( nframes_t n )
  148. {
  149. const float FS = n;
  150. const float T = 0.05f;
  151. w = _cutoff / (FS * T);
  152. }
  153. bool
  154. Value_Smoothing_Filter::apply( sample_t * __restrict__ dst, nframes_t nframes, float gt )
  155. {
  156. sample_t * dst_ = (sample_t*) __builtin_assume_aligned(dst,ALIGNMENT);
  157. const float a = 0.07f;
  158. const float b = 1 + a;
  159. const float gm = b * gt;
  160. float g1 = this->g1;
  161. float g2 = this->g2;
  162. if ( target_reached(gt) )
  163. return false;
  164. for (nframes_t i = 0; i < nframes; i++)
  165. {
  166. g1 += w * (gm - g1 - a * g2);
  167. g2 += w * (g1 - g2);
  168. dst_[i] = g2;
  169. }
  170. if ( fabsf( gt - g2 ) < 0.0001f )
  171. g2 = gt;
  172. this->g1 = g1;
  173. this->g2 = g2;
  174. return true;
  175. }