2 ZynAddSubFX - a software synthesizer
4 Resonance.C - Resonance
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
26 #include "resonance.h"
31 struct zyn_resonance
* resonance_ptr
)
33 resonance_ptr
->enabled
= false;
34 resonance_ptr
->maxdB
= 20;
35 resonance_ptr
->centerfreq
= 64; // 1 kHz
36 resonance_ptr
->octavesfreq
= 64;
37 resonance_ptr
->protectthefundamental
= 0;
38 resonance_ptr
->center
= 1.0;
39 resonance_ptr
->bw
= 1.0;
40 for (int i
= 0 ; i
< N_RES_POINTS
; i
++)
42 resonance_ptr
->points
[i
] = 64;
47 * Get the center frequency of the resonance graph
50 zyn_resonance_get_center_freq(
51 struct zyn_resonance
* resonance_ptr
)
53 return 10000.0 * pow(10, -(1.0 - resonance_ptr
->centerfreq
/ 127.0) * 2.0);
57 * Get the number of octave that the resonance functions applies to
60 zyn_resonance_get_octaves_freq(
61 struct zyn_resonance
* resonance_ptr
)
63 return 0.25 + 10.0 * resonance_ptr
->octavesfreq
/ 127.0;
67 * Get the frequency from x, where x is [0..1]; x is the x coordinate
70 zyn_resonance_get_freq_x(
71 struct zyn_resonance
* resonance_ptr
,
81 octf
= pow(2.0, zyn_resonance_get_octaves_freq(resonance_ptr
));
83 return zyn_resonance_get_center_freq(resonance_ptr
) / sqrt(octf
) * pow(octf
,x
);
87 * Apply the resonance to FFT data
91 struct zyn_resonance
* resonance_ptr
,
93 struct zyn_fft_freqs
* fftdata_ptr
,
106 // if the resonance is disabled
107 if (!resonance_ptr
->enabled
)
113 l1
= log(zyn_resonance_get_freq_x(resonance_ptr
, 0.0) * resonance_ptr
->center
);
114 l2
= log(2.0) * zyn_resonance_get_octaves_freq(resonance_ptr
) * resonance_ptr
->bw
;
116 for (i
= 0 ; i
< N_RES_POINTS
; i
++)
118 if (sum
< resonance_ptr
->points
[i
])
120 sum
= resonance_ptr
->points
[i
];
129 for (i
= 1 ; i
< n
; i
++)
131 x
= (log(freq
* i
) - l1
) / l2
; // compute where the n-th hamonics fits to the graph
142 if (kx1
>= N_RES_POINTS
)
144 kx1
= N_RES_POINTS
- 1;
148 if (kx2
>= N_RES_POINTS
)
150 kx2
= N_RES_POINTS
- 1;
153 y
= (resonance_ptr
->points
[kx1
] * (1.0 - dx
) + resonance_ptr
->points
[kx2
] * dx
) / 127.0 - sum
/ 127.0;
155 y
= pow(10.0, y
* resonance_ptr
->maxdB
/ 20.0);
157 if (resonance_ptr
->protectthefundamental
!= 0 && i
== 1)
162 fftdata_ptr
->c
[i
] *= y
;
163 fftdata_ptr
->s
[i
] *= y
;
170 void Resonance::set_center(float center
)
172 ctlcenter
= pow(3.0, center
);
176 void Resonance::set_badnwidth(float bandwidth
)
178 ctlbw
= pow(1.5, bandwidth
* 0.5);
182 * Set a point of resonance function with a value
184 void Resonance::setpoint(int n
,unsigned char p
){
185 if ((n
<0)||(n
>=N_RES_POINTS
)) return;
190 * Gets the response at the frequency "freq"
193 REALTYPE
Resonance::getfreqresponse(REALTYPE freq
){
194 REALTYPE l1
=log(getfreqx(0.0)*ctlcenter
),
195 l2
=log(2.0)*getoctavesfreq()*ctlbw
,sum
=0.0;
197 for (int i
=0;i
<N_RES_POINTS
;i
++) if (sum
<Prespoints
[i
]) sum
=Prespoints
[i
];
198 if (sum
<1.0) sum
=1.0;
200 REALTYPE x
=(log(freq
)-l1
)/l2
;//compute where the n-th hamonics fits to the graph
203 REALTYPE dx
=x
-floor(x
);x
=floor(x
);
204 int kx1
=(int)x
; if (kx1
>=N_RES_POINTS
) kx1
=N_RES_POINTS
-1;
205 int kx2
=kx1
+1;if (kx2
>=N_RES_POINTS
) kx2
=N_RES_POINTS
-1;
206 REALTYPE result
=(Prespoints
[kx1
]*(1.0-dx
)+Prespoints
[kx2
]*dx
)/127.0-sum
/127.0;
207 result
=pow(10.0,result
*PmaxdB
/20.0);
213 * Smooth the resonance function
215 void Resonance::smooth(){
216 REALTYPE old
=Prespoints
[0];
217 for (int i
=0;i
<N_RES_POINTS
;i
++){
218 old
=old
*0.4+Prespoints
[i
]*0.6;
219 Prespoints
[i
]=(int) old
;
221 old
=Prespoints
[N_RES_POINTS
-1];
222 for (int i
=N_RES_POINTS
-1;i
>0;i
--){
223 old
=old
*0.4+Prespoints
[i
]*0.6;
224 Prespoints
[i
]=(int) old
+1;
225 if (Prespoints
[i
]>127) Prespoints
[i
]=127;
230 * Randomize the resonance function
232 void Resonance::randomize(int type
)
237 r
= (int)(zyn_random() * 127.0);
239 for (i
= 0 ; i
< N_RES_POINTS
; i
++)
242 if ((zyn_random() < 0.1) && (type
== 0))
244 r
= (int)(zyn_random() * 127.0);
247 if ((zyn_random() < 0.3) && (type
== 1))
249 r
= (int)(zyn_random() * 127.0);
254 r
= (int)(zyn_random() * 127.0);
262 * Interpolate the peaks
264 void Resonance::interpolatepeaks(int type
){
265 int x1
=0,y1
=Prespoints
[0];
266 for (int i
=1;i
<N_RES_POINTS
;i
++){
267 if ((Prespoints
[i
]!=64)||(i
+1==N_RES_POINTS
)){
268 int y2
=Prespoints
[i
];
269 for (int k
=0;k
<i
-x1
;k
++){
270 float x
=(float) k
/(i
-x1
);
271 if (type
==0) x
=(1-cos(x
*PI
))*0.5;
272 Prespoints
[x1
+k
]=(int)(y1
*(1.0-x
)+y2
*x
);
281 * Get the x coordinate from frequency (used by the UI)
283 REALTYPE
Resonance::getfreqpos(REALTYPE freq
){
284 return((log(freq
)-log(getfreqx(0.0)))/log(2.0)/getoctavesfreq());