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
|
/*
* August 24, 1998
* Copyright (C) 1998 Juergen Mueller And Sundry Contributors
* This source code is freely redistributable and may be used for
* any purpose. This copyright notice must be maintained.
* Juergen Mueller And Sundry Contributors are not responsible for
* the consequences of using this software.
*/
/*
* Flanger effect.
*
* Flow diagram scheme:
*
* * gain-in ___
* ibuff -----+--------------------------------------------->| |
* | _______ | |
* | | | * decay | |
* +---->| delay |------------------------------->| + |
* |_______| | |
* /|\ | |
* | |___|
* | |
* +---------------+ +------------------+ | * gain-out
* | Delay control |<-----| modulation speed | |
* +---------------+ +------------------+ +----->obuff
*
*
* The delay is controled by a sine or triangle modulation.
*
* Usage:
* flanger gain-in gain-out delay decay speed [ -s | -t ]
*
* Where:
* gain-in, decay : 0.0 ... 1.0 volume
* gain-out : 0.0 ... volume
* delay : 0.0 ... 5.0 msec
* speed : 0.1 ... 2.0 Hz modulation
* -s : modulation by sine (default)
* -t : modulation by triangle
*
* Note:
* when decay is close to 1.0, the samples may begin clipping or the output
* can saturate!
*
* Hint:
* 1 / out-gain > gain-in * ( 1 + decay )
*
*/
/*
* Sound Tools flanger effect file.
*/
#include <stdlib.h> /* Harmless, and prototypes atof() etc. --dgc */
#include <math.h>
#include <string.h>
#include "st.h"
#define MOD_SINE 0
#define MOD_TRIANGLE 1
/* Private data for SKEL file */
typedef struct flangerstuff {
int modulation;
int counter;
int phase;
double *flangerbuf;
float in_gain, out_gain;
float delay, decay;
float speed;
long length;
int *lookup_tab;
long maxsamples, fade_out;
} *flanger_t;
/* Private data for SKEL file */
LONG flanger_clip24(l)
LONG l;
{
if (l >= ((LONG)1 << 24))
return ((LONG)1 << 24) - 1;
else if (l <= -((LONG)1 << 24))
return -((LONG)1 << 24) + 1;
else
return l;
}
/* This was very painful. We need a sine library. */
void flanger_sine(buf, len, depth)
int *buf;
long len;
long depth;
{
long i;
double val;
for (i = 0; i < len; i++) {
val = sin((double)i/(double)len * 2.0 * M_PI);
buf[i] = (int) ((1.0 + val) * depth / 2.0);
}
}
void flanger_triangle(buf, len, depth)
int *buf;
long len;
long depth;
{
long i;
double val;
for (i = 0; i < len / 2; i++) {
val = i * 2.0 / len;
buf[i] = (int) (val * depth);
}
for (i = len / 2; i < len ; i++) {
val = (len - i) * 2.0 / len;
buf[i] = (int) (val * depth);
}
}
/*
* Process options
*/
void flanger_getopts(effp, n, argv)
eff_t effp;
int n;
char **argv;
{
flanger_t flanger = (flanger_t) effp->priv;
if (!((n == 5) || (n == 6)))
fail("Usage: flanger gain-in gain-out delay decay speed [ -s | -t ]");
sscanf(argv[0], "%f", &flanger->in_gain);
sscanf(argv[1], "%f", &flanger->out_gain);
sscanf(argv[2], "%f", &flanger->delay);
sscanf(argv[3], "%f", &flanger->decay);
sscanf(argv[4], "%f", &flanger->speed);
flanger->modulation = MOD_SINE;
if ( n == 6 ) {
if ( !strcmp(argv[5], "-s"))
flanger->modulation = MOD_SINE;
else if ( ! strcmp(argv[5], "-t"))
flanger->modulation = MOD_TRIANGLE;
else
fail("Usage: flanger gain-in gain-out delay decay speed [ -s | -t ]");
}
}
/*
* Prepare for processing.
*/
void flanger_start(effp)
eff_t effp;
{
flanger_t flanger = (flanger_t) effp->priv;
int i;
flanger->maxsamples = flanger->delay * effp->ininfo.rate / 1000.0;
if ( flanger->in_gain < 0.0 )
fail("flanger: gain-in must be positive!\n");
if ( flanger->in_gain > 1.0 )
fail("flanger: gain-in must be less than 1.0!\n");
if ( flanger->out_gain < 0.0 )
fail("flanger: gain-out must be positive!\n");
if ( flanger->delay < 0.0 )
fail("flanger: delay must be positive!\n");
if ( flanger->delay > 5.0 )
fail("flanger: delay must be less than 5.0 msec!\n");
if ( flanger->speed < 0.1 )
fail("flanger: speed must be more than 0.1 Hz!\n");
if ( flanger->speed > 2.0 )
fail("flanger: speed must be less than 2.0 Hz!\n");
if ( flanger->decay < 0.0 )
fail("flanger: decay must be positive!\n" );
if ( flanger->decay > 1.0 )
fail("flanger: decay must be less that 1.0!\n" );
/* Be nice and check the hint with warning, if... */
if ( flanger->in_gain * ( 1.0 + flanger->decay ) > 1.0 / flanger->out_gain )
warn("flanger: warning >>> gain-out can cause saturation or clipping of output <<<");
flanger->length = effp->ininfo.rate / flanger->speed;
if (! (flanger->flangerbuf = \
(double *) malloc(sizeof (double) * flanger->maxsamples)))
fail("flanger: Cannot malloc %d bytes!\n",
sizeof(double) * flanger->maxsamples);
for ( i = 0; i < flanger->maxsamples; i++ )
flanger->flangerbuf[i] = 0.0;
if (! (flanger->lookup_tab = \
(int *) malloc(sizeof (int) * flanger->length)))
fail("flanger: Cannot malloc %d bytes!\n",
sizeof(int) * flanger->length);
if ( flanger->modulation == MOD_SINE )
flanger_sine(flanger->lookup_tab, flanger->length, \
flanger->maxsamples - 1);
else
flanger_triangle(flanger->lookup_tab, flanger->length, \
flanger->maxsamples - 1);
flanger->counter = 0;
flanger->phase = 0;
flanger->fade_out = flanger->maxsamples;
}
/*
* Processed signed long samples from ibuf to obuf.
* Return number of samples processed.
*/
void flanger_flow(effp, ibuf, obuf, isamp, osamp)
eff_t effp;
LONG *ibuf, *obuf;
int *isamp, *osamp;
{
flanger_t flanger = (flanger_t) effp->priv;
int len, done;
double d_in, d_out;
LONG out;
len = ((*isamp > *osamp) ? *osamp : *isamp);
for(done = 0; done < len; done++) {
/* Store delays as 24-bit signed longs */
d_in = (double) *ibuf++ / 256;
/* Compute output first */
d_out = d_in * flanger->in_gain;
d_out += flanger->flangerbuf[(flanger->maxsamples + \
flanger->counter - flanger->lookup_tab[flanger->phase]) % \
flanger->maxsamples] * flanger->decay;
/* Adjust the output volume and size to 24 bit */
d_out = d_out * flanger->out_gain;
out = flanger_clip24((LONG) d_out);
*obuf++ = out * 256;
/* Mix decay of delay and input */
flanger->flangerbuf[flanger->counter] = d_in;
flanger->counter = \
( flanger->counter + 1 ) % flanger->maxsamples;
flanger->phase = ( flanger->phase + 1 ) % flanger->length;
}
/* processed all samples */
}
/*
* Drain out reverb lines.
*/
void flanger_drain(effp, obuf, osamp)
eff_t effp;
LONG *obuf;
int *osamp;
{
flanger_t flanger = (flanger_t) effp->priv;
int done;
double d_in, d_out;
LONG out;
done = 0;
while ( ( done < *osamp ) && ( done < flanger->fade_out ) ) {
d_in = 0;
d_out = 0;
/* Compute output first */
d_out += flanger->flangerbuf[(flanger->maxsamples + \
flanger->counter - flanger->lookup_tab[flanger->phase]) % \
flanger->maxsamples] * flanger->decay;
/* Adjust the output volume and size to 24 bit */
d_out = d_out * flanger->out_gain;
out = flanger_clip24((LONG) d_out);
*obuf++ = out * 256;
/* Mix decay of delay and input */
flanger->flangerbuf[flanger->counter] = d_in;
flanger->counter = \
( flanger->counter + 1 ) % flanger->maxsamples;
flanger->phase = ( flanger->phase + 1 ) % flanger->length;
done++;
flanger->fade_out--;
}
/* samples playd, it remains */
*osamp = done;
}
/*
* Clean up flanger effect.
*/
void flanger_stop(effp)
eff_t effp;
{
flanger_t flanger = (flanger_t) effp->priv;
free((char *) flanger->flangerbuf);
flanger->flangerbuf = (double *) -1; /* guaranteed core dump */
free((char *) flanger->lookup_tab);
flanger->lookup_tab = (int *) -1; /* guaranteed core dump */
}
|