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.

104 lines
3.2KB

  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. // Strumming logic.
  28. #ifndef RINGS_DSP_STRUMMER_H_
  29. #define RINGS_DSP_STRUMMER_H_
  30. #include "stmlib/stmlib.h"
  31. #include "rings/dsp/onset_detector.h"
  32. #include "rings/dsp/part.h"
  33. namespace rings {
  34. class Strummer {
  35. public:
  36. Strummer() { }
  37. ~Strummer() { }
  38. void Init(float ioi, float sr) {
  39. onset_detector_.Init(
  40. 8.0f / kSampleRate,
  41. 160.0f / kSampleRate,
  42. 1600.0f / kSampleRate,
  43. sr,
  44. ioi);
  45. inhibit_timer_ = static_cast<int32_t>(ioi * sr);
  46. inhibit_counter_ = 0;
  47. previous_note_ = 69.0f;
  48. }
  49. void Process(
  50. const float* in,
  51. size_t size,
  52. PerformanceState* performance_state) {
  53. bool has_onset = in && onset_detector_.Process(in, size);
  54. bool note_changed = fabs(performance_state->note - previous_note_) > 0.4f;
  55. int32_t inhibit_timer = inhibit_timer_;
  56. if (performance_state->internal_strum) {
  57. bool has_external_note_cv = !performance_state->internal_note;
  58. bool has_external_exciter = !performance_state->internal_exciter;
  59. if (has_external_note_cv) {
  60. performance_state->strum = note_changed;
  61. } else if (has_external_exciter) {
  62. performance_state->strum = has_onset;
  63. // Use longer inhibit time for onset detector.
  64. inhibit_timer *= 4;
  65. } else {
  66. // Nothing is connected. Should the module play itself in this case?
  67. performance_state->strum = false;
  68. }
  69. }
  70. if (inhibit_counter_) {
  71. --inhibit_counter_;
  72. performance_state->strum = false;
  73. } else {
  74. if (performance_state->strum) {
  75. inhibit_counter_ = inhibit_timer;
  76. }
  77. }
  78. previous_note_ = performance_state->note;
  79. }
  80. private:
  81. float previous_note_;
  82. int32_t inhibit_counter_;
  83. int32_t inhibit_timer_;
  84. OnsetDetector onset_detector_;
  85. DISALLOW_COPY_AND_ASSIGN(Strummer);
  86. };
  87. } // namespace rings
  88. #endif // RINGS_DSP_STRUMMER_H_