Mozzi  alpha 0.01.1t
sound synthesis library for Arduino
 All Classes Functions Typedefs
Smooth.h
00001 /*
00002  * Smooth.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 SMOOTH_H_
00024 #define SMOOTH_H_
00025 
00026 #include "Arduino.h"
00027 #include "fixedMath.h"
00028 
00043 template <class T>
00044 class Smooth
00045 {
00046 private:
00047                 long last_out;
00048                 Q0n16 a;
00049 
00050 public:
00056                 Smooth(float smoothness)
00057                 {
00058                                 setSmoothness(smoothness);
00059                 }
00060 
00065                 inline
00066                 T next(T in)
00067                 {
00068                                 long out = ((((((long)in - (last_out>>8)) * a))>>8) + last_out);
00069                                 last_out = out;
00070                                 return (T)(out>>8);
00071                 }
00072 
00078                 inline
00079                 void setSmoothness(float smoothness)
00080                 {
00081                                 a=float_to_Q0n16(1.f-smoothness);
00082                 }
00083 
00084 };
00085 
00086  // doxygen can ignore the specialisations
00088 
00090 template <>
00091 class Smooth <unsigned char>
00092 {
00093 private:
00094                 unsigned int last_out;
00095                 Q0n8 a;
00096 
00097 public:
00103                 Smooth(float smoothness)
00104                 {
00105                                 setSmoothness(smoothness);
00106                 }
00107 
00112                 inline
00113                 unsigned char next(unsigned char in)
00114                 {
00115                                 unsigned int out = (((((int)in - (last_out>>8)) * a)) + last_out);
00116                                 last_out = out;
00117                                 return (unsigned char)(out>>8);
00118                 }
00119 
00125                 inline
00126                 void setSmoothness(float smoothness)
00127                 {
00128                                 a=float_to_Q0n8(1.f-smoothness);
00129                 }
00130 
00131 };
00132 
00133 
00135 template <>
00136 class Smooth <char>
00137 {
00138 private:
00139                 int last_out;
00140                 Q0n8 a;
00141 
00142 public:
00148                 Smooth(float smoothness)
00149                 {
00150                                 setSmoothness(smoothness);
00151                 }
00152 
00157                 inline
00158                 char next(char in)
00159                 {
00160                                 int out = (((((int)in - (last_out>>8)) * a)) + last_out);
00161                                 last_out = out;
00162                                 return (char)(out>>8);
00163                 }
00164 
00170                 inline
00171                 void setSmoothness(float smoothness)
00172                 {
00173                                 a=float_to_Q0n8(1.f-smoothness);
00174                 }
00175 
00176 };
00177 
00179 template <>
00180 class Smooth <float>
00181 {
00182 private:
00183                 float last_out;
00184                 float a;
00185 
00186 public:
00192                 Smooth(float smoothness)
00193                 {
00194                                 setSmoothness(smoothness);
00195                 }
00196 
00201                 inline
00202                 float next(float in)
00203                 {
00204                                 float out = last_out + a * (in - last_out);
00205                                 //float out = (in - last_out * a) + last_out;
00206                                 last_out = out;
00207                                 return out;
00208                 }
00209 
00215                 inline
00216                 void setSmoothness(float smoothness)
00217                 {
00218                                 a=1.f-smoothness;
00219                 }
00220 
00221 };
00222 
00223 
00226 #endif /* SMOOTH_H_ */