af_tremolo.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2015 Kyle Swanson <k@ylo.ph>.
  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/opt.h"
  21. #include "avfilter.h"
  22. #include "internal.h"
  23. #include "audio.h"
  24. typedef struct TremoloContext {
  25. const AVClass *class;
  26. double freq;
  27. double depth;
  28. double *table;
  29. int index;
  30. } TremoloContext;
  31. #define OFFSET(x) offsetof(TremoloContext, x)
  32. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  33. static const AVOption tremolo_options[] = {
  34. { "f", "set frequency in hertz", OFFSET(freq), AV_OPT_TYPE_DOUBLE, {.dbl = 5.0}, 0.1, 20000.0, FLAGS },
  35. { "d", "set depth as percentage", OFFSET(depth), AV_OPT_TYPE_DOUBLE, {.dbl = 0.5}, 0.0, 1.0, FLAGS },
  36. { NULL }
  37. };
  38. AVFILTER_DEFINE_CLASS(tremolo);
  39. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  40. {
  41. AVFilterContext *ctx = inlink->dst;
  42. AVFilterLink *outlink = ctx->outputs[0];
  43. TremoloContext *s = ctx->priv;
  44. const double *src = (const double *)in->data[0];
  45. const int channels = inlink->channels;
  46. const int nb_samples = in->nb_samples;
  47. AVFrame *out;
  48. double *dst;
  49. int n, c;
  50. if (av_frame_is_writable(in)) {
  51. out = in;
  52. } else {
  53. out = ff_get_audio_buffer(outlink, in->nb_samples);
  54. if (!out) {
  55. av_frame_free(&in);
  56. return AVERROR(ENOMEM);
  57. }
  58. av_frame_copy_props(out, in);
  59. }
  60. dst = (double *)out->data[0];
  61. for (n = 0; n < nb_samples; n++) {
  62. for (c = 0; c < channels; c++)
  63. dst[c] = src[c] * s->table[s->index];
  64. dst += channels;
  65. src += channels;
  66. s->index++;
  67. if (s->index >= inlink->sample_rate / s->freq)
  68. s->index = 0;
  69. }
  70. if (in != out)
  71. av_frame_free(&in);
  72. return ff_filter_frame(outlink, out);
  73. }
  74. static int query_formats(AVFilterContext *ctx)
  75. {
  76. AVFilterFormats *formats;
  77. AVFilterChannelLayouts *layouts;
  78. static const enum AVSampleFormat sample_fmts[] = {
  79. AV_SAMPLE_FMT_DBL,
  80. AV_SAMPLE_FMT_NONE
  81. };
  82. int ret;
  83. layouts = ff_all_channel_counts();
  84. if (!layouts)
  85. return AVERROR(ENOMEM);
  86. ret = ff_set_common_channel_layouts(ctx, layouts);
  87. if (ret < 0)
  88. return ret;
  89. formats = ff_make_format_list(sample_fmts);
  90. if (!formats)
  91. return AVERROR(ENOMEM);
  92. ret = ff_set_common_formats(ctx, formats);
  93. if (ret < 0)
  94. return ret;
  95. formats = ff_all_samplerates();
  96. if (!formats)
  97. return AVERROR(ENOMEM);
  98. return ff_set_common_samplerates(ctx, formats);
  99. }
  100. static av_cold void uninit(AVFilterContext *ctx)
  101. {
  102. TremoloContext *s = ctx->priv;
  103. av_freep(&s->table);
  104. }
  105. static int config_input(AVFilterLink *inlink)
  106. {
  107. AVFilterContext *ctx = inlink->dst;
  108. TremoloContext *s = ctx->priv;
  109. const double offset = 1. - s->depth / 2.;
  110. int i;
  111. s->table = av_malloc_array(inlink->sample_rate / s->freq, sizeof(*s->table));
  112. if (!s->table)
  113. return AVERROR(ENOMEM);
  114. for (i = 0; i < inlink->sample_rate / s->freq; i++) {
  115. double env = s->freq * i / inlink->sample_rate;
  116. env = sin(2 * M_PI * fmod(env + 0.25, 1.0));
  117. s->table[i] = env * (1 - fabs(offset)) + offset;
  118. }
  119. s->index = 0;
  120. return 0;
  121. }
  122. static const AVFilterPad avfilter_af_tremolo_inputs[] = {
  123. {
  124. .name = "default",
  125. .type = AVMEDIA_TYPE_AUDIO,
  126. .config_props = config_input,
  127. .filter_frame = filter_frame,
  128. },
  129. { NULL }
  130. };
  131. static const AVFilterPad avfilter_af_tremolo_outputs[] = {
  132. {
  133. .name = "default",
  134. .type = AVMEDIA_TYPE_AUDIO,
  135. },
  136. { NULL }
  137. };
  138. AVFilter ff_af_tremolo = {
  139. .name = "tremolo",
  140. .description = NULL_IF_CONFIG_SMALL("Apply tremolo effect."),
  141. .priv_size = sizeof(TremoloContext),
  142. .priv_class = &tremolo_class,
  143. .uninit = uninit,
  144. .query_formats = query_formats,
  145. .inputs = avfilter_af_tremolo_inputs,
  146. .outputs = avfilter_af_tremolo_outputs,
  147. };