From f70d0b19af49396750f10a179b7db5dd2d06fa94 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Tue, 29 Sep 2020 12:00:46 +0200 Subject: [PATCH] channelmix: add option to NORMALIZE volumes Normalize the complete matrix with the same coefficient --- spa/plugins/audioconvert/channelmix-ops.c | 13 +++++++++---- spa/plugins/audioconvert/channelmix-ops.h | 1 + 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/spa/plugins/audioconvert/channelmix-ops.c b/spa/plugins/audioconvert/channelmix-ops.c index 7cd91bc28..51f843c4e 100644 --- a/spa/plugins/audioconvert/channelmix-ops.c +++ b/spa/plugins/audioconvert/channelmix-ops.c @@ -166,6 +166,7 @@ static int make_matrix(struct channelmix *mix) float clev = SQRT1_2; float slev = SQRT1_2; float llev = 0.5f; + float maxsum = 0.0f; spa_log_debug(mix->log, "src-mask:%08"PRIx64" dst-mask:%08"PRIx64, src_mask, dst_mask); @@ -355,7 +356,7 @@ static int make_matrix(struct channelmix *mix) } } done: - for (ic = 0, i = 0; i < NUM_CHAN; i++) { + for (jc = 0, ic = 0, i = 0; i < NUM_CHAN; i++) { float sum = 0.0f; if ((dst_mask & (1UL << (i + 2))) == 0) continue; @@ -365,11 +366,15 @@ done: mix->matrix_orig[ic][jc++] = matrix[i][j]; sum += fabs(matrix[i][j]); } - if (sum > 1.0f) - for (j = 0; j < jc; j++) - mix->matrix_orig[ic][j] /= sum; + maxsum = SPA_MAX(maxsum, sum); ic++; } + if (SPA_FLAG_IS_SET(mix->options, CHANNELMIX_OPTION_NORMALIZE) && + maxsum > 1.0f) { + for (i = 0; i < ic; i++) + for (j = 0; j < jc; j++) + mix->matrix_orig[i][j] /= maxsum; + } return 0; } diff --git a/spa/plugins/audioconvert/channelmix-ops.h b/spa/plugins/audioconvert/channelmix-ops.h index 4aac05540..a89e66a50 100644 --- a/spa/plugins/audioconvert/channelmix-ops.h +++ b/spa/plugins/audioconvert/channelmix-ops.h @@ -47,6 +47,7 @@ struct channelmix { uint64_t dst_mask; uint32_t cpu_flags; #define CHANNELMIX_OPTION_MIX_LFE (1<<0) /**< mix LFE */ +#define CHANNELMIX_OPTION_NORMALIZE (1<<1) /**< normalize volumes */ uint32_t options; struct spa_log *log;