Mozzi  version v2.0
sound synthesis library for Arduino
Line.h
1 /*
2  * Line.h
3  *
4  * This file is part of Mozzi.
5  *
6  * Copyright 2012-2024 Tim Barrass and the Mozzi Team
7  *
8  * Mozzi is licensed under the GNU Lesser General Public Licence (LGPL) Version 2.1 or later.
9  *
10  */
11 
12 
13 #ifndef LINE_H_
14 #define LINE_H_
15 
16 #include <Arduino.h>
17 
18 #include<FixMath.h>
19 
38 template <class T>
39 class Line
40 {
41 private:
42  T current_value;
43  T step_size;
44 
45 public:
49  Line ()
50  {
51  ;
52  }
53 
54 
55 
59  inline
60  T next()
61  {
62  current_value += step_size;
63  //Serial.println(current_value);
64  return current_value;
65  }
66 
67 
68 
74  inline
75  void set(T value)
76  {
77  current_value=value;
78  }
79 
80 
81 
86  inline
87  void set(T targetvalue, T num_steps)
88  {
89  if(num_steps) {
90  T numerator = targetvalue-current_value;
91  step_size= numerator/num_steps;
92  } else {
93  step_size = 0;
94  current_value = targetvalue;
95  }
96  }
97 
103  inline
104  void set(T startvalue, T targetvalue, T num_steps)
105  {
106  set(startvalue);
107  set(targetvalue, num_steps);
108  }
109 };
110 
111 
112 /* unsigned char specialisation (probably not very useful because step size will likely = 0) */
113 template <>
114 class Line <unsigned char>
115 {
116 private:
117  unsigned char current_value;
118  char step_size;
119 
120 public:
124  Line ()
125  {
126  ;
127  }
128 
129 
130 
134  inline
135  unsigned char next()
136  {
137  current_value += step_size;
138  return current_value;
139  }
140 
141 
142 
148  inline
149  void set(unsigned char value)
150  {
151  current_value=value;
152  }
153 
154 
155 
160  inline
161  void set(unsigned char targetvalue, unsigned char num_steps)
162  {
163  step_size=(char)((((float)targetvalue-current_value)/num_steps));
164  }
165 
171  inline
172  void set(unsigned char startvalue, unsigned char targetvalue, unsigned char num_steps)
173  {
174  set(startvalue);
175  set(targetvalue, num_steps);
176  }
177 
178 };
179 
180 
181 /* unsigned int specialisation */
182 template <>
183 class Line <unsigned int>
184 {
185 private:
186  unsigned int current_value;
187  int step_size;
188 
189 public:
193  Line ()
194  {
195  ;
196  }
197 
198 
199 
203  inline
204  unsigned int next()
205  {
206  current_value += step_size;
207  return current_value;
208  }
209 
210 
211 
217  inline
218  void set(unsigned int value)
219  {
220  current_value=value;
221  }
222 
223 
224 
229  inline
230  void set(unsigned int targetvalue, unsigned int num_steps)
231  {
232  step_size=(int)((((float)targetvalue-current_value)/num_steps));
233  }
234 
235 
241  inline
242  void set(unsigned int startvalue, unsigned int targetvalue, unsigned int num_steps)
243  {
244  set(startvalue);
245  set(targetvalue, num_steps);
246  }
247 };
248 
249 
250 
251 
252 
253 /* unsigned long specialisation */
254 template <>
255 class Line <unsigned long>
256 {
257 private:
258  unsigned long current_value;
259  long step_size;
260 
261 public:
265  Line ()
266  {
267  ;
268  }
269 
270 
271 
275  inline
276  unsigned long next()
277  {
278  current_value += step_size;
279  return current_value;
280  }
281 
282 
283 
289  inline
290  void set(unsigned long value)
291  {
292  current_value=value;
293  }
294 
295 
296 
301  inline
302  void set(unsigned long targetvalue, unsigned long num_steps)
303  {
304  step_size=(long)((((float)targetvalue-current_value)/num_steps));
305  }
306 
312  inline
313  void set(unsigned long startvalue, unsigned long targetvalue, unsigned long num_steps)
314  {
315  set(startvalue);
316  set(targetvalue, num_steps);
317  }
318 };
319 
320 
321 /* UFix specialisation */
322 template<int8_t NI, int8_t NF>
323 class Line<UFix<NI, NF>>
324 {
325 private:
326  typedef UFix<NI, NF> internal_type;
327  internal_type current_value;
328  SFix<NI,NF> step_size;
329 
330 public:
334  Line (){;}
335 
339  inline
340  internal_type next()
341  {
342  current_value = current_value + step_size;
343  return current_value;
344  }
345 
351  inline
352  void set(internal_type value)
353  {
354  current_value=value;
355  }
356 
361  template<int8_t _NI>
362  void set(internal_type targetvalue, UFix<_NI,0> num_steps)
363  {
364  if(num_steps.asRaw()) {
365  auto numerator = targetvalue-current_value;
366  step_size = numerator*num_steps.invAccurate();
367  } else {
368  step_size = 0;
369  current_value = targetvalue;
370  }
371  }
372 
373 
378  template<typename T>
379  void set(internal_type targetvalue, T num_steps)
380  {
381  if(num_steps) {
382  auto numerator = targetvalue-current_value;
383  step_size = internal_type(numerator.asRaw()/num_steps,true);
384  } else {
385  step_size = 0;
386  current_value = targetvalue;
387  }
388  }
389 
395  template<typename T>
396  void set(internal_type startvalue, internal_type targetvalue, T num_steps)
397  {
398  set(startvalue);
399  set(targetvalue, num_steps);
400  }
401 };
402 
403 
404 /* SFix specialisation (if someone has an idea to avoid duplication with UFix) */
405 template<int8_t NI, int8_t NF>
406 class Line<SFix<NI, NF>>
407 {
408 private:
409  typedef SFix<NI, NF> internal_type;
410  internal_type current_value;
411  SFix<NI+1, NF> step_size;
412 
413 public:
417  Line (){;}
418 
422  inline
423  internal_type next()
424  {
425  current_value = current_value + step_size;
426  return current_value;
427  }
428 
434  inline
435  void set(internal_type value)
436  {
437  current_value=value;
438  }
439 
444  template<int8_t _NI>
445  void set(internal_type targetvalue, UFix<_NI,0> num_steps)
446  {
447  if(num_steps.asRaw()) {
448  auto numerator = targetvalue-current_value;
449  step_size = numerator*num_steps.invAccurate();
450  } else {
451  step_size = 0;
452  current_value = targetvalue;
453  }
454  }
455 
456 
461  template<typename T>
462  void set(internal_type targetvalue, T num_steps)
463  {
464  if(num_steps) {
465  auto numerator = targetvalue-current_value;
466  step_size = internal_type(numerator.asRaw()/num_steps,true);
467  } else {
468  step_size = 0;
469  current_value = targetvalue;
470  }
471  }
472 
478  template<typename T>
479  void set(internal_type startvalue, internal_type targetvalue, T num_steps)
480  {
481  set(startvalue);
482  set(targetvalue, num_steps);
483  }
484 };
485 
486 
487 
493 #endif /* LINE_H_ */
internal_type next()
Increments one step along the line.
Definition: Line.h:423
void set(internal_type value)
Set the current value of the line.
Definition: Line.h:435
void set(internal_type targetvalue, T num_steps)
Given a target value and the number of steps to take on the way, this calculates the step size needed...
Definition: Line.h:462
void set(internal_type targetvalue, UFix< _NI, 0 > num_steps)
Given a target value and the number of steps to take on the way, this calculates the step size needed...
Definition: Line.h:445
Line()
Constructor.
Definition: Line.h:417
void set(internal_type startvalue, internal_type targetvalue, T num_steps)
Given a new starting value, target value and the number of steps to take on the way,...
Definition: Line.h:479
internal_type next()
Increments one step along the line.
Definition: Line.h:340
void set(internal_type targetvalue, T num_steps)
Given a target value and the number of steps to take on the way, this calculates the step size needed...
Definition: Line.h:379
void set(internal_type targetvalue, UFix< _NI, 0 > num_steps)
Given a target value and the number of steps to take on the way, this calculates the step size needed...
Definition: Line.h:362
void set(internal_type value)
Set the current value of the line.
Definition: Line.h:352
void set(internal_type startvalue, internal_type targetvalue, T num_steps)
Given a new starting value, target value and the number of steps to take on the way,...
Definition: Line.h:396
Line()
Constructor.
Definition: Line.h:334
Line()
Constructor.
Definition: Line.h:124
void set(unsigned char targetvalue, unsigned char num_steps)
Given a target value and the number of steps to take on the way, this calculates the step size needed...
Definition: Line.h:161
void set(unsigned char value)
Set the current value of the line.
Definition: Line.h:149
void set(unsigned char startvalue, unsigned char targetvalue, unsigned char num_steps)
Given a new starting value, target value and the number of steps to take on the way,...
Definition: Line.h:172
unsigned char next()
Increments one step along the line.
Definition: Line.h:135
void set(unsigned int targetvalue, unsigned int num_steps)
Given a target value and the number of steps to take on the way, this calculates the step size needed...
Definition: Line.h:230
void set(unsigned int value)
Set the current value of the line.
Definition: Line.h:218
void set(unsigned int startvalue, unsigned int targetvalue, unsigned int num_steps)
Given a new starting value, target value and the number of steps to take on the way,...
Definition: Line.h:242
Line()
Constructor.
Definition: Line.h:193
unsigned int next()
Increments one step along the line.
Definition: Line.h:204
void set(unsigned long startvalue, unsigned long targetvalue, unsigned long num_steps)
Given a new starting value, target value and the number of steps to take on the way,...
Definition: Line.h:313
unsigned long next()
Increments one step along the line.
Definition: Line.h:276
Line()
Constructor.
Definition: Line.h:265
void set(unsigned long value)
Set the current value of the line.
Definition: Line.h:290
void set(unsigned long targetvalue, unsigned long num_steps)
Given a target value and the number of steps to take on the way, this calculates the step size needed...
Definition: Line.h:302
For linear changes with a minimum of calculation at each step.
Definition: Line.h:40
void set(T startvalue, T targetvalue, T num_steps)
Given a new starting value, target value and the number of steps to take on the way,...
Definition: Line.h:104
T next()
Increments one step along the line.
Definition: Line.h:60
void set(T value)
Set the current value of the line.
Definition: Line.h:75
void set(T targetvalue, T num_steps)
Given a target value and the number of steps to take on the way, this calculates the step size needed...
Definition: Line.h:87
Line()
Constructor.
Definition: Line.h:49