| 1 |
/***************************************************/ |
| 2 |
/*! \class NRev |
| 3 |
\brief CCRMA's NRev reverberator class. |
| 4 |
|
| 5 |
This class is derived from the CLM NRev |
| 6 |
function, which is based on the use of |
| 7 |
networks of simple allpass and comb delay |
| 8 |
filters. This particular arrangement consists |
| 9 |
of 6 comb filters in parallel, followed by 3 |
| 10 |
allpass filters, a lowpass filter, and another |
| 11 |
allpass in series, followed by two allpass |
| 12 |
filters in parallel with corresponding right |
| 13 |
and left outputs. |
| 14 |
|
| 15 |
by Perry R. Cook and Gary P. Scavone, 1995 - 2005. |
| 16 |
*/ |
| 17 |
/***************************************************/ |
| 18 |
|
| 19 |
#include "NRev.h" |
| 20 |
#include <math.h> |
| 21 |
|
| 22 |
using namespace Nyq; |
| 23 |
|
| 24 |
NRev :: NRev(StkFloat T60) |
| 25 |
{ |
| 26 |
int lengths[15] = {1433, 1601, 1867, 2053, 2251, 2399, 347, 113, 37, 59, 53, 43, 37, 29, 19}; |
| 27 |
double scaler = Stk::sampleRate() / 25641.0; |
| 28 |
|
| 29 |
int delay, i; |
| 30 |
for (i=0; i<15; i++) { |
| 31 |
delay = (int) floor(scaler * lengths[i]); |
| 32 |
if ( (delay & 1) == 0) delay++; |
| 33 |
while ( !this->isPrime(delay) ) delay += 2; |
| 34 |
lengths[i] = delay; |
| 35 |
} |
| 36 |
|
| 37 |
for (i=0; i<6; i++) { |
| 38 |
combDelays_[i].setMaximumDelay( lengths[i] ); |
| 39 |
combDelays_[i].setDelay( lengths[i] ); |
| 40 |
combCoefficient_[i] = pow(10.0, (-3 * lengths[i] / (T60 * Stk::sampleRate()))); |
| 41 |
} |
| 42 |
|
| 43 |
for (i=0; i<8; i++) { |
| 44 |
allpassDelays_[i].setMaximumDelay( lengths[i+6] ); |
| 45 |
allpassDelays_[i].setDelay( lengths[i+6] ); |
| 46 |
} |
| 47 |
|
| 48 |
this->setT60( T60 ); |
| 49 |
allpassCoefficient_ = 0.7; |
| 50 |
effectMix_ = 0.3; |
| 51 |
this->clear(); |
| 52 |
} |
| 53 |
|
| 54 |
NRev :: ~NRev() |
| 55 |
{ |
| 56 |
} |
| 57 |
|
| 58 |
void NRev :: clear() |
| 59 |
{ |
| 60 |
int i; |
| 61 |
for (i=0; i<6; i++) combDelays_[i].clear(); |
| 62 |
for (i=0; i<8; i++) allpassDelays_[i].clear(); |
| 63 |
lastOutput_[0] = 0.0; |
| 64 |
lastOutput_[1] = 0.0; |
| 65 |
lowpassState_ = 0.0; |
| 66 |
} |
| 67 |
|
| 68 |
void NRev :: setT60( StkFloat T60 ) |
| 69 |
{ |
| 70 |
for ( int i=0; i<6; i++ ) |
| 71 |
combCoefficient_[i] = pow(10.0, (-3.0 * combDelays_[i].getDelay() / (T60 * Stk::sampleRate()))); |
| 72 |
} |
| 73 |
|
| 74 |
StkFloat NRev :: computeSample(StkFloat input) |
| 75 |
{ |
| 76 |
StkFloat temp, temp0, temp1, temp2, temp3; |
| 77 |
int i; |
| 78 |
|
| 79 |
temp0 = 0.0; |
| 80 |
for (i=0; i<6; i++) { |
| 81 |
temp = input + (combCoefficient_[i] * combDelays_[i].lastOut()); |
| 82 |
temp0 += combDelays_[i].tick(temp); |
| 83 |
} |
| 84 |
for (i=0; i<3; i++) { |
| 85 |
temp = allpassDelays_[i].lastOut(); |
| 86 |
temp1 = allpassCoefficient_ * temp; |
| 87 |
temp1 += temp0; |
| 88 |
allpassDelays_[i].tick(temp1); |
| 89 |
temp0 = -(allpassCoefficient_ * temp1) + temp; |
| 90 |
} |
| 91 |
|
| 92 |
// One-pole lowpass filter. |
| 93 |
lowpassState_ = 0.7*lowpassState_ + 0.3*temp0; |
| 94 |
temp = allpassDelays_[3].lastOut(); |
| 95 |
temp1 = allpassCoefficient_ * temp; |
| 96 |
temp1 += lowpassState_; |
| 97 |
allpassDelays_[3].tick(temp1); |
| 98 |
temp1 = -(allpassCoefficient_ * temp1) + temp; |
| 99 |
|
| 100 |
temp = allpassDelays_[4].lastOut(); |
| 101 |
temp2 = allpassCoefficient_ * temp; |
| 102 |
temp2 += temp1; |
| 103 |
allpassDelays_[4].tick(temp2); |
| 104 |
lastOutput_[0] = effectMix_*(-(allpassCoefficient_ * temp2) + temp); |
| 105 |
|
| 106 |
temp = allpassDelays_[5].lastOut(); |
| 107 |
temp3 = allpassCoefficient_ * temp; |
| 108 |
temp3 += temp1; |
| 109 |
allpassDelays_[5].tick(temp3); |
| 110 |
lastOutput_[1] = effectMix_*(-(allpassCoefficient_ * temp3) + temp); |
| 111 |
|
| 112 |
temp = (1.0 - effectMix_) * input; |
| 113 |
lastOutput_[0] += temp; |
| 114 |
lastOutput_[1] += temp; |
| 115 |
|
| 116 |
return Effect::lastOut(); |
| 117 |
} |