From 8a4ebc7dd7cbfe98f24612aa56c386f380707879 Mon Sep 17 00:00:00 2001
From: Michael Bradshaw <mjbshaw@google.com>
Date: Tue, 8 Aug 2017 17:07:26 -0700
Subject: [PATCH] lavf: add more beep options to sine asrc

Signed-off-by: Michael Bradshaw <mjbshaw@google.com>
---
 doc/filters.texi        | 13 ++++++++++++-
 libavfilter/asrc_sine.c | 22 ++++++++++++++++------
 2 files changed, 28 insertions(+), 7 deletions(-)

diff --git a/doc/filters.texi b/doc/filters.texi
index ce7e053194..2741623688 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -4624,7 +4624,18 @@ Set the carrier frequency. Default is 440 Hz.
 
 @item beep_factor, b
 Enable a periodic beep every second with frequency @var{beep_factor} times
-the carrier frequency. Default is 0, meaning the beep is disabled.
+the carrier frequency. Default is 0, meaning the beep is disabled. If
+@var{frequency} is 0, this value is interpreted as the beep frequency (in Hertz)
+(rather than a multiplier of the @var{frequency}).
+
+@item beep_delay
+The delay for the first beep, in seconds. Default is 0.
+
+@item beep_period
+The time beriod between two beeps, in seconds. Default is 1.
+
+@item beep_duration
+The duration of a beep, in seconds. Default is 0.04.
 
 @item sample_rate, r
 Specify the sample rate, default is 44100.
diff --git a/libavfilter/asrc_sine.c b/libavfilter/asrc_sine.c
index 3a87210b4b..4f0e44caa2 100644
--- a/libavfilter/asrc_sine.c
+++ b/libavfilter/asrc_sine.c
@@ -32,6 +32,9 @@ typedef struct SineContext {
     const AVClass *class;
     double frequency;
     double beep_factor;
+    int64_t beep_delay;
+    int64_t beep_period;
+    int64_t beep_duration;
     char *samples_per_frame;
     AVExpr *samples_per_frame_expr;
     int sample_rate;
@@ -40,7 +43,6 @@ typedef struct SineContext {
     int64_t pts;
     uint32_t phi;  ///< current phase of the sine (2pi = 1<<32)
     uint32_t dphi; ///< phase increment between two samples
-    unsigned beep_period;
     unsigned beep_index;
     unsigned beep_length;
     uint32_t phi_beep;  ///< current phase of the beep
@@ -61,7 +63,7 @@ typedef struct SineContext {
     OPT_GENERIC(name, field, def, min, max, descr, DOUBLE, dbl, __VA_ARGS__)
 
 #define OPT_DUR(name, field, def, min, max, descr, ...) \
-    OPT_GENERIC(name, field, def, min, max, descr, DURATION, str, __VA_ARGS__)
+    OPT_GENERIC(name, field, def, min, max, descr, DURATION, i64, __VA_ARGS__)
 
 #define OPT_STR(name, field, def, min, max, descr, ...) \
     OPT_GENERIC(name, field, def, min, max, descr, STRING, str, __VA_ARGS__)
@@ -71,6 +73,9 @@ static const AVOption sine_options[] = {
     OPT_DBL("f",                 frequency,            440, 0, DBL_MAX,   "set the sine frequency",),
     OPT_DBL("beep_factor",       beep_factor,            0, 0, DBL_MAX,   "set the beep frequency factor",),
     OPT_DBL("b",                 beep_factor,            0, 0, DBL_MAX,   "set the beep frequency factor",),
+    OPT_DUR("beep_delay",        beep_delay,             0, 0, INT64_MAX,   "set the delay for the first beep",),
+    OPT_DUR("beep_period",       beep_period, AV_TIME_BASE, 1, INT64_MAX, "set the gap between beeps",),
+    OPT_DUR("beep_duration",     beep_duration, AV_TIME_BASE / 25, 1, INT64_MAX, "set the duration of a beep",),
     OPT_INT("sample_rate",       sample_rate,        44100, 1, INT_MAX,   "set the sample rate",),
     OPT_INT("r",                 sample_rate,        44100, 1, INT_MAX,   "set the sample rate",),
     OPT_DUR("duration",          duration,               0, 0, INT64_MAX, "set the audio duration",),
@@ -152,10 +157,15 @@ static av_cold int init(AVFilterContext *ctx)
     make_sin_table(sine->sin);
 
     if (sine->beep_factor) {
-        sine->beep_period = sine->sample_rate;
-        sine->beep_length = sine->beep_period / 25;
-        sine->dphi_beep = ldexp(sine->beep_factor * sine->frequency, 32) /
-                          sine->sample_rate + 0.5;
+        double beep_frequency = (sine->frequency ? sine->frequency : 1.0) *
+                                sine->beep_factor;
+        sine->beep_period = av_rescale(sine->beep_period, sine->sample_rate,
+                                       AV_TIME_BASE);
+        sine->beep_index = av_rescale(-sine->beep_delay, sine->sample_rate,
+                                      AV_TIME_BASE);
+        sine->beep_length = av_rescale(sine->beep_duration, sine->sample_rate,
+                                       AV_TIME_BASE);
+        sine->dphi_beep = ldexp(beep_frequency, 32) / sine->sample_rate + 0.5;
     }
 
     ret = av_expr_parse(&sine->samples_per_frame_expr,
-- 
2.14.0.rc1.383.gd1ce394fe2-goog

