3 * Copyright (C) 2012-2019 Filipe Coelho <falktx@falktx.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * For a full copy of the GNU General Public License see the doc/GPL.txt file.
18 #include "CarlaNative.h"
20 #include "CarlaDefines.h"
21 #include "CarlaMIDI.h"
26 // -----------------------------------------------------------------------
38 const NativeHostDescriptor
* host
;
46 // -----------------------------------------------------------------------
48 static NativePluginHandle
lfo_instantiate(const NativeHostDescriptor
* host
)
50 LfoHandle
* const handle
= (LfoHandle
*)malloc(sizeof(LfoHandle
));
58 handle
->multiplier
= 1.0f
;
59 handle
->baseStart
= 0.0f
;
64 #define handlePtr ((LfoHandle*)handle)
66 static void lfo_cleanup(NativePluginHandle handle
)
71 static uint32_t lfo_get_parameter_count(NativePluginHandle handle
)
79 static const NativeParameter
* lfo_get_parameter_info(NativePluginHandle handle
, uint32_t index
)
81 if (index
> PARAM_COUNT
)
84 static NativeParameter param
;
85 static NativeParameterScalePoint paramModes
[5];
87 param
.hints
= NATIVE_PARAMETER_IS_ENABLED
|NATIVE_PARAMETER_IS_AUTOMATABLE
;
88 param
.scalePointCount
= 0;
89 param
.scalePoints
= NULL
;
91 paramModes
[0].label
= "Triangle";
92 paramModes
[1].label
= "Sawtooth";
93 paramModes
[2].label
= "Sawtooth (inverted)";
94 paramModes
[3].label
= "Sine (TODO)";
95 paramModes
[4].label
= "Square";
97 paramModes
[0].value
= 1.0f
;
98 paramModes
[1].value
= 2.0f
;
99 paramModes
[2].value
= 3.0f
;
100 paramModes
[3].value
= 4.0f
;
101 paramModes
[4].value
= 5.0f
;
108 param
.hints
|= NATIVE_PARAMETER_IS_INTEGER
|NATIVE_PARAMETER_USES_SCALEPOINTS
;
109 param
.ranges
.def
= 1.0f
;
110 param
.ranges
.min
= 1.0f
;
111 param
.ranges
.max
= 5.0f
;
112 param
.ranges
.step
= 1.0f
;
113 param
.ranges
.stepSmall
= 1.0f
;
114 param
.ranges
.stepLarge
= 1.0f
;
115 param
.scalePointCount
= 5;
116 param
.scalePoints
= paramModes
;
119 param
.name
= "Speed";
120 param
.unit
= "(coef)";
121 param
.ranges
.def
= 1.0f
;
122 param
.ranges
.min
= 0.01f
;
123 param
.ranges
.max
= 2048.0f
;
124 param
.ranges
.step
= 0.25f
;
125 param
.ranges
.stepSmall
= 0.1f
;
126 param
.ranges
.stepLarge
= 0.5f
;
128 case PARAM_MULTIPLIER
:
129 param
.name
= "Multiplier";
130 param
.unit
= "(coef)";
131 param
.ranges
.def
= 1.0f
;
132 param
.ranges
.min
= 0.01f
;
133 param
.ranges
.max
= 2.0f
;
134 param
.ranges
.step
= 0.01f
;
135 param
.ranges
.stepSmall
= 0.0001f
;
136 param
.ranges
.stepLarge
= 0.1f
;
138 case PARAM_BASE_START
:
139 param
.name
= "Start value";
141 param
.ranges
.def
= 0.0f
;
142 param
.ranges
.min
= -1.0f
;
143 param
.ranges
.max
= 1.0f
;
144 param
.ranges
.step
= 0.01f
;
145 param
.ranges
.stepSmall
= 0.0001f
;
146 param
.ranges
.stepLarge
= 0.1f
;
149 param
.name
= "LFO Out";
151 param
.hints
|= NATIVE_PARAMETER_IS_OUTPUT
;
152 param
.ranges
.def
= 0.0f
;
153 param
.ranges
.min
= 0.0f
;
154 param
.ranges
.max
= 1.0f
;
155 param
.ranges
.step
= 0.01f
;
156 param
.ranges
.stepSmall
= 0.0001f
;
157 param
.ranges
.stepLarge
= 0.1f
;
167 static float lfo_get_parameter_value(NativePluginHandle handle
, uint32_t index
)
172 return (float)handlePtr
->mode
;
174 return (float)handlePtr
->speed
;
175 case PARAM_MULTIPLIER
:
176 return handlePtr
->multiplier
;
177 case PARAM_BASE_START
:
178 return handlePtr
->baseStart
;
180 return handlePtr
->value
;
186 static void lfo_set_parameter_value(NativePluginHandle handle
, uint32_t index
, float value
)
191 handlePtr
->mode
= (int)value
;
194 handlePtr
->speed
= value
;
196 case PARAM_MULTIPLIER
:
197 handlePtr
->multiplier
= value
;
199 case PARAM_BASE_START
:
200 handlePtr
->baseStart
= value
;
203 handlePtr
->value
= value
;
208 // FIXME for v3.0, use const for the input buffer
209 static void lfo_process(NativePluginHandle handle
,
210 float** inBuffer
, float** outBuffer
, uint32_t frames
,
211 const NativeMidiEvent
* midiEvents
, uint32_t midiEventCount
)
213 const NativeHostDescriptor
* const host
= handlePtr
->host
;
214 const NativeTimeInfo
* const timeInfo
= host
->get_time_info(host
->handle
);
216 if (! timeInfo
->playing
)
219 const double bpm
= timeInfo
->bbt
.valid
? timeInfo
->bbt
.beatsPerMinute
: 120.0;
220 const double sampleRate
= host
->get_sample_rate(host
->handle
);
222 const double speedRate
= handlePtr
->speed
/(bpm
/60.0/sampleRate
);
223 const uint speedRatei
= (uint
)speedRate
;
227 switch (handlePtr
->mode
)
230 value
= fabs(1.0-(double)(timeInfo
->frame
% speedRatei
)/(speedRate
/2.0));
233 value
= (double)(timeInfo
->frame
% speedRatei
)/speedRate
;
235 case 3: // Sawtooth (inverted)
236 value
= 1.0 - (double)(timeInfo
->frame
% speedRatei
)/speedRate
;
238 case 4: // Sine -- TODO!
242 value
= (timeInfo
->frame
% speedRatei
<= speedRatei
/2) ? 1.0 : 0.0;
246 value
*= (double)handlePtr
->multiplier
;
247 value
+= (double)handlePtr
->baseStart
;
250 handlePtr
->value
= 0.0f
;
251 else if (value
>= 1.0)
252 handlePtr
->value
= 1.0f
;
254 handlePtr
->value
= (float)value
;
263 (void)midiEventCount
;
268 // -----------------------------------------------------------------------
270 static const NativePluginDescriptor lfoDesc
= {
271 .category
= NATIVE_PLUGIN_CATEGORY_UTILITY
,
272 .hints
= NATIVE_PLUGIN_IS_RTSAFE
,
273 .supports
= NATIVE_PLUGIN_SUPPORTS_NOTHING
,
280 .paramIns
= PARAM_COUNT
-1,
285 .copyright
= "GNU GPL v2+",
287 .instantiate
= lfo_instantiate
,
288 .cleanup
= lfo_cleanup
,
290 .get_parameter_count
= lfo_get_parameter_count
,
291 .get_parameter_info
= lfo_get_parameter_info
,
292 .get_parameter_value
= lfo_get_parameter_value
,
294 .get_midi_program_count
= NULL
,
295 .get_midi_program_info
= NULL
,
297 .set_parameter_value
= lfo_set_parameter_value
,
298 .set_midi_program
= NULL
,
299 .set_custom_data
= NULL
,
304 .ui_set_parameter_value
= NULL
,
305 .ui_set_midi_program
= NULL
,
306 .ui_set_custom_data
= NULL
,
310 .process
= lfo_process
,
318 // -----------------------------------------------------------------------
320 void carla_register_native_plugin_lfo(void);
322 void carla_register_native_plugin_lfo(void)
324 carla_register_native_plugin(&lfoDesc
);
327 // -----------------------------------------------------------------------