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.

271 lines
7.2KB

  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. return;
  41. for ( nframes_t i = 0; i < nframes; i++ )
  42. buf_[i] *= g;
  43. }
  44. void
  45. buffer_apply_gain_unaligned ( sample_t * __restrict__ buf, nframes_t nframes, float g )
  46. {
  47. if ( g == 1.0f )
  48. return;
  49. for ( nframes_t i = 0; i < nframes; i++ )
  50. buf[i] *= g;
  51. }
  52. void
  53. buffer_apply_gain_buffer ( sample_t * __restrict__ buf, const sample_t * __restrict__ gainbuf, nframes_t nframes )
  54. {
  55. sample_t * buf_ = (sample_t*) assume_aligned(buf);
  56. const sample_t * gainbuf_ = (const sample_t*) assume_aligned(gainbuf);
  57. for ( nframes_t i = 0; i < nframes; i++ )
  58. buf_[i] *= gainbuf_[i];
  59. }
  60. void
  61. buffer_copy_and_apply_gain_buffer ( sample_t * __restrict__ dst, const sample_t * __restrict__ src, const sample_t * __restrict__ gainbuf, nframes_t nframes )
  62. {
  63. sample_t * dst_ = (sample_t*) assume_aligned(dst);
  64. const sample_t * src_ = (const sample_t*) assume_aligned(src);
  65. const sample_t * gainbuf_ = (const sample_t*) assume_aligned(gainbuf);
  66. for ( nframes_t i = 0; i < nframes; i++ )
  67. dst_[i] = src_[i] * gainbuf_[i];
  68. }
  69. void
  70. buffer_mix ( sample_t * __restrict__ dst, const sample_t * __restrict__ src, nframes_t nframes )
  71. {
  72. sample_t * dst_ = (sample_t*) assume_aligned(dst);
  73. const sample_t * src_ = (const sample_t*) assume_aligned(src);
  74. for ( nframes_t i = 0; i < nframes; i++ )
  75. dst_[i] += src_[i];
  76. }
  77. void
  78. buffer_mix_with_gain ( sample_t * __restrict__ dst, const sample_t * __restrict__ src, nframes_t nframes, float g )
  79. {
  80. sample_t * dst_ = (sample_t*) assume_aligned(dst);
  81. const sample_t * src_ = (const sample_t*) assume_aligned(src);
  82. for ( nframes_t i = 0; i < nframes; i++ )
  83. dst_[i] += src_[i] * g;
  84. }
  85. void
  86. buffer_interleave_one_channel ( sample_t * __restrict__ dst, const sample_t * __restrict__ src, int channel, int channels, nframes_t nframes )
  87. {
  88. dst += channel;
  89. while ( nframes-- )
  90. {
  91. *dst = *(src++);
  92. dst += channels;
  93. }
  94. }
  95. void
  96. buffer_interleave_one_channel_and_mix ( sample_t *__restrict__ dst, const sample_t * __restrict__ src, int channel, int channels, nframes_t nframes )
  97. {
  98. dst += channel;
  99. while ( nframes-- )
  100. {
  101. *dst += *(src++);
  102. dst += channels;
  103. }
  104. }
  105. void
  106. buffer_deinterleave_one_channel ( sample_t * __restrict__ dst, const sample_t * __restrict__ src, int channel, int channels, nframes_t nframes )
  107. {
  108. src += channel;
  109. while ( nframes-- )
  110. {
  111. *(dst++) = *src;
  112. src += channels;
  113. }
  114. }
  115. void
  116. 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 )
  117. {
  118. sample_t * dst_ = (sample_t*) assume_aligned(dst);
  119. const sample_t * src_ = (const sample_t*) assume_aligned(src);
  120. dst_ += dst_channel;
  121. src_ += src_channel;
  122. while ( nframes-- )
  123. {
  124. *dst_ += *src_;
  125. dst_ += dst_channels;
  126. src_ += src_channels;
  127. }
  128. }
  129. void
  130. 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 )
  131. {
  132. sample_t * dst_ = (sample_t*) assume_aligned(dst);
  133. const sample_t * src_ = (const sample_t*) assume_aligned(src);
  134. dst_ += dst_channel;
  135. src_ += src_channel;
  136. while ( nframes-- )
  137. {
  138. *dst_ = *src_;
  139. dst_ += dst_channels;
  140. src_ += src_channels;
  141. }
  142. }
  143. void
  144. buffer_fill_with_silence ( sample_t *buf, nframes_t nframes )
  145. {
  146. memset( buf, 0, nframes * sizeof( sample_t ) );
  147. }
  148. bool
  149. buffer_is_digital_black ( const sample_t *buf, nframes_t nframes )
  150. {
  151. while ( nframes-- )
  152. {
  153. if (! *(buf++) )
  154. continue;
  155. return false;
  156. }
  157. return true;
  158. }
  159. float
  160. buffer_get_peak ( const sample_t * __restrict__ buf, nframes_t nframes )
  161. {
  162. const sample_t * buf_ = (const sample_t*) assume_aligned(buf);
  163. float pmax = 0.0f;
  164. float pmin = 0.0f;
  165. for ( nframes_t i = 0; i < nframes; i++ )
  166. {
  167. pmax = buf_[i] > pmax ? buf_[i] : pmax;
  168. pmin = buf_[i] < pmin ? buf_[i] : pmin;
  169. }
  170. pmax = fabsf(pmax);
  171. pmin = fabsf(pmin);
  172. return pmax > pmin ? pmax : pmin;
  173. }
  174. void
  175. buffer_copy ( sample_t * __restrict__ dst, const sample_t * __restrict__ src, nframes_t nframes )
  176. {
  177. memcpy( dst, src, nframes * sizeof( sample_t ) );
  178. }
  179. void
  180. buffer_copy_and_apply_gain ( sample_t * __restrict__ dst, const sample_t * __restrict__ src, nframes_t nframes, float gain )
  181. {
  182. memcpy( dst, src, nframes * sizeof( sample_t ) );
  183. buffer_apply_gain( dst, nframes, gain );
  184. }
  185. void
  186. Value_Smoothing_Filter::sample_rate ( nframes_t n )
  187. {
  188. const float FS = n;
  189. const float T = 0.05f;
  190. w = _cutoff / (FS * T);
  191. }
  192. bool
  193. Value_Smoothing_Filter::apply( sample_t * __restrict__ dst, nframes_t nframes, float gt )
  194. {
  195. sample_t * dst_ = (sample_t*) assume_aligned(dst);
  196. const float a = 0.07f;
  197. const float b = 1 + a;
  198. const float gm = b * gt;
  199. float g1 = this->g1;
  200. float g2 = this->g2;
  201. if ( target_reached(gt) )
  202. return false;
  203. for (nframes_t i = 0; i < nframes; i++)
  204. {
  205. g1 += w * (gm - g1 - a * g2);
  206. g2 += w * (g1 - g2);
  207. dst_[i] = g2;
  208. }
  209. if ( fabsf( gt - g2 ) < 0.0001f )
  210. g2 = gt;
  211. this->g1 = g1;
  212. this->g2 = g2;
  213. return true;
  214. }