-
-
Notifications
You must be signed in to change notification settings - Fork 144
/
CarlaPlugin.cpp
2395 lines (1899 loc) · 77.8 KB
/
CarlaPlugin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Carla Plugin
* Copyright (C) 2011-2017 Filipe Coelho <falktx@falktx.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* For a full copy of the GNU General Public License see the doc/GPL.txt file.
*/
#include "CarlaPluginInternal.hpp"
#include "CarlaEngine.hpp"
#include "CarlaBackendUtils.hpp"
#include "CarlaBase64Utils.hpp"
#include "CarlaMathUtils.hpp"
#include "CarlaPluginUI.hpp"
#include "CarlaStringList.hpp"
#include <ctime>
#include "water/files/File.h"
#include "water/streams/MemoryOutputStream.h"
#include "water/xml/XmlDocument.h"
#include "water/xml/XmlElement.h"
using water::CharPointer_UTF8;
using water::File;
using water::MemoryOutputStream;
using water::Result;
using water::String;
using water::XmlDocument;
using water::XmlElement;
CARLA_BACKEND_START_NAMESPACE
// -------------------------------------------------------------------
// Fallback data
static const ParameterData kParameterDataNull = { PARAMETER_UNKNOWN, 0x0, PARAMETER_NULL, -1, -1, 0 };
static const ParameterRanges kParameterRangesNull = { 0.0f, 0.0f, 1.0f, 0.01f, 0.0001f, 0.1f };
static const MidiProgramData kMidiProgramDataNull = { 0, 0, nullptr };
static const CustomData kCustomDataFallback = { nullptr, nullptr, nullptr };
static /* */ CustomData kCustomDataFallbackNC = { nullptr, nullptr, nullptr };
static const PluginPostRtEvent kPluginPostRtEventFallback = { kPluginPostRtEventNull, 0, 0, 0.0f };
// -------------------------------------------------------------------
// ParamSymbol struct, needed for CarlaPlugin::loadStateSave()
struct ParamSymbol {
int32_t index;
const char* symbol;
ParamSymbol(const uint32_t i, const char* const s)
: index(static_cast<int32_t>(i)),
symbol(carla_strdup(s)) {}
~ParamSymbol() noexcept
{
CARLA_SAFE_ASSERT_RETURN(symbol != nullptr,)
delete[] symbol;
symbol = nullptr;
}
#ifdef CARLA_PROPER_CPP11_SUPPORT
ParamSymbol() = delete;
CARLA_DECLARE_NON_COPY_STRUCT(ParamSymbol)
#endif
};
// -------------------------------------------------------------------
// Constructor and destructor
CarlaPlugin::CarlaPlugin(CarlaEngine* const engine, const uint id)
: pData(new ProtectedData(engine, id))
{
CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
CARLA_SAFE_ASSERT(id < engine->getMaxPluginNumber());
carla_debug("CarlaPlugin::CarlaPlugin(%p, %i)", engine, id);
switch (engine->getProccessMode())
{
case ENGINE_PROCESS_MODE_SINGLE_CLIENT:
case ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS:
CARLA_SAFE_ASSERT(id < MAX_DEFAULT_PLUGINS);
break;
case ENGINE_PROCESS_MODE_CONTINUOUS_RACK:
CARLA_SAFE_ASSERT(id < MAX_RACK_PLUGINS);
break;
case ENGINE_PROCESS_MODE_PATCHBAY:
CARLA_SAFE_ASSERT(id < MAX_PATCHBAY_PLUGINS);
break;
case ENGINE_PROCESS_MODE_BRIDGE:
CARLA_SAFE_ASSERT(id == 0);
break;
}
}
CarlaPlugin::~CarlaPlugin()
{
carla_debug("CarlaPlugin::~CarlaPlugin()");
delete pData;
}
// -------------------------------------------------------------------
// Information (base)
uint CarlaPlugin::getId() const noexcept
{
return pData->id;
}
uint CarlaPlugin::getHints() const noexcept
{
return pData->hints;
}
uint CarlaPlugin::getOptionsEnabled() const noexcept
{
return pData->options;
}
bool CarlaPlugin::isEnabled() const noexcept
{
return pData->enabled;
}
const char* CarlaPlugin::getName() const noexcept
{
return pData->name;
}
const char* CarlaPlugin::getFilename() const noexcept
{
return pData->filename;
}
const char* CarlaPlugin::getIconName() const noexcept
{
return pData->iconName;
}
PluginCategory CarlaPlugin::getCategory() const noexcept
{
return getPluginCategoryFromName(pData->name);
}
int64_t CarlaPlugin::getUniqueId() const noexcept
{
return 0;
}
uint32_t CarlaPlugin::getLatencyInFrames() const noexcept
{
return 0;
}
// -------------------------------------------------------------------
// Information (count)
uint32_t CarlaPlugin::getAudioInCount() const noexcept
{
return pData->audioIn.count;
}
uint32_t CarlaPlugin::getAudioOutCount() const noexcept
{
return pData->audioOut.count;
}
uint32_t CarlaPlugin::getCVInCount() const noexcept
{
return pData->cvIn.count;
}
uint32_t CarlaPlugin::getCVOutCount() const noexcept
{
return pData->cvOut.count;
}
uint32_t CarlaPlugin::getMidiInCount() const noexcept
{
return (pData->extraHints & PLUGIN_EXTRA_HINT_HAS_MIDI_IN) ? 1 : 0;
}
uint32_t CarlaPlugin::getMidiOutCount() const noexcept
{
return (pData->extraHints & PLUGIN_EXTRA_HINT_HAS_MIDI_OUT) ? 1 : 0;
}
uint32_t CarlaPlugin::getParameterCount() const noexcept
{
return pData->param.count;
}
uint32_t CarlaPlugin::getParameterScalePointCount(const uint32_t parameterId) const noexcept
{
CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0);
return 0;
}
uint32_t CarlaPlugin::getProgramCount() const noexcept
{
return pData->prog.count;
}
uint32_t CarlaPlugin::getMidiProgramCount() const noexcept
{
return pData->midiprog.count;
}
uint32_t CarlaPlugin::getCustomDataCount() const noexcept
{
return static_cast<uint32_t>(pData->custom.count());
}
// -------------------------------------------------------------------
// Information (current data)
int32_t CarlaPlugin::getCurrentProgram() const noexcept
{
return pData->prog.current;
}
int32_t CarlaPlugin::getCurrentMidiProgram() const noexcept
{
return pData->midiprog.current;
}
const ParameterData& CarlaPlugin::getParameterData(const uint32_t parameterId) const noexcept
{
CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, kParameterDataNull);
return pData->param.data[parameterId];
}
const ParameterRanges& CarlaPlugin::getParameterRanges(const uint32_t parameterId) const noexcept
{
CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, kParameterRangesNull);
return pData->param.ranges[parameterId];
}
bool CarlaPlugin::isParameterOutput(const uint32_t parameterId) const noexcept
{
CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
return (pData->param.data[parameterId].type == PARAMETER_OUTPUT);
}
const MidiProgramData& CarlaPlugin::getMidiProgramData(const uint32_t index) const noexcept
{
CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count, kMidiProgramDataNull);
return pData->midiprog.data[index];
}
const CustomData& CarlaPlugin::getCustomData(const uint32_t index) const noexcept
{
return pData->custom.getAt(index, kCustomDataFallback);
}
std::size_t CarlaPlugin::getChunkData(void** const dataPtr) noexcept
{
CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr, 0);
CARLA_SAFE_ASSERT(false); // this should never happen
return 0;
}
// -------------------------------------------------------------------
// Information (per-plugin data)
uint CarlaPlugin::getOptionsAvailable() const noexcept
{
CARLA_SAFE_ASSERT(false); // this should never happen
return 0x0;
}
float CarlaPlugin::getParameterValue(const uint32_t parameterId) const noexcept
{
CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), 0.0f);
CARLA_SAFE_ASSERT(false); // this should never happen
return 0.0f;
}
float CarlaPlugin::getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const noexcept
{
CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), 0.0f);
CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId), 0.0f);
CARLA_SAFE_ASSERT(false); // this should never happen
return 0.0f;
}
void CarlaPlugin::getLabel(char* const strBuf) const noexcept
{
strBuf[0] = '\0';
}
void CarlaPlugin::getMaker(char* const strBuf) const noexcept
{
strBuf[0] = '\0';
}
void CarlaPlugin::getCopyright(char* const strBuf) const noexcept
{
strBuf[0] = '\0';
}
void CarlaPlugin::getRealName(char* const strBuf) const noexcept
{
strBuf[0] = '\0';
}
void CarlaPlugin::getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept
{
CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
CARLA_SAFE_ASSERT(false); // this should never happen
strBuf[0] = '\0';
}
void CarlaPlugin::getParameterSymbol(const uint32_t parameterId, char* const strBuf) const noexcept
{
CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
strBuf[0] = '\0';
}
void CarlaPlugin::getParameterText(const uint32_t parameterId, char* const strBuf) noexcept
{
CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
CARLA_SAFE_ASSERT(false); // this should never happen
strBuf[0] = '\0';
}
void CarlaPlugin::getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept
{
CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
strBuf[0] = '\0';
}
void CarlaPlugin::getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const noexcept
{
CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId),);
CARLA_SAFE_ASSERT(false); // this should never happen
strBuf[0] = '\0';
}
float CarlaPlugin::getInternalParameterValue(const int32_t parameterId) const noexcept
{
#ifndef BUILD_BRIDGE
CARLA_SAFE_ASSERT_RETURN(parameterId != PARAMETER_NULL && parameterId > PARAMETER_MAX, 0.0f);
switch (parameterId)
{
case PARAMETER_ACTIVE:
return pData->active;
case PARAMETER_CTRL_CHANNEL:
return pData->ctrlChannel;
case PARAMETER_DRYWET:
return pData->postProc.dryWet;
case PARAMETER_VOLUME:
return pData->postProc.volume;
case PARAMETER_BALANCE_LEFT:
return pData->postProc.balanceLeft;
case PARAMETER_BALANCE_RIGHT:
return pData->postProc.balanceRight;
case PARAMETER_PANNING:
return pData->postProc.panning;
};
#endif
CARLA_SAFE_ASSERT_RETURN(parameterId >= 0, 0.0f);
return getParameterValue(static_cast<uint32_t>(parameterId));
}
void CarlaPlugin::getProgramName(const uint32_t index, char* const strBuf) const noexcept
{
CARLA_SAFE_ASSERT_RETURN(index < pData->prog.count,);
CARLA_SAFE_ASSERT_RETURN(pData->prog.names[index] != nullptr,);
std::strncpy(strBuf, pData->prog.names[index], STR_MAX);
}
void CarlaPlugin::getMidiProgramName(const uint32_t index, char* const strBuf) const noexcept
{
CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
CARLA_SAFE_ASSERT_RETURN(pData->midiprog.data[index].name != nullptr,);
std::strncpy(strBuf, pData->midiprog.data[index].name, STR_MAX);
}
void CarlaPlugin::getParameterCountInfo(uint32_t& ins, uint32_t& outs) const noexcept
{
ins = 0;
outs = 0;
for (uint32_t i=0; i < pData->param.count; ++i)
{
if (pData->param.data[i].type == PARAMETER_INPUT)
++ins;
else if (pData->param.data[i].type == PARAMETER_OUTPUT)
++outs;
}
}
// -------------------------------------------------------------------
// Set data (state)
void CarlaPlugin::prepareForSave()
{
}
void CarlaPlugin::resetParameters() noexcept
{
for (uint i=0; i < pData->param.count; ++i)
{
const ParameterData& paramData(pData->param.data[i]);
const ParameterRanges& paramRanges(pData->param.ranges[i]);
if (paramData.type != PARAMETER_INPUT)
continue;
if ((paramData.hints & PARAMETER_IS_ENABLED) == 0)
continue;
setParameterValue(i, paramRanges.def, true, true, true);
}
}
void CarlaPlugin::randomizeParameters() noexcept
{
float value, random;
char strBuf[STR_MAX+1];
strBuf[STR_MAX] = '\0';
std::srand(static_cast<uint>(std::time(nullptr)));
for (uint i=0; i < pData->param.count; ++i)
{
const ParameterData& paramData(pData->param.data[i]);
if (paramData.type != PARAMETER_INPUT)
continue;
if ((paramData.hints & PARAMETER_IS_ENABLED) == 0)
continue;
getParameterName(i, strBuf);
if (std::strstr(strBuf, "olume") != nullptr)
continue;
if (std::strstr(strBuf, "Master") != nullptr)
continue;
const ParameterRanges& paramRanges(pData->param.ranges[i]);
if (paramData.hints & PARAMETER_IS_BOOLEAN)
{
random = static_cast<float>(std::rand()) / static_cast<float>(RAND_MAX);
value = random > 0.5 ? paramRanges.max : paramRanges.min;
}
else
{
random = static_cast<float>(std::rand()) / static_cast<float>(RAND_MAX);
value = random * (paramRanges.max - paramRanges.min) + paramRanges.min;
if (paramData.hints & PARAMETER_IS_INTEGER)
value = std::rint(value);
}
setParameterValue(i, value, true, true, true);
}
}
const CarlaStateSave& CarlaPlugin::getStateSave(const bool callPrepareForSave)
{
if (callPrepareForSave)
prepareForSave();
pData->stateSave.clear();
const PluginType pluginType(getType());
char strBuf[STR_MAX+1];
// ---------------------------------------------------------------
// Basic info
getLabel(strBuf);
pData->stateSave.type = carla_strdup(getPluginTypeAsString(getType()));
pData->stateSave.name = carla_strdup(pData->name);
pData->stateSave.label = carla_strdup(strBuf);
pData->stateSave.uniqueId = getUniqueId();
#ifndef BUILD_BRIDGE
pData->stateSave.options = pData->options;
#endif
if (pData->filename != nullptr)
pData->stateSave.binary = carla_strdup(pData->filename);
#ifndef BUILD_BRIDGE
// ---------------------------------------------------------------
// Internals
pData->stateSave.active = pData->active;
pData->stateSave.dryWet = pData->postProc.dryWet;
pData->stateSave.volume = pData->postProc.volume;
pData->stateSave.balanceLeft = pData->postProc.balanceLeft;
pData->stateSave.balanceRight = pData->postProc.balanceRight;
pData->stateSave.panning = pData->postProc.panning;
pData->stateSave.ctrlChannel = pData->ctrlChannel;
#endif
bool usingChunk = false;
// ---------------------------------------------------------------
// Chunk
if (pData->options & PLUGIN_OPTION_USE_CHUNKS)
{
void* data = nullptr;
const std::size_t dataSize(getChunkData(&data));
if (data != nullptr && dataSize > 0)
{
pData->stateSave.chunk = CarlaString::asBase64(data, dataSize).dup();
if (pluginType != PLUGIN_INTERNAL)
usingChunk = true;
}
}
// ---------------------------------------------------------------
// Current Program
if (pData->prog.current >= 0 && pluginType != PLUGIN_LV2 && pluginType != PLUGIN_GIG)
{
pData->stateSave.currentProgramIndex = pData->prog.current;
pData->stateSave.currentProgramName = carla_strdup(pData->prog.names[pData->prog.current]);
}
// ---------------------------------------------------------------
// Current MIDI Program
if (pData->midiprog.current >= 0 && pluginType != PLUGIN_LV2 && pluginType != PLUGIN_SF2)
{
const MidiProgramData& mpData(pData->midiprog.getCurrent());
pData->stateSave.currentMidiBank = static_cast<int32_t>(mpData.bank);
pData->stateSave.currentMidiProgram = static_cast<int32_t>(mpData.program);
}
// ---------------------------------------------------------------
// Parameters
const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
for (uint32_t i=0; i < pData->param.count; ++i)
{
const ParameterData& paramData(pData->param.data[i]);
if ((paramData.hints & PARAMETER_IS_ENABLED) == 0)
continue;
const bool dummy = paramData.type != PARAMETER_INPUT || usingChunk;
if (dummy && paramData.midiCC <= -1)
continue;
CarlaStateSave::Parameter* const stateParameter(new CarlaStateSave::Parameter());
stateParameter->dummy = dummy;
stateParameter->index = paramData.index;
#ifndef BUILD_BRIDGE
stateParameter->midiCC = paramData.midiCC;
stateParameter->midiChannel = paramData.midiChannel;
#endif
getParameterName(i, strBuf);
stateParameter->name = carla_strdup(strBuf);
getParameterSymbol(i, strBuf);
stateParameter->symbol = carla_strdup(strBuf);;
if (! dummy)
{
stateParameter->value = getParameterValue(i);
if (paramData.hints & PARAMETER_USES_SAMPLERATE)
stateParameter->value /= sampleRate;
}
pData->stateSave.parameters.append(stateParameter);
}
// ---------------------------------------------------------------
// Custom Data
for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
{
const CustomData& cData(it.getValue(kCustomDataFallback));
CARLA_SAFE_ASSERT_CONTINUE(cData.isValid());
CarlaStateSave::CustomData* stateCustomData(new CarlaStateSave::CustomData());
stateCustomData->type = carla_strdup(cData.type);
stateCustomData->key = carla_strdup(cData.key);
stateCustomData->value = carla_strdup(cData.value);
pData->stateSave.customData.append(stateCustomData);
}
return pData->stateSave;
}
void CarlaPlugin::loadStateSave(const CarlaStateSave& stateSave)
{
char strBuf[STR_MAX+1];
const bool usesMultiProgs(pData->hints & PLUGIN_USES_MULTI_PROGS);
const PluginType pluginType(getType());
// ---------------------------------------------------------------
// Part 1 - PRE-set custom data (only those which reload programs)
for (CarlaStateSave::CustomDataItenerator it = stateSave.customData.begin2(); it.valid(); it.next())
{
const CarlaStateSave::CustomData* const stateCustomData(it.getValue(nullptr));
CARLA_SAFE_ASSERT_CONTINUE(stateCustomData != nullptr);
CARLA_SAFE_ASSERT_CONTINUE(stateCustomData->isValid());
const char* const key(stateCustomData->key);
/**/ if (pluginType == PLUGIN_DSSI && (std::strcmp (key, "reloadprograms") == 0 ||
std::strcmp (key, "load" ) == 0 ||
std::strncmp(key, "patches", 7) == 0 ))
pass();
else if (usesMultiProgs && std::strcmp(key, "midiPrograms") == 0)
pass();
else
continue;
setCustomData(stateCustomData->type, key, stateCustomData->value, true);
}
// ---------------------------------------------------------------
// Part 2 - set program
if (stateSave.currentProgramIndex >= 0 && stateSave.currentProgramName != nullptr)
{
int32_t programId = -1;
// index < count
if (stateSave.currentProgramIndex < static_cast<int32_t>(pData->prog.count))
{
programId = stateSave.currentProgramIndex;
}
// index not valid, try to find by name
else
{
for (uint32_t i=0; i < pData->prog.count; ++i)
{
strBuf[0] = '\0';
getProgramName(i, strBuf);
if (strBuf[0] != '\0' && std::strcmp(stateSave.currentProgramName, strBuf) == 0)
{
programId = static_cast<int32_t>(i);
break;
}
}
}
// set program now, if valid
if (programId >= 0)
setProgram(programId, true, true, true);
}
// ---------------------------------------------------------------
// Part 3 - set midi program
if (stateSave.currentMidiBank >= 0 && stateSave.currentMidiProgram >= 0 && ! usesMultiProgs)
setMidiProgramById(static_cast<uint32_t>(stateSave.currentMidiBank), static_cast<uint32_t>(stateSave.currentMidiProgram), true, true, true);
// ---------------------------------------------------------------
// Part 4a - get plugin parameter symbols
LinkedList<ParamSymbol*> paramSymbols;
if (pluginType == PLUGIN_LADSPA || pluginType == PLUGIN_LV2)
{
for (uint32_t i=0; i < pData->param.count; ++i)
{
strBuf[0] = '\0';
getParameterSymbol(i, strBuf);
if (strBuf[0] != '\0')
{
ParamSymbol* const paramSymbol(new ParamSymbol(i, strBuf));
paramSymbols.append(paramSymbol);
}
}
}
// ---------------------------------------------------------------
// Part 4b - set parameter values (carefully)
const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
for (CarlaStateSave::ParameterItenerator it = stateSave.parameters.begin2(); it.valid(); it.next())
{
CarlaStateSave::Parameter* const stateParameter(it.getValue(nullptr));
CARLA_SAFE_ASSERT_CONTINUE(stateParameter != nullptr);
int32_t index = -1;
if (pluginType == PLUGIN_LADSPA)
{
// Try to set by symbol, otherwise use index
if (stateParameter->symbol != nullptr && stateParameter->symbol[0] != '\0')
{
for (LinkedList<ParamSymbol*>::Itenerator it2 = paramSymbols.begin2(); it2.valid(); it2.next())
{
ParamSymbol* const paramSymbol(it2.getValue(nullptr));
CARLA_SAFE_ASSERT_CONTINUE(paramSymbol != nullptr);
CARLA_SAFE_ASSERT_CONTINUE(paramSymbol->symbol != nullptr);
if (std::strcmp(stateParameter->symbol, paramSymbol->symbol) == 0)
{
index = paramSymbol->index;
break;
}
}
if (index == -1)
index = stateParameter->index;
}
else
index = stateParameter->index;
}
else if (pluginType == PLUGIN_LV2)
{
// Symbol only
if (stateParameter->symbol != nullptr && stateParameter->symbol[0] != '\0')
{
for (LinkedList<ParamSymbol*>::Itenerator it2 = paramSymbols.begin2(); it2.valid(); it2.next())
{
ParamSymbol* const paramSymbol(it2.getValue(nullptr));
CARLA_SAFE_ASSERT_CONTINUE(paramSymbol != nullptr);
CARLA_SAFE_ASSERT_CONTINUE(paramSymbol->symbol != nullptr);
if (std::strcmp(stateParameter->symbol, paramSymbol->symbol) == 0)
{
index = paramSymbol->index;
break;
}
}
if (index == -1)
carla_stderr("Failed to find LV2 parameter symbol '%s')", stateParameter->symbol);
}
else
carla_stderr("LV2 Plugin parameter '%s' has no symbol", stateParameter->name);
}
else
{
// Index only
index = stateParameter->index;
}
// Now set parameter
if (index >= 0 && index < static_cast<int32_t>(pData->param.count))
{
//CARLA_SAFE_ASSERT(stateParameter->isInput == (pData
if (! stateParameter->dummy)
{
if (pData->param.data[index].hints & PARAMETER_USES_SAMPLERATE)
stateParameter->value *= sampleRate;
setParameterValue(static_cast<uint32_t>(index), stateParameter->value, true, true, true);
}
#ifndef BUILD_BRIDGE
setParameterMidiCC(static_cast<uint32_t>(index), stateParameter->midiCC, true, true);
setParameterMidiChannel(static_cast<uint32_t>(index), stateParameter->midiChannel, true, true);
#endif
}
else
carla_stderr("Could not set parameter data for '%s'", stateParameter->name);
}
// ---------------------------------------------------------------
// Part 4c - clear
for (LinkedList<ParamSymbol*>::Itenerator it = paramSymbols.begin2(); it.valid(); it.next())
{
ParamSymbol* const paramSymbol(it.getValue(nullptr));
delete paramSymbol;
}
paramSymbols.clear();
// ---------------------------------------------------------------
// Part 5 - set custom data
for (CarlaStateSave::CustomDataItenerator it = stateSave.customData.begin2(); it.valid(); it.next())
{
const CarlaStateSave::CustomData* const stateCustomData(it.getValue(nullptr));
CARLA_SAFE_ASSERT_CONTINUE(stateCustomData != nullptr);
CARLA_SAFE_ASSERT_CONTINUE(stateCustomData->isValid());
const char* const key(stateCustomData->key);
if (pluginType == PLUGIN_DSSI && (std::strcmp (key, "reloadprograms") == 0 ||
std::strcmp (key, "load" ) == 0 ||
std::strncmp(key, "patches", 7) == 0 ))
continue;
if (usesMultiProgs && std::strcmp(key, "midiPrograms") == 0)
continue;
setCustomData(stateCustomData->type, key, stateCustomData->value, true);
}
// ---------------------------------------------------------------
// Part 5x - set lv2 state
if (pluginType == PLUGIN_LV2 && pData->custom.count() > 0)
setCustomData(CUSTOM_DATA_TYPE_STRING, "CarlaLoadLv2StateNow", "true", true);
// ---------------------------------------------------------------
// Part 6 - set chunk
if (stateSave.chunk != nullptr && (pData->options & PLUGIN_OPTION_USE_CHUNKS) != 0)
{
std::vector<uint8_t> chunk(carla_getChunkFromBase64String(stateSave.chunk));
#ifdef CARLA_PROPER_CPP11_SUPPORT
setChunkData(chunk.data(), chunk.size());
#else
setChunkData(&chunk.front(), chunk.size());
#endif
}
#ifndef BUILD_BRIDGE
// ---------------------------------------------------------------
// Part 6 - set internal stuff
const uint availOptions(getOptionsAvailable());
for (uint i=0; i<10; ++i) // FIXME - get this value somehow...
{
const uint option(1u << i);
if (availOptions & option)
setOption(option, (stateSave.options & option) != 0, true);
}
setDryWet(stateSave.dryWet, true, true);
setVolume(stateSave.volume, true, true);
setBalanceLeft(stateSave.balanceLeft, true, true);
setBalanceRight(stateSave.balanceRight, true, true);
setPanning(stateSave.panning, true, true);
setCtrlChannel(stateSave.ctrlChannel, true, true);
setActive(stateSave.active, true, true);
#endif
pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, 0, 0, 0.0f, nullptr);
}
bool CarlaPlugin::saveStateToFile(const char* const filename)
{
CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
carla_debug("CarlaPlugin::saveStateToFile(\"%s\")", filename);
MemoryOutputStream out, streamState;
getStateSave().dumpToMemoryStream(streamState);
out << "<?xml version='1.0' encoding='UTF-8'?>\n";
out << "<!DOCTYPE CARLA-PRESET>\n";
out << "<CARLA-PRESET VERSION='2.0'>\n";
out << streamState;
out << "</CARLA-PRESET>\n";
const String jfilename = String(CharPointer_UTF8(filename));
File file(jfilename);
if (file.replaceWithData(out.getData(), out.getDataSize()))
return true;
pData->engine->setLastError("Failed to write file");
return false;
}
bool CarlaPlugin::loadStateFromFile(const char* const filename)
{
// TODO set errors
CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
carla_debug("CarlaPlugin::loadStateFromFile(\"%s\")", filename);
const String jfilename = String(CharPointer_UTF8(filename));
File file(jfilename);
CARLA_SAFE_ASSERT_RETURN(file.existsAsFile(), false);
XmlDocument xml(file);
ScopedPointer<XmlElement> xmlElement(xml.getDocumentElement(true));
CARLA_SAFE_ASSERT_RETURN(xmlElement != nullptr, false);
CARLA_SAFE_ASSERT_RETURN(xmlElement->getTagName().equalsIgnoreCase("carla-preset"), false);
// completely load file
xmlElement = xml.getDocumentElement(false);
CARLA_SAFE_ASSERT_RETURN(xmlElement != nullptr, false);
if (pData->stateSave.fillFromXmlElement(xmlElement))
{
loadStateSave(pData->stateSave);
return true;
}
return false;
}
bool CarlaPlugin::exportAsLV2(const char* const lv2path)
{
CARLA_SAFE_ASSERT_RETURN(lv2path != nullptr && lv2path[0] != '\0', false);
carla_debug("CarlaPlugin::exportAsLV2(\"%s\")", lv2path);
CarlaString bundlepath(lv2path);
if (! bundlepath.endsWith(".lv2"))
bundlepath += ".lv2";
const File bundlefolder(bundlepath.buffer());
if (bundlefolder.existsAsFile())
{
pData->engine->setLastError("Requested filename already exists as file, use a folder instead");
return false;
}
if (! bundlefolder.exists())
{
const Result res(bundlefolder.createDirectory());
if (res.failed())
{
pData->engine->setLastError(res.getErrorMessage().toRawUTF8());
return false;
}
}
CarlaString symbol(pData->name);
symbol.toBasic();
{
const CarlaString pluginFilename(bundlepath + CARLA_OS_SEP_STR + symbol + ".xml");
if (! saveStateToFile(pluginFilename))
return false;
}
{
MemoryOutputStream manifestStream;
manifestStream << "@prefix lv2: <http://lv2plug.in/ns/lv2core#> .\n";
manifestStream << "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n";
manifestStream << "@prefix ui: <http://lv2plug.in/ns/extensions/ui#> .\n";
manifestStream << "\n";
manifestStream << "<" << symbol.buffer() << ".ttl>\n";
manifestStream << " a lv2:Plugin ;\n";
manifestStream << " lv2:binary <" << symbol.buffer() << CARLA_LIB_EXT "> ;\n";
manifestStream << " rdfs:seeAlso <" << symbol.buffer() << ".ttl> .\n";
manifestStream << "\n";
manifestStream << "<ext-ui>\n";
manifestStream << " a <http://kxstudio.sf.net/ns/lv2ext/external-ui#Widget> ;\n";
manifestStream << " ui:binary <" << symbol.buffer() << CARLA_LIB_EXT "> ;\n";
manifestStream << " lv2:extensionData <http://lv2plug.in/ns/extensions/ui#idleInterface> ,\n";
manifestStream << " <http://lv2plug.in/ns/extensions/ui#showInterface> ;\n";
manifestStream << " lv2:requiredFeature <http://lv2plug.in/ns/ext/instance-access> .\n";
manifestStream << "\n";
const CarlaString manifestFilename(bundlepath + CARLA_OS_SEP_STR "manifest.ttl");
const File manifestFile(manifestFilename.buffer());
if (! manifestFile.replaceWithData(manifestStream.getData(), manifestStream.getDataSize()))
{
pData->engine->setLastError("Failed to write manifest.ttl file");
return false;
}
}
{
MemoryOutputStream mainStream;
mainStream << "@prefix atom: <http://lv2plug.in/ns/ext/atom#> .\n";