af_flanger.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright (c) 2006 Rob Sykes <robs@users.sourceforge.net>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/avstring.h"
  21. #include "libavutil/opt.h"
  22. #include "libavutil/samplefmt.h"
  23. #include "avfilter.h"
  24. #include "audio.h"
  25. #include "internal.h"
  26. #include "generate_wave_table.h"
  27. #define INTERPOLATION_LINEAR 0
  28. #define INTERPOLATION_QUADRATIC 1
  29. typedef struct FlangerContext {
  30. const AVClass *class;
  31. double delay_min;
  32. double delay_depth;
  33. double feedback_gain;
  34. double delay_gain;
  35. double speed;
  36. int wave_shape;
  37. double channel_phase;
  38. int interpolation;
  39. double in_gain;
  40. int max_samples;
  41. uint8_t **delay_buffer;
  42. int delay_buf_pos;
  43. double *delay_last;
  44. float *lfo;
  45. int lfo_length;
  46. int lfo_pos;
  47. } FlangerContext;
  48. #define OFFSET(x) offsetof(FlangerContext, x)
  49. #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  50. static const AVOption flanger_options[] = {
  51. { "delay", "base delay in milliseconds", OFFSET(delay_min), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 30, A },
  52. { "depth", "added swept delay in milliseconds", OFFSET(delay_depth), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0, 10, A },
  53. { "regen", "percentage regeneration (delayed signal feedback)", OFFSET(feedback_gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -95, 95, A },
  54. { "width", "percentage of delayed signal mixed with original", OFFSET(delay_gain), AV_OPT_TYPE_DOUBLE, {.dbl=71}, 0, 100, A },
  55. { "speed", "sweeps per second (Hz)", OFFSET(speed), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0.1, 10, A },
  56. { "shape", "swept wave shape", OFFSET(wave_shape), AV_OPT_TYPE_INT, {.i64=WAVE_SIN}, WAVE_SIN, WAVE_NB-1, A, "type" },
  57. { "triangular", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_TRI}, 0, 0, A, "type" },
  58. { "t", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_TRI}, 0, 0, A, "type" },
  59. { "sinusoidal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_SIN}, 0, 0, A, "type" },
  60. { "s", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_SIN}, 0, 0, A, "type" },
  61. { "phase", "swept wave percentage phase-shift for multi-channel", OFFSET(channel_phase), AV_OPT_TYPE_DOUBLE, {.dbl=25}, 0, 100, A },
  62. { "interp", "delay-line interpolation", OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, A, "itype" },
  63. { "linear", NULL, 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATION_LINEAR}, 0, 0, A, "itype" },
  64. { "quadratic", NULL, 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATION_QUADRATIC}, 0, 0, A, "itype" },
  65. { NULL }
  66. };
  67. AVFILTER_DEFINE_CLASS(flanger);
  68. static int init(AVFilterContext *ctx)
  69. {
  70. FlangerContext *s = ctx->priv;
  71. s->feedback_gain /= 100;
  72. s->delay_gain /= 100;
  73. s->channel_phase /= 100;
  74. s->delay_min /= 1000;
  75. s->delay_depth /= 1000;
  76. s->in_gain = 1 / (1 + s->delay_gain);
  77. s->delay_gain /= 1 + s->delay_gain;
  78. s->delay_gain *= 1 - fabs(s->feedback_gain);
  79. return 0;
  80. }
  81. static int query_formats(AVFilterContext *ctx)
  82. {
  83. AVFilterChannelLayouts *layouts;
  84. AVFilterFormats *formats;
  85. static const enum AVSampleFormat sample_fmts[] = {
  86. AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE
  87. };
  88. int ret;
  89. layouts = ff_all_channel_counts();
  90. if (!layouts)
  91. return AVERROR(ENOMEM);
  92. ret = ff_set_common_channel_layouts(ctx, layouts);
  93. if (ret < 0)
  94. return ret;
  95. formats = ff_make_format_list(sample_fmts);
  96. if (!formats)
  97. return AVERROR(ENOMEM);
  98. ret = ff_set_common_formats(ctx, formats);
  99. if (ret < 0)
  100. return ret;
  101. formats = ff_all_samplerates();
  102. if (!formats)
  103. return AVERROR(ENOMEM);
  104. return ff_set_common_samplerates(ctx, formats);
  105. }
  106. static int config_input(AVFilterLink *inlink)
  107. {
  108. AVFilterContext *ctx = inlink->dst;
  109. FlangerContext *s = ctx->priv;
  110. s->max_samples = (s->delay_min + s->delay_depth) * inlink->sample_rate + 2.5;
  111. s->lfo_length = inlink->sample_rate / s->speed;
  112. s->delay_last = av_calloc(inlink->channels, sizeof(*s->delay_last));
  113. s->lfo = av_calloc(s->lfo_length, sizeof(*s->lfo));
  114. if (!s->lfo || !s->delay_last)
  115. return AVERROR(ENOMEM);
  116. ff_generate_wave_table(s->wave_shape, AV_SAMPLE_FMT_FLT, s->lfo, s->lfo_length,
  117. rint(s->delay_min * inlink->sample_rate),
  118. s->max_samples - 2., 3 * M_PI_2);
  119. return av_samples_alloc_array_and_samples(&s->delay_buffer, NULL,
  120. inlink->channels, s->max_samples,
  121. inlink->format, 0);
  122. }
  123. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  124. {
  125. AVFilterContext *ctx = inlink->dst;
  126. FlangerContext *s = ctx->priv;
  127. AVFrame *out_frame;
  128. int chan, i;
  129. if (av_frame_is_writable(frame)) {
  130. out_frame = frame;
  131. } else {
  132. out_frame = ff_get_audio_buffer(ctx->outputs[0], frame->nb_samples);
  133. if (!out_frame) {
  134. av_frame_free(&frame);
  135. return AVERROR(ENOMEM);
  136. }
  137. av_frame_copy_props(out_frame, frame);
  138. }
  139. for (i = 0; i < frame->nb_samples; i++) {
  140. s->delay_buf_pos = (s->delay_buf_pos + s->max_samples - 1) % s->max_samples;
  141. for (chan = 0; chan < inlink->channels; chan++) {
  142. double *src = (double *)frame->extended_data[chan];
  143. double *dst = (double *)out_frame->extended_data[chan];
  144. double delayed_0, delayed_1;
  145. double delayed;
  146. double in, out;
  147. int channel_phase = chan * s->lfo_length * s->channel_phase + .5;
  148. double delay = s->lfo[(s->lfo_pos + channel_phase) % s->lfo_length];
  149. int int_delay = (int)delay;
  150. double frac_delay = modf(delay, &delay);
  151. double *delay_buffer = (double *)s->delay_buffer[chan];
  152. in = src[i];
  153. delay_buffer[s->delay_buf_pos] = in + s->delay_last[chan] *
  154. s->feedback_gain;
  155. delayed_0 = delay_buffer[(s->delay_buf_pos + int_delay++) % s->max_samples];
  156. delayed_1 = delay_buffer[(s->delay_buf_pos + int_delay++) % s->max_samples];
  157. if (s->interpolation == INTERPOLATION_LINEAR) {
  158. delayed = delayed_0 + (delayed_1 - delayed_0) * frac_delay;
  159. } else {
  160. double a, b;
  161. double delayed_2 = delay_buffer[(s->delay_buf_pos + int_delay++) % s->max_samples];
  162. delayed_2 -= delayed_0;
  163. delayed_1 -= delayed_0;
  164. a = delayed_2 * .5 - delayed_1;
  165. b = delayed_1 * 2 - delayed_2 *.5;
  166. delayed = delayed_0 + (a * frac_delay + b) * frac_delay;
  167. }
  168. s->delay_last[chan] = delayed;
  169. out = in * s->in_gain + delayed * s->delay_gain;
  170. dst[i] = out;
  171. }
  172. s->lfo_pos = (s->lfo_pos + 1) % s->lfo_length;
  173. }
  174. if (frame != out_frame)
  175. av_frame_free(&frame);
  176. return ff_filter_frame(ctx->outputs[0], out_frame);
  177. }
  178. static av_cold void uninit(AVFilterContext *ctx)
  179. {
  180. FlangerContext *s = ctx->priv;
  181. av_freep(&s->lfo);
  182. av_freep(&s->delay_last);
  183. if (s->delay_buffer)
  184. av_freep(&s->delay_buffer[0]);
  185. av_freep(&s->delay_buffer);
  186. }
  187. static const AVFilterPad flanger_inputs[] = {
  188. {
  189. .name = "default",
  190. .type = AVMEDIA_TYPE_AUDIO,
  191. .config_props = config_input,
  192. .filter_frame = filter_frame,
  193. },
  194. { NULL }
  195. };
  196. static const AVFilterPad flanger_outputs[] = {
  197. {
  198. .name = "default",
  199. .type = AVMEDIA_TYPE_AUDIO,
  200. },
  201. { NULL }
  202. };
  203. AVFilter ff_af_flanger = {
  204. .name = "flanger",
  205. .description = NULL_IF_CONFIG_SMALL("Apply a flanging effect to the audio."),
  206. .query_formats = query_formats,
  207. .priv_size = sizeof(FlangerContext),
  208. .priv_class = &flanger_class,
  209. .init = init,
  210. .uninit = uninit,
  211. .inputs = flanger_inputs,
  212. .outputs = flanger_outputs,
  213. };