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.

228 lines
6.0KB

  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_fill_with_silence ( sample_t *buf, nframes_t nframes )
  115. {
  116. memset( buf, 0, nframes * sizeof( sample_t ) );
  117. }
  118. bool
  119. buffer_is_digital_black ( sample_t *buf, nframes_t nframes )
  120. {
  121. while ( nframes-- )
  122. {
  123. if ( 0 != buf[nframes] )
  124. return false;
  125. }
  126. return true;
  127. }
  128. float
  129. buffer_get_peak ( const sample_t * __restrict__ buf, nframes_t nframes )
  130. {
  131. const sample_t * buf_ = (const sample_t*) assume_aligned(buf);
  132. float p = 0.0f;
  133. while ( nframes-- )
  134. {
  135. const float s = fabs(*(buf_++));
  136. p = s > p ? s : p;
  137. }
  138. return p;
  139. }
  140. void
  141. buffer_copy ( sample_t * __restrict__ dst, const sample_t * __restrict__ src, nframes_t nframes )
  142. {
  143. memcpy( dst, src, nframes * sizeof( sample_t ) );
  144. }
  145. void
  146. buffer_copy_and_apply_gain ( sample_t * __restrict__ dst, const sample_t * __restrict__ src, nframes_t nframes, float gain )
  147. {
  148. memcpy( dst, src, nframes * sizeof( sample_t ) );
  149. buffer_apply_gain( dst, nframes, gain );
  150. }
  151. void
  152. Value_Smoothing_Filter::sample_rate ( nframes_t n )
  153. {
  154. const float FS = n;
  155. const float T = 0.05f;
  156. w = _cutoff / (FS * T);
  157. }
  158. bool
  159. Value_Smoothing_Filter::apply( sample_t * __restrict__ dst, nframes_t nframes, float gt )
  160. {
  161. sample_t * dst_ = (sample_t*) assume_aligned(dst);
  162. const float a = 0.07f;
  163. const float b = 1 + a;
  164. const float gm = b * gt;
  165. float g1 = this->g1;
  166. float g2 = this->g2;
  167. if ( target_reached(gt) )
  168. return false;
  169. for (nframes_t i = 0; i < nframes; i++)
  170. {
  171. g1 += w * (gm - g1 - a * g2);
  172. g2 += w * (g1 - g2);
  173. dst_[i] = g2;
  174. }
  175. if ( fabsf( gt - g2 ) < 0.0001f )
  176. g2 = gt;
  177. this->g1 = g1;
  178. this->g2 = g2;
  179. return true;
  180. }