blob: 0634741c9ab942a5838919e3514b1789541edd9d [file] [log] [blame]
Andy Hungcd044842014-08-07 11:04:34 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_AUDIO_RESAMPLER_PUBLIC_H
18#define ANDROID_AUDIO_RESAMPLER_PUBLIC_H
19
Andy Hung6770c6f2015-04-07 13:43:36 -070020#include <stdint.h>
21
Andy Hungcd044842014-08-07 11:04:34 -070022// AUDIO_RESAMPLER_DOWN_RATIO_MAX is the maximum ratio between the original
23// audio sample rate and the target rate when downsampling,
24// as permitted in the audio framework, e.g. AudioTrack and AudioFlinger.
25// In practice, it is not recommended to downsample more than 6:1
26// for best audio quality, even though the audio framework permits a larger
27// downsampling ratio.
28// TODO: replace with an API
29#define AUDIO_RESAMPLER_DOWN_RATIO_MAX 256
30
Andy Hung6770c6f2015-04-07 13:43:36 -070031// AUDIO_RESAMPLER_UP_RATIO_MAX is the maximum suggested ratio between the original
32// audio sample rate and the target rate when upsampling. It is loosely enforced by
33// the system. One issue with large upsampling ratios is the approximation by
34// an int32_t of the phase increments, making the resulting sample rate inexact.
35#define AUDIO_RESAMPLER_UP_RATIO_MAX 65536
36
Andy Hung0e48d252015-01-26 11:43:15 -080037// Returns the source frames needed to resample to destination frames. This is not a precise
38// value and depends on the resampler (and possibly how it handles rounding internally).
39// Nevertheless, this should be an upper bound on the requirements of the resampler.
40// If srcSampleRate and dstSampleRate are equal, then it returns destination frames, which
41// may not be true if the resampler is asynchronous.
42static inline size_t sourceFramesNeeded(
43 uint32_t srcSampleRate, size_t dstFramesRequired, uint32_t dstSampleRate) {
44 // +1 for rounding - always do this even if matched ratio (resampler may use phases not ratio)
45 // +1 for additional sample needed for interpolation
46 return srcSampleRate == dstSampleRate ? dstFramesRequired :
47 size_t((uint64_t)dstFramesRequired * srcSampleRate / dstSampleRate + 1 + 1);
48}
49
Andy Hung6770c6f2015-04-07 13:43:36 -070050// An upper bound for the number of destination frames possible from srcFrames
51// after sample rate conversion. This may be used for buffer sizing.
52static inline size_t destinationFramesPossible(size_t srcFrames, uint32_t srcSampleRate,
53 uint32_t dstSampleRate) {
54 if (srcSampleRate == dstSampleRate) {
55 return srcFrames;
56 }
57 uint64_t dstFrames = (uint64_t)srcFrames * dstSampleRate / srcSampleRate;
58 return dstFrames > 2 ? dstFrames - 2 : 0;
59}
60
Andy Hungcd044842014-08-07 11:04:34 -070061#endif // ANDROID_AUDIO_RESAMPLER_PUBLIC_H