Mozzi  version 2015-05-11-20:23
sound synthesis library for Arduino
 All Classes Functions Typedefs Groups
Smooth.h
1 /*
2  * Smooth.h
3  *
4  * 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 #ifndef SMOOTH_H_
13 #define SMOOTH_H_
14 
15 #include "Arduino.h"
16 #include "mozzi_fixmath.h"
17 
34 template <class T>
35 class Smooth
36 {
37 private:
38  long last_out;
39  Q0n16 a;
40 
41 public:
47  Smooth(float smoothness)
48  {
49  setSmoothness(smoothness);
50  }
51 
56  inline
57  T next(T in)
58  {
59  long out = ((((((long)in - (last_out>>8)) * a))>>8) + last_out);
60  last_out = out;
61  return (T)(out>>8);
62  }
63 
64 
69  inline
70  T operator()(T n) {
71  return next(n);
72  }
73 
74 
80  inline
81  void setSmoothness(float smoothness)
82  {
83  a=float_to_Q0n16(1.f-smoothness);
84  }
85 
86 };
87 
88  // doxygen can ignore the specialisations
90 
92 template <>
93 class Smooth <uint8_t>
94 {
95 private:
96  unsigned int last_out;
97  Q0n8 a;
98 
99 public:
105  Smooth(float smoothness)
106  {
107  setSmoothness(smoothness);
108  }
109 
114  inline
115  uint8_t next(uint8_t in)
116  {
117  unsigned int out = (((((int)in - (last_out>>8)) * a)) + last_out);
118  last_out = out;
119  return (uint8_t)(out>>8);
120  }
121 
122 
127  inline
128  uint8_t operator()(uint8_t n) {
129  return next(n);
130  }
131 
132 
138  inline
139  void setSmoothness(float smoothness)
140  {
141  a=float_to_Q0n8(1.f-smoothness);
142  }
143 
144 };
145 
146 
148 template <>
149 class Smooth <int8_t>
150 {
151 private:
152  int last_out;
153  Q0n8 a;
154 
155 public:
161  Smooth(float smoothness)
162  {
163  setSmoothness(smoothness);
164  }
165 
166 
171  inline
172  int8_t next(int8_t in)
173  {
174  int out = (((((int)in - (last_out>>8)) * a)) + last_out);
175  last_out = out;
176  return (int8_t)(out>>8);
177  }
178 
179 
184  inline
185  int8_t operator()(int8_t n) {
186  return next(n);
187  }
188 
189 
195  inline
196  void setSmoothness(float smoothness)
197  {
198  a=float_to_Q0n8(1.f-smoothness);
199  }
200 
201 };
202 
204 template <>
205 class Smooth <float>
206 {
207 private:
208  float last_out;
209  float a;
210 
211 public:
217  Smooth(float smoothness)
218  {
219  setSmoothness(smoothness);
220  }
221 
226  inline
227  float next(float in)
228  {
229  float out = last_out + a * (in - last_out);
230  //float out = (in - last_out * a) + last_out;
231  last_out = out;
232  return out;
233  }
234 
235 
240  inline
241  float operator()(float n) {
242  return next(n);
243  }
244 
245 
251  inline
252  void setSmoothness(float smoothness)
253  {
254  a=1.f-smoothness;
255  }
256 
257 };
258 
259 
267 #endif /* SMOOTH_H_ */
void setSmoothness(float smoothness)
Sets how much smoothing the filter will apply to its input.
Definition: Smooth.h:81
Smooth(float smoothness)
Constructor.
Definition: Smooth.h:47
Q0n8 float_to_Q0n8(float a)
Convert float to Q0n8 fix.
Definition: mozzi_fixmath.h:97
uint16_t Q0n16
unsigned fractional number using 16 fractional bits, represents 0.0 to 0.999
Definition: mozzi_fixmath.h:29
A simple infinite impulse response low pass filter for smoothing control or audio signals...
Definition: Smooth.h:35
T operator()(T n)
Filters the input and returns the filtered value.
Definition: Smooth.h:70
Q0n16 float_to_Q0n16(float a)
Convert float to Q0n16 fix.
T next(T in)
Filters the input and returns the filtered value.
Definition: Smooth.h:57
uint8_t Q0n8
unsigned fractional number using 8 fractional bits, represents 0.0 to 0.996
Definition: mozzi_fixmath.h:27