Mozzi  version 2016-12-11-17:03
sound synthesis library for Arduino
LowPassFilter.h
1 /*
2  * LowPassFilter.h
3  *
4  * Copyright 2012 Tim Barrass
5  *
6  * This file is part of Mozzi.
7  *
8  * Mozzi is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
9  *
10  */
11 
12 #ifndef LOWPASS_H_
13 #define LOWPASS_H_
14 
15 /*
16 simple resonant filter posted to musicdsp.org by Paul Kellett http://www.musicdsp.org/archive.php?classid=3#259
17 
18 // set feedback amount given f and q between 0 and 1
19 fb = q + q/(1.0 - f);
20 
21 // for each sample...
22 buf0 = buf0 + f * (in - buf0 + fb * (buf0 - buf1));
23 buf1 = buf1 + f * (buf0 - buf1);
24 out = buf1;
25 
26 fixed point version of the filter
27 "dave's blog of art and programming" http://www.pawfal.org/dave/blog/2011/09/
28 */
29 
30 
31 // we are using .n fixed point (n bits for the fractional part)
32 #define FX_SHIFT 8
33 #define SHIFTED_1 ((uint8_t) 255)
34 
38 {
39 
40 public:
41 
42 
46  }
47 
48 
53  void setCutoffFreq(uint8_t cutoff)
54  {
55  f = cutoff;
56  fb = q+ucfxmul(q, SHIFTED_1 - cutoff);
57  }
58 
59 
63  void setResonance(uint8_t resonance)
64  {
65  q = resonance;
66  }
67 
73  // 10.5 to 12.5 us, mostly 10.5 us (was 14us)
74  inline
75  int next(int in)
76  {
77  //setPin13High();
78  buf0+=fxmul(((in - buf0) + fxmul(fb, buf0-buf1)), f);
79  buf1+=ifxmul(buf0-buf1, f); // could overflow if input changes fast
80  //setPin13Low();
81  return buf1;
82  }
83 
84 
85 private:
86  uint8_t q;
87  uint8_t f;
88  unsigned int fb;
89  int buf0,buf1;
90 
91 
92  // // multiply two fixed point numbers (returns fixed point)
93  // inline
94  // long fxmul(long a, long b)
95  // {
96  // return (a*b)>>FX_SHIFT;
97  // }
98 
99  // multiply two fixed point numbers (returns fixed point)
100  inline
101  unsigned int ucfxmul(uint8_t a, uint8_t b)
102  {
103  return (((unsigned int)a*b)>>FX_SHIFT);
104  }
105 
106  // multiply two fixed point numbers (returns fixed point)
107  inline
108  int ifxmul(int a, uint8_t b)
109  {
110  return ((a*b)>>FX_SHIFT);
111  }
112 
113  // multiply two fixed point numbers (returns fixed point)
114  inline
115  long fxmul(long a, int b)
116  {
117  return ((a*b)>>FX_SHIFT);
118  }
119 
120 };
121 
127 #endif /* LOWPASS_H_ */
void setResonance(uint8_t resonance)
Set the resonance.
Definition: LowPassFilter.h:63
A resonant low pass filter for audio signals.
Definition: LowPassFilter.h:37
LowPassFilter()
Constructor.
Definition: LowPassFilter.h:45
int next(int in)
Calculate the next sample, given an input signal.
Definition: LowPassFilter.h:75
void setCutoffFreq(uint8_t cutoff)
Set the cut off frequency,.
Definition: LowPassFilter.h:53