AudioFormatAdapter.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* /android/src/frameworks/base/media/libeffects/AudioFormatAdapter.h
  2. **
  3. ** Copyright 2009, The Android Open Source Project
  4. **
  5. ** Licensed under the Apache License, Version 2.0 (the "License");
  6. ** you may not use this file except in compliance with the License.
  7. ** You may obtain a copy of the License at
  8. **
  9. ** http://www.apache.org/licenses/LICENSE-2.0
  10. **
  11. ** Unless required by applicable law or agreed to in writing, software
  12. ** distributed under the License is distributed on an "AS IS" BASIS,
  13. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ** See the License for the specific language governing permissions and
  15. ** limitations under the License.
  16. */
  17. #ifndef AUDIOFORMATADAPTER_H_
  18. #define AUDIOFORMATADAPTER_H_
  19. #include <hardware/audio_effect.h>
  20. #define min(x,y) (((x) < (y)) ? (x) : (y))
  21. namespace android {
  22. // An adapter for an audio processor working on audio_sample_t samples with a
  23. // buffer override behavior to arbitrary sample formats and buffer behaviors.
  24. // The adapter may work on any processing class which has a processing function
  25. // with the following signature:
  26. // void process(const audio_sample_t * pIn,
  27. // audio_sample_t * pOut,
  28. // int frameCount);
  29. // It is assumed that the underlying processor works in S7.24 format and an
  30. // overwrite behavior.
  31. //
  32. // Usage is simple: just work with the processor normally, but instead of
  33. // calling its process() function directly, work with the process() function of
  34. // the adapter.
  35. // The adapter supports re-configuration to a different format on the fly.
  36. //
  37. // T The processor class.
  38. // bufSize The maximum number of samples (single channel) to process on a
  39. // single call to the underlying processor. Setting this to a small
  40. // number will save a little memory, but will cost function call
  41. // overhead, resulting from multiple calls to the underlying process()
  42. // per a single call to this class's process().
  43. template<class T, size_t bufSize>
  44. class AudioFormatAdapter {
  45. public:
  46. // Configure the adapter.
  47. // processor The underlying audio processor.
  48. // nChannels Number of input and output channels. The adapter does not do
  49. // channel conversion - this parameter must be in sync with the
  50. // actual processor.
  51. // pcmFormat The desired input/output sample format.
  52. // behavior The desired behavior (overwrite or accumulate).
  53. void configure(T & processor, int nChannels, uint8_t pcmFormat,
  54. uint32_t behavior) {
  55. mpProcessor = &processor;
  56. mNumChannels = nChannels;
  57. mPcmFormat = pcmFormat;
  58. mBehavior = behavior;
  59. mMaxSamplesPerCall = bufSize / nChannels;
  60. }
  61. // Process a block of samples.
  62. // pIn A buffer of samples with the format specified on
  63. // configure().
  64. // pOut A buffer of samples with the format specified on
  65. // configure(). May be the same as pIn.
  66. // numSamples The number of multi-channel samples to process.
  67. void process(const void * pIn, void * pOut, uint32_t numSamples) {
  68. while (numSamples > 0) {
  69. uint32_t numSamplesIter = min(numSamples, mMaxSamplesPerCall);
  70. uint32_t nSamplesChannels = numSamplesIter * mNumChannels;
  71. // This branch of "if" is untested
  72. if (mPcmFormat == AUDIO_FORMAT_PCM_8_24_BIT) {
  73. if (mBehavior == EFFECT_BUFFER_ACCESS_WRITE) {
  74. mpProcessor->process(
  75. reinterpret_cast<const audio_sample_t *> (pIn),
  76. reinterpret_cast<audio_sample_t *> (pOut),
  77. numSamplesIter);
  78. } else if (mBehavior == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
  79. mpProcessor->process(
  80. reinterpret_cast<const audio_sample_t *> (pIn),
  81. mBuffer, numSamplesIter);
  82. MixOutput(pOut, numSamplesIter);
  83. } else {
  84. assert(false);
  85. }
  86. pIn = reinterpret_cast<const audio_sample_t *> (pIn)
  87. + nSamplesChannels;
  88. pOut = reinterpret_cast<audio_sample_t *> (pOut)
  89. + nSamplesChannels;
  90. } else {
  91. ConvertInput(pIn, nSamplesChannels);
  92. mpProcessor->process(mBuffer, mBuffer, numSamplesIter);
  93. ConvertOutput(pOut, nSamplesChannels);
  94. }
  95. numSamples -= numSamplesIter;
  96. }
  97. }
  98. private:
  99. // The underlying processor.
  100. T * mpProcessor;
  101. // The number of input/output channels.
  102. int mNumChannels;
  103. // The desired PCM format.
  104. uint8_t mPcmFormat;
  105. // The desired buffer behavior.
  106. uint32_t mBehavior;
  107. // An intermediate buffer for processing.
  108. audio_sample_t mBuffer[bufSize];
  109. // The buffer size, divided by the number of channels - represents the
  110. // maximum number of multi-channel samples that can be stored in the
  111. // intermediate buffer.
  112. size_t mMaxSamplesPerCall;
  113. // Converts a buffer of input samples to audio_sample_t format.
  114. // Output is written to the intermediate buffer.
  115. // pIn The input buffer with the format designated in configure().
  116. // When function exist will point to the next unread input
  117. // sample.
  118. // numSamples The number of single-channel samples to process.
  119. void ConvertInput(const void *& pIn, uint32_t numSamples) {
  120. if (mPcmFormat == AUDIO_FORMAT_PCM_16_BIT) {
  121. const int16_t * pIn16 = reinterpret_cast<const int16_t *>(pIn);
  122. audio_sample_t * pOut = mBuffer;
  123. while (numSamples-- > 0) {
  124. *(pOut++) = s15_to_audio_sample_t(*(pIn16++));
  125. }
  126. pIn = pIn16;
  127. } else {
  128. assert(false);
  129. }
  130. }
  131. // Converts audio_sample_t samples from the intermediate buffer to the
  132. // output buffer, converting to the desired format and buffer behavior.
  133. // pOut The buffer to write the output to.
  134. // When function exist will point to the next output sample.
  135. // numSamples The number of single-channel samples to process.
  136. void ConvertOutput(void *& pOut, uint32_t numSamples) {
  137. if (mPcmFormat == AUDIO_FORMAT_PCM_16_BIT) {
  138. const audio_sample_t * pIn = mBuffer;
  139. int16_t * pOut16 = reinterpret_cast<int16_t *>(pOut);
  140. if (mBehavior == EFFECT_BUFFER_ACCESS_WRITE) {
  141. while (numSamples-- > 0) {
  142. *(pOut16++) = audio_sample_t_to_s15_clip(*(pIn++));
  143. }
  144. } else if (mBehavior == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
  145. while (numSamples-- > 0) {
  146. *(pOut16++) += audio_sample_t_to_s15_clip(*(pIn++));
  147. }
  148. } else {
  149. assert(false);
  150. }
  151. pOut = pOut16;
  152. } else {
  153. assert(false);
  154. }
  155. }
  156. // Accumulate data from the intermediate buffer to the output. Output is
  157. // assumed to be of audio_sample_t type.
  158. // pOut The buffer to mix the output to.
  159. // When function exist will point to the next output sample.
  160. // numSamples The number of single-channel samples to process.
  161. void MixOutput(void *& pOut, uint32_t numSamples) {
  162. const audio_sample_t * pIn = mBuffer;
  163. audio_sample_t * pOut24 = reinterpret_cast<audio_sample_t *>(pOut);
  164. numSamples *= mNumChannels;
  165. while (numSamples-- > 0) {
  166. *(pOut24++) += *(pIn++);
  167. }
  168. pOut = pOut24;
  169. }
  170. };
  171. }
  172. #endif // AUDIOFORMATADAPTER_H_