1 /*
2  * MIDI PBToCC plugin based on DISTRHO Plugin Framework (DPF)
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Copyright (C) 2019 Christopher Arndt <info@chrisarndt.de>
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to
10  * deal in the Software without restriction, including without limitation the
11  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12  * sell copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24  * IN THE SOFTWARE.
25  */
26 
27 #ifndef PLUGIN_MIDIPBTOCC_H
28 #define PLUGIN_MIDIPBTOCC_H
29 
30 #include "DistrhoPlugin.hpp"
31 
32 START_NAMESPACE_DISTRHO
33 
34 #ifndef IN_RANGE
35 #define IN_RANGE(v, min, max) ((min) <= (max) ? ((v) >= (min) && (v) <= (max)) : ((v) >= (min) || (v) <= (max)))
36 #endif
37 
38 #ifndef MAP
39 #define MAP(v, imin, imax, omin, omax) (((v) - (imin)) * ((omax) - (omin)) / ((imax) - (imin)) + (omin))
40 #endif
41 
42 #ifndef MIN
43 #define MIN(a,b) ( (a) < (b) ? (a) : (b) )
44 #endif
45 
46 #ifndef MAX
47 #define MAX(a,b) ( (a) > (b) ? (a) : (b) )
48 #endif
49 
50 #ifndef CLAMP
51 #define CLAMP(v, min, max) (MIN((max), MAX((min), (v))))
52 #endif
53 
54 #define MIDI_CONTROL_CHANGE 0xB0
55 #define MIDI_PITCH_BEND 0xE0
56 
57 // -----------------------------------------------------------------------
58 
59 class PluginMIDIPBToCC : public Plugin {
60 public:
61     enum Parameters {
62         paramFilterChannel,
63         paramKeepOriginal,
64         paramPBMin,
65         paramPBMax,
66         paramCC1,
67         paramCC1Min,
68         paramCC1Max,
69         paramCC2,
70         paramCC2Min,
71         paramCC2Max,
72         paramCount
73     };
74 
75     PluginMIDIPBToCC();
76 
77 protected:
78     // -------------------------------------------------------------------
79     // Information
80 
getLabel() const81     const char* getLabel() const noexcept override {
82         return "MIDIPBToCC";
83     }
84 
getDescription() const85     const char* getDescription() const override {
86         return "Convert Pitch Bend into Control Change messages";
87     }
88 
getMaker() const89     const char* getMaker() const noexcept override {
90         return "chrisarndt.de";
91     }
92 
getHomePage() const93     const char* getHomePage() const override {
94         return "https://chrisarndt.de/plugins/midipbtocc";
95     }
96 
getLicense() const97     const char* getLicense() const noexcept override {
98         return "https://spdx.org/licenses/MIT";
99     }
100 
getVersion() const101     uint32_t getVersion() const noexcept override {
102         return d_version(1, 2, 2);
103     }
104 
105     // Go to:
106     //
107     // http://service.steinberg.de/databases/plugin.nsf/plugIn
108     //
109     // Get a proper plugin UID and fill it in here!
getUniqueId() const110     int64_t getUniqueId() const noexcept override {
111         return d_cconst('M', 'P', 'b', 'C');
112     }
113 
114     // -------------------------------------------------------------------
115     // Init
116 
117     void initParameter(uint32_t index, Parameter& parameter) override;
118     void initProgramName(uint32_t index, String& programName) override;
119 
120     // -------------------------------------------------------------------
121     // Internal data
122 
123     float getParameterValue(uint32_t index) const override;
124     void setParameterValue(uint32_t index, float value) override;
125     void loadProgram(uint32_t index) override;
126 
127     // -------------------------------------------------------------------
128     // Optional
129 
130     // Optional callback to inform the plugin about a sample rate change.
131     void sampleRateChanged(double newSampleRate) override;
132 
133     // -------------------------------------------------------------------
134     // Process
135 
136     void activate() override;
137 
138     void run(const float**, float**, uint32_t,
139              const MidiEvent* midiEvents, uint32_t midiEventCount) override;
140 
141 
142     // -------------------------------------------------------------------
143 
144 private:
145     float fParams[paramCount];
146     int8_t filterChannel;
147 
148     DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(PluginMIDIPBToCC)
149 };
150 
151 struct Preset {
152     const char* name;
153     const float params[PluginMIDIPBToCC::paramCount];
154 };
155 
156 const Preset factoryPresets[] = {
157     {
158         "Modulation +",
159         {0.0, 0.0, -8192.0, 8191.0, 1.0, 64.0, 127.0, 1.0, 63.0, 0.0}
160     },
161     {
162         "Modulation +/-",
163         {0.0, 0.0, -8192.0, 8191.0, 1.0, 0.0, 127.0, 1.0, 0.0, 127.0}
164     },
165     {
166         "PB + / Mod -",
167         {0.0, 0.0, -8192.0, -1, 1.0, 0.0, 127.0, 1.0, 0.0, 127.0}
168     }
169 };
170 
171 const uint presetCount = sizeof(factoryPresets) / sizeof(Preset);
172 
173 // -----------------------------------------------------------------------
174 
175 END_NAMESPACE_DISTRHO
176 
177 #endif  // #ifndef PLUGIN_MIDIPBTOCC_H
178