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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
|
/*
* Diverse Bristol audio routines.
* Copyright (c) by Nick Copeland <nickycopeland@hotmail.com> 1996,2012
*
*
* 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; either version 3 of the License, or
* (at your option) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.
*
*/
/*#define BRISTOL_DBG */
/*
* Need to have basic template for an operator. Will consist of
*
* aksenvinit()
* operate()
* reset()
* destroy()
*
* destroy() is in the library.
*
* Operate will be called when all the inputs have been loaded, and the result
* will be an output buffer written to the next operator.
*/
#include "bristol.h"
#include "aksenv.h"
/*
* The name of this operator, IO count, and IO names.
*/
#define OPNAME "AKSENV"
#define OPDESCRIPTION "Digital ADSR Envelope Generator"
#define PCOUNT 4
#define IOCOUNT 3
#define AKSENV_OUT_IND 0
static float rate;
/*
* Reset any local memory information.
*/
static int destroy(bristolOP *operator)
{
#ifdef BRISTOL_DBG
printf("reset(%x)\n", operator);
#endif
/*
* Unmalloc anything we added to this structure
*/
bristolfree(operator->specs);
/*
* Free any local memory. We should also free ourselves, since we did the
* initial allocation.
*/
cleanup(operator);
return(0);
}
/*
* Reset any local memory information.
*/
static int reset(bristolOP *operator, bristolOPParams *param)
{
#ifdef BRISTOL_DBG
printf("reset(%x)\n", operator);
#endif
param->param[0].float_val = 0.01;
param->param[1].int_val = 50000; /* Hm, about one second! */
param->param[2].float_val = 0.01;
param->param[3].float_val = 50000; /* Hm, about one second! */
return(0);
}
/*
* Alter an internal parameter of an operator.
*/
static int param(bristolOP *operator, bristolOPParams *param,
unsigned char index, float value)
{
int offset = (value * (CONTROLLER_RANGE - 1)) + 1;
if (offset >= CONTROLLER_RANGE)
offset = CONTROLLER_RANGE - 1;
else if (offset < 0)
offset = 0;
#ifdef BRISTOL_DBG
printf("param(%x, %f)\n", operator, value);
#endif
/*
* Attack 2m-1s
* On 0-2.5s
* Decay 3ms-15.s
* off 10ms-5s + off
*/
switch (index) {
case 0:
/*
* Attack value must go from 88 to one secondsworth samples
*/
if (value < 0.002)
value = 0.002;
param->param[index].float_val = 1.0 / (value * rate);
/*printf("%i is %f\n", index, param->param[index].float_val); */
break;
case 1:
/*
* On from 0 to 2.5 * rate
*/
param->param[index].float_val = value * rate * 2.5f;
/*printf("%i is %f\n", index, param->param[index].float_val); */
break;
case 2:
/*
* Decay
*/
if (value < ((float) 3.0f / 15000.0f))
value = ((float) 3.0) / 15000.0f;
param->param[index].float_val = 1.0f / (value * rate * 15);
/*printf("%i is %f\n", index, param->param[index].float_val); */
param->param[index].float_val = value;
break;
case 3:
/*
* For manual trigger
*/
if (value == 1.0)
param->param[index].int_val = 0;
else
param->param[index].int_val = 1;
if ((value = value * rate * 5) < 440)
value = 440;
param->param[index].float_val = value;
/*printf("%i is %f\n", index, param->param[index].float_val); */
break;
}
#ifdef BRISTOL_DBG
printf("a %f, d %f, s %0.10f r %f g %f %i, %i\n",
param->param[0].float_val, param->param[1].float_val,
param->param[2].float_val, param->param[3].float_val,
param->param[4].float_val, param->param[5].int_val, offset);
#endif
return(0);
}
#define MM_PARAM voice->velocity
/*#define MM_PARAM voice->baudio->contcontroller[1] */
/*
* Pure output signal generator - will drive amps, filters, DCOs, whatever.
*/
static int operate(register bristolOP *operator, bristolVoice *voice,
bristolOPParams *param, void *lcl)
{
register bristolAKSENVlocal *local = lcl;
register float cgain, attack, on, decay, off, *ob, egain, oncount;
register int count, rampup = -1;
bristolAKSENV *specs;
specs = (bristolAKSENV *) operator->specs;
#ifdef BRISTOL_DBG
printf("aksenv(%x, %x, %x, %x)\n", operator, voice, param, local);
#endif
attack = param->param[0].float_val;
on = param->param[1].float_val;
off = param->param[3].float_val;
/*printf("%f %f %f %f\n", attack, on, decay, off); */
/*
* We are only going to stuff the decay modifier once per call. This is
* a change to a slow parameter so the coarse adjustment should not have
* a major effect on the result?
decay = param->param[2].float_val;
*/
if ((cgain = specs->spec.io[1].buf[0] * 0.01 + param->param[2].float_val)
< ((float) 3.0f / 15000.0f))
cgain = ((float) 3.0) / 15000.0f;
decay = 1.0f / (cgain * rate * 15);
/*
* Egain is to smooth over changes to the velocity gain when we retrigger
* the voice.
*/
cgain = local->cgain;
egain = local->egain;
oncount = local->oncount;
if (egain < cgain)
rampup = 1;
else if (egain > cgain)
rampup = 0;
count = specs->spec.io[AKSENV_OUT_IND].samplecount;
ob = specs->spec.io[AKSENV_OUT_IND].buf;
if (voice->flags & BRISTOL_KEYON)
{
cgain = 0.0;
local->cstate = STATE_ATTACK;
voice->flags &= ~BRISTOL_KEYON;
if ((~voice->flags & BRISTOL_KEYREON) &&
(voice->offset > 0) && (voice->offset < count))
{
if (voice->baudio->midiflags & BRISTOL_MIDI_DEBUG1)
printf("envelope trigger offset %i frames\n", voice->offset);
memset(ob, 0.0f, voice->offset * sizeof(float));
ob += voice->offset;
count -= voice->offset;
}
}
/*
* See if we need to retrigger. If we are triggered already then reset
* the trigger if signal is zero. If we are not triggered then may set state
*/
if (local->estate == 0)
{
if (specs->spec.io[2].buf[0] > 0)
{
local->cstate = STATE_ATTACK;
local->estate = 1;
}
} else {
if (specs->spec.io[2].buf[0] <= 0)
local->estate = 0;
}
/*
* Attack 2m-1s
* On 0-2.5s
* Decay 3ms-15s
* off 10ms-5s + off
*
* We need some state indicator to say when we are leading into the attack.
* This is actually in the MIDI voice structure, but we do not have access
* to this. We should assume that a MIDI event sets our cstate as necessary?
*/
while (count > 0)
{
/*
* We have parameters for rate of attack, on time, rate of decay and
* off time. If offtime is 1.0 then we do not have automatic retrigger
* but wait for a KEYON flag.
*/
switch (local->cstate)
{
case STATE_ATTACK:
while ((cgain += attack) < 1.0f)
{
*ob++ = cgain;
if (count-- <= 0)
break;
}
if (cgain >= 1.0)
{
local->cstate = STATE_ON;
oncount = on;
}
break;
case STATE_ON:
while (--oncount > 0)
{
*ob++ = 1.0f;
if (count-- <= 0)
break;
}
if (oncount <= 0)
local->cstate = STATE_DECAY;
break;
case STATE_DECAY:
while ((cgain -= decay) > 0.0f)
{
*ob++ = cgain;
if (count-- <= 0)
break;
}
if (cgain <= 0.0f)
{
local->cstate = STATE_OFF;
oncount = off;
}
break;
case STATE_OFF:
while (--oncount > 0)
{
*ob++ = 0.0f;
if (count-- <= 0)
break;
}
if (oncount <= 0)
{
if (param->param[3].int_val != 0)
local->cstate = STATE_ATTACK;
}
break;
}
}
local->cgain = cgain;
local->egain = egain;
local->oncount = oncount;
return(0);
}
/*
* Setup any variables in our OP structure, in our IO structures, and malloc
* any memory we need.
*/
bristolOP *
aksenvinit(bristolOP **operator, int index, int samplerate, int samplecount)
{
bristolAKSENV *specs;
#ifdef BRISTOL_DBG
printf("aksenvinit(%x(%x), %i, %i, %i)\n",
operator, *operator, index, samplerate, samplecount);
#endif
*operator = bristolOPinit(operator, index, samplecount);
rate = samplerate;
/*
* Then the local parameters specific to this operator. These will be
* the same for each operator, but must be inited in the local code.
*/
(*operator)->operate = operate;
(*operator)->destroy = destroy;
(*operator)->reset = reset;
(*operator)->param = param;
specs = (bristolAKSENV *) bristolmalloc0(sizeof(bristolAKSENV));
(*operator)->specs = (bristolOPSpec *) specs;
(*operator)->size = sizeof(bristolAKSENV);
/*
* These are specific to this operator, and will need to be altered for
* each operator.
*/
specs->spec.opname = OPNAME;
specs->spec.description = OPDESCRIPTION;
specs->spec.pcount = PCOUNT;
specs->spec.iocount = IOCOUNT;
specs->spec.localsize = sizeof(bristolAKSENVlocal);
/*
* Now fill in the aksenv specs for this operator. These are specific to
* an ADSR.
*/
specs->spec.param[0].pname = "attack";
specs->spec.param[0].description = "Initial ramp up rate";
specs->spec.param[0].type = BRISTOL_FLOAT;
specs->spec.param[0].low = 0;
specs->spec.param[0].high = 1;
specs->spec.param[0].flags = BRISTOL_ROTARY|BRISTOL_SLIDER;
specs->spec.param[1].pname = "on";
specs->spec.param[1].description = "Decay to sustain rate";
specs->spec.param[1].type = BRISTOL_FLOAT;
specs->spec.param[1].low = 0;
specs->spec.param[1].high = 1;
specs->spec.param[1].flags = BRISTOL_ROTARY|BRISTOL_SLIDER;
specs->spec.param[2].pname = "decay";
specs->spec.param[2].description = "Output level steady state";
specs->spec.param[2].type = BRISTOL_FLOAT;
specs->spec.param[2].low = 0;
specs->spec.param[2].high = 1;
specs->spec.param[2].flags = BRISTOL_ROTARY|BRISTOL_SLIDER;
specs->spec.param[3].pname = "off";
specs->spec.param[3].description = "Final decay rate";
specs->spec.param[3].type = BRISTOL_FLOAT;
specs->spec.param[3].low = 0;
specs->spec.param[3].high = 1;
specs->spec.param[3].flags = BRISTOL_ROTARY|BRISTOL_SLIDER;
/*
* Now fill in the dco IO specs.
*/
specs->spec.io[0].ioname = "output";
specs->spec.io[0].description = "ADSR Envelope Output Signal";
specs->spec.io[0].samplerate = samplerate;
specs->spec.io[0].samplecount = samplecount;
specs->spec.io[0].flags = BRISTOL_DC|BRISTOL_OUTPUT;
specs->spec.io[1].ioname = "decay";
specs->spec.io[1].description = "decay parameter modifier";
specs->spec.io[1].samplerate = samplerate;
specs->spec.io[1].samplecount = samplecount;
specs->spec.io[1].flags = BRISTOL_DC|BRISTOL_OUTPUT;
specs->spec.io[2].ioname = "trigger";
specs->spec.io[2].description = "trigger gate";
specs->spec.io[2].samplerate = samplerate;
specs->spec.io[2].samplecount = samplecount;
specs->spec.io[2].flags = BRISTOL_DC|BRISTOL_OUTPUT;
return(*operator);
}
|