1135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent/* //device/include/server/AudioFlinger/AudioCoefInterpolator.h
2135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent **
3135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent ** Copyright 2007, The Android Open Source Project
4135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent **
5135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent ** Licensed under the Apache License, Version 2.0 (the "License");
6135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent ** you may not use this file except in compliance with the License.
7135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent ** You may obtain a copy of the License at
8135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent **
9135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent **     http://www.apache.org/licenses/LICENSE-2.0
10135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent **
11135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent ** Unless required by applicable law or agreed to in writing, software
12135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent ** distributed under the License is distributed on an "AS IS" BASIS,
13135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent ** See the License for the specific language governing permissions and
15135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent ** limitations under the License.
16135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent */
17135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
18135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent#ifndef ANDROID_AUDIO_COEF_INTERPOLATOR_H
19135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent#define ANDROID_AUDIO_COEF_INTERPOLATOR_H
20135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
21135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent#include "AudioCommon.h"
22135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
23135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurentnamespace android {
24135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
25135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent// A helper class for linear interpolation of N-D -> M-D coefficient tables.
26135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent// This class provides support for out-of-range indexes.
27135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent// Details:
28135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent// The purpose is efficient approximation of a N-dimensional vector to
29135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent// M-dimensional function. The approximation is based on a table of output
30135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent// values on a uniform grid of the input values. Values not on the grid are
31135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent// linearly interpolated.
32135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent// Access to values are done by specifying input values in table index units,
33135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent// having an integer and a fractional part, e.g. retrieving a value from index
34135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent// 1.4 will result in linear interpolation between index 1 and index 2.
35135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurentclass AudioCoefInterpolator {
36135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurentpublic:
37135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // Constructor.
38135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // nInDims      Number of input dimensions (limited to MAX_IN_DIMS).
39135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // inDims       An array of size nInDims with the size of the table on each
40135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    //              respective dimension.
41135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // nOutDims     Number of output dimensions (limited to MAX_OUT_DIMS).
42135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // table        The coefficient table. Should be of size:
43135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    //              inDims[0]*inDims[1]*...*inDims[nInDims-1]*nOutDims, where
44135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    //              func([i,j,k]) = table(i,j,k,:)
45135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    AudioCoefInterpolator(size_t nInDims, const size_t inDims[],
46135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent                          size_t nOutDims, const audio_coef_t * table);
47135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
48135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // Get the value of the approximated function at a given point.
49135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // intCoord     The integer part of the input value. Should be an array of
50135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    //              size nInDims.
51135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // fracCoord    The fractional part of the input value. Should be an array
52135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    //              of size nInDims. This value is in 32-bit precision.
53135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // out          An array for the output value. Should be of size nOutDims.
54135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    void getCoef(const int intCoord[], uint32_t fracCoord[], audio_coef_t out[]);
55135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
56135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurentprivate:
57135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // Maximum allowed number of input dimensions.
58135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    static const size_t MAX_IN_DIMS = 8;
59135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // Maximum allowed number of output dimensions.
60135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    static const size_t MAX_OUT_DIMS = 8;
61135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
62135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // Number of input dimensions.
63135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    size_t mNumInDims;
64135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // Number of input dimensions.
65135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    size_t mInDims[MAX_IN_DIMS];
66135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // The offset between two consecutive indexes of each dimension. This is in
67135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // fact a cumulative product of mInDims (done in reverse).
68135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    size_t mInDimOffsets[MAX_IN_DIMS];
69135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // Number of output dimensions.
70135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    size_t mNumOutDims;
71135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // The coefficient table.
72135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    const audio_coef_t * mTable;
73135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
74135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // A recursive function for getting an interpolated coefficient value.
75135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // The recursion depth is the number of input dimensions.
76135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // At each step, we fetch two interpolated values of the current dimension,
77135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // by two recursive calls to this method for the next dimensions. We then
78135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // linearly interpolate these values over the current dimension.
79135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // index      The linear integer index of the value we need to interpolate.
80135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // fracCoord  A vector of fractional coordinates for each of the input
81135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    //            dimensions.
82135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // out        Where the output should be written. Needs to be of size
83135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    //            mNumOutDims.
84135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // dim        The input dimensions we are currently interpolating. This
85135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    //            value will be increased on recursive calls.
86135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    void getCoefRecurse(size_t index, const uint32_t fracCoord[],
87135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent                        audio_coef_t out[], size_t dim);
88135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
89135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // Scalar interpolation of two data points.
90135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // lo       The first data point.
91135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // hi       The second data point.
92135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // frac     A 32-bit fraction designating the weight of the second point.
93135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    static audio_coef_t interp(audio_coef_t lo, audio_coef_t hi, uint32_t frac);
94135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent};
95135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
96135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent}
97135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
98135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent#endif // ANDROID_AUDIO_COEF_INTERPOLATOR_H
99