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