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.

145 lines
4.3KB

  1. #ifndef STK_MESH2D_H
  2. #define STK_MESH2D_H
  3. #include "Instrmnt.h"
  4. #include "OnePole.h"
  5. namespace stk {
  6. /***************************************************/
  7. /*! \class Mesh2D
  8. \brief Two-dimensional rectilinear waveguide mesh class.
  9. This class implements a rectilinear,
  10. two-dimensional digital waveguide mesh
  11. structure. For details, see Van Duyne and
  12. Smith, "Physical Modeling with the 2-D Digital
  13. Waveguide Mesh", Proceedings of the 1993
  14. International Computer Music Conference.
  15. This is a digital waveguide model, making its
  16. use possibly subject to patents held by Stanford
  17. University, Yamaha, and others.
  18. Control Change Numbers:
  19. - X Dimension = 2
  20. - Y Dimension = 4
  21. - Mesh Decay = 11
  22. - X-Y Input Position = 1
  23. by Julius Smith, 2000 - 2002.
  24. Revised by Gary Scavone for STK, 2002.
  25. */
  26. /***************************************************/
  27. const unsigned short NXMAX = 12;
  28. const unsigned short NYMAX = 12;
  29. class Mesh2D : public Instrmnt
  30. {
  31. public:
  32. //! Class constructor, taking the x and y dimensions in samples.
  33. Mesh2D( unsigned short nX, unsigned short nY );
  34. //! Class destructor.
  35. ~Mesh2D( void );
  36. //! Reset and clear all internal state.
  37. void clear( void );
  38. //! Set the x dimension size in samples.
  39. void setNX( unsigned short lenX );
  40. //! Set the y dimension size in samples.
  41. void setNY( unsigned short lenY );
  42. //! Set the x, y input position on a 0.0 - 1.0 scale.
  43. void setInputPosition( StkFloat xFactor, StkFloat yFactor );
  44. //! Set the loss filters gains (0.0 - 1.0).
  45. void setDecay( StkFloat decayFactor );
  46. //! Impulse the mesh with the given amplitude (frequency ignored).
  47. void noteOn( StkFloat frequency, StkFloat amplitude );
  48. //! Stop a note with the given amplitude (speed of decay) ... currently ignored.
  49. void noteOff( StkFloat amplitude );
  50. //! Calculate and return the signal energy stored in the mesh.
  51. StkFloat energy( void );
  52. //! Input a sample to the mesh and compute one output sample.
  53. StkFloat inputTick( StkFloat input );
  54. //! Perform the control change specified by \e number and \e value (0.0 - 128.0).
  55. void controlChange( int number, StkFloat value );
  56. //! Compute and return one output sample.
  57. StkFloat tick( unsigned int channel = 0 );
  58. //! Fill a channel of the StkFrames object with computed outputs.
  59. /*!
  60. The \c channel argument must be less than the number of
  61. channels in the StkFrames argument (the first channel is specified
  62. by 0). However, range checking is only performed if _STK_DEBUG_
  63. is defined during compilation, in which case an out-of-range value
  64. will trigger an StkError exception.
  65. */
  66. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  67. protected:
  68. StkFloat tick0();
  69. StkFloat tick1();
  70. void clearMesh();
  71. unsigned short NX_, NY_;
  72. unsigned short xInput_, yInput_;
  73. OnePole filterX_[NXMAX];
  74. OnePole filterY_[NYMAX];
  75. StkFloat v_[NXMAX-1][NYMAX-1]; // junction velocities
  76. StkFloat vxp_[NXMAX][NYMAX]; // positive-x velocity wave
  77. StkFloat vxm_[NXMAX][NYMAX]; // negative-x velocity wave
  78. StkFloat vyp_[NXMAX][NYMAX]; // positive-y velocity wave
  79. StkFloat vym_[NXMAX][NYMAX]; // negative-y velocity wave
  80. // Alternate buffers
  81. StkFloat vxp1_[NXMAX][NYMAX]; // positive-x velocity wave
  82. StkFloat vxm1_[NXMAX][NYMAX]; // negative-x velocity wave
  83. StkFloat vyp1_[NXMAX][NYMAX]; // positive-y velocity wave
  84. StkFloat vym1_[NXMAX][NYMAX]; // negative-y velocity wave
  85. int counter_; // time in samples
  86. };
  87. inline StkFrames& Mesh2D :: tick( StkFrames& frames, unsigned int channel )
  88. {
  89. unsigned int nChannels = lastFrame_.channels();
  90. #if defined(_STK_DEBUG_)
  91. if ( channel > frames.channels() - nChannels ) {
  92. oStream_ << "Mesh2D::tick(): channel and StkFrames arguments are incompatible!";
  93. handleError( StkError::FUNCTION_ARGUMENT );
  94. }
  95. #endif
  96. StkFloat *samples = &frames[channel];
  97. unsigned int j, hop = frames.channels() - nChannels;
  98. if ( nChannels == 1 ) {
  99. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
  100. *samples++ = tick();
  101. }
  102. else {
  103. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
  104. *samples++ = tick();
  105. for ( j=1; j<nChannels; j++ )
  106. *samples++ = lastFrame_[j];
  107. }
  108. }
  109. return frames;
  110. }
  111. } // stk namespace
  112. #endif