![]() |
Mozzi
alpha 0.01.1o
sound synthesis library for Arduino
|
00001 /* 00002 based on http://www.musicdsp.org/showone.php?id=23 00003 00004 State variable filter: Digital approximation of Chamberlin two-pole low pass. 00005 Type : 12db resonant low, high, notch or bandpass 00006 References : Effect Design Part 1, Jon Dattorro, J. Audio Eng. Soc., Vol 45, No. 9, 1997 September 00007 00008 00009 //cutoff: 20 - 4096Hz; 00010 //qvalue: 1.0 - 100.0; 00011 00012 */ 00013 00014 #ifndef STATEVARIABLE_H_ 00015 #define STATEVARIABLE_H_ 00016 00017 #include "Arduino.h" 00018 #include "util/atomic.h" 00019 #include "fixedMath.h" 00020 #include "math.h" 00021 #include "utils.h" 00022 //#include "fastSqrt.cpp" 00023 00024 00025 enum filter_types {LOWPASS,BANDPASS,HIGHPASS,NOTCH}; 00026 /* 00027 #define LOWPASS ((byte) 0) 00028 #define HIGHPASS ((byte) 1) 00029 #define BANDPASS ((byte) 2) 00030 #define NOTCH ((byte) 3) 00031 */ 00032 00033 00034 template <char FILTER_TYPE> 00035 class StateVariable 00036 {} 00037 ; 00038 00039 00041 template <(char)LOWPASS> 00042 class StateVariable 00043 { 00044 00045 public: 00046 00047 00050 StateVariable() 00051 { 00052 } 00053 00054 00055 void setResonance(float qvalue){ 00056 // where Q1 goes from 2 to 0, ie qvalue goes from .5 to infinity 00057 float Q1 = 1/qvalue; 00058 q = float_to_Q0n8(Q1); 00059 scale = float_to_Q0n8(sqrt(Q1)); 00060 } 00061 00062 00063 00064 void setCentreFreq(float centre_freq){ 00065 // simple frequency tuning with error towards nyquist 00066 // F is the filter's center frequency 00067 float F1 = 2*PI*centre_freq/(float)AUDIO_RATE; 00068 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) 00069 { 00070 f = float_to_Q1n15(F1); 00071 } 00072 } 00073 00074 00075 00081 inline 00082 int next(int input) 00083 { 00084 //SET_PIN13_HIGH; 00085 low += (((long)band * f)>>15); 00086 int high = (((long)input - low - (((long)band * q)>>8))*scale)>>8; 00087 band += (((long)high * f)>>15); 00088 //int notch = high + low; 00089 00090 //SET_PIN13_LOW; 00091 /* 00092 #if (FILTER_TYPE == LOWPASS) 00093 00094 out = low; 00095 #elif (FILTER_TYPE == BANDPASS) 00096 out = band; 00097 #elif (FILTER_TYPE == HIGHPASS) 00098 out = high; 00099 #elif (FILTER_TYPE == NOTCH) 00100 out = notch; 00101 #endif 00102 */ 00103 return low; 00104 } 00105 00106 00107 00108 private: 00109 int low, band; 00110 Q0n8 q,scale; 00111 Q1n15 f; 00112 }; 00113 00114 00115 00116 00117 00118 00119 00120 00121 00122 00123 #endif /* STATEVARIABLE_H_ */ 00124 00125 /* 00126 00127 http://www.musicdsp.org/showone.php?id=142 00128 00129 State Variable Filter (Chamberlin version) 00130 References : Hal Chamberlin, "Musical Applications of Microprocessors," 2nd Ed, Hayden Book Company 1985. pp 490-492.Code : 00131 //Input/Output 00132 I - input sample 00133 L - lowpass output sample 00134 B - bandpass output sample 00135 H - highpass output sample 00136 N - notch output sample 00137 F1 - Frequency control parameter 00138 Q1 - Q control parameter 00139 D1 - delay associated with bandpass output 00140 D2 - delay associated with low-pass output 00141 00142 // parameters: 00143 Q1 = 1/Q 00144 // where Q1 goes from 2 to 0, ie Q goes from .5 to infinity 00145 00146 // simple frequency tuning with error towards nyquist 00147 // F is the filter's center frequency, and Fs is the sampling rate 00148 F1 = 2*pi*F/Fs 00149 00150 // ideal tuning: 00151 F1 = 2 * sin( pi * F / Fs ) 00152 00153 // algorithm 00154 // loop 00155 L = D2 + F1 * D1 00156 H = I - L - Q1*D1 00157 B = F1 * H + D1 00158 N = H + L 00159 00160 // store delays 00161 D1 = B 00162 D2 = L 00163 00164 // outputs 00165 L,H,B,N 00166 00167 00168 */