AudioResamplerCubic.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #define LOG_TAG "AudioResamplerCubic"
  17. #include <stdint.h>
  18. #include <string.h>
  19. #include <sys/types.h>
  20. #include "audio/android/cutils/log.h"
  21. #include "audio/android/AudioResampler.h"
  22. #include "audio/android/AudioResamplerCubic.h"
  23. namespace cocos2d { namespace experimental {
  24. // ----------------------------------------------------------------------------
  25. void AudioResamplerCubic::init() {
  26. memset(&left, 0, sizeof(state));
  27. memset(&right, 0, sizeof(state));
  28. }
  29. size_t AudioResamplerCubic::resample(int32_t* out, size_t outFrameCount,
  30. AudioBufferProvider* provider) {
  31. // should never happen, but we overflow if it does
  32. // ALOG_ASSERT(outFrameCount < 32767);
  33. // select the appropriate resampler
  34. switch (mChannelCount) {
  35. case 1:
  36. return resampleMono16(out, outFrameCount, provider);
  37. case 2:
  38. return resampleStereo16(out, outFrameCount, provider);
  39. default:
  40. LOG_ALWAYS_FATAL("invalid channel count: %d", mChannelCount);
  41. return 0;
  42. }
  43. }
  44. size_t AudioResamplerCubic::resampleStereo16(int32_t* out, size_t outFrameCount,
  45. AudioBufferProvider* provider) {
  46. int32_t vl = mVolume[0];
  47. int32_t vr = mVolume[1];
  48. size_t inputIndex = mInputIndex;
  49. uint32_t phaseFraction = mPhaseFraction;
  50. uint32_t phaseIncrement = mPhaseIncrement;
  51. size_t outputIndex = 0;
  52. size_t outputSampleCount = outFrameCount * 2;
  53. size_t inFrameCount = getInFrameCountRequired(outFrameCount);
  54. // fetch first buffer
  55. if (mBuffer.frameCount == 0) {
  56. mBuffer.frameCount = inFrameCount;
  57. provider->getNextBuffer(&mBuffer, mPTS);
  58. if (mBuffer.raw == NULL) {
  59. return 0;
  60. }
  61. // ALOGW("New buffer: offset=%p, frames=%dn", mBuffer.raw, mBuffer.frameCount);
  62. }
  63. int16_t *in = mBuffer.i16;
  64. while (outputIndex < outputSampleCount) {
  65. int32_t sample;
  66. int32_t x;
  67. // calculate output sample
  68. x = phaseFraction >> kPreInterpShift;
  69. out[outputIndex++] += vl * interp(&left, x);
  70. out[outputIndex++] += vr * interp(&right, x);
  71. // out[outputIndex++] += vr * in[inputIndex*2];
  72. // increment phase
  73. phaseFraction += phaseIncrement;
  74. uint32_t indexIncrement = (phaseFraction >> kNumPhaseBits);
  75. phaseFraction &= kPhaseMask;
  76. // time to fetch another sample
  77. while (indexIncrement--) {
  78. inputIndex++;
  79. if (inputIndex == mBuffer.frameCount) {
  80. inputIndex = 0;
  81. provider->releaseBuffer(&mBuffer);
  82. mBuffer.frameCount = inFrameCount;
  83. provider->getNextBuffer(&mBuffer,
  84. calculateOutputPTS(outputIndex / 2));
  85. if (mBuffer.raw == NULL) {
  86. goto save_state; // ugly, but efficient
  87. }
  88. in = mBuffer.i16;
  89. // ALOGW("New buffer: offset=%p, frames=%d", mBuffer.raw, mBuffer.frameCount);
  90. }
  91. // advance sample state
  92. advance(&left, in[inputIndex*2]);
  93. advance(&right, in[inputIndex*2+1]);
  94. }
  95. }
  96. save_state:
  97. // ALOGW("Done: index=%d, fraction=%u", inputIndex, phaseFraction);
  98. mInputIndex = inputIndex;
  99. mPhaseFraction = phaseFraction;
  100. return outputIndex / 2 /* channels for stereo */;
  101. }
  102. size_t AudioResamplerCubic::resampleMono16(int32_t* out, size_t outFrameCount,
  103. AudioBufferProvider* provider) {
  104. int32_t vl = mVolume[0];
  105. int32_t vr = mVolume[1];
  106. size_t inputIndex = mInputIndex;
  107. uint32_t phaseFraction = mPhaseFraction;
  108. uint32_t phaseIncrement = mPhaseIncrement;
  109. size_t outputIndex = 0;
  110. size_t outputSampleCount = outFrameCount * 2;
  111. size_t inFrameCount = getInFrameCountRequired(outFrameCount);
  112. // fetch first buffer
  113. if (mBuffer.frameCount == 0) {
  114. mBuffer.frameCount = inFrameCount;
  115. provider->getNextBuffer(&mBuffer, mPTS);
  116. if (mBuffer.raw == NULL) {
  117. return 0;
  118. }
  119. // ALOGW("New buffer: offset=%p, frames=%d", mBuffer.raw, mBuffer.frameCount);
  120. }
  121. int16_t *in = mBuffer.i16;
  122. while (outputIndex < outputSampleCount) {
  123. int32_t sample;
  124. int32_t x;
  125. // calculate output sample
  126. x = phaseFraction >> kPreInterpShift;
  127. sample = interp(&left, x);
  128. out[outputIndex++] += vl * sample;
  129. out[outputIndex++] += vr * sample;
  130. // increment phase
  131. phaseFraction += phaseIncrement;
  132. uint32_t indexIncrement = (phaseFraction >> kNumPhaseBits);
  133. phaseFraction &= kPhaseMask;
  134. // time to fetch another sample
  135. while (indexIncrement--) {
  136. inputIndex++;
  137. if (inputIndex == mBuffer.frameCount) {
  138. inputIndex = 0;
  139. provider->releaseBuffer(&mBuffer);
  140. mBuffer.frameCount = inFrameCount;
  141. provider->getNextBuffer(&mBuffer,
  142. calculateOutputPTS(outputIndex / 2));
  143. if (mBuffer.raw == NULL) {
  144. goto save_state; // ugly, but efficient
  145. }
  146. // ALOGW("New buffer: offset=%p, frames=%dn", mBuffer.raw, mBuffer.frameCount);
  147. in = mBuffer.i16;
  148. }
  149. // advance sample state
  150. advance(&left, in[inputIndex]);
  151. }
  152. }
  153. save_state:
  154. // ALOGW("Done: index=%d, fraction=%u", inputIndex, phaseFraction);
  155. mInputIndex = inputIndex;
  156. mPhaseFraction = phaseFraction;
  157. return outputIndex;
  158. }
  159. // ----------------------------------------------------------------------------
  160. }} // namespace cocos2d { namespace experimental {