forked from thestk/stk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DelayA.cpp
117 lines (93 loc) · 3.2 KB
/
DelayA.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
111
112
113
114
115
116
117
/***************************************************/
/*! \class DelayA
\brief STK allpass interpolating delay line class.
This class implements a fractional-length digital delay-line using
a first-order allpass filter. If the delay and maximum length are
not specified during instantiation, a fixed maximum length of 4095
and a delay of zero is set.
An allpass filter has unity magnitude gain but variable phase
delay properties, making it useful in achieving fractional delays
without affecting a signal's frequency magnitude response. In
order to achieve a maximally flat phase delay response, the
minimum delay possible in this implementation is limited to a
value of 0.5.
by Perry R. Cook and Gary P. Scavone, 1995--2016.
*/
/***************************************************/
#include "DelayA.h"
namespace stk {
DelayA :: DelayA( StkFloat delay, unsigned long maxDelay )
{
if ( delay < 0.5 ) {
oStream_ << "DelayA::DelayA: delay must be >= 0.5!";
handleError( StkError::FUNCTION_ARGUMENT );
}
if ( delay > (StkFloat) maxDelay ) {
oStream_ << "DelayA::DelayA: maxDelay must be > than delay argument!";
handleError( StkError::FUNCTION_ARGUMENT );
}
// Writing before reading allows delays from 0 to length-1.
if ( maxDelay + 1 > inputs_.size() )
inputs_.resize( maxDelay + 1, 1, 0.0 );
inPoint_ = 0;
this->setDelay( delay );
apInput_ = 0.0;
doNextOut_ = true;
}
DelayA :: ~DelayA()
{
}
void DelayA :: clear()
{
for ( unsigned int i=0; i<inputs_.size(); i++ )
inputs_[i] = 0.0;
lastFrame_[0] = 0.0;
apInput_ = 0.0;
}
void DelayA :: setMaximumDelay( unsigned long delay )
{
if ( delay < inputs_.size() ) return;
inputs_.resize(delay + 1, 1, 0.0);
}
void DelayA :: setDelay( StkFloat delay )
{
unsigned long length = inputs_.size();
if ( delay + 1 > length ) { // The value is too big.
oStream_ << "DelayA::setDelay: argument (" << delay << ") greater than maximum!";
handleError( StkError::WARNING ); return;
}
if ( delay < 0.5 ) {
oStream_ << "DelayA::setDelay: argument (" << delay << ") less than 0.5 not possible!";
handleError( StkError::WARNING );
}
StkFloat outPointer = inPoint_ - delay + 1.0; // outPoint chases inpoint
delay_ = delay;
while ( outPointer < 0 )
outPointer += length; // modulo maximum length
outPoint_ = (long) outPointer; // integer part
if ( outPoint_ == length ) outPoint_ = 0;
alpha_ = 1.0 + outPoint_ - outPointer; // fractional part
if ( alpha_ < 0.5 ) {
// The optimal range for alpha is about 0.5 - 1.5 in order to
// achieve the flattest phase delay response.
outPoint_ += 1;
if ( outPoint_ >= length ) outPoint_ -= length;
alpha_ += (StkFloat) 1.0;
}
coeff_ = (1.0 - alpha_) / (1.0 + alpha_); // coefficient for allpass
}
StkFloat DelayA :: tapOut( unsigned long tapDelay )
{
long tap = inPoint_ - tapDelay - 1;
while ( tap < 0 ) // Check for wraparound.
tap += inputs_.size();
return inputs_[tap];
}
void DelayA :: tapIn( StkFloat value, unsigned long tapDelay )
{
long tap = inPoint_ - tapDelay - 1;
while ( tap < 0 ) // Check for wraparound.
tap += inputs_.size();
inputs_[tap] = value;
}
} // stk namespace