Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 20 | // AUDIO_RESAMPLER_DOWN_RATIO_MAX is the maximum ratio between the original |
| 21 | // audio sample rate and the target rate when downsampling, |
| 22 | // as permitted in the audio framework, e.g. AudioTrack and AudioFlinger. |
| 23 | // In practice, it is not recommended to downsample more than 6:1 |
| 24 | // for best audio quality, even though the audio framework permits a larger |
| 25 | // downsampling ratio. |
| 26 | // TODO: replace with an API |
| 27 | #define AUDIO_RESAMPLER_DOWN_RATIO_MAX 256 |
| 28 | |
Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 29 | // Returns the source frames needed to resample to destination frames. This is not a precise |
| 30 | // value and depends on the resampler (and possibly how it handles rounding internally). |
| 31 | // Nevertheless, this should be an upper bound on the requirements of the resampler. |
| 32 | // If srcSampleRate and dstSampleRate are equal, then it returns destination frames, which |
| 33 | // may not be true if the resampler is asynchronous. |
| 34 | static inline size_t sourceFramesNeeded( |
| 35 | uint32_t srcSampleRate, size_t dstFramesRequired, uint32_t dstSampleRate) { |
| 36 | // +1 for rounding - always do this even if matched ratio (resampler may use phases not ratio) |
| 37 | // +1 for additional sample needed for interpolation |
| 38 | return srcSampleRate == dstSampleRate ? dstFramesRequired : |
| 39 | size_t((uint64_t)dstFramesRequired * srcSampleRate / dstSampleRate + 1 + 1); |
| 40 | } |
| 41 | |
Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 42 | #endif // ANDROID_AUDIO_RESAMPLER_PUBLIC_H |