Skip to content

Commit

Permalink
AU: handle UI->DSP state changes
Browse files Browse the repository at this point in the history
Signed-off-by: falkTX <falktx@falktx.com>
  • Loading branch information
falkTX committed Feb 23, 2024
1 parent 3dccf54 commit ffb4006
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 22 deletions.
55 changes: 39 additions & 16 deletions distrho/src/DistrhoPluginAU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,19 +402,28 @@ class PluginAU
return noErr;
#endif

case 'DPFe':
DISTRHO_SAFE_ASSERT_UINT_RETURN(inScope == kAudioUnitScope_Global, inScope, kAudioUnitErr_InvalidScope);
DISTRHO_SAFE_ASSERT_UINT_RETURN(inElement < fParameterCount, inElement, kAudioUnitErr_InvalidElement);
outDataSize = sizeof(bool);
outWritable = true;
return noErr;

case 'DPFp':
DISTRHO_SAFE_ASSERT_UINT_RETURN(inScope == kAudioUnitScope_Global, inScope, kAudioUnitErr_InvalidScope);
DISTRHO_SAFE_ASSERT_UINT_RETURN(inElement < fParameterCount, inElement, kAudioUnitErr_InvalidElement);
outDataSize = sizeof(float);
outWritable = true;
return noErr;

case 'DPFt':
#if DISTRHO_PLUGIN_WANT_STATE
case 'DPFs':
DISTRHO_SAFE_ASSERT_UINT_RETURN(inScope == kAudioUnitScope_Global, inScope, kAudioUnitErr_InvalidScope);
DISTRHO_SAFE_ASSERT_UINT_RETURN(inElement < fParameterCount, inElement, kAudioUnitErr_InvalidElement);
outDataSize = sizeof(bool);
DISTRHO_SAFE_ASSERT_RETURN(inElement != 0, kAudioUnitErr_InvalidElement);
outDataSize = inElement;
outWritable = true;
return noErr;
#endif

// unwanted properties
case kAudioUnitProperty_CPULoad:
Expand Down Expand Up @@ -917,46 +926,60 @@ class PluginAU
// TODO
return noErr;

case 'DPFp':
case 'DPFe':
DISTRHO_SAFE_ASSERT_UINT_RETURN(inScope == kAudioUnitScope_Global, inScope, kAudioUnitErr_InvalidScope);
DISTRHO_SAFE_ASSERT_UINT_RETURN(inElement < fParameterCount, inElement, kAudioUnitErr_InvalidElement);
DISTRHO_SAFE_ASSERT_UINT_RETURN(inDataSize == sizeof(float), inDataSize, kAudioUnitErr_InvalidPropertyValue);
DISTRHO_SAFE_ASSERT_UINT_RETURN(inDataSize == sizeof(bool), inDataSize, kAudioUnitErr_InvalidPropertyValue);
{
const float value = *static_cast<const float*>(inData);
DISTRHO_SAFE_ASSERT_RETURN(std::isfinite(value), kAudioUnitErr_InvalidParameterValue);

fLastParameterValues[inElement] = value;
fPlugin.setParameterValue(inElement, value);
const bool started = *static_cast<const bool*>(inData);

AudioUnitEvent event;
std::memset(&event, 0, sizeof(event));

event.mEventType = kAudioUnitEvent_ParameterValueChange;
event.mEventType = started ? kAudioUnitEvent_BeginParameterChangeGesture
: kAudioUnitEvent_EndParameterChangeGesture;
event.mArgument.mParameter.mAudioUnit = fComponent;
event.mArgument.mParameter.mParameterID = inElement;
event.mArgument.mParameter.mScope = kAudioUnitScope_Global;
AUEventListenerNotify(NULL, NULL, &event);
}
return noErr;

case 'DPFt':
case 'DPFp':
DISTRHO_SAFE_ASSERT_UINT_RETURN(inScope == kAudioUnitScope_Global, inScope, kAudioUnitErr_InvalidScope);
DISTRHO_SAFE_ASSERT_UINT_RETURN(inElement < fParameterCount, inElement, kAudioUnitErr_InvalidElement);
DISTRHO_SAFE_ASSERT_UINT_RETURN(inDataSize == sizeof(bool), inDataSize, kAudioUnitErr_InvalidPropertyValue);
DISTRHO_SAFE_ASSERT_UINT_RETURN(inDataSize == sizeof(float), inDataSize, kAudioUnitErr_InvalidPropertyValue);
{
const bool started = *static_cast<const bool*>(inData);
const float value = *static_cast<const float*>(inData);
DISTRHO_SAFE_ASSERT_RETURN(std::isfinite(value), kAudioUnitErr_InvalidParameterValue);

fLastParameterValues[inElement] = value;
fPlugin.setParameterValue(inElement, value);

AudioUnitEvent event;
std::memset(&event, 0, sizeof(event));

event.mEventType = started ? kAudioUnitEvent_BeginParameterChangeGesture
: kAudioUnitEvent_EndParameterChangeGesture;
event.mEventType = kAudioUnitEvent_ParameterValueChange;
event.mArgument.mParameter.mAudioUnit = fComponent;
event.mArgument.mParameter.mParameterID = inElement;
event.mArgument.mParameter.mScope = kAudioUnitScope_Global;
AUEventListenerNotify(NULL, NULL, &event);
}
return noErr;

#if DISTRHO_PLUGIN_WANT_STATE
case 'DPFs':
DISTRHO_SAFE_ASSERT_UINT_RETURN(inScope == kAudioUnitScope_Global, inScope, kAudioUnitErr_InvalidScope);
DISTRHO_SAFE_ASSERT_RETURN(inElement != 0, kAudioUnitErr_InvalidElement);
DISTRHO_SAFE_ASSERT_UINT2_RETURN(inDataSize == inElement, inDataSize, inElement, kAudioUnitErr_InvalidPropertyValue);
{
const char* const key = static_cast<const char*>(inData);
const char* const value = key + std::strlen(key) + 1;

fPlugin.setState(key, value);
}
return noErr;
#endif
}

d_stdout("TODO SetProperty(%d:%s, %d:%s, %d, %p, %u)", inProp, AudioUnitPropertyID2Str(inProp), inScope, AudioUnitScope2Str(inScope), inElement, inData, inDataSize);
Expand Down
15 changes: 13 additions & 2 deletions distrho/src/DistrhoUIAU.mm
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ static void auPropertyChangedCallback(void* const userData,

void editParameter(const uint32_t rindex, const bool started) const
{
AudioUnitSetProperty(fComponent, 'DPFt', kAudioUnitScope_Global, rindex, &started, sizeof(bool));
AudioUnitSetProperty(fComponent, 'DPFe', kAudioUnitScope_Global, rindex, &started, sizeof(bool));
}

static void editParameterCallback(void* const ptr, const uint32_t rindex, const bool started)
Expand All @@ -175,8 +175,19 @@ static void setParameterCallback(void* const ptr, const uint32_t rindex, const f
}

#if DISTRHO_PLUGIN_WANT_STATE
void setState(const char*, const char*)
void setState(const char* const key, const char* const value)
{
const size_t len_key = std::strlen(key);
const size_t len_value = std::strlen(value);
const size_t len_combined = len_key + len_value + 2;
char* const data = new char[len_combined];
std::memcpy(data, key, len_key);
std::memcpy(data + len_key + 1, value, len_value);
data[len_key] = data[len_key + len_value + 1] = '\0';

AudioUnitSetProperty(fComponent, 'DPFs', kAudioUnitScope_Global, len_combined, data, len_combined);

delete[] data;
}

static void setStateCallback(void* const ptr, const char* const key, const char* const value)
Expand Down
3 changes: 3 additions & 0 deletions examples/Meters/DistrhoPluginInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
#define DISTRHO_PLUGIN_URI "http://distrho.sf.net/examples/Meters"
#define DISTRHO_PLUGIN_CLAP_ID "studio.kx.distrho.examples.meters"

#define DISTRHO_PLUGIN_AU_SUBTYPE mtrs
#define DISTRHO_PLUGIN_AU_MANUFACTURER Dstr

#define DISTRHO_PLUGIN_HAS_UI 1
#define DISTRHO_PLUGIN_IS_RT_SAFE 1
#define DISTRHO_PLUGIN_NUM_INPUTS 2
Expand Down
28 changes: 24 additions & 4 deletions examples/Meters/ExampleUIMeters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ class ExampleUIMeters : public UI
fColorValue(0),
// init meter values to 0
fOutLeft(0.0f),
fOutRight(0.0f)
fOutRight(0.0f),
// FIXME
fNeedsRepaint(false)
{
setGeometryConstraints(32, 128, false);
}
Expand Down Expand Up @@ -71,7 +73,9 @@ class ExampleUIMeters : public UI
if (fOutLeft != value)
{
fOutLeft = value;
repaint();
// FIXME
// repaint();
fNeedsRepaint = true;
}
break;

Expand All @@ -84,7 +88,9 @@ class ExampleUIMeters : public UI
if (fOutRight != value)
{
fOutRight = value;
repaint();
// FIXME
// repaint();
fNeedsRepaint = true;
}
break;
}
Expand Down Expand Up @@ -198,6 +204,15 @@ class ExampleUIMeters : public UI
return true;
}

void uiIdle() override
{
if (fNeedsRepaint)
{
fNeedsRepaint = false;
repaint();
}
}

// -------------------------------------------------------------------------------------------------------

private:
Expand All @@ -213,6 +228,9 @@ class ExampleUIMeters : public UI
*/
float fOutLeft, fOutRight;

// FIXME this shouldnt be needed!
bool fNeedsRepaint;

/**
Update color if needed.
*/
Expand All @@ -233,7 +251,9 @@ class ExampleUIMeters : public UI
break;
}

repaint();
// FIXME
// repaint();
fNeedsRepaint = true;
}

/**
Expand Down
1 change: 1 addition & 0 deletions examples/Meters/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ TARGETS += lv2_sep
TARGETS += vst2
TARGETS += vst3
TARGETS += clap
TARGETS += au

endif # HAVE_OPENGL

Expand Down

0 comments on commit ffb4006

Please sign in to comment.