The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | #include <stdint.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <sys/types.h> |
| 20 | #include <cutils/log.h> |
| 21 | #include <cutils/properties.h> |
| 22 | |
| 23 | #include "AudioResampler.h" |
| 24 | #include "AudioResamplerSinc.h" |
| 25 | #include "AudioResamplerCubic.h" |
| 26 | |
| 27 | namespace android { |
| 28 | // ---------------------------------------------------------------------------- |
| 29 | |
| 30 | class AudioResamplerOrder1 : public AudioResampler { |
| 31 | public: |
| 32 | AudioResamplerOrder1(int bitDepth, int inChannelCount, int32_t sampleRate) : |
| 33 | AudioResampler(bitDepth, inChannelCount, sampleRate), mX0L(0), mX0R(0) { |
| 34 | } |
| 35 | virtual void resample(int32_t* out, size_t outFrameCount, |
| 36 | AudioBufferProvider* provider); |
| 37 | private: |
| 38 | // number of bits used in interpolation multiply - 15 bits avoids overflow |
| 39 | static const int kNumInterpBits = 15; |
| 40 | |
| 41 | // bits to shift the phase fraction down to avoid overflow |
| 42 | static const int kPreInterpShift = kNumPhaseBits - kNumInterpBits; |
| 43 | |
| 44 | void init() {} |
| 45 | void resampleMono16(int32_t* out, size_t outFrameCount, |
| 46 | AudioBufferProvider* provider); |
| 47 | void resampleStereo16(int32_t* out, size_t outFrameCount, |
| 48 | AudioBufferProvider* provider); |
| 49 | static inline int32_t Interp(int32_t x0, int32_t x1, uint32_t f) { |
| 50 | return x0 + (((x1 - x0) * (int32_t)(f >> kPreInterpShift)) >> kNumInterpBits); |
| 51 | } |
| 52 | static inline void Advance(size_t* index, uint32_t* frac, uint32_t inc) { |
| 53 | *frac += inc; |
| 54 | *index += (size_t)(*frac >> kNumPhaseBits); |
| 55 | *frac &= kPhaseMask; |
| 56 | } |
| 57 | int mX0L; |
| 58 | int mX0R; |
| 59 | }; |
| 60 | |
| 61 | // ---------------------------------------------------------------------------- |
| 62 | AudioResampler* AudioResampler::create(int bitDepth, int inChannelCount, |
| 63 | int32_t sampleRate, int quality) { |
| 64 | |
| 65 | // can only create low quality resample now |
| 66 | AudioResampler* resampler; |
| 67 | |
| 68 | char value[PROPERTY_VALUE_MAX]; |
| 69 | if (property_get("af.resampler.quality", value, 0)) { |
| 70 | quality = atoi(value); |
| 71 | LOGD("forcing AudioResampler quality to %d", quality); |
| 72 | } |
| 73 | |
| 74 | if (quality == DEFAULT) |
| 75 | quality = LOW_QUALITY; |
| 76 | |
| 77 | switch (quality) { |
| 78 | default: |
| 79 | case LOW_QUALITY: |
| 80 | resampler = new AudioResamplerOrder1(bitDepth, inChannelCount, sampleRate); |
| 81 | break; |
| 82 | case MED_QUALITY: |
| 83 | resampler = new AudioResamplerCubic(bitDepth, inChannelCount, sampleRate); |
| 84 | break; |
| 85 | case HIGH_QUALITY: |
| 86 | resampler = new AudioResamplerSinc(bitDepth, inChannelCount, sampleRate); |
| 87 | break; |
| 88 | } |
| 89 | |
| 90 | // initialize resampler |
| 91 | resampler->init(); |
| 92 | return resampler; |
| 93 | } |
| 94 | |
| 95 | AudioResampler::AudioResampler(int bitDepth, int inChannelCount, |
| 96 | int32_t sampleRate) : |
| 97 | mBitDepth(bitDepth), mChannelCount(inChannelCount), |
| 98 | mSampleRate(sampleRate), mInSampleRate(sampleRate), mInputIndex(0), |
| 99 | mPhaseFraction(0) { |
| 100 | // sanity check on format |
| 101 | if ((bitDepth != 16) ||(inChannelCount < 1) || (inChannelCount > 2)) { |
| 102 | LOGE("Unsupported sample format, %d bits, %d channels", bitDepth, |
| 103 | inChannelCount); |
| 104 | // LOG_ASSERT(0); |
| 105 | } |
| 106 | |
| 107 | // initialize common members |
| 108 | mVolume[0] = mVolume[1] = 0; |
| 109 | mBuffer.raw = NULL; |
| 110 | |
| 111 | // save format for quick lookup |
| 112 | if (inChannelCount == 1) { |
| 113 | mFormat = MONO_16_BIT; |
| 114 | } else { |
| 115 | mFormat = STEREO_16_BIT; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | AudioResampler::~AudioResampler() { |
| 120 | } |
| 121 | |
| 122 | void AudioResampler::setSampleRate(int32_t inSampleRate) { |
| 123 | mInSampleRate = inSampleRate; |
| 124 | mPhaseIncrement = (uint32_t)((kPhaseMultiplier * inSampleRate) / mSampleRate); |
| 125 | } |
| 126 | |
| 127 | void AudioResampler::setVolume(int16_t left, int16_t right) { |
| 128 | // TODO: Implement anti-zipper filter |
| 129 | mVolume[0] = left; |
| 130 | mVolume[1] = right; |
| 131 | } |
| 132 | |
| 133 | // ---------------------------------------------------------------------------- |
| 134 | |
| 135 | void AudioResamplerOrder1::resample(int32_t* out, size_t outFrameCount, |
| 136 | AudioBufferProvider* provider) { |
| 137 | |
| 138 | // should never happen, but we overflow if it does |
| 139 | // LOG_ASSERT(outFrameCount < 32767); |
| 140 | |
| 141 | // select the appropriate resampler |
| 142 | switch (mChannelCount) { |
| 143 | case 1: |
| 144 | resampleMono16(out, outFrameCount, provider); |
| 145 | break; |
| 146 | case 2: |
| 147 | resampleStereo16(out, outFrameCount, provider); |
| 148 | break; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | void AudioResamplerOrder1::resampleStereo16(int32_t* out, size_t outFrameCount, |
| 153 | AudioBufferProvider* provider) { |
| 154 | |
| 155 | int32_t vl = mVolume[0]; |
| 156 | int32_t vr = mVolume[1]; |
| 157 | |
| 158 | size_t inputIndex = mInputIndex; |
| 159 | uint32_t phaseFraction = mPhaseFraction; |
| 160 | uint32_t phaseIncrement = mPhaseIncrement; |
| 161 | size_t outputIndex = 0; |
| 162 | size_t outputSampleCount = outFrameCount * 2; |
| 163 | |
| 164 | // LOGE("starting resample %d frames, inputIndex=%d, phaseFraction=%d, phaseIncrement=%d\n", |
| 165 | // outFrameCount, inputIndex, phaseFraction, phaseIncrement); |
| 166 | |
| 167 | while (outputIndex < outputSampleCount) { |
| 168 | |
| 169 | // buffer is empty, fetch a new one |
| 170 | if (mBuffer.raw == NULL) { |
| 171 | provider->getNextBuffer(&mBuffer); |
| 172 | if (mBuffer.raw == NULL) |
| 173 | break; |
| 174 | // LOGE("New buffer fetched: %d frames\n", mBuffer.frameCount); |
| 175 | } |
| 176 | int16_t *in = mBuffer.i16; |
| 177 | |
| 178 | // handle boundary case |
| 179 | while (inputIndex == 0) { |
| 180 | // LOGE("boundary case\n"); |
| 181 | out[outputIndex++] += vl * Interp(mX0L, in[0], phaseFraction); |
| 182 | out[outputIndex++] += vr * Interp(mX0R, in[1], phaseFraction); |
| 183 | Advance(&inputIndex, &phaseFraction, phaseIncrement); |
| 184 | if (outputIndex == outputSampleCount) |
| 185 | break; |
| 186 | } |
| 187 | |
| 188 | // process input samples |
| 189 | // LOGE("general case\n"); |
| 190 | while (outputIndex < outputSampleCount) { |
| 191 | out[outputIndex++] += vl * Interp(in[inputIndex*2-2], |
| 192 | in[inputIndex*2], phaseFraction); |
| 193 | out[outputIndex++] += vr * Interp(in[inputIndex*2-1], |
| 194 | in[inputIndex*2+1], phaseFraction); |
| 195 | Advance(&inputIndex, &phaseFraction, phaseIncrement); |
| 196 | if (inputIndex >= mBuffer.frameCount) |
| 197 | break; |
| 198 | } |
| 199 | // LOGE("loop done - outputIndex=%d, inputIndex=%d\n", outputIndex, inputIndex); |
| 200 | |
| 201 | // if done with buffer, save samples |
| 202 | if (inputIndex >= mBuffer.frameCount) { |
| 203 | inputIndex -= mBuffer.frameCount; |
| 204 | |
| 205 | // LOGE("buffer done, new input index", inputIndex); |
| 206 | |
| 207 | mX0L = mBuffer.i16[mBuffer.frameCount*2-2]; |
| 208 | mX0R = mBuffer.i16[mBuffer.frameCount*2-1]; |
| 209 | provider->releaseBuffer(&mBuffer); |
| 210 | |
| 211 | // verify that the releaseBuffer NULLS the buffer pointer |
| 212 | // LOG_ASSERT(mBuffer.raw == NULL); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | // LOGE("output buffer full - outputIndex=%d, inputIndex=%d\n", outputIndex, inputIndex); |
| 217 | |
| 218 | // save state |
| 219 | mInputIndex = inputIndex; |
| 220 | mPhaseFraction = phaseFraction; |
| 221 | } |
| 222 | |
| 223 | void AudioResamplerOrder1::resampleMono16(int32_t* out, size_t outFrameCount, |
| 224 | AudioBufferProvider* provider) { |
| 225 | |
| 226 | int32_t vl = mVolume[0]; |
| 227 | int32_t vr = mVolume[1]; |
| 228 | |
| 229 | size_t inputIndex = mInputIndex; |
| 230 | uint32_t phaseFraction = mPhaseFraction; |
| 231 | uint32_t phaseIncrement = mPhaseIncrement; |
| 232 | size_t outputIndex = 0; |
| 233 | size_t outputSampleCount = outFrameCount * 2; |
| 234 | |
| 235 | // LOGE("starting resample %d frames, inputIndex=%d, phaseFraction=%d, phaseIncrement=%d\n", |
| 236 | // outFrameCount, inputIndex, phaseFraction, phaseIncrement); |
| 237 | |
| 238 | while (outputIndex < outputSampleCount) { |
| 239 | |
| 240 | // buffer is empty, fetch a new one |
| 241 | if (mBuffer.raw == NULL) { |
| 242 | provider->getNextBuffer(&mBuffer); |
| 243 | if (mBuffer.raw == NULL) |
| 244 | break; |
| 245 | // LOGE("New buffer fetched: %d frames\n", mBuffer.frameCount); |
| 246 | } |
| 247 | int16_t *in = mBuffer.i16; |
| 248 | |
| 249 | // handle boundary case |
| 250 | while (inputIndex == 0) { |
| 251 | // LOGE("boundary case\n"); |
| 252 | int32_t sample = Interp(mX0L, in[0], phaseFraction); |
| 253 | out[outputIndex++] += vl * sample; |
| 254 | out[outputIndex++] += vr * sample; |
| 255 | Advance(&inputIndex, &phaseFraction, phaseIncrement); |
| 256 | if (outputIndex == outputSampleCount) |
| 257 | break; |
| 258 | } |
| 259 | |
| 260 | // process input samples |
| 261 | // LOGE("general case\n"); |
| 262 | while (outputIndex < outputSampleCount) { |
| 263 | int32_t sample = Interp(in[inputIndex-1], in[inputIndex], |
| 264 | phaseFraction); |
| 265 | out[outputIndex++] += vl * sample; |
| 266 | out[outputIndex++] += vr * sample; |
| 267 | Advance(&inputIndex, &phaseFraction, phaseIncrement); |
| 268 | if (inputIndex >= mBuffer.frameCount) |
| 269 | break; |
| 270 | } |
| 271 | // LOGE("loop done - outputIndex=%d, inputIndex=%d\n", outputIndex, inputIndex); |
| 272 | |
| 273 | // if done with buffer, save samples |
| 274 | if (inputIndex >= mBuffer.frameCount) { |
| 275 | inputIndex -= mBuffer.frameCount; |
| 276 | |
| 277 | // LOGE("buffer done, new input index", inputIndex); |
| 278 | |
| 279 | mX0L = mBuffer.i16[mBuffer.frameCount-1]; |
| 280 | provider->releaseBuffer(&mBuffer); |
| 281 | |
| 282 | // verify that the releaseBuffer NULLS the buffer pointer |
| 283 | // LOG_ASSERT(mBuffer.raw == NULL); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | // LOGE("output buffer full - outputIndex=%d, inputIndex=%d\n", outputIndex, inputIndex); |
| 288 | |
| 289 | // save state |
| 290 | mInputIndex = inputIndex; |
| 291 | mPhaseFraction = phaseFraction; |
| 292 | } |
| 293 | |
| 294 | // ---------------------------------------------------------------------------- |
| 295 | } |
| 296 | ; // namespace android |
| 297 | |