AudioResampler.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 <android/log.h>
  20. #include <sys/system_properties.h>
  21. #include "audio/android/AudioBufferProvider.h"
  22. //#include <cutils/compiler.h>
  23. //#include <utils/Compat.h>
  24. //#include <media/AudioBufferProvider.h>
  25. //#include <system/audio.h>
  26. #include <assert.h>
  27. #include "audio/android/audio.h"
  28. namespace cocos2d { namespace experimental {
  29. class AudioResampler {
  30. public:
  31. // Determines quality of SRC.
  32. // LOW_QUALITY: linear interpolator (1st order)
  33. // MED_QUALITY: cubic interpolator (3rd order)
  34. // HIGH_QUALITY: fixed multi-tap FIR (e.g. 48KHz->44.1KHz)
  35. // NOTE: high quality SRC will only be supported for
  36. // certain fixed rate conversions. Sample rate cannot be
  37. // changed dynamically.
  38. enum src_quality {
  39. DEFAULT_QUALITY=0,
  40. LOW_QUALITY=1,
  41. MED_QUALITY=2,
  42. HIGH_QUALITY=3,
  43. VERY_HIGH_QUALITY=4,
  44. };
  45. static const CONSTEXPR float UNITY_GAIN_FLOAT = 1.0f;
  46. static AudioResampler* create(audio_format_t format, int inChannelCount,
  47. int32_t sampleRate, src_quality quality=DEFAULT_QUALITY);
  48. virtual ~AudioResampler();
  49. virtual void init() = 0;
  50. virtual void setSampleRate(int32_t inSampleRate);
  51. virtual void setVolume(float left, float right);
  52. virtual void setLocalTimeFreq(uint64_t freq);
  53. // set the PTS of the next buffer output by the resampler
  54. virtual void setPTS(int64_t pts);
  55. // Resample int16_t samples from provider and accumulate into 'out'.
  56. // A mono provider delivers a sequence of samples.
  57. // A stereo provider delivers a sequence of interleaved pairs of samples.
  58. //
  59. // In either case, 'out' holds interleaved pairs of fixed-point Q4.27.
  60. // That is, for a mono provider, there is an implicit up-channeling.
  61. // Since this method accumulates, the caller is responsible for clearing 'out' initially.
  62. //
  63. // For a float resampler, 'out' holds interleaved pairs of float samples.
  64. //
  65. // Multichannel interleaved frames for n > 2 is supported for quality DYN_LOW_QUALITY,
  66. // DYN_MED_QUALITY, and DYN_HIGH_QUALITY.
  67. //
  68. // Returns the number of frames resampled into the out buffer.
  69. virtual size_t resample(int32_t* out, size_t outFrameCount,
  70. AudioBufferProvider* provider) = 0;
  71. virtual void reset();
  72. virtual size_t getUnreleasedFrames() const { return mInputIndex; }
  73. // called from destructor, so must not be virtual
  74. src_quality getQuality() const { return mQuality; }
  75. protected:
  76. // number of bits for phase fraction - 30 bits allows nearly 2x downsampling
  77. static const int kNumPhaseBits = 30;
  78. // phase mask for fraction
  79. static const uint32_t kPhaseMask = (1LU<<kNumPhaseBits)-1;
  80. // multiplier to calculate fixed point phase increment
  81. static const double kPhaseMultiplier;
  82. AudioResampler(int inChannelCount, int32_t sampleRate, src_quality quality);
  83. // prevent copying
  84. AudioResampler(const AudioResampler&);
  85. AudioResampler& operator=(const AudioResampler&);
  86. int64_t calculateOutputPTS(int outputFrameIndex);
  87. const int32_t mChannelCount;
  88. const int32_t mSampleRate;
  89. int32_t mInSampleRate;
  90. AudioBufferProvider::Buffer mBuffer;
  91. union {
  92. int16_t mVolume[2];
  93. uint32_t mVolumeRL;
  94. };
  95. int16_t mTargetVolume[2];
  96. size_t mInputIndex;
  97. int32_t mPhaseIncrement;
  98. uint32_t mPhaseFraction;
  99. uint64_t mLocalTimeFreq;
  100. int64_t mPTS;
  101. // returns the inFrameCount required to generate outFrameCount frames.
  102. //
  103. // Placed here to be a consistent for all resamplers.
  104. //
  105. // Right now, we use the upper bound without regards to the current state of the
  106. // input buffer using integer arithmetic, as follows:
  107. //
  108. // (static_cast<uint64_t>(outFrameCount)*mInSampleRate + (mSampleRate - 1))/mSampleRate;
  109. //
  110. // The double precision equivalent (float may not be precise enough):
  111. // ceil(static_cast<double>(outFrameCount) * mInSampleRate / mSampleRate);
  112. //
  113. // this relies on the fact that the mPhaseIncrement is rounded down from
  114. // #phases * mInSampleRate/mSampleRate and the fact that Sum(Floor(x)) <= Floor(Sum(x)).
  115. // http://www.proofwiki.org/wiki/Sum_of_Floors_Not_Greater_Than_Floor_of_Sums
  116. //
  117. // (so long as double precision is computed accurately enough to be considered
  118. // greater than or equal to the Floor(x) value in int32_t arithmetic; thus this
  119. // will not necessarily hold for floats).
  120. //
  121. // TODO:
  122. // Greater accuracy and a tight bound is obtained by:
  123. // 1) subtract and adjust for the current state of the AudioBufferProvider buffer.
  124. // 2) using the exact integer formula where (ignoring 64b casting)
  125. // inFrameCount = (mPhaseIncrement * (outFrameCount - 1) + mPhaseFraction) / phaseWrapLimit;
  126. // phaseWrapLimit is the wraparound (1 << kNumPhaseBits), if not specified explicitly.
  127. //
  128. inline size_t getInFrameCountRequired(size_t outFrameCount) {
  129. return (static_cast<uint64_t>(outFrameCount)*mInSampleRate
  130. + (mSampleRate - 1))/mSampleRate;
  131. }
  132. inline float clampFloatVol(float volume) {
  133. if (volume > UNITY_GAIN_FLOAT) {
  134. return UNITY_GAIN_FLOAT;
  135. } else if (volume >= 0.) {
  136. return volume;
  137. }
  138. return 0.; // NaN or negative volume maps to 0.
  139. }
  140. private:
  141. const src_quality mQuality;
  142. // Return 'true' if the quality level is supported without explicit request
  143. static bool qualityIsSupported(src_quality quality);
  144. // For pthread_once()
  145. static void init_routine();
  146. // Return the estimated CPU load for specific resampler in MHz.
  147. // The absolute number is irrelevant, it's the relative values that matter.
  148. static uint32_t qualityMHz(src_quality quality);
  149. };
  150. // ----------------------------------------------------------------------------
  151. }} // namespace cocos2d { namespace experimental {