Mozzi  version v2.0
sound synthesis library for Arduino
AudioDelay.h
1 /*
2  * AudioDelay.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 #ifndef AUDIODELAY_H_
13 #define AUDIODELAY_H_
14 
15 
26 template <unsigned int NUM_BUFFER_SAMPLES, class T = int8_t>
28 {
29 
30 private:
31 
32  T delay_array[NUM_BUFFER_SAMPLES];
33  unsigned int _write_pos;
34  unsigned int _delaytime_cells;
35 
36 public:
37 
40  AudioDelay(): _write_pos(0)
41  {}
42 
43 
49  AudioDelay(unsigned int delaytime_cells): _write_pos(0), _delaytime_cells(delaytime_cells)
50  {}
51 
52 
53 
58  inline
59  T next(T in_value, unsigned int delaytime_cells)
60  {
61  ++_write_pos &= (NUM_BUFFER_SAMPLES - 1);
62  unsigned int read_pos = (_write_pos - delaytime_cells) & (NUM_BUFFER_SAMPLES - 1);
63 
64  // why does delay jump if I read it before writing?
65  delay_array[_write_pos] = in_value; // write to buffer
66  T delay_sig = delay_array[read_pos] ; // read the delay buffer
67 
68  return (T)delay_sig;
69  }
70 
71 
72 
76  inline
77  T next(T in_value)
78  {
79  ++_write_pos &= (NUM_BUFFER_SAMPLES - 1);
80  unsigned int read_pos = (_write_pos - _delaytime_cells) & (NUM_BUFFER_SAMPLES - 1);
81 
82  // why does delay jump if I read it before writing?
83  delay_array[_write_pos] = in_value; // write to buffer
84  T delay_sig = delay_array[read_pos] ; // read the delay buffer
85 
86  return delay_sig;
87  }
88 
89 
93  inline
94  void set(unsigned int delaytime_cells){
95  _delaytime_cells = delaytime_cells;
96  }
97 
98 
103  inline
104  T read(unsigned int delaytime_cells)
105  {
106  unsigned int read_pos = (_write_pos - delaytime_cells) & (NUM_BUFFER_SAMPLES - 1);
107  return delay_array[read_pos];
108  }
109 
110 
111  // /** Input a value to the delay but don't advance the write position, change the delay time or retrieve the output signal.
112  // This can be useful for manually adding feedback to the delay line, "behind" the advancing write head.
113  // @param input the signal input.
114  // */
115  // inline
116  // void writeFeedback(T input)
117  // {
118  // delay_array[_write_pos] = input;
119  // }
120 
121 };
122 
128 #endif // #ifndef AUDIODELAY_H_
129 
Audio delay line for comb filter, flange, chorus and short echo effects.
Definition: AudioDelay.h:28
T next(T in_value, unsigned int delaytime_cells)
Input a value to the delay and retrieve the signal in the delay line at the position delaytime_cells.
Definition: AudioDelay.h:59
T read(unsigned int delaytime_cells)
Retrieve the signal in the delay line at the position delaytime_cells.
Definition: AudioDelay.h:104
T next(T in_value)
Input a value to the delay and retrieve the signal in the delay line at the position delaytime_cells.
Definition: AudioDelay.h:77
AudioDelay()
Constructor.
Definition: AudioDelay.h:40
AudioDelay(unsigned int delaytime_cells)
Constructor.
Definition: AudioDelay.h:49
void set(unsigned int delaytime_cells)
Set the delay time, measured in cells.
Definition: AudioDelay.h:94