![]() |
Mozzi
alpha 0.01.1t
sound synthesis library for Arduino
|
00001 /* 00002 * LowPassFilter.h 00003 * 00004 * Copyright 2012 Tim Barrass 00005 * 00006 * This file is part of Mozzi. 00007 * 00008 * Mozzi is free software: you can redistribute it and/or modify 00009 * it under the terms of the GNU General Public License as published by 00010 * the Free Software Foundation, either version 3 of the License, or 00011 * (at your option) any later version. 00012 * 00013 * Mozzi is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU General Public License 00019 * along with Mozzi. If not, see <http://www.gnu.org/licenses/>. 00020 * 00021 */ 00022 00023 #ifndef LOWPASS_H_ 00024 #define LOWPASS_H_ 00025 00026 /* 00027 simple resonant filter posted to musicdsp.org by Paul Kellett http://www.musicdsp.org/archive.php?classid=3#259 00028 00029 // set feedback amount given f and q between 0 and 1 00030 fb = q + q/(1.0 - f); 00031 00032 // for each sample... 00033 buf0 = buf0 + f * (in - buf0 + fb * (buf0 - buf1)); 00034 buf1 = buf1 + f * (buf0 - buf1); 00035 out = buf1; 00036 00037 fixed point version of the filter 00038 "dave's blog of art and programming" http://www.pawfal.org/dave/blog/2011/09/ 00039 */ 00040 00041 00042 // we are using .n fixed point (n bits for the fractional part) 00043 #define FX_SHIFT 8 00044 #define SHIFTED_1 256 00045 00048 class LowPassFilter 00049 { 00050 00051 public: 00052 00053 00056 LowPassFilter(); 00057 00058 00063 void setCutoffFreq(unsigned char cutoff) 00064 { 00065 f = cutoff; 00066 setFeedback((int)cutoff); 00067 } 00068 00069 00073 void setResonance(unsigned char resonance) 00074 { 00075 q = resonance; 00076 } 00077 00082 //16us 00083 inline 00084 int next(int in) 00085 { 00086 buf0+=fxmul(f, ((in - buf0) + fxmul(fb, buf0-buf1))); 00087 buf1+=fxmul(f, buf0-buf1); 00088 return buf1; 00089 } 00090 00091 00092 private: 00093 int f; 00094 long fb; 00095 int q; 00096 int buf0,buf1; 00097 00098 inline 00099 void setFeedback(int f) 00100 { 00101 fb = q+fxmul(q, (int)SHIFTED_1 - f); 00102 } 00103 00104 // convert an int into to its fixed representation 00105 inline 00106 long fx(int i) 00107 { 00108 return (i<<FX_SHIFT); 00109 } 00110 00111 00112 // // multiply two fixed point numbers (returns fixed point) 00113 // inline 00114 // long fxmul(long a, long b) 00115 // { 00116 // return (a*b)>>FX_SHIFT; 00117 // } 00118 00119 // multiply two fixed point numbers (returns fixed point) 00120 inline 00121 long fxmul(long a, int b) 00122 { 00123 return ((a*b)>>FX_SHIFT); 00124 } 00125 00126 00127 }; 00128 00129 #endif /* LOWPASS_H_ */