blob: 07d946deb9d510019b1169d91289b4805ef72784 [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 Hungc5656cc2015-03-26 19:04:33 -070037#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 Hung8edb8dc2015-03-26 19:13:55 -070045// TODO: Consider putting these inlines into a class scope
46
Andy Hung0e48d252015-01-26 11:43:15 -080047// 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.
52static 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 Hung6770c6f2015-04-07 13:43:36 -070060// An upper bound for the number of destination frames possible from srcFrames
61// after sample rate conversion. This may be used for buffer sizing.
62static 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 Hung8edb8dc2015-03-26 19:13:55 -070071static 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 Hungcd044842014-08-07 11:04:34 -070080#endif // ANDROID_AUDIO_RESAMPLER_PUBLIC_H