Arduino STK  4.6.2
Clarinet.h
1 #ifndef STK_CLARINET_H
2 #define STK_CLARINET_H
3 
4 #include "Instrmnt.h"
5 #include "DelayL.h"
6 #include "ReedTable.h"
7 #include "OneZero.h"
8 #include "Envelope.h"
9 #include "Noise.h"
10 #include "SineWave.h"
11 
12 namespace stk {
13 
14 /***************************************************/
36 /***************************************************/
37 
38 class Clarinet : public Instrmnt
39 {
40  public:
42 
45  Clarinet( StkFloat lowestFrequency = 8.0 );
46 
48  ~Clarinet( void );
49 
51  void clear( void );
52 
54  void setFrequency( StkFloat frequency );
55 
57  void startBlowing( StkFloat amplitude, StkFloat rate );
58 
60  void stopBlowing( StkFloat rate );
61 
63  void noteOn( StkFloat frequency, StkFloat amplitude );
64 
66  void noteOff( StkFloat amplitude );
67 
69  void controlChange( int number, StkFloat value );
70 
72  StkFloat tick( unsigned int channel = 0 );
73 
75 
82  StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
83 
84  protected:
85 
86  DelayL delayLine_;
87  ReedTable reedTable_;
88  OneZero filter_;
89  Envelope envelope_;
90  Noise noise_;
91  SineWave vibrato_;
92 
93  StkFloat outputGain_;
94  StkFloat noiseGain_;
95  StkFloat vibratoGain_;
96 };
97 
98 inline StkFloat Clarinet :: tick( unsigned int )
99 {
100  StkFloat pressureDiff;
101  StkFloat breathPressure;
102 
103  // Calculate the breath pressure (envelope + noise + vibrato)
104  breathPressure = envelope_.tick();
105  breathPressure += breathPressure * noiseGain_ * noise_.tick();
106  breathPressure += breathPressure * vibratoGain_ * vibrato_.tick();
107 
108  // Perform commuted loss filtering.
109  pressureDiff = -0.95 * filter_.tick( delayLine_.lastOut() );
110 
111  // Calculate pressure difference of reflected and mouthpiece pressures.
112  pressureDiff = pressureDiff - breathPressure;
113 
114  // Perform non-linear scattering using pressure difference in reed function.
115  lastFrame_[0] = delayLine_.tick(breathPressure + pressureDiff * reedTable_.tick(pressureDiff));
116 
117  // Apply output gain.
118  lastFrame_[0] *= outputGain_;
119 
120  return lastFrame_[0];
121 }
122 
123 inline StkFrames& Clarinet :: tick( StkFrames& frames, unsigned int channel )
124 {
125  unsigned int nChannels = lastFrame_.channels();
126 #if defined(_STK_DEBUG_)
127  if ( channel > frames.channels() - nChannels ) {
128  oStream_ << "Clarinet::tick(): channel and StkFrames arguments are incompatible!";
129  handleError( StkError::FUNCTION_ARGUMENT );
130  }
131 #endif
132 
133  StkFloat *samples = &frames[channel];
134  unsigned int j, hop = frames.channels() - nChannels;
135  if ( nChannels == 1 ) {
136  for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
137  *samples++ = tick();
138  }
139  else {
140  for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
141  *samples++ = tick();
142  for ( j=1; j<nChannels; j++ )
143  *samples++ = lastFrame_[j];
144  }
145  }
146 
147  return frames;
148 }
149 
150 } // stk namespace
151 
152 #endif
STK clarinet physical model class.
Definition: Clarinet.h:39
void startBlowing(StkFloat amplitude, StkFloat rate)
Apply breath pressure to instrument with given amplitude and rate of increase.
void noteOn(StkFloat frequency, StkFloat amplitude)
Start a note with the given frequency and amplitude.
StkFloat tick(unsigned int channel=0)
Compute and return one output sample.
Definition: Clarinet.h:98
void noteOff(StkFloat amplitude)
Stop a note with the given amplitude (speed of decay).
~Clarinet(void)
Class destructor.
void controlChange(int number, StkFloat value)
Perform the control change specified by number and value (0.0 - 128.0).
void clear(void)
Reset and clear all internal state.
Clarinet(StkFloat lowestFrequency=8.0)
Class constructor, taking the lowest desired playing frequency.
void stopBlowing(StkFloat rate)
Decrease breath pressure with given rate of decrease.
void setFrequency(StkFloat frequency)
Set instrument parameters for a particular frequency.
STK linear interpolating delay line class.
Definition: DelayL.h:28
StkFloat lastOut(void) const
Return the last computed output value.
Definition: DelayL.h:76
StkFloat tick(StkFloat input)
Input one sample to the filter and return one output.
Definition: DelayL.h:163
STK linear line envelope class.
Definition: Envelope.h:22
StkFloat tick(void)
Compute and return one output sample.
Definition: Envelope.h:88
STK instrument abstract base class.
Definition: Instrmnt.h:20
STK noise generator.
Definition: Noise.h:22
StkFloat tick(void)
Compute and return one output sample.
Definition: Noise.h:59
STK one-zero filter class.
Definition: OneZero.h:21
StkFloat tick(StkFloat input)
Input one sample to the filter and return one output.
Definition: OneZero.h:79
STK reed table class.
Definition: ReedTable.h:28
StkFloat tick(StkFloat input)
Take one sample input and map to one sample of output.
Definition: ReedTable.h:81
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