Mozzi  alpha 0.01.1t
sound synthesis library for Arduino
 All Classes Functions Typedefs
AudioDelay.h
00001 /*
00002  * AudioDelay.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 AUDIODELAY_H_
00024 #define AUDIODELAY_H_
00025 
00026 
00035 template <unsigned int NUM_BUFFER_SAMPLES>
00036 class AudioDelay
00037 {
00038 
00039 private:
00040 
00041                 char delay_array[NUM_BUFFER_SAMPLES];
00042                 unsigned int _write_pos;
00043                 unsigned int _delaytime_cells;
00044                 
00045 public:
00046 
00049                 AudioDelay(): _write_pos(0)
00050                 {}
00051                 
00052                 
00058                 AudioDelay(unsigned int delaytime_cells): _write_pos(0), _delaytime_cells(delaytime_cells)
00059                 {}
00060 
00061                 
00062 
00067                 inline
00068                 char next(char in_value, unsigned int delaytime_cells)
00069                 {
00070                                 ++_write_pos &= (NUM_BUFFER_SAMPLES - 1);
00071                                 unsigned int read_pos = (_write_pos - delaytime_cells) & (NUM_BUFFER_SAMPLES - 1);
00072 
00073                                 // why does delay jump if I read it before writing?
00074                                 delay_array[_write_pos] = in_value;                                             // write to buffer
00075                                 char delay_sig = delay_array[read_pos] ;        // read the delay buffer
00076 
00077                                 return (char)(((int) in_value + delay_sig)>>1);
00078                 }
00079                 
00080                 
00081                 
00085                 inline
00086                 char next(char in_value)
00087                 {
00088                                 ++_write_pos &= (NUM_BUFFER_SAMPLES - 1);
00089                                 unsigned int read_pos = (_write_pos - _delaytime_cells) & (NUM_BUFFER_SAMPLES - 1);
00090 
00091                                 // why does delay jump if I read it before writing?
00092                                 delay_array[_write_pos] = in_value;                                             // write to buffer
00093                                 char delay_sig = delay_array[read_pos] ;        // read the delay buffer
00094 
00095                                 return (char)(((int) in_value + delay_sig)>>1);
00096                 }
00097 
00098                 
00099                 inline
00100                 void set(unsigned int delaytime_cells){
00101                                 _delaytime_cells = delaytime_cells;
00102                 }
00103 
00104 };
00105 
00106 #endif        //  #ifndef AUDIODELAY_H_
00107