AudioResamplerCubic.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. #pragma once
  17. #include <stdint.h>
  18. #include <sys/types.h>
  19. #include "audio/android/AudioResampler.h"
  20. #include "audio/android/AudioBufferProvider.h"
  21. namespace cocos2d { namespace experimental {
  22. // ----------------------------------------------------------------------------
  23. class AudioResamplerCubic : public AudioResampler {
  24. public:
  25. AudioResamplerCubic(int inChannelCount, int32_t sampleRate) :
  26. AudioResampler(inChannelCount, sampleRate, MED_QUALITY) {
  27. }
  28. virtual size_t resample(int32_t* out, size_t outFrameCount,
  29. AudioBufferProvider* provider);
  30. private:
  31. // number of bits used in interpolation multiply - 14 bits avoids overflow
  32. static const int kNumInterpBits = 14;
  33. // bits to shift the phase fraction down to avoid overflow
  34. static const int kPreInterpShift = kNumPhaseBits - kNumInterpBits;
  35. typedef struct {
  36. int32_t a, b, c, y0, y1, y2, y3;
  37. } state;
  38. void init();
  39. size_t resampleMono16(int32_t* out, size_t outFrameCount,
  40. AudioBufferProvider* provider);
  41. size_t resampleStereo16(int32_t* out, size_t outFrameCount,
  42. AudioBufferProvider* provider);
  43. static inline int32_t interp(state* p, int32_t x) {
  44. return (((((p->a * x >> 14) + p->b) * x >> 14) + p->c) * x >> 14) + p->y1;
  45. }
  46. static inline void advance(state* p, int16_t in) {
  47. p->y0 = p->y1;
  48. p->y1 = p->y2;
  49. p->y2 = p->y3;
  50. p->y3 = in;
  51. p->a = (3 * (p->y1 - p->y2) - p->y0 + p->y3) >> 1;
  52. p->b = (p->y2 << 1) + p->y0 - (((5 * p->y1 + p->y3)) >> 1);
  53. p->c = (p->y2 - p->y0) >> 1;
  54. }
  55. state left, right;
  56. };
  57. // ----------------------------------------------------------------------------
  58. }} // namespace cocos2d { namespace experimental {