Arduino STK  4.6.2
Modal.h
1 #ifndef STK_MODAL_H
2 #define STK_MODAL_H
3 
4 #include "Instrmnt.h"
5 #include "Envelope.h"
6 #include "FileLoop.h"
7 #include "MemoryLoop.h"
8 #include "MemoryWvIn.h"
9 #include "SineWave.h"
10 #include "BiQuad.h"
11 #include "OnePole.h"
12 
13 namespace stk {
14 
15 /***************************************************/
26 /***************************************************/
27 
28 class Modal : public Instrmnt
29 {
30 public:
32 
35  Modal( unsigned int modes = 4 );
36 
38  virtual ~Modal( void );
39 
41  void clear( void );
42 
44  virtual void setFrequency( StkFloat frequency );
45 
47  void setRatioAndRadius( unsigned int modeIndex, StkFloat ratio, StkFloat radius );
48 
50  void setMasterGain( StkFloat aGain ) { masterGain_ = aGain; };
51 
53  void setDirectGain( StkFloat aGain ) { directGain_ = aGain; };
54 
56  void setModeGain( unsigned int modeIndex, StkFloat gain );
57 
59  virtual void strike( StkFloat amplitude );
60 
62  void damp( StkFloat amplitude );
63 
65  void noteOn( StkFloat frequency, StkFloat amplitude );
66 
68  void noteOff( StkFloat amplitude );
69 
71  virtual void controlChange( int number, StkFloat value ) = 0;
72 
74  StkFloat tick( unsigned int channel = 0 );
75 
77 
84  StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
85 
86 protected:
87 
88  Envelope envelope_;
89  FileWvIn *wave_;
90  BiQuad **filters_;
91  OnePole onepole_;
92  SineWave vibrato_;
93 
94  unsigned int nModes_;
95  std::vector<StkFloat> ratios_;
96  std::vector<StkFloat> radii_;
97 
98  StkFloat vibratoGain_;
99  StkFloat masterGain_;
100  StkFloat directGain_;
101  StkFloat stickHardness_;
102  StkFloat strikePosition_;
103  StkFloat baseFrequency_;
104 };
105 
106 inline StkFloat Modal :: tick( unsigned int )
107 {
108  StkFloat temp = masterGain_ * onepole_.tick( wave_->tick() * envelope_.tick() );
109 
110  StkFloat temp2 = 0.0;
111  for ( unsigned int i=0; i<nModes_; i++ )
112  temp2 += filters_[i]->tick(temp);
113 
114  temp2 -= temp2 * directGain_;
115  temp2 += directGain_ * temp;
116 
117  if ( vibratoGain_ != 0.0 ) {
118  // Calculate AM and apply to master out
119  temp = 1.0 + ( vibrato_.tick() * vibratoGain_ );
120  temp2 = temp * temp2;
121  }
122 
123  lastFrame_[0] = temp2;
124  return lastFrame_[0];
125 }
126 
127 inline StkFrames& Modal :: tick( StkFrames& frames, unsigned int channel )
128 {
129  unsigned int nChannels = lastFrame_.channels();
130 #if defined(_STK_DEBUG_)
131  if ( channel > frames.channels() - nChannels ) {
132  oStream_ << "Modal::tick(): channel and StkFrames arguments are incompatible!";
133  handleError( StkError::FUNCTION_ARGUMENT );
134  }
135 #endif
136 
137  StkFloat *samples = &frames[channel];
138  unsigned int j, hop = frames.channels() - nChannels;
139  if ( nChannels == 1 ) {
140  for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
141  *samples++ = tick();
142  }
143  else {
144  for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
145  *samples++ = tick();
146  for ( j=1; j<nChannels; j++ )
147  *samples++ = lastFrame_[j];
148  }
149  }
150 
151  return frames;
152 }
153 
154 } // stk namespace
155 
156 #endif
STK biquad (two-pole, two-zero) filter class.
Definition: BiQuad.h:21
STK linear line envelope class.
Definition: Envelope.h:22
StkFloat tick(void)
Compute and return one output sample.
Definition: Envelope.h:88
STK audio file input class.
Definition: FileWvIn.h:53
virtual StkFloat tick(unsigned int channel=0)
Compute a sample frame and return the specified channel value.
STK instrument abstract base class.
Definition: Instrmnt.h:20
STK resonance model abstract base class.
Definition: Modal.h:29
void setModeGain(unsigned int modeIndex, StkFloat gain)
Set the gain for a specified mode filter.
void damp(StkFloat amplitude)
Damp modes with a given decay factor (0.0 - 1.0).
void noteOff(StkFloat amplitude)
Stop a note with the given amplitude (speed of decay).
void noteOn(StkFloat frequency, StkFloat amplitude)
Start a note with the given frequency and amplitude.
virtual void setFrequency(StkFloat frequency)
Set instrument parameters for a particular frequency.
void setMasterGain(StkFloat aGain)
Set the master gain.
Definition: Modal.h:50
void setDirectGain(StkFloat aGain)
Set the direct gain.
Definition: Modal.h:53
void setRatioAndRadius(unsigned int modeIndex, StkFloat ratio, StkFloat radius)
Set the ratio and radius for a specified mode filter.
virtual ~Modal(void)
Class destructor.
virtual void controlChange(int number, StkFloat value)=0
Perform the control change specified by number and value (0.0 - 128.0).
virtual void strike(StkFloat amplitude)
Initiate a strike with the given amplitude (0.0 - 1.0).
StkFloat tick(unsigned int channel=0)
Compute and return one output sample.
Definition: Modal.h:106
void clear(void)
Reset and clear all internal state.
Modal(unsigned int modes=4)
Class constructor, taking the desired number of modes to create.
STK one-pole filter class.
Definition: OnePole.h:21
StkFloat tick(StkFloat input)
Input one sample to the filter and return one output.
Definition: OnePole.h:80
STK sinusoid oscillator class.
Definition: SineWave.h:26
StkFloat tick(void)
Compute and return one output sample.
Definition: SineWave.h:99
An STK class to handle vectorized audio data.
Definition: Stk.h:287
unsigned int channels(void) const
Return the number of channels represented by the data.
Definition: Stk.h:415
unsigned int frames(void) const
Return the number of sample frames represented by the data.
Definition: Stk.h:418
static void handleError(const char *message, StkError::Type type)
Static function for error reporting and handling using c-strings.
The STK namespace.
Definition: ADSR.h:8