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
|
/*
* July 5, 1991
* Copyright 1991 Lance Norskog And Sundry Contributors
* This source code is freely redistributable and may be used for
* any purpose. This copyright notice must be maintained.
* Lance Norskog And Sundry Contributors are not responsible for
* the consequences of using this software.
*/
/*
** Echo effect. based on:
**
** echoplex.c - echo generator
**
** Copyright (C) 1989 by Jef Poskanzer.
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation. This software is provided "as is" without express or
** implied warranty.
*/
/*
* Sound Tools echo effect file.
*/
#if defined(AMIGA) || defined(ARM)
#include <stdlib.h> /* Harmless, and prototypes atof() etc. --dgc */
#endif
#include <math.h>
#include "st.h"
#define FADE_THRESH 10
#define MYBUFSIZ 256
#define DELAYBUFSIZ ( 50L * MAXRATE )
#define MAXDELAYS 30
struct echoplex {
int counter;
int numdelays;
long *delaybuf;
float delay[MAXDELAYS], atten[MAXDELAYS];
long samples[MAXDELAYS], maxsamples;
long pl, ppl, pppl;
};
/* Private data for SKEL file */
typedef struct echostuff {
struct echoplex *echoplex;
} *echo_t;
#ifndef abs
#define abs(a) ((a) >= 0 ? (a) : -(a))
#endif
LONG clip24();
IMPORT writing;
/*
* Process options
*/
echo_getopts(effp, n, argv)
eff_t effp;
int n;
char **argv;
{
echo_t echo = (echo_t) effp->priv;
struct echoplex *e;
int i;
echo->echoplex = (struct echoplex *) malloc(sizeof(struct echoplex));
e = echo->echoplex;
e->numdelays = 0;
e->maxsamples = 0;
if ((n == 0) || (n & 1))
fail("Usage: echo delay attenuation [ delay attenuation ... ]");
i = 0;
while (i < n) {
/* Linux bug and it's cleaner. */
sscanf(argv[i++], "%f", &e->delay[e->numdelays]);
sscanf(argv[i++], "%f", &e->atten[e->numdelays]);
e->numdelays++;
}
}
/*
* Prepare for processing.
*/
echo_start(effp)
eff_t effp;
{
echo_t echo = (echo_t) effp->priv;
struct echoplex *e = echo->echoplex;
int i;
for(i = 0; i < e->numdelays; i++) {
e->samples[i] = e->delay[i] * effp->ininfo.rate;
if ( e->samples[i] < 1 )
fail(" delay must positive, aye!");
if ( e->samples[i] > DELAYBUFSIZ )
fail("Echo: delay must be less than %g seconds",
DELAYBUFSIZ / (float) effp->ininfo.rate );
if ( e->atten[i] < 0.0 )
fail("attenuation must positive, aye!\n" );
if ( e->samples[i] > e->maxsamples )
e->maxsamples = e->samples[i];
}
if (! (e->delaybuf = (LONG *) malloc(sizeof (LONG) * e->maxsamples)))
fail("Echo: Cannot malloc %d bytes\n",
sizeof(LONG) * e->maxsamples);
for ( i = 0; i < e->maxsamples; ++i )
e->delaybuf[i] = 0;
e->pppl = e->ppl = e->pl = 0x7fffff; /* fade-outs */
e->counter = 0;
}
/*
* Processed signed LONG samples from ibuf to obuf.
* Return number of samples processed.
*/
echo_flow(effp, ibuf, obuf, isamp, osamp)
eff_t effp;
LONG *ibuf, *obuf;
int *isamp, *osamp;
{
echo_t echo = (echo_t) effp->priv;
struct echoplex *e = echo->echoplex;
int len, done;
int i, j;
LONG l;
i = e->counter;
len = ((*isamp > *osamp) ? *osamp : *isamp);
for(done = 0; done < len; done++) {
/* Store delays as 24-bit signed longs */
l = *ibuf++ / 256;
for ( j = 0; j < e->numdelays; ++j )
l = l +
e->delaybuf[( i + e->maxsamples - e->samples[j]) % e->maxsamples] * e->atten[j];
l = clip24(l);
e->delaybuf[i] = l;
*obuf++ = l * 256;
i++; /* XXX need a % maxsamples here ? */
i %= e->maxsamples;
}
e->counter = i;
/* processed all samples */
}
/*
* Drain out echo lines.
*/
echo_drain(effp, obuf, osamp)
eff_t effp;
LONG *obuf;
LONG *osamp;
{
echo_t echo = (echo_t) effp->priv;
struct echoplex *e = echo->echoplex;
LONG l;
int i, j, done;
i = e->counter;
done = 0;
/* drain out delay samples */
do {
l = 0;
for ( j = 0; j < e->numdelays; ++j )
l +=
e->delaybuf[(i + e->maxsamples - e->samples[j]) % e->maxsamples] * e->atten[j];
l = clip24(l);
e->delaybuf[i] = l;
obuf[done++] = l * 256;
e->pppl = e->ppl;
e->ppl = e->pl;
e->pl = l;
i++; /* need a % maxsamples here ? */
i %= e->maxsamples;
} while((done < *osamp) &&
((abs(e->pl) + abs(e->ppl) + abs(e->pppl)) > FADE_THRESH));
e->counter = i;
*osamp = done;
/* drain will not be called again */
}
/*
* Clean up echo effect.
*/
echo_stop(effp)
eff_t effp;
{
echo_t echo = (echo_t) effp->priv;
free((char *) echo->echoplex);
echo->echoplex = (struct echoplex *) -1; /* guaranteed core dump */
}
LONG
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;
}
|