Mozzi  version v1.1.0
sound synthesis library for Arduino
StateVariable.h
1 /*
2  * StateVariable.h
3  *
4  * This implementation 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 /**
14 State Variable Filter (approximation of Chamberlin version)
15 Informed by pseudocode at http://www.musicdsp.org/showone.php?id=23 and
16 http://www.musicdsp.org/showone.php?id=142. Here's the original:
17 ---------------------
18 cutoff = cutoff freq in Hz
19 fs = sampling frequency //(e.g. 44100Hz)
20 f = 2 sin (pi * cutoff / fs) //[approximately]
21 q = resonance/bandwidth [0 < q <= 1] most res: q=1, less: q=0
22 low = lowpass output
23 high = highpass output
24 band = bandpass output
25 notch = notch output
26 
27 scale = q
28 
29 low=high=band=0;
30 
31 //--beginloop
32 low = low + f * band;
33 high = scale * input - low - q*band;
34 band = f * high + band;
35 notch = high + low;
36 //--endloop
37 ----------------------
38 References :
39 Hal Chamberlin, Musical Applications of Microprocessors, 2nd Ed, Hayden Book
40 Company 1985. pp 490-492. Jon Dattorro, Effect Design Part 1, J. Audio Eng.
41 Soc., Vol 45, No. 9, 1997 September
42 */
43 
44 #ifndef STATEVARIABLE_H_
45 #define STATEVARIABLE_H_
46 
47 #include "Arduino.h"
48 #include "math.h"
49 #include "meta.h"
50 #include "mozzi_fixmath.h"
51 #include "mozzi_utils.h"
52 #include "ResonantFilter.h"
53 
54 //enum filter_types { LOWPASS, BANDPASS, HIGHPASS, NOTCH };
55 
56 /** A State Variable filter which offers 12db resonant low, high, bandpass and
57 notch modes.
58 @tparam FILTER_TYPE choose between LOWPASS, BANDPASS, HIGHPASS and NOTCH.
59 @note To save processing time, this version of the filter does not saturate
60 internally, so any resonant peaks are unceremoniously truncated. It may be
61 worth adding code to constrain the internal variables to enable resonant
62 saturating effects.
63 @todo Try adding code to constrain the internal variables to enable resonant
64 saturating effects.
65 */
66 template <int8_t FILTER_TYPE> class StateVariable {
67 
68 public:
69  /** Constructor.
70  */
72 
73  /** Set how resonant the filter will be.
74  @param resonance a byte value between 1 and 255.
75  The lower this value is, the more resonant the filter.
76  At very low values, the filter can output loud peaks which can exceed
77  Mozzi's output range, so you may need to attenuate the output in your sketch.
78  @note Timing < 500 ns
79  */
80  void setResonance(Q0n8 resonance) {
81  // qvalue goes from 255 to 0, representing .999 to 0 in fixed point
82  // lower q, more resonance
83  q = resonance;
84  scale = (Q0n8)sqrt((unsigned int)resonance << 8);
85  }
86 
87  /** Set the centre or corner frequency of the filter.
88  @param centre_freq 20 - 4096 Hz (AUDIO_RATE/4).
89  This will be the cut-off frequency for LOWPASS and HIGHPASS, and the
90  centre frequency to pass or reduce for BANDPASS and NOTCH.
91  @note Timing 25-30us
92  @note The frequency calculation is VERY "approximate". This really needs to
93  be fixed.
94  */
95  void setCentreFreq(unsigned int centre_freq) {
96  // simple frequency tuning with error towards nyquist (reference? where did
97  // this come from?)
98  // f = (Q1n15)(((Q16n16_2PI*centre_freq)>>AUDIO_RATE_AS_LSHIFT)>>1);
99  f = (Q15n16)((Q16n16_2PI * centre_freq) >>
100  (AUDIO_RATE_AS_LSHIFT)); // this works best for now
101  // f = (Q15n16)(((Q16n16_2PI*centre_freq)<<(16-AUDIO_RATE_AS_LSHIFT))>>16);
102  // // a small shift left and a round 16 right is faster than big
103  // non-byte-aligned right in one go float ff =
104  // Q16n16_to_float(((Q16n16_PI*centre_freq))>>AUDIO_RATE_AS_LSHIFT); f =
105  // float_to_Q15n16(2.0f *sin(ff));
106  }
107 
108  /** Calculate the next sample, given an input signal.
109  @param input the signal input.
110  @return the signal output.
111  @note Timing: 16 - 20 us
112  */
113  inline int next(int input) {
114  // chooses a different next() function depending on whether the
115  // filter is declared as LOWPASS, BANDPASS, HIGHPASS or NOTCH.
116  // See meta.h.
117  return next(input, Int2Type<FILTER_TYPE>());
118  }
119 
120 private:
121  int low, band;
122  Q0n8 q, scale;
123  volatile Q15n16 f;
124 
125  /** Calculate the next sample, given an input signal.
126  @param in the signal input.
127  @return the signal output.
128  @note Timing: 16 - 20 us
129  */
130  inline int next(int input, Int2Type<LOWPASS>) {
131  // setPin13High();
132  low += ((f * band) >> 16);
133  int high = (((long)input - low - (((long)band * q) >> 8)) * scale) >> 8;
134  band += ((f * high) >> 16);
135  // int notch = high + low;
136  // setPin13Low();
137  return low;
138  }
139 
140  /** Calculate the next sample, given an input signal.
141  @param input the signal input.
142  @return the signal output.
143  @note Timing:
144  */
145  inline int next(int input, Int2Type<BANDPASS>) {
146  // setPin13High();
147  low += ((f * band) >> 16);
148  int high = (((long)input - low - (((long)band * q) >> 8)) * scale) >> 8;
149  band += ((f * high) >> 16);
150  // int notch = high + low;
151  // setPin13Low();
152  return band;
153  }
154 
155  /** Calculate the next sample, given an input signal.
156  @param input the signal input.
157  @return the signal output.
158  @note Timing:
159  */
160  inline int next(int input, Int2Type<HIGHPASS>) {
161  // setPin13High();
162  low += ((f * band) >> 16);
163  int high = (((long)input - low - (((long)band * q) >> 8)) * scale) >> 8;
164  band += ((f * high) >> 16);
165  // int notch = high + low;
166  // setPin13Low();
167  return high;
168  }
169 
170  /** Calculate the next sample, given an input signal.
171  @param input the signal input.
172  @return the signal output.
173  @note Timing: 16 - 20 us
174  */
175  inline int next(int input, Int2Type<NOTCH>) {
176  // setPin13High();
177  low += ((f * band) >> 16);
178  int high = (((long)input - low - (((long)band * q) >> 8)) * scale) >> 8;
179  band += ((f * high) >> 16);
180  int notch = high + low;
181  // setPin13Low();
182  return notch;
183  }
184 };
185 
186 /**
187 @example 11.Audio_Filters/StateVariableFilter/StateVariableFilter.ino
188 This example demonstrates the StateVariable class.
189 */
190 
191 #endif /* STATEVARIABLE_H_ */
int32_t Q15n16
signed fractional number using 15 integer bits and 16 fractional bits, represents -32767...
Definition: mozzi_fixmath.h:40
void setResonance(Q0n8 resonance)
Set how resonant the filter will be.
Definition: StateVariable.h:80
Enables you to instantiate a template based on an integer value.
Definition: meta.h:20
StateVariable()
Constructor.
Definition: StateVariable.h:71
int next(int input)
Calculate the next sample, given an input signal.
#define AUDIO_RATE_AS_LSHIFT
Definition: MozziGuts.h:193
State Variable Filter (approximation of Chamberlin version) Informed by pseudocode at http://www...
Definition: StateVariable.h:66
#define Q16n16_2PI
2*PI in Q16n16 format
Definition: mozzi_fixmath.h:69
uint8_t Q0n8
unsigned fractional number using 8 fractional bits, represents 0.0 to 0.996
Definition: mozzi_fixmath.h:27
void setCentreFreq(unsigned int centre_freq)
Set the centre or corner frequency of the filter.
Definition: StateVariable.h:95