filter-graph: add normalize option for sofa

Add an option to enable the use of the sofa loudness function to
normalize the IR.

See #5322
This commit is contained in:
Wim Taymans 2026-06-25 16:24:38 +02:00
parent a5ade5f96e
commit 7dc35030dc
2 changed files with 18 additions and 2 deletions

View file

@ -50,6 +50,8 @@ static void * spatializer_instantiate(const struct spa_fga_plugin *plugin, const
const char *val; const char *val;
char key[256]; char key[256];
char filename[PATH_MAX] = ""; char filename[PATH_MAX] = "";
bool normalize = false;
float normalized;
int len; int len;
errno = EINVAL; errno = EINVAL;
@ -104,6 +106,13 @@ static void * spatializer_instantiate(const struct spa_fga_plugin *plugin, const
goto error; goto error;
} }
} }
else if (spa_streq(key, "normalize")) {
if (spa_json_parse_bool(val, len, &normalize) <= 0) {
spa_log_error(impl->log, "spatializer:normalize requires a bool");
errno = EINVAL;
goto error;
}
}
else if (spa_streq(key, "latency")) { else if (spa_streq(key, "latency")) {
if (spa_json_parse_float(val, len, &impl->latency) <= 0) { if (spa_json_parse_float(val, len, &impl->latency) <= 0) {
spa_log_error(impl->log, "spatializer:latency requires a number"); spa_log_error(impl->log, "spatializer:latency requires a number");
@ -206,8 +215,13 @@ static void * spatializer_instantiate(const struct spa_fga_plugin *plugin, const
if (impl->tailsize <= 0) if (impl->tailsize <= 0)
impl->tailsize = SPA_CLAMP(4096, impl->blocksize, 32768); impl->tailsize = SPA_CLAMP(4096, impl->blocksize, 32768);
spa_log_info(impl->log, "using n_samples:%u %d:%d blocksize gain:%f sofa:%s", impl->n_samples, if (normalize)
impl->blocksize, impl->tailsize, impl->gain, filename); normalized = mysofa_loudness(impl->sofa->hrtf);
spa_log_info(impl->log, "using n_samples:%u %d:%d blocksize %sgain:%f sofa:%s", impl->n_samples,
impl->blocksize, impl->tailsize,
normalize ? "auto" : "", normalize ? normalized : impl->gain, filename);
impl->tmp[0] = calloc(impl->plugin->quantum_limit, sizeof(float)); impl->tmp[0] = calloc(impl->plugin->quantum_limit, sizeof(float));
impl->tmp[1] = calloc(impl->plugin->quantum_limit, sizeof(float)); impl->tmp[1] = calloc(impl->plugin->quantum_limit, sizeof(float));

View file

@ -743,6 +743,7 @@ extern struct spa_handle_factory spa_filter_graph_factory;
* filename = ... * filename = ...
* gain = ... * gain = ...
* latency = ... * latency = ...
* normalize = ...
* } * }
* control = { * control = {
* "Azimuth" = ... * "Azimuth" = ...
@ -764,6 +765,7 @@ extern struct spa_handle_factory spa_filter_graph_factory;
* and contain the HRTF for the various spatial positions. * and contain the HRTF for the various spatial positions.
* - `gain` the overall gain to apply to the IR file, default 1.0. * - `gain` the overall gain to apply to the IR file, default 1.0.
* - `latency` the latency introduced by the filter, default 0 * - `latency` the latency introduced by the filter, default 0
* - `normalize` automatically normalize the loudness of the IR, default false
* *
* - `Azimuth` controls the azimuth, this is the direction the sound is coming from * - `Azimuth` controls the azimuth, this is the direction the sound is coming from
* in degrees between 0 and 360. 0 is straight ahead. 90 is left, 180 * in degrees between 0 and 360. 0 is straight ahead. 90 is left, 180