xref: /aosp_15_r20/external/oboe/apps/OboeTester/app/src/main/cpp/InterpolatingDelayLine.cpp (revision 05767d913155b055644481607e6fa1e35e2fe72c)
1 /*
2  * Copyright 2019 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 
17 #include <algorithm>
18 
19 #include "InterpolatingDelayLine.h"
20 
InterpolatingDelayLine(int32_t delaySize)21 InterpolatingDelayLine::InterpolatingDelayLine(int32_t delaySize) {
22     mDelaySize = delaySize;
23     mDelayLine = std::make_unique<float[]>(delaySize);
24 }
25 
process(float delay,float input)26 float InterpolatingDelayLine::process(float delay, float input) {
27     float *writeAddress = mDelayLine.get() + mCursor;
28     *writeAddress = input;
29     mDelayLine.get()[mCursor] = input;
30     int32_t delayInt = std::min(mDelaySize - 1, (int32_t) delay);
31     int32_t readIndex = mCursor - delayInt;
32     if (readIndex < 0) {
33         readIndex += mDelaySize;
34     }
35     // TODO interpolate
36     float *readAddress = mDelayLine.get() + readIndex;
37     float output = *readAddress;
38     mCursor++;
39     if (mCursor >= mDelaySize) {
40         mCursor = 0;
41     }
42     return output;
43 };
44