asrc_sine.c 7.7 KB

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