1 /*
2 Copyright (c) 2009-2010 Tero Lindeman (kometbomb)
3 
4 Permission is hereby granted, free of charge, to any person
5 obtaining a copy of this software and associated documentation
6 files (the "Software"), to deal in the Software without
7 restriction, including without limitation the rights to use,
8 copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the
10 Software is furnished to do so, subject to the following
11 conditions:
12 
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 OTHER DEALINGS IN THE SOFTWARE.
24 */
25 
26 
27 #include "cydflt.h"
28 #include "macros.h"
29 #include <assert.h>
30 
cydflt_set_coeff(CydFilter * flt,Uint16 frequency,Uint16 resonance)31 void cydflt_set_coeff(CydFilter *flt, Uint16 frequency, Uint16 resonance)
32 {
33 	flt->q = 2048 - frequency;
34 	flt->p = frequency + ((Sint32)(0.8f * 2048.0f) * frequency / 2048 * flt->q) / 2048;
35 	flt->f = flt->p + flt->p - 2048;
36 	flt->q = resonance;
37 }
38 
39 
cydflt_cycle(CydFilter * flt,Sint32 input)40 void cydflt_cycle(CydFilter *flt, Sint32 input)
41 {
42 	input -= flt->q * flt->b4 / 2048;                          //feedback
43 	Sint32 t1 = flt->b1;
44 	flt->b1 = (input + flt->b0) * flt->p / 2048- flt->b1 * flt->f / 2048;
45 	Sint32 t2 = flt->b2;
46 	flt->b2 = (flt->b1 + t1) * flt->p / 2048 - flt->b2 * flt->f / 2048;
47 	t1 = flt->b3;
48 	flt->b3 = (flt->b2 + t2) * flt->p / 2048 - flt->b3 * flt->f / 2048;
49 	flt->b4 = (flt->b3 + t1) * flt->p / 2048 - flt->b4 * flt->f / 2048;
50 	flt->b4 = my_min(32767, my_max(-32768, flt->b4));    //clipping
51 
52 	flt->b0 = input;
53 }
54 
55 
cydflt_output_lp(CydFilter * flt)56 Sint32 cydflt_output_lp(CydFilter *flt)
57 {
58 	return flt->b4;
59 }
60 
61 
cydflt_output_hp(CydFilter * flt)62 Sint32 cydflt_output_hp(CydFilter *flt)
63 {
64 	return flt->b0 - flt->b4;
65 }
66 
67 
cydflt_output_bp(CydFilter * flt)68 Sint32 cydflt_output_bp(CydFilter *flt)
69 {
70 	return 3 * (flt->b3 - flt->b4);
71 }
72