Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add base implementation of configurable audio ports extension #64

Merged
merged 5 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions include/clap/helpers/plugin.hh
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,22 @@ namespace clap { namespace helpers {
return false;
}

//--------------------------------------//
// clap_plugin_configurable_audio_ports //
//--------------------------------------//
virtual bool implementsConfigurableAudioPorts() const noexcept { return false; }
virtual bool configurableAudioPortsCanApplyConfiguration(const clap_audio_port_configuration_request *requests,
uint32_t request_count) const noexcept {
return false;
}
virtual bool configurableAudioPortsApplyConfiguration(const clap_audio_port_configuration_request *requests,
uint32_t request_count) noexcept {
return false;
}
void ensureClapAudioPortConfigurationRequestIsValid(
const clap_audio_port_configuration_request *requests,
uint32_t request_count);

//--------------------//
// clap_plugin_params //
//--------------------//
Expand Down Expand Up @@ -315,6 +331,11 @@ namespace clap { namespace helpers {
void ensureAudioThread(const char *method) const noexcept;
void ensureParamThread(const char *method) const noexcept;

////////////////////
// General Checks //
////////////////////
void ensureIsInactive(const char *methodName) const noexcept;
Schroedingers-Cat marked this conversation as resolved.
Show resolved Hide resolved

///////////////
// Utilities //
///////////////
Expand Down Expand Up @@ -427,6 +448,14 @@ namespace clap { namespace helpers {
bool is_active,
uint32_t sample_size) noexcept;

// clap_plugin_configurable_audio_ports
static bool clapConfigurableAudioPortsCanApplyConfiguration(const clap_plugin_t *plugin,
const clap_audio_port_configuration_request *requests,
uint32_t request_count) noexcept;
static bool clapConfigurableAudioPortsApplyActivation(const clap_plugin_t *plugin,
Schroedingers-Cat marked this conversation as resolved.
Show resolved Hide resolved
const clap_audio_port_configuration_request *requests,
uint32_t request_count) noexcept;

// clap_plugin_params
static uint32_t clapParamsCount(const clap_plugin *plugin) noexcept;
static bool clapParamsInfo(const clap_plugin *plugin,
Expand Down Expand Up @@ -552,6 +581,7 @@ namespace clap { namespace helpers {
static const clap_plugin_audio_ports _pluginAudioPorts;
static const clap_plugin_audio_ports_config _pluginAudioPortsConfig;
static const clap_plugin_audio_ports_activation _pluginAudioPortsActivation;
static const clap_plugin_configurable_audio_ports _pluginConfigurableAudioPorts;
static const clap_plugin_gui _pluginGui;
static const clap_plugin_latency _pluginLatency;
static const clap_plugin_note_name _pluginNoteName;
Expand Down
88 changes: 88 additions & 0 deletions include/clap/helpers/plugin.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ namespace clap { namespace helpers {
clapAudioPortsActivationSetActive,
};

template <MisbehaviourHandler h, CheckingLevel l>
const clap_plugin_configurable_audio_ports Plugin<h, l>::_pluginConfigurableAudioPorts = {
clapConfigurableAudioPortsCanApplyConfiguration,
clapConfigurableAudioPortsApplyActivation,
};


template <MisbehaviourHandler h, CheckingLevel l>
const clap_plugin_params Plugin<h, l>::_pluginParams = {
clapParamsCount,
Expand Down Expand Up @@ -472,6 +479,8 @@ namespace clap { namespace helpers {
return &_pluginAudioPortsActivation;
if (!strcmp(id, CLAP_EXT_AUDIO_PORTS_CONFIG) && self.implementsAudioPortsConfig())
return &_pluginAudioPortsConfig;
if (!strcmp(id, CLAP_EXT_CONFIGURABLE_AUDIO_PORTS) && self.implementsConfigurableAudioPorts())
return &_pluginConfigurableAudioPorts;
if (!strcmp(id, CLAP_EXT_PARAMS) && self.implementsParams())
return &_pluginParams;
if ((!strcmp(id, CLAP_EXT_PARAM_INDICATION) ||
Expand Down Expand Up @@ -791,6 +800,69 @@ namespace clap { namespace helpers {
return self.paramsCount();
}

//--------------------------------------//
// clap_plugin_configurable_audio_ports //
//--------------------------------------//

template <MisbehaviourHandler h, CheckingLevel l>
bool Plugin<h, l>::clapConfigurableAudioPortsCanApplyConfiguration(
const clap_plugin_t *plugin,
const clap_audio_port_configuration_request *requests,
uint32_t request_count) noexcept {
Schroedingers-Cat marked this conversation as resolved.
Show resolved Hide resolved
auto &self = from(plugin);
auto methodName = "clap_plugin_configurable_audio_ports.can_apply_configuration";
self.ensureMainThread(methodName);
self.ensureIsInactive(methodName);
self.ensureClapAudioPortConfigurationRequestIsValid(requests, request_count);

return self.configurableAudioPortsCanApplyConfiguration(requests, request_count);
}

template <MisbehaviourHandler h, CheckingLevel l>
bool Plugin<h, l>::clapConfigurableAudioPortsApplyActivation(
const clap_plugin_t *plugin,
const clap_audio_port_configuration_request *requests,
uint32_t request_count) noexcept {
auto &self = from(plugin);
auto methodName = "clap_plugin_configurable_audio_ports.apply_configuration";
self.ensureMainThread(methodName);
self.ensureIsInactive(methodName);
self.ensureClapAudioPortConfigurationRequestIsValid(requests, request_count);

if (l >= CheckingLevel::Minimal &&
!self.configurableAudioPortsCanApplyConfiguration(requests, request_count)) {
self.hostMisbehaving(
Schroedingers-Cat marked this conversation as resolved.
Show resolved Hide resolved
"Host requested a configuration that the plugin did not accept. Check with "
"clap_plugin_configurable_audio_ports.can_apply_configuration before applying a "
"configuration!");
}

return self.configurableAudioPortsApplyConfiguration(requests, request_count);
}

template <MisbehaviourHandler h, CheckingLevel l>
void Plugin<h, l>::ensureClapAudioPortConfigurationRequestIsValid(
const clap_audio_port_configuration_request *requests,
uint32_t request_count) {
if (l == CheckingLevel::None)
return;

for (int i = 0; i < request_count; ++i) {
if (!requests[i].port_type)
continue;

if (strcmp(requests[i].port_type, CLAP_PORT_MONO) == 0 && requests[i].channel_count != 1) {
hostMisbehaving("Host requested a mono port type with a channel count other than one.");
} else if (strcmp(requests[i].port_type, CLAP_PORT_STEREO) == 0 && requests[i].channel_count != 2) {
hostMisbehaving("Host requested a stereo port type with a channel count other than two.");
} else if (strcmp(requests[i].port_type, CLAP_PORT_SURROUND) == 0 && requests[i].channel_count < 3) {
Schroedingers-Cat marked this conversation as resolved.
Show resolved Hide resolved
hostMisbehaving("Host requested a surround port type with insufficient channel count.");
} else if (strcmp(requests[i].port_type, CLAP_PORT_AMBISONIC) == 0 && requests[i].channel_count < 4) {
Schroedingers-Cat marked this conversation as resolved.
Show resolved Hide resolved
hostMisbehaving("Host requested an ambisonic port type with insufficient channel count.");
}
}
}

//--------------------//
// clap_plugin_params //
//--------------------//
Expand Down Expand Up @@ -1793,6 +1865,22 @@ namespace clap { namespace helpers {
hostMisbehaving(msg.str());
}

////////////////////
// General Checks //
////////////////////
template <MisbehaviourHandler h, CheckingLevel l>
void Plugin<h, l>::ensureIsInactive(const char *methodName) const noexcept {
if (l == CheckingLevel::None)
return;

if (!isActive())
return;

std::ostringstream msg;
msg << "it is illegal to call " << methodName << "() while the plugin is active!";
hostMisbehaving(msg.str());
}

///////////////
// Utilities //
///////////////
Expand Down
Loading