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 | |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 20 | #include <stdint.h> |
| 21 | |
Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 22 | // 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 Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 31 | // 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 Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 37 | #define AUDIO_TIMESTRETCH_SPEED_MIN 0.5f |
| 38 | #define AUDIO_TIMESTRETCH_SPEED_MAX 2.0f |
| 39 | #define AUDIO_TIMESTRETCH_SPEED_NORMAL 1.0f |
| 40 | |
| 41 | #define AUDIO_TIMESTRETCH_PITCH_MIN 0.5f |
| 42 | #define AUDIO_TIMESTRETCH_PITCH_MAX 2.0f |
| 43 | #define AUDIO_TIMESTRETCH_PITCH_NORMAL 1.0f |
| 44 | |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 45 | // TODO: Consider putting these inlines into a class scope |
| 46 | |
Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 47 | // Returns the source frames needed to resample to destination frames. This is not a precise |
| 48 | // value and depends on the resampler (and possibly how it handles rounding internally). |
| 49 | // Nevertheless, this should be an upper bound on the requirements of the resampler. |
| 50 | // If srcSampleRate and dstSampleRate are equal, then it returns destination frames, which |
| 51 | // may not be true if the resampler is asynchronous. |
| 52 | static inline size_t sourceFramesNeeded( |
| 53 | uint32_t srcSampleRate, size_t dstFramesRequired, uint32_t dstSampleRate) { |
| 54 | // +1 for rounding - always do this even if matched ratio (resampler may use phases not ratio) |
| 55 | // +1 for additional sample needed for interpolation |
| 56 | return srcSampleRate == dstSampleRate ? dstFramesRequired : |
| 57 | size_t((uint64_t)dstFramesRequired * srcSampleRate / dstSampleRate + 1 + 1); |
| 58 | } |
| 59 | |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 60 | // An upper bound for the number of destination frames possible from srcFrames |
| 61 | // after sample rate conversion. This may be used for buffer sizing. |
| 62 | static inline size_t destinationFramesPossible(size_t srcFrames, uint32_t srcSampleRate, |
| 63 | uint32_t dstSampleRate) { |
| 64 | if (srcSampleRate == dstSampleRate) { |
| 65 | return srcFrames; |
| 66 | } |
| 67 | uint64_t dstFrames = (uint64_t)srcFrames * dstSampleRate / srcSampleRate; |
| 68 | return dstFrames > 2 ? dstFrames - 2 : 0; |
| 69 | } |
| 70 | |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 71 | static inline size_t sourceFramesNeededWithTimestretch( |
| 72 | uint32_t srcSampleRate, size_t dstFramesRequired, uint32_t dstSampleRate, |
| 73 | float speed) { |
| 74 | // required is the number of input frames the resampler needs |
| 75 | size_t required = sourceFramesNeeded(srcSampleRate, dstFramesRequired, dstSampleRate); |
| 76 | // to deliver this, the time stretcher requires: |
| 77 | return required * (double)speed + 1 + 1; // accounting for rounding dependencies |
| 78 | } |
| 79 | |
Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 80 | #endif // ANDROID_AUDIO_RESAMPLER_PUBLIC_H |