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.

142 lines
3.9KB

  1. // Copyright 2015 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. // See http://creativecommons.org/licenses/MIT/ for more information.
  24. //
  25. // -----------------------------------------------------------------------------
  26. //
  27. // Part for the string synth easter egg.
  28. #ifndef RINGS_DSP_STRING_SYNTH_PART_H_
  29. #define RINGS_DSP_STRING_SYNTH_PART_H_
  30. #include "stmlib/stmlib.h"
  31. #include "stmlib/dsp/filter.h"
  32. #include "rings/dsp/dsp.h"
  33. #include "rings/dsp/fx/chorus.h"
  34. #include "rings/dsp/fx/ensemble.h"
  35. #include "rings/dsp/fx/reverb.h"
  36. #include "rings/dsp/limiter.h"
  37. #include "rings/dsp/note_filter.h"
  38. #include "rings/dsp/patch.h"
  39. #include "rings/dsp/performance_state.h"
  40. #include "rings/dsp/string_synth_envelope.h"
  41. #include "rings/dsp/string_synth_voice.h"
  42. namespace rings {
  43. const int32_t kMaxStringSynthPolyphony = 4;
  44. const int32_t kStringSynthVoices = 12;
  45. const int32_t kMaxChordSize = 8;
  46. const int32_t kNumHarmonics = 3;
  47. const int32_t kNumFormants = 3;
  48. enum FxType {
  49. FX_FORMANT,
  50. FX_CHORUS,
  51. FX_REVERB,
  52. FX_FORMANT_2,
  53. FX_ENSEMBLE,
  54. FX_REVERB_2,
  55. FX_LAST
  56. };
  57. struct VoiceGroup {
  58. float tonic;
  59. StringSynthEnvelope envelope;
  60. int32_t chord;
  61. float structure;
  62. };
  63. class StringSynthPart {
  64. public:
  65. StringSynthPart() { }
  66. ~StringSynthPart() { }
  67. void Init(uint16_t* reverb_buffer);
  68. void Process(
  69. const PerformanceState& performance_state,
  70. const Patch& patch,
  71. const float* in,
  72. float* out,
  73. float* aux,
  74. size_t size);
  75. inline void set_polyphony(int32_t polyphony) {
  76. int32_t old_polyphony = polyphony_;
  77. polyphony_ = std::min(polyphony, kMaxStringSynthPolyphony);
  78. for (int32_t i = old_polyphony; i < polyphony_; ++i) {
  79. group_[i].tonic = group_[0].tonic + i * 0.01f;
  80. }
  81. if (active_group_ >= polyphony_) {
  82. active_group_ = 0;
  83. }
  84. }
  85. inline void set_fx(FxType fx_type) {
  86. if ((fx_type % 3) != (fx_type_ % 3)) {
  87. clear_fx_ = true;
  88. }
  89. fx_type_ = fx_type;
  90. }
  91. private:
  92. void ProcessEnvelopes(float shape, uint8_t* flags, float* values);
  93. void ComputeRegistration(float gain, float registration, float* amplitudes);
  94. void ProcessFormantFilter(float vowel, float shift, float resonance,
  95. float* out, float* aux, size_t size);
  96. StringSynthVoice<kNumHarmonics> voice_[kStringSynthVoices];
  97. VoiceGroup group_[kMaxStringSynthPolyphony];
  98. stmlib::Svf formant_filter_[kNumFormants];
  99. Ensemble ensemble_;
  100. Reverb reverb_;
  101. Chorus chorus_;
  102. Limiter limiter_;
  103. int32_t num_voices_;
  104. int32_t active_group_;
  105. uint32_t step_counter_;
  106. int32_t polyphony_;
  107. int32_t acquisition_delay_;
  108. FxType fx_type_;
  109. NoteFilter note_filter_;
  110. float filter_in_buffer_[kMaxBlockSize];
  111. float filter_out_buffer_[kMaxBlockSize];
  112. bool clear_fx_;
  113. DISALLOW_COPY_AND_ASSIGN(StringSynthPart);
  114. };
  115. } // namespace rings
  116. #endif // RINGS_DSP_STRING_SYNTH_VOICE_H_