Mozzi  version v1.1.0
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
9  * Attribution-NonCommercial-ShareAlike 4.0 International License.
10  *
11  */
12 
13 #ifndef LOWPASS_H_
14 #define LOWPASS_H_
15 
16 /*
17 simple resonant filter posted to musicdsp.org by Paul Kellett
18 http://www.musicdsp.org/archive.php?classid=3#259
19 
20 // set feedback amount given f and q between 0 and 1
21 fb = q + q/(1.0 - f);
22 
23 // for each sample...
24 buf0 = buf0 + f * (in - buf0 + fb * (buf0 - buf1));
25 buf1 = buf1 + f * (buf0 - buf1);
26 out = buf1;
27 
28 fixed point version of the filter
29 "dave's blog of art and programming" http://www.pawfal.org/dave/blog/2011/09/
30 */
31 
32 // we are using .n fixed point (n bits for the fractional part)
33 #define FX_SHIFT 8
34 #define SHIFTED_1 ((uint8_t)255)
35 
36 /** A resonant low pass filter for audio signals.
37  */
39 {
40 
41 public:
42  /** Constructor.
43  */
44  LowPassFilter() { ; }
45 
46  /** deprecated. Use setCutoffFreqAndResonance(uint8_t cutoff, uint8_t
47  resonance).
48 
49  Set the cut off frequency,
50  @param cutoff use the range 0-255 to represent 0-8191 Hz (AUDIO_RATE/2).
51  Be careful of distortion at the lower end, especially with high resonance.
52  */
53  void setCutoffFreq(uint8_t cutoff)
54  {
55  f = cutoff;
56  fb = q + ucfxmul(q, SHIFTED_1 - cutoff);
57  }
58 
59  /** deprecated. Use setCutoffFreqAndResonance(uint8_t cutoff, uint8_t
60  resonance).
61 
62  Set the resonance. If you hear unwanted distortion, back off the resonance.
63  After setting resonance, you need to call setCuttoffFreq() to hear the change!
64  @param resonance in the range 0-255, with 255 being most resonant.
65  @note Remember to call setCuttoffFreq() after resonance is changed!
66  */
67  void setResonance(uint8_t resonance) { q = resonance; }
68 
69  /**
70  Set the cut off frequency and resonance. Replaces setCutoffFreq() and
71  setResonance(). (Because the internal calculations need to be done whenever either parameter changes.)
72  @param cutoff range 0-255 represents 0-8191 Hz (AUDIO_RATE/2).
73  Be careful of distortion at the lower end, especially with high resonance.
74  @param resonance range 0-255, 255 is most resonant.
75  */
76  void setCutoffFreqAndResonance(uint8_t cutoff, uint8_t resonance)
77  {
78  f = cutoff;
79  q = resonance; // hopefully optimised away when compiled, just here for
80  // backwards compatibility
81  fb = q + ucfxmul(q, SHIFTED_1 - cutoff);
82  }
83 
84  /** Calculate the next sample, given an input signal.
85  @param in the signal input.
86  @return the signal output.
87  @note Timing: about 11us.
88  */
89  // 10.5 to 12.5 us, mostly 10.5 us (was 14us)
90  inline int next(int in)
91  {
92  // setPin13High();
93  buf0 += fxmul(((in - buf0) + fxmul(fb, buf0 - buf1)), f);
94  buf1 += ifxmul(buf0 - buf1, f); // could overflow if input changes fast
95  // setPin13Low();
96  return buf1;
97  }
98 
99 private:
100  uint8_t q;
101  uint8_t f;
102  unsigned int fb;
103  int buf0, buf1;
104 
105  // // multiply two fixed point numbers (returns fixed point)
106  // inline
107  // long fxmul(long a, long b)
108  // {
109  // return (a*b)>>FX_SHIFT;
110  // }
111 
112  // multiply two fixed point numbers (returns fixed point)
113  inline unsigned int ucfxmul(uint8_t a, uint8_t b)
114  {
115  return (((unsigned int)a * b) >> FX_SHIFT);
116  }
117 
118  // multiply two fixed point numbers (returns fixed point)
119  inline int ifxmul(int a, uint8_t b) { return ((a * b) >> FX_SHIFT); }
120 
121  // multiply two fixed point numbers (returns fixed point)
122  inline long fxmul(long a, int b) { return ((a * b) >> FX_SHIFT); }
123 };
124 
125 /**
126 @example 10.Audio_Filters/LowPassFilter/LowPassFilter.ino
127 This example demonstrates the LowPassFilter class.
128 */
129 
130 #endif /* LOWPASS_H_ */
void setResonance(uint8_t resonance)
deprecated.
Definition: LowPassFilter.h:67
#define FX_SHIFT
Definition: LowPassFilter.h:33
A resonant low pass filter for audio signals.
Definition: LowPassFilter.h:38
#define SHIFTED_1
Definition: LowPassFilter.h:34
void setCutoffFreqAndResonance(uint8_t cutoff, uint8_t resonance)
Set the cut off frequency and resonance.
Definition: LowPassFilter.h:76
LowPassFilter()
Constructor.
Definition: LowPassFilter.h:44
int next(int in)
Calculate the next sample, given an input signal.
Definition: LowPassFilter.h:90
void setCutoffFreq(uint8_t cutoff)
deprecated.
Definition: LowPassFilter.h:53