1 /* //device/include/server/AudioFlinger/AudioPeakingFilter.cpp
3 ** Copyright 2007, The Android Open Source Project
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
9 ** http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "AudioPeakingFilter.h"
19 #include "AudioCommon.h"
20 #include "EffectsMath.h"
25 #define LIKELY( exp ) (__builtin_expect( (exp) != 0, true ))
26 #define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false ))
29 // Format of the coefficient table:
30 // kCoefTable[freq][gain][bw][coef]
31 // freq - peak frequency, in octaves below Nyquist,from -9 to -1.
32 // gain - gain, in millibel, starting at -9600, jumps of 1024, to 4736 millibel.
33 // bw - bandwidth, starting at 1 cent, jumps of 1024, to 3073 cents.
39 static const size_t kInDims[3] = {9, 15, 4};
40 static const audio_coef_t kCoefTable[9*15*4*5] = {
41 #include "AudioPeakingFilterCoef.inl"
44 AudioCoefInterpolator AudioPeakingFilter::mCoefInterp(3, kInDims, 5, (const audio_coef_t*) kCoefTable);
46 AudioPeakingFilter::AudioPeakingFilter(int nChannels, int sampleRate)
47 : mBiquad(nChannels, sampleRate) {
48 configure(nChannels, sampleRate);
52 void AudioPeakingFilter::configure(int nChannels, int sampleRate) {
53 mNiquistFreq = sampleRate * 500;
54 mFrequencyFactor = ((1ull) << 42) / mNiquistFreq;
55 mBiquad.configure(nChannels, sampleRate);
56 setFrequency(mNominalFrequency);
60 void AudioPeakingFilter::reset() {
67 void AudioPeakingFilter::setFrequency(uint32_t millihertz) {
68 mNominalFrequency = millihertz;
69 if (UNLIKELY(millihertz > mNiquistFreq / 2)) {
70 millihertz = mNiquistFreq / 2;
72 uint32_t normFreq = static_cast<uint32_t>(
73 (static_cast<uint64_t>(millihertz) * mFrequencyFactor) >> 10);
74 if (LIKELY(normFreq > (1 << 23))) {
75 mFrequency = (Effects_log2(normFreq) - ((32-9) << 15)) << (FREQ_PRECISION_BITS - 15);
81 void AudioPeakingFilter::setGain(int32_t millibel) {
82 mGain = millibel + 9600;
85 void AudioPeakingFilter::setBandwidth(uint32_t cents) {
86 mBandwidth = cents - 1;
89 void AudioPeakingFilter::commit(bool immediate) {
90 audio_coef_t coefs[5];
92 mFrequency >> FREQ_PRECISION_BITS,
93 mGain >> GAIN_PRECISION_BITS,
94 mBandwidth >> BANDWIDTH_PRECISION_BITS
96 uint32_t fracCoord[3] = {
97 mFrequency << (32 - FREQ_PRECISION_BITS),
98 static_cast<uint32_t>(mGain) << (32 - GAIN_PRECISION_BITS),
99 mBandwidth << (32 - BANDWIDTH_PRECISION_BITS)
101 mCoefInterp.getCoef(intCoord, fracCoord, coefs);
102 mBiquad.setCoefs(coefs, immediate);
105 void AudioPeakingFilter::getBandRange(uint32_t & low, uint32_t & high) const {
106 // Half bandwidth, in octaves, 15-bit precision
107 int32_t halfBW = (((mBandwidth + 1) / 2) << 15) / 1200;
109 low = static_cast<uint32_t>((static_cast<uint64_t>(mNominalFrequency) * Effects_exp2(-halfBW + (16 << 15))) >> 16);
110 if (UNLIKELY(halfBW >= (16 << 15))) {
113 high = static_cast<uint32_t>((static_cast<uint64_t>(mNominalFrequency) * Effects_exp2(halfBW + (16 << 15))) >> 16);
114 if (UNLIKELY(high > mNiquistFreq)) {