1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
/*
ZynAddSubFX - a software synthesizer
LFO.cpp - LFO class implementation
Copyright (C) 2006,2007,2008,2009 Nedko Arnaudov <nedko@arnaudov.name>
Copyright (C) 2002-2005 Nasca Octavian Paul
Author: Nasca Octavian Paul
This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License (version 2) for more details.
You should have received a copy of the GNU General Public License (version 2)
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <assert.h>
#include "globals.h"
#include "lfo_parameters.h"
#include "lfo.h"
LFO::LFO()
{
}
LFO::~LFO()
{
}
void
LFO::init(
float sample_rate,
float base_frequency, // note
const struct zyn_lfo_parameters * parameters_ptr,
unsigned int type)
{
float lfostretch;
float lfofreq;
m_sample_rate = sample_rate;
// max 2x/octave
lfostretch = pow(base_frequency / 440.0, parameters_ptr->stretch);
lfofreq = pow(2, parameters_ptr->frequency * 10.0);
lfofreq -= 1.0;
lfofreq /= 12.0;
lfofreq *= lfostretch;
m_incx = fabs(lfofreq) * (float)SOUND_BUFFER_SIZE / sample_rate;
m_x = parameters_ptr->random_start_phase ? zyn_random() : parameters_ptr->start_phase;
// Limit the Frequency(or else...)
if (m_incx > 0.49999999)
{
m_incx = 0.499999999;
}
m_depth_randomness_enabled = parameters_ptr->depth_randomness_enabled;
if (m_depth_randomness_enabled)
{
if (parameters_ptr->depth_randomness < 0.0)
{
assert(0); // this should be checked by caller
m_depth_randomness = 0.0;
}
else if (parameters_ptr->depth_randomness > 1.0)
{
assert(0); // this should be checked by caller
m_depth_randomness = 1.0;
}
else
{
m_depth_randomness = parameters_ptr->depth_randomness;
}
m_amp1 = (1 - m_depth_randomness) + m_depth_randomness * zyn_random();
m_amp2 = (1 - m_depth_randomness) + m_depth_randomness * zyn_random();
}
else
{
m_amp1 = 1;
m_amp2 = 1;
}
m_frequency_randomness_enabled = parameters_ptr->frequency_randomness_enabled;
if (m_frequency_randomness_enabled)
{
// m_frequency_randomness = pow(parameters_ptr->frequency_randomness, 2.0) * 2.0 * 4.0;
m_frequency_randomness = pow(parameters_ptr->frequency_randomness, 2.0) * 4.0;
}
switch (type)
{
case ZYN_LFO_TYPE_AMPLITUDE:
m_lfointensity = parameters_ptr->depth;
break;
case ZYN_LFO_TYPE_FILTER: // in octave
m_lfointensity = parameters_ptr->depth * 4.0;
break;
case ZYN_LFO_TYPE_FREQUENCY: // in centi
m_lfointensity = pow(2, parameters_ptr->depth * 11.0) - 1.0;
m_x -= 0.25; // chance the starting phase
break;
default:
assert(0);
}
m_shape = parameters_ptr->shape;
m_delay = parameters_ptr->delay;
m_incrnd = m_nextincrnd = 1.0;
// twice because I want incrnd & nextincrnd to be random
computenextincrnd();
computenextincrnd();
}
/*
* LFO out
*/
float
LFO::lfoout()
{
float out;
float tmp;
switch (m_shape)
{
case ZYN_LFO_SHAPE_TYPE_SINE:
out = cos(m_x * 2.0 * PI);
case ZYN_LFO_SHAPE_TYPE_TRIANGLE:
if ((m_x >= 0.0) && (m_x < 0.25))
{
out = 4.0 * m_x;
}
else if ((m_x > 0.25) && (m_x < 0.75))
{
out = 2 - 4 * m_x;
}
else
{
out = 4.0 * m_x - 4.0;
}
break;
case ZYN_LFO_SHAPE_TYPE_SQUARE:
if (m_x < 0.5)
{
out =- 1;
}
else
{
out = 1;
}
break;
case ZYN_LFO_SHAPE_TYPE_RAMP_UP:
out = (m_x - 0.5) * 2.0;
break;
case ZYN_LFO_SHAPE_TYPE_RAMP_DOWN:
out = (0.5 - m_x) * 2.0;
break;
case ZYN_LFO_SHAPE_TYPE_EXP_DOWN_1:
out = pow(0.05, m_x) * 2.0 - 1.0;
break;
case ZYN_LFO_SHAPE_TYPE_EXP_DOWN_2:
out = pow(0.001, m_x) * 2.0 - 1.0;
break;
default:
assert(0);
};
if ((m_shape == ZYN_LFO_SHAPE_TYPE_SINE) ||
(m_shape == ZYN_LFO_SHAPE_TYPE_TRIANGLE))
{
out *= m_lfointensity * (m_amp1 + m_x * (m_amp2 - m_amp1));
}
else
{
out *= m_lfointensity * m_amp2;
}
if (m_delay < 0.00001)
{
if (m_frequency_randomness_enabled == 0)
{
m_x += m_incx;
}
else
{
tmp = (m_incrnd * (1.0 - m_x) + m_nextincrnd * m_x);
if (tmp > 1.0)
{
tmp = 1.0;
}
else if (tmp < 0.0)
{
tmp = 0.0;
}
m_x += m_incx * tmp;
}
if (m_x >= 1)
{
m_x = fmod(m_x, 1.0);
m_amp1 = m_amp2;
if (m_depth_randomness_enabled)
{
m_amp2 = (1 - m_depth_randomness) + m_depth_randomness * zyn_random();
}
else
{
m_amp2 = 1;
}
computenextincrnd();
}
}
else
{
m_delay -= (float)SOUND_BUFFER_SIZE / m_sample_rate;
}
return out;
}
/*
* LFO out (for amplitude)
*/
float
LFO::amplfoout()
{
REALTYPE out;
out = 1.0 - m_lfointensity + lfoout();
if (out < -1.0)
{
out = -1.0;
}
else if (out > 1.0)
{
out = 1.0;
}
return out;
}
void
LFO::computenextincrnd()
{
if (!m_frequency_randomness_enabled)
{
return;
}
m_incrnd = m_nextincrnd;
m_nextincrnd = pow(0.5, m_frequency_randomness) + zyn_random() * (pow(2.0, m_frequency_randomness) - 1.0);
}
|