From 07f6dde3dda120a2dc8872ca2360c819ec803266 Mon Sep 17 00:00:00 2001 From: Pauli Virtanen Date: Tue, 14 Jan 2025 21:54:45 +0200 Subject: [PATCH] audioconvert: add function to get phase to resampler API Support getting any fractional phase part of the resampler delay in its API. --- spa/plugins/audioconvert/resample-native.c | 22 ++++++++++++++++++++++ spa/plugins/audioconvert/resample-peaks.c | 6 ++++++ spa/plugins/audioconvert/resample.h | 2 ++ 3 files changed, 30 insertions(+) diff --git a/spa/plugins/audioconvert/resample-native.c b/spa/plugins/audioconvert/resample-native.c index 8412127ff..bf8779cd8 100644 --- a/spa/plugins/audioconvert/resample-native.c +++ b/spa/plugins/audioconvert/resample-native.c @@ -315,6 +315,27 @@ static uint32_t impl_native_delay (struct resample *r) return d->n_taps / 2 - 1; } +static int32_t impl_native_phase_ns(struct resample *r) +{ + struct native_data *d = r->data; + + if (d->func == d->info->process_full) { + float pho = -(float)((int32_t)d->phase) / d->out_rate; + + if (pho != 0.0f) + pho += 1.0f; + return (int32_t)(pho * SPA_NSEC_PER_SEC / r->i_rate); + } else if (d->func == d->info->process_inter) { + float pho = -d->phase / d->out_rate; + + if (pho != 0.0f) + pho += 1.0f; + return (int32_t)(pho * SPA_NSEC_PER_SEC / r->i_rate); + } + + return 0; +} + int resample_native_init(struct resample *r) { struct native_data *d; @@ -331,6 +352,7 @@ int resample_native_init(struct resample *r) r->process = impl_native_process; r->reset = impl_native_reset; r->delay = impl_native_delay; + r->phase_ns = impl_native_phase_ns; q = &window_qualities[r->quality]; diff --git a/spa/plugins/audioconvert/resample-peaks.c b/spa/plugins/audioconvert/resample-peaks.c index 51c28e7b6..431260b5c 100644 --- a/spa/plugins/audioconvert/resample-peaks.c +++ b/spa/plugins/audioconvert/resample-peaks.c @@ -100,6 +100,11 @@ static void impl_peaks_reset (struct resample *r) d->i_count = d->o_count = 0; } +static int32_t impl_peaks_phase_ns (struct resample *r) +{ + return 0; +} + int resample_peaks_init(struct resample *r) { struct peaks_data *d; @@ -125,6 +130,7 @@ int resample_peaks_init(struct resample *r) r->delay = impl_peaks_delay; r->in_len = impl_peaks_in_len; r->out_len = impl_peaks_out_len; + r->phase_ns = impl_peaks_phase_ns; spa_log_debug(r->log, "peaks %p: in:%d out:%d features:%08x:%08x", r, r->i_rate, r->o_rate, r->cpu_flags, d->peaks.cpu_flags); diff --git a/spa/plugins/audioconvert/resample.h b/spa/plugins/audioconvert/resample.h index a07b559c7..86c48adc8 100644 --- a/spa/plugins/audioconvert/resample.h +++ b/spa/plugins/audioconvert/resample.h @@ -32,6 +32,7 @@ struct resample { void * SPA_RESTRICT dst[], uint32_t *out_len); void (*reset) (struct resample *r); uint32_t (*delay) (struct resample *r); + int32_t (*phase_ns) (struct resample *r); void *data; }; @@ -42,6 +43,7 @@ struct resample { #define resample_process(r,...) (r)->process(r,__VA_ARGS__) #define resample_reset(r) (r)->reset(r) #define resample_delay(r) (r)->delay(r) +#define resample_phase_ns(r) (r)->phase_ns(r) int resample_native_init(struct resample *r); int resample_peaks_init(struct resample *r);