Mozzi  version v2.0
sound synthesis library for Arduino
ReverbTank.h
1 /*
2  * ReverbTank.h
3  *
4  * This file is part of Mozzi.
5  *
6  * Copyright 2013-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 REVERBTANK_H
13 #define REVERBTANK_H
14 
15 #include "AudioDelay.h"
16 /**
17 A reverb which sounds like the inside of a tin can.
18 ReverbTank is small enough to fit on the Arduino Nano, which for some reason
19 wasn't able to fit a larger version which did fit on other 328 based boards. For
20 simplicity, ReverbTank has hardcoded maximum delay sizes but also has default
21 delay times which can be changed in the constructor or by setting during run
22 time to allow live tweaking.
23 This is a highly simplified design drawing on and probably misinterpreting
24 Miller Puckette's G08.reverb recirculating reverb example for Pure Data.
25 
26 The room size according to the maximum delay lengths corresponds to:
27 
28 early reflections and recirculating delay 1: 128/16384 seconds * 340.29 m/s speed of sound = 3.5 metres
29 recirculating delay 2: 7 metres
30 It looks bigger on paper than it sounds.
31 */
32 class
33  ReverbTank {
34 
35 public:
36  /** Constructor. This has default values for the early reflection times, recirculating delay lengths and feedback level,
37  which can be changed here in the constructor or set with other functions during run time.
38  @param early_reflection1 how long in delay cells till the first early reflection, from 0 to 127
39  @param early_reflection2 how long in delay cells till the second early reflection, from early_reflection1 to 127
40  @param early_reflection3 how long in delay cells till the third early reflection, from early_reflection2 to 127
41  @param loop1_delay how long in delay cells for the first recirculating delay, form 0 to 127
42  @param loop2_delay how long in delay cells for the first recirculating delay, form 0 to 255
43  @param feedback_level how much recirculation, from -128 to 127
44  */
46  int8_t early_reflection1 = 37,
47  int8_t early_reflection2 = 77,
48  int8_t early_reflection3 = 127,
49  int8_t loop1_delay=117,
50  uint8_t loop2_delay=255,
51  int8_t feedback_level = 85):
54  {
55  aLoopDel1.set(loop1_delay);
56  aLoopDel2.set(loop2_delay);
57  }
58 
59 
60  /** Process the next audio sample and return the reverbed signal. This returns only the "wet" signal,
61  which can be combined with the dry input signal in the sketch.
62  @param input the audio signal to process
63  @return the processed signal
64  */
65  int next(int input){
66  static int recycle1, recycle2;
67 
68  // early reflections
69  int asig = aLoopDel0.next(input, _early_reflection1);
70  asig += aLoopDel0.read(_early_reflection2);
71  asig += aLoopDel0.read(_early_reflection3);
72  asig >>= 2;
73 
74  // recirculating delays
75  int8_t feedback_sig1 = (int8_t) min(max(((recycle1 * _feedback_level)>>7),-128),127); // feedback clipped
76  int8_t feedback_sig2 = (int8_t) min(max(((recycle2 * _feedback_level)>>7),-128),127); // feedback clipped
77  int sig3 = aLoopDel1.next(asig+feedback_sig1);
78  int sig4 = aLoopDel2.next(asig+feedback_sig2);
79  recycle1 = sig3 + sig4;
80  recycle2 = sig3 - sig4;
81 
82  return recycle1;
83  }
84 
85 
86  /** Set the early reflection times in terms of delay cells.
87  @param early_reflection1 how long in delay cells till the first early reflection, from 0 to 127
88  @param early_reflection2 how long in delay cells till the second early reflection, from early_reflection1 to 127
89  @param early_reflection3 how long in delay cells till the third early reflection, from early_reflection2 to 127
90  */
91  void setEarlyReflections(int8_t early_reflection1, int8_t early_reflection2, int8_t early_reflection3){
92  _early_reflection1=early_reflection1;
93  _early_reflection2=early_reflection2;
94  _early_reflection3=early_reflection3;
95  }
96 
97 
98  /** Set the loop delay times in terms of delay cells.
99  @param loop1_delay how long in delay cells for the first recirculating delay, form 0 to 127
100  @param loop2_delay how long in delay cells for the first recirculating delay, form 0 to 255
101  */
102  void setLoopDelays(int8_t loop1_delay, uint8_t loop2_delay){
103  aLoopDel1.set(loop1_delay);
104  aLoopDel2.set(loop2_delay);
105  }
106 
107  /** Set the feedback level for the recirculating delays.
108  @param feedback_level how much recirculation, from -128 to 127
109  */
110  void setFeebackLevel(int8_t feedback_level){
111  _feedback_level=feedback_level;
112  }
113 
114 
115 private:
116  int8_t _early_reflection1;
117  int8_t _early_reflection2;
118  int8_t _early_reflection3;
119 
120  int8_t _feedback_level;
121 
122  AudioDelay <128> aLoopDel0; // 128/16384 seconds * 340.29 m/s speed of sound = 3.5 metres
123  AudioDelay <128,int> aLoopDel1;
124  AudioDelay <256,int> aLoopDel2; // 7 metres
125 
126 };
127 
128 /**
129 @example 09.Delays/ReverbTank_STANDARD/ReverbTank_STANDARD.ino
130 This example demonstrates the ReverbTank class.
131 */
132 
133 #endif // #ifndef REVERBTANK_H