[pulseaudio-discuss] [PATCH 05/18] resampler: choose_work_format use switch instead of if/else

poljar (Damir Jelić) poljarinho at gmail.com
Mon Jul 15 06:48:27 PDT 2013


This way the function for choosing the work format should
be more readable and easier to expand in the future.
---
 src/pulsecore/resampler.c | 33 +++++++++++++++++++++++----------
 1 file changed, 23 insertions(+), 10 deletions(-)

diff --git a/src/pulsecore/resampler.c b/src/pulsecore/resampler.c
index 0adde29..e3be709 100644
--- a/src/pulsecore/resampler.c
+++ b/src/pulsecore/resampler.c
@@ -251,13 +251,27 @@ static pa_sample_format_t pa_resampler_choose_work_format(
     pa_assert(method >= 0);
     pa_assert(method < PA_RESAMPLER_MAX);
 
-    if ((method >= PA_RESAMPLER_SPEEX_FIXED_BASE && method <= PA_RESAMPLER_SPEEX_FIXED_MAX) ||
-        (method == PA_RESAMPLER_FFMPEG))
-        work_format = PA_SAMPLE_S16NE;
-    else if (method == PA_RESAMPLER_TRIVIAL || method == PA_RESAMPLER_COPY || method == PA_RESAMPLER_PEAKS) {
+    if (method >= PA_RESAMPLER_SPEEX_FIXED_BASE && method <= PA_RESAMPLER_SPEEX_FIXED_MAX)
+        method = PA_RESAMPLER_SPEEX_FIXED_BASE;
 
-        if (map_required || a != b || method == PA_RESAMPLER_PEAKS) {
+    switch (method) {
+        /* This block is for resampling functions that only
+         * support the S16 sample format. */
+        case PA_RESAMPLER_SPEEX_FIXED_BASE:     /* fall through */
+        case PA_RESAMPLER_FFMPEG:
+            work_format = PA_SAMPLE_S16NE;
+            break;
 
+        /* This block is for resampling functions that support
+         * any sample format. */
+        case PA_RESAMPLER_COPY:                 /* fall through */
+        case PA_RESAMPLER_TRIVIAL:
+            if (!map_required && a == b) {
+                work_format = a;
+                break;
+            }
+                                                /* Else fall trough */
+        case PA_RESAMPLER_PEAKS:
             if (a == PA_SAMPLE_S16NE || b == PA_SAMPLE_S16NE)
                 work_format = PA_SAMPLE_S16NE;
             else if (a == PA_SAMPLE_S32NE || a == PA_SAMPLE_S32RE ||
@@ -271,12 +285,11 @@ static pa_sample_format_t pa_resampler_choose_work_format(
                 work_format = PA_SAMPLE_FLOAT32NE;
             else
                 work_format = PA_SAMPLE_S16NE;
+            break;
 
-        } else
-            work_format = a;
-
-    } else
-        work_format = PA_SAMPLE_FLOAT32NE;
+        default:
+            work_format = PA_SAMPLE_FLOAT32NE;
+    }
 
     return work_format;
 }
-- 
1.8.3.2



More information about the pulseaudio-discuss mailing list