You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
6.9KB

  1. #ifndef STK_GUITAR_H
  2. #define STK_GUITAR_H
  3. #include "Stk.h"
  4. #include "Twang.h"
  5. #include "OnePole.h"
  6. #include "OneZero.h"
  7. namespace stk {
  8. /***************************************************/
  9. /*! \class Guitar
  10. \brief STK guitar model class.
  11. This class implements a guitar model with an arbitrary number of
  12. strings (specified during instantiation). Each string is
  13. represented by an stk::Twang object. The model supports commuted
  14. synthesis, as discussed by Smith and Karjalainen. It also includes
  15. a basic body coupling model and supports feedback.
  16. This class does not attempt voice management. Rather, most
  17. functions support a parameter to specify a particular string
  18. number and string (voice) management is assumed to occur
  19. externally. Note that this class does not inherit from
  20. stk::Instrmnt because of API inconsistencies.
  21. This is a digital waveguide model, making its use possibly subject
  22. to patents held by Stanford University, Yamaha, and others.
  23. Control Change Numbers:
  24. - Bridge Coupling Gain = 2
  25. - Pluck Position = 4
  26. - Loop Gain = 11
  27. - Coupling Filter Pole = 1
  28. - Pick Filter Pole = 128
  29. by Gary P. Scavone, 2012.
  30. */
  31. /***************************************************/
  32. class Guitar : public Stk
  33. {
  34. public:
  35. //! Class constructor, specifying an arbitrary number of strings (default = 6).
  36. Guitar( unsigned int nStrings = 6, std::string bodyfile = "" );
  37. //! Reset and clear all internal state.
  38. void clear( void );
  39. //! Set the string excitation, using either a soundfile or computed noise.
  40. /*!
  41. If no argument is provided, the std::string is empty, or an error
  42. occurs reading the file data, an enveloped noise signal will be
  43. generated for use as the pluck excitation.
  44. */
  45. void setBodyFile( std::string bodyfile = "" );
  46. //! Set the pluck position for one or all strings.
  47. /*!
  48. If the \c string argument is < 0, the pluck position is set
  49. for all strings.
  50. */
  51. void setPluckPosition( StkFloat position, int string = -1 );
  52. //! Set the loop gain for one or all strings.
  53. /*!
  54. If the \c string argument is < 0, the loop gain is set for all
  55. strings.
  56. */
  57. void setLoopGain( StkFloat gain, int string = -1 );
  58. //! Set instrument parameters for a particular frequency.
  59. void setFrequency( StkFloat frequency, unsigned int string = 0 );
  60. //! Start a note with the given frequency and amplitude.
  61. /*!
  62. If the \c amplitude parameter is less than 0.2, the string will
  63. be undamped but it will not be "plucked."
  64. */
  65. void noteOn( StkFloat frequency, StkFloat amplitude, unsigned int string = 0 );
  66. //! Stop a note with the given amplitude (speed of decay).
  67. void noteOff( StkFloat amplitude, unsigned int string = 0 );
  68. //! Perform the control change specified by \e number and \e value (0.0 - 128.0).
  69. /*!
  70. If the \c string argument is < 0, then the control change is
  71. applied to all strings (if appropriate).
  72. */
  73. void controlChange( int number, StkFloat value, int string = -1 );
  74. //! Return the last computed output value.
  75. StkFloat lastOut( void ) { return lastFrame_[0]; };
  76. //! Take an optional input sample and compute one output sample.
  77. StkFloat tick( StkFloat input = 0.0 );
  78. //! Take a channel of the \c iFrames object as inputs to the class and write outputs to the \c oFrames object.
  79. /*!
  80. The \c iFrames object reference is returned. Each channel
  81. argument must be less than the number of channels in the
  82. corresponding StkFrames argument (the first channel is specified
  83. by 0). However, range checking is only performed if _STK_DEBUG_
  84. is defined during compilation, in which case an out-of-range value
  85. will trigger an StkError exception.
  86. */
  87. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  88. //! Take a channel of the \c iFrames object as inputs to the effect and write outputs to the \c oFrames object.
  89. /*!
  90. The \c iFrames object reference is returned. Each channel
  91. argument must be less than the number of channels in the
  92. corresponding StkFrames argument (the first channel is specified
  93. by 0). However, range checking is only performed if _STK_DEBUG_
  94. is defined during compilation, in which case an out-of-range value
  95. will trigger an StkError exception.
  96. */
  97. StkFrames& tick( StkFrames& iFrames, StkFrames &oFrames, unsigned int iChannel = 0, unsigned int oChannel = 0 );
  98. protected:
  99. std::vector< stk::Twang > strings_;
  100. std::vector< int > stringState_; // 0 = off, 1 = decaying, 2 = on
  101. std::vector< unsigned int > decayCounter_;
  102. std::vector< unsigned int > filePointer_;
  103. std::vector< StkFloat > pluckGains_;
  104. OnePole pickFilter_;
  105. OnePole couplingFilter_;
  106. StkFloat couplingGain_;
  107. StkFrames excitation_;
  108. StkFrames lastFrame_;
  109. };
  110. inline StkFloat Guitar :: tick( StkFloat input )
  111. {
  112. StkFloat temp, output = 0.0;
  113. lastFrame_[0] /= strings_.size(); // evenly spread coupling across strings
  114. for ( unsigned int i=0; i<strings_.size(); i++ ) {
  115. if ( stringState_[i] ) {
  116. temp = input;
  117. // If pluckGain < 0.2, let string ring but don't pluck it.
  118. if ( filePointer_[i] < excitation_.frames() && pluckGains_[i] > 0.2 )
  119. temp += pluckGains_[i] * excitation_[filePointer_[i]++];
  120. temp += couplingGain_ * couplingFilter_.tick( lastFrame_[0] ); // bridge coupling
  121. output += strings_[i].tick( temp );
  122. // Check if string energy has decayed sufficiently to turn it off.
  123. if ( stringState_[i] == 1 ) {
  124. if ( fabs( strings_[i].lastOut() ) < 0.001 ) decayCounter_[i]++;
  125. else decayCounter_[i] = 0;
  126. if ( decayCounter_[i] > (unsigned int) floor( 0.1 * Stk::sampleRate() ) ) {
  127. stringState_[i] = 0;
  128. decayCounter_[i] = 0;
  129. }
  130. }
  131. }
  132. }
  133. return lastFrame_[0] = output;
  134. }
  135. inline StkFrames& Guitar :: tick( StkFrames& frames, unsigned int channel )
  136. {
  137. #if defined(_STK_DEBUG_)
  138. if ( channel >= frames.channels() ) {
  139. oStream_ << "Guitar::tick(): channel and StkFrames arguments are incompatible!";
  140. handleError( StkError::FUNCTION_ARGUMENT );
  141. }
  142. #endif
  143. StkFloat *samples = &frames[channel];
  144. unsigned int hop = frames.channels();
  145. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
  146. *samples = tick( *samples );
  147. return frames;
  148. }
  149. inline StkFrames& Guitar :: tick( StkFrames& iFrames, StkFrames& oFrames, unsigned int iChannel, unsigned int oChannel )
  150. {
  151. #if defined(_STK_DEBUG_)
  152. if ( iChannel >= iFrames.channels() || oChannel >= oFrames.channels() ) {
  153. oStream_ << "Guitar::tick(): channel and StkFrames arguments are incompatible!";
  154. handleError( StkError::FUNCTION_ARGUMENT );
  155. }
  156. #endif
  157. StkFloat *iSamples = &iFrames[iChannel];
  158. StkFloat *oSamples = &oFrames[oChannel];
  159. unsigned int iHop = iFrames.channels(), oHop = oFrames.channels();
  160. for ( unsigned int i=0; i<iFrames.frames(); i++, iSamples += iHop, oSamples += oHop )
  161. *oSamples = tick( *iSamples );
  162. return iFrames;
  163. }
  164. } // stk namespace
  165. #endif