AudioEqualizer.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright 2009, 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. #ifndef AUDIOEQUALIZER_H_
  17. #define AUDIOEQUALIZER_H_
  18. #include "AudioCommon.h"
  19. namespace android {
  20. class AudioShelvingFilter;
  21. class AudioPeakingFilter;
  22. // A parametric audio equalizer. Supports an arbitrary number of bands and
  23. // presets.
  24. // The EQ is composed of a low-shelf, zero or more peaking filters and a high
  25. // shelf, where each band has frequency and gain controls, and the peaking
  26. // filters have an additional bandwidth control.
  27. class AudioEqualizer {
  28. public:
  29. // Configuration of a single band.
  30. struct BandConfig {
  31. // Gain in millibel.
  32. int32_t gain;
  33. // Frequency in millihertz.
  34. uint32_t freq;
  35. // Bandwidth in cents (ignored on shelving filters).
  36. uint32_t bandwidth;
  37. };
  38. // Preset configuration.
  39. struct PresetConfig {
  40. // Human-readable name.
  41. const char * name;
  42. // An array of size nBands where each element is a configuration for the
  43. // corresponding band.
  44. const BandConfig * bandConfigs;
  45. };
  46. // This value is used when requesting current preset, and EQ is not using a
  47. // preset.
  48. static const int PRESET_CUSTOM = -1;
  49. // Get the required memory size for an instance of this class.
  50. // nBands Number of bands required in the instance.
  51. static size_t GetInstanceSize(int nBands);
  52. // Create an instance of this class.
  53. // If succeeds, a respective call is expected to freeInstance(), regardless
  54. // of who owns the context memory.
  55. // pMem A memory buffer of at least the size returned by
  56. // GetInstanceSize(), where the instance context is to be
  57. // stored. If NULL, it will be automatically allocated (using
  58. // malloc).
  59. // nBands Number of bands. Must be >= 2.
  60. // nChannels Number of input/output channels (interlaced).
  61. // sampleRate The input/output sample rate, in Hz.
  62. // presets The presets configuration. May be NULL, but in that case the
  63. // client is required not to call preset-related functions.
  64. // This array is owned by the client and is not copied. It
  65. // must be kept valid by the client as long as the instance is
  66. // alive.
  67. // nPresets Number of elements in the presets array.
  68. // returns The instance if success. NULL if pMem is NULL and allocation
  69. // failed.
  70. static AudioEqualizer * CreateInstance(void * pMem, int nBands,
  71. int nChannels,
  72. int sampleRate,
  73. const PresetConfig * presets,
  74. int nPresets);
  75. // Reconfiguration of the filter. Changes input/output format, but does not
  76. // alter current parameter values. Causes reset of the delay lines.
  77. // nChannels Number of input/output channels (interlaced).
  78. // sampleRate The input/output sample rate, in Hz.
  79. void configure(int nChannels, int sampleRate);
  80. // Resets the filter parameters to the following values:
  81. // frequency: 0
  82. // gain: 0
  83. // bandwidth: 1200 cents.
  84. // It also disables the filter. Does not clear the delay lines.
  85. void reset();
  86. // Clears delay lines. Does not alter parameter values.
  87. void clear();
  88. // Frees the object. Will free the memory if the object owned it, i.e. if
  89. // a NULL pointer was passed to CreateInstance as pMem.
  90. void free();
  91. // Sets gain value. Actual change will only take place upon commit().
  92. // This value will be remembered even if the filter is in disabled() state.
  93. // band The band to set the gain for.
  94. // millibel Gain value in millibel (1/100 of decibel).
  95. void setGain(int band, int32_t millibel);
  96. // Gets gain of a certain band. This is always the last value set (or
  97. // default value after reset).
  98. // band The band to get the gain for.
  99. // returns Gain value in millibel (1/100 of decibel).
  100. int32_t getGain(int band) const;
  101. // Sets cutoff frequency value. Actual change will only take place upon
  102. // commit().
  103. // This value will be remembered even if the filter is in disabled() state.
  104. // band The band to set the frequency for.
  105. // millihertz Frequency value in mHz.
  106. void setFrequency(int band, uint32_t millihertz);
  107. // Gets frequency of a certain band. This is always the last value set (or
  108. // default value after reset).
  109. // band The band to get the frequency for.
  110. // returns Frequency value in mHz.
  111. uint32_t getFrequency(int band) const;
  112. // Sets bandwidth value. Actual change will only take place upon commit().
  113. // This value will be remembered even if the filter is in disabled() state.
  114. // If called on the first or last band, this call is ignored.
  115. // band The band to set the frequency for.
  116. // cents Bandwidth value in cents (1/1200 octave).
  117. void setBandwidth(int band, uint32_t cents);
  118. // Gets bandwidth of a certain band. This is always the last value set (or
  119. // default value after reset). For the first and last bands, 0 is always
  120. // returned.
  121. // band The band to get the bandwidth for.
  122. // returns Bandwidth value in cents (1/1200 octave).
  123. uint32_t getBandwidth(int band) const;
  124. // Gets lower and upper boundaries of a band.
  125. // For the low shelf, the low bound is 0 and the high bound is the band
  126. // frequency.
  127. // For the high shelf, the low bound is the band frequency and the high
  128. // bound is Nyquist.
  129. // For the peaking filters, they are the gain[dB]/2 points.
  130. void getBandRange(int band, uint32_t & low, uint32_t & high) const;
  131. // Gets a human-readable name for a preset ID. Will return "Custom" if
  132. // PRESET_CUSTOM is passed.
  133. // preset The preset ID. Must be less than number of presets.
  134. const char * getPresetName(int preset) const;
  135. // Gets the number of presets.
  136. int getNumPresets() const;
  137. // Gets the currently set preset ID.
  138. // Will return PRESET_CUSTOM in case the EQ parameters have been modified
  139. // manually since a preset was set.
  140. int getPreset() const;
  141. // Sets the current preset by ID.
  142. // All the band parameters will be overridden.
  143. // Change will not be applied until commit() is called.
  144. // preset The preset ID. Must be less than number of presets.
  145. // PRESET_CUSTOM is NOT a valid value here.
  146. void setPreset(int preset);
  147. // Applies all parameter changes done to this point in time.
  148. // If the filter is disabled, the new parameters will take place when it is
  149. // enabled again. Does not introduce artifacts, unless immediate is set.
  150. // immediate Whether to apply change abruptly (ignored if filter is
  151. // disabled).
  152. void commit(bool immediate = false);
  153. // Process a buffer of input data. The input and output should contain
  154. // frameCount * nChannels interlaced samples. Processing can be done
  155. // in-place, by passing the same buffer as both arguments.
  156. // pIn Input buffer.
  157. // pOut Output buffer.
  158. // frameCount Number of frames to produce on each call to process().
  159. void process(const audio_sample_t * pIn, audio_sample_t * pOut,
  160. int frameCount);
  161. // Enables the filter, so it would start processing input. Does not
  162. // introduce artifacts, unless immediate is set.
  163. // immediate Whether to apply change abruptly.
  164. void enable(bool immediate = false);
  165. // Disabled (bypasses) the filter. Does not introduce artifacts, unless
  166. // immediate is set.
  167. // immediate Whether to apply change abruptly.
  168. void disable(bool immediate = false);
  169. // Returns the band with the maximum influence on a given frequency.
  170. // Result is unaffected by whether EQ is enabled or not, or by whether
  171. // changes have been committed or not.
  172. // targetFreq The target frequency, in millihertz.
  173. int getMostRelevantBand(uint32_t targetFreq) const;
  174. private:
  175. // Bottom frequency, in mHz.
  176. static const int kMinFreq = 20000;
  177. // Sample rate, in Hz.
  178. int mSampleRate;
  179. // Number of peaking filters. Total number of bands is +2.
  180. int mNumPeaking;
  181. // Preset configurations.
  182. const PresetConfig * mpPresets;
  183. // Number of elements in mpPresets;
  184. int mNumPresets;
  185. // Current preset.
  186. int mCurPreset;
  187. // Memory space to free when instance is deleted, or NULL if no memory is
  188. // owned.
  189. void * mpMem;
  190. // The low-shelving filter.
  191. AudioShelvingFilter * mpLowShelf;
  192. // The high-shelving filter.
  193. AudioShelvingFilter * mpHighShelf;
  194. // An array of size mNumPeaking of peaking filters.
  195. AudioPeakingFilter * mpPeakingFilters;
  196. // Constructor. Resets the filter (see reset()). Must call init() doing
  197. // anything else.
  198. // pMem Memory buffer for bands.
  199. // nChannels Number of input/output channels (interlaced).
  200. // sampleRate The input/output sample rate, in Hz.
  201. // ownMem Whether pMem is owned by me.
  202. // presets The presets configuration. May be NULL, but in that case the
  203. // client is required not to call preset-related functions.
  204. // This array is owned by the client and is not copied. It
  205. // must be kept valid by the client as long as the instance is
  206. // alive.
  207. // nPresets Number of elements in the presets array.
  208. AudioEqualizer(void * pMem, int nBands, int nChannels, int sampleRate,
  209. bool ownMem, const PresetConfig * presets, int nPresets);
  210. };
  211. }
  212. #endif // AUDIOEQUALIZER_H_