Expose voice detune parameter of the lv2 plugin
[zyn.git] / lfo.cpp
blob1c4ed63de72e438ae34c554e329c7b025c966d01
1 /*
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
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <math.h>
27 #include <assert.h>
29 #include "globals.h"
30 #include "lfo_parameters.h"
31 #include "lfo.h"
33 LFO::LFO()
37 LFO::~LFO()
41 void
42 LFO::init(
43 float sample_rate,
44 float base_frequency, // note
45 const struct zyn_lfo_parameters * parameters_ptr,
46 unsigned int type)
48 float lfostretch;
49 float lfofreq;
51 m_sample_rate = sample_rate;
53 // max 2x/octave
54 lfostretch = pow(base_frequency / 440.0, parameters_ptr->stretch);
56 lfofreq = pow(2, parameters_ptr->frequency * 10.0);
57 lfofreq -= 1.0;
58 lfofreq /= 12.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)
68 m_incx = 0.499999999;
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;
85 else
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();
93 else
95 m_amp1 = 1;
96 m_amp2 = 1;
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;
107 switch (type)
109 case ZYN_LFO_TYPE_AMPLITUDE:
110 m_lfointensity = parameters_ptr->depth;
111 break;
113 case ZYN_LFO_TYPE_FILTER: // in octave
114 m_lfointensity = parameters_ptr->depth * 4.0;
115 break;
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
120 break;
122 default:
123 assert(0);
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
131 computenextincrnd();
132 computenextincrnd();
136 * LFO out
138 float
139 LFO::lfoout()
141 float out;
142 float tmp;
144 switch (m_shape)
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))
151 out = 4.0 * m_x;
153 else if ((m_x > 0.25) && (m_x < 0.75))
155 out = 2 - 4 * m_x;
157 else
159 out = 4.0 * m_x - 4.0;
162 break;
164 case ZYN_LFO_SHAPE_TYPE_SQUARE:
165 if (m_x < 0.5)
167 out =- 1;
169 else
171 out = 1;
174 break;
176 case ZYN_LFO_SHAPE_TYPE_RAMP_UP:
177 out = (m_x - 0.5) * 2.0;
178 break;
180 case ZYN_LFO_SHAPE_TYPE_RAMP_DOWN:
181 out = (0.5 - m_x) * 2.0;
182 break;
184 case ZYN_LFO_SHAPE_TYPE_EXP_DOWN_1:
185 out = pow(0.05, m_x) * 2.0 - 1.0;
186 break;
188 case ZYN_LFO_SHAPE_TYPE_EXP_DOWN_2:
189 out = pow(0.001, m_x) * 2.0 - 1.0;
190 break;
192 default:
193 assert(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));
201 else
203 out *= m_lfointensity * m_amp2;
206 if (m_delay < 0.00001)
208 if (m_frequency_randomness_enabled == 0)
210 m_x += m_incx;
212 else
214 tmp = (m_incrnd * (1.0 - m_x) + m_nextincrnd * m_x);
215 if (tmp > 1.0)
217 tmp = 1.0;
219 else if (tmp < 0.0)
221 tmp = 0.0;
224 m_x += m_incx * tmp;
227 if (m_x >= 1)
229 m_x = fmod(m_x, 1.0);
230 m_amp1 = m_amp2;
232 if (m_depth_randomness_enabled)
234 m_amp2 = (1 - m_depth_randomness) + m_depth_randomness * zyn_random();
236 else
238 m_amp2 = 1;
241 computenextincrnd();
244 else
246 m_delay -= (float)SOUND_BUFFER_SIZE / m_sample_rate;
249 return out;
253 * LFO out (for amplitude)
255 float
256 LFO::amplfoout()
258 REALTYPE out;
260 out = 1.0 - m_lfointensity + lfoout();
262 if (out < -1.0)
264 out = -1.0;
266 else if (out > 1.0)
268 out = 1.0;
271 return out;
274 void
275 LFO::computenextincrnd()
277 if (!m_frequency_randomness_enabled)
279 return;
282 m_incrnd = m_nextincrnd;
284 m_nextincrnd = pow(0.5, m_frequency_randomness) + zyn_random() * (pow(2.0, m_frequency_randomness) - 1.0);