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 -----------+------------------------------------------>| |
16 * +----->| delay 1 |------------------------->| |
20 * +---------->| delay 2 |-------------------->| |
24 * +--------------->| delay n |--------------->|___|
30 * echo gain-in gain-out delay-1 decay-1 [delay-2 decay-2 ... delay-n decay-n]
33 * gain-in, decay-1 ... decay-n : 0.0 ... 1.0 volume
34 * gain-out : 0.0 ... volume
35 * delay-1 ... delay-n : > 0.0 msec
38 * when decay is close to 1.0, the samples can begin clipping and the output
42 * 1 / out-gain > gain-in ( 1 + decay-1 + ... + decay-n )
47 #include <stdlib.h> /* Harmless, and prototypes atof() etc. --dgc */
49 #define DELAY_BUFSIZ ( 50 * 50U * 1024 )
50 #define MAX_ECHOS 7 /* 24 bit x ( 1 + MAX_ECHOS ) = */
51 /* 24 bit x 8 = 32 bit !!! */
53 /* Private data for SKEL file */
58 float in_gain
, out_gain
;
59 float delay
[MAX_ECHOS
], decay
[MAX_ECHOS
];
60 ptrdiff_t samples
[MAX_ECHOS
], maxsamples
;
64 /* Private data for SKEL file */
70 static int sox_echo_getopts(sox_effect_t
* effp
, int argc
, char **argv
)
72 priv_t
* echo
= (priv_t
*) effp
->priv
;
78 if ((argc
< 4) || (argc
% 2))
79 return lsx_usage(effp
);
82 sscanf(argv
[i
++], "%f", &echo
->in_gain
);
83 sscanf(argv
[i
++], "%f", &echo
->out_gain
);
85 if ( echo
->num_delays
>= MAX_ECHOS
)
86 lsx_fail("echo: to many delays, use less than %i delays",
88 /* Linux bug and it's cleaner. */
89 sscanf(argv
[i
++], "%f", &echo
->delay
[echo
->num_delays
]);
90 sscanf(argv
[i
++], "%f", &echo
->decay
[echo
->num_delays
]);
97 * Prepare for processing.
99 static int sox_echo_start(sox_effect_t
* effp
)
101 priv_t
* echo
= (priv_t
*) effp
->priv
;
106 echo
->maxsamples
= 0;
107 if ( echo
->in_gain
< 0.0 )
109 lsx_fail("echo: gain-in must be positive!");
112 if ( echo
->in_gain
> 1.0 )
114 lsx_fail("echo: gain-in must be less than 1.0!");
117 if ( echo
->out_gain
< 0.0 )
119 lsx_fail("echo: gain-in must be positive!");
122 for ( i
= 0; i
< echo
->num_delays
; i
++ ) {
123 echo
->samples
[i
] = echo
->delay
[i
] * effp
->in_signal
.rate
/ 1000.0;
124 if ( echo
->samples
[i
] < 1 )
126 lsx_fail("echo: delay must be positive!");
129 if ( echo
->samples
[i
] > (ptrdiff_t)DELAY_BUFSIZ
)
131 lsx_fail("echo: delay must be less than %g seconds!",
132 DELAY_BUFSIZ
/ effp
->in_signal
.rate
);
135 if ( echo
->decay
[i
] < 0.0 )
137 lsx_fail("echo: decay must be positive!" );
140 if ( echo
->decay
[i
] > 1.0 )
142 lsx_fail("echo: decay must be less than 1.0!" );
145 if ( echo
->samples
[i
] > echo
->maxsamples
)
146 echo
->maxsamples
= echo
->samples
[i
];
148 echo
->delay_buf
= lsx_malloc(sizeof (double) * echo
->maxsamples
);
149 for ( j
= 0; j
< echo
->maxsamples
; ++j
)
150 echo
->delay_buf
[j
] = 0.0;
151 /* Be nice and check the hint with warning, if... */
153 for ( i
= 0; i
< echo
->num_delays
; i
++ )
154 sum_in_volume
+= echo
->decay
[i
];
155 if ( sum_in_volume
* echo
->in_gain
> 1.0 / echo
->out_gain
)
156 lsx_warn("echo: warning >>> gain-out can cause saturation of output <<<");
158 echo
->fade_out
= echo
->maxsamples
;
160 effp
->out_signal
.length
= SOX_UNKNOWN_LEN
; /* TODO: calculate actual length */
162 return (SOX_SUCCESS
);
166 * Processed signed long samples from ibuf to obuf.
167 * Return number of samples processed.
169 static int sox_echo_flow(sox_effect_t
* effp
, const sox_sample_t
*ibuf
, sox_sample_t
*obuf
,
170 size_t *isamp
, size_t *osamp
)
172 priv_t
* echo
= (priv_t
*) effp
->priv
;
176 size_t len
= min(*isamp
, *osamp
);
177 *isamp
= *osamp
= len
;
180 /* Store delays as 24-bit signed longs */
181 d_in
= (double) *ibuf
++ / 256;
182 /* Compute output first */
183 d_out
= d_in
* echo
->in_gain
;
184 for ( j
= 0; j
< echo
->num_delays
; j
++ ) {
185 d_out
+= echo
->delay_buf
[
186 (echo
->counter
+ echo
->maxsamples
- echo
->samples
[j
]) % echo
->maxsamples
]
189 /* Adjust the output volume and size to 24 bit */
190 d_out
= d_out
* echo
->out_gain
;
191 out
= SOX_24BIT_CLIP_COUNT((sox_sample_t
) d_out
, effp
->clips
);
193 /* Store input in delay buffer */
194 echo
->delay_buf
[echo
->counter
] = d_in
;
195 /* Adjust the counter */
196 echo
->counter
= ( echo
->counter
+ 1 ) % echo
->maxsamples
;
198 /* processed all samples */
199 return (SOX_SUCCESS
);
203 * Drain out reverb lines.
205 static int sox_echo_drain(sox_effect_t
* effp
, sox_sample_t
*obuf
, size_t *osamp
)
207 priv_t
* echo
= (priv_t
*) effp
->priv
;
214 /* drain out delay samples */
215 while ( ( done
< *osamp
) && ( done
< echo
->fade_out
) ) {
218 for ( j
= 0; j
< echo
->num_delays
; j
++ ) {
219 d_out
+= echo
->delay_buf
[
220 (echo
->counter
+ echo
->maxsamples
- echo
->samples
[j
]) % echo
->maxsamples
]
223 /* Adjust the output volume and size to 24 bit */
224 d_out
= d_out
* echo
->out_gain
;
225 out
= SOX_24BIT_CLIP_COUNT((sox_sample_t
) d_out
, effp
->clips
);
227 /* Store input in delay buffer */
228 echo
->delay_buf
[echo
->counter
] = d_in
;
229 /* Adjust the counters */
230 echo
->counter
= ( echo
->counter
+ 1 ) % echo
->maxsamples
;
234 /* samples played, it remains */
236 if (echo
->fade_out
== 0)
242 static int sox_echo_stop(sox_effect_t
* effp
)
244 priv_t
* echo
= (priv_t
*) effp
->priv
;
246 free(echo
->delay_buf
);
247 echo
->delay_buf
= NULL
;
248 return (SOX_SUCCESS
);
251 static sox_effect_handler_t sox_echo_effect
= {
253 "gain-in gain-out delay decay [ delay decay ... ]",
254 SOX_EFF_LENGTH
| SOX_EFF_GAIN
,
263 const sox_effect_handler_t
*lsx_echo_effect_fn(void)
265 return &sox_echo_effect
;