Mozzi  alpha 0.01.1t
sound synthesis library for Arduino
 All Classes Functions Typedefs
Line.h
00001 /*
00002  * Line.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 LINE_H_
00024 #define LINE_H_
00025 
00026 #include "Arduino.h"
00027 #include <util/atomic.h>
00028 
00043 template <class T>
00044 class Line
00045 {
00046 private:
00047                 volatile T current_value; // volatile because it could be set in control interrupt and updated in audio
00048                 T step_size;
00049 
00050 public:
00054                 Line ()
00055                 {
00056                                 ;
00057                 }
00058 
00062                 inline
00063                 T next()
00064                 {
00065                                 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
00066                                 {
00067                                                 current_value += step_size;
00068                                 }
00069                                 return current_value;
00070                 }
00071 
00072 
00073 
00078                 inline
00079                 void set(T value)
00080                 {
00081                                 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
00082                                 {
00083                                                 current_value=value;
00084                                 }
00085                 }
00086 
00087 
00088 
00094                 inline
00095                 void set(T targetvalue, T num_steps)
00096                 {
00097                                 step_size=(T)((((float)targetvalue-current_value)/num_steps));
00098                 }
00099 
00106                 inline
00107                 void set(T startvalue, T targetvalue, T num_steps)
00108                 {
00109                                 set(startvalue);
00110                                 set(targetvalue, num_steps);
00111                 }
00112 };
00113 
00114 #endif /* LINE_H_ */