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.

162 lines
4.6KB

  1. #ifndef STK_VOICFORM_H
  2. #define STK_VOICFORM_H
  3. #include "Instrmnt.h"
  4. #include "Envelope.h"
  5. #include "Noise.h"
  6. #include "SingWave.h"
  7. #include "FormSwep.h"
  8. #include "OnePole.h"
  9. #include "OneZero.h"
  10. namespace stk {
  11. /***************************************************/
  12. /*! \class VoicForm
  13. \brief Four formant synthesis instrument.
  14. This instrument contains an excitation singing
  15. wavetable (looping wave with random and
  16. periodic vibrato, smoothing on frequency,
  17. etc.), excitation noise, and four sweepable
  18. complex resonances.
  19. Measured formant data is included, and enough
  20. data is there to support either parallel or
  21. cascade synthesis. In the floating point case
  22. cascade synthesis is the most natural so
  23. that's what you'll find here.
  24. Control Change Numbers:
  25. - Voiced/Unvoiced Mix = 2
  26. - Vowel/Phoneme Selection = 4
  27. - Vibrato Frequency = 11
  28. - Vibrato Gain = 1
  29. - Loudness (Spectral Tilt) = 128
  30. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  31. */
  32. /***************************************************/
  33. class VoicForm : public Instrmnt
  34. {
  35. public:
  36. //! Class constructor.
  37. /*!
  38. An StkError will be thrown if the rawwave path is incorrectly set.
  39. */
  40. VoicForm( void );
  41. //! Class destructor.
  42. ~VoicForm( void );
  43. //! Reset and clear all internal state.
  44. void clear( void );
  45. //! Set instrument parameters for a particular frequency.
  46. void setFrequency( StkFloat frequency );
  47. //! Set instrument parameters for the given phoneme. Returns false if phoneme not found.
  48. bool setPhoneme( const char* phoneme );
  49. //! Set the voiced component gain.
  50. void setVoiced( StkFloat vGain ) { voiced_->setGainTarget(vGain); };
  51. //! Set the unvoiced component gain.
  52. void setUnVoiced( StkFloat nGain ) { noiseEnv_.setTarget(nGain); };
  53. //! Set the sweep rate for a particular formant filter (0-3).
  54. void setFilterSweepRate( unsigned int whichOne, StkFloat rate );
  55. //! Set voiced component pitch sweep rate.
  56. void setPitchSweepRate( StkFloat rate ) { voiced_->setSweepRate(rate); };
  57. //! Start the voice.
  58. void speak( void ) { voiced_->noteOn(); };
  59. //! Stop the voice.
  60. void quiet( void );
  61. //! Start a note with the given frequency and amplitude.
  62. void noteOn( StkFloat frequency, StkFloat amplitude );
  63. //! Stop a note with the given amplitude (speed of decay).
  64. void noteOff( StkFloat amplitude ) { this->quiet(); };
  65. //! Perform the control change specified by \e number and \e value (0.0 - 128.0).
  66. void controlChange( int number, StkFloat value );
  67. //! Compute and return one output sample.
  68. StkFloat tick( unsigned int channel = 0 );
  69. //! Fill a channel of the StkFrames object with computed outputs.
  70. /*!
  71. The \c channel argument must be less than the number of
  72. channels in the StkFrames argument (the first channel is specified
  73. by 0). However, range checking is only performed if _STK_DEBUG_
  74. is defined during compilation, in which case an out-of-range value
  75. will trigger an StkError exception.
  76. */
  77. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  78. protected:
  79. SingWave *voiced_;
  80. Noise noise_;
  81. Envelope noiseEnv_;
  82. FormSwep filters_[4];
  83. OnePole onepole_;
  84. OneZero onezero_;
  85. };
  86. inline StkFloat VoicForm :: tick( unsigned int )
  87. {
  88. StkFloat temp;
  89. temp = onepole_.tick( onezero_.tick( voiced_->tick() ) );
  90. temp += noiseEnv_.tick() * noise_.tick();
  91. lastFrame_[0] = filters_[0].tick(temp);
  92. lastFrame_[0] += filters_[1].tick(temp);
  93. lastFrame_[0] += filters_[2].tick(temp);
  94. lastFrame_[0] += filters_[3].tick(temp);
  95. /*
  96. temp += noiseEnv_.tick() * noise_.tick();
  97. lastFrame_[0] = filters_[0].tick(temp);
  98. lastFrame_[0] = filters_[1].tick(lastFrame_[0]);
  99. lastFrame_[0] = filters_[2].tick(lastFrame_[0]);
  100. lastFrame_[0] = filters_[3].tick(lastFrame_[0]);
  101. */
  102. return lastFrame_[0];
  103. }
  104. inline StkFrames& VoicForm :: tick( StkFrames& frames, unsigned int channel )
  105. {
  106. unsigned int nChannels = lastFrame_.channels();
  107. #if defined(_STK_DEBUG_)
  108. if ( channel > frames.channels() - nChannels ) {
  109. oStream_ << "VoicForm::tick(): channel and StkFrames arguments are incompatible!";
  110. handleError( StkError::FUNCTION_ARGUMENT );
  111. }
  112. #endif
  113. StkFloat *samples = &frames[channel];
  114. unsigned int j, hop = frames.channels() - nChannels;
  115. if ( nChannels == 1 ) {
  116. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
  117. *samples++ = tick();
  118. }
  119. else {
  120. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
  121. *samples++ = tick();
  122. for ( j=1; j<nChannels; j++ )
  123. *samples++ = lastFrame_[j];
  124. }
  125. }
  126. return frames;
  127. }
  128. } // stk namespace
  129. #endif