asrc_sine.c 9.2 KB

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