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.

190 lines
5.6KB

  1. /***************************************************/
  2. /*! \class VoicForm
  3. \brief Four formant synthesis instrument.
  4. This instrument contains an excitation singing
  5. wavetable (looping wave with random and
  6. periodic vibrato, smoothing on frequency,
  7. etc.), excitation noise, and four sweepable
  8. complex resonances.
  9. Measured formant data is included, and enough
  10. data is there to support either parallel or
  11. cascade synthesis. In the floating point case
  12. cascade synthesis is the most natural so
  13. that's what you'll find here.
  14. Control Change Numbers:
  15. - Voiced/Unvoiced Mix = 2
  16. - Vowel/Phoneme Selection = 4
  17. - Vibrato Frequency = 11
  18. - Vibrato Gain = 1
  19. - Loudness (Spectral Tilt) = 128
  20. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  21. */
  22. /***************************************************/
  23. #include "VoicForm.h"
  24. #include "Phonemes.h"
  25. #include "SKINImsg.h"
  26. #include <cstring>
  27. #include <cmath>
  28. namespace stk {
  29. VoicForm :: VoicForm( void ) : Instrmnt()
  30. {
  31. // Concatenate the STK rawwave path to the rawwave file
  32. voiced_ = new SingWave( (Stk::rawwavePath() + "impuls20.raw").c_str(), true );
  33. voiced_->setGainRate( 0.001 );
  34. voiced_->setGainTarget( 0.0 );
  35. for ( int i=0; i<4; i++ )
  36. filters_[i].setSweepRate( 0.001 );
  37. onezero_.setZero( -0.9 );
  38. onepole_.setPole( 0.9 );
  39. noiseEnv_.setRate( 0.001 );
  40. noiseEnv_.setTarget( 0.0 );
  41. this->setPhoneme( "eee" );
  42. this->clear();
  43. }
  44. VoicForm :: ~VoicForm( void )
  45. {
  46. delete voiced_;
  47. }
  48. void VoicForm :: clear( void )
  49. {
  50. onezero_.clear();
  51. onepole_.clear();
  52. for ( int i=0; i<4; i++ ) {
  53. filters_[i].clear();
  54. }
  55. }
  56. void VoicForm :: setFrequency( StkFloat frequency )
  57. {
  58. #if defined(_STK_DEBUG_)
  59. if ( frequency <= 0.0 ) {
  60. oStream_ << "VoicForm::setFrequency: parameter is less than or equal to zero!";
  61. handleError( StkError::WARNING ); return;
  62. }
  63. #endif
  64. voiced_->setFrequency( frequency );
  65. }
  66. bool VoicForm :: setPhoneme( const char *phoneme )
  67. {
  68. bool found = false;
  69. unsigned int i = 0;
  70. while( i < 32 && !found ) {
  71. if ( !strcmp( Phonemes::name(i), phoneme ) ) {
  72. found = true;
  73. filters_[0].setTargets( Phonemes::formantFrequency(i, 0), Phonemes::formantRadius(i, 0), pow(10.0, Phonemes::formantGain(i, 0 ) / 20.0) );
  74. filters_[1].setTargets( Phonemes::formantFrequency(i, 1), Phonemes::formantRadius(i, 1), pow(10.0, Phonemes::formantGain(i, 1 ) / 20.0) );
  75. filters_[2].setTargets( Phonemes::formantFrequency(i, 2), Phonemes::formantRadius(i, 2), pow(10.0, Phonemes::formantGain(i, 2 ) / 20.0) );
  76. filters_[3].setTargets( Phonemes::formantFrequency(i, 3), Phonemes::formantRadius(i, 3), pow(10.0, Phonemes::formantGain(i, 3 ) / 20.0) );
  77. this->setVoiced( Phonemes::voiceGain( i ) );
  78. this->setUnVoiced( Phonemes::noiseGain( i ) );
  79. }
  80. i++;
  81. }
  82. if ( !found ) {
  83. oStream_ << "VoicForm::setPhoneme: phoneme " << phoneme << " not found!";
  84. handleError( StkError::WARNING );
  85. }
  86. return found;
  87. }
  88. void VoicForm :: setFilterSweepRate( unsigned int whichOne, StkFloat rate )
  89. {
  90. if ( whichOne > 3 ) {
  91. oStream_ << "VoicForm::setFilterSweepRate: filter select argument outside range 0-3!";
  92. handleError( StkError::WARNING ); return;
  93. }
  94. filters_[whichOne].setSweepRate(rate);
  95. }
  96. void VoicForm :: quiet( void )
  97. {
  98. voiced_->noteOff();
  99. noiseEnv_.setTarget( 0.0 );
  100. }
  101. void VoicForm :: noteOn( StkFloat frequency, StkFloat amplitude )
  102. {
  103. this->setFrequency( frequency );
  104. voiced_->setGainTarget( amplitude );
  105. onepole_.setPole( 0.97 - (amplitude * 0.2) );
  106. }
  107. void VoicForm :: controlChange( int number, StkFloat value )
  108. {
  109. #if defined(_STK_DEBUG_)
  110. if ( Stk::inRange( value, 0.0, 128.0 ) == false ) {
  111. oStream_ << "VoicForm::controlChange: value (" << value << ") is out of range!";
  112. handleError( StkError::WARNING ); return;
  113. }
  114. #endif
  115. StkFloat normalizedValue = value * ONE_OVER_128;
  116. if (number == __SK_Breath_) { // 2
  117. this->setVoiced( 1.0 - normalizedValue );
  118. this->setUnVoiced( 0.01 * normalizedValue );
  119. }
  120. else if (number == __SK_FootControl_) { // 4
  121. StkFloat temp = 0.0;
  122. unsigned int i = (int) value;
  123. if (i < 32) {
  124. temp = 0.9;
  125. }
  126. else if (i < 64) {
  127. i -= 32;
  128. temp = 1.0;
  129. }
  130. else if (i < 96) {
  131. i -= 64;
  132. temp = 1.1;
  133. }
  134. else if (i < 128) {
  135. i -= 96;
  136. temp = 1.2;
  137. }
  138. else if (i == 128) {
  139. i = 0;
  140. temp = 1.4;
  141. }
  142. filters_[0].setTargets( temp * Phonemes::formantFrequency(i, 0), Phonemes::formantRadius(i, 0), pow(10.0, Phonemes::formantGain(i, 0 ) / 20.0) );
  143. filters_[1].setTargets( temp * Phonemes::formantFrequency(i, 1), Phonemes::formantRadius(i, 1), pow(10.0, Phonemes::formantGain(i, 1 ) / 20.0) );
  144. filters_[2].setTargets( temp * Phonemes::formantFrequency(i, 2), Phonemes::formantRadius(i, 2), pow(10.0, Phonemes::formantGain(i, 2 ) / 20.0) );
  145. filters_[3].setTargets( temp * Phonemes::formantFrequency(i, 3), Phonemes::formantRadius(i, 3), pow(10.0, Phonemes::formantGain(i, 3 ) / 20.0) );
  146. this->setVoiced( Phonemes::voiceGain( i ) );
  147. this->setUnVoiced( Phonemes::noiseGain( i ) );
  148. }
  149. else if (number == __SK_ModFrequency_) // 11
  150. voiced_->setVibratoRate( normalizedValue * 12.0); // 0 to 12 Hz
  151. else if (number == __SK_ModWheel_) // 1
  152. voiced_->setVibratoGain( normalizedValue * 0.2);
  153. else if (number == __SK_AfterTouch_Cont_) { // 128
  154. this->setVoiced( normalizedValue );
  155. onepole_.setPole( 0.97 - ( normalizedValue * 0.2) );
  156. }
  157. #if defined(_STK_DEBUG_)
  158. else {
  159. oStream_ << "VoicForm::controlChange: undefined control number (" << number << ")!";
  160. handleError( StkError::WARNING );
  161. }
  162. #endif
  163. }
  164. } // stk namespace