1 // Copyright 2014 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 // Modal synthesis voice.
28 
29 #ifndef ELEMENTS_DSP_VOICE_H_
30 #define ELEMENTS_DSP_VOICE_H_
31 
32 #include "stmlib/stmlib.h"
33 
34 #include "stmlib/dsp/filter.h"
35 
36 #include "elements/dsp/dsp.h"
37 #include "elements/dsp/exciter.h"
38 #include "elements/dsp/multistage_envelope.h"
39 #include "elements/dsp/patch.h"
40 #include "elements/dsp/resonator.h"
41 #include "elements/dsp/tube.h"
42 
43 #include "elements/dsp/fx/diffuser.h"
44 
45 namespace elements {
46 
47 class Voice {
48  public:
Voice()49   Voice() { }
~Voice()50   ~Voice() { }
51 
52   void Init();
53   void Process(
54       const Patch& patch,
55       float frequency,
56       float strength,
57       const bool gate_in,
58       const float* blow_in,
59       const float* strike_in,
60       float* raw,
61       float* center,
62       float* sides,
63       size_t size);
64   // For metering.
exciter_level()65   inline float exciter_level() const { return exciter_level_; }
Panic()66   void Panic() {
67     ResetResonator();
68   }
69 
70  private:
71   void ResetResonator();
GetGateFlags(bool gate_in)72   inline uint8_t GetGateFlags(bool gate_in) {
73     uint8_t flags = 0;
74     if (gate_in) {
75       if (!previous_gate_) {
76         flags |= EXCITER_FLAG_RISING_EDGE;
77       }
78       flags |= EXCITER_FLAG_GATE;
79     } else if (previous_gate_) {
80       flags = EXCITER_FLAG_FALLING_EDGE;
81     }
82     previous_gate_ = gate_in;
83     return flags;
84   }
85 
86   MultistageEnvelope envelope_;
87   Tube tube_;
88   Exciter bow_;
89   Exciter blow_;
90   Exciter strike_;
91   Diffuser diffuser_;
92   Resonator resonator_;
93 
94   float strength_;
95   float envelope_value_;
96 
97   float exciter_level_;
98 
99   float bow_buffer_[kMaxBlockSize];
100   float bow_strength_buffer_[kMaxBlockSize];
101   float blow_buffer_[kMaxBlockSize];
102   float strike_buffer_[kMaxBlockSize];
103   float external_buffer_[kMaxBlockSize];
104 
105   float diffuser_buffer_[1024];
106 
107   bool previous_gate_;
108 
109   DISALLOW_COPY_AND_ASSIGN(Voice);
110 };
111 
112 }  // namespace elements
113 
114 #endif  // ELEMENTS_DSP_VOICE_H_
115