-
Notifications
You must be signed in to change notification settings - Fork 7
/
dfxplugin-rtas.cpp
1045 lines (892 loc) · 33 KB
/
dfxplugin-rtas.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
/*------------------------------------------------------------------------
Destroy FX Library is a collection of foundation code
for creating audio processing plug-ins.
Copyright (C) 2009-2023 Sophia Poirier
This file is part of the Destroy FX Library (version 1.0).
Destroy FX Library 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
(at your option) any later version.
Destroy FX Library 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.
You should have received a copy of the GNU General Public License
along with Destroy FX Library. If not, see <http:https://www.gnu.org/licenses/>.
To contact the author, use the contact form at http:https://destroyfx.org
Destroy FX is a sovereign entity comprised of Sophia Poirier and Tom Murphy 7.
This is our class for E-Z plugin-making and E-Z multiple-API support.
This is where we connect the RTAS/AudioSuite API to our DfxPlugin system.
------------------------------------------------------------------------*/
#include "dfxplugin.h"
#include <algorithm>
#include <cmath>
#include <cstring>
#include "CEffectTypeAS.h"
#include "CEffectTypeRTAS.h"
#include "CPluginControl_LinearBoostCut.h"
#include "CPluginControl_LinearGain.h"
#include "CPluginControl_LinearPercent.h"
#include "CPluginControl_LinearQ.h"
#include "CPluginControl_LinearThld.h"
#include "CPluginControl_LinearTime.h"
#include "CPluginControl_List.h"
#include "CPluginControl_LogFrequency.h"
#include "CPluginControl_LogGain.h"
#include "CPluginControl_LogPercent.h"
#include "CPluginControl_LogQ.h"
#include "CPluginControl_LogTime.h"
#include "CPluginControl_LogTimeS.h"
#include "CPluginControl_OnOff.h"
#include "dfxmath.h"
#include "dfxmisc.h"
//#include "FicPluginEnums.h"
#include "PlugInUtils.h"
#if !TARGET_PLUGIN_HAS_GUI
#include "CClippingView.h"
#include "CGainMeterView.h"
#include "CNoResourceView.h"
#include "CPictBackgroundTextEditor.h"
#include "CSliderControlEditor.h"
#include "CStateTextControlEditor.h"
#include "CStaticCustomText.h"
#include "CTextCustomPopupEditor.h"
#endif
//-----------------------------------------------------------------------------
static OSType DFX_IterateAlphaNumericFourCharCode(OSType inPreviousCode);
#pragma mark -
#pragma mark init
#pragma mark -
//-----------------------------------------------------------------------------
void DfxPlugin::EffectInit()
{
do_PostConstructor();
AddParametersToList();
if (IsAS())
{
mInputAudioStreams_as.assign(GetNumOutputs(), nullptr);
mOutputAudioStreams_as.assign(GetNumOutputs(), nullptr);
}
do_initialize();
/*
long bypassControlIndex = 0;
ComponentResult bypassResult = GetMasterBypassControl(&bypassControlIndex);
CPluginControl_Discrete* bypassControl = dynamic_cast<CPluginControl_Discrete*>( GetControl(bypassControlIndex) );
char bypassName[32] {};
bypassControl->GetNameOfLength(bypassName, std::size(bypassName) - 1, 0);
std::fprintf(stderr, "%s IsAutomatable() = %s (error = %ld)\n", bypassName, bypassControl->IsAutomatable() ? "true" : "false", bypassResult);
*/
}
//-----------------------------------------------------------------------------
void DfxPlugin::Free()
{
do_cleanup();
do_PreDestructor();
TARGET_API_BASE_CLASS::Free();
}
//-----------------------------------------------------------------------------
ComponentResult DfxPlugin::ResetPlugInState()
{
do_reset();
return noErr;
}
//-----------------------------------------------------------------------------
ComponentResult DfxPlugin::Prime(Boolean inPriming)
{
if (inPriming)
do_reset();
return noErr;
}
//-----------------------------------------------------------------------------
void DfxPlugin::DoTokenIdle()
{
CEffectProcess::DoTokenIdle();
#if TARGET_PLUGIN_HAS_GUI
if (mCustomUI_p)
{
// if ( !IsAS() || (IsAS() && (GetASPreviewState() == previewState_Off)) )
mCustomUI_p->Idle();
}
#endif
}
#pragma mark -
#pragma mark parameters
#pragma mark -
//-----------------------------------------------------------------------------
void DfxPlugin::AddParametersToList()
{
constexpr OSType globalBypassFourCharID = FOURCC('b', 'y', 'p', 'a');
AddControl(new CPluginControl_OnOff(globalBypassFourCharID, "Global Bypass\nG Bypass\nGByp\nByp", false, true));
DefineMasterBypassControlIndex(dfx::kParameterID_RTASGlobalBypass);
OSType paramFourCharID = 0;
for (dfx::ParameterID i = 0; i < getnumparameters(); i++)
{
if (!parameterisvalid(i))
{
continue; // XXX eh actually maybe we need to add a parameter for every index?
}
auto const paramMin_f = getparametermin_f(i), paramMax_f = getparametermax_f(i), paramDefault_f = getparameterdefault_f(i);
auto const paramMin_i = getparametermin_i(i), paramMax_i = getparametermax_i(i), paramDefault_i = getparameterdefault_i(i);
auto const paramName = getparametername(i);
paramFourCharID = DFX_IterateAlphaNumericFourCharCode(paramFourCharID);
// make sure to skip over the ID already used for global bypass
if (paramFourCharID == globalBypassFourCharID)
{
paramFourCharID = DFX_IterateAlphaNumericFourCharCode(paramFourCharID);
}
bool const paramAutomatable = !(hasparameterattribute(i, DfxParam::kAttribute_Hidden) || hasparameterattribute(i, DfxParam::kAttribute_Unused));
constexpr int numCurvedSteps = 1000;
double const stepSize_default = (paramMax_f - paramMin_f) / (double)numCurvedSteps;
auto const paramCurve = getparametercurve(i);
auto const paramCurveSpec = getparametercurvespec(i);
bool const paramIsLog = (paramCurve == DfxParam::Curve::Log) || (paramCurve == DfxParam::Curve::Exp);
if ( getparameterusevaluestrings(i) )
{
std::vector<std::string> valueStringsVector;
for (int64_t stringIndex=paramMin_i; stringIndex <= paramMax_i; stringIndex++)
{
if (auto const valueString = getparametervaluestring(i, stringIndex))
{
valueStringsVector.push_back(*valueString);
}
}
AddControl( new CPluginControl_List(paramFourCharID, paramName.c_str(), valueStringsVector, paramDefault_i - paramMin_i, paramAutomatable) );
}
else if (getparametervaluetype(i) == DfxParam::ValueType::Int)
{
AddControl( new CPluginControl_Discrete(paramFourCharID, paramName.c_str(), paramMin_i, paramMax_i, paramDefault_i, paramAutomatable) );
}
else if (getparametervaluetype(i) == DfxParam::ValueType::Boolean)
{
AddControl( new CPluginControl_OnOff(paramFourCharID, paramName.c_str(), getparameterdefault_b(i), paramAutomatable) );
}
else
{
switch ( getparameterunit(i) )
{
case DfxParam::Unit::Percent:
case DfxParam::Unit::DryWetMix:
if (paramIsLog)
AddControl( new CPluginControl_LogPercent(paramFourCharID, paramName.c_str(), paramMin_f * dfx::kRTASPercentScalar, paramMax_f * dfx::kRTASPercentScalar, numCurvedSteps, paramDefault_f * dfx::kRTASPercentScalar, paramAutomatable) );
else
AddControl( new CPluginControl_LinearPercent(paramFourCharID, paramName.c_str(), paramMin_f * dfx::kRTASPercentScalar, paramMax_f * dfx::kRTASPercentScalar, stepSize_default * dfx::kRTASPercentScalar, paramDefault_f * dfx::kRTASPercentScalar, paramAutomatable) );
break;
case DfxParam::Unit::MS:
if (paramIsLog)
AddControl( new CPluginControl_LogTime(paramFourCharID, paramName.c_str(), paramMin_f, paramMax_f, numCurvedSteps, paramDefault_f, paramAutomatable) );
else
AddControl( new CPluginControl_LinearTime(paramFourCharID, paramName.c_str(), paramMin_f, paramMax_f, stepSize_default, paramDefault_f, paramAutomatable) );
break;
case DfxParam::Unit::Seconds:
// XXX not actually really sure whether CPluginControl_TimeS is for seconds? or maybe samples?
AddControl( new CPluginControl_LogTimeS(paramFourCharID, paramName.c_str(), paramMin_f, paramMax_f, numCurvedSteps, paramDefault_f, paramAutomatable) );
break;
case DfxParam::Unit::Hz:
if (paramCurve == DfxParam::Curve::Log)
AddControl( new CPluginControl_LogFrequency(paramFourCharID, paramName.c_str(), paramMin_f, paramMax_f, numCurvedSteps, paramDefault_f, paramAutomatable) );
else
AddControl( new CPluginControl_DfxCurvedFrequency(paramFourCharID, paramName.c_str(), paramMin_f, paramMax_f, numCurvedSteps, paramDefault_f, paramAutomatable, paramCurve, paramCurveSpec) );
break;
case DfxParam::Unit::Decibles:
// XXX or perhaps use CPluginControl_Level, or split the two out from each other?
AddControl( new CPluginControl_LinearBoostCut(paramFourCharID, paramName.c_str(), paramMin_f, paramMax_f, stepSize_default, paramDefault_f, paramAutomatable) );
break;
case DfxParam::Unit::LinearGain:
if (paramIsLog)
AddControl( new CPluginControl_LogGain(paramFourCharID, paramName.c_str(), paramMin_f, paramMax_f, numCurvedSteps, paramDefault_f, paramAutomatable) );
else
AddControl( new CPluginControl_LinearGain(paramFourCharID, paramName.c_str(), paramMin_f, paramMax_f, stepSize_default, paramDefault_f, paramAutomatable) );
break;
default:
{
if (paramIsLog)
AddControl( new CPluginControl_Log(paramFourCharID, paramName.c_str(), paramMin_f, paramMax_f, numCurvedSteps, paramDefault_f, paramAutomatable) );
else
AddControl( new CPluginControl_Linear(paramFourCharID, paramName.c_str(), paramMin_f, paramMax_f, stepSize_default, paramDefault_f, paramAutomatable) );
}
break;
}
}
}
}
//-----------------------------------------------------------------------------
// XXX implement
ComponentResult DfxPlugin::GetControlNameOfLength(long inParameterIndex, char* outName, long inNameLength, OSType inControllerType, FicBoolean* outReverseHighlight)
{
if (outName == nullptr)
{
return paramErr;
}
/*
if (inNameLength <= dfx::kParameterShortNameMax_RTAS)
{
char* shortNameString = nullptr;
if (inParameterIndex == dfx::kParameterID_RTASGlobalBypass)
{
shortNameString = "Bypass"; // any shortened version of this is fine
}
else
{
shortNameString = GetParameterShortName( dfx::ParameterID_FromRTAS(inParameterIndex) );
}
if (shortNameString != nullptr)
{
strlcpy(outName, shortNameString, inNameLength + 1);
outName[inNameLength] = 0;
if (outReverseHighlight != nullptr)
{
*outReverseHighlight = false; // XXX assume control is not to be highlighted
}
return noErr;
}
}
*/
return TARGET_API_BASE_CLASS::GetControlNameOfLength(inParameterIndex, outName, inNameLength, inControllerType, outReverseHighlight);
}
//-----------------------------------------------------------------------------
// XXX implement
ComponentResult DfxPlugin::GetValueString(long inParameterIndex, long inValue, StringPtr outValueString, long inMaxLength)
{
if (outValueString == nullptr)
{
return paramErr;
}
/*
if (inMaxLength <= dfx::kParameterValueShortNameMax_rtas)
{
auto const shortValueString = GetParameterValueShortString(dfx::ParameterID_FromRTAS(inParameterIndex), inValue);
if (shortValueString != nullptr)
{
strlcpy((char*)(outValueString + 1), shortValueString, inMaxLength + 1);
outValueString[0] = (static_cast<long>(std::strlen(shortValueString)) > inMaxLength) ? inMaxLength : std::strlen(shortValueString);
return noErr;
}
}
*/
return TARGET_API_BASE_CLASS::GetValueString(inParameterIndex, inValue, outValueString, inMaxLength);
}
//-----------------------------------------------------------------------------
void DfxPlugin::UpdateControlValueInAlgorithm(long inParameterIndex)
{
if (inParameterIndex == dfx::kParameterID_RTASGlobalBypass)
{
mGlobalBypass_rtas = dynamic_cast<CPluginControl_Discrete*>(GetControl(inParameterIndex))->GetDiscrete();
return;
}
auto const parameterID = dfx::ParameterID_FromRTAS(inParameterIndex);
if (!parameterisvalid(parameterID))
{
return;
}
switch (getparametervaluetype(parameterID))
{
case DfxParam::ValueType::Float:
parameters[parameterID].set_f(GetParameter_f_FromRTAS(parameterID));
break;
case DfxParam::ValueType::Int:
parameters[parameterID].set_i(GetParameter_i_FromRTAS(parameterID));
break;
case DfxParam::ValueType::Boolean:
parameters[parameterID].set_b(GetParameter_b_FromRTAS(parameterID));
break;
default:
break;
}
}
//-----------------------------------------------------------------------------
double DfxPlugin::GetParameter_f_FromRTAS(dfx::ParameterID inParameterID)
{
double resultValue = dynamic_cast<CPluginControl_Continuous*>(GetControl(dfx::ParameterID_ToRTAS(inParameterID)))->GetContinuous();
if ( (getparameterunit(inParameterID) == DfxParam::Unit::Percent) || (getparameterunit(inParameterID) == DfxParam::Unit::DryWetMix) )
resultValue /= dfx::kRTASPercentScalar;
return resultValue;
}
//-----------------------------------------------------------------------------
int64_t DfxPlugin::GetParameter_i_FromRTAS(dfx::ParameterID inParameterID)
{
int64_t resultValue = dynamic_cast<CPluginControl_Discrete*>(GetControl(dfx::ParameterID_ToRTAS(inParameterID)))->GetDiscrete();
if ( getparameterusevaluestrings(inParameterID) )
resultValue += getparametermin_i(inParameterID);
return resultValue;
}
//-----------------------------------------------------------------------------
bool DfxPlugin::GetParameter_b_FromRTAS(dfx::ParameterID inParameterID)
{
return (GetParameter_i_FromRTAS(inParameterID) != 0);
}
//-----------------------------------------------------------------------------
ComponentResult DfxPlugin::IsControlAutomatable(long inControlIndex, short* outItIs)
{
if (outItIs == nullptr)
{
return paramErr;
}
// XXX test this first, since dfx::ParameterID_FromRTAS() makes it an invalid ID
if (inControlIndex == dfx::kParameterID_RTASGlobalBypass)
{
*outItIs = 1;
return noErr;
}
auto const parameterID = dfx::ParameterID_FromRTAS(inControlIndex);
if (!parameterisvalid(parameterID))
{
return paramErr;
}
if (hasparameterattribute(parameterID, DfxParam::kAttribute_Unused) || hasparameterattribute(parameterID, DfxParam::kAttribute_Hidden))
{
*outItIs = 0;
}
else
{
*outItIs = 1;
}
return noErr;
}
#ifndef TARGET_API_AUDIOSUITE
#include "SliderConversions.h"
#include "PlugInAssert.h"
//-----------------------------------------------------------------------------
CPluginControl_DfxCurved::CPluginControl_DfxCurved(OSType id, char const* name, double min, double max,
int numSteps, double defaultValue, bool isAutomatable,
DfxParam::Curve inCurve, double inCurveSpec,
PrefixDictionaryEntry const* begin, PrefixDictionaryEntry const* end)
: CPluginControl_Continuous(id, name, min, max, defaultValue, isAutomatable, begin, end),
mCurve(inCurve), mCurveSpec(inCurveSpec), mNumSteps(numSteps)
{
PIASSERT(numSteps != 0); // mNumSteps==0 can cause problems with devide-by-zero in PT
SetValue( GetDefaultValue() );
}
//-----------------------------------------------------------------------------
long CPluginControl_DfxCurved::GetNumSteps() const
{
return mNumSteps;
}
//-----------------------------------------------------------------------------
long CPluginControl_DfxCurved::ConvertContinuousToControl(double continuous) const
{
continuous = DfxParam::contract(continuous, GetMin(), GetMax(), mCurve, mCurveSpec);
return DoubleToLongControl(continuous, 0.0, 1.0);
}
//-----------------------------------------------------------------------------
double CPluginControl_DfxCurved::ConvertControlToContinuous(long control) const
{
double continous = LongControlToDouble(control, 0.0, 1.0);
return DfxParam::expand(continous, GetMin(), GetMax(), mCurve, mCurveSpec);
}
#endif // !TARGET_API_AUDIOSUITE
#pragma mark -
#pragma mark state
#pragma mark -
//-----------------------------------------------------------------------------
ComponentResult DfxPlugin::SetChunk(OSType inChunkID, SFicPlugInChunk* chunk)
{
return TARGET_API_BASE_CLASS::SetChunk(inChunkID, chunk);
}
#pragma mark -
#pragma mark dsp
#pragma mark -
#ifdef TARGET_API_AUDIOSUITE
//-----------------------------------------------------------------------------
UInt32 DfxPlugin::ProcessAudio(bool inIsGlobalBypassed)
{
if (!IsAS())
{
return 0;
}
long totalInputSamples = 0; // total number of input samples in one input buffer
// use the mono channel input sample number (guaranteed to be connected)
if (GetInputConnection(0) != nullptr)
{
totalInputSamples = GetInputConnection(0)->mNumSamplesInBuf;
}
for (SInt32 ch = 0; ch < GetNumOutputs(); ch++)
{
// XXX what to do if there are no valid connections for the channel?
mInputAudioStreams_as[ch] = nullptr;
mOutputAudioStreams_as[ch] = nullptr;
DAEConnectionPtr outputConnection = GetOutputConnection(ch);
if (outputConnection != nullptr) // if no valid connection, don't do anything
{
mOutputAudioStreams_as[ch] = (float*)(outputConnection->mBuffer);
DAEConnectionPtr inputConnection = GetInputConnection(ch);
if (inputConnection != nullptr) // have a valid input connection
{
mInputAudioStreams_as[ch] = (float*)(inputConnection->mBuffer);
}
else // no input connection; use default value of zero
{
// (re)allocate the zero audio buffer if it is not currently large enough for this rendering slice
if (std::ssize(mZeroAudioBuffer) < totalInputSamples)
{
mZeroAudioBuffer.assign(totalInputSamples, 0.0f);
}
mInputAudioStreams_as[ch] = mZeroAudioBuffer.data();
}
if (inIsGlobalBypassed)
{
for (long i = 0; i < totalInputSamples; i++)
{
mOutputAudioStreams_as[ch][i] = mInputAudioStreams_as[ch][i];
}
}
// do the sample number adjustment
outputConnection->mNumSamplesInBuf = totalInputSamples;
if (inputConnection != nullptr)
{
inputConnection->mNumSamplesInBuf = 0;
}
}
}
if (!inIsGlobalBypassed)
{
RenderAudio(mInputAudioStreams_as.data(), mOutputAudioStreams_as.data(), totalInputSamples);
}
// Get the current number of samples analyzed and pass this info
// back to the DAE application so it knows how much we've processed.
// This is a global position keeper, it should be incremented by the
// number of samples processed on a single track, not the total processed
// by all tracks.
return totalInputSamples;
}
#endif
//-----------------------------------------------------------------------------
void DfxPlugin::RenderAudio(float** inAudioStreams, float** outAudioStreams, long inNumFramesToProcess)
{
preprocessaudio(dfx::math::ToUnsigned(inNumFramesToProcess));
// RTAS clip monitoring
for (SInt32 channel = 0; (channel < GetNumInputs()) && !fClipped; channel++)
{
mInputAudioStreams[channel] = nullptr;
if (!inAudioStreams[channel]) // XXX possible in AudioSuite
{
continue;
}
for (long samp = 0; (samp < inNumFramesToProcess) && !fClipped; samp++)
{
if (std::fabs(inAudioStreams[channel][samp]) > 1.0f)
{
fClipped = true;
break;
}
}
if (mInPlaceAudioProcessingAllowed)
{
mInputAudioStreams[channel] = inAudioStreams[channel];
}
else
{
std::copy_n(inAudioStreams[channel], dfx::math::ToUnsigned(inNumFramesToProcess), mInputOutOfPlaceAudioBuffers[channel].data());
}
}
for (SInt32 channel = 0; channel < GetNumOutputs(); channel++)
{
if (!inAudioStreams[channel] || !outAudioStreams[channel]) // XXX possible in AudioSuite
{
continue;
}
if (mGlobalBypass_rtas)
{
SInt32 const inputChannelIndex = (GetNumInputs() < GetNumOutputs()) ? (GetNumInputs() - 1) : channel;
std::copy_n(mInputAudioStreams[inputChannelIndex], dfx::math::ToUnsigned(inNumFramesToProcess), outAudioStreams[channel]);
}
else
{
if (!mInPlaceAudioProcessingAllowed)
{
std::fill_n(outAudioStreams[channel], dfx::math::ToUnsigned(inNumFramesToProcess), 0.0f);
}
#if TARGET_PLUGIN_USES_DSPCORE
if (mDSPCores[channel])
{
std::span inputAudio(mInputAudioStreams[channel], dfx::math::ToUnsigned(inNumFramesToProcess));
if (asymmetricalchannels())
{
if (channel == 0)
{
std::copy_n(mInputAudioStreams[channel], sampleFrames, mAsymmetricalInputAudioBuffer.data());
}
inputAudio = std::span(mAsymmetricalInputAudioBuffer).subspan(0, dfx::math::ToUnsigned(inNumFramesToProcess));
}
mDSPCores[channel]->process(inputAudio, {outAudioStreams[channel], dfx::math::ToUnsigned(inNumFramesToProcess)});
}
#endif
}
}
#if !TARGET_PLUGIN_USES_DSPCORE
if (!mGlobalBypass_rtas)
{
processaudio(const_cast<float const* const*>(mInputAudioStreams.data()), const_cast<float* const*>(outAudioStreams), dfx::math::ToUnsigned(inNumFramesToProcess));
}
#endif
// RTAS clip monitoring
for (SInt32 channel = 0; (channel < GetNumOutputs()) && !fClipped; channel++)
{
if (outAudioStreams[channel] == nullptr) // XXX possible in AudioSuite
{
continue;
}
for (long samp = 0; samp < inNumFramesToProcess; samp++)
{
if (std::fabs(outAudioStreams[channel][samp]) > 1.0f)
{
fClipped = true;
break;
}
}
}
postprocessaudio();
}
#pragma mark -
#pragma mark MIDI
#pragma mark -
#if TARGET_PLUGIN_USES_MIDI
//-----------------------------------------------------------------------------------------
#endif
// TARGET_PLUGIN_USES_MIDI
#pragma mark -
#pragma mark GUI
#pragma mark -
//-----------------------------------------------------------------------------
CPlugInView* DfxPlugin::CreateCPlugInView()
{
#if TARGET_PLUGIN_HAS_GUI
std::unique_ptr<CNoResourceView> ui;
try
{
if (!mCustomUI_p)
{
mCustomUI_p.reset(CreateCTemplateCustomUI(this));
mCustomUI_p->GetRect( &(mPIWinRect.left), &(mPIWinRect.top), &(mPIWinRect.right), &(mPIWinRect.bottom) );
}
ui = std::make_unique<CNoResourceView>();
#if PT_SDK_VERSION < 0x08000000
constexpr long viewSizeOffset = 1; // XXX +1 because of a bug where CNoResourceView::SetSize() subtracts 1 from width and height
#else
constexpr long viewSizeOffset = 0; // the bug is fixed in the Pro Tools 8 SDK
#endif
ui->SetSize(mPIWinRect.right+viewSizeOffset, mPIWinRect.bottom+viewSizeOffset);
mNoUIView_p = (CTemplateNoUIView*) ui->AddView2("!NoUIView[('NoID')]", 0, 0, mPIWinRect.right, mPIWinRect.bottom, false);
if (mNoUIView_p != nullptr)
{
mNoUIView_p->SetCustomUI(mCustomUI_p.get());
}
}
catch (...)
{
mCustomUI_p.reset();
ui.reset();
}
return ui.release();
#else
OSType meterFourCharID = 0;
OSType meterIDs[EffectLayerDef::MAX_NUM_CONNECTIONS] {};
for (UInt32 i = 0; i < EffectLayerDef::MAX_NUM_CONNECTIONS; i++)
{
meterFourCharID = DFX_IterateAlphaNumericFourCharCode(meterFourCharID);
meterIDs[i] = meterFourCharID;
}
OSType clipIDs[EffectLayerDef::MAX_NUM_CONNECTIONS] {};
for (UInt32 i = 0; i < EffectLayerDef::MAX_NUM_CONNECTIONS; i++)
{
meterFourCharID = DFX_IterateAlphaNumericFourCharCode(meterFourCharID);
clipIDs[i] = meterFourCharID;
}
return CreateMinimalGUI(this, meterIDs, clipIDs);
#endif
}
#if TARGET_PLUGIN_HAS_GUI
//-----------------------------------------------------------------------------
void DfxPlugin::GetViewRect(Rect* outViewRect)
{
if (!mCustomUI_p)
{
mCustomUI_p.reset(CreateCTemplateCustomUI(this));
}
if (outViewRect == nullptr)
{
return;
}
mCustomUI_p->GetRect( &(mPIWinRect.left), &(mPIWinRect.top), &(mPIWinRect.right), &(mPIWinRect.bottom) );
outViewRect->left = mPIWinRect.left;
outViewRect->top = mPIWinRect.top;
outViewRect->right = mPIWinRect.right;
outViewRect->bottom = mPIWinRect.bottom;
}
//-----------------------------------------------------------------------------
void DfxPlugin::SetViewPort(GrafPtr inPort)
{
mMainPort = inPort;
CEffectProcess::SetViewPort(mMainPort);
if (mMainPort != nullptr)
{
if (mCustomUI_p)
{
#if TARGET_OS_WIN32
Rect aRect;
GetPortBounds(inPort, &aRect);
if ( ((aRect.left <= 0) && (aRect.top <= 0)) && ((mLeftOffset == 0) && (mTopOffset == 0)) )
{
mLeftOffset = -aRect.left;
mTopOffset = -aRect.top;
}
#endif
void* windowPtr = nullptr;
#if TARGET_OS_WIN32
windowPtr = (void*) ASI_GethWnd((WindowPtr)mMainPort);
#elif TARGET_OS_MAC
windowPtr = (void*) GetWindowFromPort(mMainPort);
#endif
mCustomUI_p->Open(windowPtr, mLeftOffset, mTopOffset);
if (mNoUIView_p != nullptr)
{
mNoUIView_p->SetEnable(true);
}
#if TARGET_OS_WIN32
mCustomUI_p->Draw(mPIWinRect.left, mPIWinRect.top, mPIWinRect.right, mPIWinRect.bottom);
#endif
}
}
else
{
if (mCustomUI_p)
{
mCustomUI_p->Close();
}
if (mNoUIView_p != nullptr)
{
mNoUIView_p->SetEnable(false);
}
}
return;
}
//-----------------------------------------------------------------------------
long DfxPlugin::SetControlValue(long inControlIndex, long inValue)
{
return static_cast<long>(CProcess::SetControlValue(inControlIndex, inValue));
}
//-----------------------------------------------------------------------------
long DfxPlugin::GetControlValue(long inControlIndex, long* outValue)
{
return static_cast<long>(CProcess::GetControlValue(inControlIndex, outValue));
}
//-----------------------------------------------------------------------------
long DfxPlugin::GetControlDefaultValue(long inControlIndex, long* outValue)
{
return static_cast<long>(CProcess::GetControlDefaultValue(inControlIndex, outValue));
}
//-----------------------------------------------------------------------------
ComponentResult DfxPlugin::UpdateControlGraphic(long inControlIndex, long inValue)
{
if (mCustomUI_p)
return mCustomUI_p->UpdateGraphicControl(inControlIndex, inValue);
else
return noErr;
}
//-----------------------------------------------------------------------------
int DfxPlugin::ProcessTouchControl(long inControlIndex)
{
return static_cast<int>(CProcess::TouchControl(inControlIndex));
}
//-----------------------------------------------------------------------------
int DfxPlugin::ProcessReleaseControl(long inControlIndex)
{
return static_cast<int>(CProcess::ReleaseControl(inControlIndex));
}
//-----------------------------------------------------------------------------
void DfxPlugin::ProcessDoIdle()
{
this->DoIdle(nullptr);
gProcessGroup->YieldCriticalSection();
}
//-----------------------------------------------------------------------------
ComponentResult DfxPlugin::SetControlHighliteInfo(long inControlIndex, short inIsHighlighted, short inColor)
{
if (inControlIndex == dfx::kParameterID_RTASGlobalBypass)
{
CProcess::SetControlHighliteInfo(inControlIndex, inIsHighlighted, inColor);
}
if (mCustomUI_p)
{
mCustomUI_p->SetControlHighlight(inControlIndex, inIsHighlighted, inColor);
}
return noErr;
}
//-----------------------------------------------------------------------------
ComponentResult DfxPlugin::ChooseControl(Point inLocalCoord, long* outControlIndex)
{
if (outControlIndex == nullptr)
{
return paramErr;
}
*outControlIndex = 0;
if (mCustomUI_p)
{
mCustomUI_p->GetControlIndexFromPoint(inLocalCoord.h, inLocalCoord.v, outControlIndex);
}
return noErr;
}
#ifdef TARGET_API_AUDIOSUITE
//-----------------------------------------------------------------------------
// override this method to improve UI compatibility with non-Pro Tools hosts (like Media Composer)
void DfxPlugin::SetViewOrigin(Point anOrigin)
{
TARGET_API_BASE_CLASS::SetViewOrigin(anOrigin);
Rect ourPlugInViewRect;
fOurPlugInView->GetRect(&ourPlugInViewRect);
mLeftOffset = ourPlugInViewRect.left;
mTopOffset = ourPlugInViewRect.top;
}
#endif
#endif // TARGET_PLUGIN_HAS_GUI
#pragma mark -
#pragma mark EffectGroup
#pragma mark -
#ifndef TARGET_API_AUDIOSUITE
extern CEffectProcess* DFX_NewEffectProcess() noexcept;
extern CEffectProcess* DFX_NewEffectProcessAS() noexcept;
#if PT_SDK_VERSION < 0x08000000
enum
{
plugInGestalt_SupportsRTElastic = pluginGestalt_WantsLoadSettingsFileCallback + 1,
plugInGestalt_RequiresPitchData,
pluginGestalt_WantsStateReset,
plugInGestalt_SupportsControlChangesInThread,
pluginGestalt_UseSmallPreviewBuffer
};
#endif
//-----------------------------------------------------------------------------
CProcessGroupInterface* CProcessGroup::CreateProcessGroup()
{
return new DfxEffectGroup;
}
//-----------------------------------------------------------------------------
DfxEffectGroup::DfxEffectGroup()
{
DefineManufacturerNamesAndID(PLUGIN_CREATOR_NAME_STRING"\n", PLUGIN_CREATOR_ID);
DefinePlugInNamesAndVersion(PLUGIN_NAME_STRING, dfx::CompositePluginVersionNumberValue());
AddGestalt(pluginGestalt_IsCacheable);
#if PT_SDK_VERSION >= 0x08000000 // XXX otherwise compiling with earlier SDK (stupidly) results in an assert
AddGestalt(plugInGestalt_SupportsControlChangesInThread);
#endif
}
//-----------------------------------------------------------------------------
// XXX fix to honor channel configuration
void DfxEffectGroup::CreateEffectTypes()
{
OSType effectTypeFourCharID = 0;
// for (UInt32 i=1; i <= EffectLayerDef::MAX_NUM_CONNECTIONS; i++)
// int maxNumChannels = SurroundFormatToNumChannels(ePlugIn_StemFormat_LastExplicitChoice);
// for (int i=1; i <= maxNumChannels; i++)
for (int i = static_cast<int>(ePlugIn_StemFormat_FirstExplicitChoice) - 1; i <= static_cast<int>(ePlugIn_StemFormat_LastExplicitChoice); i++)
{
effectTypeFourCharID = DFX_IterateAlphaNumericFourCharCode(effectTypeFourCharID);
CEffectType* effectType {};
if (i < static_cast<int>(ePlugIn_StemFormat_FirstExplicitChoice))
{
effectType = new CEffectTypeAS(effectTypeFourCharID, PLUGIN_ID, PLUGIN_CATEGORY_RTAS);
// effectType->AddGestalt(pluginGestalt_MultiInputModeOnly); // XXX I see no reason to require this?
// effectType->DefineGenericNumInputsAndOutputs(EffectLayerDef::MAX_NUM_CONNECTIONS, EffectLayerDef::MAX_NUM_CONNECTIONS);
effectType->DefineGenericNumInputsAndOutputs(2, 2); // XXX hack for Lowender
effectType->RemoveGestalt(pluginGestalt_SupportsStemFormats); // XXX not allowed to support StemFormats when NOT doing MultiInputModeOnly
}
else
{
if (i > ePlugIn_StemFormat_Stereo) break; // XXX hack for Lowender, still need to implement real channel config handle
effectType = new CEffectTypeRTAS(effectTypeFourCharID, PLUGIN_ID, PLUGIN_CATEGORY_RTAS);
effectType->DefineStemFormats((EPlugIn_StemFormat)i, (EPlugIn_StemFormat)i);
}
dfx_AddEffectType(effectType);
}
}
//-----------------------------------------------------------------------------
void DfxEffectGroup::Initialize()
{
CEffectGroup::Initialize();
#if TARGET_PLUGIN_HAS_GUI
CCustomView::AddNewViewProc("NoUIView", CreateCTemplateNoUIView);
#else
// CGainMeterView doesn't have a RegisterView() method, so we have to do this stuff instead:
CCustomView::AddNewViewProc("GainMeterView", CreateCGainMeterView);
CCustomView::AddKeyword("labels", kConstantElem, CGainMeterView::kLabelKeyword);
// necessary for the "minimal" generic GUI
CCustomView::AddNewViewProc("ClippingView", CreateCClippingView);
CStaticCustomText::RegisterView();
CPictBackgroundTextEditor::RegisterView();
CCustomView::AddNewViewProc("Slider", CreateCSliderControlEditor);
CCustomView::AddNewViewProc("StateTextControlEditor", CreateCStateTextControlEditor);
CCustomView::AddNewViewProc("TextCustomPopupEditor", CreateCTextCustomPopupEditor);
#endif
}
//-----------------------------------------------------------------------------
void DfxEffectGroup::dfx_AddEffectType(CEffectType* inEffectType)
{
inEffectType->DefineTypeNames(PLUGIN_NAME_STRING);
inEffectType->DefineSampleRateSupport(eSupports48kAnd96kAnd192k);
inEffectType->AddGestalt(pluginGestalt_CanBypass);
inEffectType->AddGestalt(pluginGestalt_SupportsClipMeter);
#if TARGET_PLUGIN_HAS_GUI
inEffectType->AddGestalt(pluginGestalt_DoesNotUseDigiUI);
#endif
long digiPluginType = 0;
ComponentResult result = inEffectType->GetPlugInType(&digiPluginType);
if (result == noErr)
{
switch (digiPluginType)
{
case pluginType_Host: // RTAS
inEffectType->AttachEffectProcessCreator(DFX_NewEffectProcess);
inEffectType->AddGestalt(pluginGestalt_SupportsVariableQuanta);
//#if PT_SDK_VERSION >= 0x08000000 // XXX otherwise our ResetPlugInState callback doesn't actually get called
inEffectType->AddGestalt(pluginGestalt_WantsStateReset);
//#endif
break;
case pluginType_ASP: // AudioSuite
inEffectType->AttachEffectProcessCreator(DFX_NewEffectProcessAS);
inEffectType->AddGestalt(pluginGestalt_UseSmallPreviewBuffer);
break;
default:
break;
}
}
AddEffectType(inEffectType);
}
#endif