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
|
/* -*- Mode: C ; c-basic-offset: 2 -*- */
/*****************************************************************************
*
* Copyright (C) 2006,2007,2008,2009 Nedko Arnaudov <nedko@arnaudov.name>
* Copyright (C) 2002-2005 Nasca Octavian Paul
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License
*
* 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
*****************************************************************************/
#include <stdbool.h>
#include <math.h>
#include <assert.h>
#include "globals.h"
#include "portamento.h"
#include "addsynth.h"
#include "fft.h"
#include "oscillator.h"
#include "addsynth_component.h"
//#define LOG_LEVEL LOG_LEVEL_DEBUG
#include "log.h"
void
zyn_portamento_init(
struct zyn_portamento * portamento_ptr)
{
portamento_ptr->enabled = false;
portamento_ptr->used = false;
portamento_ptr->time = 0.5;
portamento_ptr->up_down_time_stretch = 0.0;
portamento_ptr->pitch_threshold = 3; /* 3 equally tempered semitones */
portamento_ptr->pitch_threshold_above = true;
}
// returns true if the portamento's conditions are true, else returns false
bool
zyn_portamento_start(
float sample_rate,
struct zyn_portamento * portamento_ptr,
float oldfreq,
float newfreq)
{
float portamentotime;
float tmprap;
float thresholdrap;
portamento_ptr->x = 0.0;
if (portamento_ptr->used)
{
LOG_DEBUG("Not using portamento, already used");
return false;
}
if (!portamento_ptr->enabled)
{
LOG_DEBUG("Not using portamento, disabled");
return false;
}
portamentotime = powf(100.0, portamento_ptr->time) / 50.0; // portamento time in seconds
if (portamento_ptr->up_down_time_stretch >= 0.0 &&
newfreq < oldfreq)
{
if (portamento_ptr->up_down_time_stretch == 1.0)
{
LOG_DEBUG("Not using portamento down portamento because of time stretch value");
return false;
}
portamentotime *= pow(0.1, portamento_ptr->up_down_time_stretch);
}
if (portamento_ptr->up_down_time_stretch < 0.0 &&
newfreq > oldfreq)
{
if (portamento_ptr->up_down_time_stretch == -1.0)
{
LOG_DEBUG("Not using portamento up portamento because of time stretch value");
return false;
}
portamentotime *= pow(0.1, -portamento_ptr->up_down_time_stretch);
}
portamento_ptr->dx = SOUND_BUFFER_SIZE / (portamentotime * sample_rate);
portamento_ptr->origfreqrap = oldfreq / newfreq;
if (portamento_ptr->origfreqrap > 1.0)
{
tmprap = portamento_ptr->origfreqrap;
}
else
{
tmprap = 1.0 / portamento_ptr->origfreqrap;
}
thresholdrap = pow(2.0, portamento_ptr->pitch_threshold / 12.0);
if (!portamento_ptr->pitch_threshold_above &&
tmprap - 0.00001 > thresholdrap)
{
LOG_DEBUG("Not using portamento because it is not below threshold");
return false;
}
if (portamento_ptr->pitch_threshold_above &&
tmprap + 0.00001 < thresholdrap)
{
LOG_DEBUG("Not using portamento because it is not above threshold");
return false;
}
LOG_DEBUG("Using portamento");
portamento_ptr->used = true;
portamento_ptr->freqrap=portamento_ptr->origfreqrap;
return true;
}
// update portamento values
void
zyn_portamento_update(
struct zyn_portamento * portamento_ptr)
{
if (!portamento_ptr->used)
{
return;
}
portamento_ptr->x += portamento_ptr->dx;
if (portamento_ptr->x > 1.0)
{
portamento_ptr->x = 1.0;
portamento_ptr->used = false;
}
portamento_ptr->freqrap = (1.0 - portamento_ptr->x) * portamento_ptr->origfreqrap + portamento_ptr->x;
}
#define portamento_ptr ((struct zyn_portamento * )context)
float
zyn_component_portamento_get_float(
void * context,
unsigned int parameter)
{
switch (parameter)
{
case ZYNADD_PARAMETER_FLOAT_PORTAMENTO_TIME:
return portamento_ptr->time;
case ZYNADD_PARAMETER_FLOAT_PORTAMENTO_TIME_STRETCH:
return portamento_ptr->up_down_time_stretch;
default:
LOG_ERROR("Unknown portamento float parameter %u", parameter);
assert(0);
}
}
void
zyn_component_portamento_set_float(
void * context,
unsigned int parameter,
float value)
{
switch (parameter)
{
case ZYNADD_PARAMETER_FLOAT_PORTAMENTO_TIME:
portamento_ptr->time = value;
return;
case ZYNADD_PARAMETER_FLOAT_PORTAMENTO_TIME_STRETCH:
portamento_ptr->up_down_time_stretch = value;
return;
default:
LOG_ERROR("Unknown portamento float parameter %u", parameter);
assert(0);
}
}
signed int
zyn_component_portamento_get_int(
void * context,
unsigned int parameter)
{
switch (parameter)
{
case ZYNADD_PARAMETER_INT_PORTAMENTO_PITCH_THRESHOLD:
return portamento_ptr->pitch_threshold;
default:
LOG_ERROR("Unknown portamento int parameter %u", parameter);
assert(0);
}
}
void
zyn_component_portamento_set_int(
void * context,
unsigned int parameter,
signed int value)
{
switch (parameter)
{
case ZYNADD_PARAMETER_INT_PORTAMENTO_PITCH_THRESHOLD:
portamento_ptr->pitch_threshold = value;
return;
default:
LOG_ERROR("Unknown portamento int parameter %u", parameter);
assert(0);
}
}
bool
zyn_component_portamento_get_bool(
void * context,
unsigned int parameter)
{
switch (parameter)
{
case ZYNADD_PARAMETER_BOOL_PORTAMENTO_ENABLED:
return portamento_ptr->enabled;
case ZYNADD_PARAMETER_BOOL_PORTAMENTO_PITCH_THRESHOLD_ABOVE:
return portamento_ptr->pitch_threshold_above;
default:
LOG_ERROR("Unknown bool portamento parameter %u", parameter);
assert(0);
}
}
void
zyn_component_portamento_set_bool(
void * context,
unsigned int parameter,
bool value)
{
switch (parameter)
{
case ZYNADD_PARAMETER_BOOL_PORTAMENTO_ENABLED:
portamento_ptr->enabled = value;
return;
case ZYNADD_PARAMETER_BOOL_PORTAMENTO_PITCH_THRESHOLD_ABOVE:
portamento_ptr->pitch_threshold_above = value;
return;
default:
LOG_ERROR("Unknown bool portamento parameter %u", parameter);
assert(0);
}
}
#undef portamento_ptr
void
zyn_addsynth_component_init_portamento(
struct zyn_component_descriptor * component_ptr,
struct zyn_portamento * portamento_ptr)
{
ZYN_INIT_COMPONENT(component_ptr, portamento_ptr, zyn_component_portamento_);
}
|