1 // Copyright 2013 Emilie Gillet.
2 //
3 // Author: Emilie Gillet (emilie.o.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 
29 #ifndef PEAKS_DRUMS_SNARE_DRUM_H_
30 #define PEAKS_DRUMS_SNARE_DRUM_H_
31 
32 #include "stmlib/stmlib.h"
33 
34 #include "peaks/drums/svf.h"
35 #include "peaks/drums/excitation.h"
36 
37 #include <cstdio>
38 
39 #include "peaks/gate_processor.h"
40 
41 namespace peaks {
42 
43 class SnareDrum {
44  public:
SnareDrum()45   SnareDrum() { }
~SnareDrum()46   ~SnareDrum() { }
47 
48   void Init();
49   void Process(const GateFlags* gate_flags, int16_t* out, size_t size);
50 
Configure(uint16_t * parameter,ControlMode control_mode)51   void Configure(uint16_t* parameter, ControlMode control_mode) {
52     if (control_mode == CONTROL_MODE_HALF) {
53       set_frequency(0);
54       set_decay(32768);
55       set_tone(parameter[0]);
56       set_snappy(parameter[1]);
57     } else {
58       set_frequency(parameter[0] - 32768);
59       set_tone(parameter[1]);
60       set_snappy(parameter[2]);
61       set_decay(parameter[3]);
62     }
63   }
64 
set_tone(uint16_t tone)65   void set_tone(uint16_t tone) {
66     gain_1_ = 22000 - (tone >> 2);
67     gain_2_ = 22000 + (tone >> 2);
68   }
69 
set_snappy(uint16_t snappy)70   void set_snappy(uint16_t snappy) {
71     snappy >>= 1;
72     if (snappy >= 28672) {
73       snappy = 28672;
74     }
75     snappy_ = 512 + snappy;
76   }
77 
set_decay(uint16_t decay)78   void set_decay(uint16_t decay) {
79     body_1_.set_resonance(29000 + (decay >> 5));
80     body_2_.set_resonance(26500 + (decay >> 5));
81     excitation_noise_.set_decay(4092 + (decay >> 14));
82   }
83 
set_frequency(int16_t frequency)84   void set_frequency(int16_t frequency) {
85     int16_t base_note = 52 << 7;
86     int32_t transposition = frequency;
87     base_note += transposition * 896 >> 15;
88     body_1_.set_frequency(base_note);
89     body_2_.set_frequency(base_note + (12 << 7));
90     noise_.set_frequency(base_note + (48 << 7));
91   }
92 
93  private:
94   Excitation excitation_1_up_;
95   Excitation excitation_1_down_;
96   Excitation excitation_2_;
97   Excitation excitation_noise_;
98   Svf body_1_;
99   Svf body_2_;
100   Svf noise_;
101 
102   int32_t gain_1_;
103   int32_t gain_2_;
104 
105   uint16_t snappy_;
106 
107   DISALLOW_COPY_AND_ASSIGN(SnareDrum);
108 };
109 
110 }  // namespace peaks
111 
112 #endif  // PEAKS_DRUMS_SNARE_DRUM_H_
113