You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
8.0KB

  1. /*
  2. * Copyright (c) 2013 Nicolas George
  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/avassert.h"
  21. #include "libavutil/channel_layout.h"
  22. #include "libavutil/opt.h"
  23. #include "audio.h"
  24. #include "avfilter.h"
  25. #include "internal.h"
  26. typedef struct {
  27. const AVClass *class;
  28. double frequency;
  29. double beep_factor;
  30. int samples_per_frame;
  31. int sample_rate;
  32. int64_t duration;
  33. int16_t *sin;
  34. int64_t pts;
  35. uint32_t phi; ///< current phase of the sine (2pi = 1<<32)
  36. uint32_t dphi; ///< phase increment between two samples
  37. unsigned beep_period;
  38. unsigned beep_index;
  39. unsigned beep_length;
  40. uint32_t phi_beep; ///< current phase of the beep
  41. uint32_t dphi_beep; ///< phase increment of the beep
  42. } SineContext;
  43. #define CONTEXT SineContext
  44. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  45. #define OPT_GENERIC(name, field, def, min, max, descr, type, deffield, ...) \
  46. { name, descr, offsetof(CONTEXT, field), AV_OPT_TYPE_ ## type, \
  47. { .deffield = def }, min, max, FLAGS, __VA_ARGS__ }
  48. #define OPT_INT(name, field, def, min, max, descr, ...) \
  49. OPT_GENERIC(name, field, def, min, max, descr, INT, i64, __VA_ARGS__)
  50. #define OPT_DBL(name, field, def, min, max, descr, ...) \
  51. OPT_GENERIC(name, field, def, min, max, descr, DOUBLE, dbl, __VA_ARGS__)
  52. #define OPT_DUR(name, field, def, min, max, descr, ...) \
  53. OPT_GENERIC(name, field, def, min, max, descr, DURATION, str, __VA_ARGS__)
  54. static const AVOption sine_options[] = {
  55. OPT_DBL("frequency", frequency, 440, 0, INFINITY, "set the sine frequency"),
  56. OPT_DBL("f", frequency, 440, 0, INFINITY, "set the sine frequency"),
  57. OPT_DBL("beep_factor", beep_factor, 0, 0, INFINITY, "set the beep fequency factor"),
  58. OPT_DBL("b", beep_factor, 0, 0, INFINITY, "set the beep fequency factor"),
  59. OPT_INT("sample_rate", sample_rate, 44100, 1, INT_MAX, "set the sample rate"),
  60. OPT_INT("r", sample_rate, 44100, 1, INT_MAX, "set the sample rate"),
  61. OPT_DUR("duration", duration, 0, 0, INT64_MAX, "set the audio duration"),
  62. OPT_DUR("d", duration, 0, 0, INT64_MAX, "set the audio duration"),
  63. OPT_INT("samples_per_frame", samples_per_frame, 1024, 0, INT_MAX, "set the number of samples per frame"),
  64. {NULL},
  65. };
  66. AVFILTER_DEFINE_CLASS(sine);
  67. #define LOG_PERIOD 15
  68. #define AMPLITUDE 4095
  69. #define AMPLITUDE_SHIFT 3
  70. static void make_sin_table(int16_t *sin)
  71. {
  72. unsigned half_pi = 1 << (LOG_PERIOD - 2);
  73. unsigned ampls = AMPLITUDE << AMPLITUDE_SHIFT;
  74. uint64_t unit2 = (uint64_t)(ampls * ampls) << 32;
  75. unsigned step, i, c, s, k, new_k, n2;
  76. /* Principle: if u = exp(i*a1) and v = exp(i*a2), then
  77. exp(i*(a1+a2)/2) = (u+v) / length(u+v) */
  78. sin[0] = 0;
  79. sin[half_pi] = ampls;
  80. for (step = half_pi; step > 1; step /= 2) {
  81. /* k = (1 << 16) * amplitude / length(u+v)
  82. In exact values, k is constant at a given step */
  83. k = 0x10000;
  84. for (i = 0; i < half_pi / 2; i += step) {
  85. s = sin[i] + sin[i + step];
  86. c = sin[half_pi - i] + sin[half_pi - i - step];
  87. n2 = s * s + c * c;
  88. /* Newton's method to solve n² * k² = unit² */
  89. while (1) {
  90. new_k = (k + unit2 / ((uint64_t)k * n2) + 1) >> 1;
  91. if (k == new_k)
  92. break;
  93. k = new_k;
  94. }
  95. sin[i + step / 2] = (k * s + 0x7FFF) >> 16;
  96. sin[half_pi - i - step / 2] = (k * c + 0x8000) >> 16;
  97. }
  98. }
  99. /* Unshift amplitude */
  100. for (i = 0; i <= half_pi; i++)
  101. sin[i] = (sin[i] + (1 << (AMPLITUDE_SHIFT - 1))) >> AMPLITUDE_SHIFT;
  102. /* Use symmetries to fill the other three quarters */
  103. for (i = 0; i < half_pi; i++)
  104. sin[half_pi * 2 - i] = sin[i];
  105. for (i = 0; i < 2 * half_pi; i++)
  106. sin[i + 2 * half_pi] = -sin[i];
  107. }
  108. static av_cold int init(AVFilterContext *ctx, const char *args)
  109. {
  110. SineContext *sine = ctx->priv;
  111. static const char *shorthand[] = { "frequency", "beep_factor", NULL };
  112. int ret;
  113. sine->class = &sine_class;
  114. av_opt_set_defaults(sine);
  115. if ((ret = av_opt_set_from_string(sine, args, shorthand, "=", ":")) < 0)
  116. return ret;
  117. if (!(sine->sin = av_malloc(sizeof(*sine->sin) << LOG_PERIOD)))
  118. return AVERROR(ENOMEM);
  119. sine->dphi = ldexp(sine->frequency, 32) / sine->sample_rate + 0.5;
  120. make_sin_table(sine->sin);
  121. if (sine->beep_factor) {
  122. sine->beep_period = sine->sample_rate;
  123. sine->beep_length = sine->beep_period / 25;
  124. sine->dphi_beep = ldexp(sine->beep_factor * sine->frequency, 32) /
  125. sine->sample_rate + 0.5;
  126. }
  127. return 0;
  128. }
  129. static av_cold void uninit(AVFilterContext *ctx)
  130. {
  131. SineContext *sine = ctx->priv;
  132. av_freep(&sine->sin);
  133. }
  134. static av_cold int query_formats(AVFilterContext *ctx)
  135. {
  136. SineContext *sine = ctx->priv;
  137. static const int64_t chlayouts[] = { AV_CH_LAYOUT_MONO, -1 };
  138. int sample_rates[] = { sine->sample_rate, -1 };
  139. static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16,
  140. AV_SAMPLE_FMT_NONE };
  141. ff_set_common_formats (ctx, ff_make_format_list(sample_fmts));
  142. ff_set_common_channel_layouts(ctx, avfilter_make_format64_list(chlayouts));
  143. ff_set_common_samplerates(ctx, ff_make_format_list(sample_rates));
  144. return 0;
  145. }
  146. static av_cold int config_props(AVFilterLink *outlink)
  147. {
  148. SineContext *sine = outlink->src->priv;
  149. sine->duration = av_rescale(sine->duration, sine->sample_rate, AV_TIME_BASE);
  150. return 0;
  151. }
  152. static int request_frame(AVFilterLink *outlink)
  153. {
  154. SineContext *sine = outlink->src->priv;
  155. AVFrame *frame;
  156. int i, nb_samples = sine->samples_per_frame;
  157. int16_t *samples;
  158. if (sine->duration) {
  159. nb_samples = FFMIN(nb_samples, sine->duration - sine->pts);
  160. av_assert1(nb_samples >= 0);
  161. if (!nb_samples)
  162. return AVERROR_EOF;
  163. }
  164. if (!(frame = ff_get_audio_buffer(outlink, nb_samples)))
  165. return AVERROR(ENOMEM);
  166. samples = (int16_t *)frame->data[0];
  167. for (i = 0; i < nb_samples; i++) {
  168. samples[i] = sine->sin[sine->phi >> (32 - LOG_PERIOD)];
  169. sine->phi += sine->dphi;
  170. if (sine->beep_index < sine->beep_length) {
  171. samples[i] += sine->sin[sine->phi_beep >> (32 - LOG_PERIOD)] << 1;
  172. sine->phi_beep += sine->dphi_beep;
  173. }
  174. if (++sine->beep_index == sine->beep_period)
  175. sine->beep_index = 0;
  176. }
  177. frame->pts = sine->pts;
  178. sine->pts += nb_samples;
  179. return ff_filter_frame(outlink, frame);
  180. }
  181. static const AVFilterPad sine_outputs[] = {
  182. {
  183. .name = "default",
  184. .type = AVMEDIA_TYPE_AUDIO,
  185. .request_frame = request_frame,
  186. .config_props = config_props,
  187. },
  188. { NULL }
  189. };
  190. AVFilter avfilter_asrc_sine = {
  191. .name = "sine",
  192. .description = NULL_IF_CONFIG_SMALL("Generate sine wave audio signal."),
  193. .query_formats = query_formats,
  194. .init = init,
  195. .uninit = uninit,
  196. .priv_size = sizeof(SineContext),
  197. .inputs = NULL,
  198. .outputs = sine_outputs,
  199. .priv_class = &sine_class,
  200. };