Skip to content

Commit

Permalink
VST2 Client: Avoid C-style casts of function pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
reuk committed Nov 21, 2024
1 parent 04fa895 commit 6e910d8
Showing 1 changed file with 46 additions and 41 deletions.
87 changes: 46 additions & 41 deletions modules/juce_audio_plugin_client/juce_audio_plugin_client_VST2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,38 @@ class JuceVSTWrapper final : public AudioProcessorListener,

memset (&vstEffect, 0, sizeof (vstEffect));
vstEffect.magic = 0x56737450 /* 'VstP' */;
vstEffect.dispatcher = (Vst2::AEffectDispatcherProc) dispatcherCB;
vstEffect.dispatcher = [] (Vst2::AEffect* vstInterface,
Vst2::VstInt32 opCode,
Vst2::VstInt32 index,
Vst2::VstIntPtr value,
void* ptr,
float opt) -> Vst2::VstIntPtr
{
auto* wrapper = getWrapper (vstInterface);
VstOpCodeArguments args = { index, value, ptr, opt };

if (opCode == Vst2::effClose)
{
wrapper->dispatcher (opCode, args);
delete wrapper;
return 1;
}

return wrapper->dispatcher (opCode, args);
};

vstEffect.process = nullptr;
vstEffect.setParameter = (Vst2::AEffectSetParameterProc) setParameterCB;
vstEffect.getParameter = (Vst2::AEffectGetParameterProc) getParameterCB;

vstEffect.setParameter = [] (Vst2::AEffect* vstInterface, Vst2::VstInt32 index, float value)
{
getWrapper (vstInterface)->setParameter (index, value);
};

vstEffect.getParameter = [] (Vst2::AEffect* vstInterface, Vst2::VstInt32 index) -> float
{
return getWrapper (vstInterface)->getParameter (index);
};

vstEffect.numPrograms = jmax (1, processor->getNumPrograms());
vstEffect.numParams = juceParameters.getNumParameters();
vstEffect.numInputs = maxNumInChannels;
Expand All @@ -292,8 +320,21 @@ class JuceVSTWrapper final : public AudioProcessorListener,
vstEffect.version = JucePlugin_VersionCode;
#endif

vstEffect.processReplacing = (Vst2::AEffectProcessProc) processReplacingCB;
vstEffect.processDoubleReplacing = (Vst2::AEffectProcessDoubleProc) processDoubleReplacingCB;
vstEffect.processReplacing = [] (Vst2::AEffect* vstInterface,
float** inputs,
float** outputs,
Vst2::VstInt32 sampleFrames)
{
getWrapper (vstInterface)->processReplacing (inputs, outputs, sampleFrames);
};

vstEffect.processDoubleReplacing = [] (Vst2::AEffect* vstInterface,
double** inputs,
double** outputs,
Vst2::VstInt32 sampleFrames)
{
getWrapper (vstInterface)->processDoubleReplacing (inputs, outputs, sampleFrames);
};

vstEffect.flags |= Vst2::effFlagsHasEditor;

Expand Down Expand Up @@ -505,22 +546,12 @@ class JuceVSTWrapper final : public AudioProcessorListener,
internalProcessReplacing (inputs, outputs, sampleFrames, floatTempBuffers);
}

static void processReplacingCB (Vst2::AEffect* vstInterface, float** inputs, float** outputs, int32 sampleFrames)
{
getWrapper (vstInterface)->processReplacing (inputs, outputs, sampleFrames);
}

void processDoubleReplacing (double** inputs, double** outputs, int32 sampleFrames)
{
jassert (processor->isUsingDoublePrecision());
internalProcessReplacing (inputs, outputs, sampleFrames, doubleTempBuffers);
}

static void processDoubleReplacingCB (Vst2::AEffect* vstInterface, double** inputs, double** outputs, int32 sampleFrames)
{
getWrapper (vstInterface)->processDoubleReplacing (inputs, outputs, sampleFrames);
}

//==============================================================================
void resume()
{
Expand Down Expand Up @@ -678,22 +709,12 @@ class JuceVSTWrapper final : public AudioProcessorListener,
return 0.0f;
}

static float getParameterCB (Vst2::AEffect* vstInterface, int32 index)
{
return getWrapper (vstInterface)->getParameter (index);
}

void setParameter (int32 index, float value)
{
if (auto* param = juceParameters.getParamForIndex (index))
setValueAndNotifyIfChanged (*param, value);
}

static void setParameterCB (Vst2::AEffect* vstInterface, int32 index, float value)
{
getWrapper (vstInterface)->setParameter (index, value);
}

void audioProcessorParameterChanged (AudioProcessor*, int index, float newValue) override
{
if (inParameterChangedCallback.get())
Expand Down Expand Up @@ -902,22 +923,6 @@ class JuceVSTWrapper final : public AudioProcessorListener,
}
}

static pointer_sized_int dispatcherCB (Vst2::AEffect* vstInterface, int32 opCode, int32 index,
pointer_sized_int value, void* ptr, float opt)
{
auto* wrapper = getWrapper (vstInterface);
VstOpCodeArguments args = { index, value, ptr, opt };

if (opCode == Vst2::effClose)
{
wrapper->dispatcher (opCode, args);
delete wrapper;
return 1;
}

return wrapper->dispatcher (opCode, args);
}

//==============================================================================
// A component to hold the AudioProcessorEditor, and cope with some housekeeping
// chores when it changes or repaints.
Expand Down

0 comments on commit 6e910d8

Please sign in to comment.