Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -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 | |
Andy Hung | 6b3b7e3 | 2015-03-29 00:49:22 -0700 | [diff] [blame] | 17 | #define LOG_TAG "AudioResamplerCubic" |
Glenn Kasten | 5159c7e | 2011-07-08 09:32:50 -0700 | [diff] [blame] | 18 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 19 | #include <stdint.h> |
| 20 | #include <string.h> |
| 21 | #include <sys/types.h> |
Mark Salyzyn | eb16561 | 2017-01-10 09:08:19 -0800 | [diff] [blame^] | 22 | |
| 23 | #include <log/log.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 24 | |
| 25 | #include "AudioResampler.h" |
| 26 | #include "AudioResamplerCubic.h" |
| 27 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 28 | namespace android { |
| 29 | // ---------------------------------------------------------------------------- |
| 30 | |
| 31 | void AudioResamplerCubic::init() { |
| 32 | memset(&left, 0, sizeof(state)); |
| 33 | memset(&right, 0, sizeof(state)); |
| 34 | } |
| 35 | |
Andy Hung | 6b3b7e3 | 2015-03-29 00:49:22 -0700 | [diff] [blame] | 36 | size_t AudioResamplerCubic::resample(int32_t* out, size_t outFrameCount, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 37 | AudioBufferProvider* provider) { |
| 38 | |
| 39 | // should never happen, but we overflow if it does |
Steve Block | c1dc1cb | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 40 | // ALOG_ASSERT(outFrameCount < 32767); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 41 | |
| 42 | // select the appropriate resampler |
| 43 | switch (mChannelCount) { |
| 44 | case 1: |
Andy Hung | 6b3b7e3 | 2015-03-29 00:49:22 -0700 | [diff] [blame] | 45 | return resampleMono16(out, outFrameCount, provider); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 46 | case 2: |
Andy Hung | 6b3b7e3 | 2015-03-29 00:49:22 -0700 | [diff] [blame] | 47 | return resampleStereo16(out, outFrameCount, provider); |
| 48 | default: |
| 49 | LOG_ALWAYS_FATAL("invalid channel count: %d", mChannelCount); |
| 50 | return 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
Andy Hung | 6b3b7e3 | 2015-03-29 00:49:22 -0700 | [diff] [blame] | 54 | size_t AudioResamplerCubic::resampleStereo16(int32_t* out, size_t outFrameCount, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 55 | AudioBufferProvider* provider) { |
| 56 | |
| 57 | int32_t vl = mVolume[0]; |
| 58 | int32_t vr = mVolume[1]; |
| 59 | |
| 60 | size_t inputIndex = mInputIndex; |
| 61 | uint32_t phaseFraction = mPhaseFraction; |
| 62 | uint32_t phaseIncrement = mPhaseIncrement; |
| 63 | size_t outputIndex = 0; |
| 64 | size_t outputSampleCount = outFrameCount * 2; |
Andy Hung | 24781ff | 2014-02-19 12:45:19 -0800 | [diff] [blame] | 65 | size_t inFrameCount = getInFrameCountRequired(outFrameCount); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 66 | |
| 67 | // fetch first buffer |
| 68 | if (mBuffer.frameCount == 0) { |
| 69 | mBuffer.frameCount = inFrameCount; |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 70 | provider->getNextBuffer(&mBuffer); |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 71 | if (mBuffer.raw == NULL) { |
Andy Hung | 6b3b7e3 | 2015-03-29 00:49:22 -0700 | [diff] [blame] | 72 | return 0; |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 73 | } |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 74 | // ALOGW("New buffer: offset=%p, frames=%dn", mBuffer.raw, mBuffer.frameCount); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 75 | } |
| 76 | int16_t *in = mBuffer.i16; |
| 77 | |
| 78 | while (outputIndex < outputSampleCount) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 79 | int32_t x; |
| 80 | |
| 81 | // calculate output sample |
| 82 | x = phaseFraction >> kPreInterpShift; |
| 83 | out[outputIndex++] += vl * interp(&left, x); |
| 84 | out[outputIndex++] += vr * interp(&right, x); |
| 85 | // out[outputIndex++] += vr * in[inputIndex*2]; |
| 86 | |
| 87 | // increment phase |
| 88 | phaseFraction += phaseIncrement; |
| 89 | uint32_t indexIncrement = (phaseFraction >> kNumPhaseBits); |
| 90 | phaseFraction &= kPhaseMask; |
| 91 | |
| 92 | // time to fetch another sample |
| 93 | while (indexIncrement--) { |
| 94 | |
| 95 | inputIndex++; |
| 96 | if (inputIndex == mBuffer.frameCount) { |
| 97 | inputIndex = 0; |
| 98 | provider->releaseBuffer(&mBuffer); |
| 99 | mBuffer.frameCount = inFrameCount; |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 100 | provider->getNextBuffer(&mBuffer); |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 101 | if (mBuffer.raw == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 102 | goto save_state; // ugly, but efficient |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 103 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 104 | in = mBuffer.i16; |
Glenn Kasten | 90bebef | 2012-01-27 15:24:38 -0800 | [diff] [blame] | 105 | // ALOGW("New buffer: offset=%p, frames=%d", mBuffer.raw, mBuffer.frameCount); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | // advance sample state |
| 109 | advance(&left, in[inputIndex*2]); |
| 110 | advance(&right, in[inputIndex*2+1]); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | save_state: |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 115 | // ALOGW("Done: index=%d, fraction=%u", inputIndex, phaseFraction); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 116 | mInputIndex = inputIndex; |
| 117 | mPhaseFraction = phaseFraction; |
Andy Hung | 6b3b7e3 | 2015-03-29 00:49:22 -0700 | [diff] [blame] | 118 | return outputIndex / 2 /* channels for stereo */; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Andy Hung | 6b3b7e3 | 2015-03-29 00:49:22 -0700 | [diff] [blame] | 121 | size_t AudioResamplerCubic::resampleMono16(int32_t* out, size_t outFrameCount, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 122 | AudioBufferProvider* provider) { |
| 123 | |
| 124 | int32_t vl = mVolume[0]; |
| 125 | int32_t vr = mVolume[1]; |
| 126 | |
| 127 | size_t inputIndex = mInputIndex; |
| 128 | uint32_t phaseFraction = mPhaseFraction; |
| 129 | uint32_t phaseIncrement = mPhaseIncrement; |
| 130 | size_t outputIndex = 0; |
| 131 | size_t outputSampleCount = outFrameCount * 2; |
Andy Hung | 24781ff | 2014-02-19 12:45:19 -0800 | [diff] [blame] | 132 | size_t inFrameCount = getInFrameCountRequired(outFrameCount); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 133 | |
| 134 | // fetch first buffer |
| 135 | if (mBuffer.frameCount == 0) { |
| 136 | mBuffer.frameCount = inFrameCount; |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 137 | provider->getNextBuffer(&mBuffer); |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 138 | if (mBuffer.raw == NULL) { |
Andy Hung | 6b3b7e3 | 2015-03-29 00:49:22 -0700 | [diff] [blame] | 139 | return 0; |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 140 | } |
Glenn Kasten | 90bebef | 2012-01-27 15:24:38 -0800 | [diff] [blame] | 141 | // ALOGW("New buffer: offset=%p, frames=%d", mBuffer.raw, mBuffer.frameCount); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 142 | } |
| 143 | int16_t *in = mBuffer.i16; |
| 144 | |
| 145 | while (outputIndex < outputSampleCount) { |
| 146 | int32_t sample; |
| 147 | int32_t x; |
| 148 | |
| 149 | // calculate output sample |
| 150 | x = phaseFraction >> kPreInterpShift; |
| 151 | sample = interp(&left, x); |
| 152 | out[outputIndex++] += vl * sample; |
| 153 | out[outputIndex++] += vr * sample; |
| 154 | |
| 155 | // increment phase |
| 156 | phaseFraction += phaseIncrement; |
| 157 | uint32_t indexIncrement = (phaseFraction >> kNumPhaseBits); |
| 158 | phaseFraction &= kPhaseMask; |
| 159 | |
| 160 | // time to fetch another sample |
| 161 | while (indexIncrement--) { |
| 162 | |
| 163 | inputIndex++; |
| 164 | if (inputIndex == mBuffer.frameCount) { |
| 165 | inputIndex = 0; |
| 166 | provider->releaseBuffer(&mBuffer); |
| 167 | mBuffer.frameCount = inFrameCount; |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 168 | provider->getNextBuffer(&mBuffer); |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 169 | if (mBuffer.raw == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 170 | goto save_state; // ugly, but efficient |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 171 | } |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 172 | // ALOGW("New buffer: offset=%p, frames=%dn", mBuffer.raw, mBuffer.frameCount); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 173 | in = mBuffer.i16; |
| 174 | } |
| 175 | |
| 176 | // advance sample state |
| 177 | advance(&left, in[inputIndex]); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | save_state: |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 182 | // ALOGW("Done: index=%d, fraction=%u", inputIndex, phaseFraction); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 183 | mInputIndex = inputIndex; |
| 184 | mPhaseFraction = phaseFraction; |
Andy Hung | 6b3b7e3 | 2015-03-29 00:49:22 -0700 | [diff] [blame] | 185 | return outputIndex; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | // ---------------------------------------------------------------------------- |
Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 189 | } // namespace android |