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.

136 lines
2.7KB

  1. /*
  2. Copyright (C) 1999 Juhana Sadeharju
  3. kouhia at nic.funet.fi
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include <stdio.h>
  17. #include <math.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include "../include/gverbdsp.h"
  21. namespace rack_plugin_rcm {
  22. #define TRUE 1
  23. #define FALSE 0
  24. ty_diffuser *diffuser_make(int size, float coeff)
  25. {
  26. ty_diffuser *p;
  27. int i;
  28. p = (ty_diffuser *)malloc(sizeof(ty_diffuser));
  29. p->size = size;
  30. p->coeff = coeff;
  31. p->idx = 0;
  32. p->buf = (float *)malloc(size*sizeof(float));
  33. for (i = 0; i < size; i++) p->buf[i] = 0.0;
  34. return(p);
  35. }
  36. void diffuser_free(ty_diffuser *p)
  37. {
  38. free(p->buf);
  39. free(p);
  40. }
  41. void diffuser_flush(ty_diffuser *p)
  42. {
  43. memset(p->buf, 0, p->size * sizeof(float));
  44. }
  45. ty_damper *damper_make(float damping)
  46. {
  47. ty_damper *p;
  48. p = (ty_damper *)malloc(sizeof(ty_damper));
  49. p->damping = damping;
  50. p->delay = 0.0f;
  51. return(p);
  52. }
  53. void damper_free(ty_damper *p)
  54. {
  55. free(p);
  56. }
  57. void damper_flush(ty_damper *p)
  58. {
  59. p->delay = 0.0f;
  60. }
  61. ty_fixeddelay *fixeddelay_make(int size)
  62. {
  63. ty_fixeddelay *p;
  64. int i;
  65. p = (ty_fixeddelay *)malloc(sizeof(ty_fixeddelay));
  66. p->size = size;
  67. p->idx = 0;
  68. p->buf = (float *)malloc(size*sizeof(float));
  69. for (i = 0; i < size; i++) p->buf[i] = 0.0;
  70. return(p);
  71. }
  72. void fixeddelay_free(ty_fixeddelay *p)
  73. {
  74. free(p->buf);
  75. free(p);
  76. }
  77. void fixeddelay_flush(ty_fixeddelay *p)
  78. {
  79. memset(p->buf, 0, p->size * sizeof(float));
  80. }
  81. int isprime(int n)
  82. {
  83. unsigned int i;
  84. const unsigned int lim = (int)sqrtf((float)n);
  85. if (n == 2) return(TRUE);
  86. if ((n & 1) == 0) return(FALSE);
  87. for(i = 3; i <= lim; i += 2)
  88. if ((n % i) == 0) return(FALSE);
  89. return(TRUE);
  90. }
  91. int nearest_prime(int n, float rerror)
  92. /* relative error; new prime will be in range
  93. * [n-n*rerror, n+n*rerror];
  94. */
  95. {
  96. int bound,k;
  97. if (isprime(n)) return(n);
  98. /* assume n is large enough and n*rerror enough smaller than n */
  99. bound = n*rerror;
  100. for(k = 1; k <= bound; k++) {
  101. if (isprime(n+k)) return(n+k);
  102. if (isprime(n-k)) return(n-k);
  103. }
  104. return(-1);
  105. }
  106. } // namespace rack_plugin_rcm