Debian Codegraph @

lib-src/libnyquist/nyquist/nyqstk/src/Delay.cpp

raw
1 /***************************************************/
2 /*! \class Delay
3 \brief STK non-interpolating delay line class.
4
5 This protected Filter subclass implements
6 a non-interpolating digital delay-line.
7 A fixed maximum length of 4095 and a delay
8 of zero is set using the default constructor.
9 Alternatively, the delay and maximum length
10 can be set during instantiation with an
11 overloaded constructor.
12
13 A non-interpolating delay line is typically
14 used in fixed delay-length applications, such
15 as for reverberation.
16
17 by Perry R. Cook and Gary P. Scavone, 1995 - 2005.
18 */
19 /***************************************************/
20
21 #include "Delay.h"
22
23 using namespace Nyq;
24
25 Delay :: Delay() : Filter()
26 {
27 // Default maximum delay length set to 4095.
28 inputs_.resize( 4096 );
29 this->clear();
30
31 inPoint_ = 0;
32 outPoint_ = 0;
33 delay_ = 0;
34 }
35
36 Delay :: Delay(unsigned long delay, unsigned long maxDelay)
37 {
38 // Writing before reading allows delays from 0 to length-1.
39 // If we want to allow a delay of maxDelay, we need a
40 // delay-line of length = maxDelay+1.
41 if ( maxDelay < 1 ) {
42 errorString_ << "Delay::Delay: maxDelay must be > 0!\n";
43 handleError( StkError::FUNCTION_ARGUMENT );
44 }
45
46 if ( delay > maxDelay ) {
47 errorString_ << "Delay::Delay: maxDelay must be > than delay argument!\n";
48 handleError( StkError::FUNCTION_ARGUMENT );
49 }
50
51 if ( maxDelay > inputs_.size()-1 ) {
52 inputs_.resize( maxDelay+1 );
53 this->clear();
54 }
55
56 inPoint_ = 0;
57 this->setDelay( delay );
58 }
59
60 Delay :: ~Delay()
61 {
62 }
63
64 void Delay :: clear(void)
65 {
66 for (unsigned int i=0; i<inputs_.size(); i++)
67 inputs_[i] = 0.0;
68 outputs_[0] = 0.0;
69 }
70
71 void Delay :: setMaximumDelay(unsigned long delay)
72 {
73 if ( delay < inputs_.size() ) return;
74
75 /*
76 if ( delay < 0 ) {
77 errorString_ << "Delay::setMaximumDelay: argument (" << delay << ") less than zero!\n";
78 handleError( StkError::WARNING );
79 return;
80 }
81 else
82 */
83 if (delay < delay_ ) {
84 errorString_ << "Delay::setMaximumDelay: argument (" << delay << ") less than current delay setting (" << delay_ << ")!\n";
85 handleError( StkError::WARNING );
86 return;
87 }
88
89 inputs_.resize( delay + 1 );
90 }
91
92 void Delay :: setDelay(unsigned long delay)
93 {
94 if ( delay > inputs_.size() - 1 ) { // The value is too big.
95 errorString_ << "Delay::setDelay: argument (" << delay << ") too big ... setting to maximum!\n";
96 handleError( StkError::WARNING );
97
98 // Force delay to maximum length.
99 outPoint_ = inPoint_ + 1;
100 if ( outPoint_ == inputs_.size() ) outPoint_ = 0;
101 delay_ = inputs_.size() - 1;
102 }
103 /*
104 else if ( delay < 0 ) {
105 errorString_ << "Delay::setDelay: argument (" << delay << ") less than zero ... setting to zero!\n";
106 handleError( StkError::WARNING );
107
108 outPoint_ = inPoint_;
109 delay_ = 0;
110 }
111 */
112 else { // read chases write
113 if ( inPoint_ >= delay ) outPoint_ = inPoint_ - delay;
114 else outPoint_ = inputs_.size() + inPoint_ - delay;
115 delay_ = delay;
116 }
117 }
118
119 unsigned long Delay :: getDelay(void) const
120 {
121 return (unsigned long) delay_;
122 }
123
124 StkFloat Delay :: energy(void) const
125 {
126 unsigned long i;
127 StkFloat e = 0;
128 if (inPoint_ >= outPoint_) {
129 for (i=outPoint_; i<inPoint_; i++) {
130 StkFloat t = inputs_[i];
131 e += t*t;
132 }
133 } else {
134 for (i=outPoint_; i<inputs_.size(); i++) {
135 StkFloat t = inputs_[i];
136 e += t*t;
137 }
138 for (i=0; i<inPoint_; i++) {
139 StkFloat t = inputs_[i];
140 e += t*t;
141 }
142 }
143 return e;
144 }
145
146 StkFloat Delay :: contentsAt(unsigned long tapDelay)
147 {
148 unsigned long i = tapDelay;
149 if (i < 1) {
150 errorString_ << "Delay::contentsAt: argument (" << tapDelay << ") too small!";
151 handleError( StkError::WARNING );
152 return 0.0;
153 }
154 else if (i > delay_) {
155 errorString_ << "Delay::contentsAt: argument (" << tapDelay << ") too big!";
156 handleError( StkError::WARNING );
157 return 0.0;
158 }
159
160 intptr_t tap = inPoint_ - i;
161 if (tap < 0) // Check for wraparound.
162 tap += inputs_.size();
163
164 return inputs_[tap];
165 }
166
167 StkFloat Delay :: lastOut(void) const
168 {
169 return Filter::lastOut();
170 }
171
172 StkFloat Delay :: nextOut(void)
173 {
174 return inputs_[outPoint_];
175 }
176
177 StkFloat Delay :: computeSample( StkFloat input )
178 {
179 inputs_[inPoint_++] = input;
180
181 // Check for end condition
182 if (inPoint_ == inputs_.size())
183 inPoint_ = 0;
184
185 // Read out next value
186 outputs_[0] = inputs_[outPoint_++];
187
188 if (outPoint_ == inputs_.size())
189 outPoint_ = 0;
190
191 return outputs_[0];
192 }
193
194 StkFloat Delay :: tick( StkFloat input )
195 {
196 return computeSample( input );
197 }
198
199 StkFrames& Delay :: tick( StkFrames& frames, unsigned int channel )
200 {
201 return Filter::tick( frames, channel );
202 }