asrc_anoisesrc.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 License
  8. * 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
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/opt.h"
  21. #include "audio.h"
  22. #include "avfilter.h"
  23. #include "internal.h"
  24. #include "libavutil/lfg.h"
  25. #include "libavutil/random_seed.h"
  26. typedef struct {
  27. const AVClass *class;
  28. int sample_rate;
  29. double amplitude;
  30. int64_t duration;
  31. int64_t color;
  32. int64_t seed;
  33. int nb_samples;
  34. int64_t pts;
  35. int infinite;
  36. double (*filter)(double white, double *buf);
  37. double buf[7];
  38. AVLFG c;
  39. } ANoiseSrcContext;
  40. #define OFFSET(x) offsetof(ANoiseSrcContext, x)
  41. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  42. static const AVOption anoisesrc_options[] = {
  43. { "sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 15, INT_MAX, FLAGS },
  44. { "r", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 15, INT_MAX, FLAGS },
  45. { "amplitude", "set amplitude", OFFSET(amplitude), AV_OPT_TYPE_DOUBLE, {.dbl = 1.}, 0., 1., FLAGS },
  46. { "a", "set amplitude", OFFSET(amplitude), AV_OPT_TYPE_DOUBLE, {.dbl = 1.}, 0., 1., FLAGS },
  47. { "duration", "set duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX, FLAGS },
  48. { "d", "set duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX, FLAGS },
  49. { "color", "set noise color", OFFSET(color), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 2, FLAGS, "color" },
  50. { "colour", "set noise color", OFFSET(color), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 2, FLAGS, "color" },
  51. { "c", "set noise color", OFFSET(color), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 2, FLAGS, "color" },
  52. { "white", 0, 0, AV_OPT_TYPE_CONST, {.i64 = 0}, 0, 0, FLAGS, "color" },
  53. { "pink", 0, 0, AV_OPT_TYPE_CONST, {.i64 = 1}, 0, 0, FLAGS, "color" },
  54. { "brown", 0, 0, AV_OPT_TYPE_CONST, {.i64 = 2}, 0, 0, FLAGS, "color" },
  55. { "seed", "set random seed", OFFSET(seed), AV_OPT_TYPE_INT64, {.i64 = -1}, -1, UINT_MAX, FLAGS },
  56. { "s", "set random seed", OFFSET(seed), AV_OPT_TYPE_INT64, {.i64 = -1}, -1, UINT_MAX, FLAGS },
  57. { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
  58. { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
  59. {NULL}
  60. };
  61. AVFILTER_DEFINE_CLASS(anoisesrc);
  62. static av_cold int query_formats(AVFilterContext *ctx)
  63. {
  64. ANoiseSrcContext *s = ctx->priv;
  65. static const int64_t chlayouts[] = { AV_CH_LAYOUT_MONO, -1 };
  66. int sample_rates[] = { s->sample_rate, -1 };
  67. static const enum AVSampleFormat sample_fmts[] = {
  68. AV_SAMPLE_FMT_DBL,
  69. AV_SAMPLE_FMT_NONE
  70. };
  71. AVFilterFormats *formats;
  72. AVFilterChannelLayouts *layouts;
  73. int ret;
  74. formats = ff_make_format_list(sample_fmts);
  75. if (!formats)
  76. return AVERROR(ENOMEM);
  77. ret = ff_set_common_formats (ctx, formats);
  78. if (ret < 0)
  79. return ret;
  80. layouts = avfilter_make_format64_list(chlayouts);
  81. if (!layouts)
  82. return AVERROR(ENOMEM);
  83. ret = ff_set_common_channel_layouts(ctx, layouts);
  84. if (ret < 0)
  85. return ret;
  86. formats = ff_make_format_list(sample_rates);
  87. if (!formats)
  88. return AVERROR(ENOMEM);
  89. return ff_set_common_samplerates(ctx, formats);
  90. }
  91. static double white_filter(double white, double *buf)
  92. {
  93. return white;
  94. };
  95. static double pink_filter(double white, double *buf)
  96. {
  97. double pink;
  98. /* http://www.musicdsp.org/files/pink.txt */
  99. buf[0] = 0.99886 * buf[0] + white * 0.0555179;
  100. buf[1] = 0.99332 * buf[1] + white * 0.0750759;
  101. buf[2] = 0.96900 * buf[2] + white * 0.1538520;
  102. buf[3] = 0.86650 * buf[3] + white * 0.3104856;
  103. buf[4] = 0.55000 * buf[4] + white * 0.5329522;
  104. buf[5] = -0.7616 * buf[5] - white * 0.0168980;
  105. pink = buf[0] + buf[1] + buf[2] + buf[3] + buf[4] + buf[5] + buf[6] + white * 0.5362;
  106. buf[6] = white * 0.115926;
  107. return pink * 0.11;
  108. }
  109. static double brown_filter(double white, double *buf)
  110. {
  111. double brown;
  112. brown = ((0.02 * white) + buf[0]) / 1.02;
  113. buf[0] = brown;
  114. return brown * 3.5;
  115. }
  116. static av_cold int config_props(AVFilterLink *outlink)
  117. {
  118. AVFilterContext *ctx = outlink->src;
  119. ANoiseSrcContext *s = ctx->priv;
  120. if (s->seed == -1)
  121. s->seed = av_get_random_seed();
  122. av_lfg_init(&s->c, s->seed);
  123. if (s->duration == 0)
  124. s->infinite = 1;
  125. s->duration = av_rescale(s->duration, s->sample_rate, AV_TIME_BASE);
  126. switch (s->color) {
  127. case 0: s->filter = white_filter; break;
  128. case 1: s->filter = pink_filter; break;
  129. case 2: s->filter = brown_filter; break;
  130. }
  131. return 0;
  132. }
  133. static int request_frame(AVFilterLink *outlink)
  134. {
  135. AVFilterContext *ctx = outlink->src;
  136. ANoiseSrcContext *s = ctx->priv;
  137. AVFrame *frame;
  138. int nb_samples, i;
  139. double *dst;
  140. if (!s->infinite && s->duration <= 0) {
  141. return AVERROR_EOF;
  142. } else if (!s->infinite && s->duration < s->nb_samples) {
  143. nb_samples = s->duration;
  144. } else {
  145. nb_samples = s->nb_samples;
  146. }
  147. if (!(frame = ff_get_audio_buffer(outlink, nb_samples)))
  148. return AVERROR(ENOMEM);
  149. dst = (double *)frame->data[0];
  150. for (i = 0; i < nb_samples; i++) {
  151. double white;
  152. white = s->amplitude * ((2 * ((double) av_lfg_get(&s->c) / 0xffffffff)) - 1);
  153. dst[i] = s->filter(white, s->buf);
  154. }
  155. if (!s->infinite)
  156. s->duration -= nb_samples;
  157. frame->pts = s->pts;
  158. s->pts += nb_samples;
  159. return ff_filter_frame(outlink, frame);
  160. }
  161. static const AVFilterPad anoisesrc_outputs[] = {
  162. {
  163. .name = "default",
  164. .type = AVMEDIA_TYPE_AUDIO,
  165. .request_frame = request_frame,
  166. .config_props = config_props,
  167. },
  168. { NULL }
  169. };
  170. AVFilter ff_asrc_anoisesrc = {
  171. .name = "anoisesrc",
  172. .description = NULL_IF_CONFIG_SMALL("Generate a noise audio signal."),
  173. .query_formats = query_formats,
  174. .priv_size = sizeof(ANoiseSrcContext),
  175. .inputs = NULL,
  176. .outputs = anoisesrc_outputs,
  177. .priv_class = &anoisesrc_class,
  178. };