2 ZynAddSubFX - a software synthesizer
4 SVFilter.C - Several state-variable filters
5 Copyright (C) 2002-2005 Nasca Octavian Paul
6 Author: Nasca Octavian Paul
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of version 2 of the GNU General Public License
10 as published by the Free Software Foundation.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License (version 2) for more details.
17 You should have received a copy of the GNU General Public License (version 2)
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "filter_base.h"
29 #include "sv_filter.h"
31 void SVFilter::init(float sample_rate
, int type
, REALTYPE Ffreq
, REALTYPE Fq
, unsigned char Fstages
, float gain
)
33 m_sample_rate
= sample_rate
;
40 needsinterpolation
= 0;
43 if (stages
>= MAX_FILTER_STAGES
)
45 stages
= MAX_FILTER_STAGES
;
49 setfreq_and_q(Ffreq
,Fq
);
51 m_outgain
= dB2rap(gain
);
54 m_outgain
= sqrt(m_outgain
);
58 void SVFilter::cleanup()
62 for (i
= 0 ; i
< MAX_FILTER_STAGES
+ 1 ; i
++)
75 void SVFilter::computefiltercoefs()
77 par
.f
= freq
/ m_sample_rate
* 4.0;
84 par
.q
= 1.0 - atan(sqrt(q
)) * 2.0 / PI
;
85 par
.q
= pow(par
.q
, 1.0 / (stages
+ 1));
86 par
.q_sqrt
= sqrt(par
.q
);
89 void SVFilter::setfreq(REALTYPE frequency
)
99 rap
= freq
/ frequency
;
105 oldabovenq
= abovenq
;
106 abovenq
= frequency
> (m_sample_rate
/ 2 - 500.0);
108 nyquistthresh
= (abovenq
^ oldabovenq
);
110 if (rap
> 3.0 || nyquistthresh
!= 0)
112 // if the frequency is changed fast, it needs interpolation (now, filter and coeficients backup)
115 needsinterpolation
= 1;
123 computefiltercoefs();
128 void SVFilter::setfreq_and_q(REALTYPE frequency
,REALTYPE q_
)
135 void SVFilter::setq(REALTYPE q_
)
138 computefiltercoefs();
141 void SVFilter::settype(int type
)
145 computefiltercoefs();
148 void SVFilter::setgain(REALTYPE dBgain
)
150 m_gain
= dB2rap(dBgain
);
151 computefiltercoefs();
154 void SVFilter::setstages(int stages_
)
156 if (stages_
>= MAX_FILTER_STAGES
)
158 stages_
= MAX_FILTER_STAGES
- 1;
165 computefiltercoefs();
168 void SVFilter::singlefilterout(REALTYPE
*smp
,fstage
&x
,parameters
&par
)
175 case ZYN_FILTER_SV_TYPE_LOWPASS
:
178 case ZYN_FILTER_SV_TYPE_HIGHPASS
:
181 case ZYN_FILTER_SV_TYPE_BANDPASS
:
184 case ZYN_FILTER_SV_TYPE_NOTCH
:
192 for (i
= 0 ; i
< SOUND_BUFFER_SIZE
; i
++)
194 x
.low
= x
.low
+ par
.f
* x
.band
;
195 x
.high
= par
.q_sqrt
* smp
[i
] - x
.low
- par
.q
* x
.band
;
196 x
.band
= par
.f
* x
.high
+ x
.band
;
197 x
.notch
= x
.high
+ x
.low
;
203 void SVFilter::filterout(REALTYPE
*smp
)
208 if (needsinterpolation
!= 0)
210 for (i
= 0 ; i
< SOUND_BUFFER_SIZE
; i
++)
215 for (i
=0 ; i
< stages
+ 1 ; i
++)
217 singlefilterout(m_ismp
, st
[i
], ipar
);
221 for (i
= 0 ; i
< stages
+ 1 ; i
++)
223 singlefilterout(smp
, st
[i
], par
);
226 if (needsinterpolation
!= 0)
228 for (i
= 0 ; i
< SOUND_BUFFER_SIZE
; i
++)
230 x
= i
/ (REALTYPE
)SOUND_BUFFER_SIZE
;
231 smp
[i
] = m_ismp
[i
] * (1.0 - x
) + smp
[i
] * x
;
234 needsinterpolation
= 0;
237 for (i
= 0 ; i
< SOUND_BUFFER_SIZE
; i
++)