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.

113 lines
3.1KB

  1. // Copyright 2013 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. // 808-style snare drum.
  28. #ifndef PEAKS_DRUMS_SNARE_DRUM_H_
  29. #define PEAKS_DRUMS_SNARE_DRUM_H_
  30. #include "stmlib/stmlib.h"
  31. #include "peaks/drums/svf.h"
  32. #include "peaks/drums/excitation.h"
  33. #include <cstdio>
  34. #include "peaks/gate_processor.h"
  35. namespace peaks {
  36. class SnareDrum {
  37. public:
  38. SnareDrum() { }
  39. ~SnareDrum() { }
  40. void Init();
  41. int16_t ProcessSingleSample(uint8_t control) IN_RAM;
  42. void Configure(uint16_t* parameter, ControlMode control_mode) {
  43. if (control_mode == CONTROL_MODE_HALF) {
  44. set_frequency(0);
  45. set_decay(32768);
  46. set_tone(parameter[0]);
  47. set_snappy(parameter[1]);
  48. } else {
  49. set_frequency(parameter[0] - 32768);
  50. set_tone(parameter[1]);
  51. set_snappy(parameter[2]);
  52. set_decay(parameter[3]);
  53. }
  54. }
  55. void set_tone(uint16_t tone) {
  56. gain_1_ = 22000 - (tone >> 2);
  57. gain_2_ = 22000 + (tone >> 2);
  58. }
  59. void set_snappy(uint16_t snappy) {
  60. snappy >>= 1;
  61. if (snappy >= 28672) {
  62. snappy = 28672;
  63. }
  64. snappy_ = 512 + snappy;
  65. }
  66. void set_decay(uint16_t decay) {
  67. body_1_.set_resonance(29000 + (decay >> 5));
  68. body_2_.set_resonance(26500 + (decay >> 5));
  69. excitation_noise_.set_decay(4092 + (decay >> 14));
  70. }
  71. void set_frequency(int16_t frequency) {
  72. int16_t base_note = 52 << 7;
  73. int32_t transposition = frequency;
  74. base_note += transposition * 896 >> 15;
  75. body_1_.set_frequency(base_note);
  76. body_2_.set_frequency(base_note + (12 << 7));
  77. noise_.set_frequency(base_note + (48 << 7));
  78. }
  79. private:
  80. Excitation excitation_1_up_;
  81. Excitation excitation_1_down_;
  82. Excitation excitation_2_;
  83. Excitation excitation_noise_;
  84. Svf body_1_;
  85. Svf body_2_;
  86. Svf noise_;
  87. int32_t gain_1_;
  88. int32_t gain_2_;
  89. uint16_t snappy_;
  90. DISALLOW_COPY_AND_ASSIGN(SnareDrum);
  91. };
  92. } // namespace peaks
  93. #endif // PEAKS_DRUMS_SNARE_DRUM_H_