From a0884c82a91a3688bd0f448ad4502796782db7bb Mon Sep 17 00:00:00 2001 From: Kevin Mitchell Date: Wed, 16 Mar 2016 23:37:46 -0700 Subject: audio: add af_select_best_samplerate function This function chooses the best match to a given samplerate from a provided list. This can be used, for example, by the ao to decide what samplerate to use for output. --- audio/format.c | 31 +++++++++++++++++++++++++++++++ audio/format.h | 1 + 2 files changed, 32 insertions(+) diff --git a/audio/format.c b/audio/format.c index 06360b884b..bbe0cec9bb 100644 --- a/audio/format.c +++ b/audio/format.c @@ -245,6 +245,37 @@ void af_get_best_sample_formats(int src_format, int out_formats[AF_FORMAT_COUNT] out_formats[num] = 0; } +// Return the best match to src_samplerate from the list provided in the array +// *available, which must be terminated by 0, or itself NULL. If *available is +// empty or NULL, return a negative value. Exact match to src_samplerate is +// most preferred, followed by the lowest integer multiple, followed by the +// maximum of *available. +int af_select_best_samplerate(int src_samplerate, const int *available) +{ + if (!available) + return -1; + + int min_mult_rate = INT_MAX; + int max_rate = INT_MIN; + for (int i = 0; available[i]; i++) { + if (available[i] == src_samplerate) + return available[i]; + + if (!(available[i] % src_samplerate)) + min_mult_rate = MPMIN(min_mult_rate, available[i]); + + max_rate = MPMAX(max_rate, available[i]); + } + + if (min_mult_rate < INT_MAX) + return min_mult_rate; + + if (max_rate > INT_MIN) + return max_rate; + + return -1; +} + // Return the number of samples that make up one frame in this format. // You get the byte size by multiplying them with sample size and channel count. int af_format_sample_alignment(int format) diff --git a/audio/format.h b/audio/format.h index de1ff1a30d..3d5cc1024b 100644 --- a/audio/format.h +++ b/audio/format.h @@ -76,6 +76,7 @@ int af_fmt_seconds_to_bytes(int format, float seconds, int channels, int sampler void af_fill_silence(void *dst, size_t bytes, int format); void af_get_best_sample_formats(int src_format, int out_formats[AF_FORMAT_COUNT]); +int af_select_best_samplerate(int src_sampelrate, const int *available); int af_format_sample_alignment(int format); -- cgit v1.2.3