af_apulsator.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (c) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen and others
  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/avassert.h"
  21. #include "libavutil/opt.h"
  22. #include "avfilter.h"
  23. #include "internal.h"
  24. #include "audio.h"
  25. enum PulsatorModes { SINE, TRIANGLE, SQUARE, SAWUP, SAWDOWN, NB_MODES };
  26. enum PulsatorTimings { UNIT_BPM, UNIT_MS, UNIT_HZ, NB_TIMINGS };
  27. typedef struct SimpleLFO {
  28. double phase;
  29. double freq;
  30. double offset;
  31. double amount;
  32. double pwidth;
  33. int mode;
  34. int srate;
  35. } SimpleLFO;
  36. typedef struct AudioPulsatorContext {
  37. const AVClass *class;
  38. int mode;
  39. double level_in;
  40. double level_out;
  41. double amount;
  42. double offset_l;
  43. double offset_r;
  44. double pwidth;
  45. double bpm;
  46. double hertz;
  47. int ms;
  48. int timing;
  49. SimpleLFO lfoL, lfoR;
  50. } AudioPulsatorContext;
  51. #define OFFSET(x) offsetof(AudioPulsatorContext, x)
  52. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  53. static const AVOption apulsator_options[] = {
  54. { "level_in", "set input gain", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, FLAGS, },
  55. { "level_out", "set output gain", OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, FLAGS, },
  56. { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=SINE}, SINE, NB_MODES-1, FLAGS, "mode" },
  57. { "sine", NULL, 0, AV_OPT_TYPE_CONST, {.i64=SINE}, 0, 0, FLAGS, "mode" },
  58. { "triangle", NULL, 0, AV_OPT_TYPE_CONST, {.i64=TRIANGLE},0, 0, FLAGS, "mode" },
  59. { "square", NULL, 0, AV_OPT_TYPE_CONST, {.i64=SQUARE}, 0, 0, FLAGS, "mode" },
  60. { "sawup", NULL, 0, AV_OPT_TYPE_CONST, {.i64=SAWUP}, 0, 0, FLAGS, "mode" },
  61. { "sawdown", NULL, 0, AV_OPT_TYPE_CONST, {.i64=SAWDOWN}, 0, 0, FLAGS, "mode" },
  62. { "amount", "set modulation", OFFSET(amount), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, FLAGS },
  63. { "offset_l", "set offset L", OFFSET(offset_l), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 1, FLAGS },
  64. { "offset_r", "set offset R", OFFSET(offset_r), AV_OPT_TYPE_DOUBLE, {.dbl=.5}, 0, 1, FLAGS },
  65. { "width", "set pulse width", OFFSET(pwidth), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 2, FLAGS },
  66. { "timing", "set timing", OFFSET(timing), AV_OPT_TYPE_INT, {.i64=2}, 0, NB_TIMINGS-1, FLAGS, "timing" },
  67. { "bpm", NULL, 0, AV_OPT_TYPE_CONST, {.i64=UNIT_BPM}, 0, 0, FLAGS, "timing" },
  68. { "ms", NULL, 0, AV_OPT_TYPE_CONST, {.i64=UNIT_MS}, 0, 0, FLAGS, "timing" },
  69. { "hz", NULL, 0, AV_OPT_TYPE_CONST, {.i64=UNIT_HZ}, 0, 0, FLAGS, "timing" },
  70. { "bpm", "set BPM", OFFSET(bpm), AV_OPT_TYPE_DOUBLE, {.dbl=120}, 30, 300, FLAGS },
  71. { "ms", "set ms", OFFSET(ms), AV_OPT_TYPE_INT, {.i64=500}, 10, 2000, FLAGS },
  72. { "hz", "set frequency", OFFSET(hertz), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0.01, 100, FLAGS },
  73. { NULL }
  74. };
  75. AVFILTER_DEFINE_CLASS(apulsator);
  76. static void lfo_advance(SimpleLFO *lfo, unsigned count)
  77. {
  78. lfo->phase = fabs(lfo->phase + count * lfo->freq / lfo->srate);
  79. if (lfo->phase >= 1)
  80. lfo->phase = fmod(lfo->phase, 1);
  81. }
  82. static double lfo_get_value(SimpleLFO *lfo)
  83. {
  84. double phs = FFMIN(100, lfo->phase / FFMIN(1.99, FFMAX(0.01, lfo->pwidth)) + lfo->offset);
  85. double val;
  86. if (phs > 1)
  87. phs = fmod(phs, 1.);
  88. switch (lfo->mode) {
  89. case SINE:
  90. val = sin(phs * 2 * M_PI);
  91. break;
  92. case TRIANGLE:
  93. if (phs > 0.75)
  94. val = (phs - 0.75) * 4 - 1;
  95. else if (phs > 0.25)
  96. val = -4 * phs + 2;
  97. else
  98. val = phs * 4;
  99. break;
  100. case SQUARE:
  101. val = phs < 0.5 ? -1 : +1;
  102. break;
  103. case SAWUP:
  104. val = phs * 2 - 1;
  105. break;
  106. case SAWDOWN:
  107. val = 1 - phs * 2;
  108. break;
  109. default: av_assert0(0);
  110. }
  111. return val * lfo->amount;
  112. }
  113. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  114. {
  115. AVFilterContext *ctx = inlink->dst;
  116. AVFilterLink *outlink = ctx->outputs[0];
  117. AudioPulsatorContext *s = ctx->priv;
  118. const double *src = (const double *)in->data[0];
  119. const int nb_samples = in->nb_samples;
  120. const double level_out = s->level_out;
  121. const double level_in = s->level_in;
  122. const double amount = s->amount;
  123. AVFrame *out;
  124. double *dst;
  125. int n;
  126. if (av_frame_is_writable(in)) {
  127. out = in;
  128. } else {
  129. out = ff_get_audio_buffer(inlink, in->nb_samples);
  130. if (!out) {
  131. av_frame_free(&in);
  132. return AVERROR(ENOMEM);
  133. }
  134. av_frame_copy_props(out, in);
  135. }
  136. dst = (double *)out->data[0];
  137. for (n = 0; n < nb_samples; n++) {
  138. double outL;
  139. double outR;
  140. double inL = src[0] * level_in;
  141. double inR = src[1] * level_in;
  142. double procL = inL;
  143. double procR = inR;
  144. procL *= lfo_get_value(&s->lfoL) * 0.5 + amount / 2;
  145. procR *= lfo_get_value(&s->lfoR) * 0.5 + amount / 2;
  146. outL = procL + inL * (1 - amount);
  147. outR = procR + inR * (1 - amount);
  148. outL *= level_out;
  149. outR *= level_out;
  150. dst[0] = outL;
  151. dst[1] = outR;
  152. lfo_advance(&s->lfoL, 1);
  153. lfo_advance(&s->lfoR, 1);
  154. dst += 2;
  155. src += 2;
  156. }
  157. if (in != out)
  158. av_frame_free(&in);
  159. return ff_filter_frame(outlink, out);
  160. }
  161. static int query_formats(AVFilterContext *ctx)
  162. {
  163. AVFilterChannelLayouts *layout = NULL;
  164. AVFilterFormats *formats = NULL;
  165. int ret;
  166. if ((ret = ff_add_format (&formats, AV_SAMPLE_FMT_DBL )) < 0 ||
  167. (ret = ff_set_common_formats (ctx , formats )) < 0 ||
  168. (ret = ff_add_channel_layout (&layout , AV_CH_LAYOUT_STEREO)) < 0 ||
  169. (ret = ff_set_common_channel_layouts (ctx , layout )) < 0)
  170. return ret;
  171. formats = ff_all_samplerates();
  172. return ff_set_common_samplerates(ctx, formats);
  173. }
  174. static int config_input(AVFilterLink *inlink)
  175. {
  176. AVFilterContext *ctx = inlink->dst;
  177. AudioPulsatorContext *s = ctx->priv;
  178. double freq;
  179. switch (s->timing) {
  180. case UNIT_BPM: freq = s->bpm / 60; break;
  181. case UNIT_MS: freq = 1 / (s->ms / 1000.); break;
  182. case UNIT_HZ: freq = s->hertz; break;
  183. default: av_assert0(0);
  184. }
  185. s->lfoL.freq = freq;
  186. s->lfoR.freq = freq;
  187. s->lfoL.mode = s->mode;
  188. s->lfoR.mode = s->mode;
  189. s->lfoL.offset = s->offset_l;
  190. s->lfoR.offset = s->offset_r;
  191. s->lfoL.srate = inlink->sample_rate;
  192. s->lfoR.srate = inlink->sample_rate;
  193. s->lfoL.amount = s->amount;
  194. s->lfoR.amount = s->amount;
  195. s->lfoL.pwidth = s->pwidth;
  196. s->lfoR.pwidth = s->pwidth;
  197. return 0;
  198. }
  199. static const AVFilterPad inputs[] = {
  200. {
  201. .name = "default",
  202. .type = AVMEDIA_TYPE_AUDIO,
  203. .config_props = config_input,
  204. .filter_frame = filter_frame,
  205. },
  206. { NULL }
  207. };
  208. static const AVFilterPad outputs[] = {
  209. {
  210. .name = "default",
  211. .type = AVMEDIA_TYPE_AUDIO,
  212. },
  213. { NULL }
  214. };
  215. AVFilter ff_af_apulsator = {
  216. .name = "apulsator",
  217. .description = NULL_IF_CONFIG_SMALL("Audio pulsator."),
  218. .priv_size = sizeof(AudioPulsatorContext),
  219. .priv_class = &apulsator_class,
  220. .query_formats = query_formats,
  221. .inputs = inputs,
  222. .outputs = outputs,
  223. };