1 /* 2 * Copyright 2019 Google LLC 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 * https://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 #ifndef ANDROID_FXLAB_DELAYLINEEFFECT_H 18 #define ANDROID_FXLAB_DELAYLINEEFFECT_H 19 20 #include <functional> 21 22 #include "utils/SineWave.h" 23 #include "utils/DelayLine.h" 24 25 // Abstract class for implementing delay line based effects 26 // This functor retains state (it must be used sequentially) 27 // Effects are float, mono, 48000 hz 28 template <class iter_type> 29 class DelayLineEffect { 30 31 public: 32 // delay > 0, depth in samples, mod is control signal DelayLineEffect(float blend,float feedForward,float feedBack,int delay,int depth,std::function<float ()> && mod)33 DelayLineEffect(float blend, float feedForward, float feedBack, int delay, int depth, std::function<float()> &&mod) : 34 kBlend(blend), 35 kFeedForward(feedForward), 36 kFeedBack(feedBack), 37 kDelay(delay), 38 kDepth(depth), 39 mMod(mod) { } 40 41 operator()42 void operator () (typename std::iterator_traits<iter_type>::reference x) { 43 auto delayInput = x + kFeedBack * delayLine[kTap]; 44 auto variableDelay = mMod() * kDepth + kTap; 45 int index = static_cast<int>(variableDelay); 46 auto fracComp = 1 - (variableDelay - index); 47 //linear 48 // auto interpolated = fracComp * delayLine[index] + (1 - fracComp) * delayLine[index + 1]; 49 // all - pass 50 float interpolated = fracComp * delayLine[index] + delayLine[index + 1] 51 - fracComp * prevInterpolated; 52 53 prevInterpolated = interpolated; 54 delayLine.push(delayInput); 55 x = interpolated * kFeedForward + kBlend * delayInput; 56 } operator()57 void operator () (iter_type begin, iter_type end) { 58 for (; begin != end; ++begin) { 59 operator()(*begin); 60 } 61 } 62 private: 63 // Weights 64 const float kBlend; 65 const float kFeedForward; 66 const float kFeedBack; 67 const int kDelay; 68 const int kDepth; 69 const int kTap = kDelay + kDepth; 70 71 // Control function 72 const std::function<float()> mMod; 73 74 // Memory 75 float prevInterpolated = 0; // for all pass interp 76 const size_t kDelayLineSize = kTap + kDepth + 1; // index one is immediate prev sample 77 DelayLine<typename std::iterator_traits<iter_type>::value_type> delayLine {kDelayLineSize}; 78 }; 79 #endif //ANDROID_FXLAB_DELAYLINEEFFECT_H 80