Reverb system using parallel structure of comb filters and cascade structure of allpass filters for ST Nucleo F401RE.

Dependencies:   UITDSP_ADDA mbed

Committer:
MikamiUitOpen
Date:
Fri Oct 31 07:34:23 2014 +0000
Revision:
1:51c85e0a6e1e
Parent:
0:72e321f29769
2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MikamiUitOpen 0:72e321f29769 1 //--------------------------------------------------------------
MikamiUitOpen 0:72e321f29769 2 // Comb filter
MikamiUitOpen 0:72e321f29769 3 // 2014/10/23, Copyright (c) 2014 MIKAMI, Naoki
MikamiUitOpen 0:72e321f29769 4 //--------------------------------------------------------------
MikamiUitOpen 0:72e321f29769 5
MikamiUitOpen 0:72e321f29769 6 #ifndef REVERB_UNIT_HPP
MikamiUitOpen 0:72e321f29769 7 #define REVERB_UNIT_HPP
MikamiUitOpen 0:72e321f29769 8
MikamiUitOpen 0:72e321f29769 9 #include "mbed.h"
MikamiUitOpen 0:72e321f29769 10
MikamiUitOpen 0:72e321f29769 11 // Base class for reverb unit
MikamiUitOpen 0:72e321f29769 12 template<int delay> class Reverb
MikamiUitOpen 0:72e321f29769 13 {
MikamiUitOpen 0:72e321f29769 14 private:
MikamiUitOpen 0:72e321f29769 15 int ptr_;
MikamiUitOpen 0:72e321f29769 16 float un_[delay]; // for delay
MikamiUitOpen 0:72e321f29769 17 protected:
MikamiUitOpen 0:72e321f29769 18 float Get() { return un_[ptr_]; }
MikamiUitOpen 0:72e321f29769 19 void Set(float x)
MikamiUitOpen 0:72e321f29769 20 {
MikamiUitOpen 0:72e321f29769 21 un_[ptr_] = x;
MikamiUitOpen 0:72e321f29769 22 if (++ptr_ >= delay) ptr_ = 0;
MikamiUitOpen 0:72e321f29769 23 }
MikamiUitOpen 0:72e321f29769 24 public:
MikamiUitOpen 0:72e321f29769 25 // Constructor
MikamiUitOpen 0:72e321f29769 26 Reverb() : ptr_(0) { Clear(); }
MikamiUitOpen 0:72e321f29769 27
MikamiUitOpen 0:72e321f29769 28 // Execute of filter (pure virtual function)
MikamiUitOpen 0:72e321f29769 29 virtual float Execute(float x) = 0;
MikamiUitOpen 0:72e321f29769 30
MikamiUitOpen 0:72e321f29769 31 // Clear internal delay elements
MikamiUitOpen 0:72e321f29769 32 void Clear()
MikamiUitOpen 0:72e321f29769 33 { for (int n=0; n<delay; n++) un_[n] = 0; }
MikamiUitOpen 0:72e321f29769 34 };
MikamiUitOpen 0:72e321f29769 35
MikamiUitOpen 0:72e321f29769 36 // Comb filter
MikamiUitOpen 0:72e321f29769 37 template<int delay> class CombFilter : public Reverb<delay>
MikamiUitOpen 0:72e321f29769 38 {
MikamiUitOpen 0:72e321f29769 39 private:
MikamiUitOpen 1:51c85e0a6e1e 40 const float GC_;
MikamiUitOpen 0:72e321f29769 41 public:
MikamiUitOpen 0:72e321f29769 42 // Constructor
MikamiUitOpen 1:51c85e0a6e1e 43 CombFilter(float g) : GC_(g) {}
MikamiUitOpen 0:72e321f29769 44
MikamiUitOpen 0:72e321f29769 45 // Execute comb filter
MikamiUitOpen 0:72e321f29769 46 virtual float Execute(float x)
MikamiUitOpen 0:72e321f29769 47 {
MikamiUitOpen 0:72e321f29769 48 float yn = Reverb<delay>::Get();
MikamiUitOpen 1:51c85e0a6e1e 49 Reverb<delay>::Set(x + GC_*yn);
MikamiUitOpen 0:72e321f29769 50 return yn;
MikamiUitOpen 0:72e321f29769 51 }
MikamiUitOpen 0:72e321f29769 52 };
MikamiUitOpen 0:72e321f29769 53
MikamiUitOpen 0:72e321f29769 54 // Allpass filter
MikamiUitOpen 0:72e321f29769 55 template<int delay> class AllpassFilter : public Reverb<delay>
MikamiUitOpen 0:72e321f29769 56 {
MikamiUitOpen 0:72e321f29769 57 private:
MikamiUitOpen 0:72e321f29769 58 const float G0_;
MikamiUitOpen 0:72e321f29769 59 public:
MikamiUitOpen 0:72e321f29769 60 // Constructor
MikamiUitOpen 0:72e321f29769 61 AllpassFilter(float g) : G0_(g) {}
MikamiUitOpen 0:72e321f29769 62
MikamiUitOpen 0:72e321f29769 63 // Execute comb filter
MikamiUitOpen 0:72e321f29769 64 virtual float Execute(float x)
MikamiUitOpen 0:72e321f29769 65 {
MikamiUitOpen 0:72e321f29769 66 float un = x + G0_*Reverb<delay>::Get();
MikamiUitOpen 0:72e321f29769 67 float yn = -G0_*un + Reverb<delay>::Get();
MikamiUitOpen 0:72e321f29769 68 Reverb<delay>::Set(un);
MikamiUitOpen 0:72e321f29769 69 return yn;
MikamiUitOpen 0:72e321f29769 70 }
MikamiUitOpen 0:72e321f29769 71 };
MikamiUitOpen 0:72e321f29769 72
MikamiUitOpen 0:72e321f29769 73 #endif // REVERB_UNIT_HPP
MikamiUitOpen 0:72e321f29769 74