165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian/* 265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * Copyright (C) 2007 The Android Open Source Project 365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * 465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * Licensed under the Apache License, Version 2.0 (the "License"); 565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * you may not use this file except in compliance with the License. 665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * You may obtain a copy of the License at 765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * 865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * http://www.apache.org/licenses/LICENSE-2.0 965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * 1065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * Unless required by applicable law or agreed to in writing, software 1165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * distributed under the License is distributed on an "AS IS" BASIS, 1265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * See the License for the specific language governing permissions and 1465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * limitations under the License. 1565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian */ 1665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 1765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#ifndef ANDROID_AUDIO_RESAMPLER_CUBIC_H 1865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#define ANDROID_AUDIO_RESAMPLER_CUBIC_H 1965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 2065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <stdint.h> 2165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <sys/types.h> 2265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <cutils/log.h> 2365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 2465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include "AudioResampler.h" 2565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 2665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopiannamespace android { 2765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ---------------------------------------------------------------------------- 2865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 2965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianclass AudioResamplerCubic : public AudioResampler { 3065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianpublic: 313348e36c51e91e78020bcc6578eda83d97c31becAndy Hung AudioResamplerCubic(int inChannelCount, int32_t sampleRate) : 323348e36c51e91e78020bcc6578eda83d97c31becAndy Hung AudioResampler(inChannelCount, sampleRate, MED_QUALITY) { 3365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 346b3b7e304e0f8f167241b2c75f1eb04a9ef192ecAndy Hung virtual size_t resample(int32_t* out, size_t outFrameCount, 3565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian AudioBufferProvider* provider); 3665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianprivate: 3765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // number of bits used in interpolation multiply - 14 bits avoids overflow 3865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian static const int kNumInterpBits = 14; 3965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 4065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // bits to shift the phase fraction down to avoid overflow 4165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian static const int kPreInterpShift = kNumPhaseBits - kNumInterpBits; 4265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian typedef struct { 4365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian int32_t a, b, c, y0, y1, y2, y3; 4465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } state; 4565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian void init(); 466b3b7e304e0f8f167241b2c75f1eb04a9ef192ecAndy Hung size_t resampleMono16(int32_t* out, size_t outFrameCount, 4765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian AudioBufferProvider* provider); 486b3b7e304e0f8f167241b2c75f1eb04a9ef192ecAndy Hung size_t resampleStereo16(int32_t* out, size_t outFrameCount, 4965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian AudioBufferProvider* provider); 5065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian static inline int32_t interp(state* p, int32_t x) { 5165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return (((((p->a * x >> 14) + p->b) * x >> 14) + p->c) * x >> 14) + p->y1; 5265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 5365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian static inline void advance(state* p, int16_t in) { 5465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian p->y0 = p->y1; 5565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian p->y1 = p->y2; 5665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian p->y2 = p->y3; 5765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian p->y3 = in; 58e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten p->a = (3 * (p->y1 - p->y2) - p->y0 + p->y3) >> 1; 5965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian p->b = (p->y2 << 1) + p->y0 - (((5 * p->y1 + p->y3)) >> 1); 6065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian p->c = (p->y2 - p->y0) >> 1; 6165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 6265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian state left, right; 6365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}; 6465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 6565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ---------------------------------------------------------------------------- 6663238efb0d674758902918e3cdaac322126484b7Glenn Kasten} // namespace android 6765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 6865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#endif /*ANDROID_AUDIO_RESAMPLER_CUBIC_H*/ 69