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.

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