[pulseaudio-discuss,v3,3/6] Refactor codes regarding volume ramp

Submitted by Sangchul Lee on April 4, 2017, 2:36 p.m.

Details

Message ID 1491316616-21358-4-git-send-email-sangchul1011@gmail.com
State New
Headers show
Series "volume ramping" ( rev: 3 ) in PulseAudio

Not browsing as part of any series.

Commit Message

Sangchul Lee April 4, 2017, 2:36 p.m.
mix: Use pa_sample_size_of_format function instead of format_sample_size_table.
volume, mix: Revise according to PA coding convention.
sink-input: Remove unused parameter of pa_sink_input_set_volume_ramp function.
sink-input: Use bool instead of pa_bool_t.
sink-input: Add static check_and_apply_ramp function to reduce duplicated codes.

Signed-off-by: Sangchul Lee <sc11.lee@samsung.com>
---
 src/pulse/volume.c         |  5 +----
 src/pulse/volume.h         |  6 +++---
 src/pulsecore/mix.c        | 42 +++++++++++++----------------------------
 src/pulsecore/mix.h        |  6 +++---
 src/pulsecore/sink-input.c | 47 ++++++++++++++++++++++------------------------
 src/pulsecore/sink-input.h |  3 ++-
 6 files changed, 44 insertions(+), 65 deletions(-)

Patch hide | download patch | download mbox

diff --git a/src/pulse/volume.c b/src/pulse/volume.c
index 85072c1..5f9c4ed 100644
--- a/src/pulse/volume.c
+++ b/src/pulse/volume.c
@@ -445,10 +445,7 @@  int pa_cvolume_channels_equal_to(const pa_cvolume *a, pa_volume_t v) {
     unsigned c;
     pa_assert(a);
 
-    if (pa_cvolume_valid(a) == 0)
-        abort();
-
-    /* pa_return_val_if_fail(pa_cvolume_valid(a), 0); */
+    pa_return_val_if_fail(pa_cvolume_valid(a), 0);
     pa_return_val_if_fail(PA_VOLUME_IS_VALID(v), 0);
 
     for (c = 0; c < a->channels; c++)
diff --git a/src/pulse/volume.h b/src/pulse/volume.h
index 2ae3451..1b66427 100644
--- a/src/pulse/volume.h
+++ b/src/pulse/volume.h
@@ -440,16 +440,16 @@  typedef enum pa_volume_ramp_type {
 } pa_volume_ramp_type_t;
 
 /** A structure encapsulating a volume ramp */
-typedef struct pa_volume_ramp_t {
+typedef struct pa_volume_ramp {
     pa_volume_ramp_type_t type;
     long length;
     pa_volume_t target;
-} pa_volume_ramp_t;
+} pa_volume_ramp;
 
 /** A structure encapsulating a multichannel volume ramp */
 typedef struct pa_cvolume_ramp {
     uint8_t channels;
-    pa_volume_ramp_t ramps[PA_CHANNELS_MAX];
+    pa_volume_ramp ramps[PA_CHANNELS_MAX];
 } pa_cvolume_ramp;
 
 /** Return non-zero when *a == *b */
diff --git a/src/pulsecore/mix.c b/src/pulsecore/mix.c
index 1e4cc1e..b87f9ff 100644
--- a/src/pulsecore/mix.c
+++ b/src/pulsecore/mix.c
@@ -769,23 +769,7 @@  static const pa_calc_volume_no_mapping_func_t calc_volume_table_no_mapping[] = {
   [PA_SAMPLE_S24_32BE]  = (pa_calc_volume_no_mapping_func_t) calc_linear_integer_volume_no_mapping
 };
 
-static const unsigned format_sample_size_table[] = {
-  [PA_SAMPLE_U8]        = 1,
-  [PA_SAMPLE_ALAW]      = 1,
-  [PA_SAMPLE_ULAW]      = 1,
-  [PA_SAMPLE_S16LE]     = 2,
-  [PA_SAMPLE_S16BE]     = 2,
-  [PA_SAMPLE_FLOAT32LE] = 4,
-  [PA_SAMPLE_FLOAT32BE] = 4,
-  [PA_SAMPLE_S32LE]     = 4,
-  [PA_SAMPLE_S32BE]     = 4,
-  [PA_SAMPLE_S24LE]     = 3,
-  [PA_SAMPLE_S24BE]     = 3,
-  [PA_SAMPLE_S24_32LE]  = 4,
-  [PA_SAMPLE_S24_32BE]  = 4
-};
-
-static float calc_volume_ramp_linear(pa_volume_ramp_int_t *ramp) {
+static float calc_volume_ramp_linear(pa_volume_ramp_int *ramp) {
     pa_assert(ramp);
     pa_assert(ramp->length > 0);
 
@@ -793,7 +777,7 @@  static float calc_volume_ramp_linear(pa_volume_ramp_int_t *ramp) {
     return ramp->start + (ramp->length - ramp->left) * (ramp->end - ramp->start) / (float) ramp->length;
 }
 
-static float calc_volume_ramp_logarithmic(pa_volume_ramp_int_t *ramp) {
+static float calc_volume_ramp_logarithmic(pa_volume_ramp_int *ramp) {
     float x_val, s, e;
     long temp;
 
@@ -816,7 +800,7 @@  static float calc_volume_ramp_logarithmic(pa_volume_ramp_int_t *ramp) {
     return s + x_val * (e - s) / powf(ramp->length, 10);
 }
 
-static float calc_volume_ramp_cubic(pa_volume_ramp_int_t *ramp) {
+static float calc_volume_ramp_cubic(pa_volume_ramp_int *ramp) {
     float x_val, s, e;
     long temp;
 
@@ -839,7 +823,7 @@  static float calc_volume_ramp_cubic(pa_volume_ramp_int_t *ramp) {
     return s + x_val * (e - s) / cbrtf(ramp->length);
 }
 
-typedef float (*pa_calc_volume_ramp_func_t) (pa_volume_ramp_int_t *);
+typedef float (*pa_calc_volume_ramp_func_t) (pa_volume_ramp_int *);
 
 static const pa_calc_volume_ramp_func_t calc_volume_ramp_table[] = {
     [PA_VOLUME_RAMP_TYPE_LINEAR] = (pa_calc_volume_ramp_func_t) calc_volume_ramp_linear,
@@ -847,18 +831,18 @@  static const pa_calc_volume_ramp_func_t calc_volume_ramp_table[] = {
     [PA_VOLUME_RAMP_TYPE_CUBIC] = (pa_calc_volume_ramp_func_t) calc_volume_ramp_cubic
 };
 
-static void calc_volume_ramps(pa_cvolume_ramp_int *ram, float *vol)
+static void calc_volume_ramps(pa_cvolume_ramp_int *ramp, float *vol)
 {
     int i;
 
-    for (i = 0; i < ram->channels; i++) {
-        if (ram->ramps[i].left <= 0) {
-            if (ram->ramps[i].target == PA_VOLUME_NORM) {
+    for (i = 0; i < ramp->channels; i++) {
+        if (ramp->ramps[i].left <= 0) {
+            if (ramp->ramps[i].target == PA_VOLUME_NORM) {
                 vol[i] = 1.0;
             }
         } else {
-            vol[i] = ram->ramps[i].curr = calc_volume_ramp_table[ram->ramps[i].type] (&ram->ramps[i]);
-            ram->ramps[i].left--;
+            vol[i] = ramp->ramps[i].curr = calc_volume_ramp_table[ramp->ramps[i].type] (&ramp->ramps[i]);
+            ramp->ramps[i].left--;
         }
     }
 }
@@ -880,7 +864,7 @@  void pa_volume_ramp_memchunk(
     pa_assert(pa_frame_aligned(c->length, spec));
     pa_assert(ramp);
 
-    length_in_frames = c->length / format_sample_size_table[spec->format] / spec->channels;
+    length_in_frames = c->length / pa_sample_size_of_format(spec->format) / spec->channels;
 
     if (pa_memblock_is_silence(c->memblock)) {
         for (i = 0; i < ramp->channels; i++) {
@@ -905,11 +889,11 @@  void pa_volume_ramp_memchunk(
         calc_volume_table_no_mapping[spec->format] ((void *)linear, vol, spec->channels);
 
         /* we only process one frame per iteration */
-        do_volume (ptr, (void *)linear, spec->channels, format_sample_size_table[spec->format] * spec->channels);
+        do_volume(ptr, (void *)linear, spec->channels, pa_sample_size_of_format(spec->format) * spec->channels);
 
         /* pa_log_debug("1: %d  2: %d", linear[0], linear[1]); */
 
-        ptr = (uint8_t*)ptr + format_sample_size_table[spec->format] * spec->channels;
+        ptr = (uint8_t*)ptr + pa_sample_size_of_format(spec->format) * spec->channels;
     }
 
     pa_memblock_release(c->memblock);
diff --git a/src/pulsecore/mix.h b/src/pulsecore/mix.h
index 0f86b6f..ccd8255 100644
--- a/src/pulsecore/mix.h
+++ b/src/pulsecore/mix.h
@@ -59,7 +59,7 @@  void pa_volume_memchunk(
     const pa_sample_spec *spec,
     const pa_cvolume *volume);
 
-typedef struct pa_volume_ramp_int_t {
+typedef struct pa_volume_ramp_int {
     pa_volume_ramp_type_t type;
     long length;
     long left;
@@ -67,11 +67,11 @@  typedef struct pa_volume_ramp_int_t {
     float end;
     float curr;
     pa_volume_t target;
-} pa_volume_ramp_int_t;
+} pa_volume_ramp_int;
 
 typedef struct pa_cvolume_ramp_int {
     uint8_t channels;
-    pa_volume_ramp_int_t ramps[PA_CHANNELS_MAX];
+    pa_volume_ramp_int ramps[PA_CHANNELS_MAX];
 } pa_cvolume_ramp_int;
 
 pa_cvolume_ramp_int* pa_cvolume_ramp_convert(const pa_cvolume_ramp *src, pa_cvolume_ramp_int *dst, int sample_rate);
diff --git a/src/pulsecore/sink-input.c b/src/pulsecore/sink-input.c
index ffa877b..f18e4f9 100644
--- a/src/pulsecore/sink-input.c
+++ b/src/pulsecore/sink-input.c
@@ -853,6 +853,25 @@  pa_usec_t pa_sink_input_get_latency(pa_sink_input *i, pa_usec_t *sink_latency) {
     return r[0];
 }
 
+/* Called from main context */
+static void check_and_apply_ramp(pa_cvolume_ramp_int *ramp, const pa_sample_spec *spec, pa_memchunk *chunk) {
+    pa_cvolume target;
+
+    pa_assert(ramp);
+    pa_assert(spec);
+    pa_assert(chunk);
+
+    /* check for possible volume ramp and apply it */
+    if (pa_cvolume_ramp_active(ramp)) {
+        pa_memchunk_make_writable(chunk, 0);
+        pa_volume_ramp_memchunk(chunk, spec, ramp);
+    } else if (pa_cvolume_ramp_target_active(ramp)) {
+        pa_memchunk_make_writable(chunk, 0);
+        pa_cvolume_ramp_get_targets(ramp, &target);
+        pa_volume_memchunk(chunk, spec, &target);
+    }
+}
+
 /* Called from thread context */
 void pa_sink_input_peek(pa_sink_input *i, size_t slength /* in sink bytes */, pa_memchunk *chunk, pa_cvolume *volume) {
     bool do_volume_adj_here, need_volume_factor_sink;
@@ -942,8 +961,6 @@  void pa_sink_input_peek(pa_sink_input *i, size_t slength /* in sink bytes */, pa
         while (tchunk.length > 0) {
             pa_memchunk wchunk;
             bool nvfs = need_volume_factor_sink;
-            pa_cvolume target;
-            pa_bool_t tmp;
 
             wchunk = tchunk;
             pa_memblock_ref(wchunk.memblock);
@@ -980,16 +997,7 @@  void pa_sink_input_peek(pa_sink_input *i, size_t slength /* in sink bytes */, pa
                     pa_volume_memchunk(&wchunk, &i->sink->sample_spec, &i->volume_factor_sink);
                 }
 
-                /* check for possible volume ramp */
-                if (pa_cvolume_ramp_active(&i->thread_info.ramp)) {
-                    pa_memchunk_make_writable(&wchunk, 0);
-                    pa_volume_ramp_memchunk(&wchunk, &i->sink->sample_spec, &(i->thread_info.ramp));
-                } else if ((tmp = pa_cvolume_ramp_target_active(&(i->thread_info.ramp)))) {
-                    pa_memchunk_make_writable(&wchunk, 0);
-                    pa_cvolume_ramp_get_targets(&i->thread_info.ramp, &target);
-                    pa_volume_memchunk(&wchunk, &i->sink->sample_spec, &target);
-                }
-
+                check_and_apply_ramp(&i->thread_info.ramp, &i->sink->sample_spec, &wchunk);
                 pa_memblockq_push_align(i->thread_info.render_memblockq, &wchunk);
             } else {
                 pa_memchunk rchunk;
@@ -1006,16 +1014,7 @@  void pa_sink_input_peek(pa_sink_input *i, size_t slength /* in sink bytes */, pa
                         pa_volume_memchunk(&rchunk, &i->sink->sample_spec, &i->volume_factor_sink);
                     }
 
-                    /* check for possible volume ramp */
-                    if (pa_cvolume_ramp_active(&(i->thread_info.ramp))) {
-                        pa_memchunk_make_writable(&rchunk, 0);
-                        pa_volume_ramp_memchunk(&rchunk, &i->sink->sample_spec, &(i->thread_info.ramp));
-                    } else if (pa_cvolume_ramp_target_active(&(i->thread_info.ramp))) {
-                        pa_memchunk_make_writable(&rchunk, 0);
-                        pa_cvolume_ramp_get_targets(&i->thread_info.ramp, &target);
-                        pa_volume_memchunk(&rchunk, &i->sink->sample_spec, &target);
-                    }
-
+                    check_and_apply_ramp(&i->thread_info.ramp, &i->sink->sample_spec, &wchunk);
                     pa_memblockq_push_align(i->thread_info.render_memblockq, &rchunk);
                     pa_memblock_unref(rchunk.memblock);
                 }
@@ -1384,9 +1383,7 @@  int pa_sink_input_remove_volume_factor(pa_sink_input *i, const char *key) {
 void pa_sink_input_set_volume_ramp(
         pa_sink_input *i,
         const pa_cvolume_ramp *ramp,
-        pa_bool_t send_msg,
-        pa_bool_t save) {
-
+        bool send_msg) {
     pa_sink_input_assert_ref(i);
     pa_assert_ctl_context();
     pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
diff --git a/src/pulsecore/sink-input.h b/src/pulsecore/sink-input.h
index 243e93a..c994ef7 100644
--- a/src/pulsecore/sink-input.h
+++ b/src/pulsecore/sink-input.h
@@ -32,6 +32,7 @@ 
 #include <pulsecore/client.h>
 #include <pulsecore/sink.h>
 #include <pulsecore/core.h>
+#include <pulsecore/mix.h>
 
 typedef enum pa_sink_input_state {
     PA_SINK_INPUT_INIT,         /*< The stream is not active yet, because pa_sink_input_put() has not been called yet */
@@ -375,7 +376,7 @@  void pa_sink_input_set_volume(pa_sink_input *i, const pa_cvolume *volume, bool s
 void pa_sink_input_add_volume_factor(pa_sink_input *i, const char *key, const pa_cvolume *volume_factor);
 int pa_sink_input_remove_volume_factor(pa_sink_input *i, const char *key);
 pa_cvolume *pa_sink_input_get_volume(pa_sink_input *i, pa_cvolume *volume, bool absolute);
-void pa_sink_input_set_volume_ramp(pa_sink_input *i, const pa_cvolume_ramp *ramp, pa_bool_t send_msg, pa_bool_t save);
+void pa_sink_input_set_volume_ramp(pa_sink_input *i, const pa_cvolume_ramp *ramp, bool send_msg);
 
 void pa_sink_input_set_mute(pa_sink_input *i, bool mute, bool save);