-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathDelay.cpp
110 lines (90 loc) · 2.67 KB
/
Delay.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/***************************************************/
/*! \class Delay
\brief STK non-interpolating delay line class.
This class implements a non-interpolating digital delay-line. If
the delay and maximum length are not specified during
instantiation, a fixed maximum length of 4095 and a delay of zero
is set.
A non-interpolating delay line is typically used in fixed
delay-length applications, such as for reverberation.
by Perry R. Cook and Gary P. Scavone, 1995--2023.
*/
/***************************************************/
#include "Delay.h"
namespace stk {
Delay :: Delay( unsigned long delay, unsigned long maxDelay )
{
// Writing before reading allows delays from 0 to length-1.
// If we want to allow a delay of maxDelay, we need a
// delay-line of length = maxDelay+1.
if ( delay > maxDelay ) {
oStream_ << "Delay::Delay: maxDelay must be > than delay argument!\n";
handleError( StkError::FUNCTION_ARGUMENT );
}
if ( ( maxDelay + 1 ) > inputs_.size() )
inputs_.resize( maxDelay + 1, 1, 0.0 );
inPoint_ = 0;
this->setDelay( delay );
}
Delay :: ~Delay()
{
}
void Delay :: setMaximumDelay( unsigned long delay )
{
if ( delay < inputs_.size() ) return;
inputs_.resize( delay + 1, 1, 0.0 );
}
void Delay :: setDelay( unsigned long delay )
{
if ( delay > inputs_.size() - 1 ) { // The value is too big.
oStream_ << "Delay::setDelay: argument (" << delay << ") greater than maximum!\n";
handleError( StkError::WARNING ); return;
}
// read chases write
if ( inPoint_ >= delay ) outPoint_ = inPoint_ - delay;
else outPoint_ = inputs_.size() + inPoint_ - delay;
delay_ = delay;
}
StkFloat Delay :: energy( void ) const
{
unsigned long i;
StkFloat e = 0;
if ( inPoint_ >= outPoint_ ) {
for ( i=outPoint_; i<inPoint_; i++ ) {
StkFloat t = inputs_[i];
e += t*t;
}
} else {
for ( i=outPoint_; i<inputs_.size(); i++ ) {
StkFloat t = inputs_[i];
e += t*t;
}
for ( i=0; i<inPoint_; i++ ) {
StkFloat t = inputs_[i];
e += t*t;
}
}
return e;
}
StkFloat Delay :: tapOut( unsigned long tapDelay )
{
long tap = inPoint_ - tapDelay - 1;
while ( tap < 0 ) // Check for wraparound.
tap += inputs_.size();
return inputs_[tap];
}
void Delay :: tapIn( StkFloat value, unsigned long tapDelay )
{
long tap = inPoint_ - tapDelay - 1;
while ( tap < 0 ) // Check for wraparound.
tap += inputs_.size();
inputs_[tap] = value;
}
StkFloat Delay :: addTo( StkFloat value, unsigned long tapDelay )
{
long tap = inPoint_ - tapDelay - 1;
while ( tap < 0 ) // Check for wraparound.
tap += inputs_.size();
return inputs_[tap]+= value;
}
} // stk namespace