-
Notifications
You must be signed in to change notification settings - Fork 185
/
Twang.h
160 lines (128 loc) · 5.19 KB
/
Twang.h
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#ifndef STK_TWANG_H
#define STK_TWANG_H
#include "Stk.h"
#include "DelayA.h"
#include "DelayL.h"
#include "Fir.h"
namespace stk {
/***************************************************/
/*! \class Twang
\brief STK enhanced plucked string class.
This class implements an enhanced plucked-string
physical model, a la Jaffe-Smith, Smith,
Karjalainen and others. It includes a comb
filter to simulate pluck position. The tick()
function takes an input sample, which is added
to the delayline input. This can be used to
implement commuted synthesis (if the input
samples are derived from the impulse response of
a body filter) and/or feedback (as in an electric
guitar model).
This is a digital waveguide model, making its
use possibly subject to patents held by Stanford
University, Yamaha, and others.
by Perry R. Cook and Gary P. Scavone, 1995--2023.
*/
/***************************************************/
class Twang : public Stk
{
public:
//! Class constructor, taking the lowest desired playing frequency.
Twang( StkFloat lowestFrequency = 50.0 );
//! Reset and clear all internal state.
void clear( void );
//! Set the delayline parameters to allow frequencies as low as specified.
void setLowestFrequency( StkFloat frequency );
//! Set the delayline parameters for a particular frequency.
void setFrequency( StkFloat frequency );
//! Set the pluck or "excitation" position along the string (0.0 - 1.0).
void setPluckPosition( StkFloat position );
//! Set the nominal loop gain.
/*!
The actual loop gain is based on the value set with this
function, but scaled slightly according to the frequency. Higher
frequency settings have greater loop gains because of
high-frequency loop-filter roll-off.
*/
void setLoopGain( StkFloat loopGain );
//! Set the loop filter coefficients.
/*!
The loop filter can be any arbitrary FIR filter. By default,
the coefficients are set for a first-order lowpass filter with
coefficients b = [0.5 0.5].
*/
void setLoopFilter( std::vector<StkFloat> coefficients );
//! Return an StkFrames reference to the last output sample frame.
//const StkFrames& lastFrame( void ) const { return lastFrame_; };
//! Return the last computed output value.
// StkFloat lastOut( void ) { return lastFrame_[0]; };
StkFloat lastOut( void ) { return lastOutput_; };
//! Compute and return one output sample.
StkFloat tick( StkFloat input );
//! Take a channel of the \c iFrames object as inputs to the class and write outputs to the \c oFrames object.
/*!
The \c iFrames object reference is returned. Each channel
argument must be less than the number of channels in the
corresponding StkFrames argument (the first channel is specified
by 0). However, range checking is only performed if _STK_DEBUG_
is defined during compilation, in which case an out-of-range value
will trigger an StkError exception.
*/
StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
//! Take a channel of the \c iFrames object as inputs to the effect and write outputs to the \c oFrames object.
/*!
The \c iFrames object reference is returned. Each channel
argument must be less than the number of channels in the
corresponding StkFrames argument (the first channel is specified
by 0). However, range checking is only performed if _STK_DEBUG_
is defined during compilation, in which case an out-of-range value
will trigger an StkError exception.
*/
StkFrames& tick( StkFrames& iFrames, StkFrames &oFrames, unsigned int iChannel = 0, unsigned int oChannel = 0 );
protected:
DelayA delayLine_;
DelayL combDelay_;
Fir loopFilter_;
StkFloat lastOutput_;
StkFloat frequency_;
StkFloat loopGain_;
StkFloat pluckPosition_;
};
inline StkFloat Twang :: tick( StkFloat input )
{
lastOutput_ = delayLine_.tick( input + loopFilter_.tick( delayLine_.lastOut() ) );
lastOutput_ -= combDelay_.tick( lastOutput_ ); // comb filtering on output
lastOutput_ *= 0.5;
return lastOutput_;
}
inline StkFrames& Twang :: tick( StkFrames& frames, unsigned int channel )
{
#if defined(_STK_DEBUG_)
if ( channel >= frames.channels() ) {
oStream_ << "Twang::tick(): channel and StkFrames arguments are incompatible!";
handleError( StkError::FUNCTION_ARGUMENT );
}
#endif
StkFloat *samples = &frames[channel];
unsigned int hop = frames.channels();
for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
*samples = tick( *samples );
return frames;
}
inline StkFrames& Twang :: tick( StkFrames& iFrames, StkFrames& oFrames, unsigned int iChannel, unsigned int oChannel )
{
#if defined(_STK_DEBUG_)
if ( iChannel >= iFrames.channels() || oChannel >= oFrames.channels() ) {
oStream_ << "Twang::tick(): channel and StkFrames arguments are incompatible!";
handleError( StkError::FUNCTION_ARGUMENT );
}
#endif
StkFloat *iSamples = &iFrames[iChannel];
StkFloat *oSamples = &oFrames[oChannel];
unsigned int iHop = iFrames.channels(), oHop = oFrames.channels();
for ( unsigned int i=0; i<iFrames.frames(); i++, iSamples += iHop, oSamples += oHop )
*oSamples = tick( *iSamples );
return iFrames;
}
} // stk namespace
#endif