2 ZynAddSubFX - a software synthesizer
4 LFO.cpp - LFO class implementation
5 Copyright (C) 2006,2007,2008,2009 Nedko Arnaudov <nedko@arnaudov.name>
6 Copyright (C) 2002-2005 Nasca Octavian Paul
7 Author: Nasca Octavian Paul
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of version 2 of the GNU General Public License
11 as published by the Free Software Foundation.
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 (version 2) for more details.
18 You should have received a copy of the GNU General Public License (version 2)
19 along with this program; if not, write to the Free Software Foundation,
20 Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "lfo_parameters.h"
44 float base_frequency
, // note
45 const struct zyn_lfo_parameters
* parameters_ptr
,
51 m_sample_rate
= sample_rate
;
54 lfostretch
= pow(base_frequency
/ 440.0, parameters_ptr
->stretch
);
56 lfofreq
= pow(2, parameters_ptr
->frequency
* 10.0);
59 lfofreq
*= lfostretch
;
61 m_incx
= fabs(lfofreq
) * (float)SOUND_BUFFER_SIZE
/ sample_rate
;
63 m_x
= parameters_ptr
->random_start_phase
? zyn_random() : parameters_ptr
->start_phase
;
65 // Limit the Frequency(or else...)
66 if (m_incx
> 0.49999999)
71 m_depth_randomness_enabled
= parameters_ptr
->depth_randomness_enabled
;
73 if (m_depth_randomness_enabled
)
75 if (parameters_ptr
->depth_randomness
< 0.0)
77 assert(0); // this should be checked by caller
78 m_depth_randomness
= 0.0;
80 else if (parameters_ptr
->depth_randomness
> 1.0)
82 assert(0); // this should be checked by caller
83 m_depth_randomness
= 1.0;
87 m_depth_randomness
= parameters_ptr
->depth_randomness
;
90 m_amp1
= (1 - m_depth_randomness
) + m_depth_randomness
* zyn_random();
91 m_amp2
= (1 - m_depth_randomness
) + m_depth_randomness
* zyn_random();
99 m_frequency_randomness_enabled
= parameters_ptr
->frequency_randomness_enabled
;
101 if (m_frequency_randomness_enabled
)
103 // m_frequency_randomness = pow(parameters_ptr->frequency_randomness, 2.0) * 2.0 * 4.0;
104 m_frequency_randomness
= pow(parameters_ptr
->frequency_randomness
, 2.0) * 4.0;
109 case ZYN_LFO_TYPE_AMPLITUDE
:
110 m_lfointensity
= parameters_ptr
->depth
;
113 case ZYN_LFO_TYPE_FILTER
: // in octave
114 m_lfointensity
= parameters_ptr
->depth
* 4.0;
117 case ZYN_LFO_TYPE_FREQUENCY
: // in centi
118 m_lfointensity
= pow(2, parameters_ptr
->depth
* 11.0) - 1.0;
119 m_x
-= 0.25; // chance the starting phase
126 m_shape
= parameters_ptr
->shape
;
127 m_delay
= parameters_ptr
->delay
;
128 m_incrnd
= m_nextincrnd
= 1.0;
130 // twice because I want incrnd & nextincrnd to be random
146 case ZYN_LFO_SHAPE_TYPE_SINE
:
147 out
= cos(m_x
* 2.0 * PI
);
148 case ZYN_LFO_SHAPE_TYPE_TRIANGLE
:
149 if ((m_x
>= 0.0) && (m_x
< 0.25))
153 else if ((m_x
> 0.25) && (m_x
< 0.75))
159 out
= 4.0 * m_x
- 4.0;
164 case ZYN_LFO_SHAPE_TYPE_SQUARE
:
176 case ZYN_LFO_SHAPE_TYPE_RAMP_UP
:
177 out
= (m_x
- 0.5) * 2.0;
180 case ZYN_LFO_SHAPE_TYPE_RAMP_DOWN
:
181 out
= (0.5 - m_x
) * 2.0;
184 case ZYN_LFO_SHAPE_TYPE_EXP_DOWN_1
:
185 out
= pow(0.05, m_x
) * 2.0 - 1.0;
188 case ZYN_LFO_SHAPE_TYPE_EXP_DOWN_2
:
189 out
= pow(0.001, m_x
) * 2.0 - 1.0;
196 if ((m_shape
== ZYN_LFO_SHAPE_TYPE_SINE
) ||
197 (m_shape
== ZYN_LFO_SHAPE_TYPE_TRIANGLE
))
199 out
*= m_lfointensity
* (m_amp1
+ m_x
* (m_amp2
- m_amp1
));
203 out
*= m_lfointensity
* m_amp2
;
206 if (m_delay
< 0.00001)
208 if (m_frequency_randomness_enabled
== 0)
214 tmp
= (m_incrnd
* (1.0 - m_x
) + m_nextincrnd
* m_x
);
229 m_x
= fmod(m_x
, 1.0);
232 if (m_depth_randomness_enabled
)
234 m_amp2
= (1 - m_depth_randomness
) + m_depth_randomness
* zyn_random();
246 m_delay
-= (float)SOUND_BUFFER_SIZE
/ m_sample_rate
;
253 * LFO out (for amplitude)
260 out
= 1.0 - m_lfointensity
+ lfoout();
275 LFO::computenextincrnd()
277 if (!m_frequency_randomness_enabled
)
282 m_incrnd
= m_nextincrnd
;
284 m_nextincrnd
= pow(0.5, m_frequency_randomness
) + zyn_random() * (pow(2.0, m_frequency_randomness
) - 1.0);