audioconvert: resampler: change resample_phase() unit to input samples

Report fractional delay in input samples instead of nsec at the nominal
input rate, as that is closer to what the value actually means.
This commit is contained in:
Pauli Virtanen 2025-01-18 12:39:21 +02:00
parent 9889d1ce0a
commit f3a9ebd569
4 changed files with 16 additions and 16 deletions

View file

@ -315,29 +315,26 @@ static uint32_t impl_native_delay (struct resample *r)
return d->n_taps / 2 - 1; return d->n_taps / 2 - 1;
} }
static int32_t impl_native_phase_ns(struct resample *r) static float impl_native_phase (struct resample *r)
{ {
struct native_data *d = r->data; struct native_data *d = r->data;
float pho = 0;
if (d->func == d->info->process_full) { if (d->func == d->info->process_full) {
float pho = -(float)((int32_t)d->phase) / d->out_rate; pho = -(float)((int32_t)d->phase) / d->out_rate;
/* XXX: this is how it seems to behave, but not clear why */ /* XXX: this is how it seems to behave, but not clear why */
if (d->hist >= d->n_taps - 1) if (d->hist >= d->n_taps - 1)
pho += 1.0f; pho += 1.0f;
return (int32_t)(pho * SPA_NSEC_PER_SEC / r->i_rate);
} else if (d->func == d->info->process_inter) { } else if (d->func == d->info->process_inter) {
float pho = -d->phase / d->out_rate; pho = -d->phase / d->out_rate;
/* XXX: this is how it seems to behave, but not clear why */ /* XXX: this is how it seems to behave, but not clear why */
if (d->hist >= d->n_taps - 1) if (d->hist >= d->n_taps - 1)
pho += 1.0f; pho += 1.0f;
return (int32_t)(pho * SPA_NSEC_PER_SEC / r->i_rate);
} }
return 0; return pho;
} }
int resample_native_init(struct resample *r) int resample_native_init(struct resample *r)
@ -356,7 +353,7 @@ int resample_native_init(struct resample *r)
r->process = impl_native_process; r->process = impl_native_process;
r->reset = impl_native_reset; r->reset = impl_native_reset;
r->delay = impl_native_delay; r->delay = impl_native_delay;
r->phase_ns = impl_native_phase_ns; r->phase = impl_native_phase;
q = &window_qualities[r->quality]; q = &window_qualities[r->quality];

View file

@ -100,7 +100,7 @@ static void impl_peaks_reset (struct resample *r)
d->i_count = d->o_count = 0; d->i_count = d->o_count = 0;
} }
static int32_t impl_peaks_phase_ns (struct resample *r) static float impl_peaks_phase (struct resample *r)
{ {
return 0; return 0;
} }
@ -130,7 +130,7 @@ int resample_peaks_init(struct resample *r)
r->delay = impl_peaks_delay; r->delay = impl_peaks_delay;
r->in_len = impl_peaks_in_len; r->in_len = impl_peaks_in_len;
r->out_len = impl_peaks_out_len; r->out_len = impl_peaks_out_len;
r->phase_ns = impl_peaks_phase_ns; r->phase = impl_peaks_phase;
spa_log_debug(r->log, "peaks %p: in:%d out:%d features:%08x:%08x", r, 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); r->i_rate, r->o_rate, r->cpu_flags, d->peaks.cpu_flags);

View file

@ -32,7 +32,10 @@ struct resample {
void * SPA_RESTRICT dst[], uint32_t *out_len); void * SPA_RESTRICT dst[], uint32_t *out_len);
void (*reset) (struct resample *r); void (*reset) (struct resample *r);
uint32_t (*delay) (struct resample *r); uint32_t (*delay) (struct resample *r);
int32_t (*phase_ns) (struct resample *r);
/** Fractional part of delay (in input samples) */
float (*phase) (struct resample *r);
void *data; void *data;
}; };
@ -43,7 +46,7 @@ struct resample {
#define resample_process(r,...) (r)->process(r,__VA_ARGS__) #define resample_process(r,...) (r)->process(r,__VA_ARGS__)
#define resample_reset(r) (r)->reset(r) #define resample_reset(r) (r)->reset(r)
#define resample_delay(r) (r)->delay(r) #define resample_delay(r) (r)->delay(r)
#define resample_phase_ns(r) (r)->phase_ns(r) #define resample_phase(r) (r)->phase(r)
int resample_native_init(struct resample *r); int resample_native_init(struct resample *r);
int resample_peaks_init(struct resample *r); int resample_peaks_init(struct resample *r);

View file

@ -222,7 +222,7 @@ static void check_delay(double rate, uint32_t out_rate, uint32_t options)
feed_sine(&r, 512, &in, &in_phase, false); feed_sine(&r, 512, &in, &in_phase, false);
/* Test delay */ /* Test delay */
expect = resample_delay(&r) + (double)resample_phase_ns(&r) * 48000 / SPA_NSEC_PER_SEC; expect = resample_delay(&r) + (double)resample_phase(&r);
out = feed_sine(&r, 256, &in, &in_phase, true); out = feed_sine(&r, 256, &in, &in_phase, true);
got = find_delay(samp_in, in, samp_out, out, out_rate / 48000.0, 100, tol); got = find_delay(samp_in, in, samp_out, out, out_rate / 48000.0, 100, tol);
@ -295,7 +295,7 @@ static void check_delay_vary_rate(double rate, double end_rate, uint32_t out_rat
feed_sine(&r, 255, &in, &in_phase, false); feed_sine(&r, 255, &in, &in_phase, false);
/* Test delay */ /* Test delay */
expect = (double)resample_delay(&r) + (double)resample_phase_ns(&r) * 48000 / SPA_NSEC_PER_SEC; expect = (double)resample_delay(&r) + (double)resample_phase(&r);
out = feed_sine(&r, 256, &in, &in_phase, true); out = feed_sine(&r, 256, &in, &in_phase, true);
got = find_delay(samp_in, in, samp_out, out, out_rate/48000.0, 100, tol); got = find_delay(samp_in, in, samp_out, out, out_rate/48000.0, 100, tol);
@ -360,7 +360,7 @@ static void run(uint32_t in_rate, uint32_t out_rate, double end_rate, double mid
feed_sine(&r, 255, &in, &in_phase, true); feed_sine(&r, 255, &in, &in_phase, true);
/* Test delay */ /* Test delay */
expect = (double)resample_delay(&r) + (double)resample_phase_ns(&r) * (double)in_rate / SPA_NSEC_PER_SEC; expect = (double)resample_delay(&r) + (double)resample_phase(&r);
out = feed_sine(&r, 256, &in, &in_phase, true); out = feed_sine(&r, 256, &in, &in_phase, true);
got = find_delay(samp_in, in, samp_out, out, ((double)out_rate)/in_rate, 100, tol); got = find_delay(samp_in, in, samp_out, out, ((double)out_rate)/in_rate, 100, tol);