AudioResamplerPublic.h revision c5656cc900aeb4a705e27508dd82c70030a97709
1cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung/*
2cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung * Copyright (C) 2014 The Android Open Source Project
3cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung *
4cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung * Licensed under the Apache License, Version 2.0 (the "License");
5cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung * you may not use this file except in compliance with the License.
6cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung * You may obtain a copy of the License at
7cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung *
8cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung *      http://www.apache.org/licenses/LICENSE-2.0
9cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung *
10cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung * Unless required by applicable law or agreed to in writing, software
11cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung * distributed under the License is distributed on an "AS IS" BASIS,
12cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung * See the License for the specific language governing permissions and
14cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung * limitations under the License.
15cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung */
16cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung
17cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung#ifndef ANDROID_AUDIO_RESAMPLER_PUBLIC_H
18cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung#define ANDROID_AUDIO_RESAMPLER_PUBLIC_H
19cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung
206770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung#include <stdint.h>
216770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung
22cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung// AUDIO_RESAMPLER_DOWN_RATIO_MAX is the maximum ratio between the original
23cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung// audio sample rate and the target rate when downsampling,
24cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung// as permitted in the audio framework, e.g. AudioTrack and AudioFlinger.
25cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung// In practice, it is not recommended to downsample more than 6:1
26cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung// for best audio quality, even though the audio framework permits a larger
27cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung// downsampling ratio.
28cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung// TODO: replace with an API
29cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung#define AUDIO_RESAMPLER_DOWN_RATIO_MAX 256
30cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung
316770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung// AUDIO_RESAMPLER_UP_RATIO_MAX is the maximum suggested ratio between the original
326770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung// audio sample rate and the target rate when upsampling.  It is loosely enforced by
336770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung// the system. One issue with large upsampling ratios is the approximation by
346770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung// an int32_t of the phase increments, making the resulting sample rate inexact.
356770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung#define AUDIO_RESAMPLER_UP_RATIO_MAX 65536
366770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung
37c5656cc900aeb4a705e27508dd82c70030a97709Andy Hung#define AUDIO_TIMESTRETCH_SPEED_MIN    0.5f
38c5656cc900aeb4a705e27508dd82c70030a97709Andy Hung#define AUDIO_TIMESTRETCH_SPEED_MAX    2.0f
39c5656cc900aeb4a705e27508dd82c70030a97709Andy Hung#define AUDIO_TIMESTRETCH_SPEED_NORMAL 1.0f
40c5656cc900aeb4a705e27508dd82c70030a97709Andy Hung
41c5656cc900aeb4a705e27508dd82c70030a97709Andy Hung#define AUDIO_TIMESTRETCH_PITCH_MIN    0.5f
42c5656cc900aeb4a705e27508dd82c70030a97709Andy Hung#define AUDIO_TIMESTRETCH_PITCH_MAX    2.0f
43c5656cc900aeb4a705e27508dd82c70030a97709Andy Hung#define AUDIO_TIMESTRETCH_PITCH_NORMAL 1.0f
44c5656cc900aeb4a705e27508dd82c70030a97709Andy Hung
450e48d25606c82def035ad10a5b3923767a765cddAndy Hung// Returns the source frames needed to resample to destination frames.  This is not a precise
460e48d25606c82def035ad10a5b3923767a765cddAndy Hung// value and depends on the resampler (and possibly how it handles rounding internally).
470e48d25606c82def035ad10a5b3923767a765cddAndy Hung// Nevertheless, this should be an upper bound on the requirements of the resampler.
480e48d25606c82def035ad10a5b3923767a765cddAndy Hung// If srcSampleRate and dstSampleRate are equal, then it returns destination frames, which
490e48d25606c82def035ad10a5b3923767a765cddAndy Hung// may not be true if the resampler is asynchronous.
500e48d25606c82def035ad10a5b3923767a765cddAndy Hungstatic inline size_t sourceFramesNeeded(
510e48d25606c82def035ad10a5b3923767a765cddAndy Hung        uint32_t srcSampleRate, size_t dstFramesRequired, uint32_t dstSampleRate) {
520e48d25606c82def035ad10a5b3923767a765cddAndy Hung    // +1 for rounding - always do this even if matched ratio (resampler may use phases not ratio)
530e48d25606c82def035ad10a5b3923767a765cddAndy Hung    // +1 for additional sample needed for interpolation
540e48d25606c82def035ad10a5b3923767a765cddAndy Hung    return srcSampleRate == dstSampleRate ? dstFramesRequired :
550e48d25606c82def035ad10a5b3923767a765cddAndy Hung            size_t((uint64_t)dstFramesRequired * srcSampleRate / dstSampleRate + 1 + 1);
560e48d25606c82def035ad10a5b3923767a765cddAndy Hung}
570e48d25606c82def035ad10a5b3923767a765cddAndy Hung
586770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung// An upper bound for the number of destination frames possible from srcFrames
596770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung// after sample rate conversion.  This may be used for buffer sizing.
606770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hungstatic inline size_t destinationFramesPossible(size_t srcFrames, uint32_t srcSampleRate,
616770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        uint32_t dstSampleRate) {
626770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    if (srcSampleRate == dstSampleRate) {
636770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        return srcFrames;
646770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    }
656770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    uint64_t dstFrames = (uint64_t)srcFrames * dstSampleRate / srcSampleRate;
666770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    return dstFrames > 2 ? dstFrames - 2 : 0;
676770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung}
686770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung
69cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung#endif // ANDROID_AUDIO_RESAMPLER_PUBLIC_H
70