1 /* libSoX Echo effect August 24, 1998
3 * Copyright (C) 1998 Juergen Mueller And Sundry Contributors
4 * This source code is freely redistributable and may be used for
5 * any purpose. This copyright notice must be maintained.
6 * Juergen Mueller And Sundry Contributors are not responsible for
7 * the consequences of using this software.
10 * Flow diagram scheme for n delays ( 1 <= n <= MAX_ECHOS ):
13 * ibuff --+--------------------------------------------------->| |
15 * | +----------------------------------->| |
17 * | | +--------------------->| |
19 * | _________ | _________ | _________ +--->|___|
20 * | | | | | | | | | | |
21 * +-->| delay 1 |-+-| delay 2 |-+...-| delay n |--+ | * gain-out
22 * |_________| |_________| |_________| |
25 * echos gain-in gain-out delay-1 decay-1 [delay-2 decay-2 ... delay-n decay-n]
28 * gain-in, decay-1 ... decay-n : 0.0 ... 1.0 volume
29 * gain-out : 0.0 ... volume
30 * delay-1 ... delay-n : > 0.0 msec
33 * when decay is close to 1.0, the samples can begin clipping and the output
37 * 1 / out-gain > gain-in ( 1 + decay-1 + ... + decay-n )
43 #include <stdlib.h> /* Harmless, and prototypes atof() etc. --dgc */
45 #define DELAY_BUFSIZ ( 50 * 50U * 1024 )
46 #define MAX_ECHOS 7 /* 24 bit x ( 1 + MAX_ECHOS ) = */
47 /* 24 bit x 8 = 32 bit !!! */
49 /* Private data for SKEL file */
51 int counter
[MAX_ECHOS
];
54 float in_gain
, out_gain
;
55 float delay
[MAX_ECHOS
], decay
[MAX_ECHOS
];
56 ptrdiff_t samples
[MAX_ECHOS
], pointer
[MAX_ECHOS
];
60 /* Private data for SKEL file */
65 static int sox_echos_getopts(sox_effect_t
* effp
, int argc
, char **argv
)
67 priv_t
* echos
= (priv_t
*) effp
->priv
;
70 echos
->num_delays
= 0;
73 if ((argc
< 4) || (argc
% 2))
74 return lsx_usage(effp
);
77 sscanf(argv
[i
++], "%f", &echos
->in_gain
);
78 sscanf(argv
[i
++], "%f", &echos
->out_gain
);
80 /* Linux bug and it's cleaner. */
81 sscanf(argv
[i
++], "%f", &echos
->delay
[echos
->num_delays
]);
82 sscanf(argv
[i
++], "%f", &echos
->decay
[echos
->num_delays
]);
84 if ( echos
->num_delays
> MAX_ECHOS
)
86 lsx_fail("echos: to many delays, use less than %i delays",
91 echos
->sumsamples
= 0;
96 * Prepare for processing.
98 static int sox_echos_start(sox_effect_t
* effp
)
100 priv_t
* echos
= (priv_t
*) effp
->priv
;
105 if ( echos
->in_gain
< 0.0 )
107 lsx_fail("echos: gain-in must be positive!");
110 if ( echos
->in_gain
> 1.0 )
112 lsx_fail("echos: gain-in must be less than 1.0!");
115 if ( echos
->out_gain
< 0.0 )
117 lsx_fail("echos: gain-in must be positive!");
120 for ( i
= 0; i
< echos
->num_delays
; i
++ ) {
121 echos
->samples
[i
] = echos
->delay
[i
] * effp
->in_signal
.rate
/ 1000.0;
122 if ( echos
->samples
[i
] < 1 )
124 lsx_fail("echos: delay must be positive!");
127 if ( echos
->samples
[i
] > (ptrdiff_t)DELAY_BUFSIZ
)
129 lsx_fail("echos: delay must be less than %g seconds!",
130 DELAY_BUFSIZ
/ effp
->in_signal
.rate
);
133 if ( echos
->decay
[i
] < 0.0 )
135 lsx_fail("echos: decay must be positive!" );
138 if ( echos
->decay
[i
] > 1.0 )
140 lsx_fail("echos: decay must be less than 1.0!" );
143 echos
->counter
[i
] = 0;
144 echos
->pointer
[i
] = echos
->sumsamples
;
145 echos
->sumsamples
+= echos
->samples
[i
];
147 echos
->delay_buf
= lsx_malloc(sizeof (double) * echos
->sumsamples
);
148 for ( j
= 0; j
< echos
->sumsamples
; ++j
)
149 echos
->delay_buf
[j
] = 0.0;
150 /* Be nice and check the hint with warning, if... */
152 for ( i
= 0; i
< echos
->num_delays
; i
++ )
153 sum_in_volume
+= echos
->decay
[i
];
154 if ( sum_in_volume
* echos
->in_gain
> 1.0 / echos
->out_gain
)
155 lsx_warn("echos: warning >>> gain-out can cause saturation of output <<<");
157 effp
->out_signal
.length
= SOX_UNKNOWN_LEN
; /* TODO: calculate actual length */
159 return (SOX_SUCCESS
);
163 * Processed signed long samples from ibuf to obuf.
164 * Return number of samples processed.
166 static int sox_echos_flow(sox_effect_t
* effp
, const sox_sample_t
*ibuf
, sox_sample_t
*obuf
,
167 size_t *isamp
, size_t *osamp
)
169 priv_t
* echos
= (priv_t
*) effp
->priv
;
173 size_t len
= min(*isamp
, *osamp
);
174 *isamp
= *osamp
= len
;
177 /* Store delays as 24-bit signed longs */
178 d_in
= (double) *ibuf
++ / 256;
179 /* Compute output first */
180 d_out
= d_in
* echos
->in_gain
;
181 for ( j
= 0; j
< echos
->num_delays
; j
++ ) {
182 d_out
+= echos
->delay_buf
[echos
->counter
[j
] + echos
->pointer
[j
]] * echos
->decay
[j
];
184 /* Adjust the output volume and size to 24 bit */
185 d_out
= d_out
* echos
->out_gain
;
186 out
= SOX_24BIT_CLIP_COUNT((sox_sample_t
) d_out
, effp
->clips
);
188 /* Mix decay of delays and input */
189 for ( j
= 0; j
< echos
->num_delays
; j
++ ) {
191 echos
->delay_buf
[echos
->counter
[j
] + echos
->pointer
[j
]] = d_in
;
193 echos
->delay_buf
[echos
->counter
[j
] + echos
->pointer
[j
]] =
194 echos
->delay_buf
[echos
->counter
[j
-1] + echos
->pointer
[j
-1]] + d_in
;
196 /* Adjust the counters */
197 for ( j
= 0; j
< echos
->num_delays
; j
++ )
199 ( echos
->counter
[j
] + 1 ) % echos
->samples
[j
];
201 /* processed all samples */
202 return (SOX_SUCCESS
);
206 * Drain out reverb lines.
208 static int sox_echos_drain(sox_effect_t
* effp
, sox_sample_t
*obuf
, size_t *osamp
)
210 priv_t
* echos
= (priv_t
*) effp
->priv
;
217 /* drain out delay samples */
218 while ( ( done
< *osamp
) && ( done
< echos
->sumsamples
) ) {
221 for ( j
= 0; j
< echos
->num_delays
; j
++ ) {
222 d_out
+= echos
->delay_buf
[echos
->counter
[j
] + echos
->pointer
[j
]] * echos
->decay
[j
];
224 /* Adjust the output volume and size to 24 bit */
225 d_out
= d_out
* echos
->out_gain
;
226 out
= SOX_24BIT_CLIP_COUNT((sox_sample_t
) d_out
, effp
->clips
);
228 /* Mix decay of delays and input */
229 for ( j
= 0; j
< echos
->num_delays
; j
++ ) {
231 echos
->delay_buf
[echos
->counter
[j
] + echos
->pointer
[j
]] = d_in
;
233 echos
->delay_buf
[echos
->counter
[j
] + echos
->pointer
[j
]] =
234 echos
->delay_buf
[echos
->counter
[j
-1] + echos
->pointer
[j
-1]];
236 /* Adjust the counters */
237 for ( j
= 0; j
< echos
->num_delays
; j
++ )
239 ( echos
->counter
[j
] + 1 ) % echos
->samples
[j
];
243 /* samples played, it remains */
245 if (echos
->sumsamples
== 0)
252 * Clean up echos effect.
254 static int sox_echos_stop(sox_effect_t
* effp
)
256 priv_t
* echos
= (priv_t
*) effp
->priv
;
258 free(echos
->delay_buf
);
259 echos
->delay_buf
= NULL
;
260 return (SOX_SUCCESS
);
263 static sox_effect_handler_t sox_echos_effect
= {
265 "gain-in gain-out delay decay [ delay decay ... ]",
266 SOX_EFF_LENGTH
| SOX_EFF_GAIN
,
275 const sox_effect_handler_t
*lsx_echos_effect_fn(void)
277 return &sox_echos_effect
;