3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003/2005 Angel Ortega <angel@triptico.com>
6 ss_eff.c - Software syntesizer's digital effects
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 http://www.triptico.com
43 struct ss_eff
* ss_eff_add(struct ss_eff
** ec
, double size
,
44 float gain
, float (* func
)(struct ss_eff
*, float))
45 /* adds an effect to the ec chain */
49 /* convert to frames */
52 /* create new structure and reset */
53 if((e
=malloc(sizeof(struct ss_eff
))) == NULL
)
56 memset(e
, '\0', sizeof(struct ss_eff
));
65 /* loop to add e to the end */
66 for(t
=*ec
;t
->next
!= NULL
;t
=t
->next
);
74 e
->wave
=(float *) malloc(((int) size
) * sizeof(float));
75 memset(e
->wave
, '\0', ((int) size
) * sizeof(float));
86 void ss_eff_set_lfo(struct ss_eff
* e
, double freq
, double phase
, double depth
)
90 e
->lfo_inc
=(freq
* 6.28) / (double) ss_frequency
;
91 e
->lfo_depth
=MS2F(depth
);
95 double ss_eff_lfo(struct ss_eff
* e
)
96 /* processes an lfo */
106 * ss_eff_process - Processes a chain of digital effects
107 * @e: the effect chain
110 * Processes a chain of digital effects, taking each one as input
111 * the output of the previous one.
113 float ss_eff_process(struct ss_eff
* e
, float s
)
120 /* increment cursor */
121 if(++e
->cursor
>= e
->size
)
133 * ss_eff_off - Destroys a chain of digital effects
134 * @ec: the effect chain
136 * Destroys a chain of digital effects.
138 void ss_eff_off(struct ss_eff
** ec
)
142 struct ss_eff
* e
=(*ec
)->next
;
144 /* free the buffer, if any */
145 if((*ec
)->wave
!= NULL
)
148 /* free the effect itself */
161 static float func_delay(struct ss_eff
* e
, float input
)
165 s
=e
->wave
[(int) e
->cursor
];
166 e
->wave
[(int) e
->cursor
]=input
;
173 * ss_eff_delay - Adds a delay effect.
174 * @ec: the effect chain
175 * @size: delay in milliseconds
177 * Adds a delay effect. On output, this effect will simply
178 * delay the output of the samples fed to it in @size
179 * frames. No further filtering is done.
181 void ss_eff_delay(struct ss_eff
** ec
, double size
)
183 ss_eff_add(ec
, size
, 0, func_delay
);
189 static float func_echo(struct ss_eff
* e
, float input
)
193 s
=e
->wave
[(int) e
->cursor
] * e
->gain
;
194 e
->wave
[(int) e
->cursor
]=input
;
201 * ss_eff_echo - Adds an echo effect.
202 * @ec: the effect chain
203 * @size: delay in milliseconds
206 * Adds an echo effect. Outputs the current sample mixed
207 * with the product of the sample sent @size frames ago
208 * multiplied by the specified @gain.
210 void ss_eff_echo(struct ss_eff
** ec
, double size
, float gain
)
212 ss_eff_add(ec
, size
, gain
, func_echo
);
218 static float func_comb(struct ss_eff
* e
, float input
)
222 s
=e
->wave
[(int) e
->cursor
];
223 e
->wave
[(int) e
->cursor
]=input
+ (s
* e
->gain
);
230 * effect_comb - Adds a comb filter.
231 * @ec: the effect chain
232 * @size: delay in milliseconds
235 * Adds a comb filter, being @size the number of samples to
236 * delay and @gain the feedback output. Comb filters are
239 void ss_eff_comb(struct ss_eff
** ec
, double size
, float gain
)
241 ss_eff_add(ec
, size
, gain
, func_comb
);
247 static float func_allpass(struct ss_eff
* e
, float input
)
251 t
=e
->wave
[(int)e
->cursor
];
253 u
=input
+ (t
* e
->gain
);
256 e
->wave
[(int) e
->cursor
]=u
;
263 * ss_eff_allpass - Adds an allpass filter.
264 * @ec: the effect chain
265 * @size: delay in milliseconds
268 * Adds an allpass filter, being @size the number of samples to
269 * delay and @gain the feedback output. Allpass filters are
272 void ss_eff_allpass(struct ss_eff
** ec
, double size
, float gain
)
274 ss_eff_add(ec
, size
, gain
, func_allpass
);
280 static float func_flanger(struct ss_eff
* e
, float input
)
285 c
=e
->cursor
- e
->lfo_depth
- (e
->lfo_depth
* ss_eff_lfo(e
));
287 s
=ss_get_sample(e
->wave
, e
->size
, c
) * e
->gain
;
289 e
->wave
[(int) e
->cursor
]=input
;
297 * ss_eff_flanger: Adds a flanger effect.
298 * @ec: the effect chain
299 * @size: delay in milliseconds
301 * @depth: flanger depth in milliseconds
302 * @freq: LFO frequency [0..1]
303 * @phase: initial phase [0..1]
305 * Adds a flanger effect, being @size the number of samples
306 * to delay, @gain the output gain, @depth the number of samples
307 * the output will be 'flanged' (bigger values mean bigger
308 * fluctuations in the final frequency), @freq the frequency of
309 * the LFO in Hz and @phase the initial LFO value as a
310 * fractional part of a period, being 0 the start of the period,
311 * 0.5 half a period and so on. The LFO is sinusoidal.
313 void ss_eff_flanger(struct ss_eff
** ec
, double size
, float gain
,
314 double depth
, double freq
, double phase
)
318 e
=ss_eff_add(ec
, size
, gain
, func_flanger
);
319 ss_eff_set_lfo(e
, freq
, phase
, depth
);
325 static float func_wobble(struct ss_eff
* e
, float input
)
329 s
=input
* ss_eff_lfo(e
);
336 * effect_wobble - Adds a wobble effect.
337 * @ec: the effect chain
338 * @freq: frequency [0..1]
339 * @phase: initial phase [0..1]
341 * Adds a wobble effect, where the sample amplitudes are
342 * multiplied by an LFO, so sound volume wobbles from full
343 * volume to silence twice a period. @freq is the LFO frequency
344 * in Hz and @phase the initial LFO value as a
345 * fractional part of a period, being 0 the start of the period,
346 * 0.5 half a period and so on. The LFO is sinusoidal.
348 void ss_eff_wobble(struct ss_eff
** ec
, double freq
, double phase
)
352 e
=ss_eff_add(ec
, 0, 0, func_wobble
);
353 ss_eff_set_lfo(e
, freq
, phase
, 0);
357 /* square wave wobble */
359 static float func_square_wobble(struct ss_eff
* e
, float input
)
361 return(ss_eff_lfo(e
) > 0 ? input
: 0);
366 * ss_eff_square_wobble - Adds a square wave wobble effect.
367 * @ec: the effect chain
368 * @freq: frequency [0..1]
369 * @phase: initial phase [0..1]
371 * Adds an effect like the wobble one (see documentation for
372 * effect_wobble()), but using a square wave, meaning that input
373 * goes unfiltered (full amplitude) for half the period and
374 * complete silence the other half.
376 void ss_eff_square_wobble(struct ss_eff
** ec
, double freq
, double phase
)
380 e
=ss_eff_add(ec
, 0, 0, func_square_wobble
);
381 ss_eff_set_lfo(e
, freq
, phase
, 0);
387 static float func_fader(struct ss_eff
* e
, float input
)
404 * ss_eff_fader - Adds a fader effect.
405 * @ec: the effect chain
406 * @size: number of milliseconds the fader will last
407 * @initial: initial volume
408 * @final: final volume
410 * Adds a fader effect. The effect will fade in or out the input
411 * volume from @initial to @final during @size samples.
413 void ss_eff_fader(struct ss_eff
** ec
, double size
, float initial
, float final
)
417 e
=ss_eff_add(ec
, size
, initial
, func_fader
);
419 e
->igain
=(final
- initial
) / (float) MS2F(size
);
424 * ss_eff_reverb - Adds a simple reverb effect.
425 * @ec: the effect chain
427 * Adds a simple reverb effect, using a chain of allpass filters.
429 void ss_eff_reverb(struct ss_eff
** ec
)
431 ss_eff_allpass(ec
, 20.0, 0.9);
432 ss_eff_allpass(ec
, 36.0, 0.9);
433 ss_eff_allpass(ec
, 39.0, 0.9);