Mozzi  version 2015-05-11-20:23
sound synthesis library for Arduino
 All Classes Functions Typedefs Groups
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 Attribution-NonCommercial-ShareAlike 4.0 International License.
9  *
10  */
11 
12 
44 #ifndef STATEVARIABLE_H_
45 #define STATEVARIABLE_H_
46 
47 #include "Arduino.h"
48 #include "util/atomic.h"
49 #include "mozzi_fixmath.h"
50 #include "math.h"
51 #include "mozzi_utils.h"
52 #include "meta.h"
53 
54 enum filter_types {LOWPASS,BANDPASS,HIGHPASS,NOTCH};
55 
64 template <int8_t FILTER_TYPE>
66 {
67 
68 public:
69 
70 
74  {
75  }
76 
77 
78 
86  void setResonance(Q0n8 resonance){
87  // qvalue goes from 255 to 0, representing .999 to 0 in fixed point
88  // lower q, more resonance
89  q = resonance;
90  //ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
91  //{
92  scale = (Q0n8)sqrt((unsigned int) resonance<<8);
93  //}
94  }
95 
96 
97 
105  void setCentreFreq(unsigned int centre_freq){
106  // simple frequency tuning with error towards nyquist (reference? where did this come from?)
107  //f = (Q1n15)(((Q16n16_2PI*centre_freq)>>AUDIO_RATE_AS_LSHIFT)>>1);
108  f = (Q15n16)((Q16n16_2PI*centre_freq)>>(AUDIO_RATE_AS_LSHIFT)); // this works best for now
109  //f = (Q15n16)(((Q16n16_2PI*centre_freq)<<(16-AUDIO_RATE_AS_LSHIFT))>>16); // a small shift left and a round 16 right is faster than big non-uint8_t-aligned right in one go
110  //float ff = Q16n16_to_float(((Q16n16_PI*centre_freq))>>AUDIO_RATE_AS_LSHIFT);
111  //f = float_to_Q15n16(2.0f *sin(ff));
112  //}
113  }
114 
115 
121  inline
122  int next(int input)
123  {
124  // chooses a different next() function depending on whether the
125  // filter is declared as LOWPASS, BANDPASS, HIGHPASS or NOTCH.
126  // See meta.h.
127  return next(input, Int2Type<FILTER_TYPE>());
128  }
129 
130 
131 
132 
133 private:
134  int low, band;
135  Q0n8 q,scale;
136  volatile Q15n16 f;
137 
138 
144  inline
145  int next(int input, Int2Type<LOWPASS>)
146  {
147  //setPin13High();
148  low += ((f*band)>>16);
149  int high = (((long)input - low - (((long)band * q)>>8))*scale)>>8;
150  band += ((f*high)>>16);
151  //int notch = high + low;
152  //setPin13Low();
153  return low;
154  }
155 
156 
162  inline
163  int next(int input, Int2Type<BANDPASS>)
164  {
165  //setPin13High();
166  low += ((f*band)>>16);
167  int high = (((long)input - low - (((long)band * q)>>8))*scale)>>8;
168  band += ((f*high)>>16);
169  //int notch = high + low;
170  //setPin13Low();
171  return band;
172  }
173 
174 
175 
181  inline
182  int next(int input, Int2Type<HIGHPASS>)
183  {
184  //setPin13High();
185  low += ((f*band)>>16);
186  int high = (((long)input - low - (((long)band * q)>>8))*scale)>>8;
187  band += ((f*high)>>16);
188  //int notch = high + low;
189  //setPin13Low();
190  return high;
191  }
192 
193 
194 
200  inline
201  int next(int input, Int2Type<NOTCH>)
202  {
203  //setPin13High();
204  low += ((f*band)>>16);
205  int high = (((long)input - low - (((long)band * q)>>8))*scale)>>8;
206  band += ((f*high)>>16);
207  int notch = high + low;
208  //setPin13Low();
209  return notch;
210  }
211 
212 };
213 
219 #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:86
Enables you to instantiate a template based on an integer value.
Definition: meta.h:20
StateVariable()
Constructor.
Definition: StateVariable.h:73
int next(int input)
Calculate the next sample, given an input signal.
A State Variable filter which offers 12db resonant low, high, bandpass and notch modes.
Definition: StateVariable.h:65
#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.