From ec5b02815d1b022b8a028c4121b27162880a0cdd Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Mon, 12 Sep 2022 19:38:03 +0200 Subject: [PATCH] Use an enum for the audio port type Otherwise we can't provide a static lifetime string pointer in the info struct. --- .../serialization/clap/ext/audio-ports.cpp | 30 +++++++++++++++- .../serialization/clap/ext/audio-ports.h | 36 ++++++++++++++++--- 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/src/common/serialization/clap/ext/audio-ports.cpp b/src/common/serialization/clap/ext/audio-ports.cpp index 80921a48..23ffc452 100644 --- a/src/common/serialization/clap/ext/audio-ports.cpp +++ b/src/common/serialization/clap/ext/audio-ports.cpp @@ -20,12 +20,40 @@ namespace clap { namespace ext { namespace audio_ports { +AudioPortType parse_port_type(const char* port_type) { + if (!port_type) { + return AudioPortType::Unknown; + } + + if (strcmp(port_type, CLAP_PORT_MONO) == 0) { + return AudioPortType::Mono; + } else if (strcmp(port_type, CLAP_PORT_STEREO) == 0) { + return AudioPortType::Stereo; + } else { + return AudioPortType::Unknown; + } +} + +const char* audio_port_type_to_string(AudioPortType port_type) { + switch (port_type) { + case AudioPortType::Mono: + return CLAP_PORT_MONO; + break; + case AudioPortType::Stereo: + return CLAP_PORT_STEREO; + break; + default: + return nullptr; + break; + } +} + AudioPortInfo::AudioPortInfo(const clap_audio_port_info_t& original) : id(original.id), name(original.name), flags(original.flags), channel_count(original.channel_count), - port_type(original.port_type), + port_type(parse_port_type(original.port_type)), in_place_pair(original.in_place_pair) {} } // namespace audio_ports diff --git a/src/common/serialization/clap/ext/audio-ports.h b/src/common/serialization/clap/ext/audio-ports.h index a79c4915..78e6ab6f 100644 --- a/src/common/serialization/clap/ext/audio-ports.h +++ b/src/common/serialization/clap/ext/audio-ports.h @@ -31,6 +31,35 @@ namespace clap { namespace ext { namespace audio_ports { +/** + * An enum representing the value of `clap_audio_port_info::port_type`. We can't + * serialize the string directly as we still need to write a pointer to it with + * static lifetime to the host's info struct. + */ +enum class AudioPortType : uint32_t { + /** A null pointer or unrecognized value. */ + Unknown, + /** `CLAP_PORT_MONO` */ + Mono, + /** `CLAP_PORT_STEREO` */ + Stereo, + + // There are also special values for CV, surround, and ambisonics, but those + // are part of draft extensions +}; + +/** + * Convert a `clap_audio_port_info::port_type` string into our port type enum. + */ +AudioPortType parse_audio_port_type(const char* port_type); + +/** + * Convert an `AudioPortType` to a static string pointer that can be used in the + * `clap_audio_port_info` struct. This is a null pointer if the port type was + * unknown or unspecified. + */ +const char* audio_port_type_to_string(AudioPortType port_type); + /** * A serializable version of `clap_audio_port_info` that owns all of the data it * references. @@ -51,10 +80,7 @@ struct AudioPortInfo { std::string name; uint32_t flags; uint32_t channel_count; - // We could create an enum for this and only serialize the predefined types, - // but store the actual string is easier and more future proof without - // having a noticeable impact on performance. - std::string port_type; + AudioPortType port_type; clap_id in_place_pair; template @@ -63,7 +89,7 @@ struct AudioPortInfo { s.text1b(name, 4096); s.value4b(flags); s.value4b(channel_count); - s.text1b(port_type); + s.value4b(port_type); s.value4b(in_place_pair); } };