1 /* 2 * Copyright (C) 2016 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 * This code was translated from the JSyn Java code. 17 * JSyn is Copyright 2009 Phil Burk, Mobileer Inc 18 * JSyn is licensed under the Apache License, Version 2.0 19 */ 20 21 #ifndef SYNTHMARK_LOOKUP_TABLE_H 22 #define SYNTHMARK_LOOKUP_TABLE_H 23 24 #include <cstdint> 25 #include "SynthTools.h" 26 27 namespace marksynth { 28 29 class LookupTable { 30 public: LookupTable(int32_t numEntries)31 LookupTable(int32_t numEntries) 32 : mNumEntries(numEntries) 33 {} 34 ~LookupTable()35 virtual ~LookupTable() { 36 delete[] mTable; 37 } 38 fillTable()39 void fillTable() { 40 // Add 2 guard points for interpolation and roundoff error. 41 int tableSize = mNumEntries + 2; 42 mTable = new float[tableSize]; 43 // Fill the table with calculated values 44 float scale = 1.0f / mNumEntries; 45 for (int i = 0; i < tableSize; i++) { 46 float value = calculate(i * scale); 47 mTable[i] = value; 48 } 49 } 50 51 /** 52 * @param input normalized between 0.0 and 1.0 53 */ lookup(float input)54 float lookup(float input) { 55 float fractionalTableIndex = input * mNumEntries; 56 int32_t index = (int) floor(fractionalTableIndex); 57 float fraction = fractionalTableIndex - index; 58 float baseValue = mTable[index]; 59 float value = baseValue 60 + (fraction * (mTable[index + 1] - baseValue)); 61 return value; 62 } 63 64 virtual float calculate(float input) = 0; 65 66 private: 67 int32_t mNumEntries; 68 synth_float_t *mTable; 69 }; 70 71 }; 72 #endif // SYNTHMARK_LOOKUP_TABLE_H 73