1/* //device/include/server/AudioFlinger/AudioCoefInterpolator.h
2 **
3 ** Copyright 2007, 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
18#ifndef ANDROID_AUDIO_COEF_INTERPOLATOR_H
19#define ANDROID_AUDIO_COEF_INTERPOLATOR_H
20
21#include "AudioCommon.h"
22
23namespace android {
24
25// A helper class for linear interpolation of N-D -> M-D coefficient tables.
26// This class provides support for out-of-range indexes.
27// Details:
28// The purpose is efficient approximation of a N-dimensional vector to
29// M-dimensional function. The approximation is based on a table of output
30// values on a uniform grid of the input values. Values not on the grid are
31// linearly interpolated.
32// Access to values are done by specifying input values in table index units,
33// having an integer and a fractional part, e.g. retrieving a value from index
34// 1.4 will result in linear interpolation between index 1 and index 2.
35class AudioCoefInterpolator {
36public:
37    // Constructor.
38    // nInDims      Number of input dimensions (limited to MAX_IN_DIMS).
39    // inDims       An array of size nInDims with the size of the table on each
40    //              respective dimension.
41    // nOutDims     Number of output dimensions (limited to MAX_OUT_DIMS).
42    // table        The coefficient table. Should be of size:
43    //              inDims[0]*inDims[1]*...*inDims[nInDims-1]*nOutDims, where
44    //              func([i,j,k]) = table(i,j,k,:)
45    AudioCoefInterpolator(size_t nInDims, const size_t inDims[],
46                          size_t nOutDims, const audio_coef_t * table);
47
48    // Get the value of the approximated function at a given point.
49    // intCoord     The integer part of the input value. Should be an array of
50    //              size nInDims.
51    // fracCoord    The fractional part of the input value. Should be an array
52    //              of size nInDims. This value is in 32-bit precision.
53    // out          An array for the output value. Should be of size nOutDims.
54    void getCoef(const int intCoord[], uint32_t fracCoord[], audio_coef_t out[]);
55
56private:
57    // Maximum allowed number of input dimensions.
58    static const size_t MAX_IN_DIMS = 8;
59    // Maximum allowed number of output dimensions.
60    static const size_t MAX_OUT_DIMS = 8;
61
62    // Number of input dimensions.
63    size_t mNumInDims;
64    // Number of input dimensions.
65    size_t mInDims[MAX_IN_DIMS];
66    // The offset between two consecutive indexes of each dimension. This is in
67    // fact a cumulative product of mInDims (done in reverse).
68    size_t mInDimOffsets[MAX_IN_DIMS];
69    // Number of output dimensions.
70    size_t mNumOutDims;
71    // The coefficient table.
72    const audio_coef_t * mTable;
73
74    // A recursive function for getting an interpolated coefficient value.
75    // The recursion depth is the number of input dimensions.
76    // At each step, we fetch two interpolated values of the current dimension,
77    // by two recursive calls to this method for the next dimensions. We then
78    // linearly interpolate these values over the current dimension.
79    // index      The linear integer index of the value we need to interpolate.
80    // fracCoord  A vector of fractional coordinates for each of the input
81    //            dimensions.
82    // out        Where the output should be written. Needs to be of size
83    //            mNumOutDims.
84    // dim        The input dimensions we are currently interpolating. This
85    //            value will be increased on recursive calls.
86    void getCoefRecurse(size_t index, const uint32_t fracCoord[],
87                        audio_coef_t out[], size_t dim);
88
89    // Scalar interpolation of two data points.
90    // lo       The first data point.
91    // hi       The second data point.
92    // frac     A 32-bit fraction designating the weight of the second point.
93    static audio_coef_t interp(audio_coef_t lo, audio_coef_t hi, uint32_t frac);
94};
95
96}
97
98#endif // ANDROID_AUDIO_COEF_INTERPOLATOR_H
99