audioconvert: add optional hilbert transform to rear channels

Add an option to do a hilbert transform on the generated rear channels
to do a 90 degree pahse shift on them. This can improve spacialization
of the rear channels.

See #861
This commit is contained in:
Wim Taymans 2021-03-09 21:07:37 +01:00
parent 5dd0a12875
commit 5b3388e4ac
9 changed files with 121 additions and 17 deletions

View file

@ -302,10 +302,10 @@ channelmix_f32_2_5p1_c(struct channelmix *mix, void * SPA_RESTRICT dst[],
lr4_process(&mix->lr4[2], d[2], d[3], v2, n_samples);
lr4_process(&mix->lr4[3], d[3], d[3], v3, n_samples);
delay_run(mix->buffer[0], &mix->pos[0], BUFFER_SIZE, mix->delay,
d[4], d[5], v4, n_samples);
delay_run(mix->buffer[1], &mix->pos[1], BUFFER_SIZE, mix->delay,
d[5], d[5], v5, n_samples);
delay_convolve_run(mix->buffer[0], &mix->pos[0], BUFFER_SIZE, mix->delay,
mix->taps, mix->n_taps, d[4], d[5], v4, n_samples);
delay_convolve_run(mix->buffer[1], &mix->pos[1], BUFFER_SIZE, mix->delay,
mix->taps, mix->n_taps, d[5], d[5], -v5, n_samples);
}
else {
for (n = 0; n < n_samples; n++) {
@ -320,10 +320,10 @@ channelmix_f32_2_5p1_c(struct channelmix *mix, void * SPA_RESTRICT dst[],
lr4_process(&mix->lr4[2], d[2], d[3], v2, n_samples);
lr4_process(&mix->lr4[3], d[3], d[3], v3, n_samples);
delay_run(mix->buffer[0], &mix->pos[0], BUFFER_SIZE, mix->delay,
d[4], d[5], v4, n_samples);
delay_run(mix->buffer[1], &mix->pos[1], BUFFER_SIZE, mix->delay,
d[5], d[5], v5, n_samples);
delay_convolve_run(mix->buffer[0], &mix->pos[0], BUFFER_SIZE, mix->delay,
mix->taps, mix->n_taps, d[4], d[5], v4, n_samples);
delay_convolve_run(mix->buffer[1], &mix->pos[1], BUFFER_SIZE, mix->delay,
mix->taps, mix->n_taps, d[5], d[5], -v5, n_samples);
}
}
@ -362,10 +362,10 @@ channelmix_f32_2_7p1_c(struct channelmix *mix, void * SPA_RESTRICT dst[],
lr4_process(&mix->lr4[2], d[2], d[3], v2, n_samples);
lr4_process(&mix->lr4[3], d[3], d[3], v3, n_samples);
delay_run(mix->buffer[0], &mix->pos[0], BUFFER_SIZE, mix->delay,
d[6], d[7], v6, n_samples);
delay_run(mix->buffer[1], &mix->pos[1], BUFFER_SIZE, mix->delay,
d[7], d[7], v7, n_samples);
delay_convolve_run(mix->buffer[0], &mix->pos[0], BUFFER_SIZE, mix->delay,
mix->taps, mix->n_taps, d[6], d[7], v6, n_samples);
delay_convolve_run(mix->buffer[1], &mix->pos[1], BUFFER_SIZE, mix->delay,
mix->taps, mix->n_taps, d[7], d[7], -v7, n_samples);
}
else {
for (n = 0; n < n_samples; n++) {
@ -382,10 +382,10 @@ channelmix_f32_2_7p1_c(struct channelmix *mix, void * SPA_RESTRICT dst[],
lr4_process(&mix->lr4[2], d[2], d[3], v2, n_samples);
lr4_process(&mix->lr4[3], d[3], d[3], v3, n_samples);
delay_run(mix->buffer[0], &mix->pos[0], BUFFER_SIZE, mix->delay,
d[6], d[7], v6, n_samples);
delay_run(mix->buffer[1], &mix->pos[1], BUFFER_SIZE, mix->delay,
d[7], d[7], v7, n_samples);
delay_convolve_run(mix->buffer[0], &mix->pos[0], BUFFER_SIZE, mix->delay,
mix->taps, mix->n_taps, d[6], d[7], v6, n_samples);
delay_convolve_run(mix->buffer[1], &mix->pos[1], BUFFER_SIZE, mix->delay,
mix->taps, mix->n_taps, d[7], d[7], -v7, n_samples);
}
}

View file

@ -35,6 +35,7 @@
#define VOLUME_NORM 1.0f
#include "channelmix-ops.h"
#include "hilbert.h"
#undef SPA_LOG_TOPIC_DEFAULT
#define SPA_LOG_TOPIC_DEFAULT log_topic
@ -469,6 +470,7 @@ done:
for (j = 0; j < jc; j++)
mix->matrix_orig[i][j] /= maxsum;
}
return 0;
}
@ -550,7 +552,16 @@ int channelmix_init(struct channelmix *mix)
mix->cpu_flags = info->cpu_flags;
mix->delay = mix->rear_delay * mix->freq / 1000.0f;
spa_log_debug(mix->log, "selected %s delay:%d", info->name, mix->delay);
spa_log_debug(mix->log, "selected %s delay:%d options:%08x", info->name, mix->delay,
mix->options);
if (mix->hilbert_taps > 0) {
mix->n_taps = SPA_CLAMP(mix->hilbert_taps, 15, 255) | 1;
blackman_window(mix->taps, mix->n_taps);
hilbert_generate(mix->taps, mix->n_taps);
} else {
mix->n_taps = 1;
mix->taps[0] = 1.0f;
}
return make_matrix(mix);
}

View file

@ -48,6 +48,9 @@ extern struct spa_log_topic *log_topic;
#define BUFFER_SIZE 4096
#define BUFFER_SIZE 4096
#define MAX_TAPS 255
struct channelmix {
uint32_t src_chan;
uint32_t dst_chan;
@ -73,11 +76,14 @@ struct channelmix {
float lfe_cutoff; /* in Hz, 0 is disabled */
float rear_delay; /* in ms, 0 is disabled */
float widen; /* stereo widen. 0 is disabled */
uint32_t hilbert_taps; /* to phase shift, 0 disabled */
struct lr4 lr4[SPA_AUDIO_MAX_CHANNELS];
float buffer[2][BUFFER_SIZE];
uint32_t pos[2];
uint32_t delay;
float taps[MAX_TAPS];
uint32_t n_taps;
void (*process) (struct channelmix *mix, void * SPA_RESTRICT dst[],
const void * SPA_RESTRICT src[], uint32_t n_samples);

View file

@ -527,6 +527,15 @@ static int impl_node_enum_params(void *object, int seq,
this->mix.widen, 0.0, 1.0),
SPA_PROP_INFO_params, SPA_POD_Bool(true));
break;
case 15:
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_PropInfo, id,
SPA_PROP_INFO_name, SPA_POD_String("channelmix.hilbert-taps"),
SPA_PROP_INFO_description, SPA_POD_String("Taps for phase shift of rear"),
SPA_PROP_INFO_type, SPA_POD_CHOICE_RANGE_Int(
this->mix.hilbert_taps, 0, MAX_TAPS),
SPA_PROP_INFO_params, SPA_POD_Bool(true));
break;
default:
return 0;
}
@ -582,6 +591,8 @@ static int impl_node_enum_params(void *object, int seq,
spa_pod_builder_float(&b, this->mix.rear_delay);
spa_pod_builder_string(&b, "channelmix.stereo-widen");
spa_pod_builder_float(&b, this->mix.widen);
spa_pod_builder_string(&b, "channelmix.hilbert-taps");
spa_pod_builder_int(&b, this->mix.hilbert_taps);
spa_pod_builder_pop(&b, &f[1]);
param = spa_pod_builder_pop(&b, &f[0]);
break;
@ -621,6 +632,8 @@ static int channelmix_set_param(struct impl *this, const char *k, const char *s)
spa_atof(s, &this->mix.rear_delay);
else if (spa_streq(k, "channelmix.stereo-widen"))
spa_atof(s, &this->mix.widen);
else if (spa_streq(k, "channelmix.hilbert-taps"))
spa_atou32(s, &this->mix.hilbert_taps, 0);
else
return 0;
return 1;

View file

@ -0,0 +1,69 @@
/* Hilbert function
*
* Copyright © 2021 Wim Taymans
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef HILBERT_H
#define HILBERT_H
#ifdef __cplusplus
extern "C" {
#endif
#include <errno.h>
#include <stddef.h>
#include <math.h>
static inline void blackman_window(float *taps, int n_taps)
{
int n;
for (n = 0; n < n_taps; n++) {
float w = 2 * M_PI * n / (n_taps-1);
taps[n] = 0.3635819 - 0.4891775 * cos(w)
+ 0.1365995 * cos(2 * w) - 0.0106411 * cos(3 * w);
}
}
static inline int hilbert_generate(float *taps, int n_taps)
{
int i;
if ((n_taps & 1) == 0)
return -EINVAL;
for (i = 0; i < n_taps; i++) {
int k = -(n_taps / 2) + i;
if (k & 1) {
float pk = M_PI * k;
taps[i] *= (1.0f - cosf(pk)) / pk;
} else {
taps[i] = 0.0f;
}
}
return 0;
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* HILBERT_H */

View file

@ -87,4 +87,5 @@ stream.properties = {
#channelmix.lfe-cutoff = 0
#channelmix.rear-delay = 12.0
#channelmix.stereo-widen = 0.0
#channelmix.hilbert-taps = 0
}

View file

@ -77,4 +77,5 @@ stream.properties = {
#channelmix.lfe-cutoff = 0
#channelmix.rear-delay = 12.0
#channelmix.stereo-widen = 0.0
#channelmix.hilbert-taps = 0
}

View file

@ -208,6 +208,7 @@ context.objects = [
#channelmix.lfe-cutoff = 0
#channelmix.rear-delay = 12.0
#channelmix.stereo-widen = 0.0
#channelmix.hilbert-taps = 0
channelmix.disable = true
#node.param.Props = {
# params = [
@ -265,6 +266,7 @@ context.objects = [
#channelmix.lfe-cutoff = 0
#channelmix.rear-delay = 12.0
#channelmix.stereo-widen = 0.0
#channelmix.hilbert-taps = 0
channelmix.disable = true
#node.param.Props = {
# params = [

View file

@ -87,6 +87,7 @@ stream.properties = {
#channelmix.lfe-cutoff = 0
#channelmix.rear-delay = 12.0
#channelmix.stereo-widen = 0.0
#channelmix.hilbert-taps = 0
}
# client/stream specific properties