1 /*
2 * Carla Plugin Host
3 * Copyright (C) 2011-2021 Filipe Coelho <falktx@falktx.com>
4 *
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.
9 *
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.
14 *
15 * For a full copy of the GNU General Public License see the doc/GPL.txt file.
16 */
17
18 #include "CarlaEngine.hpp"
19 #include "CarlaMathUtils.hpp"
20 #include "CarlaMIDI.h"
21
22 CARLA_BACKEND_START_NAMESPACE
23
24 // -----------------------------------------------------------------------
25 // EngineControlEvent
26
convertToMidiData(const uint8_t channel,uint8_t data[3]) const27 uint8_t EngineControlEvent::convertToMidiData(const uint8_t channel, uint8_t data[3]) const noexcept
28 {
29 switch (type)
30 {
31 case kEngineControlEventTypeNull:
32 break;
33
34 case kEngineControlEventTypeParameter:
35 CARLA_SAFE_ASSERT_RETURN(param < MAX_MIDI_VALUE, 0);
36
37 data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE | (channel & MIDI_CHANNEL_BIT));
38
39 if (MIDI_IS_CONTROL_BANK_SELECT(param))
40 {
41 data[1] = MIDI_CONTROL_BANK_SELECT;
42 if (midiValue >= 0)
43 data[2] = uint8_t(midiValue);
44 else
45 data[2] = uint8_t(carla_fixedValue<float>(0.0f, static_cast<float>(MAX_MIDI_VALUE-1), normalizedValue));
46 }
47 else
48 {
49 data[1] = static_cast<uint8_t>(param);
50 if (midiValue >= 0)
51 data[2] = uint8_t(midiValue);
52 else
53 data[2] = uint8_t(carla_fixedValue<float>(0.0f, 1.0f, normalizedValue) * static_cast<float>(MAX_MIDI_VALUE-1));
54 }
55 return 3;
56
57 case kEngineControlEventTypeMidiBank:
58 data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE | (channel & MIDI_CHANNEL_BIT));
59 data[1] = MIDI_CONTROL_BANK_SELECT;
60 data[2] = uint8_t(carla_fixedValue<uint16_t>(0, MAX_MIDI_VALUE-1, param));
61 return 3;
62
63 case kEngineControlEventTypeMidiProgram:
64 data[0] = static_cast<uint8_t>(MIDI_STATUS_PROGRAM_CHANGE | (channel & MIDI_CHANNEL_BIT));
65 data[1] = uint8_t(carla_fixedValue<uint16_t>(0, MAX_MIDI_VALUE-1, param));
66 return 2;
67
68 case kEngineControlEventTypeAllSoundOff:
69 data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE | (channel & MIDI_CHANNEL_BIT));
70 data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
71 return 2;
72
73 case kEngineControlEventTypeAllNotesOff:
74 data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE | (channel & MIDI_CHANNEL_BIT));
75 data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
76 return 2;
77 }
78
79 return 0;
80 }
81
82 // -----------------------------------------------------------------------
83 // EngineEvent
84
fillFromMidiData(const uint8_t size,const uint8_t * const data,const uint8_t midiPortOffset)85 void EngineEvent::fillFromMidiData(const uint8_t size, const uint8_t* const data, const uint8_t midiPortOffset) noexcept
86 {
87 if (size == 0 || data == nullptr || data[0] < MIDI_STATUS_NOTE_OFF)
88 {
89 type = kEngineEventTypeNull;
90 channel = 0;
91 return;
92 }
93
94 // get channel
95 channel = uint8_t(MIDI_GET_CHANNEL_FROM_DATA(data));
96
97 // get status
98 const uint8_t midiStatus(uint8_t(MIDI_GET_STATUS_FROM_DATA(data)));
99
100 if (midiStatus == MIDI_STATUS_CONTROL_CHANGE)
101 {
102 CARLA_SAFE_ASSERT_RETURN(size >= 2,);
103
104 type = kEngineEventTypeControl;
105
106 const uint8_t midiControl(data[1]);
107
108 if (MIDI_IS_CONTROL_BANK_SELECT(midiControl))
109 {
110 CARLA_SAFE_ASSERT_RETURN(size >= 3,);
111
112 const uint8_t midiBank(data[2]);
113
114 ctrl.type = kEngineControlEventTypeMidiBank;
115 ctrl.param = midiBank;
116 ctrl.midiValue = -1;
117 ctrl.normalizedValue = 0.0f;
118 ctrl.handled = true;
119 }
120 else if (midiControl == MIDI_CONTROL_ALL_SOUND_OFF)
121 {
122 ctrl.type = kEngineControlEventTypeAllSoundOff;
123 ctrl.param = 0;
124 ctrl.midiValue = -1;
125 ctrl.normalizedValue = 0.0f;
126 ctrl.handled = true;
127 }
128 else if (midiControl == MIDI_CONTROL_ALL_NOTES_OFF)
129 {
130 ctrl.type = kEngineControlEventTypeAllNotesOff;
131 ctrl.param = 0;
132 ctrl.midiValue = -1;
133 ctrl.normalizedValue = 0.0f;
134 ctrl.handled = true;
135 }
136 else
137 {
138 CARLA_SAFE_ASSERT_RETURN(size >= 3,);
139
140 // ensures 0.0<->1.0 value range
141 const int8_t midiValue = static_cast<int8_t>(carla_fixedValue<uint8_t>(0, 127, data[2]));
142
143 ctrl.type = kEngineControlEventTypeParameter;
144 ctrl.param = midiControl;
145 ctrl.midiValue = midiValue;
146 ctrl.normalizedValue = float(midiValue)/127.0f;
147 ctrl.handled = false;
148 }
149 }
150 else if (midiStatus == MIDI_STATUS_PROGRAM_CHANGE)
151 {
152 CARLA_SAFE_ASSERT_RETURN(size >= 2,);
153
154 type = kEngineEventTypeControl;
155
156 const uint8_t midiProgram(data[1]);
157
158 ctrl.type = kEngineControlEventTypeMidiProgram;
159 ctrl.param = midiProgram;
160 ctrl.midiValue = -1;
161 ctrl.normalizedValue = 0.0f;
162 ctrl.handled = true;
163 }
164 else
165 {
166 type = kEngineEventTypeMidi;
167
168 midi.port = midiPortOffset;
169 midi.size = size;
170
171 if (size > EngineMidiEvent::kDataSize)
172 {
173 midi.dataExt = data;
174 std::memset(midi.data, 0, sizeof(uint8_t)*EngineMidiEvent::kDataSize);
175 }
176 else
177 {
178 midi.data[0] = midiStatus;
179
180 uint8_t i=1;
181 for (; i < size; ++i)
182 midi.data[i] = data[i];
183 for (; i < EngineMidiEvent::kDataSize; ++i)
184 midi.data[i] = 0;
185
186 midi.dataExt = nullptr;
187 }
188 }
189 }
190
191 // -----------------------------------------------------------------------
192 // EngineOptions
193
EngineOptions()194 EngineOptions::EngineOptions() noexcept
195 #ifdef CARLA_OS_LINUX
196 : processMode(ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS),
197 transportMode(ENGINE_TRANSPORT_MODE_JACK),
198 #else
199 : processMode(ENGINE_PROCESS_MODE_PATCHBAY),
200 transportMode(ENGINE_TRANSPORT_MODE_INTERNAL),
201 #endif
202 transportExtra(nullptr),
203 forceStereo(false),
204 resetXruns(false),
205 preferPluginBridges(false),
206 #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
207 preferUiBridges(false),
208 #else
209 preferUiBridges(true),
210 #endif
211 uisAlwaysOnTop(true),
212 pluginsAreStandalone(false),
213 bgColor(0x000000ff),
214 fgColor(0xffffffff),
215 uiScale(1.0f),
216 maxParameters(MAX_DEFAULT_PARAMETERS),
217 uiBridgesTimeout(4000),
218 audioBufferSize(512),
219 audioSampleRate(44100),
220 audioTripleBuffer(false),
221 audioDriver(nullptr),
222 audioDevice(nullptr),
223 #ifndef BUILD_BRIDGE
224 # ifdef CARLA_OS_WIN
225 oscEnabled(false),
226 # else
227 oscEnabled(true),
228 # endif
229 oscPortTCP(22752),
230 oscPortUDP(22752),
231 #endif
232 pathAudio(nullptr),
233 pathMIDI(nullptr),
234 pathLADSPA(nullptr),
235 pathDSSI(nullptr),
236 pathLV2(nullptr),
237 pathVST2(nullptr),
238 pathVST3(nullptr),
239 pathSF2(nullptr),
240 pathSFZ(nullptr),
241 binaryDir(nullptr),
242 resourceDir(nullptr),
243 clientNamePrefix(nullptr),
244 preventBadBehaviour(false),
245 frontendWinId(0)
246 #ifndef CARLA_OS_WIN
247 , wine()
248 #endif
249 {
250 }
251
~EngineOptions()252 EngineOptions::~EngineOptions() noexcept
253 {
254 if (audioDriver != nullptr)
255 {
256 delete[] audioDriver;
257 audioDriver = nullptr;
258 }
259 if (audioDevice != nullptr)
260 {
261 delete[] audioDevice;
262 audioDevice = nullptr;
263 }
264 if (pathAudio != nullptr)
265 {
266 delete[] pathAudio;
267 pathAudio = nullptr;
268 }
269 if (pathMIDI != nullptr)
270 {
271 delete[] pathMIDI;
272 pathMIDI = nullptr;
273 }
274 if (pathLADSPA != nullptr)
275 {
276 delete[] pathLADSPA;
277 pathLADSPA = nullptr;
278 }
279 if (pathDSSI != nullptr)
280 {
281 delete[] pathDSSI;
282 pathDSSI = nullptr;
283 }
284 if (pathLV2 != nullptr)
285 {
286 delete[] pathLV2;
287 pathLV2 = nullptr;
288 }
289 if (pathVST2 != nullptr)
290 {
291 delete[] pathVST2;
292 pathVST2 = nullptr;
293 }
294 if (pathVST3 != nullptr)
295 {
296 delete[] pathVST3;
297 pathVST3 = nullptr;
298 }
299 if (pathSF2 != nullptr)
300 {
301 delete[] pathSF2;
302 pathSF2 = nullptr;
303 }
304 if (pathSFZ != nullptr)
305 {
306 delete[] pathSFZ;
307 pathSFZ = nullptr;
308 }
309 if (binaryDir != nullptr)
310 {
311 delete[] binaryDir;
312 binaryDir = nullptr;
313 }
314 if (resourceDir != nullptr)
315 {
316 delete[] resourceDir;
317 resourceDir = nullptr;
318 }
319 if (clientNamePrefix != nullptr)
320 {
321 delete[] clientNamePrefix;
322 clientNamePrefix = nullptr;
323 }
324 }
325
326 #ifndef CARLA_OS_WIN
Wine()327 EngineOptions::Wine::Wine() noexcept
328 : executable(nullptr),
329 autoPrefix(true),
330 fallbackPrefix(nullptr),
331 rtPrio(true),
332 baseRtPrio(15),
333 serverRtPrio(10) {}
334
~Wine()335 EngineOptions::Wine::~Wine() noexcept
336 {
337 if (executable != nullptr)
338 {
339 delete[] executable;
340 executable = nullptr;
341 }
342
343 if (fallbackPrefix != nullptr)
344 {
345 delete[] fallbackPrefix;
346 fallbackPrefix = nullptr;
347 }
348 }
349 #endif
350
351 // -----------------------------------------------------------------------
352 // EngineTimeInfoBBT
353
EngineTimeInfoBBT()354 EngineTimeInfoBBT::EngineTimeInfoBBT() noexcept
355 : valid(false),
356 bar(0),
357 beat(0),
358 tick(0.0),
359 barStartTick(0.0),
360 beatsPerBar(0.0f),
361 beatType(0.0f),
362 ticksPerBeat(0.0),
363 beatsPerMinute(0.0) {}
364
EngineTimeInfoBBT(const EngineTimeInfoBBT & bbt)365 EngineTimeInfoBBT::EngineTimeInfoBBT(const EngineTimeInfoBBT& bbt) noexcept
366 : valid(bbt.valid),
367 bar(bbt.bar),
368 beat(bbt.beat),
369 tick(bbt.tick),
370 barStartTick(bbt.barStartTick),
371 beatsPerBar(bbt.beatsPerBar),
372 beatType(bbt.beatType),
373 ticksPerBeat(bbt.ticksPerBeat),
374 beatsPerMinute(bbt.beatsPerMinute) {}
375
clear()376 void EngineTimeInfoBBT::clear() noexcept
377 {
378 valid = false;
379 bar = 0;
380 beat = 0;
381 tick = 0.0;
382 barStartTick = 0.0;
383 beatsPerBar = 0.0f;
384 beatType = 0.0f;
385 ticksPerBeat = 0.0;
386 beatsPerMinute = 0.0;
387 }
388
389 // -----------------------------------------------------------------------
390 // EngineTimeInfo
391
EngineTimeInfo()392 EngineTimeInfo::EngineTimeInfo() noexcept
393 : playing(false),
394 frame(0),
395 usecs(0),
396 bbt() {}
397
clear()398 void EngineTimeInfo::clear() noexcept
399 {
400 playing = false;
401 frame = 0;
402 usecs = 0;
403 bbt.clear();
404 }
405
EngineTimeInfo(const EngineTimeInfo & info)406 EngineTimeInfo::EngineTimeInfo(const EngineTimeInfo& info) noexcept
407 : playing(info.playing),
408 frame(info.frame),
409 usecs(info.usecs),
410 bbt(info.bbt) {}
411
operator =(const EngineTimeInfo & info)412 EngineTimeInfo& EngineTimeInfo::operator=(const EngineTimeInfo& info) noexcept
413 {
414 playing = info.playing;
415 frame = info.frame;
416 usecs = info.usecs;
417 bbt.valid = info.bbt.valid;
418 bbt.bar = info.bbt.bar;
419 bbt.beat = info.bbt.beat;
420 bbt.tick = info.bbt.tick;
421 bbt.barStartTick = info.bbt.barStartTick;
422 bbt.beatsPerBar = info.bbt.beatsPerBar;
423 bbt.beatType = info.bbt.beatType;
424 bbt.ticksPerBeat = info.bbt.ticksPerBeat;
425 bbt.beatsPerMinute = info.bbt.beatsPerMinute;
426
427 return *this;
428 }
429
compareIgnoringRollingFrames(const EngineTimeInfo & timeInfo,const uint32_t maxFrames) const430 bool EngineTimeInfo::compareIgnoringRollingFrames(const EngineTimeInfo& timeInfo, const uint32_t maxFrames) const noexcept
431 {
432 if (timeInfo.playing != playing || timeInfo.bbt.valid != bbt.valid)
433 return false;
434
435 if (bbt.valid)
436 {
437 if (carla_isNotEqual(timeInfo.bbt.beatsPerBar, bbt.beatsPerBar))
438 return false;
439 if (carla_isNotEqual(timeInfo.bbt.beatsPerMinute, bbt.beatsPerMinute))
440 return false;
441 }
442
443 // frame matches, nothing else to compare
444 if (timeInfo.frame == frame)
445 return true;
446
447 // if we went back in time, so a case of reposition
448 if (frame > timeInfo.frame)
449 return false;
450
451 // not playing, so don't bother checking transport
452 // assume frame changed, likely playback has stopped
453 if (! playing)
454 return false;
455
456 // if we are within expected bounds, assume we are rolling normally
457 if (frame + maxFrames <= timeInfo.frame)
458 return true;
459
460 // out of bounds, another reposition
461 return false;
462 }
463
operator ==(const EngineTimeInfo & timeInfo) const464 bool EngineTimeInfo::operator==(const EngineTimeInfo& timeInfo) const noexcept
465 {
466 if (timeInfo.playing != playing || timeInfo.frame != frame || timeInfo.bbt.valid != bbt.valid)
467 return false;
468 if (! bbt.valid)
469 return true;
470 if (carla_isNotEqual(timeInfo.bbt.beatsPerBar, bbt.beatsPerBar))
471 return false;
472 if (carla_isNotEqual(timeInfo.bbt.beatsPerMinute, bbt.beatsPerMinute))
473 return false;
474 return true;
475 }
476
operator !=(const EngineTimeInfo & timeInfo) const477 bool EngineTimeInfo::operator!=(const EngineTimeInfo& timeInfo) const noexcept
478 {
479 return !operator==(timeInfo);
480 }
481
482 // -----------------------------------------------------------------------
483
484 CARLA_BACKEND_END_NAMESPACE
485