1 /*
2   ZynAddSubFX - a software synthesizer
3 
4   Filter.cpp - Filters, uses analog,formant,etc. filters
5   Copyright (C) 2002-2005 Nasca Octavian Paul
6   Author: Nasca Octavian Paul
7 
8   This program is free software; you can redistribute it and/or
9   modify it under the terms of the GNU General Public License
10   as published by the Free Software Foundation; either version 2
11   of the License, or (at your option) any later version.
12 */
13 
14 #include <cmath>
15 #include <cstdio>
16 #include <cassert>
17 
18 #include "Filter.h"
19 #include "AnalogFilter.h"
20 #include "FormantFilter.h"
21 #include "SVFilter.h"
22 #include "MoogFilter.h"
23 #include "CombFilter.h"
24 #include "../Params/FilterParams.h"
25 #include "../Misc/Allocator.h"
26 
27 namespace zyn {
28 
Filter(unsigned int srate,int bufsize)29 Filter::Filter(unsigned int srate, int bufsize)
30     : outgain(1.0f),
31       samplerate(srate),
32       buffersize(bufsize)
33 {
34     alias();
35 }
36 
generate(Allocator & memory,const FilterParams * pars,unsigned int srate,int bufsize)37 Filter *Filter::generate(Allocator &memory, const FilterParams *pars,
38         unsigned int srate, int bufsize)
39 {
40     assert(srate != 0);
41     assert(bufsize != 0);
42 
43     unsigned char Ftype   = pars->Ptype;
44     unsigned char Fstages = pars->Pstages;
45 
46     Filter *filter;
47     switch(pars->Pcategory) {
48         case 1:
49             filter = memory.alloc<FormantFilter>(pars, &memory, srate, bufsize);
50             break;
51         case 2:
52             filter = memory.alloc<SVFilter>(Ftype, 1000.0f, pars->getq(), Fstages, srate, bufsize);
53             filter->outgain = dB2rap(pars->getgain());
54             if(filter->outgain > 1.0f)
55                 filter->outgain = sqrt(filter->outgain);
56             break;
57         case 3:
58             filter = memory.alloc<MoogFilter>(Ftype, 1000.0f, pars->getq(), srate, bufsize);
59             filter->setgain(pars->getgain());
60             break;
61         case 4:
62             filter = memory.alloc<CombFilter>(&memory, Ftype, 1000.0f, pars->getq(), srate, bufsize);
63             filter->outgain = dB2rap(pars->getgain());
64             break;
65         default:
66             filter = memory.alloc<AnalogFilter>(Ftype, 1000.0f, pars->getq(), Fstages, srate, bufsize);
67             if((Ftype >= 6) && (Ftype <= 8))
68                 filter->setgain(pars->getgain());
69             else
70                 filter->outgain = dB2rap(pars->getgain());
71             break;
72     }
73     return filter;
74 }
75 
getrealfreq(float freqpitch)76 float Filter::getrealfreq(float freqpitch)
77 {
78     return powf(2.0f, freqpitch + 9.96578428f); //log2(1000)=9.95748f
79 }
80 
81 }
82