diff --git a/modules/juce_audio_processors/format_types/lv2/juce_lv2_config.h b/modules/juce_audio_processors/format_types/lv2/juce_lv2_config.h new file mode 100644 index 0000000000..73b24a2f52 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/juce_lv2_config.h @@ -0,0 +1,45 @@ +#pragma once + +#define LILV_DYN_MANIFEST +#define LILV_STATIC +#define LV2_STATIC +#define SERD_STATIC +#define SORD_STATIC +#define SRATOM_STATIC +#define ZIX_STATIC + +#define LILV_VERSION "0.24.12" +#define SERD_VERSION "0.30.10" +#define SORD_VERSION "0.16.9" + +#define LILV_CXX 1 + +#if JUCE_WINDOWS + #define LILV_DIR_SEP "\\" + #define LILV_PATH_SEP ";" +#else + #define LILV_DIR_SEP "/" + #define LILV_PATH_SEP ":" +#endif + +#ifndef LILV_DEFAULT_LV2_PATH + #if JUCE_MAC || JUCE_IOS + #define LILV_DEFAULT_LV2_PATH \ + "~/Library/Audio/Plug-Ins/LV2" LILV_PATH_SEP \ + "~/.lv2" LILV_PATH_SEP \ + "/usr/local/lib/lv2" LILV_PATH_SEP \ + "/usr/lib/lv2" LILV_PATH_SEP \ + "/Library/Audio/Plug-Ins/LV2" + #elif JUCE_WINDOWS + #define LILV_DEFAULT_LV2_PATH \ + "%APPDATA%\\LV2" LILV_PATH_SEP \ + "%COMMONPROGRAMFILES%\\LV2" + #elif JUCE_LINUX || JUCE_ANDROID + #define LILV_DEFAULT_LV2_PATH \ + "~/.lv2" LILV_PATH_SEP \ + "/usr/lib/lv2" LILV_PATH_SEP \ + "/usr/local/lib/lv2" + #else + #error "Unsupported platform" + #endif +#endif diff --git a/modules/juce_audio_processors/format_types/lv2/lilv/COPYING b/modules/juce_audio_processors/format_types/lv2/lilv/COPYING new file mode 100644 index 0000000000..9748bb93d7 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lilv/COPYING @@ -0,0 +1,13 @@ +Copyright 2011-2021 David Robillard + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. \ No newline at end of file diff --git a/modules/juce_audio_processors/format_types/lv2/lilv/lilv/lilv.h b/modules/juce_audio_processors/format_types/lv2/lilv/lilv/lilv.h new file mode 100644 index 0000000000..5fd7a9f2c7 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lilv/lilv/lilv.h @@ -0,0 +1,2117 @@ +/* + Copyright 2007-2019 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +/// @file lilv.h API for Lilv, a lightweight LV2 host library. + +#ifndef LILV_LILV_H +#define LILV_LILV_H + +#include "lv2/core/lv2.h" +#include "lv2/urid/urid.h" + +#include +#include +#include +#include + +#if defined(_WIN32) && !defined(LILV_STATIC) && defined(LILV_INTERNAL) +# define LILV_API __declspec(dllexport) +#elif defined(_WIN32) && !defined(LILV_STATIC) +# define LILV_API __declspec(dllimport) +#elif defined(__GNUC__) +# define LILV_API __attribute__((visibility("default"))) +#else +# define LILV_API +#endif + +#if defined(__GNUC__) && \ + (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)) +# define LILV_DEPRECATED __attribute__((__deprecated__)) +#else +# define LILV_DEPRECATED +#endif + +#ifdef __cplusplus +extern "C" { +# if defined(__clang__) +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" +# endif +#endif + +#define LILV_NS_DOAP "http://usefulinc.com/ns/doap#" +#define LILV_NS_FOAF "http://xmlns.com/foaf/0.1/" +#define LILV_NS_LILV "http://drobilla.net/ns/lilv#" +#define LILV_NS_LV2 "http://lv2plug.in/ns/lv2core#" +#define LILV_NS_OWL "http://www.w3.org/2002/07/owl#" +#define LILV_NS_RDF "http://www.w3.org/1999/02/22-rdf-syntax-ns#" +#define LILV_NS_RDFS "http://www.w3.org/2000/01/rdf-schema#" +#define LILV_NS_XSD "http://www.w3.org/2001/XMLSchema#" + +#define LILV_URI_ATOM_PORT "http://lv2plug.in/ns/ext/atom#AtomPort" +#define LILV_URI_AUDIO_PORT "http://lv2plug.in/ns/lv2core#AudioPort" +#define LILV_URI_CONTROL_PORT "http://lv2plug.in/ns/lv2core#ControlPort" +#define LILV_URI_CV_PORT "http://lv2plug.in/ns/lv2core#CVPort" +#define LILV_URI_EVENT_PORT "http://lv2plug.in/ns/ext/event#EventPort" +#define LILV_URI_INPUT_PORT "http://lv2plug.in/ns/lv2core#InputPort" +#define LILV_URI_MIDI_EVENT "http://lv2plug.in/ns/ext/midi#MidiEvent" +#define LILV_URI_OUTPUT_PORT "http://lv2plug.in/ns/lv2core#OutputPort" +#define LILV_URI_PORT "http://lv2plug.in/ns/lv2core#Port" + +struct LilvInstanceImpl; + +/** + @defgroup lilv Lilv C API + This is the complete public C API of lilv. + @{ +*/ + +typedef struct LilvPluginImpl LilvPlugin; /**< LV2 Plugin. */ +typedef struct LilvPluginClassImpl LilvPluginClass; /**< Plugin Class. */ +typedef struct LilvPortImpl LilvPort; /**< Port. */ +typedef struct LilvScalePointImpl LilvScalePoint; /**< Scale Point. */ +typedef struct LilvUIImpl LilvUI; /**< Plugin UI. */ +typedef struct LilvNodeImpl LilvNode; /**< Typed Value. */ +typedef struct LilvWorldImpl LilvWorld; /**< Lilv World. */ +typedef struct LilvInstanceImpl LilvInstance; /**< Plugin instance. */ +typedef struct LilvStateImpl LilvState; /**< Plugin state. */ + +typedef void LilvIter; /**< Collection iterator */ +typedef void LilvPluginClasses; /**< A set of #LilvPluginClass. */ +typedef void LilvPlugins; /**< A set of #LilvPlugin. */ +typedef void LilvScalePoints; /**< A set of #LilvScalePoint. */ +typedef void LilvUIs; /**< A set of #LilvUI. */ +typedef void LilvNodes; /**< A set of #LilvNode. */ + +/** + Free memory allocated by Lilv. + + This function exists because some systems require memory allocated by a + library to be freed by code in the same library. It is otherwise equivalent + to the standard C free() function. +*/ +LILV_API +void +lilv_free(void* ptr); + +/** + @defgroup lilv_node Nodes + @{ +*/ + +/** + Convert a file URI string to a local path string. + + For example, "file://foo/bar/baz.ttl" returns "/foo/bar/baz.ttl". + Return value is shared and must not be deleted by caller. + This function does not handle escaping correctly and should not be used for + general file URIs. Use lilv_file_uri_parse() instead. + + @return `uri` converted to a path, or NULL on failure (URI is not local). +*/ +LILV_API +LILV_DEPRECATED +const char* +lilv_uri_to_path(const char* uri); + +/** + Convert a file URI string to a local path string. + + For example, "file://foo/bar%20one/baz.ttl" returns "/foo/bar one/baz.ttl". + Return value must be freed by caller with lilv_free(). + + @param uri The file URI to parse. + @param hostname If non-NULL, set to the hostname in the URI, if any. + @return `uri` converted to a path, or NULL on failure (URI is not local). +*/ +LILV_API +char* +lilv_file_uri_parse(const char* uri, char** hostname); + +/** + Create a new URI value. + + Returned value must be freed by caller with lilv_node_free(). +*/ +LILV_API +LilvNode* +lilv_new_uri(LilvWorld* world, const char* uri); + +/** + Create a new file URI value. + + Relative paths are resolved against the current working directory. Note + that this may yield unexpected results if `host` is another machine. + + @param world The world. + @param host Host name, or NULL. + @param path Path on host. + @return A new node that must be freed by caller. +*/ +LILV_API +LilvNode* +lilv_new_file_uri(LilvWorld* world, const char* host, const char* path); + +/** + Create a new string value (with no language). + + Returned value must be freed by caller with lilv_node_free(). +*/ +LILV_API +LilvNode* +lilv_new_string(LilvWorld* world, const char* str); + +/** + Create a new integer value. + + Returned value must be freed by caller with lilv_node_free(). +*/ +LILV_API +LilvNode* +lilv_new_int(LilvWorld* world, int val); + +/** + Create a new floating point value. + + Returned value must be freed by caller with lilv_node_free(). +*/ +LILV_API +LilvNode* +lilv_new_float(LilvWorld* world, float val); + +/** + Create a new boolean value. + + Returned value must be freed by caller with lilv_node_free(). +*/ +LILV_API +LilvNode* +lilv_new_bool(LilvWorld* world, bool val); + +/** + Free a LilvNode. + + It is safe to call this function on NULL. +*/ +LILV_API +void +lilv_node_free(LilvNode* val); + +/** + Duplicate a LilvNode. +*/ +LILV_API +LilvNode* +lilv_node_duplicate(const LilvNode* val); + +/** + Return whether two values are equivalent. +*/ +LILV_API +bool +lilv_node_equals(const LilvNode* value, const LilvNode* other); + +/** + Return this value as a Turtle/SPARQL token. + + Returned value must be freed by caller with lilv_free(). + + Example tokens: + + - URI: <http://example.org/foo> + - QName: doap:name + - String: "this is a string" + - Float: 1.0 + - Integer: 1 + - Boolean: true +*/ +LILV_API +char* +lilv_node_get_turtle_token(const LilvNode* value); + +/** + Return whether the value is a URI (resource). +*/ +LILV_API +bool +lilv_node_is_uri(const LilvNode* value); + +/** + Return this value as a URI string, like "http://example.org/foo". + + Valid to call only if `lilv_node_is_uri(value)` returns true. + Returned value is owned by `value` and must not be freed by caller. +*/ +LILV_API +const char* +lilv_node_as_uri(const LilvNode* value); + +/** + Return whether the value is a blank node (resource with no URI). +*/ +LILV_API +bool +lilv_node_is_blank(const LilvNode* value); + +/** + Return this value as a blank node identifier, like "genid03". + + Valid to call only if `lilv_node_is_blank(value)` returns true. + Returned value is owned by `value` and must not be freed by caller. +*/ +LILV_API +const char* +lilv_node_as_blank(const LilvNode* value); + +/** + Return whether this value is a literal (that is, not a URI). + + Returns true if `value` is a string or numeric value. +*/ +LILV_API +bool +lilv_node_is_literal(const LilvNode* value); + +/** + Return whether this value is a string literal. + + Returns true if `value` is a string value (and not numeric). +*/ +LILV_API +bool +lilv_node_is_string(const LilvNode* value); + +/** + Return `value` as a string. +*/ +LILV_API +const char* +lilv_node_as_string(const LilvNode* value); + +/** + Return the path of a file URI node. + + Returns NULL if `value` is not a file URI. + Returned value must be freed by caller with lilv_free(). +*/ +LILV_API +char* +lilv_node_get_path(const LilvNode* value, char** hostname); + +/** + Return whether this value is a decimal literal. +*/ +LILV_API +bool +lilv_node_is_float(const LilvNode* value); + +/** + Return `value` as a float. + + Valid to call only if `lilv_node_is_float(value)` or + `lilv_node_is_int(value)` returns true. +*/ +LILV_API +float +lilv_node_as_float(const LilvNode* value); + +/** + Return whether this value is an integer literal. +*/ +LILV_API +bool +lilv_node_is_int(const LilvNode* value); + +/** + Return `value` as an integer. + + Valid to call only if `lilv_node_is_int(value)` returns true. +*/ +LILV_API +int +lilv_node_as_int(const LilvNode* value); + +/** + Return whether this value is a boolean. +*/ +LILV_API +bool +lilv_node_is_bool(const LilvNode* value); + +/** + Return `value` as a bool. + + Valid to call only if `lilv_node_is_bool(value)` returns true. +*/ +LILV_API +bool +lilv_node_as_bool(const LilvNode* value); + +/** + @} + @defgroup lilv_collections Collections + + Lilv has several collection types for holding various types of value. + Each collection type supports a similar basic API, except #LilvPlugins which + is internal and thus lacks a free function: + + - void PREFIX_free (coll) + - unsigned PREFIX_size (coll) + - LilvIter* PREFIX_begin (coll) + + The types of collection are: + + - LilvPlugins, with function prefix `lilv_plugins_`. + - LilvPluginClasses, with function prefix `lilv_plugin_classes_`. + - LilvScalePoints, with function prefix `lilv_scale_points_`. + - LilvNodes, with function prefix `lilv_nodes_`. + - LilvUIs, with function prefix `lilv_uis_`. + + @{ +*/ + +/* Collections */ + +/** + Iterate over each element of a collection. + + @code + LILV_FOREACH(plugin_classes, i, classes) { + LilvPluginClass c = lilv_plugin_classes_get(classes, i); + // ... + } + @endcode +*/ +#define LILV_FOREACH(colltype, iter, collection) \ + for (LilvIter* iter = lilv_##colltype##_begin(collection); \ + !lilv_##colltype##_is_end(collection, iter); \ + (iter) = lilv_##colltype##_next(collection, iter)) + +/* LilvPluginClasses */ + +LILV_API +void +lilv_plugin_classes_free(LilvPluginClasses* collection); + +LILV_API +unsigned +lilv_plugin_classes_size(const LilvPluginClasses* collection); + +LILV_API +LilvIter* +lilv_plugin_classes_begin(const LilvPluginClasses* collection); + +LILV_API +const LilvPluginClass* +lilv_plugin_classes_get(const LilvPluginClasses* collection, LilvIter* i); + +LILV_API +LilvIter* +lilv_plugin_classes_next(const LilvPluginClasses* collection, LilvIter* i); + +LILV_API +bool +lilv_plugin_classes_is_end(const LilvPluginClasses* collection, LilvIter* i); + +/** + Get a plugin class from `classes` by URI. + + Return value is shared (stored in `classes`) and must not be freed or + modified by the caller in any way. + + @return NULL if no plugin class with `uri` is found in `classes`. +*/ +LILV_API +const LilvPluginClass* +lilv_plugin_classes_get_by_uri(const LilvPluginClasses* classes, + const LilvNode* uri); + +/* ScalePoints */ + +LILV_API +void +lilv_scale_points_free(LilvScalePoints* collection); + +LILV_API +unsigned +lilv_scale_points_size(const LilvScalePoints* collection); + +LILV_API +LilvIter* +lilv_scale_points_begin(const LilvScalePoints* collection); + +LILV_API +const LilvScalePoint* +lilv_scale_points_get(const LilvScalePoints* collection, LilvIter* i); + +LILV_API +LilvIter* +lilv_scale_points_next(const LilvScalePoints* collection, LilvIter* i); + +LILV_API +bool +lilv_scale_points_is_end(const LilvScalePoints* collection, LilvIter* i); + +/* UIs */ + +LILV_API +void +lilv_uis_free(LilvUIs* collection); + +LILV_API +unsigned +lilv_uis_size(const LilvUIs* collection); + +LILV_API +LilvIter* +lilv_uis_begin(const LilvUIs* collection); + +LILV_API +const LilvUI* +lilv_uis_get(const LilvUIs* collection, LilvIter* i); + +LILV_API +LilvIter* +lilv_uis_next(const LilvUIs* collection, LilvIter* i); + +LILV_API +bool +lilv_uis_is_end(const LilvUIs* collection, LilvIter* i); + +/** + Get a UI from `uis` by URI. + + Return value is shared (stored in `uis`) and must not be freed or + modified by the caller in any way. + + @return NULL if no UI with `uri` is found in `list`. +*/ +LILV_API +const LilvUI* +lilv_uis_get_by_uri(const LilvUIs* uis, const LilvNode* uri); + +/* Nodes */ + +LILV_API +void +lilv_nodes_free(LilvNodes* collection); + +LILV_API +unsigned +lilv_nodes_size(const LilvNodes* collection); + +LILV_API +LilvIter* +lilv_nodes_begin(const LilvNodes* collection); + +LILV_API +const LilvNode* +lilv_nodes_get(const LilvNodes* collection, LilvIter* i); + +LILV_API +LilvIter* +lilv_nodes_next(const LilvNodes* collection, LilvIter* i); + +LILV_API +bool +lilv_nodes_is_end(const LilvNodes* collection, LilvIter* i); + +LILV_API +LilvNode* +lilv_nodes_get_first(const LilvNodes* collection); + +/** + Return whether `values` contains `value`. +*/ +LILV_API +bool +lilv_nodes_contains(const LilvNodes* nodes, const LilvNode* value); + +/** + Return a new LilvNodes that contains all nodes from both `a` and `b`. +*/ +LILV_API +LilvNodes* +lilv_nodes_merge(const LilvNodes* a, const LilvNodes* b); + +/* Plugins */ + +LILV_API +unsigned +lilv_plugins_size(const LilvPlugins* collection); + +LILV_API +LilvIter* +lilv_plugins_begin(const LilvPlugins* collection); + +LILV_API +const LilvPlugin* +lilv_plugins_get(const LilvPlugins* collection, LilvIter* i); + +LILV_API +LilvIter* +lilv_plugins_next(const LilvPlugins* collection, LilvIter* i); + +LILV_API +bool +lilv_plugins_is_end(const LilvPlugins* collection, LilvIter* i); + +/** + Get a plugin from `plugins` by URI. + + Return value is shared (stored in `plugins`) and must not be freed or + modified by the caller in any way. + + @return NULL if no plugin with `uri` is found in `plugins`. +*/ +LILV_API +const LilvPlugin* +lilv_plugins_get_by_uri(const LilvPlugins* plugins, const LilvNode* uri); + +/** + @} + @defgroup lilv_world World + + The "world" represents all Lilv state, and is used to discover/load/cache + LV2 data (plugins, UIs, and extensions). + Normal hosts which just need to load plugins by URI should simply use + lilv_world_load_all() to discover/load the system's LV2 resources. + + @{ +*/ + +/** + Initialize a new, empty world. + + If initialization fails, NULL is returned. +*/ +LILV_API +LilvWorld* +lilv_world_new(void); + +/** + Enable/disable language filtering. + + Language filtering applies to any functions that return (a) value(s). + With filtering enabled, Lilv will automatically return the best value(s) + for the current LANG. With filtering disabled, all matching values will + be returned regardless of language tag. Filtering is enabled by default. +*/ +#define LILV_OPTION_FILTER_LANG "http://drobilla.net/ns/lilv#filter-lang" + +/** + Enable/disable dynamic manifest support. + + Dynamic manifest data will only be loaded if this option is true. +*/ +#define LILV_OPTION_DYN_MANIFEST "http://drobilla.net/ns/lilv#dyn-manifest" + +/** + Set application-specific LV2_PATH. + + This overrides the LV2_PATH from the environment, so that lilv will only + look inside the given path. This can be used to make self-contained + applications. +*/ +#define LILV_OPTION_LV2_PATH "http://drobilla.net/ns/lilv#lv2-path" + +/** + Set an option for `world`. + + Currently recognized options: + + - #LILV_OPTION_FILTER_LANG + - #LILV_OPTION_DYN_MANIFEST + - #LILV_OPTION_LV2_PATH +*/ +LILV_API +void +lilv_world_set_option(LilvWorld* world, const char* uri, const LilvNode* value); + +/** + Destroy the world. + + It is safe to call this function on NULL. Note that destroying `world` will + destroy all the objects it contains. Do not destroy the world until you are + finished with all objects that came from it. +*/ +LILV_API +void +lilv_world_free(LilvWorld* world); + +/** + Load all installed LV2 bundles on the system. + + This is the recommended way for hosts to load LV2 data. It implements the + established/standard best practice for discovering all LV2 data on the + system. The environment variable LV2_PATH may be used to control where + this function will look for bundles. + + Hosts should use this function rather than explicitly load bundles, except + in special circumstances such as development utilities, or hosts that ship + with special plugin bundles which are installed to a known location. +*/ +LILV_API +void +lilv_world_load_all(LilvWorld* world); + +/** + Load a specific bundle. + + `bundle_uri` must be a fully qualified URI to the bundle directory, + with the trailing slash, eg. file:///usr/lib/lv2/foo.lv2/ + + Normal hosts should not need this function (use lilv_world_load_all()). + + Hosts MUST NOT attach any long-term significance to bundle paths + (for example in save files), since there are no guarantees they will remain + unchanged between (or even during) program invocations. Plugins (among + other things) MUST be identified by URIs (not paths) in save files. +*/ +LILV_API +void +lilv_world_load_bundle(LilvWorld* world, const LilvNode* bundle_uri); + +/** + Load all specifications from currently loaded bundles. + + This is for hosts that explicitly load specific bundles, its use is not + necessary when using lilv_world_load_all(). This function parses the + specifications and adds them to the model. +*/ +LILV_API +void +lilv_world_load_specifications(LilvWorld* world); + +/** + Load all plugin classes from currently loaded specifications. + + Must be called after lilv_world_load_specifications(). This is for hosts + that explicitly load specific bundles, its use is not necessary when using + lilv_world_load_all(). +*/ +LILV_API +void +lilv_world_load_plugin_classes(LilvWorld* world); + +/** + Unload a specific bundle. + + This unloads statements loaded by lilv_world_load_bundle(). Note that this + is not necessarily all information loaded from the bundle. If any resources + have been separately loaded with lilv_world_load_resource(), they must be + separately unloaded with lilv_world_unload_resource(). +*/ +LILV_API +int +lilv_world_unload_bundle(LilvWorld* world, const LilvNode* bundle_uri); + +/** + Load all the data associated with the given `resource`. + + All accessible data files linked to `resource` with rdfs:seeAlso will be + loaded into the world model. + + @param world The world. + @param resource Must be a subject (a URI or a blank node). + @return The number of files parsed, or -1 on error +*/ +LILV_API +int +lilv_world_load_resource(LilvWorld* world, const LilvNode* resource); + +/** + Unload all the data associated with the given `resource`. + + This unloads all data loaded by a previous call to + lilv_world_load_resource() with the given `resource`. + + @param world The world. + @param resource Must be a subject (a URI or a blank node). +*/ +LILV_API +int +lilv_world_unload_resource(LilvWorld* world, const LilvNode* resource); + +/** + Get the parent of all other plugin classes, lv2:Plugin. +*/ +LILV_API +const LilvPluginClass* +lilv_world_get_plugin_class(const LilvWorld* world); + +/** + Return a list of all found plugin classes. + + Returned list is owned by world and must not be freed by the caller. +*/ +LILV_API +const LilvPluginClasses* +lilv_world_get_plugin_classes(const LilvWorld* world); + +/** + Return a list of all found plugins. + + The returned list contains just enough references to query + or instantiate plugins. The data for a particular plugin will not be + loaded into memory until a call to an lilv_plugin_* function results in + a query (at which time the data is cached with the LilvPlugin so future + queries are very fast). + + The returned list and the plugins it contains are owned by `world` + and must not be freed by caller. +*/ +LILV_API +const LilvPlugins* +lilv_world_get_all_plugins(const LilvWorld* world); + +/** + Find nodes matching a triple pattern. + + Either `subject` or `object` may be NULL (a wildcard), but not both. + + @return All matches for the wildcard field, or NULL. +*/ +LILV_API +LilvNodes* +lilv_world_find_nodes(LilvWorld* world, + const LilvNode* subject, + const LilvNode* predicate, + const LilvNode* object); + +/** + Find a single node that matches a pattern. + + Exactly one of `subject`, `predicate`, `object` must be NULL. + This function is equivalent to + lilv_nodes_get_first(lilv_world_find_nodes(...)) but simplifies the common + case of only wanting a single value. + + @return The first matching node, or NULL if no matches are found. +*/ +LILV_API +LilvNode* +lilv_world_get(LilvWorld* world, + const LilvNode* subject, + const LilvNode* predicate, + const LilvNode* object); + +/** + Return true iff a statement matching a certain pattern exists. + + This is useful for checking if particular statement exists without having to + bother with collections and memory management. + + @param world The world. + @param subject Subject of statement, or NULL for anything. + @param predicate Predicate (key) of statement, or NULL for anything. + @param object Object (value) of statement, or NULL for anything. +*/ +LILV_API +bool +lilv_world_ask(LilvWorld* world, + const LilvNode* subject, + const LilvNode* predicate, + const LilvNode* object); + +/** + Get an LV2 symbol for some subject. + + This will return the lv2:symbol property of the subject if it is given + explicitly, and otherwise will attempt to derive a symbol from the URI. + + @return A string node that is a valid LV2 symbol, or NULL on error. +*/ +LILV_API +LilvNode* +lilv_world_get_symbol(LilvWorld* world, const LilvNode* subject); + +/** + @} + @defgroup lilv_plugin Plugins + @{ +*/ + +/** + Check if `plugin` is valid. + + This is not a rigorous validator, but can be used to reject some malformed + plugins that could cause bugs (for example, plugins with missing required + fields). + + Note that normal hosts do NOT need to use this - lilv does not + load invalid plugins into plugin lists. This is included for plugin + testing utilities, etc. + + @return True iff `plugin` is valid. +*/ +LILV_API +bool +lilv_plugin_verify(const LilvPlugin* plugin); + +/** + Get the URI of `plugin`. + + Any serialization that refers to plugins should refer to them by this. + Hosts SHOULD NOT save any filesystem paths, plugin indexes, etc. in saved + files; save only the URI. + + The URI is a globally unique identifier for one specific plugin. Two + plugins with the same URI are compatible in port signature, and should + be guaranteed to work in a compatible and consistent way. If a plugin + is upgraded in an incompatible way (eg if it has different ports), it + MUST have a different URI than it's predecessor. + + @return A shared URI value which must not be modified or freed. +*/ +LILV_API +const LilvNode* +lilv_plugin_get_uri(const LilvPlugin* plugin); + +/** + Get the (resolvable) URI of the plugin's "main" bundle. + + This returns the URI of the bundle where the plugin itself was found. Note + that the data for a plugin may be spread over many bundles, that is, + lilv_plugin_get_data_uris() may return URIs which are not within this + bundle. + + Typical hosts should not need to use this function. + Note this always returns a fully qualified URI. If you want a local + filesystem path, use lilv_file_uri_parse(). + + @return A shared string which must not be modified or freed. +*/ +LILV_API +const LilvNode* +lilv_plugin_get_bundle_uri(const LilvPlugin* plugin); + +/** + Get the (resolvable) URIs of the RDF data files that define a plugin. + + Typical hosts should not need to use this function. + Note this always returns fully qualified URIs. If you want local + filesystem paths, use lilv_file_uri_parse(). + + @return A list of complete URLs eg. "file:///foo/ABundle.lv2/aplug.ttl", + which is shared and must not be modified or freed. +*/ +LILV_API +const LilvNodes* +lilv_plugin_get_data_uris(const LilvPlugin* plugin); + +/** + Get the (resolvable) URI of the shared library for `plugin`. + + Note this always returns a fully qualified URI. If you want a local + filesystem path, use lilv_file_uri_parse(). + + @return A shared string which must not be modified or freed. +*/ +LILV_API +const LilvNode* +lilv_plugin_get_library_uri(const LilvPlugin* plugin); + +/** + Get the name of `plugin`. + + This returns the name (doap:name) of the plugin. The name may be + translated according to the current locale, this value MUST NOT be used + as a plugin identifier (use the URI for that). + + Returned value must be freed by the caller. +*/ +LILV_API +LilvNode* +lilv_plugin_get_name(const LilvPlugin* plugin); + +/** + Get the class this plugin belongs to (like "Filters" or "Effects"). +*/ +LILV_API +const LilvPluginClass* +lilv_plugin_get_class(const LilvPlugin* plugin); + +/** + Get a value associated with the plugin in a plugin's data files. + + `predicate` must be either a URI or a QName. + + Returns the ?object of all triples found of the form: + + <plugin-uri> predicate ?object + + May return NULL if the property was not found, or if object(s) is not + sensibly represented as a LilvNodes. + + Return value must be freed by caller with lilv_nodes_free(). +*/ +LILV_API +LilvNodes* +lilv_plugin_get_value(const LilvPlugin* plugin, const LilvNode* predicate); + +/** + Return whether a feature is supported by a plugin. + + This will return true if the feature is an optional or required feature + of the plugin. +*/ +LILV_API +bool +lilv_plugin_has_feature(const LilvPlugin* plugin, const LilvNode* feature); + +/** + Get the LV2 Features supported (required or optionally) by a plugin. + + A feature is "supported" by a plugin if it is required OR optional. + + Since required features have special rules the host must obey, this function + probably shouldn't be used by normal hosts. Using + lilv_plugin_get_optional_features() and lilv_plugin_get_required_features() + separately is best in most cases. + + Returned value must be freed by caller with lilv_nodes_free(). +*/ +LILV_API +LilvNodes* +lilv_plugin_get_supported_features(const LilvPlugin* plugin); + +/** + Get the LV2 Features required by a plugin. + + If a feature is required by a plugin, hosts MUST NOT use the plugin if they + do not understand (or are unable to support) that feature. + + All values returned here MUST be passed to the plugin's instantiate method + (along with data, if necessary, as defined by the feature specification) + or plugin instantiation will fail. + + Return value must be freed by caller with lilv_nodes_free(). +*/ +LILV_API +LilvNodes* +lilv_plugin_get_required_features(const LilvPlugin* plugin); + +/** + Get the LV2 Features optionally supported by a plugin. + + Hosts MAY ignore optional plugin features for whatever reasons. Plugins + MUST operate (at least somewhat) if they are instantiated without being + passed optional features. + + Return value must be freed by caller with lilv_nodes_free(). +*/ +LILV_API +LilvNodes* +lilv_plugin_get_optional_features(const LilvPlugin* plugin); + +/** + Return whether or not a plugin provides a specific extension data. +*/ +LILV_API +bool +lilv_plugin_has_extension_data(const LilvPlugin* plugin, const LilvNode* uri); + +/** + Get a sequence of all extension data provided by a plugin. + + This can be used to find which URIs lilv_instance_get_extension_data() + will return a value for without instantiating the plugin. +*/ +LILV_API +LilvNodes* +lilv_plugin_get_extension_data(const LilvPlugin* plugin); + +/** + Get the number of ports on this plugin. +*/ +LILV_API +uint32_t +lilv_plugin_get_num_ports(const LilvPlugin* plugin); + +/** + Get the port ranges (minimum, maximum and default values) for all ports. + + `min_values`, `max_values` and `def_values` must either point to an array + of N floats, where N is the value returned by lilv_plugin_get_num_ports() + for this plugin, or NULL. The elements of the array will be set to the + the minimum, maximum and default values of the ports on this plugin, + with array index corresponding to port index. If a port doesn't have a + minimum, maximum or default value, or the port's type is not float, the + corresponding array element will be set to NAN. + + This is a convenience method for the common case of getting the range of + all float ports on a plugin, and may be significantly faster than + repeated calls to lilv_port_get_range(). +*/ +LILV_API +void +lilv_plugin_get_port_ranges_float(const LilvPlugin* plugin, + float* min_values, + float* max_values, + float* def_values); + +/** + Get the number of ports on this plugin that are members of some class(es). + + Note that this is a varargs function so ports fitting any type 'profile' + desired can be found quickly. REMEMBER TO TERMINATE THE PARAMETER LIST + OF THIS FUNCTION WITH NULL OR VERY NASTY THINGS WILL HAPPEN. +*/ +LILV_API +uint32_t +lilv_plugin_get_num_ports_of_class(const LilvPlugin* plugin, + const LilvNode* class_1, + ...); + +/** + Variant of lilv_plugin_get_num_ports_of_class() that takes a va_list. + + This function calls va_arg() on `args` but does not call va_end(). +*/ +LILV_API +uint32_t +lilv_plugin_get_num_ports_of_class_va(const LilvPlugin* plugin, + const LilvNode* class_1, + va_list args); + +/** + Return whether or not the plugin introduces (and reports) latency. + + The index of the latency port can be found with + lilv_plugin_get_latency_port() ONLY if this function returns true. +*/ +LILV_API +bool +lilv_plugin_has_latency(const LilvPlugin* plugin); + +/** + Return the index of the plugin's latency port. + + It is a fatal error to call this on a plugin without checking if the port + exists by first calling lilv_plugin_has_latency(). + + Any plugin that introduces unwanted latency that should be compensated for + (by hosts with the ability/need) MUST provide this port, which is a control + rate output port that reports the latency for each cycle in frames. +*/ +LILV_API +uint32_t +lilv_plugin_get_latency_port_index(const LilvPlugin* plugin); + +/** + Get a port on `plugin` by `index`. +*/ +LILV_API +const LilvPort* +lilv_plugin_get_port_by_index(const LilvPlugin* plugin, uint32_t index); + +/** + Get a port on `plugin` by `symbol`. + + Note this function is slower than lilv_plugin_get_port_by_index(), + especially on plugins with a very large number of ports. +*/ +LILV_API +const LilvPort* +lilv_plugin_get_port_by_symbol(const LilvPlugin* plugin, + const LilvNode* symbol); + +/** + Get a port on `plugin` by its lv2:designation. + + The designation of a port describes the meaning, assignment, allocation or + role of the port, like "left channel" or "gain". If found, the port with + matching `port_class` and `designation` is be returned, otherwise NULL is + returned. The `port_class` can be used to distinguish the input and output + ports for a particular designation. If `port_class` is NULL, any port with + the given designation will be returned. +*/ +LILV_API +const LilvPort* +lilv_plugin_get_port_by_designation(const LilvPlugin* plugin, + const LilvNode* port_class, + const LilvNode* designation); + +/** + Get the project the plugin is a part of. + + More information about the project can be read via lilv_world_find_nodes(), + typically using properties from DOAP (such as doap:name). +*/ +LILV_API +LilvNode* +lilv_plugin_get_project(const LilvPlugin* plugin); + +/** + Get the full name of the plugin's author. + + Returns NULL if author name is not present. + Returned value must be freed by caller. +*/ +LILV_API +LilvNode* +lilv_plugin_get_author_name(const LilvPlugin* plugin); + +/** + Get the email address of the plugin's author. + + Returns NULL if author email address is not present. + Returned value must be freed by caller. +*/ +LILV_API +LilvNode* +lilv_plugin_get_author_email(const LilvPlugin* plugin); + +/** + Get the address of the plugin author's home page. + + Returns NULL if author homepage is not present. + Returned value must be freed by caller. +*/ +LILV_API +LilvNode* +lilv_plugin_get_author_homepage(const LilvPlugin* plugin); + +/** + Return true iff `plugin` has been replaced by another plugin. + + The plugin will still be usable, but hosts should hide them from their + user interfaces to prevent users from using deprecated plugins. +*/ +LILV_API +bool +lilv_plugin_is_replaced(const LilvPlugin* plugin); + +/** + Write the Turtle description of `plugin` to `plugin_file`. + + This function is particularly useful for porting plugins in conjunction with + an LV2 bridge such as NASPRO. +*/ +LILV_API +void +lilv_plugin_write_description(LilvWorld* world, + const LilvPlugin* plugin, + const LilvNode* base_uri, + FILE* plugin_file); + +/** + Write a manifest entry for `plugin` to `manifest_file`. + + This function is intended for use with lilv_plugin_write_description() to + write a complete description of a plugin to a bundle. +*/ +LILV_API +void +lilv_plugin_write_manifest_entry(LilvWorld* world, + const LilvPlugin* plugin, + const LilvNode* base_uri, + FILE* manifest_file, + const char* plugin_file_path); + +/** + Get the resources related to `plugin` with lv2:appliesTo. + + Some plugin-related resources are not linked directly to the plugin with + rdfs:seeAlso and thus will not be automatically loaded along with the plugin + data (usually for performance reasons). All such resources of the given @c + type related to `plugin` can be accessed with this function. + + If `type` is NULL, all such resources will be returned, regardless of type. + + To actually load the data for each returned resource, use + lilv_world_load_resource(). +*/ +LILV_API +LilvNodes* +lilv_plugin_get_related(const LilvPlugin* plugin, const LilvNode* type); + +/** + @} + @defgroup lilv_port Ports + @{ +*/ + +/** + Get the RDF node of `port`. + + Ports nodes may be may be URIs or blank nodes. + + @return A shared node which must not be modified or freed. +*/ +LILV_API +const LilvNode* +lilv_port_get_node(const LilvPlugin* plugin, const LilvPort* port); + +/** + Port analog of lilv_plugin_get_value(). +*/ +LILV_API +LilvNodes* +lilv_port_get_value(const LilvPlugin* plugin, + const LilvPort* port, + const LilvNode* predicate); + +/** + Get a single property value of a port. + + This is equivalent to lilv_nodes_get_first(lilv_port_get_value(...)) but is + simpler to use in the common case of only caring about one value. The + caller is responsible for freeing the returned node. +*/ +LILV_API +LilvNode* +lilv_port_get(const LilvPlugin* plugin, + const LilvPort* port, + const LilvNode* predicate); + +/** + Return the LV2 port properties of a port. +*/ +LILV_API +LilvNodes* +lilv_port_get_properties(const LilvPlugin* plugin, const LilvPort* port); + +/** + Return whether a port has a certain property. +*/ +LILV_API +bool +lilv_port_has_property(const LilvPlugin* plugin, + const LilvPort* port, + const LilvNode* property); + +/** + Return whether a port supports a certain event type. + + More precisely, this returns true iff the port has an atom:supports or an + ev:supportsEvent property with `event_type` as the value. +*/ +LILV_API +bool +lilv_port_supports_event(const LilvPlugin* plugin, + const LilvPort* port, + const LilvNode* event_type); + +/** + Get the index of a port. + + The index is only valid for the life of the plugin and may change between + versions. For a stable identifier, use the symbol. +*/ +LILV_API +uint32_t +lilv_port_get_index(const LilvPlugin* plugin, const LilvPort* port); + +/** + Get the symbol of a port. + + The 'symbol' is a short string, a valid C identifier. + Returned value is owned by `port` and must not be freed. +*/ +LILV_API +const LilvNode* +lilv_port_get_symbol(const LilvPlugin* plugin, const LilvPort* port); + +/** + Get the name of a port. + + This is guaranteed to return the untranslated name (the doap:name in the + data file without a language tag). Returned value must be freed by + the caller. +*/ +LILV_API +LilvNode* +lilv_port_get_name(const LilvPlugin* plugin, const LilvPort* port); + +/** + Get all the classes of a port. + + This can be used to determine if a port is an input, output, audio, + control, midi, etc, etc, though it's simpler to use lilv_port_is_a(). + The returned list does not include lv2:Port, which is implied. + Returned value is shared and must not be destroyed by caller. +*/ +LILV_API +const LilvNodes* +lilv_port_get_classes(const LilvPlugin* plugin, const LilvPort* port); + +/** + Determine if a port is of a given class (input, output, audio, etc). + + For convenience/performance/extensibility reasons, hosts are expected to + create a LilvNode for each port class they "care about". Well-known type + URI strings like `LILV_URI_INPUT_PORT` are defined for convenience, but + this function is designed so that Lilv is usable with any port types + without requiring explicit support in Lilv. +*/ +LILV_API +bool +lilv_port_is_a(const LilvPlugin* plugin, + const LilvPort* port, + const LilvNode* port_class); + +/** + Get the default, minimum, and maximum values of a port. + + `def`, `min`, and `max` are outputs, pass pointers to uninitialized + LilvNode* variables. These will be set to point at new values (which must + be freed by the caller using lilv_node_free()), or NULL if the value does + not exist. +*/ +LILV_API +void +lilv_port_get_range(const LilvPlugin* plugin, + const LilvPort* port, + LilvNode** def, + LilvNode** min, + LilvNode** max); + +/** + Get the scale points (enumeration values) of a port. + + This returns a collection of 'interesting' named values of a port, which for + example might be appropriate entries for a value selector in a UI. + + Returned value may be NULL if `port` has no scale points, otherwise it + must be freed by caller with lilv_scale_points_free(). +*/ +LILV_API +LilvScalePoints* +lilv_port_get_scale_points(const LilvPlugin* plugin, const LilvPort* port); + +/** + @} + @defgroup lilv_state Plugin State + @{ +*/ + +/** + Load a state snapshot from the world RDF model. + + This function can be used to load the default state of a plugin by passing + the plugin URI as the `subject` parameter. + + @param world The world. + @param map URID mapper. + @param node The subject of the state description (such as a preset URI). + @return A new LilvState which must be freed with lilv_state_free(), or NULL. +*/ +LILV_API +LilvState* +lilv_state_new_from_world(LilvWorld* world, + LV2_URID_Map* map, + const LilvNode* node); + +/** + Load a state snapshot from a file. + + If `subject` is NULL, it is taken to be the URI of the file (`<>` in + Turtle). + + This function parses the file separately to create the state, it does not + parse the file into the world model, that is, the returned state is the only + new memory consumed once this function returns. + + @param world The world. + @param map URID mapper. + @param subject The subject of the state description (such as a preset URI). + @param path The path of the file containing the state description. + @return A new LilvState which must be freed with lilv_state_free(). +*/ +LILV_API +LilvState* +lilv_state_new_from_file(LilvWorld* world, + LV2_URID_Map* map, + const LilvNode* subject, + const char* path); + +/** + Load a state snapshot from a string made by lilv_state_to_string(). +*/ +LILV_API +LilvState* +lilv_state_new_from_string(LilvWorld* world, + LV2_URID_Map* map, + const char* str); + +/** + Function to get a port value. + + This function MUST set `size` and `type` appropriately. + + @param port_symbol The symbol of the port. + @param user_data The user_data passed to lilv_state_new_from_instance(). + @param size (Output) The size of the returned value. + @param type (Output) The URID of the type of the returned value. + @return A pointer to the port value. +*/ +typedef const void* (*LilvGetPortValueFunc)(const char* port_symbol, + void* user_data, + uint32_t* size, + uint32_t* type); + +/** + Create a new state snapshot from a plugin instance. + + + This function may be called simultaneously with any instance function + (except discovery functions) unless the threading class of that function + explicitly disallows this. + + To support advanced file functionality, there are several directory + parameters. The multiple parameters are necessary to support saving an + instance's state many times, or saving states from multiple instances, while + avoiding any duplication of data. For example, a host could pass the same + `copy_dir` and `link_dir` for all plugins in a session (for example + `session/shared/copy/` `session/shared/link/`), while the `save_dir` would + be unique to each plugin instance (for example `session/states/state1.lv2` + for one instance and `session/states/state2.lv2` for another instance). + Simple hosts that only wish to save a single plugin's state once may simply + use the same directory for all of them, or pass NULL to not support files at + all. + + If supported (via state:makePath passed to LV2_Descriptor::instantiate()), + `scratch_dir` should be the directory where any files created by the plugin + (for example during instantiation or while running) are stored. Any files + here that are referred to in the state will be copied to preserve their + contents at the time of the save. Lilv will assume any files within this + directory (recursively) are created by the plugin and that all other files + are immutable. Note that this function does not completely save the state, + use lilv_state_save() for that. + + See state.h from the + LV2 State extension for details on the `flags` and `features` parameters. + + @param plugin The plugin this state applies to. + + @param instance An instance of `plugin`. + + @param map The map to use for mapping URIs in state. + + @param scratch_dir Directory of files created by the plugin earlier, or + NULL. This is for hosts that support file creation at any time with state + state:makePath. These files will be copied as necessary to `copy_dir` and + not be referred to directly in state (a temporary directory is appropriate). + + @param copy_dir Directory of copies of files in `scratch_dir`, or NULL. + This directory will have the same structure as `scratch_dir` but with + possibly modified file names to distinguish revisions. This allows the + saved state to contain the exact contents of the scratch file at save time, + so that the state is not ruined if the file is later modified (for example, + by the plugin continuing to record). This can be the same as `save_dir` to + create a copy in the state bundle, but can also be a separate directory + which allows multiple state snapshots to share a single copy if the file has + not changed. + + @param link_dir Directory of links to external files, or NULL. A link will + be made in this directory to any external files referred to in plugin state. + In turn, links will be created in the save directory to these links (like + save_dir/file => link_dir/file => /foo/bar/file). This allows many state + snapshots to share a single link to an external file, so archival (for + example, with `tar -h`) will not create several copies of the file. If this + is not required, it can be the same as `save_dir`. + + @param save_dir Directory of files created by plugin during save (or NULL). + This is typically the bundle directory later passed to lilv_state_save(). + + @param get_value Function to get port values (or NULL). If NULL, the + returned state will not represent port values. This should only be NULL in + hosts that save and restore port values via some other mechanism. + + @param user_data User data to pass to `get_value`. + + @param flags Bitwise OR of LV2_State_Flags values. + + @param features Features to pass LV2_State_Interface.save(). + + @return A new LilvState which must be freed with lilv_state_free(). +*/ +LILV_API +LilvState* +lilv_state_new_from_instance(const LilvPlugin* plugin, + LilvInstance* instance, + LV2_URID_Map* map, + const char* scratch_dir, + const char* copy_dir, + const char* link_dir, + const char* save_dir, + LilvGetPortValueFunc get_value, + void* user_data, + uint32_t flags, + const LV2_Feature* const* features); + +/** + Free `state`. +*/ +LILV_API +void +lilv_state_free(LilvState* state); + +/** + Return true iff `a` is equivalent to `b`. +*/ +LILV_API +bool +lilv_state_equals(const LilvState* a, const LilvState* b); + +/** + Return the number of properties in `state`. +*/ +LILV_API +unsigned +lilv_state_get_num_properties(const LilvState* state); + +/** + Get the URI of the plugin `state` applies to. +*/ +LILV_API +const LilvNode* +lilv_state_get_plugin_uri(const LilvState* state); + +/** + Get the URI of `state`. + + This may return NULL if the state has not been saved and has no URI. +*/ +LILV_API +const LilvNode* +lilv_state_get_uri(const LilvState* state); + +/** + Get the label of `state`. +*/ +LILV_API +const char* +lilv_state_get_label(const LilvState* state); + +/** + Set the label of `state`. +*/ +LILV_API +void +lilv_state_set_label(LilvState* state, const char* label); + +/** + Set a metadata property on `state`. + + This is a generic version of lilv_state_set_label(), which sets metadata + properties visible to hosts, but not plugins. This allows storing useful + information such as comments or preset banks. + + @param state The state to set the metadata for. + @param key The key to store `value` under (URID). + @param value Pointer to the value to be stored. + @param size The size of `value` in bytes. + @param type The type of `value` (URID). + @param flags LV2_State_Flags for `value`. + @return Zero on success. +*/ +LILV_API +int +lilv_state_set_metadata(LilvState* state, + uint32_t key, + const void* value, + size_t size, + uint32_t type, + uint32_t flags); + +/** + Function to set a port value. + + @param port_symbol The symbol of the port. + @param user_data The user_data passed to lilv_state_restore(). + @param size The size of `value`. + @param type The URID of the type of `value`. + @param value A pointer to the port value. +*/ +typedef void (*LilvSetPortValueFunc)(const char* port_symbol, + void* user_data, + const void* value, + uint32_t size, + uint32_t type); + +/** + Enumerate the port values in a state snapshot. + + This function is a subset of lilv_state_restore() that only fires the + `set_value` callback and does not directly affect a plugin instance. This + is useful in hosts that need to retrieve the port values in a state snapshot + for special handling. + + @param state The state to retrieve port values from. + @param set_value A function to receive port values. + @param user_data User data to pass to `set_value`. +*/ +LILV_API +void +lilv_state_emit_port_values(const LilvState* state, + LilvSetPortValueFunc set_value, + void* user_data); + +/** + Restore a plugin instance from a state snapshot. + + This will set all the properties of `instance`, if given, to the values + stored in `state`. If `set_value` is provided, it will be called (with the + given `user_data`) to restore each port value, otherwise the host must + restore the port values itself (using lilv_state_get_port_value()) in order + to completely restore `state`. + + If the state has properties and `instance` is given, this function is in + the "instantiation" threading class, so it MUST NOT be called + simultaneously with any function on the same plugin instance. If the state + has no properties, only port values are set via `set_value`. + + See state.h from the + LV2 State extension for details on the `flags` and `features` parameters. + + @param state The state to restore, which must apply to the correct plugin. + @param instance An instance of the plugin `state` applies to, or NULL. + @param set_value A function to set a port value (may be NULL). + @param user_data User data to pass to `set_value`. + @param flags Bitwise OR of LV2_State_Flags values. + @param features Features to pass LV2_State_Interface.restore(). +*/ +LILV_API +void +lilv_state_restore(const LilvState* state, + LilvInstance* instance, + LilvSetPortValueFunc set_value, + void* user_data, + uint32_t flags, + const LV2_Feature* const* features); + +/** + Save state to a file. + + The format of state on disk is compatible with that defined in the LV2 + preset extension, so this function may be used to save presets which can + be loaded by any host. + + If `uri` is NULL, the preset URI will be a file URI, but the bundle + can safely be moved (the state file will use `<>` as the subject). + + @param world The world. + @param map URID mapper. + @param unmap URID unmapper. + @param state State to save. + @param uri URI of state, may be NULL. + @param dir Path of the bundle directory to save into. + @param filename Path of the state file relative to `dir`. +*/ +LILV_API +int +lilv_state_save(LilvWorld* world, + LV2_URID_Map* map, + LV2_URID_Unmap* unmap, + const LilvState* state, + const char* uri, + const char* dir, + const char* filename); + +/** + Save state to a string. + + This function does not use the filesystem. + + @param world The world. + + @param map URID mapper. + + @param unmap URID unmapper. + + @param state The state to serialize. + + @param uri URI for the state description (mandatory). + + @param base_uri Base URI for serialisation. Unless you know what you are + doing, pass NULL for this, otherwise the state may not be restorable via + lilv_state_new_from_string(). +*/ +LILV_API +char* +lilv_state_to_string(LilvWorld* world, + LV2_URID_Map* map, + LV2_URID_Unmap* unmap, + const LilvState* state, + const char* uri, + const char* base_uri); + +/** + Unload a state from the world and delete all associated files. + + This function DELETES FILES/DIRECTORIES FROM THE FILESYSTEM! It is intended + for removing user-saved presets, but can delete any state the user has + permission to delete, including presets shipped with plugins. + + The rdfs:seeAlso file for the state will be removed. The entry in the + bundle's manifest.ttl is removed, and if this results in an empty manifest, + then the manifest file is removed. If this results in an empty bundle, then + the bundle directory is removed as well. + + @param world The world. + @param state State to remove from the system. +*/ +LILV_API +int +lilv_state_delete(LilvWorld* world, const LilvState* state); + +/** + @} + @defgroup lilv_scalepoint Scale Points + @{ +*/ + +/** + Get the label of this scale point (enumeration value). + + Returned value is owned by `point` and must not be freed. +*/ +LILV_API +const LilvNode* +lilv_scale_point_get_label(const LilvScalePoint* point); + +/** + Get the value of this scale point (enumeration value). + + Returned value is owned by `point` and must not be freed. +*/ +LILV_API +const LilvNode* +lilv_scale_point_get_value(const LilvScalePoint* point); + +/** + @} + @defgroup lilv_class Plugin Classes + @{ +*/ + +/** + Get the URI of this class' superclass. + + Returned value is owned by `plugin_class` and must not be freed by caller. + Returned value may be NULL, if class has no parent. +*/ +LILV_API +const LilvNode* +lilv_plugin_class_get_parent_uri(const LilvPluginClass* plugin_class); + +/** + Get the URI of this plugin class. + + Returned value is owned by `plugin_class` and must not be freed by caller. +*/ +LILV_API +const LilvNode* +lilv_plugin_class_get_uri(const LilvPluginClass* plugin_class); + +/** + Get the label of this plugin class, like "Oscillators". + + Returned value is owned by `plugin_class` and must not be freed by caller. +*/ +LILV_API +const LilvNode* +lilv_plugin_class_get_label(const LilvPluginClass* plugin_class); + +/** + Get the subclasses of this plugin class. + + Returned value must be freed by caller with lilv_plugin_classes_free(). +*/ +LILV_API +LilvPluginClasses* +lilv_plugin_class_get_children(const LilvPluginClass* plugin_class); + +/** + @} + @defgroup lilv_instance Plugin Instances + @{ +*/ + +/** + @cond LILV_DOCUMENT_INSTANCE_IMPL +*/ + +/* Instance of a plugin. + + This is exposed in the ABI to allow inlining of performance critical + functions like lilv_instance_run() (simple wrappers of functions in lv2.h). + This is for performance reasons, user code should not use this definition + in any way (which is why it is not machine documented). + Truly private implementation details are hidden via `pimpl`. +*/ +struct LilvInstanceImpl { + const LV2_Descriptor* lv2_descriptor; + LV2_Handle lv2_handle; + void* pimpl; +}; + +/** + @endcond +*/ + +/** + Instantiate a plugin. + + The returned value is a lightweight handle for an LV2 plugin instance, + it does not refer to `plugin`, or any other Lilv state. The caller must + eventually free it with lilv_instance_free(). + `features` is a NULL-terminated array of features the host supports. + NULL may be passed if the host supports no additional features. + + @return NULL if instantiation failed. +*/ +LILV_API +LilvInstance* +lilv_plugin_instantiate(const LilvPlugin* plugin, + double sample_rate, + const LV2_Feature* const* features); + +/** + Free a plugin instance. + + It is safe to call this function on NULL. + `instance` is invalid after this call. +*/ +LILV_API +void +lilv_instance_free(LilvInstance* instance); + +#ifndef LILV_INTERNAL + +/** + Get the URI of the plugin which `instance` is an instance of. + + Returned string is shared and must not be modified or deleted. +*/ +static inline const char* +lilv_instance_get_uri(const LilvInstance* instance) +{ + return instance->lv2_descriptor->URI; +} + +/** + Connect a port to a data location. + + This may be called regardless of whether the plugin is activated, + activation and deactivation does not destroy port connections. +*/ +static inline void +lilv_instance_connect_port(LilvInstance* instance, + uint32_t port_index, + void* data_location) +{ + instance->lv2_descriptor->connect_port( + instance->lv2_handle, port_index, data_location); +} + +/** + Activate a plugin instance. + + This resets all state information in the plugin, except for port data + locations (as set by lilv_instance_connect_port()). This MUST be called + before calling lilv_instance_run(). +*/ +static inline void +lilv_instance_activate(LilvInstance* instance) +{ + if (instance->lv2_descriptor->activate) { + instance->lv2_descriptor->activate(instance->lv2_handle); + } +} + +/** + Run `instance` for `sample_count` frames. + + If the hint lv2:hardRTCapable is set for this plugin, this function is + guaranteed not to block. +*/ +static inline void +lilv_instance_run(LilvInstance* instance, uint32_t sample_count) +{ + instance->lv2_descriptor->run(instance->lv2_handle, sample_count); +} + +/** + Deactivate a plugin instance. + + Note that to run the plugin after this you must activate it, which will + reset all state information (except port connections). +*/ +static inline void +lilv_instance_deactivate(LilvInstance* instance) +{ + if (instance->lv2_descriptor->deactivate) { + instance->lv2_descriptor->deactivate(instance->lv2_handle); + } +} + +/** + Get extension data from the plugin instance. + + The type and semantics of the data returned is specific to the particular + extension, though in all cases it is shared and must not be deleted. +*/ +static inline const void* +lilv_instance_get_extension_data(const LilvInstance* instance, const char* uri) +{ + if (instance->lv2_descriptor->extension_data) { + return instance->lv2_descriptor->extension_data(uri); + } + + return NULL; +} + +/** + Get the LV2_Descriptor of the plugin instance. + + Normally hosts should not need to access the LV2_Descriptor directly, + use the lilv_instance_* functions. + + The returned descriptor is shared and must not be deleted. +*/ +static inline const LV2_Descriptor* +lilv_instance_get_descriptor(const LilvInstance* instance) +{ + return instance->lv2_descriptor; +} + +/** + Get the LV2_Handle of the plugin instance. + + Normally hosts should not need to access the LV2_Handle directly, + use the lilv_instance_* functions. + + The returned handle is shared and must not be deleted. +*/ +static inline LV2_Handle +lilv_instance_get_handle(const LilvInstance* instance) +{ + return instance->lv2_handle; +} + +#endif /* LILV_INTERNAL */ + +/** + @} + @defgroup lilv_ui Plugin UIs + @{ +*/ + +/** + Get all UIs for `plugin`. + + Returned value must be freed by caller using lilv_uis_free(). +*/ +LILV_API +LilvUIs* +lilv_plugin_get_uis(const LilvPlugin* plugin); + +/** + Get the URI of a Plugin UI. + + @return A shared value which must not be modified or freed. +*/ +LILV_API +const LilvNode* +lilv_ui_get_uri(const LilvUI* ui); + +/** + Get the types (URIs of RDF classes) of a Plugin UI. + + Note that in most cases lilv_ui_is_supported() should be used, which avoids + the need to use this function (and type specific logic). + + @return A shared value which must not be modified or freed. +*/ +LILV_API +const LilvNodes* +lilv_ui_get_classes(const LilvUI* ui); + +/** + Check whether a plugin UI has a given type. + + @param ui The Plugin UI + @param class_uri The URI of the LV2 UI type to check this UI against +*/ +LILV_API +bool +lilv_ui_is_a(const LilvUI* ui, const LilvNode* class_uri); + +/** + Function to determine whether a UI type is supported. + + This is provided by the user and must return non-zero iff using a UI of type + `ui_type_uri` in a container of type `container_type_uri` is supported. +*/ +typedef unsigned (*LilvUISupportedFunc)(const char* container_type_uri, + const char* ui_type_uri); + +/** + Return true iff a Plugin UI is supported as a given widget type. + + @param ui The Plugin UI + + @param supported_func User provided supported predicate. + + @param container_type The widget type to host the UI within. + + @param ui_type (Output) If non-NULL, set to the native type of the UI + which is owned by `ui` and must not be freed by the caller. + + @return The embedding quality level returned by `supported_func`. +*/ +LILV_API +unsigned +lilv_ui_is_supported(const LilvUI* ui, + LilvUISupportedFunc supported_func, + const LilvNode* container_type, + const LilvNode** ui_type); + +/** + Get the URI for a Plugin UI's bundle. + + @return A shared value which must not be modified or freed. +*/ +LILV_API +const LilvNode* +lilv_ui_get_bundle_uri(const LilvUI* ui); + +/** + Get the URI for a Plugin UI's shared library. + + @return A shared value which must not be modified or freed. +*/ +LILV_API +const LilvNode* +lilv_ui_get_binary_uri(const LilvUI* ui); + +/** + @} + @} +*/ + +#ifdef __cplusplus +# if defined(__clang__) +# pragma clang diagnostic pop +# endif +} /* extern "C" */ +#endif + +#endif /* LILV_LILV_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/lilv/lilv/lilvmm.hpp b/modules/juce_audio_processors/format_types/lv2/lilv/lilv/lilvmm.hpp new file mode 100644 index 0000000000..0ac802dea4 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lilv/lilv/lilvmm.hpp @@ -0,0 +1,440 @@ +/* + Copyright 2007-2017 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef LILV_LILVMM_HPP +#define LILV_LILVMM_HPP + +#include "lilv/lilv.h" +#include "lv2/core/lv2.h" + +#include +#include + +namespace Lilv { + +#if defined(__clang__) +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wdeprecated-declarations" +#elif defined(__GNUC__) && __GNUC__ > 4 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif + +struct Instance; +struct Node; +struct Nodes; +struct Plugin; +struct PluginClass; +struct PluginClasses; +struct Plugins; +struct Port; +struct ScalePoint; +struct ScalePoints; +struct UI; +struct UIs; +struct World; + +LILV_DEPRECATED +static inline const char* +uri_to_path(const char* uri) +{ + return lilv_uri_to_path(uri); +} + +#if defined(__clang__) +# pragma clang diagnostic pop +#elif defined(__GNUC__) && __GNUC__ > 4 +# pragma GCC diagnostic pop +#endif + +#define LILV_WRAP0(RT, prefix, name) \ + inline RT name() { return lilv_##prefix##_##name(me); } + +#define LILV_WRAP0_VOID(prefix, name) \ + inline void name() { lilv_##prefix##_##name(me); } + +#define LILV_WRAP1(RT, prefix, name, T1, a1) \ + inline RT name(T1 a1) { return lilv_##prefix##_##name(me, a1); } + +#define LILV_WRAP1_VOID(prefix, name, T1, a1) \ + inline void name(T1 a1) { lilv_##prefix##_##name(me, a1); } + +#define LILV_WRAP2(RT, prefix, name, T1, a1, T2, a2) \ + inline RT name(T1 a1, T2 a2) { return lilv_##prefix##_##name(me, a1, a2); } + +#define LILV_WRAP3(RT, prefix, name, T1, a1, T2, a2, T3, a3) \ + inline RT name(T1 a1, T2 a2, T3 a3) \ + { \ + return lilv_##prefix##_##name(me, a1, a2, a3); \ + } + +#define LILV_WRAP2_VOID(prefix, name, T1, a1, T2, a2) \ + inline void name(T1 a1, T2 a2) { lilv_##prefix##_##name(me, a1, a2); } + +#ifndef SWIG +# define LILV_WRAP_CONVERSION(CT) \ + inline operator CT*() const { return me; } +#else +# define LILV_WRAP_CONVERSION(CT) +#endif + +struct Node { + inline Node(const LilvNode* node) + : me(lilv_node_duplicate(node)) + {} + + inline Node(const Node& copy) + : me(lilv_node_duplicate(copy.me)) + {} + + inline Node& operator=(const Node& rhs) + { + if (&rhs != this) { + lilv_node_free(me); + me = lilv_node_duplicate(rhs.me); + } + return *this; + } + + inline Node(Node&& other) noexcept + : me(other.me) + { + other.me = nullptr; + } + + inline Node& operator=(Node&& rhs) noexcept + { + if (&rhs != this) { + me = rhs.me; + rhs.me = nullptr; + } + return *this; + } + + inline ~Node() { lilv_node_free(me); } + + inline bool equals(const Node& other) const + { + return lilv_node_equals(me, other.me); + } + + inline bool operator==(const Node& other) const { return equals(other); } + + LILV_WRAP_CONVERSION(LilvNode); + + LILV_WRAP0(char*, node, get_turtle_token); + LILV_WRAP0(bool, node, is_uri); + LILV_WRAP0(const char*, node, as_uri); + LILV_WRAP0(bool, node, is_blank); + LILV_WRAP0(const char*, node, as_blank); + LILV_WRAP0(bool, node, is_literal); + LILV_WRAP0(bool, node, is_string); + LILV_WRAP0(const char*, node, as_string); + LILV_WRAP0(bool, node, is_float); + LILV_WRAP0(float, node, as_float); + LILV_WRAP0(bool, node, is_int); + LILV_WRAP0(int, node, as_int); + LILV_WRAP0(bool, node, is_bool); + LILV_WRAP0(bool, node, as_bool); + + LilvNode* me; +}; + +struct ScalePoint { + inline ScalePoint(const LilvScalePoint* c_obj) + : me(c_obj) + {} + + LILV_WRAP_CONVERSION(const LilvScalePoint); + + LILV_WRAP0(const LilvNode*, scale_point, get_label); + LILV_WRAP0(const LilvNode*, scale_point, get_value); + + const LilvScalePoint* me; +}; + +struct PluginClass { + inline PluginClass(const LilvPluginClass* c_obj) + : me(c_obj) + {} + + LILV_WRAP_CONVERSION(const LilvPluginClass); + + LILV_WRAP0(Node, plugin_class, get_parent_uri); + LILV_WRAP0(Node, plugin_class, get_uri); + LILV_WRAP0(Node, plugin_class, get_label); + LILV_WRAP0(LilvPluginClasses*, plugin_class, get_children); + + const LilvPluginClass* me; +}; + +#define LILV_WRAP_COLL(CT, ET, prefix) \ + inline CT(const Lilv##CT* c_obj) \ + : me(c_obj) \ + {} \ + LILV_WRAP_CONVERSION(const Lilv##CT); \ + LILV_WRAP0(unsigned, prefix, size); \ + LILV_WRAP1(ET, prefix, get, LilvIter*, i); \ + LILV_WRAP0(LilvIter*, prefix, begin); \ + LILV_WRAP1(LilvIter*, prefix, next, LilvIter*, i); \ + LILV_WRAP1(bool, prefix, is_end, LilvIter*, i); \ + const Lilv##CT* me; + +struct PluginClasses { + LILV_WRAP_COLL(PluginClasses, PluginClass, plugin_classes); + LILV_WRAP1(PluginClass, plugin_classes, get_by_uri, const LilvNode*, uri); +}; + +struct ScalePoints { + LILV_WRAP_COLL(ScalePoints, ScalePoint, scale_points); +}; + +struct Nodes { + LILV_WRAP_COLL(Nodes, Node, nodes); + LILV_WRAP1(bool, nodes, contains, const Node&, node); + LILV_WRAP0(Node, nodes, get_first); +}; + +struct UI { + inline UI(const LilvUI* c_obj) + : me(c_obj) + {} + + LILV_WRAP_CONVERSION(const LilvUI); + + LILV_WRAP0(const LilvNode*, ui, get_uri); + LILV_WRAP0(const LilvNode*, ui, get_bundle_uri); + LILV_WRAP0(const LilvNode*, ui, get_binary_uri); + LILV_WRAP0(const LilvNodes*, ui, get_classes); + /*LILV_WRAP3(bool, ui, is_supported, + LilvUISupportedFunc, supported_func, + const LilvNode*, container_type, + const LilvNode**, ui_type);*/ + LILV_WRAP1(bool, ui, is_a, const LilvNode*, class_uri); + + const LilvUI* me; +}; + +struct UIs { + LILV_WRAP_COLL(UIs, UI, uis); +}; + +struct Port { + inline Port(const LilvPlugin* p, const LilvPort* c_obj) + : parent(p) + , me(c_obj) + {} + + LILV_WRAP_CONVERSION(const LilvPort); + +#define LILV_PORT_WRAP0(RT, name) \ + inline RT name() { return lilv_port_##name(parent, me); } + +#define LILV_PORT_WRAP1(RT, name, T1, a1) \ + inline RT name(T1 a1) { return lilv_port_##name(parent, me, a1); } + + LILV_PORT_WRAP1(LilvNodes*, get_value, LilvNode*, predicate); + LILV_PORT_WRAP0(LilvNodes*, get_properties) + LILV_PORT_WRAP1(bool, has_property, LilvNode*, property_uri); + LILV_PORT_WRAP1(bool, supports_event, LilvNode*, event_uri); + LILV_PORT_WRAP0(const LilvNode*, get_symbol); + LILV_PORT_WRAP0(LilvNode*, get_name); + LILV_PORT_WRAP0(const LilvNodes*, get_classes); + LILV_PORT_WRAP1(bool, is_a, LilvNode*, port_class); + LILV_PORT_WRAP0(LilvScalePoints*, get_scale_points); + + // TODO: get_range (output parameters) + + const LilvPlugin* parent; + const LilvPort* me; +}; + +struct Plugin { + inline Plugin(const LilvPlugin* c_obj) + : me(c_obj) + {} + + LILV_WRAP_CONVERSION(const LilvPlugin); + + LILV_WRAP0(bool, plugin, verify); + LILV_WRAP0(Node, plugin, get_uri); + LILV_WRAP0(Node, plugin, get_bundle_uri); + LILV_WRAP0(Nodes, plugin, get_data_uris); + LILV_WRAP0(Node, plugin, get_library_uri); + LILV_WRAP0(Node, plugin, get_name); + LILV_WRAP0(PluginClass, plugin, get_class); + LILV_WRAP1(Nodes, plugin, get_value, const Node&, pred); + LILV_WRAP1(bool, plugin, has_feature, const Node&, feature_uri); + LILV_WRAP0(Nodes, plugin, get_supported_features); + LILV_WRAP0(Nodes, plugin, get_required_features); + LILV_WRAP0(Nodes, plugin, get_optional_features); + LILV_WRAP0(unsigned, plugin, get_num_ports); + LILV_WRAP0(bool, plugin, has_latency); + LILV_WRAP0(unsigned, plugin, get_latency_port_index); + LILV_WRAP0(Node, plugin, get_author_name); + LILV_WRAP0(Node, plugin, get_author_email); + LILV_WRAP0(Node, plugin, get_author_homepage); + LILV_WRAP0(bool, plugin, is_replaced); + LILV_WRAP0(Nodes, plugin, get_extension_data); + LILV_WRAP0(UIs, plugin, get_uis); + LILV_WRAP1(Nodes, plugin, get_related, const Node&, type); + + inline Port get_port_by_index(unsigned index) const + { + return Port(me, lilv_plugin_get_port_by_index(me, index)); + } + + inline Port get_port_by_symbol(LilvNode* symbol) const + { + return Port(me, lilv_plugin_get_port_by_symbol(me, symbol)); + } + + inline void get_port_ranges_float(float* min_values, + float* max_values, + float* def_values) const + { + return lilv_plugin_get_port_ranges_float( + me, min_values, max_values, def_values); + } + + inline unsigned get_num_ports_of_class(LilvNode* class_1, ...) const + { + va_list args; + va_start(args, class_1); + + const uint32_t count = + lilv_plugin_get_num_ports_of_class_va(me, class_1, args); + + va_end(args); + return count; + } + + const LilvPlugin* me; +}; + +struct Plugins { + LILV_WRAP_COLL(Plugins, Plugin, plugins); + LILV_WRAP1(Plugin, plugins, get_by_uri, const LilvNode*, uri); +}; + +struct Instance { + inline Instance(LilvInstance* instance) + : me(instance) + {} + + LILV_DEPRECATED + inline Instance(Plugin plugin, double sample_rate) + { + me = lilv_plugin_instantiate(plugin, sample_rate, nullptr); + } + + LILV_DEPRECATED inline Instance(Plugin plugin, + double sample_rate, + LV2_Feature* const* features) + { + me = lilv_plugin_instantiate(plugin, sample_rate, features); + } + + static inline Instance* create(Plugin plugin, + double sample_rate, + LV2_Feature* const* features) + { + LilvInstance* me = lilv_plugin_instantiate(plugin, sample_rate, features); + + return me ? new Instance(me) : nullptr; + } + + LILV_WRAP_CONVERSION(LilvInstance); + + LILV_WRAP2_VOID(instance, + connect_port, + unsigned, + port_index, + void*, + data_location); + + LILV_WRAP0_VOID(instance, activate); + LILV_WRAP1_VOID(instance, run, unsigned, sample_count); + LILV_WRAP0_VOID(instance, deactivate); + + inline const void* get_extension_data(const char* uri) const + { + return lilv_instance_get_extension_data(me, uri); + } + + inline const LV2_Descriptor* get_descriptor() const + { + return lilv_instance_get_descriptor(me); + } + + inline LV2_Handle get_handle() const { return lilv_instance_get_handle(me); } + + LilvInstance* me; +}; + +struct World { + inline World() + : me(lilv_world_new()) + {} + + inline ~World() { lilv_world_free(me); } + + World(const World&) = delete; + World& operator=(const World&) = delete; + + World(World&&) = delete; + World& operator=(World&&) = delete; + + inline LilvNode* new_uri(const char* uri) const + { + return lilv_new_uri(me, uri); + } + + inline LilvNode* new_string(const char* str) const + { + return lilv_new_string(me, str); + } + + inline LilvNode* new_int(int val) const { return lilv_new_int(me, val); } + + inline LilvNode* new_float(float val) const + { + return lilv_new_float(me, val); + } + + inline LilvNode* new_bool(bool val) const { return lilv_new_bool(me, val); } + + inline Nodes find_nodes(const LilvNode* subject, + const LilvNode* predicate, + const LilvNode* object) const + { + return lilv_world_find_nodes(me, subject, predicate, object); + } + + LILV_WRAP2_VOID(world, set_option, const char*, uri, LilvNode*, value); + LILV_WRAP0_VOID(world, load_all); + LILV_WRAP1_VOID(world, load_bundle, LilvNode*, bundle_uri); + LILV_WRAP0(const LilvPluginClass*, world, get_plugin_class); + LILV_WRAP0(const LilvPluginClasses*, world, get_plugin_classes); + LILV_WRAP0(Plugins, world, get_all_plugins); + LILV_WRAP1(int, world, load_resource, const LilvNode*, resource); + + LilvWorld* me; +}; + +} /* namespace Lilv */ + +#endif /* LILV_LILVMM_HPP */ diff --git a/modules/juce_audio_processors/format_types/lv2/lilv/src/collections.c b/modules/juce_audio_processors/format_types/lv2/lilv/src/collections.c new file mode 100644 index 0000000000..c2c752a840 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lilv/src/collections.c @@ -0,0 +1,240 @@ +/* + Copyright 2008-2019 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "lilv_internal.h" + +#include "lilv/lilv.h" +#include "sord/sord.h" +#include "zix/common.h" +#include "zix/tree.h" + +#include +#include +#include + +int +lilv_ptr_cmp(const void* a, const void* b, const void* user_data) +{ + (void)user_data; + + return (intptr_t)a - (intptr_t)b; +} + +int +lilv_resource_node_cmp(const void* a, const void* b, const void* user_data) +{ + (void)user_data; + + const SordNode* an = ((const LilvNode*)a)->node; + const SordNode* bn = ((const LilvNode*)b)->node; + + return (intptr_t)an - (intptr_t)bn; +} + +/* Generic collection functions */ + +static inline LilvCollection* +lilv_collection_new(ZixComparator cmp, ZixDestroyFunc destructor) +{ + return zix_tree_new(false, cmp, NULL, destructor); +} + +void +lilv_collection_free(LilvCollection* collection) +{ + if (collection) { + zix_tree_free((ZixTree*)collection); + } +} + +unsigned +lilv_collection_size(const LilvCollection* collection) +{ + return (collection ? zix_tree_size((const ZixTree*)collection) : 0); +} + +LilvIter* +lilv_collection_begin(const LilvCollection* collection) +{ + return collection ? (LilvIter*)zix_tree_begin((ZixTree*)collection) : NULL; +} + +void* +lilv_collection_get(const LilvCollection* collection, const LilvIter* i) +{ + (void)collection; + + return zix_tree_get((const ZixTreeIter*)i); +} + +/* Constructors */ + +LilvScalePoints* +lilv_scale_points_new(void) +{ + return lilv_collection_new(lilv_ptr_cmp, + (ZixDestroyFunc)lilv_scale_point_free); +} + +LilvNodes* +lilv_nodes_new(void) +{ + return lilv_collection_new(lilv_ptr_cmp, (ZixDestroyFunc)lilv_node_free); +} + +LilvUIs* +lilv_uis_new(void) +{ + return lilv_collection_new(lilv_header_compare_by_uri, + (ZixDestroyFunc)lilv_ui_free); +} + +LilvPluginClasses* +lilv_plugin_classes_new(void) +{ + return lilv_collection_new(lilv_header_compare_by_uri, + (ZixDestroyFunc)lilv_plugin_class_free); +} + +/* URI based accessors (for collections of things with URIs) */ + +const LilvPluginClass* +lilv_plugin_classes_get_by_uri(const LilvPluginClasses* classes, + const LilvNode* uri) +{ + return (LilvPluginClass*)lilv_collection_get_by_uri((const ZixTree*)classes, + uri); +} + +const LilvUI* +lilv_uis_get_by_uri(const LilvUIs* uis, const LilvNode* uri) +{ + return (LilvUI*)lilv_collection_get_by_uri((const ZixTree*)uis, uri); +} + +/* Plugins */ + +LilvPlugins* +lilv_plugins_new(void) +{ + return lilv_collection_new(lilv_header_compare_by_uri, NULL); +} + +const LilvPlugin* +lilv_plugins_get_by_uri(const LilvPlugins* plugins, const LilvNode* uri) +{ + return (LilvPlugin*)lilv_collection_get_by_uri((const ZixTree*)plugins, uri); +} + +/* Nodes */ + +bool +lilv_nodes_contains(const LilvNodes* nodes, const LilvNode* value) +{ + LILV_FOREACH (nodes, i, nodes) { + if (lilv_node_equals(lilv_nodes_get(nodes, i), value)) { + return true; + } + } + + return false; +} + +LilvNodes* +lilv_nodes_merge(const LilvNodes* a, const LilvNodes* b) +{ + LilvNodes* result = lilv_nodes_new(); + + LILV_FOREACH (nodes, i, a) { + zix_tree_insert( + (ZixTree*)result, lilv_node_duplicate(lilv_nodes_get(a, i)), NULL); + } + + LILV_FOREACH (nodes, i, b) { + zix_tree_insert( + (ZixTree*)result, lilv_node_duplicate(lilv_nodes_get(b, i)), NULL); + } + + return result; +} + +/* Iterator */ + +#define LILV_COLLECTION_IMPL(prefix, CT, ET) \ + \ + unsigned prefix##_size(const CT* collection) \ + { \ + return lilv_collection_size(collection); \ + } \ + \ + LilvIter* prefix##_begin(const CT* collection) \ + { \ + return lilv_collection_begin(collection); \ + } \ + \ + const ET* prefix##_get(const CT* collection, LilvIter* i) \ + { \ + return (ET*)lilv_collection_get(collection, i); \ + } \ + \ + LilvIter* prefix##_next(const CT* collection, LilvIter* i) \ + { \ + (void)collection; \ + return zix_tree_iter_next((ZixTreeIter*)i); \ + } \ + \ + bool prefix##_is_end(const CT* collection, LilvIter* i) \ + { \ + (void)collection; \ + return zix_tree_iter_is_end((ZixTreeIter*)i); \ + } + +LILV_COLLECTION_IMPL(lilv_plugin_classes, LilvPluginClasses, LilvPluginClass) +LILV_COLLECTION_IMPL(lilv_scale_points, LilvScalePoints, LilvScalePoint) +LILV_COLLECTION_IMPL(lilv_uis, LilvUIs, LilvUI) +LILV_COLLECTION_IMPL(lilv_nodes, LilvNodes, LilvNode) +LILV_COLLECTION_IMPL(lilv_plugins, LilvPlugins, LilvPlugin) + +void +lilv_plugin_classes_free(LilvPluginClasses* collection) +{ + lilv_collection_free(collection); +} + +void +lilv_scale_points_free(LilvScalePoints* collection) +{ + lilv_collection_free(collection); +} + +void +lilv_uis_free(LilvUIs* collection) +{ + lilv_collection_free(collection); +} + +void +lilv_nodes_free(LilvNodes* collection) +{ + lilv_collection_free(collection); +} + +LilvNode* +lilv_nodes_get_first(const LilvNodes* collection) +{ + return (LilvNode*)lilv_collection_get(collection, + lilv_collection_begin(collection)); +} diff --git a/modules/juce_audio_processors/format_types/lv2/lilv/src/filesystem.c b/modules/juce_audio_processors/format_types/lv2/lilv/src/filesystem.c new file mode 100644 index 0000000000..03614cebc1 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lilv/src/filesystem.c @@ -0,0 +1,564 @@ +/* + Copyright 2007-2021 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#define _POSIX_C_SOURCE 200809L /* for fileno */ +#define _BSD_SOURCE 1 /* for realpath, symlink */ +#define _DEFAULT_SOURCE 1 /* for realpath, symlink */ + +#ifdef __APPLE__ +# define _DARWIN_C_SOURCE 1 /* for flock */ +#endif + +#include "filesystem.h" +#include "lilv_config.h" +#include "lilv_internal.h" + +#ifdef _WIN32 +# include +# include +# include +# define F_OK 0 +# define mkdir(path, flags) _mkdir(path) +# define S_ISDIR(mode) (((mode)&S_IFMT) == S_IFDIR) +#else +# include +# include +#endif + +#if USE_FLOCK && USE_FILENO +# include +#endif + +#include + +#include +#include +#include +#include +#include + +#ifndef PAGE_SIZE +# define PAGE_SIZE 4096 +#endif + +static bool +lilv_is_dir_sep(const char c) +{ + return c == '/' || c == LILV_DIR_SEP[0]; +} + +#ifdef _WIN32 +static inline bool +is_windows_path(const char* path) +{ + return (isalpha(path[0]) && (path[1] == ':' || path[1] == '|') && + (path[2] == '/' || path[2] == '\\')); +} +#endif + +char* +lilv_temp_directory_path(void) +{ +#ifdef _WIN32 + DWORD len = GetTempPath(0, NULL); + char* buf = (char*)calloc(len, 1); + if (GetTempPath(len, buf) == 0) { + free(buf); + return NULL; + } + + return buf; +#else + const char* const tmpdir = getenv("TMPDIR"); + + return tmpdir ? lilv_strdup(tmpdir) : lilv_strdup("/tmp"); +#endif +} + +bool +lilv_path_is_absolute(const char* path) +{ + if (lilv_is_dir_sep(path[0])) { + return true; + } + +#ifdef _WIN32 + if (is_windows_path(path)) { + return true; + } +#endif + + return false; +} + +bool +lilv_path_is_child(const char* path, const char* dir) +{ + if (path && dir) { + const size_t path_len = strlen(path); + const size_t dir_len = strlen(dir); + return dir && path_len >= dir_len && !strncmp(path, dir, dir_len); + } + return false; +} + +char* +lilv_path_current(void) +{ + return getcwd(NULL, 0); +} + +char* +lilv_path_absolute(const char* path) +{ + if (lilv_path_is_absolute(path)) { + return lilv_strdup(path); + } + + char* cwd = getcwd(NULL, 0); + char* abs_path = lilv_path_join(cwd, path); + free(cwd); + return abs_path; +} + +char* +lilv_path_absolute_child(const char* path, const char* parent) +{ + if (lilv_path_is_absolute(path)) { + return lilv_strdup(path); + } + + return lilv_path_join(parent, path); +} + +char* +lilv_path_relative_to(const char* path, const char* base) +{ + const size_t path_len = strlen(path); + const size_t base_len = strlen(base); + const size_t min_len = (path_len < base_len) ? path_len : base_len; + + // Find the last separator common to both paths + size_t last_shared_sep = 0; + for (size_t i = 0; i < min_len && path[i] == base[i]; ++i) { + if (lilv_is_dir_sep(path[i])) { + last_shared_sep = i; + } + } + + if (last_shared_sep == 0) { + // No common components, return path + return lilv_strdup(path); + } + + // Find the number of up references ("..") required + size_t up = 0; + for (size_t i = last_shared_sep + 1; i < base_len; ++i) { + if (lilv_is_dir_sep(base[i])) { + ++up; + } + } + +#ifdef _WIN32 + const bool use_slash = strchr(path, '/'); +#else + static const bool use_slash = true; +#endif + + // Write up references + const size_t suffix_len = path_len - last_shared_sep; + char* rel = (char*)calloc(1, suffix_len + (up * 3) + 1); + for (size_t i = 0; i < up; ++i) { + if (use_slash) { + memcpy(rel + (i * 3), "../", 3); + } else { + memcpy(rel + (i * 3), "..\\", 3); + } + } + + // Write suffix + memcpy(rel + (up * 3), path + last_shared_sep + 1, suffix_len); + return rel; +} + +char* +lilv_path_parent(const char* path) +{ + const char* s = path + strlen(path) - 1; // Last character + + // Last non-slash + for (; s > path && lilv_is_dir_sep(*s); --s) { + } + + // Last internal slash + for (; s > path && !lilv_is_dir_sep(*s); --s) { + } + + // Skip duplicates + for (; s > path && lilv_is_dir_sep(*s); --s) { + } + + if (s == path) { // Hit beginning + return lilv_is_dir_sep(*s) ? lilv_strdup("/") : lilv_strdup("."); + } + + // Pointing to the last character of the result (inclusive) + char* dirname = (char*)malloc(s - path + 2); + memcpy(dirname, path, s - path + 1); + dirname[s - path + 1] = '\0'; + return dirname; +} + +char* +lilv_path_filename(const char* path) +{ + const size_t path_len = strlen(path); + size_t last_sep = path_len; + for (size_t i = 0; i < path_len; ++i) { + if (lilv_is_dir_sep(path[i])) { + last_sep = i; + } + } + + if (last_sep >= path_len) { + return lilv_strdup(path); + } + + const size_t ret_len = path_len - last_sep; + char* const ret = (char*)calloc(ret_len + 1, 1); + + strncpy(ret, path + last_sep + 1, ret_len); + return ret; +} + +char* +lilv_path_join(const char* a, const char* b) +{ + if (!a) { + return (b && b[0]) ? lilv_strdup(b) : NULL; + } + + const size_t a_len = strlen(a); + const size_t b_len = b ? strlen(b) : 0; + const bool a_end_is_sep = a_len > 0 && lilv_is_dir_sep(a[a_len - 1]); + const size_t pre_len = a_len - (a_end_is_sep ? 1 : 0); + char* path = (char*)calloc(1, a_len + b_len + 2); + memcpy(path, a, pre_len); + +#ifdef _WIN32 + // Use forward slash if it seems that the input paths do + const bool a_has_slash = strchr(a, '/'); + const bool b_has_slash = b && strchr(b, '/'); + if (a_has_slash || b_has_slash) { + path[pre_len] = '/'; + } else { + path[pre_len] = '\\'; + } +#else + path[pre_len] = '/'; +#endif + + if (b) { + memcpy(path + pre_len + 1, + b + (lilv_is_dir_sep(b[0]) ? 1 : 0), + lilv_is_dir_sep(b[0]) ? b_len - 1 : b_len); + } + return path; +} + +char* +lilv_path_canonical(const char* path) +{ + if (!path) { + return NULL; + } + +#if defined(_WIN32) + char* out = (char*)malloc(MAX_PATH); + GetFullPathName(path, MAX_PATH, out, NULL); + return out; +#else + char* real_path = realpath(path, NULL); + return real_path ? real_path : lilv_strdup(path); +#endif +} + +bool +lilv_path_exists(const char* path) +{ +#if USE_LSTAT + struct stat st; + return !lstat(path, &st); +#else + return !access(path, F_OK); +#endif +} + +bool +lilv_is_directory(const char* path) +{ + struct stat st; + return !stat(path, &st) && S_ISDIR(st.st_mode); +} + +int +lilv_copy_file(const char* src, const char* dst) +{ + FILE* in = fopen(src, "r"); + if (!in) { + return errno; + } + + FILE* out = fopen(dst, "w"); + if (!out) { + fclose(in); + return errno; + } + + char* page = (char*)malloc(PAGE_SIZE); + size_t n_read = 0; + int st = 0; + while ((n_read = fread(page, 1, PAGE_SIZE, in)) > 0) { + if (fwrite(page, 1, n_read, out) != n_read) { + st = errno; + break; + } + } + + if (!st && fflush(out)) { + st = errno; + } + + if (!st && (ferror(in) || ferror(out))) { + st = EBADF; + } + + free(page); + fclose(in); + fclose(out); + + return st; +} + +int +lilv_symlink(const char* oldpath, const char* newpath) +{ + int ret = 0; + if (strcmp(oldpath, newpath)) { +#ifdef _WIN32 + ret = !CreateHardLink(newpath, oldpath, 0); +#else + char* target = lilv_path_relative_to(oldpath, newpath); + + ret = symlink(target, newpath); + + free(target); +#endif + } + return ret; +} + +int +lilv_flock(FILE* file, bool lock, bool block) +{ +#ifdef _WIN32 + HANDLE handle = (HANDLE)_get_osfhandle(fileno(file)); + OVERLAPPED overlapped = {0}; + + if (lock) { + const DWORD flags = + (LOCKFILE_EXCLUSIVE_LOCK | (block ? 0 : LOCKFILE_FAIL_IMMEDIATELY)); + + return !LockFileEx(handle, flags, 0, UINT32_MAX, UINT32_MAX, &overlapped); + } else { + return !UnlockFileEx(handle, 0, UINT32_MAX, UINT32_MAX, &overlapped); + } +#elif USE_FLOCK && USE_FILENO + return flock(fileno(file), + (lock ? LOCK_EX : LOCK_UN) | (block ? 0 : LOCK_NB)); +#else + return 0; +#endif +} + +void +lilv_dir_for_each(const char* path, + void* data, + void (*f)(const char* path, const char* name, void* data)) +{ +#ifdef _WIN32 + char* pat = lilv_path_join(path, "*"); + WIN32_FIND_DATA fd; + HANDLE fh = FindFirstFile(pat, &fd); + if (fh != INVALID_HANDLE_VALUE) { + do { + if (strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..")) { + f(path, fd.cFileName, data); + } + } while (FindNextFile(fh, &fd)); + } + FindClose(fh); + free(pat); +#else + DIR* dir = opendir(path); + if (dir) { + for (struct dirent* entry = NULL; (entry = readdir(dir));) { + if (strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")) { + f(path, entry->d_name, data); + } + } + closedir(dir); + } +#endif +} + +char* +lilv_create_temporary_directory_in(const char* pattern, const char* parent) +{ +#ifdef _WIN32 + static const char chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + static const int n_chars = sizeof(chars) - 1; + + const size_t pattern_len = strlen(pattern); + if (pattern_len < 7 || strcmp(pattern + pattern_len - 6, "XXXXXX")) { + errno = EINVAL; + return NULL; + } + + char* const path_pattern = lilv_path_join(parent, pattern); + const size_t path_pattern_len = strlen(path_pattern); + char* const suffix = path_pattern + path_pattern_len - 6; + + for (unsigned attempt = 0; attempt < 128; ++attempt) { + for (unsigned i = 0; i < 6; ++i) { + suffix[i] = chars[rand() % n_chars]; + } + + if (!mkdir(path_pattern, 0700)) { + return path_pattern; + } + } + + return NULL; +#else + char* const path_pattern = lilv_path_join(parent, pattern); + + return mkdtemp(path_pattern); // NOLINT (not a leak) +#endif +} + +char* +lilv_create_temporary_directory(const char* pattern) +{ + char* const tmpdir = lilv_temp_directory_path(); + char* const result = lilv_create_temporary_directory_in(pattern, tmpdir); + + free(tmpdir); + + return result; +} + +int +lilv_create_directories(const char* dir_path) +{ + char* path = lilv_strdup(dir_path); + const size_t path_len = strlen(path); + size_t i = 1; + +#ifdef _WIN32 + if (is_windows_path(dir_path)) { + i = 3; + } +#endif + + for (; i <= path_len; ++i) { + const char c = path[i]; + if (c == LILV_DIR_SEP[0] || c == '/' || c == '\0') { + path[i] = '\0'; + if (mkdir(path, 0755) && (errno != EEXIST || !lilv_is_directory(path))) { + free(path); + return errno; + } + path[i] = c; + } + } + + free(path); + return 0; +} + +static off_t +lilv_file_size(const char* path) +{ + struct stat buf; + if (stat(path, &buf)) { + return 0; + } + return buf.st_size; +} + +int +lilv_remove(const char* path) +{ +#ifdef _WIN32 + if (lilv_is_directory(path)) { + return !RemoveDirectory(path); + } +#endif + + return remove(path); +} + +bool +lilv_file_equals(const char* a_path, const char* b_path) +{ + if (!strcmp(a_path, b_path)) { + return true; // Paths match + } + + bool match = false; + FILE* a_file = NULL; + FILE* b_file = NULL; + char* const a_real = lilv_path_canonical(a_path); + char* const b_real = lilv_path_canonical(b_path); + if (!strcmp(a_real, b_real)) { + match = true; // Real paths match + } else if (lilv_file_size(a_path) != lilv_file_size(b_path)) { + match = false; // Sizes differ + } else if (!(a_file = fopen(a_real, "rb")) || + !(b_file = fopen(b_real, "rb"))) { + match = false; // Missing file matches nothing + } else { + // TODO: Improve performance by reading chunks + match = true; + while (!feof(a_file) && !feof(b_file)) { + if (fgetc(a_file) != fgetc(b_file)) { + match = false; + break; + } + } + } + + if (a_file) { + fclose(a_file); + } + if (b_file) { + fclose(b_file); + } + free(a_real); + free(b_real); + return match; +} diff --git a/modules/juce_audio_processors/format_types/lv2/lilv/src/filesystem.h b/modules/juce_audio_processors/format_types/lv2/lilv/src/filesystem.h new file mode 100644 index 0000000000..1a8dc68039 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lilv/src/filesystem.h @@ -0,0 +1,182 @@ +/* + Copyright 2007-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include +#include + +/// Return the path to a directory suitable for making temporary files +char* +lilv_temp_directory_path(void); + +/// Return true iff `path` is an absolute path +bool +lilv_path_is_absolute(const char* path); + +/// Return true iff `path` is a child of `dir` +bool +lilv_path_is_child(const char* path, const char* dir); + +/// Return the current working directory +char* +lilv_path_current(void); + +/** + Return `path` as an absolute path. + + If `path` is absolute, an identical copy of it is returned. Otherwise, the + returned path is relative to the current working directory. +*/ +char* +lilv_path_absolute(const char* path); + +/** + Return `path` as an absolute path relative to `parent`. + + If `path` is absolute, an identical copy of it is returned. Otherwise, the + returned path is relative to `parent`. +*/ +char* +lilv_path_absolute_child(const char* path, const char* parent); + +/** + Return `path` relative to `base` if possible. + + If `path` is not within `base`, a copy is returned. Otherwise, an + equivalent path relative to `base` is returned (which may contain + up-references). +*/ +char* +lilv_path_relative_to(const char* path, const char* base); + +/** + Return the path to the directory that contains `path`. + + Returns the root path if `path` is the root path. +*/ +char* +lilv_path_parent(const char* path); + +/** + Return the filename component of `path` without any directories. + + Returns the empty string if `path` is the root path. +*/ +char* +lilv_path_filename(const char* path); + +/// Join path `a` and path `b` with a single directory separator between them +char* +lilv_path_join(const char* a, const char* b); + +/** + Return `path` as a canonicalized absolute path. + + This expands all symbolic links, relative references, and removes extra + directory separators. +*/ +char* +lilv_path_canonical(const char* path); + +/// Return true iff `path` points to an existing file system entry +bool +lilv_path_exists(const char* path); + +/// Return true iff `path` points to an existing directory +bool +lilv_is_directory(const char* path); + +/** + Copy the file at path `src` to path `dst`. + + @return Zero on success, or a standard `errno` error code. +*/ +int +lilv_copy_file(const char* src, const char* dst); + +/** + Create a symlink at `newpath` that points to `oldpath`. + + @return Zero on success, otherwise non-zero and `errno` is set. +*/ +int +lilv_symlink(const char* oldpath, const char* newpath); + +/** + Set or remove an advisory exclusive lock on `file`. + + If the `lock` is true and the file is already locked by another process, or + by this process via a different file handle, then this will not succeed and + non-zero will be returned. + + @param file Handle for open file to lock. + @param lock True to set lock, false to release lock. + @param block If true, then this call will block until the lock is acquired. + @return Zero on success. +*/ +int +lilv_flock(FILE* file, bool lock, bool block); + +/** + Visit every file in the directory at `path`. + + @param path A path to a directory. + + @param data Opaque user data that is passed to `f`. + + @param f A function called on every entry in the directory. The `path` + parameter is always the directory path passed to this function, the `name` + parameter is the name of the directory entry (not its full path). +*/ +void +lilv_dir_for_each(const char* path, + void* data, + void (*f)(const char* path, const char* name, void* data)); + +/** + Create a unique temporary directory in a specific directory. + + The last six characters of `pattern` must be `XXXXXX` and will be replaced + with random characters. This works roughly like mkdtemp, except the pattern + should only be a directory name, not a full path. The created path will be + a child of the given parent directory. +*/ +char* +lilv_create_temporary_directory_in(const char* pattern, const char* parent); + +/** + Create a unique temporary directory. + + This is like lilv_create_temporary_directory_in(), except it creates the + directory in the system temporary directory. +*/ +char* +lilv_create_temporary_directory(const char* pattern); + +/** + Create the directory `dir_path` and any parent directories if necessary. + + @return Zero on success, or an `errno` error code. +*/ +int +lilv_create_directories(const char* dir_path); + +/// Remove the file or empty directory at `path` +int +lilv_remove(const char* path); + +/// Return true iff the given paths point to files with identical contents +bool +lilv_file_equals(const char* a_path, const char* b_path); diff --git a/modules/juce_audio_processors/format_types/lv2/lilv/src/instance.c b/modules/juce_audio_processors/format_types/lv2/lilv/src/instance.c new file mode 100644 index 0000000000..6a2b856eb1 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lilv/src/instance.c @@ -0,0 +1,115 @@ +/* + Copyright 2007-2019 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "lilv_internal.h" + +#include "lilv/lilv.h" +#include "lv2/core/lv2.h" +#include "serd/serd.h" + +#include +#include +#include +#include +#include + +LilvInstance* +lilv_plugin_instantiate(const LilvPlugin* plugin, + double sample_rate, + const LV2_Feature* const* features) +{ + lilv_plugin_load_if_necessary(plugin); + if (plugin->parse_errors) { + return NULL; + } + + LilvInstance* result = NULL; + const LilvNode* const lib_uri = lilv_plugin_get_library_uri(plugin); + const LilvNode* const bundle_uri = lilv_plugin_get_bundle_uri(plugin); + if (!lib_uri || !bundle_uri) { + return NULL; + } + + char* const bundle_path = + lilv_file_uri_parse(lilv_node_as_uri(bundle_uri), NULL); + + LilvLib* lib = lilv_lib_open(plugin->world, lib_uri, bundle_path, features); + if (!lib) { + serd_free(bundle_path); + return NULL; + } + + const LV2_Feature** local_features = NULL; + if (features == NULL) { + local_features = (const LV2_Feature**)malloc(sizeof(LV2_Feature*)); + local_features[0] = NULL; + } + + // Search for plugin by URI + for (uint32_t i = 0; true; ++i) { + const LV2_Descriptor* ld = lilv_lib_get_plugin(lib, i); + if (!ld) { + LILV_ERRORF("No plugin <%s> in <%s>\n", + lilv_node_as_uri(lilv_plugin_get_uri(plugin)), + lilv_node_as_uri(lib_uri)); + lilv_lib_close(lib); + break; // return NULL + } + + if (!strcmp(ld->URI, lilv_node_as_uri(lilv_plugin_get_uri(plugin)))) { + // Create LilvInstance to return + result = (LilvInstance*)malloc(sizeof(LilvInstance)); + result->lv2_descriptor = ld; + result->lv2_handle = ld->instantiate( + ld, sample_rate, bundle_path, (features) ? features : local_features); + result->pimpl = lib; + break; + } + } + + free(local_features); + serd_free(bundle_path); + + if (result) { + if (result->lv2_handle == NULL) { + // Failed to instantiate + free(result); + lilv_lib_close(lib); + return NULL; + } + + // "Connect" all ports to NULL (catches bugs) + for (uint32_t i = 0; i < lilv_plugin_get_num_ports(plugin); ++i) { + result->lv2_descriptor->connect_port(result->lv2_handle, i, NULL); + } + } + + return result; +} + +void +lilv_instance_free(LilvInstance* instance) +{ + if (!instance) { + return; + } + + instance->lv2_descriptor->cleanup(instance->lv2_handle); + instance->lv2_descriptor = NULL; + lilv_lib_close((LilvLib*)instance->pimpl); + instance->pimpl = NULL; + free(instance); +} diff --git a/modules/juce_audio_processors/format_types/lv2/lilv/src/lib.c b/modules/juce_audio_processors/format_types/lv2/lilv/src/lib.c new file mode 100644 index 0000000000..adba732054 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lilv/src/lib.c @@ -0,0 +1,127 @@ +/* + Copyright 2012-2019 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "lilv_internal.h" + +#include "lilv/lilv.h" +#include "lv2/core/lv2.h" +#include "serd/serd.h" +#include "zix/tree.h" + +#ifndef _WIN32 +# include +#endif + +#include +#include + +LilvLib* +lilv_lib_open(LilvWorld* world, + const LilvNode* uri, + const char* bundle_path, + const LV2_Feature* const* features) +{ + ZixTreeIter* i = NULL; + const LilvLib key = { + world, (LilvNode*)uri, (char*)bundle_path, NULL, NULL, NULL, 0}; + if (!zix_tree_find(world->libs, &key, &i)) { + LilvLib* llib = (LilvLib*)zix_tree_get(i); + ++llib->refs; + return llib; + } + + const char* const lib_uri = lilv_node_as_uri(uri); + char* const lib_path = + (char*)serd_file_uri_parse((const uint8_t*)lib_uri, NULL); + if (!lib_path) { + return NULL; + } + + dlerror(); + void* lib = dlopen(lib_path, RTLD_NOW); + if (!lib) { + LILV_ERRORF("Failed to open library %s (%s)\n", lib_path, dlerror()); + serd_free(lib_path); + return NULL; + } + + LV2_Descriptor_Function df = + (LV2_Descriptor_Function)lilv_dlfunc(lib, "lv2_descriptor"); + + LV2_Lib_Descriptor_Function ldf = + (LV2_Lib_Descriptor_Function)lilv_dlfunc(lib, "lv2_lib_descriptor"); + + const LV2_Lib_Descriptor* desc = NULL; + if (ldf) { + desc = ldf(bundle_path, features); + if (!desc) { + LILV_ERRORF("Call to %s:lv2_lib_descriptor failed\n", lib_path); + dlclose(lib); + serd_free(lib_path); + return NULL; + } + } else if (!df) { + LILV_ERRORF("No `lv2_descriptor' or `lv2_lib_descriptor' in %s\n", + lib_path); + dlclose(lib); + serd_free(lib_path); + return NULL; + } + serd_free(lib_path); + + LilvLib* llib = (LilvLib*)malloc(sizeof(LilvLib)); + llib->world = world; + llib->uri = lilv_node_duplicate(uri); + llib->bundle_path = lilv_strdup(bundle_path); + llib->lib = lib; + llib->lv2_descriptor = df; + llib->desc = desc; + llib->refs = 1; + + zix_tree_insert(world->libs, llib, NULL); + return llib; +} + +const LV2_Descriptor* +lilv_lib_get_plugin(LilvLib* lib, uint32_t index) +{ + if (lib->lv2_descriptor) { + return lib->lv2_descriptor(index); + } + + if (lib->desc) { + return lib->desc->get_plugin(lib->desc->handle, index); + } + + return NULL; +} + +void +lilv_lib_close(LilvLib* lib) +{ + if (--lib->refs == 0) { + dlclose(lib->lib); + + ZixTreeIter* i = NULL; + if (lib->world->libs && !zix_tree_find(lib->world->libs, lib, &i)) { + zix_tree_remove(lib->world->libs, i); + } + + lilv_node_free(lib->uri); + free(lib->bundle_path); + free(lib); + } +} diff --git a/modules/juce_audio_processors/format_types/lv2/lilv/src/lilv_internal.h b/modules/juce_audio_processors/format_types/lv2/lilv/src/lilv_internal.h new file mode 100644 index 0000000000..12b56de0b8 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lilv/src/lilv_internal.h @@ -0,0 +1,477 @@ +/* + Copyright 2007-2019 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef LILV_INTERNAL_H +#define LILV_INTERNAL_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "lilv_config.h" // IWYU pragma: keep + +#include "lilv/lilv.h" +#include "lv2/core/lv2.h" +#include "serd/serd.h" +#include "sord/sord.h" +#include "zix/tree.h" + +#include +#include +#include + +#ifdef _WIN32 +# include +# include +# include +# define dlopen(path, flags) LoadLibrary(path) +# define dlclose(lib) FreeLibrary((HMODULE)lib) +# ifdef _MSC_VER +# define __func__ __FUNCTION__ +# ifndef snprintf +# define snprintf _snprintf +# endif +# endif +# ifndef INFINITY +# define INFINITY DBL_MAX + DBL_MAX +# endif +# ifndef NAN +# define NAN INFINITY - INFINITY +# endif +static inline const char* +dlerror(void) +{ + return "Unknown error"; +} +#else +# include +#endif + +#ifdef LILV_DYN_MANIFEST +# include "lv2/dynmanifest/dynmanifest.h" +#endif + +/* + * + * Types + * + */ + +typedef void LilvCollection; + +struct LilvPortImpl { + LilvNode* node; ///< RDF node + uint32_t index; ///< lv2:index + LilvNode* symbol; ///< lv2:symbol + LilvNodes* classes; ///< rdf:type +}; + +typedef struct LilvSpecImpl { + SordNode* spec; + SordNode* bundle; + LilvNodes* data_uris; + struct LilvSpecImpl* next; +} LilvSpec; + +/** + Header of an LilvPlugin, LilvPluginClass, or LilvUI. + Any of these structs may be safely casted to LilvHeader, which is used to + implement collections using the same comparator. +*/ +struct LilvHeader { + LilvWorld* world; + LilvNode* uri; +}; + +#ifdef LILV_DYN_MANIFEST +typedef struct { + LilvNode* bundle; + void* lib; + LV2_Dyn_Manifest_Handle handle; + uint32_t refs; +} LilvDynManifest; +#endif + +typedef struct { + LilvWorld* world; + LilvNode* uri; + char* bundle_path; + void* lib; + LV2_Descriptor_Function lv2_descriptor; + const LV2_Lib_Descriptor* desc; + uint32_t refs; +} LilvLib; + +struct LilvPluginImpl { + LilvWorld* world; + LilvNode* plugin_uri; + LilvNode* bundle_uri; ///< Bundle plugin was loaded from + LilvNode* binary_uri; ///< lv2:binary +#ifdef LILV_DYN_MANIFEST + LilvDynManifest* dynmanifest; +#endif + const LilvPluginClass* plugin_class; + LilvNodes* data_uris; ///< rdfs::seeAlso + LilvPort** ports; + uint32_t num_ports; + bool loaded; + bool parse_errors; + bool replaced; +}; + +struct LilvPluginClassImpl { + LilvWorld* world; + LilvNode* uri; + LilvNode* parent_uri; + LilvNode* label; +}; + +struct LilvInstancePimpl { + LilvWorld* world; + LilvLib* lib; +}; + +typedef struct { + bool dyn_manifest; + bool filter_language; + char* lv2_path; +} LilvOptions; + +struct LilvWorldImpl { + SordWorld* world; + SordModel* model; + SerdReader* reader; + unsigned n_read_files; + LilvPluginClass* lv2_plugin_class; + LilvPluginClasses* plugin_classes; + LilvSpec* specs; + LilvPlugins* plugins; + LilvPlugins* zombies; + LilvNodes* loaded_files; + ZixTree* libs; + struct { + SordNode* dc_replaces; + SordNode* dman_DynManifest; + SordNode* doap_name; + SordNode* lv2_Plugin; + SordNode* lv2_Specification; + SordNode* lv2_appliesTo; + SordNode* lv2_binary; + SordNode* lv2_default; + SordNode* lv2_designation; + SordNode* lv2_extensionData; + SordNode* lv2_index; + SordNode* lv2_latency; + SordNode* lv2_maximum; + SordNode* lv2_microVersion; + SordNode* lv2_minimum; + SordNode* lv2_minorVersion; + SordNode* lv2_name; + SordNode* lv2_optionalFeature; + SordNode* lv2_port; + SordNode* lv2_portProperty; + SordNode* lv2_reportsLatency; + SordNode* lv2_requiredFeature; + SordNode* lv2_symbol; + SordNode* lv2_prototype; + SordNode* owl_Ontology; + SordNode* pset_value; + SordNode* rdf_a; + SordNode* rdf_value; + SordNode* rdfs_Class; + SordNode* rdfs_label; + SordNode* rdfs_seeAlso; + SordNode* rdfs_subClassOf; + SordNode* xsd_base64Binary; + SordNode* xsd_boolean; + SordNode* xsd_decimal; + SordNode* xsd_double; + SordNode* xsd_integer; + SordNode* null_uri; + } uris; + LilvOptions opt; +}; + +typedef enum { + LILV_VALUE_URI, + LILV_VALUE_STRING, + LILV_VALUE_INT, + LILV_VALUE_FLOAT, + LILV_VALUE_BOOL, + LILV_VALUE_BLANK, + LILV_VALUE_BLOB +} LilvNodeType; + +struct LilvNodeImpl { + LilvWorld* world; + SordNode* node; + LilvNodeType type; + union { + int int_val; + float float_val; + bool bool_val; + } val; +}; + +struct LilvScalePointImpl { + LilvNode* value; + LilvNode* label; +}; + +struct LilvUIImpl { + LilvWorld* world; + LilvNode* uri; + LilvNode* bundle_uri; + LilvNode* binary_uri; + LilvNodes* classes; +}; + +typedef struct LilvVersion { + int minor; + int micro; +} LilvVersion; + +/* + * + * Functions + * + */ + +LilvPort* +lilv_port_new(LilvWorld* world, + const SordNode* node, + uint32_t index, + const char* symbol); +void +lilv_port_free(const LilvPlugin* plugin, LilvPort* port); + +LilvPlugin* +lilv_plugin_new(LilvWorld* world, LilvNode* uri, LilvNode* bundle_uri); + +void +lilv_plugin_clear(LilvPlugin* plugin, LilvNode* bundle_uri); + +void +lilv_plugin_load_if_necessary(const LilvPlugin* plugin); + +void +lilv_plugin_free(LilvPlugin* plugin); + +LilvNode* +lilv_plugin_get_unique(const LilvPlugin* plugin, + const SordNode* subject, + const SordNode* predicate); + +void +lilv_collection_free(LilvCollection* collection); + +unsigned +lilv_collection_size(const LilvCollection* collection); + +LilvIter* +lilv_collection_begin(const LilvCollection* collection); + +void* +lilv_collection_get(const LilvCollection* collection, const LilvIter* i); + +LilvPluginClass* +lilv_plugin_class_new(LilvWorld* world, + const SordNode* parent_node, + const SordNode* uri, + const char* label); + +void +lilv_plugin_class_free(LilvPluginClass* plugin_class); + +LilvLib* +lilv_lib_open(LilvWorld* world, + const LilvNode* uri, + const char* bundle_path, + const LV2_Feature* const* features); + +const LV2_Descriptor* +lilv_lib_get_plugin(LilvLib* lib, uint32_t index); + +void +lilv_lib_close(LilvLib* lib); + +LilvNodes* +lilv_nodes_new(void); + +LilvPlugins* +lilv_plugins_new(void); + +LilvScalePoints* +lilv_scale_points_new(void); + +LilvPluginClasses* +lilv_plugin_classes_new(void); + +LilvUIs* +lilv_uis_new(void); + +LilvNode* +lilv_world_get_manifest_uri(LilvWorld* world, const LilvNode* bundle_uri); + +const uint8_t* +lilv_world_blank_node_prefix(LilvWorld* world); + +SerdStatus +lilv_world_load_file(LilvWorld* world, SerdReader* reader, const LilvNode* uri); + +SerdStatus +lilv_world_load_graph(LilvWorld* world, SordNode* graph, const LilvNode* uri); + +LilvUI* +lilv_ui_new(LilvWorld* world, + LilvNode* uri, + LilvNode* type_uri, + LilvNode* binary_uri); + +void +lilv_ui_free(LilvUI* ui); + +LilvNode* +lilv_node_new(LilvWorld* world, LilvNodeType type, const char* str); + +LilvNode* +lilv_node_new_from_node(LilvWorld* world, const SordNode* node); + +int +lilv_header_compare_by_uri(const void* a, const void* b, const void* user_data); + +int +lilv_lib_compare(const void* a, const void* b, const void* user_data); + +int +lilv_ptr_cmp(const void* a, const void* b, const void* user_data); + +int +lilv_resource_node_cmp(const void* a, const void* b, const void* user_data); + +static inline int +lilv_version_cmp(const LilvVersion* a, const LilvVersion* b) +{ + if (a->minor == b->minor && a->micro == b->micro) { + return 0; + } + + if ((a->minor < b->minor) || (a->minor == b->minor && a->micro < b->micro)) { + return -1; + } + + return 1; +} + +struct LilvHeader* +lilv_collection_get_by_uri(const ZixTree* seq, const LilvNode* uri); + +LilvScalePoint* +lilv_scale_point_new(LilvNode* value, LilvNode* label); + +void +lilv_scale_point_free(LilvScalePoint* point); + +SordIter* +lilv_world_query_internal(LilvWorld* world, + const SordNode* subject, + const SordNode* predicate, + const SordNode* object); + +bool +lilv_world_ask_internal(LilvWorld* world, + const SordNode* subject, + const SordNode* predicate, + const SordNode* object); + +LilvNodes* +lilv_world_find_nodes_internal(LilvWorld* world, + const SordNode* subject, + const SordNode* predicate, + const SordNode* object); + +SordModel* +lilv_world_filter_model(LilvWorld* world, + SordModel* model, + const SordNode* subject, + const SordNode* predicate, + const SordNode* object, + const SordNode* graph); + +#define FOREACH_MATCH(iter) for (; !sord_iter_end(iter); sord_iter_next(iter)) + +LilvNodes* +lilv_nodes_from_stream_objects(LilvWorld* world, + SordIter* stream, + SordQuadIndex field); + +char* +lilv_strjoin(const char* first, ...); + +char* +lilv_strdup(const char* str); + +char* +lilv_get_lang(void); + +char* +lilv_expand(const char* path); + +char* +lilv_get_latest_copy(const char* path, const char* copy_path); + +char* +lilv_find_free_path(const char* in_path, + bool (*exists)(const char*, const void*), + const void* user_data); + +typedef void (*LilvVoidFunc)(void); + +/** dlsym wrapper to return a function pointer (without annoying warning) */ +static inline LilvVoidFunc +lilv_dlfunc(void* handle, const char* symbol) +{ +#ifdef _WIN32 + return (LilvVoidFunc)GetProcAddress((HMODULE)handle, symbol); +#else + typedef LilvVoidFunc (*VoidFuncGetter)(void*, const char*); + VoidFuncGetter dlfunc = (VoidFuncGetter)dlsym; + return dlfunc(handle, symbol); +#endif +} + +#ifdef LILV_DYN_MANIFEST +static const LV2_Feature* const dman_features = {NULL}; + +void +lilv_dynmanifest_free(LilvDynManifest* dynmanifest); +#endif + +#define LILV_ERROR(str) fprintf(stderr, "%s(): error: " str, __func__) +#define LILV_ERRORF(fmt, ...) \ + fprintf(stderr, "%s(): error: " fmt, __func__, __VA_ARGS__) +#define LILV_WARN(str) fprintf(stderr, "%s(): warning: " str, __func__) +#define LILV_WARNF(fmt, ...) \ + fprintf(stderr, "%s(): warning: " fmt, __func__, __VA_ARGS__) +#define LILV_NOTE(str) fprintf(stderr, "%s(): note: " str, __func__) +#define LILV_NOTEF(fmt, ...) \ + fprintf(stderr, "%s(): note: " fmt, __func__, __VA_ARGS__) + +#ifdef __cplusplus +} +#endif + +#endif /* LILV_INTERNAL_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/lilv/src/node.c b/modules/juce_audio_processors/format_types/lv2/lilv/src/node.c new file mode 100644 index 0000000000..ac47e80040 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lilv/src/node.c @@ -0,0 +1,413 @@ +/* + Copyright 2007-2019 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "filesystem.h" +#include "lilv_internal.h" + +#include "lilv/lilv.h" +#include "serd/serd.h" +#include "sord/sord.h" + +#include +#include +#include +#include +#include +#include + +static void +lilv_node_set_numerics_from_string(LilvNode* val) +{ + const char* str = (const char*)sord_node_get_string(val->node); + + switch (val->type) { + case LILV_VALUE_URI: + case LILV_VALUE_BLANK: + case LILV_VALUE_STRING: + case LILV_VALUE_BLOB: + break; + case LILV_VALUE_INT: + val->val.int_val = strtol(str, NULL, 10); + break; + case LILV_VALUE_FLOAT: + val->val.float_val = serd_strtod(str, NULL); + break; + case LILV_VALUE_BOOL: + val->val.bool_val = !strcmp(str, "true"); + break; + } +} + +/** Note that if `type` is numeric or boolean, the returned value is corrupt + * until lilv_node_set_numerics_from_string is called. It is not + * automatically called from here to avoid overhead and imprecision when the + * exact string value is known. + */ +LilvNode* +lilv_node_new(LilvWorld* world, LilvNodeType type, const char* str) +{ + LilvNode* val = (LilvNode*)malloc(sizeof(LilvNode)); + val->world = world; + val->type = type; + + const uint8_t* ustr = (const uint8_t*)str; + switch (type) { + case LILV_VALUE_URI: + val->node = sord_new_uri(world->world, ustr); + break; + case LILV_VALUE_BLANK: + val->node = sord_new_blank(world->world, ustr); + break; + case LILV_VALUE_STRING: + val->node = sord_new_literal(world->world, NULL, ustr, NULL); + break; + case LILV_VALUE_INT: + val->node = + sord_new_literal(world->world, world->uris.xsd_integer, ustr, NULL); + break; + case LILV_VALUE_FLOAT: + val->node = + sord_new_literal(world->world, world->uris.xsd_decimal, ustr, NULL); + break; + case LILV_VALUE_BOOL: + val->node = + sord_new_literal(world->world, world->uris.xsd_boolean, ustr, NULL); + break; + case LILV_VALUE_BLOB: + val->node = + sord_new_literal(world->world, world->uris.xsd_base64Binary, ustr, NULL); + break; + } + + if (!val->node) { + free(val); + return NULL; + } + + return val; +} + +/** Create a new LilvNode from `node`, or return NULL if impossible */ +LilvNode* +lilv_node_new_from_node(LilvWorld* world, const SordNode* node) +{ + if (!node) { + return NULL; + } + + LilvNode* result = NULL; + SordNode* datatype_uri = NULL; + LilvNodeType type = LILV_VALUE_STRING; + + switch (sord_node_get_type(node)) { + case SORD_URI: + result = (LilvNode*)malloc(sizeof(LilvNode)); + result->world = world; + result->type = LILV_VALUE_URI; + result->node = sord_node_copy(node); + break; + case SORD_BLANK: + result = (LilvNode*)malloc(sizeof(LilvNode)); + result->world = world; + result->type = LILV_VALUE_BLANK; + result->node = sord_node_copy(node); + break; + case SORD_LITERAL: + datatype_uri = sord_node_get_datatype(node); + if (datatype_uri) { + if (sord_node_equals(datatype_uri, world->uris.xsd_boolean)) { + type = LILV_VALUE_BOOL; + } else if (sord_node_equals(datatype_uri, world->uris.xsd_decimal) || + sord_node_equals(datatype_uri, world->uris.xsd_double)) { + type = LILV_VALUE_FLOAT; + } else if (sord_node_equals(datatype_uri, world->uris.xsd_integer)) { + type = LILV_VALUE_INT; + } else if (sord_node_equals(datatype_uri, world->uris.xsd_base64Binary)) { + type = LILV_VALUE_BLOB; + } else { + LILV_ERRORF("Unknown datatype `%s'\n", + sord_node_get_string(datatype_uri)); + } + } + result = + lilv_node_new(world, type, (const char*)sord_node_get_string(node)); + lilv_node_set_numerics_from_string(result); + break; + } + + return result; +} + +LilvNode* +lilv_new_uri(LilvWorld* world, const char* uri) +{ + return lilv_node_new(world, LILV_VALUE_URI, uri); +} + +LilvNode* +lilv_new_file_uri(LilvWorld* world, const char* host, const char* path) +{ + char* abs_path = lilv_path_absolute(path); + SerdNode s = serd_node_new_file_uri( + (const uint8_t*)abs_path, (const uint8_t*)host, NULL, true); + + LilvNode* ret = lilv_node_new(world, LILV_VALUE_URI, (const char*)s.buf); + serd_node_free(&s); + free(abs_path); + return ret; +} + +LilvNode* +lilv_new_string(LilvWorld* world, const char* str) +{ + return lilv_node_new(world, LILV_VALUE_STRING, str); +} + +LilvNode* +lilv_new_int(LilvWorld* world, int val) +{ + char str[32]; + snprintf(str, sizeof(str), "%d", val); + LilvNode* ret = lilv_node_new(world, LILV_VALUE_INT, str); + if (ret) { + ret->val.int_val = val; + } + return ret; +} + +LilvNode* +lilv_new_float(LilvWorld* world, float val) +{ + char str[32]; + snprintf(str, sizeof(str), "%f", val); + LilvNode* ret = lilv_node_new(world, LILV_VALUE_FLOAT, str); + if (ret) { + ret->val.float_val = val; + } + return ret; +} + +LilvNode* +lilv_new_bool(LilvWorld* world, bool val) +{ + LilvNode* ret = lilv_node_new(world, LILV_VALUE_BOOL, val ? "true" : "false"); + if (ret) { + ret->val.bool_val = val; + } + return ret; +} + +LilvNode* +lilv_node_duplicate(const LilvNode* val) +{ + if (!val) { + return NULL; + } + + LilvNode* result = (LilvNode*)malloc(sizeof(LilvNode)); + result->world = val->world; + result->node = sord_node_copy(val->node); + result->val = val->val; + result->type = val->type; + return result; +} + +void +lilv_node_free(LilvNode* val) +{ + if (val) { + sord_node_free(val->world->world, val->node); + free(val); + } +} + +bool +lilv_node_equals(const LilvNode* value, const LilvNode* other) +{ + if (value == NULL && other == NULL) { + return true; + } + + if (value == NULL || other == NULL || value->type != other->type) { + return false; + } + + switch (value->type) { + case LILV_VALUE_URI: + case LILV_VALUE_BLANK: + case LILV_VALUE_STRING: + case LILV_VALUE_BLOB: + return sord_node_equals(value->node, other->node); + case LILV_VALUE_INT: + return (value->val.int_val == other->val.int_val); + case LILV_VALUE_FLOAT: + return (value->val.float_val == other->val.float_val); + case LILV_VALUE_BOOL: + return (value->val.bool_val == other->val.bool_val); + } + + return false; /* shouldn't get here */ +} + +char* +lilv_node_get_turtle_token(const LilvNode* value) +{ + const char* str = (const char*)sord_node_get_string(value->node); + size_t len = 0; + char* result = NULL; + SerdNode node; + + switch (value->type) { + case LILV_VALUE_URI: + len = strlen(str) + 3; + result = (char*)calloc(len, 1); + snprintf(result, len, "<%s>", str); + break; + case LILV_VALUE_BLANK: + len = strlen(str) + 3; + result = (char*)calloc(len, 1); + snprintf(result, len, "_:%s", str); + break; + case LILV_VALUE_STRING: + case LILV_VALUE_BOOL: + case LILV_VALUE_BLOB: + result = lilv_strdup(str); + break; + case LILV_VALUE_INT: + node = serd_node_new_integer(value->val.int_val); + result = lilv_strdup((char*)node.buf); + serd_node_free(&node); + break; + case LILV_VALUE_FLOAT: + node = serd_node_new_decimal(value->val.float_val, 8); + result = lilv_strdup((char*)node.buf); + serd_node_free(&node); + break; + } + + return result; +} + +bool +lilv_node_is_uri(const LilvNode* value) +{ + return (value && value->type == LILV_VALUE_URI); +} + +const char* +lilv_node_as_uri(const LilvNode* value) +{ + return (lilv_node_is_uri(value) + ? (const char*)sord_node_get_string(value->node) + : NULL); +} + +bool +lilv_node_is_blank(const LilvNode* value) +{ + return (value && value->type == LILV_VALUE_BLANK); +} + +const char* +lilv_node_as_blank(const LilvNode* value) +{ + return (lilv_node_is_blank(value) + ? (const char*)sord_node_get_string(value->node) + : NULL); +} + +bool +lilv_node_is_literal(const LilvNode* value) +{ + if (!value) { + return false; + } + + switch (value->type) { + case LILV_VALUE_STRING: + case LILV_VALUE_INT: + case LILV_VALUE_FLOAT: + case LILV_VALUE_BLOB: + return true; + default: + return false; + } +} + +bool +lilv_node_is_string(const LilvNode* value) +{ + return (value && value->type == LILV_VALUE_STRING); +} + +const char* +lilv_node_as_string(const LilvNode* value) +{ + return value ? (const char*)sord_node_get_string(value->node) : NULL; +} + +bool +lilv_node_is_int(const LilvNode* value) +{ + return (value && value->type == LILV_VALUE_INT); +} + +int +lilv_node_as_int(const LilvNode* value) +{ + return lilv_node_is_int(value) ? value->val.int_val : 0; +} + +bool +lilv_node_is_float(const LilvNode* value) +{ + return (value && value->type == LILV_VALUE_FLOAT); +} + +float +lilv_node_as_float(const LilvNode* value) +{ + if (lilv_node_is_float(value)) { + return value->val.float_val; + } + + if (lilv_node_is_int(value)) { + return (float)value->val.int_val; + } + + return NAN; +} + +bool +lilv_node_is_bool(const LilvNode* value) +{ + return (value && value->type == LILV_VALUE_BOOL); +} + +bool +lilv_node_as_bool(const LilvNode* value) +{ + return lilv_node_is_bool(value) ? value->val.bool_val : false; +} + +char* +lilv_node_get_path(const LilvNode* value, char** hostname) +{ + if (lilv_node_is_uri(value)) { + return lilv_file_uri_parse(lilv_node_as_uri(value), hostname); + } + return NULL; +} diff --git a/modules/juce_audio_processors/format_types/lv2/lilv/src/plugin.c b/modules/juce_audio_processors/format_types/lv2/lilv/src/plugin.c new file mode 100644 index 0000000000..9c7cbfde2d --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lilv/src/plugin.c @@ -0,0 +1,1140 @@ +/* + Copyright 2007-2019 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "lilv_internal.h" + +#include "lilv/lilv.h" +#include "serd/serd.h" +#include "sord/sord.h" +#include "zix/tree.h" + +#include "lv2/core/lv2.h" +#include "lv2/ui/ui.h" + +#ifdef LILV_DYN_MANIFEST +# include "lv2/dynmanifest/dynmanifest.h" +#endif + +#include +#include +#include +#include +#include +#include +#include + +#define NS_DOAP (const uint8_t*)"http://usefulinc.com/ns/doap#" +#define NS_FOAF (const uint8_t*)"http://xmlns.com/foaf/0.1/" + +static void +lilv_plugin_init(LilvPlugin* plugin, LilvNode* bundle_uri) +{ + plugin->bundle_uri = bundle_uri; + plugin->binary_uri = NULL; +#ifdef LILV_DYN_MANIFEST + plugin->dynmanifest = NULL; +#endif + plugin->plugin_class = NULL; + plugin->data_uris = lilv_nodes_new(); + plugin->ports = NULL; + plugin->num_ports = 0; + plugin->loaded = false; + plugin->parse_errors = false; + plugin->replaced = false; +} + +/** Ownership of `uri` and `bundle` is taken */ +LilvPlugin* +lilv_plugin_new(LilvWorld* world, LilvNode* uri, LilvNode* bundle_uri) +{ + LilvPlugin* plugin = (LilvPlugin*)malloc(sizeof(LilvPlugin)); + + plugin->world = world; + plugin->plugin_uri = uri; + + lilv_plugin_init(plugin, bundle_uri); + return plugin; +} + +void +lilv_plugin_clear(LilvPlugin* plugin, LilvNode* bundle_uri) +{ + lilv_node_free(plugin->bundle_uri); + lilv_node_free(plugin->binary_uri); + lilv_nodes_free(plugin->data_uris); + lilv_plugin_init(plugin, bundle_uri); +} + +static void +lilv_plugin_free_ports(LilvPlugin* plugin) +{ + if (plugin->ports) { + for (uint32_t i = 0; i < plugin->num_ports; ++i) { + lilv_port_free(plugin, plugin->ports[i]); + } + free(plugin->ports); + plugin->num_ports = 0; + plugin->ports = NULL; + } +} + +void +lilv_plugin_free(LilvPlugin* plugin) +{ +#ifdef LILV_DYN_MANIFEST + if (plugin->dynmanifest && --plugin->dynmanifest->refs == 0) { + lilv_dynmanifest_free(plugin->dynmanifest); + } +#endif + + lilv_node_free(plugin->plugin_uri); + plugin->plugin_uri = NULL; + + lilv_node_free(plugin->bundle_uri); + plugin->bundle_uri = NULL; + + lilv_node_free(plugin->binary_uri); + plugin->binary_uri = NULL; + + lilv_plugin_free_ports(plugin); + + lilv_nodes_free(plugin->data_uris); + plugin->data_uris = NULL; + + free(plugin); +} + +static LilvNode* +lilv_plugin_get_one(const LilvPlugin* plugin, + const SordNode* subject, + const SordNode* predicate) +{ + /* TODO: This is slower than it could be in some cases, but it's simpler to + use the existing i18n code. */ + + SordIter* stream = + lilv_world_query_internal(plugin->world, subject, predicate, NULL); + + LilvNodes* nodes = + lilv_nodes_from_stream_objects(plugin->world, stream, SORD_OBJECT); + + if (nodes) { + LilvNode* value = lilv_node_duplicate(lilv_nodes_get_first(nodes)); + lilv_nodes_free(nodes); + return value; + } + + return NULL; +} + +LilvNode* +lilv_plugin_get_unique(const LilvPlugin* plugin, + const SordNode* subject, + const SordNode* predicate) +{ + LilvNode* ret = lilv_plugin_get_one(plugin, subject, predicate); + if (!ret) { + LILV_ERRORF("No value found for (%s %s ...) property\n", + sord_node_get_string(subject), + sord_node_get_string(predicate)); + } + return ret; +} + +static void +lilv_plugin_load(LilvPlugin* plugin) +{ + SordNode* bundle_uri_node = plugin->bundle_uri->node; + const SerdNode* bundle_uri_snode = sord_node_to_serd_node(bundle_uri_node); + + SerdEnv* env = serd_env_new(bundle_uri_snode); + SerdReader* reader = + sord_new_reader(plugin->world->model, env, SERD_TURTLE, bundle_uri_node); + + SordModel* prots = lilv_world_filter_model(plugin->world, + plugin->world->model, + plugin->plugin_uri->node, + plugin->world->uris.lv2_prototype, + NULL, + NULL); + SordModel* skel = sord_new(plugin->world->world, SORD_SPO, false); + SordIter* iter = sord_begin(prots); + for (; !sord_iter_end(iter); sord_iter_next(iter)) { + const SordNode* t = sord_iter_get_node(iter, SORD_OBJECT); + LilvNode* prototype = lilv_node_new_from_node(plugin->world, t); + + lilv_world_load_resource(plugin->world, prototype); + + SordIter* statements = + sord_search(plugin->world->model, prototype->node, NULL, NULL, NULL); + FOREACH_MATCH (statements) { + SordQuad quad; + sord_iter_get(statements, quad); + quad[0] = plugin->plugin_uri->node; + sord_add(skel, quad); + } + + sord_iter_free(statements); + lilv_node_free(prototype); + } + sord_iter_free(iter); + + for (iter = sord_begin(skel); !sord_iter_end(iter); sord_iter_next(iter)) { + SordQuad quad; + sord_iter_get(iter, quad); + sord_add(plugin->world->model, quad); + } + sord_iter_free(iter); + sord_free(skel); + sord_free(prots); + + // Parse all the plugin's data files into RDF model + SerdStatus st = SERD_SUCCESS; + LILV_FOREACH (nodes, i, plugin->data_uris) { + const LilvNode* data_uri = lilv_nodes_get(plugin->data_uris, i); + + serd_env_set_base_uri(env, sord_node_to_serd_node(data_uri->node)); + st = lilv_world_load_file(plugin->world, reader, data_uri); + if (st > SERD_FAILURE) { + break; + } + } + + if (st > SERD_FAILURE) { + plugin->loaded = true; + plugin->parse_errors = true; + serd_reader_free(reader); + serd_env_free(env); + return; + } + +#ifdef LILV_DYN_MANIFEST + // Load and parse dynamic manifest data, if this is a library + if (plugin->dynmanifest) { + typedef int (*GetDataFunc)( + LV2_Dyn_Manifest_Handle handle, FILE * fp, const char* uri); + GetDataFunc get_data_func = (GetDataFunc)lilv_dlfunc( + plugin->dynmanifest->lib, "lv2_dyn_manifest_get_data"); + if (get_data_func) { + const SordNode* bundle = plugin->dynmanifest->bundle->node; + serd_env_set_base_uri(env, sord_node_to_serd_node(bundle)); + FILE* fd = tmpfile(); + get_data_func(plugin->dynmanifest->handle, + fd, + lilv_node_as_string(plugin->plugin_uri)); + rewind(fd); + serd_reader_add_blank_prefix(reader, + lilv_world_blank_node_prefix(plugin->world)); + serd_reader_read_file_handle( + reader, fd, (const uint8_t*)"(dyn-manifest)"); + fclose(fd); + } + } +#endif + serd_reader_free(reader); + serd_env_free(env); + + plugin->loaded = true; +} + +static bool +is_symbol(const char* str) +{ + for (const char* s = str; *s; ++s) { + if (!((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z') || + (s > str && *s >= '0' && *s <= '9') || *s == '_')) { + return false; + } + } + return true; +} + +static void +lilv_plugin_load_ports_if_necessary(const LilvPlugin* const_plugin) +{ + LilvPlugin* plugin = (LilvPlugin*)const_plugin; + + lilv_plugin_load_if_necessary(plugin); + + if (!plugin->ports) { + plugin->ports = (LilvPort**)malloc(sizeof(LilvPort*)); + plugin->ports[0] = NULL; + + SordIter* ports = lilv_world_query_internal(plugin->world, + plugin->plugin_uri->node, + plugin->world->uris.lv2_port, + NULL); + + FOREACH_MATCH (ports) { + const SordNode* port = sord_iter_get_node(ports, SORD_OBJECT); + + LilvNode* index = + lilv_plugin_get_unique(plugin, port, plugin->world->uris.lv2_index); + + LilvNode* symbol = + lilv_plugin_get_unique(plugin, port, plugin->world->uris.lv2_symbol); + + if (!lilv_node_is_string(symbol) || + !is_symbol((const char*)sord_node_get_string(symbol->node))) { + LILV_ERRORF("Plugin <%s> port symbol `%s' is invalid\n", + lilv_node_as_uri(plugin->plugin_uri), + lilv_node_as_string(symbol)); + lilv_node_free(symbol); + lilv_node_free(index); + lilv_plugin_free_ports(plugin); + break; + } + + if (!lilv_node_is_int(index)) { + LILV_ERRORF("Plugin <%s> port index is not an integer\n", + lilv_node_as_uri(plugin->plugin_uri)); + lilv_node_free(symbol); + lilv_node_free(index); + lilv_plugin_free_ports(plugin); + break; + } + + uint32_t this_index = lilv_node_as_int(index); + LilvPort* this_port = NULL; + if (plugin->num_ports > this_index) { + this_port = plugin->ports[this_index]; + } else { + plugin->ports = (LilvPort**)realloc( + plugin->ports, (this_index + 1) * sizeof(LilvPort*)); + memset(plugin->ports + plugin->num_ports, + '\0', + (this_index - plugin->num_ports) * sizeof(LilvPort*)); + plugin->num_ports = this_index + 1; + } + + // Havn't seen this port yet, add it to array + if (!this_port) { + this_port = lilv_port_new( + plugin->world, port, this_index, lilv_node_as_string(symbol)); + plugin->ports[this_index] = this_port; + } + + SordIter* types = lilv_world_query_internal( + plugin->world, port, plugin->world->uris.rdf_a, NULL); + FOREACH_MATCH (types) { + const SordNode* type = sord_iter_get_node(types, SORD_OBJECT); + if (sord_node_get_type(type) == SORD_URI) { + zix_tree_insert((ZixTree*)this_port->classes, + lilv_node_new_from_node(plugin->world, type), + NULL); + } else { + LILV_WARNF("Plugin <%s> port type is not a URI\n", + lilv_node_as_uri(plugin->plugin_uri)); + } + } + sord_iter_free(types); + + lilv_node_free(symbol); + lilv_node_free(index); + } + sord_iter_free(ports); + + // Check sanity + for (uint32_t i = 0; i < plugin->num_ports; ++i) { + if (!plugin->ports[i]) { + LILV_ERRORF("Plugin <%s> is missing port %u/%u\n", + lilv_node_as_uri(plugin->plugin_uri), + i, + plugin->num_ports); + lilv_plugin_free_ports(plugin); + break; + } + } + } +} + +void +lilv_plugin_load_if_necessary(const LilvPlugin* plugin) +{ + if (!plugin->loaded) { + lilv_plugin_load((LilvPlugin*)plugin); + } +} + +const LilvNode* +lilv_plugin_get_uri(const LilvPlugin* plugin) +{ + return plugin->plugin_uri; +} + +const LilvNode* +lilv_plugin_get_bundle_uri(const LilvPlugin* plugin) +{ + return plugin->bundle_uri; +} + +const LilvNode* +lilv_plugin_get_library_uri(const LilvPlugin* plugin) +{ + lilv_plugin_load_if_necessary((LilvPlugin*)plugin); + if (!plugin->binary_uri) { + // lv2:binary ?binary + SordIter* i = lilv_world_query_internal(plugin->world, + plugin->plugin_uri->node, + plugin->world->uris.lv2_binary, + NULL); + FOREACH_MATCH (i) { + const SordNode* binary_node = sord_iter_get_node(i, SORD_OBJECT); + if (sord_node_get_type(binary_node) == SORD_URI) { + ((LilvPlugin*)plugin)->binary_uri = + lilv_node_new_from_node(plugin->world, binary_node); + break; + } + } + sord_iter_free(i); + } + if (!plugin->binary_uri) { + LILV_WARNF("Plugin <%s> has no lv2:binary\n", + lilv_node_as_uri(lilv_plugin_get_uri(plugin))); + } + return plugin->binary_uri; +} + +const LilvNodes* +lilv_plugin_get_data_uris(const LilvPlugin* plugin) +{ + return plugin->data_uris; +} + +const LilvPluginClass* +lilv_plugin_get_class(const LilvPlugin* plugin) +{ + lilv_plugin_load_if_necessary((LilvPlugin*)plugin); + if (!plugin->plugin_class) { + // a ?class + SordIter* c = lilv_world_query_internal( + plugin->world, plugin->plugin_uri->node, plugin->world->uris.rdf_a, NULL); + FOREACH_MATCH (c) { + const SordNode* class_node = sord_iter_get_node(c, SORD_OBJECT); + if (sord_node_get_type(class_node) != SORD_URI) { + continue; + } + + LilvNode* klass = lilv_node_new_from_node(plugin->world, class_node); + if (!lilv_node_equals(klass, plugin->world->lv2_plugin_class->uri)) { + const LilvPluginClass* pclass = + lilv_plugin_classes_get_by_uri(plugin->world->plugin_classes, klass); + + if (pclass) { + ((LilvPlugin*)plugin)->plugin_class = pclass; + lilv_node_free(klass); + break; + } + } + + lilv_node_free(klass); + } + sord_iter_free(c); + + if (plugin->plugin_class == NULL) { + ((LilvPlugin*)plugin)->plugin_class = plugin->world->lv2_plugin_class; + } + } + return plugin->plugin_class; +} + +static LilvNodes* +lilv_plugin_get_value_internal(const LilvPlugin* plugin, + const SordNode* predicate) +{ + lilv_plugin_load_if_necessary(plugin); + return lilv_world_find_nodes_internal( + plugin->world, plugin->plugin_uri->node, predicate, NULL); +} + +bool +lilv_plugin_verify(const LilvPlugin* plugin) +{ + lilv_plugin_load_if_necessary(plugin); + if (plugin->parse_errors) { + return false; + } + + LilvNode* rdf_type = lilv_new_uri(plugin->world, LILV_NS_RDF "type"); + LilvNodes* results = lilv_plugin_get_value(plugin, rdf_type); + lilv_node_free(rdf_type); + if (!results) { + return false; + } + + lilv_nodes_free(results); + results = + lilv_plugin_get_value_internal(plugin, plugin->world->uris.doap_name); + if (!results) { + return false; + } + + lilv_nodes_free(results); + LilvNode* lv2_port = lilv_new_uri(plugin->world, LV2_CORE__port); + results = lilv_plugin_get_value(plugin, lv2_port); + lilv_node_free(lv2_port); + if (!results) { + return false; + } + + lilv_nodes_free(results); + return true; +} + +LilvNode* +lilv_plugin_get_name(const LilvPlugin* plugin) +{ + LilvNodes* results = + lilv_plugin_get_value_internal(plugin, plugin->world->uris.doap_name); + + LilvNode* ret = NULL; + if (results) { + LilvNode* val = lilv_nodes_get_first(results); + if (lilv_node_is_string(val)) { + ret = lilv_node_duplicate(val); + } + lilv_nodes_free(results); + } + + if (!ret) { + LILV_WARNF("Plugin <%s> has no (mandatory) doap:name\n", + lilv_node_as_string(lilv_plugin_get_uri(plugin))); + } + + return ret; +} + +LilvNodes* +lilv_plugin_get_value(const LilvPlugin* plugin, const LilvNode* predicate) +{ + lilv_plugin_load_if_necessary(plugin); + return lilv_world_find_nodes( + plugin->world, plugin->plugin_uri, predicate, NULL); +} + +uint32_t +lilv_plugin_get_num_ports(const LilvPlugin* plugin) +{ + lilv_plugin_load_ports_if_necessary(plugin); + return plugin->num_ports; +} + +void +lilv_plugin_get_port_ranges_float(const LilvPlugin* plugin, + float* min_values, + float* max_values, + float* def_values) +{ + lilv_plugin_load_ports_if_necessary(plugin); + LilvNode* min = NULL; + LilvNode* max = NULL; + LilvNode* def = NULL; + LilvNode** minptr = min_values ? &min : NULL; + LilvNode** maxptr = max_values ? &max : NULL; + LilvNode** defptr = def_values ? &def : NULL; + + for (uint32_t i = 0; i < plugin->num_ports; ++i) { + lilv_port_get_range(plugin, plugin->ports[i], defptr, minptr, maxptr); + + if (min_values) { + if (lilv_node_is_float(min) || lilv_node_is_int(min)) { + min_values[i] = lilv_node_as_float(min); + } else { + min_values[i] = NAN; + } + } + + if (max_values) { + if (lilv_node_is_float(max) || lilv_node_is_int(max)) { + max_values[i] = lilv_node_as_float(max); + } else { + max_values[i] = NAN; + } + } + + if (def_values) { + if (lilv_node_is_float(def) || lilv_node_is_int(def)) { + def_values[i] = lilv_node_as_float(def); + } else { + def_values[i] = NAN; + } + } + + lilv_node_free(def); + lilv_node_free(min); + lilv_node_free(max); + } +} + +uint32_t +lilv_plugin_get_num_ports_of_class_va(const LilvPlugin* plugin, + const LilvNode* class_1, + va_list args) +{ + lilv_plugin_load_ports_if_necessary(plugin); + + uint32_t count = 0; + + // Build array of classes from args so we can walk it several times + size_t n_classes = 0; + const LilvNode** classes = NULL; + for (LilvNode* c = NULL; (c = va_arg(args, LilvNode*));) { + classes = + (const LilvNode**)realloc(classes, ++n_classes * sizeof(LilvNode*)); + classes[n_classes - 1] = c; + } + + // Check each port against every type + for (unsigned i = 0; i < plugin->num_ports; ++i) { + LilvPort* port = plugin->ports[i]; + if (port && lilv_port_is_a(plugin, port, class_1)) { + bool matches = true; + for (size_t j = 0; j < n_classes; ++j) { + if (!lilv_port_is_a(plugin, port, classes[j])) { + matches = false; + break; + } + } + + if (matches) { + ++count; + } + } + } + + free(classes); + return count; +} + +uint32_t +lilv_plugin_get_num_ports_of_class(const LilvPlugin* plugin, + const LilvNode* class_1, + ...) +{ + va_list args; + va_start(args, class_1); + + uint32_t count = lilv_plugin_get_num_ports_of_class_va(plugin, class_1, args); + + va_end(args); + return count; +} + +bool +lilv_plugin_has_latency(const LilvPlugin* plugin) +{ + lilv_plugin_load_if_necessary(plugin); + SordIter* ports = lilv_world_query_internal(plugin->world, + plugin->plugin_uri->node, + plugin->world->uris.lv2_port, + NULL); + + bool ret = false; + FOREACH_MATCH (ports) { + const SordNode* port = sord_iter_get_node(ports, SORD_OBJECT); + + SordIter* prop = + lilv_world_query_internal(plugin->world, + port, + plugin->world->uris.lv2_portProperty, + plugin->world->uris.lv2_reportsLatency); + + SordIter* des = + lilv_world_query_internal(plugin->world, + port, + plugin->world->uris.lv2_designation, + plugin->world->uris.lv2_latency); + + const bool latent = !sord_iter_end(prop) || !sord_iter_end(des); + sord_iter_free(prop); + sord_iter_free(des); + if (latent) { + ret = true; + break; + } + } + sord_iter_free(ports); + + return ret; +} + +static const LilvPort* +lilv_plugin_get_port_by_property(const LilvPlugin* plugin, + const SordNode* port_property) +{ + lilv_plugin_load_ports_if_necessary(plugin); + for (uint32_t i = 0; i < plugin->num_ports; ++i) { + LilvPort* port = plugin->ports[i]; + SordIter* iter = + lilv_world_query_internal(plugin->world, + port->node->node, + plugin->world->uris.lv2_portProperty, + port_property); + + const bool found = !sord_iter_end(iter); + sord_iter_free(iter); + + if (found) { + return port; + } + } + + return NULL; +} + +const LilvPort* +lilv_plugin_get_port_by_designation(const LilvPlugin* plugin, + const LilvNode* port_class, + const LilvNode* designation) +{ + LilvWorld* world = plugin->world; + lilv_plugin_load_ports_if_necessary(plugin); + for (uint32_t i = 0; i < plugin->num_ports; ++i) { + LilvPort* port = plugin->ports[i]; + SordIter* iter = lilv_world_query_internal( + world, port->node->node, world->uris.lv2_designation, designation->node); + + const bool found = + !sord_iter_end(iter) && + (!port_class || lilv_port_is_a(plugin, port, port_class)); + sord_iter_free(iter); + + if (found) { + return port; + } + } + + return NULL; +} + +uint32_t +lilv_plugin_get_latency_port_index(const LilvPlugin* plugin) +{ + LilvNode* lv2_OutputPort = lilv_new_uri(plugin->world, LV2_CORE__OutputPort); + LilvNode* lv2_latency = lilv_new_uri(plugin->world, LV2_CORE__latency); + + const LilvPort* prop_port = lilv_plugin_get_port_by_property( + plugin, plugin->world->uris.lv2_reportsLatency); + const LilvPort* des_port = + lilv_plugin_get_port_by_designation(plugin, lv2_OutputPort, lv2_latency); + + lilv_node_free(lv2_latency); + lilv_node_free(lv2_OutputPort); + + if (prop_port) { + return prop_port->index; + } + + if (des_port) { + return des_port->index; + } + + return (uint32_t)-1; +} + +bool +lilv_plugin_has_feature(const LilvPlugin* plugin, const LilvNode* feature) +{ + lilv_plugin_load_if_necessary(plugin); + const SordNode* predicates[] = {plugin->world->uris.lv2_requiredFeature, + plugin->world->uris.lv2_optionalFeature, + NULL}; + + for (const SordNode** pred = predicates; *pred; ++pred) { + if (lilv_world_ask_internal( + plugin->world, plugin->plugin_uri->node, *pred, feature->node)) { + return true; + } + } + return false; +} + +LilvNodes* +lilv_plugin_get_supported_features(const LilvPlugin* plugin) +{ + LilvNodes* optional = lilv_plugin_get_optional_features(plugin); + LilvNodes* required = lilv_plugin_get_required_features(plugin); + LilvNodes* result = lilv_nodes_merge(optional, required); + lilv_nodes_free(optional); + lilv_nodes_free(required); + return result; +} + +LilvNodes* +lilv_plugin_get_optional_features(const LilvPlugin* plugin) +{ + lilv_plugin_load_if_necessary(plugin); + return lilv_world_find_nodes_internal(plugin->world, + plugin->plugin_uri->node, + plugin->world->uris.lv2_optionalFeature, + NULL); +} + +LilvNodes* +lilv_plugin_get_required_features(const LilvPlugin* plugin) +{ + lilv_plugin_load_if_necessary(plugin); + return lilv_world_find_nodes_internal(plugin->world, + plugin->plugin_uri->node, + plugin->world->uris.lv2_requiredFeature, + NULL); +} + +bool +lilv_plugin_has_extension_data(const LilvPlugin* plugin, const LilvNode* uri) +{ + if (!lilv_node_is_uri(uri)) { + LILV_ERRORF("Extension data `%s' is not a URI\n", + sord_node_get_string(uri->node)); + return false; + } + + lilv_plugin_load_if_necessary(plugin); + return lilv_world_ask_internal(plugin->world, + plugin->plugin_uri->node, + plugin->world->uris.lv2_extensionData, + uri->node); +} + +LilvNodes* +lilv_plugin_get_extension_data(const LilvPlugin* plugin) +{ + return lilv_plugin_get_value_internal(plugin, + plugin->world->uris.lv2_extensionData); +} + +const LilvPort* +lilv_plugin_get_port_by_index(const LilvPlugin* plugin, uint32_t index) +{ + lilv_plugin_load_ports_if_necessary(plugin); + if (index < plugin->num_ports) { + return plugin->ports[index]; + } + + return NULL; +} + +const LilvPort* +lilv_plugin_get_port_by_symbol(const LilvPlugin* plugin, const LilvNode* symbol) +{ + lilv_plugin_load_ports_if_necessary(plugin); + for (uint32_t i = 0; i < plugin->num_ports; ++i) { + LilvPort* port = plugin->ports[i]; + if (lilv_node_equals(port->symbol, symbol)) { + return port; + } + } + + return NULL; +} + +LilvNode* +lilv_plugin_get_project(const LilvPlugin* plugin) +{ + lilv_plugin_load_if_necessary(plugin); + + SordNode* lv2_project = + sord_new_uri(plugin->world->world, (const uint8_t*)LV2_CORE__project); + + SordIter* projects = lilv_world_query_internal( + plugin->world, plugin->plugin_uri->node, lv2_project, NULL); + + sord_node_free(plugin->world->world, lv2_project); + + if (sord_iter_end(projects)) { + sord_iter_free(projects); + return NULL; + } + + const SordNode* project = sord_iter_get_node(projects, SORD_OBJECT); + + sord_iter_free(projects); + return lilv_node_new_from_node(plugin->world, project); +} + +static const SordNode* +lilv_plugin_get_author(const LilvPlugin* plugin) +{ + lilv_plugin_load_if_necessary(plugin); + + SordNode* doap_maintainer = + sord_new_uri(plugin->world->world, NS_DOAP "maintainer"); + + SordIter* maintainers = lilv_world_query_internal( + plugin->world, plugin->plugin_uri->node, doap_maintainer, NULL); + + if (sord_iter_end(maintainers)) { + sord_iter_free(maintainers); + + LilvNode* project = lilv_plugin_get_project(plugin); + if (!project) { + sord_node_free(plugin->world->world, doap_maintainer); + return NULL; + } + + maintainers = lilv_world_query_internal( + plugin->world, project->node, doap_maintainer, NULL); + + lilv_node_free(project); + } + + sord_node_free(plugin->world->world, doap_maintainer); + + if (sord_iter_end(maintainers)) { + sord_iter_free(maintainers); + return NULL; + } + + const SordNode* author = sord_iter_get_node(maintainers, SORD_OBJECT); + + sord_iter_free(maintainers); + return author; +} + +static LilvNode* +lilv_plugin_get_author_property(const LilvPlugin* plugin, const uint8_t* uri) +{ + const SordNode* author = lilv_plugin_get_author(plugin); + if (author) { + SordWorld* sworld = plugin->world->world; + SordNode* pred = sord_new_uri(sworld, uri); + LilvNode* ret = lilv_plugin_get_one(plugin, author, pred); + sord_node_free(sworld, pred); + return ret; + } + return NULL; +} + +LilvNode* +lilv_plugin_get_author_name(const LilvPlugin* plugin) +{ + return lilv_plugin_get_author_property(plugin, NS_FOAF "name"); +} + +LilvNode* +lilv_plugin_get_author_email(const LilvPlugin* plugin) +{ + return lilv_plugin_get_author_property(plugin, NS_FOAF "mbox"); +} + +LilvNode* +lilv_plugin_get_author_homepage(const LilvPlugin* plugin) +{ + return lilv_plugin_get_author_property(plugin, NS_FOAF "homepage"); +} + +bool +lilv_plugin_is_replaced(const LilvPlugin* plugin) +{ + return plugin->replaced; +} + +LilvUIs* +lilv_plugin_get_uis(const LilvPlugin* plugin) +{ + lilv_plugin_load_if_necessary(plugin); + + SordNode* ui_ui_node = + sord_new_uri(plugin->world->world, (const uint8_t*)LV2_UI__ui); + SordNode* ui_binary_node = + sord_new_uri(plugin->world->world, (const uint8_t*)LV2_UI__binary); + + LilvUIs* result = lilv_uis_new(); + SordIter* uis = lilv_world_query_internal( + plugin->world, plugin->plugin_uri->node, ui_ui_node, NULL); + + FOREACH_MATCH (uis) { + const SordNode* ui = sord_iter_get_node(uis, SORD_OBJECT); + + LilvNode* type = + lilv_plugin_get_unique(plugin, ui, plugin->world->uris.rdf_a); + LilvNode* binary = + lilv_plugin_get_one(plugin, ui, plugin->world->uris.lv2_binary); + if (!binary) { + binary = lilv_plugin_get_unique(plugin, ui, ui_binary_node); + } + + if (sord_node_get_type(ui) != SORD_URI || !lilv_node_is_uri(type) || + !lilv_node_is_uri(binary)) { + lilv_node_free(binary); + lilv_node_free(type); + LILV_ERRORF("Corrupt UI <%s>\n", sord_node_get_string(ui)); + continue; + } + + LilvUI* lilv_ui = lilv_ui_new( + plugin->world, lilv_node_new_from_node(plugin->world, ui), type, binary); + + zix_tree_insert((ZixTree*)result, lilv_ui, NULL); + } + sord_iter_free(uis); + + sord_node_free(plugin->world->world, ui_binary_node); + sord_node_free(plugin->world->world, ui_ui_node); + + if (lilv_uis_size(result) > 0) { + return result; + } + + lilv_uis_free(result); + return NULL; +} + +LilvNodes* +lilv_plugin_get_related(const LilvPlugin* plugin, const LilvNode* type) +{ + lilv_plugin_load_if_necessary(plugin); + + LilvWorld* const world = plugin->world; + LilvNodes* const related = lilv_world_find_nodes_internal( + world, NULL, world->uris.lv2_appliesTo, lilv_plugin_get_uri(plugin)->node); + + if (!type) { + return related; + } + + LilvNodes* matches = lilv_nodes_new(); + LILV_FOREACH (nodes, i, related) { + LilvNode* node = (LilvNode*)lilv_collection_get((ZixTree*)related, i); + if (lilv_world_ask_internal( + world, node->node, world->uris.rdf_a, type->node)) { + zix_tree_insert( + (ZixTree*)matches, lilv_node_new_from_node(world, node->node), NULL); + } + } + + lilv_nodes_free(related); + return matches; +} + +static SerdEnv* +new_lv2_env(const SerdNode* base) +{ + SerdEnv* env = serd_env_new(base); + +#define USTR(s) ((const uint8_t*)(s)) + + serd_env_set_prefix_from_strings(env, USTR("doap"), USTR(LILV_NS_DOAP)); + serd_env_set_prefix_from_strings(env, USTR("foaf"), USTR(LILV_NS_FOAF)); + serd_env_set_prefix_from_strings(env, USTR("lv2"), USTR(LILV_NS_LV2)); + serd_env_set_prefix_from_strings(env, USTR("owl"), USTR(LILV_NS_OWL)); + serd_env_set_prefix_from_strings(env, USTR("rdf"), USTR(LILV_NS_RDF)); + serd_env_set_prefix_from_strings(env, USTR("rdfs"), USTR(LILV_NS_RDFS)); + serd_env_set_prefix_from_strings(env, USTR("xsd"), USTR(LILV_NS_XSD)); + + return env; +} + +static void +maybe_write_prefixes(SerdWriter* writer, SerdEnv* env, FILE* file) +{ + fseek(file, 0, SEEK_END); + if (ftell(file) == 0) { + serd_env_foreach(env, (SerdPrefixSink)serd_writer_set_prefix, writer); + } else { + fprintf(file, "\n"); + } +} + +void +lilv_plugin_write_description(LilvWorld* world, + const LilvPlugin* plugin, + const LilvNode* base_uri, + FILE* plugin_file) +{ + const LilvNode* subject = lilv_plugin_get_uri(plugin); + const uint32_t num_ports = lilv_plugin_get_num_ports(plugin); + const SerdNode* base = sord_node_to_serd_node(base_uri->node); + SerdEnv* env = new_lv2_env(base); + + SerdWriter* writer = + serd_writer_new(SERD_TURTLE, + (SerdStyle)(SERD_STYLE_ABBREVIATED | SERD_STYLE_CURIED), + env, + NULL, + serd_file_sink, + plugin_file); + + // Write prefixes if this is a new file + maybe_write_prefixes(writer, env, plugin_file); + + // Write plugin description + SordIter* plug_iter = + lilv_world_query_internal(world, subject->node, NULL, NULL); + sord_write_iter(plug_iter, writer); + + // Write port descriptions + for (uint32_t i = 0; i < num_ports; ++i) { + const LilvPort* port = plugin->ports[i]; + SordIter* port_iter = + lilv_world_query_internal(world, port->node->node, NULL, NULL); + sord_write_iter(port_iter, writer); + } + + serd_writer_free(writer); + serd_env_free(env); +} + +void +lilv_plugin_write_manifest_entry(LilvWorld* world, + const LilvPlugin* plugin, + const LilvNode* base_uri, + FILE* manifest_file, + const char* plugin_file_path) +{ + (void)world; + + const LilvNode* subject = lilv_plugin_get_uri(plugin); + const SerdNode* base = sord_node_to_serd_node(base_uri->node); + SerdEnv* env = new_lv2_env(base); + + SerdWriter* writer = + serd_writer_new(SERD_TURTLE, + (SerdStyle)(SERD_STYLE_ABBREVIATED | SERD_STYLE_CURIED), + env, + NULL, + serd_file_sink, + manifest_file); + + // Write prefixes if this is a new file + maybe_write_prefixes(writer, env, manifest_file); + + // Write manifest entry + serd_writer_write_statement( + writer, + 0, + NULL, + sord_node_to_serd_node(subject->node), + sord_node_to_serd_node(plugin->world->uris.rdf_a), + sord_node_to_serd_node(plugin->world->uris.lv2_Plugin), + 0, + 0); + + const SerdNode file_node = + serd_node_from_string(SERD_URI, (const uint8_t*)plugin_file_path); + serd_writer_write_statement( + writer, + 0, + NULL, + sord_node_to_serd_node(subject->node), + sord_node_to_serd_node(plugin->world->uris.rdfs_seeAlso), + &file_node, + 0, + 0); + + serd_writer_free(writer); + serd_env_free(env); +} diff --git a/modules/juce_audio_processors/format_types/lv2/lilv/src/pluginclass.c b/modules/juce_audio_processors/format_types/lv2/lilv/src/pluginclass.c new file mode 100644 index 0000000000..2f0afe742a --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lilv/src/pluginclass.c @@ -0,0 +1,91 @@ +/* + Copyright 2007-2019 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "lilv_internal.h" + +#include "lilv/lilv.h" +#include "sord/sord.h" +#include "zix/tree.h" + +#include +#include + +LilvPluginClass* +lilv_plugin_class_new(LilvWorld* world, + const SordNode* parent_node, + const SordNode* uri, + const char* label) +{ + LilvPluginClass* pc = (LilvPluginClass*)malloc(sizeof(LilvPluginClass)); + pc->world = world; + pc->uri = lilv_node_new_from_node(world, uri); + pc->label = lilv_node_new(world, LILV_VALUE_STRING, label); + pc->parent_uri = + (parent_node ? lilv_node_new_from_node(world, parent_node) : NULL); + return pc; +} + +void +lilv_plugin_class_free(LilvPluginClass* plugin_class) +{ + if (!plugin_class) { + return; + } + + lilv_node_free(plugin_class->uri); + lilv_node_free(plugin_class->parent_uri); + lilv_node_free(plugin_class->label); + free(plugin_class); +} + +const LilvNode* +lilv_plugin_class_get_parent_uri(const LilvPluginClass* plugin_class) +{ + return plugin_class->parent_uri ? plugin_class->parent_uri : NULL; +} + +const LilvNode* +lilv_plugin_class_get_uri(const LilvPluginClass* plugin_class) +{ + return plugin_class->uri; +} + +const LilvNode* +lilv_plugin_class_get_label(const LilvPluginClass* plugin_class) +{ + return plugin_class->label; +} + +LilvPluginClasses* +lilv_plugin_class_get_children(const LilvPluginClass* plugin_class) +{ + // Returned list doesn't own categories + LilvPluginClasses* all = plugin_class->world->plugin_classes; + LilvPluginClasses* result = zix_tree_new(false, lilv_ptr_cmp, NULL, NULL); + + for (ZixTreeIter* i = zix_tree_begin((ZixTree*)all); + i != zix_tree_end((ZixTree*)all); + i = zix_tree_iter_next(i)) { + const LilvPluginClass* c = (LilvPluginClass*)zix_tree_get(i); + const LilvNode* parent = lilv_plugin_class_get_parent_uri(c); + if (parent && + lilv_node_equals(lilv_plugin_class_get_uri(plugin_class), parent)) { + zix_tree_insert((ZixTree*)result, (LilvPluginClass*)c, NULL); + } + } + + return result; +} diff --git a/modules/juce_audio_processors/format_types/lv2/lilv/src/port.c b/modules/juce_audio_processors/format_types/lv2/lilv/src/port.c new file mode 100644 index 0000000000..71edaf19ac --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lilv/src/port.c @@ -0,0 +1,272 @@ +/* + Copyright 2007-2019 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "lilv_internal.h" + +#include "lv2/atom/atom.h" +#include "lv2/core/lv2.h" +#include "lv2/event/event.h" + +#include "lilv/lilv.h" +#include "sord/sord.h" +#include "zix/tree.h" + +#include +#include +#include +#include +#include + +LilvPort* +lilv_port_new(LilvWorld* world, + const SordNode* node, + uint32_t index, + const char* symbol) +{ + LilvPort* port = (LilvPort*)malloc(sizeof(LilvPort)); + port->node = lilv_node_new_from_node(world, node); + port->index = index; + port->symbol = lilv_node_new(world, LILV_VALUE_STRING, symbol); + port->classes = lilv_nodes_new(); + return port; +} + +void +lilv_port_free(const LilvPlugin* plugin, LilvPort* port) +{ + (void)plugin; + + if (port) { + lilv_node_free(port->node); + lilv_nodes_free(port->classes); + lilv_node_free(port->symbol); + free(port); + } +} + +bool +lilv_port_is_a(const LilvPlugin* plugin, + const LilvPort* port, + const LilvNode* port_class) +{ + (void)plugin; + + LILV_FOREACH (nodes, i, port->classes) { + if (lilv_node_equals(lilv_nodes_get(port->classes, i), port_class)) { + return true; + } + } + + return false; +} + +bool +lilv_port_has_property(const LilvPlugin* plugin, + const LilvPort* port, + const LilvNode* property) +{ + return lilv_world_ask_internal(plugin->world, + port->node->node, + plugin->world->uris.lv2_portProperty, + property->node); +} + +bool +lilv_port_supports_event(const LilvPlugin* plugin, + const LilvPort* port, + const LilvNode* event_type) +{ + const uint8_t* predicates[] = {(const uint8_t*)LV2_EVENT__supportsEvent, + (const uint8_t*)LV2_ATOM__supports, + NULL}; + + for (const uint8_t** pred = predicates; *pred; ++pred) { + if (lilv_world_ask_internal(plugin->world, + port->node->node, + sord_new_uri(plugin->world->world, *pred), + event_type->node)) { + return true; + } + } + return false; +} + +static LilvNodes* +lilv_port_get_value_by_node(const LilvPlugin* plugin, + const LilvPort* port, + const SordNode* predicate) +{ + return lilv_world_find_nodes_internal( + plugin->world, port->node->node, predicate, NULL); +} + +const LilvNode* +lilv_port_get_node(const LilvPlugin* plugin, const LilvPort* port) +{ + (void)plugin; + + return port->node; +} + +LilvNodes* +lilv_port_get_value(const LilvPlugin* plugin, + const LilvPort* port, + const LilvNode* predicate) +{ + if (!lilv_node_is_uri(predicate)) { + LILV_ERRORF("Predicate `%s' is not a URI\n", + sord_node_get_string(predicate->node)); + return NULL; + } + + return lilv_port_get_value_by_node(plugin, port, predicate->node); +} + +LilvNode* +lilv_port_get(const LilvPlugin* plugin, + const LilvPort* port, + const LilvNode* predicate) +{ + LilvNodes* values = lilv_port_get_value(plugin, port, predicate); + + LilvNode* value = + lilv_node_duplicate(values ? lilv_nodes_get_first(values) : NULL); + + lilv_nodes_free(values); + return value; +} + +uint32_t +lilv_port_get_index(const LilvPlugin* plugin, const LilvPort* port) +{ + (void)plugin; + + return port->index; +} + +const LilvNode* +lilv_port_get_symbol(const LilvPlugin* plugin, const LilvPort* port) +{ + (void)plugin; + + return port->symbol; +} + +LilvNode* +lilv_port_get_name(const LilvPlugin* plugin, const LilvPort* port) +{ + LilvNodes* results = + lilv_port_get_value_by_node(plugin, port, plugin->world->uris.lv2_name); + + LilvNode* ret = NULL; + if (results) { + LilvNode* val = lilv_nodes_get_first(results); + if (lilv_node_is_string(val)) { + ret = lilv_node_duplicate(val); + } + lilv_nodes_free(results); + } + + if (!ret) { + LILV_WARNF("Plugin <%s> port has no (mandatory) doap:name\n", + lilv_node_as_string(lilv_plugin_get_uri(plugin))); + } + + return ret; +} + +const LilvNodes* +lilv_port_get_classes(const LilvPlugin* plugin, const LilvPort* port) +{ + (void)plugin; + + return port->classes; +} + +void +lilv_port_get_range(const LilvPlugin* plugin, + const LilvPort* port, + LilvNode** def, + LilvNode** min, + LilvNode** max) +{ + if (def) { + LilvNodes* defaults = lilv_port_get_value_by_node( + plugin, port, plugin->world->uris.lv2_default); + *def = + defaults ? lilv_node_duplicate(lilv_nodes_get_first(defaults)) : NULL; + lilv_nodes_free(defaults); + } + + if (min) { + LilvNodes* minimums = lilv_port_get_value_by_node( + plugin, port, plugin->world->uris.lv2_minimum); + *min = + minimums ? lilv_node_duplicate(lilv_nodes_get_first(minimums)) : NULL; + lilv_nodes_free(minimums); + } + + if (max) { + LilvNodes* maximums = lilv_port_get_value_by_node( + plugin, port, plugin->world->uris.lv2_maximum); + *max = + maximums ? lilv_node_duplicate(lilv_nodes_get_first(maximums)) : NULL; + lilv_nodes_free(maximums); + } +} + +LilvScalePoints* +lilv_port_get_scale_points(const LilvPlugin* plugin, const LilvPort* port) +{ + SordIter* points = lilv_world_query_internal( + plugin->world, + port->node->node, + sord_new_uri(plugin->world->world, (const uint8_t*)LV2_CORE__scalePoint), + NULL); + + LilvScalePoints* ret = NULL; + if (!sord_iter_end(points)) { + ret = lilv_scale_points_new(); + } + + FOREACH_MATCH (points) { + const SordNode* point = sord_iter_get_node(points, SORD_OBJECT); + + LilvNode* value = + lilv_plugin_get_unique(plugin, point, plugin->world->uris.rdf_value); + + LilvNode* label = + lilv_plugin_get_unique(plugin, point, plugin->world->uris.rdfs_label); + + if (value && label) { + zix_tree_insert((ZixTree*)ret, lilv_scale_point_new(value, label), NULL); + } + } + sord_iter_free(points); + + assert(!ret || lilv_nodes_size(ret) > 0); + return ret; +} + +LilvNodes* +lilv_port_get_properties(const LilvPlugin* plugin, const LilvPort* port) +{ + LilvNode* pred = lilv_node_new_from_node( + plugin->world, plugin->world->uris.lv2_portProperty); + LilvNodes* ret = lilv_port_get_value(plugin, port, pred); + lilv_node_free(pred); + return ret; +} diff --git a/modules/juce_audio_processors/format_types/lv2/lilv/src/query.c b/modules/juce_audio_processors/format_types/lv2/lilv/src/query.c new file mode 100644 index 0000000000..eb179f229c --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lilv/src/query.c @@ -0,0 +1,144 @@ +/* + Copyright 2007-2019 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "lilv_internal.h" + +#include "lilv/lilv.h" +#include "sord/sord.h" +#include "zix/tree.h" + +#include +#include + +typedef enum { + LILV_LANG_MATCH_NONE, ///< Language does not match at all + LILV_LANG_MATCH_PARTIAL, ///< Partial (language, but not country) match + LILV_LANG_MATCH_EXACT ///< Exact (language and country) match +} LilvLangMatch; + +static LilvLangMatch +lilv_lang_matches(const char* a, const char* b) +{ + if (!a || !b) { + return LILV_LANG_MATCH_NONE; + } + + if (!strcmp(a, b)) { + return LILV_LANG_MATCH_EXACT; + } + + const char* a_dash = strchr(a, '-'); + const size_t a_lang_len = a_dash ? (size_t)(a_dash - a) : strlen(a); + const char* b_dash = strchr(b, '-'); + const size_t b_lang_len = b_dash ? (size_t)(b_dash - b) : strlen(b); + + if (a_lang_len == b_lang_len && !strncmp(a, b, a_lang_len)) { + return LILV_LANG_MATCH_PARTIAL; + } + + return LILV_LANG_MATCH_NONE; +} + +static LilvNodes* +lilv_nodes_from_stream_objects_i18n(LilvWorld* world, + SordIter* stream, + SordQuadIndex field) +{ + LilvNodes* values = lilv_nodes_new(); + const SordNode* nolang = NULL; // Untranslated value + const SordNode* partial = NULL; // Partial language match + char* syslang = lilv_get_lang(); + FOREACH_MATCH (stream) { + const SordNode* value = sord_iter_get_node(stream, field); + if (sord_node_get_type(value) == SORD_LITERAL) { + const char* lang = sord_node_get_language(value); + + if (!lang) { + nolang = value; + } else { + switch (lilv_lang_matches(lang, syslang)) { + case LILV_LANG_MATCH_EXACT: + // Exact language match, add to results + zix_tree_insert( + (ZixTree*)values, lilv_node_new_from_node(world, value), NULL); + break; + case LILV_LANG_MATCH_PARTIAL: + // Partial language match, save in case we find no exact + partial = value; + break; + case LILV_LANG_MATCH_NONE: + break; + } + } + } else { + zix_tree_insert( + (ZixTree*)values, lilv_node_new_from_node(world, value), NULL); + } + } + sord_iter_free(stream); + free(syslang); + + if (lilv_nodes_size(values) > 0) { + return values; + } + + const SordNode* best = nolang; + if (syslang && partial) { + // Partial language match for system language + best = partial; + } else if (!best) { + // No languages matches at all, and no untranslated value + // Use any value, if possible + best = partial; + } + + if (best) { + zix_tree_insert( + (ZixTree*)values, lilv_node_new_from_node(world, best), NULL); + } else { + // No matches whatsoever + lilv_nodes_free(values); + values = NULL; + } + + return values; +} + +LilvNodes* +lilv_nodes_from_stream_objects(LilvWorld* world, + SordIter* stream, + SordQuadIndex field) +{ + if (sord_iter_end(stream)) { + sord_iter_free(stream); + return NULL; + } + + if (world->opt.filter_language) { + return lilv_nodes_from_stream_objects_i18n(world, stream, field); + } + + LilvNodes* values = lilv_nodes_new(); + FOREACH_MATCH (stream) { + const SordNode* value = sord_iter_get_node(stream, field); + LilvNode* node = lilv_node_new_from_node(world, value); + if (node) { + zix_tree_insert((ZixTree*)values, node, NULL); + } + } + sord_iter_free(stream); + return values; +} diff --git a/modules/juce_audio_processors/format_types/lv2/lilv/src/scalepoint.c b/modules/juce_audio_processors/format_types/lv2/lilv/src/scalepoint.c new file mode 100644 index 0000000000..e2db94853f --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lilv/src/scalepoint.c @@ -0,0 +1,53 @@ +/* + Copyright 2007-2019 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "lilv_internal.h" + +#include "lilv/lilv.h" + +#include + +/** Ownership of value and label is taken */ +LilvScalePoint* +lilv_scale_point_new(LilvNode* value, LilvNode* label) +{ + LilvScalePoint* point = (LilvScalePoint*)malloc(sizeof(LilvScalePoint)); + point->value = value; + point->label = label; + return point; +} + +void +lilv_scale_point_free(LilvScalePoint* point) +{ + if (point) { + lilv_node_free(point->value); + lilv_node_free(point->label); + free(point); + } +} + +const LilvNode* +lilv_scale_point_get_value(const LilvScalePoint* point) +{ + return point->value; +} + +const LilvNode* +lilv_scale_point_get_label(const LilvScalePoint* point) +{ + return point->label; +} diff --git a/modules/juce_audio_processors/format_types/lv2/lilv/src/state.c b/modules/juce_audio_processors/format_types/lv2/lilv/src/state.c new file mode 100644 index 0000000000..47608188a7 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lilv/src/state.c @@ -0,0 +1,1541 @@ +/* + Copyright 2007-2019 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "filesystem.h" +#include "lilv_internal.h" + +#include "lilv/lilv.h" +#include "serd/serd.h" +#include "sord/sord.h" +#include "sratom/sratom.h" +#include "zix/tree.h" + +#include "lv2/atom/atom.h" +#include "lv2/atom/forge.h" +#include "lv2/core/lv2.h" +#include "lv2/presets/presets.h" +#include "lv2/state/state.h" +#include "lv2/urid/urid.h" + +#include +#include +#include +#include +#include +#include +#include + +#define USTR(s) ((const uint8_t*)(s)) + +typedef struct { + void* value; ///< Value/Object + size_t size; ///< Size of value + uint32_t key; ///< Key/Predicate (URID) + uint32_t type; ///< Type of value (URID) + uint32_t flags; ///< State flags (POD, etc) +} Property; + +typedef struct { + char* symbol; ///< Symbol of port + LV2_Atom* atom; ///< Value in port +} PortValue; + +typedef struct { + char* abs; ///< Absolute path of actual file + char* rel; ///< Abstract path (relative path in state dir) +} PathMap; + +typedef struct { + size_t n; + Property* props; +} PropertyArray; + +struct LilvStateImpl { + LilvNode* plugin_uri; ///< Plugin URI + LilvNode* uri; ///< State/preset URI + char* dir; ///< Save directory (if saved) + char* scratch_dir; ///< Directory for files created by plugin + char* copy_dir; ///< Directory for snapshots of external files + char* link_dir; ///< Directory for links to external files + char* label; ///< State/Preset label + ZixTree* abs2rel; ///< PathMap sorted by abs + ZixTree* rel2abs; ///< PathMap sorted by rel + PropertyArray props; ///< State properties + PropertyArray metadata; ///< State metadata + PortValue* values; ///< Port values + uint32_t atom_Path; ///< atom:Path URID + uint32_t n_values; ///< Number of port values +}; + +static int +abs_cmp(const void* a, const void* b, const void* user_data) +{ + (void)user_data; + + return strcmp(((const PathMap*)a)->abs, ((const PathMap*)b)->abs); +} + +static int +rel_cmp(const void* a, const void* b, const void* user_data) +{ + (void)user_data; + + return strcmp(((const PathMap*)a)->rel, ((const PathMap*)b)->rel); +} + +static int +property_cmp(const void* a, const void* b) +{ + const uint32_t a_key = ((const Property*)a)->key; + const uint32_t b_key = ((const Property*)b)->key; + + if (a_key < b_key) { + return -1; + } + + if (b_key < a_key) { + return 1; + } + + return 0; +} + +static int +value_cmp(const void* a, const void* b) +{ + return strcmp(((const PortValue*)a)->symbol, ((const PortValue*)b)->symbol); +} + +static void +path_rel_free(void* ptr) +{ + free(((PathMap*)ptr)->abs); + free(((PathMap*)ptr)->rel); + free(ptr); +} + +static PortValue* +append_port_value(LilvState* state, + const char* port_symbol, + const void* value, + uint32_t size, + uint32_t type) +{ + PortValue* pv = NULL; + if (value) { + state->values = (PortValue*)realloc( + state->values, (++state->n_values) * sizeof(PortValue)); + + pv = &state->values[state->n_values - 1]; + pv->symbol = lilv_strdup(port_symbol); + pv->atom = (LV2_Atom*)malloc(sizeof(LV2_Atom) + size); + pv->atom->size = size; + pv->atom->type = type; + memcpy(pv->atom + 1, value, size); + } + return pv; +} + +static const char* +lilv_state_rel2abs(const LilvState* state, const char* path) +{ + ZixTreeIter* iter = NULL; + const PathMap key = {NULL, (char*)path}; + if (state->rel2abs && !zix_tree_find(state->rel2abs, &key, &iter)) { + return ((const PathMap*)zix_tree_get(iter))->abs; + } + return path; +} + +static void +append_property(LilvState* state, + PropertyArray* array, + uint32_t key, + const void* value, + size_t size, + uint32_t type, + uint32_t flags) +{ + array->props = + (Property*)realloc(array->props, (++array->n) * sizeof(Property)); + + Property* const prop = &array->props[array->n - 1]; + if ((flags & LV2_STATE_IS_POD) || type == state->atom_Path) { + prop->value = malloc(size); + memcpy(prop->value, value, size); + } else { + prop->value = (void*)value; + } + + prop->size = size; + prop->key = key; + prop->type = type; + prop->flags = flags; +} + +static const Property* +find_property(const LilvState* const state, const uint32_t key) +{ + if (!state->props.props) { + return NULL; + } + + const Property search_key = {NULL, 0, key, 0, 0}; + + return (const Property*)bsearch(&search_key, + state->props.props, + state->props.n, + sizeof(Property), + property_cmp); +} + +static LV2_State_Status +store_callback(LV2_State_Handle handle, + uint32_t key, + const void* value, + size_t size, + uint32_t type, + uint32_t flags) +{ + LilvState* const state = (LilvState*)handle; + + if (!key) { + return LV2_STATE_ERR_UNKNOWN; // TODO: Add status for bad arguments + } + + if (find_property((const LilvState*)handle, key)) { + return LV2_STATE_ERR_UNKNOWN; // TODO: Add status for duplicate keys + } + + append_property(state, &state->props, key, value, size, type, flags); + return LV2_STATE_SUCCESS; +} + +static const void* +retrieve_callback(LV2_State_Handle handle, + uint32_t key, + size_t* size, + uint32_t* type, + uint32_t* flags) +{ + const Property* const prop = find_property((const LilvState*)handle, key); + + if (prop) { + *size = prop->size; + *type = prop->type; + *flags = prop->flags; + return prop->value; + } + return NULL; +} + +static bool +path_exists(const char* path, const void* ignored) +{ + (void)ignored; + + return lilv_path_exists(path); +} + +static bool +lilv_state_has_path(const char* path, const void* state) +{ + return lilv_state_rel2abs((const LilvState*)state, path) != path; +} + +static char* +make_path(LV2_State_Make_Path_Handle handle, const char* path) +{ + LilvState* state = (LilvState*)handle; + lilv_create_directories(state->dir); + + return lilv_path_join(state->dir, path); +} + +static char* +abstract_path(LV2_State_Map_Path_Handle handle, const char* abs_path) +{ + LilvState* state = (LilvState*)handle; + char* path = NULL; + char* real_path = lilv_path_canonical(abs_path); + const PathMap key = {real_path, NULL}; + ZixTreeIter* iter = NULL; + + if (abs_path[0] == '\0') { + return lilv_strdup(abs_path); + } + + if (!zix_tree_find(state->abs2rel, &key, &iter)) { + // Already mapped path in a previous call + PathMap* pm = (PathMap*)zix_tree_get(iter); + free(real_path); + return lilv_strdup(pm->rel); + } + + if (lilv_path_is_child(real_path, state->dir)) { + // File in state directory (loaded, or created by plugin during save) + path = lilv_path_relative_to(real_path, state->dir); + } else if (lilv_path_is_child(real_path, state->scratch_dir)) { + // File created by plugin earlier + path = lilv_path_relative_to(real_path, state->scratch_dir); + if (state->copy_dir) { + int st = lilv_create_directories(state->copy_dir); + if (st) { + LILV_ERRORF( + "Error creating directory %s (%s)\n", state->copy_dir, strerror(st)); + } + + char* cpath = lilv_path_join(state->copy_dir, path); + char* copy = lilv_get_latest_copy(real_path, cpath); + if (!copy || !lilv_file_equals(real_path, copy)) { + // No recent enough copy, make a new one + free(copy); + copy = lilv_find_free_path(cpath, path_exists, NULL); + if ((st = lilv_copy_file(real_path, copy))) { + LILV_ERRORF("Error copying state file %s (%s)\n", copy, strerror(st)); + } + } + free(real_path); + free(cpath); + + // Refer to the latest copy in plugin state + real_path = copy; + } + } else if (state->link_dir) { + // New path outside state directory, make a link + char* const name = lilv_path_filename(real_path); + + // Find a free name in the (virtual) state directory + path = lilv_find_free_path(name, lilv_state_has_path, state); + + free(name); + } else { + // No link directory, preserve absolute path + path = lilv_strdup(abs_path); + } + + // Add record to path mapping + PathMap* pm = (PathMap*)malloc(sizeof(PathMap)); + pm->abs = real_path; + pm->rel = lilv_strdup(path); + zix_tree_insert(state->abs2rel, pm, NULL); + zix_tree_insert(state->rel2abs, pm, NULL); + + return path; +} + +static char* +absolute_path(LV2_State_Map_Path_Handle handle, const char* state_path) +{ + LilvState* state = (LilvState*)handle; + char* path = NULL; + if (lilv_path_is_absolute(state_path)) { + // Absolute path, return identical path + path = lilv_strdup(state_path); + } else if (state->dir) { + // Relative path inside state directory + path = lilv_path_join(state->dir, state_path); + } else { + // State has not been saved, unmap + path = lilv_strdup(lilv_state_rel2abs(state, state_path)); + } + + return path; +} + +/** Return a new features array with built-in features added to `features`. */ +static const LV2_Feature** +add_features(const LV2_Feature* const* features, + const LV2_Feature* map, + const LV2_Feature* make, + const LV2_Feature* free) +{ + size_t n_features = 0; + for (; features && features[n_features]; ++n_features) { + } + + const LV2_Feature** ret = + (const LV2_Feature**)calloc(n_features + 4, sizeof(LV2_Feature*)); + + if (features) { + memcpy(ret, features, n_features * sizeof(LV2_Feature*)); + } + + size_t i = n_features; + if (map) { + ret[i++] = map; + } + if (make) { + ret[i++] = make; + } + if (free) { + ret[i++] = free; + } + + return ret; +} + +/// Return the canonical path for a directory with a trailing separator +static char* +real_dir(const char* path) +{ + char* abs_path = lilv_path_canonical(path); + char* base = lilv_path_join(abs_path, NULL); + free(abs_path); + return base; +} + +static const char* +state_strerror(LV2_State_Status st) +{ + switch (st) { + case LV2_STATE_SUCCESS: + return "Completed successfully"; + case LV2_STATE_ERR_BAD_TYPE: + return "Unsupported type"; + case LV2_STATE_ERR_BAD_FLAGS: + return "Unsupported flags"; + case LV2_STATE_ERR_NO_FEATURE: + return "Missing features"; + case LV2_STATE_ERR_NO_PROPERTY: + return "Missing property"; + default: + return "Unknown error"; + } +} + +static void +lilv_free_path(LV2_State_Free_Path_Handle handle, char* path) +{ + (void)handle; + + lilv_free(path); +} + +LilvState* +lilv_state_new_from_instance(const LilvPlugin* plugin, + LilvInstance* instance, + LV2_URID_Map* map, + const char* scratch_dir, + const char* copy_dir, + const char* link_dir, + const char* save_dir, + LilvGetPortValueFunc get_value, + void* user_data, + uint32_t flags, + const LV2_Feature* const* features) +{ + const LV2_Feature** sfeatures = NULL; + LilvWorld* const world = plugin->world; + LilvState* const state = (LilvState*)calloc(1, sizeof(LilvState)); + state->plugin_uri = lilv_node_duplicate(lilv_plugin_get_uri(plugin)); + state->abs2rel = zix_tree_new(false, abs_cmp, NULL, path_rel_free); + state->rel2abs = zix_tree_new(false, rel_cmp, NULL, NULL); + state->scratch_dir = scratch_dir ? real_dir(scratch_dir) : NULL; + state->copy_dir = copy_dir ? real_dir(copy_dir) : NULL; + state->link_dir = link_dir ? real_dir(link_dir) : NULL; + state->dir = save_dir ? real_dir(save_dir) : NULL; + state->atom_Path = map->map(map->handle, LV2_ATOM__Path); + + LV2_State_Map_Path pmap = {state, abstract_path, absolute_path}; + LV2_Feature pmap_feature = {LV2_STATE__mapPath, &pmap}; + LV2_State_Make_Path pmake = {state, make_path}; + LV2_Feature pmake_feature = {LV2_STATE__makePath, &pmake}; + LV2_State_Free_Path pfree = {NULL, lilv_free_path}; + LV2_Feature pfree_feature = {LV2_STATE__freePath, &pfree}; + features = sfeatures = add_features( + features, &pmap_feature, save_dir ? &pmake_feature : NULL, &pfree_feature); + + // Store port values + if (get_value) { + LilvNode* lv2_ControlPort = lilv_new_uri(world, LILV_URI_CONTROL_PORT); + LilvNode* lv2_InputPort = lilv_new_uri(world, LILV_URI_INPUT_PORT); + for (uint32_t i = 0; i < plugin->num_ports; ++i) { + const LilvPort* const port = plugin->ports[i]; + if (lilv_port_is_a(plugin, port, lv2_ControlPort) && + lilv_port_is_a(plugin, port, lv2_InputPort)) { + uint32_t size = 0; + uint32_t type = 0; + const char* sym = lilv_node_as_string(port->symbol); + const void* value = get_value(sym, user_data, &size, &type); + append_port_value(state, sym, value, size, type); + } + } + lilv_node_free(lv2_ControlPort); + lilv_node_free(lv2_InputPort); + } + + // Store properties + const LV2_Descriptor* desc = instance->lv2_descriptor; + const LV2_State_Interface* iface = + (desc->extension_data) + ? (const LV2_State_Interface*)desc->extension_data(LV2_STATE__interface) + : NULL; + + if (iface) { + LV2_State_Status st = + iface->save(instance->lv2_handle, store_callback, state, flags, features); + if (st) { + LILV_ERRORF("Error saving plugin state: %s\n", state_strerror(st)); + free(state->props.props); + state->props.props = NULL; + state->props.n = 0; + } else { + qsort(state->props.props, state->props.n, sizeof(Property), property_cmp); + } + } + + if (state->values) { + qsort(state->values, state->n_values, sizeof(PortValue), value_cmp); + } + + free(sfeatures); + return state; +} + +void +lilv_state_emit_port_values(const LilvState* state, + LilvSetPortValueFunc set_value, + void* user_data) +{ + for (uint32_t i = 0; i < state->n_values; ++i) { + const PortValue* value = &state->values[i]; + const LV2_Atom* atom = value->atom; + set_value(value->symbol, user_data, atom + 1, atom->size, atom->type); + } +} + +void +lilv_state_restore(const LilvState* state, + LilvInstance* instance, + LilvSetPortValueFunc set_value, + void* user_data, + uint32_t flags, + const LV2_Feature* const* features) +{ + if (!state) { + LILV_ERROR("lilv_state_restore() called on NULL state\n"); + return; + } + + LV2_State_Map_Path map_path = { + (LilvState*)state, abstract_path, absolute_path}; + LV2_Feature map_feature = {LV2_STATE__mapPath, &map_path}; + + LV2_State_Free_Path free_path = {NULL, lilv_free_path}; + LV2_Feature free_feature = {LV2_STATE__freePath, &free_path}; + + if (instance) { + const LV2_Descriptor* desc = instance->lv2_descriptor; + if (desc->extension_data) { + const LV2_State_Interface* iface = + (const LV2_State_Interface*)desc->extension_data(LV2_STATE__interface); + + if (iface && iface->restore) { + const LV2_Feature** sfeatures = + add_features(features, &map_feature, NULL, &free_feature); + + iface->restore(instance->lv2_handle, + retrieve_callback, + (LV2_State_Handle)state, + flags, + sfeatures); + + free(sfeatures); + } + } + } + + if (set_value) { + lilv_state_emit_port_values(state, set_value, user_data); + } +} + +static void +set_state_dir_from_model(LilvState* state, const SordNode* graph) +{ + if (!state->dir && graph) { + const char* uri = (const char*)sord_node_get_string(graph); + char* path = lilv_file_uri_parse(uri, NULL); + + state->dir = lilv_path_join(path, NULL); + free(path); + } + assert(!state->dir || lilv_path_is_absolute(state->dir)); +} + +static LilvState* +new_state_from_model(LilvWorld* world, + LV2_URID_Map* map, + SordModel* model, + const SordNode* node, + const char* dir) +{ + // Check that we know at least something about this state subject + if (!sord_ask(model, node, 0, 0, 0)) { + return NULL; + } + + // Allocate state + LilvState* const state = (LilvState*)calloc(1, sizeof(LilvState)); + state->dir = lilv_path_join(dir, NULL); + state->atom_Path = map->map(map->handle, LV2_ATOM__Path); + state->uri = lilv_node_new_from_node(world, node); + + // Get the plugin URI this state applies to + SordIter* i = sord_search(model, node, world->uris.lv2_appliesTo, 0, 0); + if (i) { + const SordNode* object = sord_iter_get_node(i, SORD_OBJECT); + const SordNode* graph = sord_iter_get_node(i, SORD_GRAPH); + state->plugin_uri = lilv_node_new_from_node(world, object); + set_state_dir_from_model(state, graph); + sord_iter_free(i); + } else if (sord_ask( + model, node, world->uris.rdf_a, world->uris.lv2_Plugin, 0)) { + // Loading plugin description as state (default state) + state->plugin_uri = lilv_node_new_from_node(world, node); + } else { + LILV_ERRORF("State %s missing lv2:appliesTo property\n", + sord_node_get_string(node)); + } + + // Get the state label + i = sord_search(model, node, world->uris.rdfs_label, NULL, NULL); + if (i) { + const SordNode* object = sord_iter_get_node(i, SORD_OBJECT); + const SordNode* graph = sord_iter_get_node(i, SORD_GRAPH); + state->label = lilv_strdup((const char*)sord_node_get_string(object)); + set_state_dir_from_model(state, graph); + sord_iter_free(i); + } + + Sratom* sratom = sratom_new(map); + SerdChunk chunk = {NULL, 0}; + LV2_Atom_Forge forge; + lv2_atom_forge_init(&forge, map); + lv2_atom_forge_set_sink( + &forge, sratom_forge_sink, sratom_forge_deref, &chunk); + + // Get port values + SordIter* ports = sord_search(model, node, world->uris.lv2_port, 0, 0); + FOREACH_MATCH (ports) { + const SordNode* port = sord_iter_get_node(ports, SORD_OBJECT); + + SordNode* label = sord_get(model, port, world->uris.rdfs_label, 0, 0); + SordNode* symbol = sord_get(model, port, world->uris.lv2_symbol, 0, 0); + SordNode* value = sord_get(model, port, world->uris.pset_value, 0, 0); + if (!value) { + value = sord_get(model, port, world->uris.lv2_default, 0, 0); + } + if (!symbol) { + LILV_ERRORF("State `%s' port missing symbol.\n", + sord_node_get_string(node)); + } else if (value) { + chunk.len = 0; + sratom_read(sratom, &forge, world->world, model, value); + const LV2_Atom* atom = (const LV2_Atom*)chunk.buf; + + append_port_value(state, + (const char*)sord_node_get_string(symbol), + LV2_ATOM_BODY_CONST(atom), + atom->size, + atom->type); + + if (label) { + lilv_state_set_label(state, (const char*)sord_node_get_string(label)); + } + } + sord_node_free(world->world, value); + sord_node_free(world->world, symbol); + sord_node_free(world->world, label); + } + sord_iter_free(ports); + + // Get properties + SordNode* statep = sord_new_uri(world->world, USTR(LV2_STATE__state)); + SordNode* state_node = sord_get(model, node, statep, NULL, NULL); + if (state_node) { + SordIter* props = sord_search(model, state_node, 0, 0, 0); + FOREACH_MATCH (props) { + const SordNode* p = sord_iter_get_node(props, SORD_PREDICATE); + const SordNode* o = sord_iter_get_node(props, SORD_OBJECT); + const char* key = (const char*)sord_node_get_string(p); + + chunk.len = 0; + lv2_atom_forge_set_sink( + &forge, sratom_forge_sink, sratom_forge_deref, &chunk); + + sratom_read(sratom, &forge, world->world, model, o); + const LV2_Atom* atom = (const LV2_Atom*)chunk.buf; + uint32_t flags = LV2_STATE_IS_POD | LV2_STATE_IS_PORTABLE; + Property prop = {NULL, 0, 0, 0, flags}; + + prop.key = map->map(map->handle, key); + prop.type = atom->type; + prop.size = atom->size; + prop.value = malloc(atom->size); + memcpy(prop.value, LV2_ATOM_BODY_CONST(atom), atom->size); + if (atom->type == forge.Path) { + prop.flags = LV2_STATE_IS_POD; + } + + if (prop.value) { + state->props.props = (Property*)realloc( + state->props.props, (++state->props.n) * sizeof(Property)); + state->props.props[state->props.n - 1] = prop; + } + } + sord_iter_free(props); + } + sord_node_free(world->world, state_node); + sord_node_free(world->world, statep); + + serd_free((void*)chunk.buf); + sratom_free(sratom); + + if (state->props.props) { + qsort(state->props.props, state->props.n, sizeof(Property), property_cmp); + } + if (state->values) { + qsort(state->values, state->n_values, sizeof(PortValue), value_cmp); + } + + return state; +} + +LilvState* +lilv_state_new_from_world(LilvWorld* world, + LV2_URID_Map* map, + const LilvNode* node) +{ + if (!lilv_node_is_uri(node) && !lilv_node_is_blank(node)) { + LILV_ERRORF("Subject `%s' is not a URI or blank node.\n", + lilv_node_as_string(node)); + return NULL; + } + + return new_state_from_model(world, map, world->model, node->node, NULL); +} + +LilvState* +lilv_state_new_from_file(LilvWorld* world, + LV2_URID_Map* map, + const LilvNode* subject, + const char* path) +{ + if (subject && !lilv_node_is_uri(subject) && !lilv_node_is_blank(subject)) { + LILV_ERRORF("Subject `%s' is not a URI or blank node.\n", + lilv_node_as_string(subject)); + return NULL; + } + + uint8_t* abs_path = (uint8_t*)lilv_path_absolute(path); + SerdNode node = serd_node_new_file_uri(abs_path, NULL, NULL, true); + SerdEnv* env = serd_env_new(&node); + SordModel* model = sord_new(world->world, SORD_SPO, false); + SerdReader* reader = sord_new_reader(model, env, SERD_TURTLE, NULL); + + serd_reader_read_file(reader, node.buf); + + SordNode* subject_node = + (subject) ? subject->node + : sord_node_from_serd_node(world->world, env, &node, NULL, NULL); + + char* dirname = lilv_path_parent(path); + char* real_path = lilv_path_canonical(dirname); + char* dir_path = lilv_path_join(real_path, NULL); + LilvState* state = + new_state_from_model(world, map, model, subject_node, dir_path); + free(dir_path); + free(real_path); + free(dirname); + + serd_node_free(&node); + free(abs_path); + serd_reader_free(reader); + sord_free(model); + serd_env_free(env); + return state; +} + +static void +set_prefixes(SerdEnv* env) +{ +#define SET_PSET(e, p, u) serd_env_set_prefix_from_strings(e, p, u) + SET_PSET(env, USTR("atom"), USTR(LV2_ATOM_PREFIX)); + SET_PSET(env, USTR("lv2"), USTR(LV2_CORE_PREFIX)); + SET_PSET(env, USTR("pset"), USTR(LV2_PRESETS_PREFIX)); + SET_PSET(env, USTR("rdf"), USTR(LILV_NS_RDF)); + SET_PSET(env, USTR("rdfs"), USTR(LILV_NS_RDFS)); + SET_PSET(env, USTR("state"), USTR(LV2_STATE_PREFIX)); + SET_PSET(env, USTR("xsd"), USTR(LILV_NS_XSD)); +} + +LilvState* +lilv_state_new_from_string(LilvWorld* world, LV2_URID_Map* map, const char* str) +{ + if (!str) { + return NULL; + } + + SerdNode base = SERD_NODE_NULL; + SerdEnv* env = serd_env_new(&base); + SordModel* model = sord_new(world->world, SORD_SPO | SORD_OPS, false); + SerdReader* reader = sord_new_reader(model, env, SERD_TURTLE, NULL); + + set_prefixes(env); + serd_reader_read_string(reader, USTR(str)); + + SordNode* o = sord_new_uri(world->world, USTR(LV2_PRESETS__Preset)); + SordNode* s = sord_get(model, NULL, world->uris.rdf_a, o, NULL); + + LilvState* state = new_state_from_model(world, map, model, s, NULL); + + sord_node_free(world->world, s); + sord_node_free(world->world, o); + serd_reader_free(reader); + sord_free(model); + serd_env_free(env); + + return state; +} + +static SerdWriter* +ttl_writer(SerdSink sink, void* stream, const SerdNode* base, SerdEnv** new_env) +{ + SerdURI base_uri = SERD_URI_NULL; + if (base && base->buf) { + serd_uri_parse(base->buf, &base_uri); + } + + SerdEnv* env = *new_env ? *new_env : serd_env_new(base); + set_prefixes(env); + + SerdWriter* writer = + serd_writer_new(SERD_TURTLE, + (SerdStyle)(SERD_STYLE_RESOLVED | SERD_STYLE_ABBREVIATED | + SERD_STYLE_CURIED), + env, + &base_uri, + sink, + stream); + + if (!*new_env) { + *new_env = env; + } + + return writer; +} + +static SerdWriter* +ttl_file_writer(FILE* fd, const SerdNode* node, SerdEnv** env) +{ + SerdWriter* writer = ttl_writer(serd_file_sink, fd, node, env); + + fseek(fd, 0, SEEK_END); + if (ftell(fd) == 0) { + serd_env_foreach(*env, (SerdPrefixSink)serd_writer_set_prefix, writer); + } else { + fprintf(fd, "\n"); + } + + return writer; +} + +static void +add_to_model(SordWorld* world, + SerdEnv* env, + SordModel* model, + const SerdNode s, + const SerdNode p, + const SerdNode o) +{ + SordNode* ss = sord_node_from_serd_node(world, env, &s, NULL, NULL); + SordNode* sp = sord_node_from_serd_node(world, env, &p, NULL, NULL); + SordNode* so = sord_node_from_serd_node(world, env, &o, NULL, NULL); + + SordQuad quad = {ss, sp, so, NULL}; + sord_add(model, quad); + + sord_node_free(world, ss); + sord_node_free(world, sp); + sord_node_free(world, so); +} + +static void +remove_manifest_entry(SordWorld* world, SordModel* model, const char* subject) +{ + SordNode* s = sord_new_uri(world, USTR(subject)); + SordIter* i = sord_search(model, s, NULL, NULL, NULL); + while (!sord_iter_end(i)) { + sord_erase(model, i); + } + sord_iter_free(i); + sord_node_free(world, s); +} + +static int +write_manifest(LilvWorld* world, + SerdEnv* env, + SordModel* model, + const SerdNode* file_uri) +{ + (void)world; + + char* const path = (char*)serd_file_uri_parse(file_uri->buf, NULL); + FILE* const wfd = fopen(path, "w"); + if (!wfd) { + LILV_ERRORF("Failed to open %s for writing (%s)\n", path, strerror(errno)); + + serd_free(path); + return 1; + } + + SerdWriter* writer = ttl_file_writer(wfd, file_uri, &env); + sord_write(model, writer, NULL); + serd_writer_free(writer); + fclose(wfd); + serd_free(path); + return 0; +} + +static int +add_state_to_manifest(LilvWorld* lworld, + const LilvNode* plugin_uri, + const char* manifest_path, + const char* state_uri, + const char* state_path) +{ + SordWorld* world = lworld->world; + SerdNode manifest = serd_node_new_file_uri(USTR(manifest_path), 0, 0, 1); + SerdNode file = serd_node_new_file_uri(USTR(state_path), 0, 0, 1); + SerdEnv* env = serd_env_new(&manifest); + SordModel* model = sord_new(world, SORD_SPO, false); + + if (lilv_path_exists(manifest_path)) { + // Read manifest into model + SerdReader* reader = sord_new_reader(model, env, SERD_TURTLE, NULL); + SerdStatus st = serd_reader_read_file(reader, manifest.buf); + if (st) { + LILV_WARNF("Failed to read manifest (%s)\n", serd_strerror(st)); + } + serd_reader_free(reader); + } + + // Choose state URI (use file URI if not given) + if (!state_uri) { + state_uri = (const char*)file.buf; + } + + // Remove any existing manifest entries for this state + remove_manifest_entry(world, model, state_uri); + + // Add manifest entry for this state to model + SerdNode s = serd_node_from_string(SERD_URI, USTR(state_uri)); + + // a pset:Preset + add_to_model(world, + env, + model, + s, + serd_node_from_string(SERD_URI, USTR(LILV_NS_RDF "type")), + serd_node_from_string(SERD_URI, USTR(LV2_PRESETS__Preset))); + + // a pset:Preset + add_to_model(world, + env, + model, + s, + serd_node_from_string(SERD_URI, USTR(LILV_NS_RDF "type")), + serd_node_from_string(SERD_URI, USTR(LV2_PRESETS__Preset))); + + // rdfs:seeAlso + add_to_model(world, + env, + model, + s, + serd_node_from_string(SERD_URI, USTR(LILV_NS_RDFS "seeAlso")), + file); + + // lv2:appliesTo + add_to_model( + world, + env, + model, + s, + serd_node_from_string(SERD_URI, USTR(LV2_CORE__appliesTo)), + serd_node_from_string(SERD_URI, USTR(lilv_node_as_string(plugin_uri)))); + + /* Re-open manifest for locked writing. We need to do this because it may + need to be truncated, and the file can only be open once on Windows. */ + + FILE* wfd = fopen(manifest_path, "wb"); + int r = 0; + if (!wfd) { + LILV_ERRORF( + "Failed to open %s for writing (%s)\n", manifest_path, strerror(errno)); + r = 1; + } + + SerdWriter* writer = ttl_file_writer(wfd, &manifest, &env); + lilv_flock(wfd, true, true); + sord_write(model, writer, NULL); + lilv_flock(wfd, false, true); + serd_writer_free(writer); + fclose(wfd); + + sord_free(model); + serd_node_free(&file); + serd_node_free(&manifest); + serd_env_free(env); + + return r; +} + +static bool +link_exists(const char* path, const void* data) +{ + const char* target = (const char*)data; + if (!lilv_path_exists(path)) { + return false; + } + char* real_path = lilv_path_canonical(path); + bool matches = !strcmp(real_path, target); + free(real_path); + return !matches; +} + +static int +maybe_symlink(const char* oldpath, const char* newpath) +{ + if (link_exists(newpath, oldpath)) { + return 0; + } + + const int st = lilv_symlink(oldpath, newpath); + if (st) { + LILV_ERRORF( + "Failed to link %s => %s (%s)\n", newpath, oldpath, strerror(errno)); + } + + return st; +} + +static void +write_property_array(const LilvState* state, + const PropertyArray* array, + Sratom* sratom, + uint32_t flags, + const SerdNode* subject, + LV2_URID_Unmap* unmap, + const char* dir) +{ + for (uint32_t i = 0; i < array->n; ++i) { + Property* prop = &array->props[i]; + const char* key = unmap->unmap(unmap->handle, prop->key); + + const SerdNode p = serd_node_from_string(SERD_URI, USTR(key)); + if (prop->type == state->atom_Path && !dir) { + const char* path = (const char*)prop->value; + const char* abs_path = lilv_state_rel2abs(state, path); + LILV_WARNF("Writing absolute path %s\n", abs_path); + sratom_write(sratom, + unmap, + flags, + subject, + &p, + prop->type, + strlen(abs_path) + 1, + abs_path); + } else if (prop->flags & LV2_STATE_IS_POD || + prop->type == state->atom_Path) { + sratom_write( + sratom, unmap, flags, subject, &p, prop->type, prop->size, prop->value); + } else { + LILV_WARNF("Lost non-POD property <%s> on save\n", key); + } + } +} + +static int +lilv_state_write(LilvWorld* world, + LV2_URID_Map* map, + LV2_URID_Unmap* unmap, + const LilvState* state, + SerdWriter* writer, + const char* uri, + const char* dir) +{ + (void)world; + + SerdNode lv2_appliesTo = + serd_node_from_string(SERD_CURIE, USTR("lv2:appliesTo")); + + const SerdNode* plugin_uri = sord_node_to_serd_node(state->plugin_uri->node); + + SerdNode subject = serd_node_from_string(SERD_URI, USTR(uri ? uri : "")); + + // a pset:Preset + SerdNode p = serd_node_from_string(SERD_URI, USTR(LILV_NS_RDF "type")); + + SerdNode o = serd_node_from_string(SERD_URI, USTR(LV2_PRESETS__Preset)); + serd_writer_write_statement(writer, 0, NULL, &subject, &p, &o, NULL, NULL); + + // lv2:appliesTo + serd_writer_write_statement( + writer, 0, NULL, &subject, &lv2_appliesTo, plugin_uri, NULL, NULL); + + // rdfs:label label + if (state->label) { + p = serd_node_from_string(SERD_URI, USTR(LILV_NS_RDFS "label")); + o = serd_node_from_string(SERD_LITERAL, USTR(state->label)); + serd_writer_write_statement(writer, 0, NULL, &subject, &p, &o, NULL, NULL); + } + + SerdEnv* env = serd_writer_get_env(writer); + const SerdNode* base = serd_env_get_base_uri(env, NULL); + + Sratom* sratom = sratom_new(map); + sratom_set_sink(sratom, + (const char*)base->buf, + (SerdStatementSink)serd_writer_write_statement, + (SerdEndSink)serd_writer_end_anon, + writer); + + // Write metadata + sratom_set_pretty_numbers(sratom, false); // Use precise types + write_property_array( + state, &state->metadata, sratom, 0, &subject, unmap, dir); + + // Write port values + sratom_set_pretty_numbers(sratom, true); // Use pretty numbers + for (uint32_t i = 0; i < state->n_values; ++i) { + PortValue* const value = &state->values[i]; + + const SerdNode port = + serd_node_from_string(SERD_BLANK, USTR(value->symbol)); + + // <> lv2:port _:symbol + p = serd_node_from_string(SERD_URI, USTR(LV2_CORE__port)); + serd_writer_write_statement( + writer, SERD_ANON_O_BEGIN, NULL, &subject, &p, &port, NULL, NULL); + + // _:symbol lv2:symbol "symbol" + p = serd_node_from_string(SERD_URI, USTR(LV2_CORE__symbol)); + o = serd_node_from_string(SERD_LITERAL, USTR(value->symbol)); + serd_writer_write_statement( + writer, SERD_ANON_CONT, NULL, &port, &p, &o, NULL, NULL); + + // _:symbol pset:value value + p = serd_node_from_string(SERD_URI, USTR(LV2_PRESETS__value)); + sratom_write(sratom, + unmap, + SERD_ANON_CONT, + &port, + &p, + value->atom->type, + value->atom->size, + value->atom + 1); + + serd_writer_end_anon(writer, &port); + } + + // Write properties + const SerdNode body = serd_node_from_string(SERD_BLANK, USTR("body")); + if (state->props.n > 0) { + p = serd_node_from_string(SERD_URI, USTR(LV2_STATE__state)); + serd_writer_write_statement( + writer, SERD_ANON_O_BEGIN, NULL, &subject, &p, &body, NULL, NULL); + } + sratom_set_pretty_numbers(sratom, false); // Use precise types + write_property_array( + state, &state->props, sratom, SERD_ANON_CONT, &body, unmap, dir); + + if (state->props.n > 0) { + serd_writer_end_anon(writer, &body); + } + + sratom_free(sratom); + return 0; +} + +static void +lilv_state_make_links(const LilvState* state, const char* dir) +{ + // Create symlinks to files + for (ZixTreeIter* i = zix_tree_begin(state->abs2rel); + i != zix_tree_end(state->abs2rel); + i = zix_tree_iter_next(i)) { + const PathMap* pm = (const PathMap*)zix_tree_get(i); + + char* path = lilv_path_absolute_child(pm->rel, dir); + if (lilv_path_is_child(pm->abs, state->copy_dir) && + strcmp(state->copy_dir, dir)) { + // Link directly to snapshot in the copy directory + maybe_symlink(pm->abs, path); + } else if (!lilv_path_is_child(pm->abs, dir)) { + const char* link_dir = state->link_dir ? state->link_dir : dir; + char* pat = lilv_path_absolute_child(pm->rel, link_dir); + if (!strcmp(dir, link_dir)) { + // Link directory is save directory, make link at exact path + remove(pat); + maybe_symlink(pm->abs, pat); + } else { + // Make a link in the link directory to external file + char* lpath = lilv_find_free_path(pat, link_exists, pm->abs); + if (!lilv_path_exists(lpath)) { + if (lilv_symlink(pm->abs, lpath)) { + LILV_ERRORF("Failed to link %s => %s (%s)\n", + pm->abs, + lpath, + strerror(errno)); + } + } + + // Make a link in the save directory to the external link + char* target = lilv_path_relative_to(lpath, dir); + maybe_symlink(lpath, path); + free(target); + free(lpath); + } + free(pat); + } + free(path); + } +} + +int +lilv_state_save(LilvWorld* world, + LV2_URID_Map* map, + LV2_URID_Unmap* unmap, + const LilvState* state, + const char* uri, + const char* dir, + const char* filename) +{ + if (!filename || !dir || lilv_create_directories(dir)) { + return 1; + } + + char* abs_dir = real_dir(dir); + char* const path = lilv_path_join(abs_dir, filename); + FILE* fd = fopen(path, "w"); + if (!fd) { + LILV_ERRORF("Failed to open %s (%s)\n", path, strerror(errno)); + free(abs_dir); + free(path); + return 4; + } + + // Create symlinks to files if necessary + lilv_state_make_links(state, abs_dir); + + // Write state to Turtle file + SerdNode file = serd_node_new_file_uri(USTR(path), NULL, NULL, true); + SerdNode node = uri ? serd_node_from_string(SERD_URI, USTR(uri)) : file; + SerdEnv* env = NULL; + SerdWriter* ttl = ttl_file_writer(fd, &file, &env); + int ret = + lilv_state_write(world, map, unmap, state, ttl, (const char*)node.buf, dir); + + // Set saved dir and uri (FIXME: const violation) + free(state->dir); + lilv_node_free(state->uri); + ((LilvState*)state)->dir = lilv_strdup(abs_dir); + ((LilvState*)state)->uri = lilv_new_uri(world, (const char*)node.buf); + + serd_node_free(&file); + serd_writer_free(ttl); + serd_env_free(env); + fclose(fd); + + // Add entry to manifest + if (!ret) { + char* const manifest = lilv_path_join(abs_dir, "manifest.ttl"); + + ret = add_state_to_manifest(world, state->plugin_uri, manifest, uri, path); + + free(manifest); + } + + free(abs_dir); + free(path); + return ret; +} + +char* +lilv_state_to_string(LilvWorld* world, + LV2_URID_Map* map, + LV2_URID_Unmap* unmap, + const LilvState* state, + const char* uri, + const char* base_uri) +{ + if (!uri) { + LILV_ERROR("Attempt to serialise state with no URI\n"); + return NULL; + } + + SerdChunk chunk = {NULL, 0}; + SerdEnv* env = NULL; + SerdNode base = serd_node_from_string(SERD_URI, USTR(base_uri)); + SerdWriter* writer = ttl_writer(serd_chunk_sink, &chunk, &base, &env); + + lilv_state_write(world, map, unmap, state, writer, uri, NULL); + + serd_writer_free(writer); + serd_env_free(env); + char* str = (char*)serd_chunk_sink_finish(&chunk); + char* result = lilv_strdup(str); + serd_free(str); + return result; +} + +static void +try_unlink(const char* state_dir, const char* path) +{ + if (!strncmp(state_dir, path, strlen(state_dir))) { + if (lilv_path_exists(path) && lilv_remove(path)) { + LILV_ERRORF("Failed to remove %s (%s)\n", path, strerror(errno)); + } + } +} + +static char* +get_canonical_path(const LilvNode* const node) +{ + char* const path = lilv_node_get_path(node, NULL); + char* const real_path = lilv_path_canonical(path); + + free(path); + return real_path; +} + +int +lilv_state_delete(LilvWorld* world, const LilvState* state) +{ + if (!state->dir) { + LILV_ERROR("Attempt to delete unsaved state\n"); + return -1; + } + + LilvNode* bundle = lilv_new_file_uri(world, NULL, state->dir); + LilvNode* manifest = lilv_world_get_manifest_uri(world, bundle); + char* manifest_path = get_canonical_path(manifest); + const bool has_manifest = lilv_path_exists(manifest_path); + SordModel* model = sord_new(world->world, SORD_SPO, false); + + if (has_manifest) { + // Read manifest into temporary local model + SerdEnv* env = serd_env_new(sord_node_to_serd_node(manifest->node)); + SerdReader* ttl = sord_new_reader(model, env, SERD_TURTLE, NULL); + serd_reader_read_file(ttl, USTR(manifest_path)); + serd_reader_free(ttl); + serd_env_free(env); + } + + if (state->uri) { + SordNode* file = + sord_get(model, state->uri->node, world->uris.rdfs_seeAlso, NULL, NULL); + if (file) { + // Remove state file + const uint8_t* uri = sord_node_get_string(file); + char* path = (char*)serd_file_uri_parse(uri, NULL); + char* real_path = lilv_path_canonical(path); + if (path) { + try_unlink(state->dir, real_path); + } + serd_free(real_path); + serd_free(path); + } + + // Remove any existing manifest entries for this state + const char* state_uri_str = lilv_node_as_string(state->uri); + remove_manifest_entry(world->world, model, state_uri_str); + remove_manifest_entry(world->world, world->model, state_uri_str); + } + + // Drop bundle from model + lilv_world_unload_bundle(world, bundle); + + if (sord_num_quads(model) == 0) { + // Manifest is empty, attempt to remove bundle entirely + if (has_manifest) { + try_unlink(state->dir, manifest_path); + } + + // Remove all known files from state bundle + if (state->abs2rel) { + // State created from instance, get paths from map + for (ZixTreeIter* i = zix_tree_begin(state->abs2rel); + i != zix_tree_end(state->abs2rel); + i = zix_tree_iter_next(i)) { + const PathMap* pm = (const PathMap*)zix_tree_get(i); + char* path = lilv_path_join(state->dir, pm->rel); + try_unlink(state->dir, path); + free(path); + } + } else { + // State loaded from model, get paths from loaded properties + for (uint32_t i = 0; i < state->props.n; ++i) { + const Property* const p = &state->props.props[i]; + if (p->type == state->atom_Path) { + try_unlink(state->dir, (const char*)p->value); + } + } + } + + if (lilv_remove(state->dir)) { + LILV_ERRORF( + "Failed to remove directory %s (%s)\n", state->dir, strerror(errno)); + } + } else { + // Still something in the manifest, update and reload bundle + const SerdNode* manifest_node = sord_node_to_serd_node(manifest->node); + SerdEnv* env = serd_env_new(manifest_node); + + write_manifest(world, env, model, manifest_node); + lilv_world_load_bundle(world, bundle); + serd_env_free(env); + } + + sord_free(model); + lilv_free(manifest_path); + lilv_node_free(manifest); + lilv_node_free(bundle); + + return 0; +} + +static void +free_property_array(LilvState* state, PropertyArray* array) +{ + for (uint32_t i = 0; i < array->n; ++i) { + Property* prop = &array->props[i]; + if ((prop->flags & LV2_STATE_IS_POD) || prop->type == state->atom_Path) { + free(prop->value); + } + } + free(array->props); +} + +void +lilv_state_free(LilvState* state) +{ + if (state) { + free_property_array(state, &state->props); + free_property_array(state, &state->metadata); + for (uint32_t i = 0; i < state->n_values; ++i) { + free(state->values[i].atom); + free(state->values[i].symbol); + } + lilv_node_free(state->plugin_uri); + lilv_node_free(state->uri); + zix_tree_free(state->abs2rel); + zix_tree_free(state->rel2abs); + free(state->values); + free(state->label); + free(state->dir); + free(state->scratch_dir); + free(state->copy_dir); + free(state->link_dir); + free(state); + } +} + +bool +lilv_state_equals(const LilvState* a, const LilvState* b) +{ + if (!lilv_node_equals(a->plugin_uri, b->plugin_uri) || + (a->label && !b->label) || (b->label && !a->label) || + (a->label && b->label && strcmp(a->label, b->label)) || + a->props.n != b->props.n || a->n_values != b->n_values) { + return false; + } + + for (uint32_t i = 0; i < a->n_values; ++i) { + PortValue* const av = &a->values[i]; + PortValue* const bv = &b->values[i]; + if (av->atom->size != bv->atom->size || av->atom->type != bv->atom->type || + strcmp(av->symbol, bv->symbol) || + memcmp(av->atom + 1, bv->atom + 1, av->atom->size)) { + return false; + } + } + + for (uint32_t i = 0; i < a->props.n; ++i) { + Property* const ap = &a->props.props[i]; + Property* const bp = &b->props.props[i]; + if (ap->key != bp->key || ap->type != bp->type || ap->flags != bp->flags) { + return false; + } + + if (ap->type == a->atom_Path) { + if (!lilv_file_equals(lilv_state_rel2abs(a, (char*)ap->value), + lilv_state_rel2abs(b, (char*)bp->value))) { + return false; + } + } else if (ap->size != bp->size || memcmp(ap->value, bp->value, ap->size)) { + return false; + } + } + + return true; +} + +unsigned +lilv_state_get_num_properties(const LilvState* state) +{ + return state->props.n; +} + +const LilvNode* +lilv_state_get_plugin_uri(const LilvState* state) +{ + return state->plugin_uri; +} + +const LilvNode* +lilv_state_get_uri(const LilvState* state) +{ + return state->uri; +} + +const char* +lilv_state_get_label(const LilvState* state) +{ + return state->label; +} + +void +lilv_state_set_label(LilvState* state, const char* label) +{ + const size_t len = strlen(label); + state->label = (char*)realloc(state->label, len + 1); + memcpy(state->label, label, len + 1); +} + +int +lilv_state_set_metadata(LilvState* state, + uint32_t key, + const void* value, + size_t size, + uint32_t type, + uint32_t flags) +{ + append_property(state, &state->metadata, key, value, size, type, flags); + return LV2_STATE_SUCCESS; +} diff --git a/modules/juce_audio_processors/format_types/lv2/lilv/src/ui.c b/modules/juce_audio_processors/format_types/lv2/lilv/src/ui.c new file mode 100644 index 0000000000..84f8cef0c3 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lilv/src/ui.c @@ -0,0 +1,115 @@ +/* + Copyright 2007-2019 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "lilv_internal.h" + +#include "lilv/lilv.h" +#include "zix/tree.h" + +#include +#include +#include +#include + +LilvUI* +lilv_ui_new(LilvWorld* world, + LilvNode* uri, + LilvNode* type_uri, + LilvNode* binary_uri) +{ + assert(uri); + assert(type_uri); + assert(binary_uri); + + LilvUI* ui = (LilvUI*)malloc(sizeof(LilvUI)); + ui->world = world; + ui->uri = uri; + ui->binary_uri = binary_uri; + + // FIXME: kludge + char* bundle = lilv_strdup(lilv_node_as_string(ui->binary_uri)); + char* last_slash = strrchr(bundle, '/') + 1; + *last_slash = '\0'; + ui->bundle_uri = lilv_new_uri(world, bundle); + free(bundle); + + ui->classes = lilv_nodes_new(); + zix_tree_insert((ZixTree*)ui->classes, type_uri, NULL); + + return ui; +} + +void +lilv_ui_free(LilvUI* ui) +{ + lilv_node_free(ui->uri); + lilv_node_free(ui->bundle_uri); + lilv_node_free(ui->binary_uri); + lilv_nodes_free(ui->classes); + free(ui); +} + +const LilvNode* +lilv_ui_get_uri(const LilvUI* ui) +{ + return ui->uri; +} + +unsigned +lilv_ui_is_supported(const LilvUI* ui, + LilvUISupportedFunc supported_func, + const LilvNode* container_type, + const LilvNode** ui_type) +{ + const LilvNodes* classes = lilv_ui_get_classes(ui); + LILV_FOREACH (nodes, c, classes) { + const LilvNode* type = lilv_nodes_get(classes, c); + const unsigned q = + supported_func(lilv_node_as_uri(container_type), lilv_node_as_uri(type)); + if (q) { + if (ui_type) { + *ui_type = type; + } + return q; + } + } + + return 0; +} + +const LilvNodes* +lilv_ui_get_classes(const LilvUI* ui) +{ + return ui->classes; +} + +bool +lilv_ui_is_a(const LilvUI* ui, const LilvNode* class_uri) +{ + return lilv_nodes_contains(ui->classes, class_uri); +} + +const LilvNode* +lilv_ui_get_bundle_uri(const LilvUI* ui) +{ + return ui->bundle_uri; +} + +const LilvNode* +lilv_ui_get_binary_uri(const LilvUI* ui) +{ + return ui->binary_uri; +} diff --git a/modules/juce_audio_processors/format_types/lv2/lilv/src/util.c b/modules/juce_audio_processors/format_types/lv2/lilv/src/util.c new file mode 100644 index 0000000000..92f72b9ed0 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lilv/src/util.c @@ -0,0 +1,291 @@ +/* + Copyright 2007-2019 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "filesystem.h" +#include "lilv_internal.h" + +#include "lilv/lilv.h" +#include "serd/serd.h" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void +lilv_free(void* ptr) +{ + free(ptr); +} + +char* +lilv_strjoin(const char* first, ...) +{ + size_t len = strlen(first); + char* result = (char*)malloc(len + 1); + + memcpy(result, first, len); + + va_list args; + va_start(args, first); + while (1) { + const char* const s = va_arg(args, const char*); + if (s == NULL) { + break; + } + + const size_t this_len = strlen(s); + char* new_result = (char*)realloc(result, len + this_len + 1); + if (!new_result) { + va_end(args); + free(result); + return NULL; + } + + result = new_result; + memcpy(result + len, s, this_len); + len += this_len; + } + va_end(args); + + result[len] = '\0'; + + return result; +} + +char* +lilv_strdup(const char* str) +{ + if (!str) { + return NULL; + } + + const size_t len = strlen(str); + char* copy = (char*)malloc(len + 1); + memcpy(copy, str, len + 1); + return copy; +} + +const char* +lilv_uri_to_path(const char* uri) +{ +#if defined(__GNUC__) && __GNUC__ > 4 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif + + return (const char*)serd_uri_to_path((const uint8_t*)uri); + +#if defined(__GNUC__) && __GNUC__ > 4 +# pragma GCC diagnostic pop +#endif +} + +char* +lilv_file_uri_parse(const char* uri, char** hostname) +{ + return (char*)serd_file_uri_parse((const uint8_t*)uri, (uint8_t**)hostname); +} + +/** Return the current LANG converted to Turtle (i.e. RFC3066) style. + * For example, if LANG is set to "en_CA.utf-8", this returns "en-ca". + */ +char* +lilv_get_lang(void) +{ + const char* const env_lang = getenv("LANG"); + if (!env_lang || !strcmp(env_lang, "") || !strcmp(env_lang, "C") || + !strcmp(env_lang, "POSIX")) { + return NULL; + } + + const size_t env_lang_len = strlen(env_lang); + char* const lang = (char*)malloc(env_lang_len + 1); + for (size_t i = 0; i < env_lang_len + 1; ++i) { + if (env_lang[i] == '_') { + lang[i] = '-'; // Convert _ to - + } else if (env_lang[i] >= 'A' && env_lang[i] <= 'Z') { + lang[i] = env_lang[i] + ('a' - 'A'); // Convert to lowercase + } else if (env_lang[i] >= 'a' && env_lang[i] <= 'z') { + lang[i] = env_lang[i]; // Lowercase letter, copy verbatim + } else if (env_lang[i] >= '0' && env_lang[i] <= '9') { + lang[i] = env_lang[i]; // Digit, copy verbatim + } else if (env_lang[i] == '\0' || env_lang[i] == '.') { + // End, or start of suffix (e.g. en_CA.utf-8), finished + lang[i] = '\0'; + break; + } else { + LILV_ERRORF("Illegal LANG `%s' ignored\n", env_lang); + free(lang); + return NULL; + } + } + + return lang; +} + +#ifndef _WIN32 + +/** Append suffix to dst, update dst_len, and return the realloc'd result. */ +static char* +strappend(char* dst, size_t* dst_len, const char* suffix, size_t suffix_len) +{ + dst = (char*)realloc(dst, *dst_len + suffix_len + 1); + memcpy(dst + *dst_len, suffix, suffix_len); + dst[(*dst_len += suffix_len)] = '\0'; + return dst; +} + +/** Append the value of the environment variable var to dst. */ +static char* +append_var(char* dst, size_t* dst_len, const char* var) +{ + // Get value from environment + const char* val = getenv(var); + if (val) { // Value found, append it + return strappend(dst, dst_len, val, strlen(val)); + } + + // No value found, append variable reference as-is + return strappend(strappend(dst, dst_len, "$", 1), dst_len, var, strlen(var)); +} + +#endif + +/** Expand variables (e.g. POSIX ~ or $FOO, Windows %FOO%) in `path`. */ +char* +lilv_expand(const char* path) +{ +#ifdef _WIN32 + char* out = (char*)malloc(MAX_PATH); + ExpandEnvironmentStrings(path, out, MAX_PATH); +#else + char* out = NULL; + size_t len = 0; + + const char* start = path; // Start of current chunk to copy + for (const char* s = path; *s;) { + if (*s == '$') { + // Hit $ (variable reference, e.g. $VAR_NAME) + for (const char* t = s + 1;; ++t) { + if (!*t || (!isupper(*t) && !isdigit(*t) && *t != '_')) { + // Append preceding chunk + out = strappend(out, &len, start, s - start); + + // Append variable value (or $VAR_NAME if not found) + char* var = (char*)calloc(t - s, 1); + memcpy(var, s + 1, t - s - 1); + out = append_var(out, &len, var); + free(var); + + // Continue after variable reference + start = s = t; + break; + } + } + } else if (*s == '~' && (*(s + 1) == '/' || !*(s + 1))) { + // Hit ~ before slash or end of string (home directory reference) + out = strappend(out, &len, start, s - start); + out = append_var(out, &len, "HOME"); + start = ++s; + } else { + ++s; + } + } + + if (*start) { + out = strappend(out, &len, start, strlen(start)); + } +#endif + + return out; +} + +char* +lilv_find_free_path(const char* in_path, + bool (*exists)(const char*, const void*), + const void* user_data) +{ + const size_t in_path_len = strlen(in_path); + char* path = (char*)malloc(in_path_len + 7); + memcpy(path, in_path, in_path_len + 1); + + for (unsigned i = 2; i < 1000000u; ++i) { + if (!exists(path, user_data)) { + return path; + } + snprintf(path, in_path_len + 7, "%s.%u", in_path, i); + } + + return NULL; +} + +typedef struct { + char* pattern; + time_t time; + char* latest; +} Latest; + +static void +update_latest(const char* path, const char* name, void* data) +{ + Latest* latest = (Latest*)data; + char* entry_path = lilv_path_join(path, name); + unsigned num = 0; + if (sscanf(entry_path, latest->pattern, &num) == 1) { + struct stat st; + if (!stat(entry_path, &st)) { + if (st.st_mtime >= latest->time) { + free(latest->latest); + latest->latest = entry_path; + } + } else { + LILV_ERRORF("stat(%s) (%s)\n", path, strerror(errno)); + } + } + if (entry_path != latest->latest) { + free(entry_path); + } +} + +/** Return the latest copy of the file at `path` that is newer. */ +char* +lilv_get_latest_copy(const char* path, const char* copy_path) +{ + char* copy_dir = lilv_path_parent(copy_path); + Latest latest = {lilv_strjoin(copy_path, ".%u", NULL), 0, NULL}; + + struct stat st; + if (!stat(path, &st)) { + latest.time = st.st_mtime; + } else { + LILV_ERRORF("stat(%s) (%s)\n", path, strerror(errno)); + } + + lilv_dir_for_each(copy_dir, &latest, update_latest); + + free(latest.pattern); + free(copy_dir); + return latest.latest; +} diff --git a/modules/juce_audio_processors/format_types/lv2/lilv/src/world.c b/modules/juce_audio_processors/format_types/lv2/lilv/src/world.c new file mode 100644 index 0000000000..0e9bd2e2be --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lilv/src/world.c @@ -0,0 +1,1249 @@ +/* + Copyright 2007-2019 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "filesystem.h" +#include "lilv_config.h" // IWYU pragma: keep +#include "lilv_internal.h" + +#include "lilv/lilv.h" +#include "serd/serd.h" +#include "sord/sord.h" +#include "zix/common.h" +#include "zix/tree.h" + +#include "lv2/core/lv2.h" +#include "lv2/presets/presets.h" + +#ifdef LILV_DYN_MANIFEST +# include "lv2/dynmanifest/dynmanifest.h" +#endif + +#include +#include +#include +#include +#include +#include + +static int +lilv_world_drop_graph(LilvWorld* world, const SordNode* graph); + +LilvWorld* +lilv_world_new(void) +{ + LilvWorld* world = (LilvWorld*)calloc(1, sizeof(LilvWorld)); + + world->world = sord_world_new(); + if (!world->world) { + goto fail; + } + + world->model = sord_new(world->world, SORD_SPO | SORD_OPS, true); + if (!world->model) { + goto fail; + } + + world->specs = NULL; + world->plugin_classes = lilv_plugin_classes_new(); + world->plugins = lilv_plugins_new(); + world->zombies = lilv_plugins_new(); + world->loaded_files = zix_tree_new( + false, lilv_resource_node_cmp, NULL, (ZixDestroyFunc)lilv_node_free); + + world->libs = zix_tree_new(false, lilv_lib_compare, NULL, NULL); + +#define NS_DCTERMS "http://purl.org/dc/terms/" +#define NS_DYNMAN "http://lv2plug.in/ns/ext/dynmanifest#" +#define NS_OWL "http://www.w3.org/2002/07/owl#" + +#define NEW_URI(uri) sord_new_uri(world->world, (const uint8_t*)(uri)) + + world->uris.dc_replaces = NEW_URI(NS_DCTERMS "replaces"); + world->uris.dman_DynManifest = NEW_URI(NS_DYNMAN "DynManifest"); + world->uris.doap_name = NEW_URI(LILV_NS_DOAP "name"); + world->uris.lv2_Plugin = NEW_URI(LV2_CORE__Plugin); + world->uris.lv2_Specification = NEW_URI(LV2_CORE__Specification); + world->uris.lv2_appliesTo = NEW_URI(LV2_CORE__appliesTo); + world->uris.lv2_binary = NEW_URI(LV2_CORE__binary); + world->uris.lv2_default = NEW_URI(LV2_CORE__default); + world->uris.lv2_designation = NEW_URI(LV2_CORE__designation); + world->uris.lv2_extensionData = NEW_URI(LV2_CORE__extensionData); + world->uris.lv2_index = NEW_URI(LV2_CORE__index); + world->uris.lv2_latency = NEW_URI(LV2_CORE__latency); + world->uris.lv2_maximum = NEW_URI(LV2_CORE__maximum); + world->uris.lv2_microVersion = NEW_URI(LV2_CORE__microVersion); + world->uris.lv2_minimum = NEW_URI(LV2_CORE__minimum); + world->uris.lv2_minorVersion = NEW_URI(LV2_CORE__minorVersion); + world->uris.lv2_name = NEW_URI(LV2_CORE__name); + world->uris.lv2_optionalFeature = NEW_URI(LV2_CORE__optionalFeature); + world->uris.lv2_port = NEW_URI(LV2_CORE__port); + world->uris.lv2_portProperty = NEW_URI(LV2_CORE__portProperty); + world->uris.lv2_reportsLatency = NEW_URI(LV2_CORE__reportsLatency); + world->uris.lv2_requiredFeature = NEW_URI(LV2_CORE__requiredFeature); + world->uris.lv2_symbol = NEW_URI(LV2_CORE__symbol); + world->uris.lv2_prototype = NEW_URI(LV2_CORE__prototype); + world->uris.owl_Ontology = NEW_URI(NS_OWL "Ontology"); + world->uris.pset_value = NEW_URI(LV2_PRESETS__value); + world->uris.rdf_a = NEW_URI(LILV_NS_RDF "type"); + world->uris.rdf_value = NEW_URI(LILV_NS_RDF "value"); + world->uris.rdfs_Class = NEW_URI(LILV_NS_RDFS "Class"); + world->uris.rdfs_label = NEW_URI(LILV_NS_RDFS "label"); + world->uris.rdfs_seeAlso = NEW_URI(LILV_NS_RDFS "seeAlso"); + world->uris.rdfs_subClassOf = NEW_URI(LILV_NS_RDFS "subClassOf"); + world->uris.xsd_base64Binary = NEW_URI(LILV_NS_XSD "base64Binary"); + world->uris.xsd_boolean = NEW_URI(LILV_NS_XSD "boolean"); + world->uris.xsd_decimal = NEW_URI(LILV_NS_XSD "decimal"); + world->uris.xsd_double = NEW_URI(LILV_NS_XSD "double"); + world->uris.xsd_integer = NEW_URI(LILV_NS_XSD "integer"); + world->uris.null_uri = NULL; + + world->lv2_plugin_class = + lilv_plugin_class_new(world, NULL, world->uris.lv2_Plugin, "Plugin"); + assert(world->lv2_plugin_class); + + world->n_read_files = 0; + world->opt.filter_language = true; + world->opt.dyn_manifest = true; + + return world; + +fail: + /* keep on rockin' in the */ free(world); + return NULL; +} + +void +lilv_world_free(LilvWorld* world) +{ + if (!world) { + return; + } + + lilv_plugin_class_free(world->lv2_plugin_class); + world->lv2_plugin_class = NULL; + + for (SordNode** n = (SordNode**)&world->uris; *n; ++n) { + sord_node_free(world->world, *n); + } + + for (LilvSpec* spec = world->specs; spec;) { + LilvSpec* next = spec->next; + sord_node_free(world->world, spec->spec); + sord_node_free(world->world, spec->bundle); + lilv_nodes_free(spec->data_uris); + free(spec); + spec = next; + } + world->specs = NULL; + + LILV_FOREACH (plugins, i, world->plugins) { + const LilvPlugin* p = lilv_plugins_get(world->plugins, i); + lilv_plugin_free((LilvPlugin*)p); + } + zix_tree_free((ZixTree*)world->plugins); + world->plugins = NULL; + + LILV_FOREACH (plugins, i, world->zombies) { + const LilvPlugin* p = lilv_plugins_get(world->zombies, i); + lilv_plugin_free((LilvPlugin*)p); + } + zix_tree_free((ZixTree*)world->zombies); + world->zombies = NULL; + + zix_tree_free((ZixTree*)world->loaded_files); + world->loaded_files = NULL; + + zix_tree_free(world->libs); + world->libs = NULL; + + zix_tree_free((ZixTree*)world->plugin_classes); + world->plugin_classes = NULL; + + sord_free(world->model); + world->model = NULL; + + sord_world_free(world->world); + world->world = NULL; + + free(world->opt.lv2_path); + free(world); +} + +void +lilv_world_set_option(LilvWorld* world, const char* uri, const LilvNode* value) +{ + if (!strcmp(uri, LILV_OPTION_DYN_MANIFEST)) { + if (lilv_node_is_bool(value)) { + world->opt.dyn_manifest = lilv_node_as_bool(value); + return; + } + } else if (!strcmp(uri, LILV_OPTION_FILTER_LANG)) { + if (lilv_node_is_bool(value)) { + world->opt.filter_language = lilv_node_as_bool(value); + return; + } + } else if (!strcmp(uri, LILV_OPTION_LV2_PATH)) { + if (lilv_node_is_string(value)) { + world->opt.lv2_path = lilv_strdup(lilv_node_as_string(value)); + return; + } + } + LILV_WARNF("Unrecognized or invalid option `%s'\n", uri); +} + +LilvNodes* +lilv_world_find_nodes(LilvWorld* world, + const LilvNode* subject, + const LilvNode* predicate, + const LilvNode* object) +{ + if (subject && !lilv_node_is_uri(subject) && !lilv_node_is_blank(subject)) { + LILV_ERRORF("Subject `%s' is not a resource\n", + sord_node_get_string(subject->node)); + return NULL; + } + + if (!predicate) { + LILV_ERROR("Missing required predicate\n"); + return NULL; + } + + if (!lilv_node_is_uri(predicate)) { + LILV_ERRORF("Predicate `%s' is not a URI\n", + sord_node_get_string(predicate->node)); + return NULL; + } + + if (!subject && !object) { + LILV_ERROR("Both subject and object are NULL\n"); + return NULL; + } + + return lilv_world_find_nodes_internal(world, + subject ? subject->node : NULL, + predicate->node, + object ? object->node : NULL); +} + +LilvNode* +lilv_world_get(LilvWorld* world, + const LilvNode* subject, + const LilvNode* predicate, + const LilvNode* object) +{ + if (!object) { + // TODO: Improve performance (see lilv_plugin_get_one) + SordIter* stream = sord_search(world->model, + subject ? subject->node : NULL, + predicate ? predicate->node : NULL, + NULL, + NULL); + + LilvNodes* nodes = + lilv_nodes_from_stream_objects(world, stream, SORD_OBJECT); + + if (nodes) { + LilvNode* value = lilv_node_duplicate(lilv_nodes_get_first(nodes)); + lilv_nodes_free(nodes); + return value; + } + + return NULL; + } + + SordNode* snode = sord_get(world->model, + subject ? subject->node : NULL, + predicate ? predicate->node : NULL, + object ? object->node : NULL, + NULL); + LilvNode* lnode = lilv_node_new_from_node(world, snode); + sord_node_free(world->world, snode); + return lnode; +} + +SordIter* +lilv_world_query_internal(LilvWorld* world, + const SordNode* subject, + const SordNode* predicate, + const SordNode* object) +{ + return sord_search(world->model, subject, predicate, object, NULL); +} + +bool +lilv_world_ask_internal(LilvWorld* world, + const SordNode* subject, + const SordNode* predicate, + const SordNode* object) +{ + return sord_ask(world->model, subject, predicate, object, NULL); +} + +bool +lilv_world_ask(LilvWorld* world, + const LilvNode* subject, + const LilvNode* predicate, + const LilvNode* object) +{ + return sord_ask(world->model, + subject ? subject->node : NULL, + predicate ? predicate->node : NULL, + object ? object->node : NULL, + NULL); +} + +SordModel* +lilv_world_filter_model(LilvWorld* world, + SordModel* model, + const SordNode* subject, + const SordNode* predicate, + const SordNode* object, + const SordNode* graph) +{ + SordModel* results = sord_new(world->world, SORD_SPO, false); + SordIter* i = sord_search(model, subject, predicate, object, graph); + for (; !sord_iter_end(i); sord_iter_next(i)) { + SordQuad quad; + sord_iter_get(i, quad); + sord_add(results, quad); + } + sord_iter_free(i); + return results; +} + +LilvNodes* +lilv_world_find_nodes_internal(LilvWorld* world, + const SordNode* subject, + const SordNode* predicate, + const SordNode* object) +{ + return lilv_nodes_from_stream_objects( + world, + lilv_world_query_internal(world, subject, predicate, object), + (object == NULL) ? SORD_OBJECT : SORD_SUBJECT); +} + +static SerdNode +lilv_new_uri_relative_to_base(const uint8_t* uri_str, + const uint8_t* base_uri_str) +{ + SerdURI base_uri; + serd_uri_parse(base_uri_str, &base_uri); + return serd_node_new_uri_from_string(uri_str, &base_uri, NULL); +} + +const uint8_t* +lilv_world_blank_node_prefix(LilvWorld* world) +{ + static char str[32]; + snprintf(str, sizeof(str), "%u", world->n_read_files++); + return (const uint8_t*)str; +} + +/** Comparator for sequences (e.g. world->plugins). */ +int +lilv_header_compare_by_uri(const void* a, const void* b, const void* user_data) +{ + (void)user_data; + + const struct LilvHeader* const header_a = (const struct LilvHeader*)a; + const struct LilvHeader* const header_b = (const struct LilvHeader*)b; + + return strcmp(lilv_node_as_uri(header_a->uri), + lilv_node_as_uri(header_b->uri)); +} + +/** + Comparator for libraries (world->libs). + + Libraries do have a LilvHeader, but we must also compare the bundle to + handle the case where the same library is loaded with different bundles, and + consequently different contents (mainly plugins). + */ +int +lilv_lib_compare(const void* a, const void* b, const void* user_data) +{ + (void)user_data; + + const LilvLib* const lib_a = (const LilvLib*)a; + const LilvLib* const lib_b = (const LilvLib*)b; + + const int cmp = + strcmp(lilv_node_as_uri(lib_a->uri), lilv_node_as_uri(lib_b->uri)); + + return cmp ? cmp : strcmp(lib_a->bundle_path, lib_b->bundle_path); +} + +/** Get an element of a collection of any object with an LilvHeader by URI. */ +static ZixTreeIter* +lilv_collection_find_by_uri(const ZixTree* seq, const LilvNode* uri) +{ + ZixTreeIter* i = NULL; + if (lilv_node_is_uri(uri)) { + struct LilvHeader key = {NULL, (LilvNode*)uri}; + zix_tree_find(seq, &key, &i); + } + return i; +} + +/** Get an element of a collection of any object with an LilvHeader by URI. */ +struct LilvHeader* +lilv_collection_get_by_uri(const ZixTree* seq, const LilvNode* uri) +{ + ZixTreeIter* const i = lilv_collection_find_by_uri(seq, uri); + + return i ? (struct LilvHeader*)zix_tree_get(i) : NULL; +} + +static void +lilv_world_add_spec(LilvWorld* world, + const SordNode* specification_node, + const SordNode* bundle_node) +{ + LilvSpec* spec = (LilvSpec*)malloc(sizeof(LilvSpec)); + spec->spec = sord_node_copy(specification_node); + spec->bundle = sord_node_copy(bundle_node); + spec->data_uris = lilv_nodes_new(); + + // Add all data files (rdfs:seeAlso) + SordIter* files = sord_search( + world->model, specification_node, world->uris.rdfs_seeAlso, NULL, NULL); + FOREACH_MATCH (files) { + const SordNode* file_node = sord_iter_get_node(files, SORD_OBJECT); + zix_tree_insert((ZixTree*)spec->data_uris, + lilv_node_new_from_node(world, file_node), + NULL); + } + sord_iter_free(files); + + // Add specification to world specification list + spec->next = world->specs; + world->specs = spec; +} + +static void +lilv_world_add_plugin(LilvWorld* world, + const SordNode* plugin_node, + const LilvNode* manifest_uri, + void* dynmanifest, + const SordNode* bundle) +{ + (void)dynmanifest; + + LilvNode* plugin_uri = lilv_node_new_from_node(world, plugin_node); + ZixTreeIter* z = NULL; + LilvPlugin* plugin = + (LilvPlugin*)lilv_plugins_get_by_uri(world->plugins, plugin_uri); + + if (plugin) { + // Existing plugin, if this is different bundle, ignore it + // (use the first plugin found in LV2_PATH) + const LilvNode* last_bundle = lilv_plugin_get_bundle_uri(plugin); + const char* plugin_uri_str = lilv_node_as_uri(plugin_uri); + if (sord_node_equals(bundle, last_bundle->node)) { + LILV_WARNF("Reloading plugin <%s>\n", plugin_uri_str); + plugin->loaded = false; + lilv_node_free(plugin_uri); + } else { + LILV_WARNF("Duplicate plugin <%s>\n", plugin_uri_str); + LILV_WARNF("... found in %s\n", lilv_node_as_string(last_bundle)); + LILV_WARNF("... and %s (ignored)\n", sord_node_get_string(bundle)); + lilv_node_free(plugin_uri); + return; + } + } else if ((z = lilv_collection_find_by_uri((const ZixTree*)world->zombies, + plugin_uri))) { + // Plugin bundle has been re-loaded, move from zombies to plugins + plugin = (LilvPlugin*)zix_tree_get(z); + zix_tree_remove((ZixTree*)world->zombies, z); + zix_tree_insert((ZixTree*)world->plugins, plugin, NULL); + lilv_node_free(plugin_uri); + lilv_plugin_clear(plugin, lilv_node_new_from_node(world, bundle)); + } else { + // Add new plugin to the world + plugin = lilv_plugin_new( + world, plugin_uri, lilv_node_new_from_node(world, bundle)); + + // Add manifest as plugin data file (as if it were rdfs:seeAlso) + zix_tree_insert( + (ZixTree*)plugin->data_uris, lilv_node_duplicate(manifest_uri), NULL); + + // Add plugin to world plugin sequence + zix_tree_insert((ZixTree*)world->plugins, plugin, NULL); + } + +#ifdef LILV_DYN_MANIFEST + // Set dynamic manifest library URI, if applicable + if (dynmanifest) { + plugin->dynmanifest = (LilvDynManifest*)dynmanifest; + ++((LilvDynManifest*)dynmanifest)->refs; + } +#endif + + // Add all plugin data files (rdfs:seeAlso) + SordIter* files = sord_search( + world->model, plugin_node, world->uris.rdfs_seeAlso, NULL, NULL); + FOREACH_MATCH (files) { + const SordNode* file_node = sord_iter_get_node(files, SORD_OBJECT); + zix_tree_insert((ZixTree*)plugin->data_uris, + lilv_node_new_from_node(world, file_node), + NULL); + } + sord_iter_free(files); +} + +SerdStatus +lilv_world_load_graph(LilvWorld* world, SordNode* graph, const LilvNode* uri) +{ + const SerdNode* base = sord_node_to_serd_node(uri->node); + SerdEnv* env = serd_env_new(base); + SerdReader* reader = sord_new_reader(world->model, env, SERD_TURTLE, graph); + + const SerdStatus st = lilv_world_load_file(world, reader, uri); + + serd_env_free(env); + serd_reader_free(reader); + return st; +} + +static void +lilv_world_load_dyn_manifest(LilvWorld* world, + SordNode* bundle_node, + const LilvNode* manifest) +{ +#ifdef LILV_DYN_MANIFEST + if (!world->opt.dyn_manifest) { + return; + } + + LV2_Dyn_Manifest_Handle handle = NULL; + + // ?dman a dynman:DynManifest bundle_node + SordModel* model = lilv_world_filter_model(world, + world->model, + NULL, + world->uris.rdf_a, + world->uris.dman_DynManifest, + bundle_node); + SordIter* iter = sord_begin(model); + for (; !sord_iter_end(iter); sord_iter_next(iter)) { + const SordNode* dmanifest = sord_iter_get_node(iter, SORD_SUBJECT); + + // ?dman lv2:binary ?binary + SordIter* binaries = sord_search( + world->model, dmanifest, world->uris.lv2_binary, NULL, bundle_node); + if (sord_iter_end(binaries)) { + sord_iter_free(binaries); + LILV_ERRORF("Dynamic manifest in <%s> has no binaries, ignored\n", + sord_node_get_string(bundle_node)); + continue; + } + + // Get binary path + const SordNode* binary = sord_iter_get_node(binaries, SORD_OBJECT); + const uint8_t* lib_uri = sord_node_get_string(binary); + char* lib_path = lilv_file_uri_parse((const char*)lib_uri, 0); + if (!lib_path) { + LILV_ERROR("No dynamic manifest library path\n"); + sord_iter_free(binaries); + continue; + } + + // Open library + dlerror(); + void* lib = dlopen(lib_path, RTLD_LAZY); + if (!lib) { + LILV_ERRORF( + "Failed to open dynmanifest library `%s' (%s)\n", lib_path, dlerror()); + sord_iter_free(binaries); + lilv_free(lib_path); + continue; + } + + // Open dynamic manifest + typedef int (*OpenFunc)(LV2_Dyn_Manifest_Handle*, + const LV2_Feature* const*); + OpenFunc dmopen = (OpenFunc)lilv_dlfunc(lib, "lv2_dyn_manifest_open"); + if (!dmopen || dmopen(&handle, &dman_features)) { + LILV_ERRORF("No `lv2_dyn_manifest_open' in `%s'\n", lib_path); + sord_iter_free(binaries); + dlclose(lib); + lilv_free(lib_path); + continue; + } + + // Get subjects (the data that would be in manifest.ttl) + typedef int (*GetSubjectsFunc)(LV2_Dyn_Manifest_Handle, FILE*); + GetSubjectsFunc get_subjects_func = + (GetSubjectsFunc)lilv_dlfunc(lib, "lv2_dyn_manifest_get_subjects"); + if (!get_subjects_func) { + LILV_ERRORF("No `lv2_dyn_manifest_get_subjects' in `%s'\n", lib_path); + sord_iter_free(binaries); + dlclose(lib); + lilv_free(lib_path); + continue; + } + + LilvDynManifest* desc = (LilvDynManifest*)malloc(sizeof(LilvDynManifest)); + desc->bundle = lilv_node_new_from_node(world, bundle_node); + desc->lib = lib; + desc->handle = handle; + desc->refs = 0; + + sord_iter_free(binaries); + + // Generate data file + FILE* fd = tmpfile(); + get_subjects_func(handle, fd); + rewind(fd); + + // Parse generated data file into temporary model + // FIXME + const SerdNode* base = sord_node_to_serd_node(dmanifest); + SerdEnv* env = serd_env_new(base); + SerdReader* reader = sord_new_reader( + world->model, env, SERD_TURTLE, sord_node_copy(dmanifest)); + serd_reader_add_blank_prefix(reader, lilv_world_blank_node_prefix(world)); + serd_reader_read_file_handle(reader, fd, (const uint8_t*)"(dyn-manifest)"); + serd_reader_free(reader); + serd_env_free(env); + + // Close (and automatically delete) temporary data file + fclose(fd); + + // ?plugin a lv2:Plugin + SordModel* plugins = lilv_world_filter_model(world, + world->model, + NULL, + world->uris.rdf_a, + world->uris.lv2_Plugin, + dmanifest); + SordIter* p = sord_begin(plugins); + FOREACH_MATCH (p) { + const SordNode* plug = sord_iter_get_node(p, SORD_SUBJECT); + lilv_world_add_plugin(world, plug, manifest, desc, bundle_node); + } + if (desc->refs == 0) { + lilv_dynmanifest_free(desc); + } + sord_iter_free(p); + sord_free(plugins); + lilv_free(lib_path); + } + sord_iter_free(iter); + sord_free(model); + +#else // LILV_DYN_MANIFEST + (void)world; + (void)bundle_node; + (void)manifest; +#endif +} + +#ifdef LILV_DYN_MANIFEST +void +lilv_dynmanifest_free(LilvDynManifest* dynmanifest) +{ + typedef int (*CloseFunc)(LV2_Dyn_Manifest_Handle); + CloseFunc close_func = + (CloseFunc)lilv_dlfunc(dynmanifest->lib, "lv2_dyn_manifest_close"); + if (close_func) { + close_func(dynmanifest->handle); + } + + dlclose(dynmanifest->lib); + lilv_node_free(dynmanifest->bundle); + free(dynmanifest); +} +#endif // LILV_DYN_MANIFEST + +LilvNode* +lilv_world_get_manifest_uri(LilvWorld* world, const LilvNode* bundle_uri) +{ + SerdNode manifest_uri = lilv_new_uri_relative_to_base( + (const uint8_t*)"manifest.ttl", sord_node_get_string(bundle_uri->node)); + LilvNode* manifest = lilv_new_uri(world, (const char*)manifest_uri.buf); + serd_node_free(&manifest_uri); + return manifest; +} + +static SordModel* +load_plugin_model(LilvWorld* world, + const LilvNode* bundle_uri, + const LilvNode* plugin_uri) +{ + // Create model and reader for loading into it + SordNode* bundle_node = bundle_uri->node; + SordModel* model = sord_new(world->world, SORD_SPO | SORD_OPS, false); + SerdEnv* env = serd_env_new(sord_node_to_serd_node(bundle_node)); + SerdReader* reader = sord_new_reader(model, env, SERD_TURTLE, NULL); + + // Load manifest + LilvNode* manifest_uri = lilv_world_get_manifest_uri(world, bundle_uri); + serd_reader_add_blank_prefix(reader, lilv_world_blank_node_prefix(world)); + serd_reader_read_file(reader, + (const uint8_t*)lilv_node_as_string(manifest_uri)); + + // Load any seeAlso files + SordModel* files = lilv_world_filter_model( + world, model, plugin_uri->node, world->uris.rdfs_seeAlso, NULL, NULL); + + SordIter* f = sord_begin(files); + FOREACH_MATCH (f) { + const SordNode* file = sord_iter_get_node(f, SORD_OBJECT); + const uint8_t* file_str = sord_node_get_string(file); + if (sord_node_get_type(file) == SORD_URI) { + serd_reader_add_blank_prefix(reader, lilv_world_blank_node_prefix(world)); + serd_reader_read_file(reader, file_str); + } + } + + sord_iter_free(f); + sord_free(files); + serd_reader_free(reader); + serd_env_free(env); + lilv_node_free(manifest_uri); + + return model; +} + +static LilvVersion +get_version(LilvWorld* world, SordModel* model, const LilvNode* subject) +{ + const SordNode* minor_node = + sord_get(model, subject->node, world->uris.lv2_minorVersion, NULL, NULL); + const SordNode* micro_node = + sord_get(model, subject->node, world->uris.lv2_microVersion, NULL, NULL); + + LilvVersion version = {0, 0}; + if (minor_node && micro_node) { + version.minor = atoi((const char*)sord_node_get_string(minor_node)); + version.micro = atoi((const char*)sord_node_get_string(micro_node)); + } + + return version; +} + +void +lilv_world_load_bundle(LilvWorld* world, const LilvNode* bundle_uri) +{ + if (!lilv_node_is_uri(bundle_uri)) { + LILV_ERRORF("Bundle URI `%s' is not a URI\n", + sord_node_get_string(bundle_uri->node)); + return; + } + + SordNode* bundle_node = bundle_uri->node; + LilvNode* manifest = lilv_world_get_manifest_uri(world, bundle_uri); + + // Read manifest into model with graph = bundle_node + SerdStatus st = lilv_world_load_graph(world, bundle_node, manifest); + if (st > SERD_FAILURE) { + LILV_ERRORF("Error reading %s\n", lilv_node_as_string(manifest)); + lilv_node_free(manifest); + return; + } + + // ?plugin a lv2:Plugin + SordIter* plug_results = sord_search( + world->model, NULL, world->uris.rdf_a, world->uris.lv2_Plugin, bundle_node); + + // Find any loaded plugins that will be replaced with a newer version + LilvNodes* unload_uris = lilv_nodes_new(); + FOREACH_MATCH (plug_results) { + const SordNode* plug = sord_iter_get_node(plug_results, SORD_SUBJECT); + + LilvNode* plugin_uri = lilv_node_new_from_node(world, plug); + const LilvPlugin* plugin = + lilv_plugins_get_by_uri(world->plugins, plugin_uri); + const LilvNode* last_bundle = + plugin ? lilv_plugin_get_bundle_uri(plugin) : NULL; + if (!plugin || sord_node_equals(bundle_node, last_bundle->node)) { + // No previously loaded version, or it's from the same bundle + lilv_node_free(plugin_uri); + continue; + } + + // Compare versions + SordModel* this_model = load_plugin_model(world, bundle_uri, plugin_uri); + LilvVersion this_version = get_version(world, this_model, plugin_uri); + SordModel* last_model = load_plugin_model(world, last_bundle, plugin_uri); + LilvVersion last_version = get_version(world, last_model, plugin_uri); + sord_free(this_model); + sord_free(last_model); + const int cmp = lilv_version_cmp(&this_version, &last_version); + if (cmp > 0) { + zix_tree_insert( + (ZixTree*)unload_uris, lilv_node_duplicate(plugin_uri), NULL); + LILV_WARNF("Replacing version %d.%d of <%s> from <%s>\n", + last_version.minor, + last_version.micro, + sord_node_get_string(plug), + sord_node_get_string(last_bundle->node)); + LILV_NOTEF("New version %d.%d found in <%s>\n", + this_version.minor, + this_version.micro, + sord_node_get_string(bundle_node)); + } else if (cmp < 0) { + LILV_WARNF("Ignoring bundle <%s>\n", sord_node_get_string(bundle_node)); + LILV_NOTEF("Newer version of <%s> loaded from <%s>\n", + sord_node_get_string(plug), + sord_node_get_string(last_bundle->node)); + lilv_node_free(plugin_uri); + sord_iter_free(plug_results); + lilv_world_drop_graph(world, bundle_node); + lilv_node_free(manifest); + lilv_nodes_free(unload_uris); + return; + } + lilv_node_free(plugin_uri); + } + + sord_iter_free(plug_results); + + // Unload any old conflicting plugins + LilvNodes* unload_bundles = lilv_nodes_new(); + LILV_FOREACH (nodes, i, unload_uris) { + const LilvNode* uri = lilv_nodes_get(unload_uris, i); + const LilvPlugin* plugin = lilv_plugins_get_by_uri(world->plugins, uri); + const LilvNode* bundle = lilv_plugin_get_bundle_uri(plugin); + + // Unload plugin and record bundle for later unloading + lilv_world_unload_resource(world, uri); + zix_tree_insert( + (ZixTree*)unload_bundles, lilv_node_duplicate(bundle), NULL); + } + lilv_nodes_free(unload_uris); + + // Now unload the associated bundles + // This must be done last since several plugins could be in the same bundle + LILV_FOREACH (nodes, i, unload_bundles) { + lilv_world_unload_bundle(world, lilv_nodes_get(unload_bundles, i)); + } + lilv_nodes_free(unload_bundles); + + // Re-search for plugin results now that old plugins are gone + plug_results = sord_search( + world->model, NULL, world->uris.rdf_a, world->uris.lv2_Plugin, bundle_node); + + FOREACH_MATCH (plug_results) { + const SordNode* plug = sord_iter_get_node(plug_results, SORD_SUBJECT); + lilv_world_add_plugin(world, plug, manifest, NULL, bundle_node); + } + sord_iter_free(plug_results); + + lilv_world_load_dyn_manifest(world, bundle_node, manifest); + + // ?spec a lv2:Specification + // ?spec a owl:Ontology + const SordNode* spec_preds[] = { + world->uris.lv2_Specification, world->uris.owl_Ontology, NULL}; + for (const SordNode** p = spec_preds; *p; ++p) { + SordIter* i = + sord_search(world->model, NULL, world->uris.rdf_a, *p, bundle_node); + FOREACH_MATCH (i) { + const SordNode* spec = sord_iter_get_node(i, SORD_SUBJECT); + lilv_world_add_spec(world, spec, bundle_node); + } + sord_iter_free(i); + } + + lilv_node_free(manifest); +} + +static int +lilv_world_drop_graph(LilvWorld* world, const SordNode* graph) +{ + SordIter* i = sord_search(world->model, NULL, NULL, NULL, graph); + while (!sord_iter_end(i)) { + const SerdStatus st = sord_erase(world->model, i); + if (st) { + LILV_ERRORF("Error removing statement from <%s> (%s)\n", + sord_node_get_string(graph), + serd_strerror(st)); + return st; + } + } + sord_iter_free(i); + + return 0; +} + +/** Remove loaded_files entry so file will be reloaded if requested. */ +static int +lilv_world_unload_file(LilvWorld* world, const LilvNode* file) +{ + ZixTreeIter* iter = NULL; + if (!zix_tree_find((ZixTree*)world->loaded_files, file, &iter)) { + zix_tree_remove((ZixTree*)world->loaded_files, iter); + return 0; + } + return 1; +} + +int +lilv_world_unload_bundle(LilvWorld* world, const LilvNode* bundle_uri) +{ + if (!bundle_uri) { + return 0; + } + + // Find all loaded files that are inside the bundle + LilvNodes* files = lilv_nodes_new(); + LILV_FOREACH (nodes, i, world->loaded_files) { + const LilvNode* file = lilv_nodes_get(world->loaded_files, i); + if (!strncmp(lilv_node_as_string(file), + lilv_node_as_string(bundle_uri), + strlen(lilv_node_as_string(bundle_uri)))) { + zix_tree_insert((ZixTree*)files, lilv_node_duplicate(file), NULL); + } + } + + // Unload all loaded files in the bundle + LILV_FOREACH (nodes, i, files) { + const LilvNode* file = lilv_nodes_get(world->plugins, i); + lilv_world_unload_file(world, file); + } + + lilv_nodes_free(files); + + /* Remove any plugins in the bundle from the plugin list. Since the + application may still have a pointer to the LilvPlugin, it can not be + destroyed here. Instead, we move it to the zombie plugin list, so it + will not be in the list returned by lilv_world_get_all_plugins() but can + still be used. + */ + ZixTreeIter* i = zix_tree_begin((ZixTree*)world->plugins); + while (i != zix_tree_end((ZixTree*)world->plugins)) { + LilvPlugin* p = (LilvPlugin*)zix_tree_get(i); + ZixTreeIter* next = zix_tree_iter_next(i); + + if (lilv_node_equals(lilv_plugin_get_bundle_uri(p), bundle_uri)) { + zix_tree_remove((ZixTree*)world->plugins, i); + zix_tree_insert((ZixTree*)world->zombies, p, NULL); + } + + i = next; + } + + // Drop everything in bundle graph + return lilv_world_drop_graph(world, bundle_uri->node); +} + +static void +load_dir_entry(const char* dir, const char* name, void* data) +{ + LilvWorld* world = (LilvWorld*)data; + char* path = lilv_strjoin(dir, "/", name, "/", NULL); + SerdNode suri = serd_node_new_file_uri((const uint8_t*)path, 0, 0, true); + LilvNode* node = lilv_new_uri(world, (const char*)suri.buf); + + lilv_world_load_bundle(world, node); + lilv_node_free(node); + serd_node_free(&suri); + free(path); +} + +/** Load all bundles in the directory at `dir_path`. */ +static void +lilv_world_load_directory(LilvWorld* world, const char* dir_path) +{ + char* path = lilv_expand(dir_path); + if (path) { + lilv_dir_for_each(path, world, load_dir_entry); + free(path); + } +} + +static const char* +first_path_sep(const char* path) +{ + for (const char* p = path; *p != '\0'; ++p) { + if (*p == LILV_PATH_SEP[0]) { + return p; + } + } + return NULL; +} + +/** Load all bundles found in `lv2_path`. + * @param lv2_path A colon-delimited list of directories. These directories + * should contain LV2 bundle directories (ie the search path is a list of + * parent directories of bundles, not a list of bundle directories). + */ +static void +lilv_world_load_path(LilvWorld* world, const char* lv2_path) +{ + while (lv2_path[0] != '\0') { + const char* const sep = first_path_sep(lv2_path); + if (sep) { + const size_t dir_len = sep - lv2_path; + char* const dir = (char*)malloc(dir_len + 1); + memcpy(dir, lv2_path, dir_len); + dir[dir_len] = '\0'; + lilv_world_load_directory(world, dir); + free(dir); + lv2_path += dir_len + 1; + } else { + lilv_world_load_directory(world, lv2_path); + lv2_path = "\0"; + } + } +} + +void +lilv_world_load_specifications(LilvWorld* world) +{ + for (LilvSpec* spec = world->specs; spec; spec = spec->next) { + LILV_FOREACH (nodes, f, spec->data_uris) { + LilvNode* file = (LilvNode*)lilv_collection_get(spec->data_uris, f); + lilv_world_load_graph(world, NULL, file); + } + } +} + +void +lilv_world_load_plugin_classes(LilvWorld* world) +{ + /* FIXME: This loads all classes, not just lv2:Plugin subclasses. + However, if the host gets all the classes via + lilv_plugin_class_get_children starting with lv2:Plugin as the root (which + is e.g. how a host would build a menu), they won't be seen anyway... + */ + + SordIter* classes = sord_search( + world->model, NULL, world->uris.rdf_a, world->uris.rdfs_Class, NULL); + FOREACH_MATCH (classes) { + const SordNode* class_node = sord_iter_get_node(classes, SORD_SUBJECT); + + SordNode* parent = sord_get( + world->model, class_node, world->uris.rdfs_subClassOf, NULL, NULL); + if (!parent || sord_node_get_type(parent) != SORD_URI) { + continue; + } + + SordNode* label = + sord_get(world->model, class_node, world->uris.rdfs_label, NULL, NULL); + if (!label) { + sord_node_free(world->world, parent); + continue; + } + + LilvPluginClass* pclass = lilv_plugin_class_new( + world, parent, class_node, (const char*)sord_node_get_string(label)); + if (pclass) { + zix_tree_insert((ZixTree*)world->plugin_classes, pclass, NULL); + } + + sord_node_free(world->world, label); + sord_node_free(world->world, parent); + } + sord_iter_free(classes); +} + +void +lilv_world_load_all(LilvWorld* world) +{ + const char* lv2_path = world->opt.lv2_path; + if (!lv2_path) { + lv2_path = getenv("LV2_PATH"); + } + if (!lv2_path) { + lv2_path = LILV_DEFAULT_LV2_PATH; + } + + // Discover bundles and read all manifest files into model + lilv_world_load_path(world, lv2_path); + + LILV_FOREACH (plugins, p, world->plugins) { + const LilvPlugin* plugin = + (const LilvPlugin*)lilv_collection_get((ZixTree*)world->plugins, p); + + // ?new dc:replaces plugin + if (sord_ask(world->model, + NULL, + world->uris.dc_replaces, + lilv_plugin_get_uri(plugin)->node, + NULL)) { + // TODO: Check if replacement is a known plugin? (expensive) + ((LilvPlugin*)plugin)->replaced = true; + } + } + + // Query out things to cache + lilv_world_load_specifications(world); + lilv_world_load_plugin_classes(world); +} + +SerdStatus +lilv_world_load_file(LilvWorld* world, SerdReader* reader, const LilvNode* uri) +{ + ZixTreeIter* iter = NULL; + if (!zix_tree_find((ZixTree*)world->loaded_files, uri, &iter)) { + return SERD_FAILURE; // File has already been loaded + } + + size_t uri_len = 0; + const uint8_t* const uri_str = + sord_node_get_string_counted(uri->node, &uri_len); + if (strncmp((const char*)uri_str, "file:", 5)) { + return SERD_FAILURE; // Not a local file + } + + if (strcmp((const char*)uri_str + uri_len - 4, ".ttl")) { + return SERD_FAILURE; // Not a Turtle file + } + + serd_reader_add_blank_prefix(reader, lilv_world_blank_node_prefix(world)); + const SerdStatus st = serd_reader_read_file(reader, uri_str); + if (st) { + LILV_ERRORF("Error loading file `%s'\n", lilv_node_as_string(uri)); + return st; + } + + zix_tree_insert( + (ZixTree*)world->loaded_files, lilv_node_duplicate(uri), NULL); + return SERD_SUCCESS; +} + +int +lilv_world_load_resource(LilvWorld* world, const LilvNode* resource) +{ + if (!lilv_node_is_uri(resource) && !lilv_node_is_blank(resource)) { + LILV_ERRORF("Node `%s' is not a resource\n", + sord_node_get_string(resource->node)); + return -1; + } + + SordModel* files = lilv_world_filter_model( + world, world->model, resource->node, world->uris.rdfs_seeAlso, NULL, NULL); + + SordIter* f = sord_begin(files); + int n_read = 0; + FOREACH_MATCH (f) { + const SordNode* file = sord_iter_get_node(f, SORD_OBJECT); + const uint8_t* file_str = sord_node_get_string(file); + LilvNode* file_node = lilv_node_new_from_node(world, file); + if (sord_node_get_type(file) != SORD_URI) { + LILV_ERRORF("rdfs:seeAlso node `%s' is not a URI\n", file_str); + } else if (!lilv_world_load_graph(world, (SordNode*)file, file_node)) { + ++n_read; + } + lilv_node_free(file_node); + } + sord_iter_free(f); + + sord_free(files); + return n_read; +} + +int +lilv_world_unload_resource(LilvWorld* world, const LilvNode* resource) +{ + if (!lilv_node_is_uri(resource) && !lilv_node_is_blank(resource)) { + LILV_ERRORF("Node `%s' is not a resource\n", + sord_node_get_string(resource->node)); + return -1; + } + + SordModel* files = lilv_world_filter_model( + world, world->model, resource->node, world->uris.rdfs_seeAlso, NULL, NULL); + + SordIter* f = sord_begin(files); + int n_dropped = 0; + FOREACH_MATCH (f) { + const SordNode* file = sord_iter_get_node(f, SORD_OBJECT); + LilvNode* file_node = lilv_node_new_from_node(world, file); + if (sord_node_get_type(file) != SORD_URI) { + LILV_ERRORF("rdfs:seeAlso node `%s' is not a URI\n", + sord_node_get_string(file)); + } else if (!lilv_world_drop_graph(world, file_node->node)) { + lilv_world_unload_file(world, file_node); + ++n_dropped; + } + lilv_node_free(file_node); + } + sord_iter_free(f); + + sord_free(files); + return n_dropped; +} + +const LilvPluginClass* +lilv_world_get_plugin_class(const LilvWorld* world) +{ + return world->lv2_plugin_class; +} + +const LilvPluginClasses* +lilv_world_get_plugin_classes(const LilvWorld* world) +{ + return world->plugin_classes; +} + +const LilvPlugins* +lilv_world_get_all_plugins(const LilvWorld* world) +{ + return world->plugins; +} + +LilvNode* +lilv_world_get_symbol(LilvWorld* world, const LilvNode* subject) +{ + // Check for explicitly given symbol + SordNode* snode = + sord_get(world->model, subject->node, world->uris.lv2_symbol, NULL, NULL); + + if (snode) { + LilvNode* ret = lilv_node_new_from_node(world, snode); + sord_node_free(world->world, snode); + return ret; + } + + if (!lilv_node_is_uri(subject)) { + return NULL; + } + + // Find rightmost segment of URI + SerdURI uri; + serd_uri_parse((const uint8_t*)lilv_node_as_uri(subject), &uri); + const char* str = "_"; + if (uri.fragment.buf) { + str = (const char*)uri.fragment.buf + 1; + } else if (uri.query.buf) { + str = (const char*)uri.query.buf; + } else if (uri.path.buf) { + const char* last_slash = strrchr((const char*)uri.path.buf, '/'); + str = last_slash ? (last_slash + 1) : (const char*)uri.path.buf; + } + + // Replace invalid characters + const size_t len = strlen(str); + char* const sym = (char*)calloc(1, len + 1); + for (size_t i = 0; i < len; ++i) { + const char c = str[i]; + if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c == '_') || + (i > 0 && c >= '0' && c <= '9'))) { + sym[i] = '_'; + } else { + sym[i] = str[i]; + } + } + + LilvNode* ret = lilv_new_string(world, sym); + free(sym); + return ret; +} diff --git a/modules/juce_audio_processors/format_types/lv2/lilv/src/zix/common.h b/modules/juce_audio_processors/format_types/lv2/lilv/src/zix/common.h new file mode 100644 index 0000000000..d47586c606 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lilv/src/zix/common.h @@ -0,0 +1,138 @@ +/* + Copyright 2016-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef ZIX_COMMON_H +#define ZIX_COMMON_H + +#include + +/** + @addtogroup zix + @{ +*/ + +/** @cond */ +#if defined(_WIN32) && !defined(ZIX_STATIC) && defined(ZIX_INTERNAL) +# define ZIX_API __declspec(dllexport) +#elif defined(_WIN32) && !defined(ZIX_STATIC) +# define ZIX_API __declspec(dllimport) +#elif defined(__GNUC__) +# define ZIX_API __attribute__((visibility("default"))) +#else +# define ZIX_API +#endif + +#ifdef __GNUC__ +# define ZIX_PURE_FUNC __attribute__((pure)) +# define ZIX_CONST_FUNC __attribute__((const)) +# define ZIX_MALLOC_FUNC __attribute__((malloc)) +#else +# define ZIX_PURE_FUNC +# define ZIX_CONST_FUNC +# define ZIX_MALLOC_FUNC +#endif + +#define ZIX_PURE_API \ + ZIX_API \ + ZIX_PURE_FUNC + +#define ZIX_CONST_API \ + ZIX_API \ + ZIX_CONST_FUNC + +#define ZIX_MALLOC_API \ + ZIX_API \ + ZIX_MALLOC_FUNC + +/** @endcond */ + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef __GNUC__ +# define ZIX_LOG_FUNC(fmt, arg1) __attribute__((format(printf, fmt, arg1))) +#else +# define ZIX_LOG_FUNC(fmt, arg1) +#endif + +// Unused parameter macro to suppresses warnings and make it impossible to use +#if defined(__cplusplus) +# define ZIX_UNUSED(name) +#elif defined(__GNUC__) +# define ZIX_UNUSED(name) name##_unused __attribute__((__unused__)) +#else +# define ZIX_UNUSED(name) name +#endif + +typedef enum { + ZIX_STATUS_SUCCESS, + ZIX_STATUS_ERROR, + ZIX_STATUS_NO_MEM, + ZIX_STATUS_NOT_FOUND, + ZIX_STATUS_EXISTS, + ZIX_STATUS_BAD_ARG, + ZIX_STATUS_BAD_PERMS +} ZixStatus; + +static inline const char* +zix_strerror(const ZixStatus status) +{ + switch (status) { + case ZIX_STATUS_SUCCESS: + return "Success"; + case ZIX_STATUS_ERROR: + return "Unknown error"; + case ZIX_STATUS_NO_MEM: + return "Out of memory"; + case ZIX_STATUS_NOT_FOUND: + return "Not found"; + case ZIX_STATUS_EXISTS: + return "Exists"; + case ZIX_STATUS_BAD_ARG: + return "Bad argument"; + case ZIX_STATUS_BAD_PERMS: + return "Bad permissions"; + } + return "Unknown error"; +} + +/** + Function for comparing two elements. +*/ +typedef int (*ZixComparator)(const void* a, + const void* b, + const void* user_data); + +/** + Function for testing equality of two elements. +*/ +typedef bool (*ZixEqualFunc)(const void* a, const void* b); + +/** + Function to destroy an element. +*/ +typedef void (*ZixDestroyFunc)(void* ptr); + +/** + @} +*/ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* ZIX_COMMON_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/lilv/src/zix/tree.c b/modules/juce_audio_processors/format_types/lv2/lilv/src/zix/tree.c new file mode 100644 index 0000000000..9faf13cfa8 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lilv/src/zix/tree.c @@ -0,0 +1,727 @@ +/* + Copyright 2011-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "zix/tree.h" + +#include "zix/common.h" + +#include +#include +#include + +typedef struct ZixTreeNodeImpl ZixTreeNode; + +struct ZixTreeImpl { + ZixTreeNode* root; + ZixDestroyFunc destroy; + ZixComparator cmp; + void* cmp_data; + size_t size; + bool allow_duplicates; +}; + +struct ZixTreeNodeImpl { + void* data; + struct ZixTreeNodeImpl* left; + struct ZixTreeNodeImpl* right; + struct ZixTreeNodeImpl* parent; + int balance; +}; + +#define MIN(a, b) (((a) < (b)) ? (a) : (b)) +#define MAX(a, b) (((a) > (b)) ? (a) : (b)) + +// Uncomment these for debugging features +// #define ZIX_TREE_DUMP 1 +// #define ZIX_TREE_VERIFY 1 +// #define ZIX_TREE_HYPER_VERIFY 1 + +#if defined(ZIX_TREE_VERIFY) || defined(ZIX_TREE_HYPER_VERIFY) +# include "tree_debug.h" +# define ASSERT_BALANCE(n) assert(verify_balance(n)) +#else +# define ASSERT_BALANCE(n) +#endif + +#ifdef ZIX_TREE_DUMP +# include "tree_debug.h" +# define DUMP(t) zix_tree_print(t->root, 0) +# define DEBUG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__) +#else +# define DUMP(t) +# define DEBUG_PRINTF(fmt, ...) +#endif + +ZixTree* +zix_tree_new(bool allow_duplicates, + ZixComparator cmp, + void* cmp_data, + ZixDestroyFunc destroy) +{ + ZixTree* t = (ZixTree*)malloc(sizeof(ZixTree)); + t->root = NULL; + t->destroy = destroy; + t->cmp = cmp; + t->cmp_data = cmp_data; + t->size = 0; + t->allow_duplicates = allow_duplicates; + return t; +} + +static void +zix_tree_free_rec(ZixTree* t, ZixTreeNode* n) +{ + if (n) { + zix_tree_free_rec(t, n->left); + zix_tree_free_rec(t, n->right); + if (t->destroy) { + t->destroy(n->data); + } + free(n); + } +} + +void +zix_tree_free(ZixTree* t) +{ + if (t) { + zix_tree_free_rec(t, t->root); + free(t); + } +} + +size_t +zix_tree_size(const ZixTree* t) +{ + return t->size; +} + +static void +rotate(ZixTreeNode* p, ZixTreeNode* q) +{ + assert(q->parent == p); + assert(p->left == q || p->right == q); + + q->parent = p->parent; + if (q->parent) { + if (q->parent->left == p) { + q->parent->left = q; + } else { + q->parent->right = q; + } + } + + if (p->right == q) { + // Rotate left + p->right = q->left; + q->left = p; + if (p->right) { + p->right->parent = p; + } + } else { + // Rotate right + assert(p->left == q); + p->left = q->right; + q->right = p; + if (p->left) { + p->left->parent = p; + } + } + + p->parent = q; +} + +/** + * Rotate left about `p`. + * + * p q + * / \ / \ + * A q => p C + * / \ / \ + * B C A B + */ +static ZixTreeNode* +rotate_left(ZixTreeNode* p, int* height_change) +{ + ZixTreeNode* const q = p->right; + *height_change = (q->balance == 0) ? 0 : -1; + + DEBUG_PRINTF("LL %ld\n", (intptr_t)p->data); + + assert(p->balance == 2); + assert(q->balance == 0 || q->balance == 1); + + rotate(p, q); + + // p->balance -= 1 + MAX(0, q->balance); + // q->balance -= 1 - MIN(0, p->balance); + --q->balance; + p->balance = -(q->balance); + + ASSERT_BALANCE(p); + ASSERT_BALANCE(q); + return q; +} + +/** + * Rotate right about `p`. + * + * p q + * / \ / \ + * q C => A p + * / \ / \ + * A B B C + * + */ +static ZixTreeNode* +rotate_right(ZixTreeNode* p, int* height_change) +{ + ZixTreeNode* const q = p->left; + *height_change = (q->balance == 0) ? 0 : -1; + + DEBUG_PRINTF("RR %ld\n", (intptr_t)p->data); + + assert(p->balance == -2); + assert(q->balance == 0 || q->balance == -1); + + rotate(p, q); + + // p->balance += 1 - MIN(0, q->balance); + // q->balance += 1 + MAX(0, p->balance); + ++q->balance; + p->balance = -(q->balance); + + ASSERT_BALANCE(p); + ASSERT_BALANCE(q); + return q; +} + +/** + * Rotate left about `p->left` then right about `p`. + * + * p r + * / \ / \ + * q D => q p + * / \ / \ / \ + * A r A B C D + * / \ + * B C + * + */ +static ZixTreeNode* +rotate_left_right(ZixTreeNode* p, int* height_change) +{ + ZixTreeNode* const q = p->left; + ZixTreeNode* const r = q->right; + + assert(p->balance == -2); + assert(q->balance == 1); + assert(r->balance == -1 || r->balance == 0 || r->balance == 1); + + DEBUG_PRINTF("LR %ld P: %2d Q: %2d R: %2d\n", + (intptr_t)p->data, + p->balance, + q->balance, + r->balance); + + rotate(q, r); + rotate(p, r); + + q->balance -= 1 + MAX(0, r->balance); + p->balance += 1 - MIN(MIN(0, r->balance) - 1, r->balance + q->balance); + // r->balance += MAX(0, p->balance) + MIN(0, q->balance); + + // p->balance = (p->left && p->right) ? -MIN(r->balance, 0) : 0; + // q->balance = - MAX(r->balance, 0); + r->balance = 0; + + *height_change = -1; + + ASSERT_BALANCE(p); + ASSERT_BALANCE(q); + ASSERT_BALANCE(r); + return r; +} + +/** + * Rotate right about `p->right` then right about `p`. + * + * p r + * / \ / \ + * A q => p q + * / \ / \ / \ + * r D A B C D + * / \ + * B C + * + */ +static ZixTreeNode* +rotate_right_left(ZixTreeNode* p, int* height_change) +{ + ZixTreeNode* const q = p->right; + ZixTreeNode* const r = q->left; + + assert(p->balance == 2); + assert(q->balance == -1); + assert(r->balance == -1 || r->balance == 0 || r->balance == 1); + + DEBUG_PRINTF("RL %ld P: %2d Q: %2d R: %2d\n", + (intptr_t)p->data, + p->balance, + q->balance, + r->balance); + + rotate(q, r); + rotate(p, r); + + q->balance += 1 - MIN(0, r->balance); + p->balance -= 1 + MAX(MAX(0, r->balance) + 1, r->balance + q->balance); + // r->balance += MAX(0, q->balance) + MIN(0, p->balance); + + // p->balance = (p->left && p->right) ? -MAX(r->balance, 0) : 0; + // q->balance = - MIN(r->balance, 0); + r->balance = 0; + // assert(r->balance == 0); + + *height_change = -1; + + ASSERT_BALANCE(p); + ASSERT_BALANCE(q); + ASSERT_BALANCE(r); + return r; +} + +static ZixTreeNode* +zix_tree_rebalance(ZixTree* t, ZixTreeNode* node, int* height_change) +{ +#ifdef ZIX_TREE_HYPER_VERIFY + const size_t old_height = height(node); +#endif + DEBUG_PRINTF("REBALANCE %ld (%d)\n", (intptr_t)node->data, node->balance); + *height_change = 0; + const bool is_root = !node->parent; + assert((is_root && t->root == node) || (!is_root && t->root != node)); + ZixTreeNode* replacement = node; + if (node->balance == -2) { + assert(node->left); + if (node->left->balance == 1) { + replacement = rotate_left_right(node, height_change); + } else { + replacement = rotate_right(node, height_change); + } + } else if (node->balance == 2) { + assert(node->right); + if (node->right->balance == -1) { + replacement = rotate_right_left(node, height_change); + } else { + replacement = rotate_left(node, height_change); + } + } + if (is_root) { + assert(!replacement->parent); + t->root = replacement; + } + DUMP(t); +#ifdef ZIX_TREE_HYPER_VERIFY + assert(old_height + *height_change == height(replacement)); +#endif + return replacement; +} + +ZixStatus +zix_tree_insert(ZixTree* t, void* e, ZixTreeIter** ti) +{ + DEBUG_PRINTF("**** INSERT %ld\n", (intptr_t)e); + int cmp = 0; + ZixTreeNode* n = t->root; + ZixTreeNode* p = NULL; + + // Find the parent p of e + while (n) { + p = n; + cmp = t->cmp(e, n->data, t->cmp_data); + if (cmp < 0) { + n = n->left; + } else if (cmp > 0 || t->allow_duplicates) { + n = n->right; + } else { + if (ti) { + *ti = n; + } + DEBUG_PRINTF("%ld EXISTS!\n", (intptr_t)e); + return ZIX_STATUS_EXISTS; + } + } + + // Allocate a new node n + if (!(n = (ZixTreeNode*)malloc(sizeof(ZixTreeNode)))) { + return ZIX_STATUS_NO_MEM; + } + memset(n, '\0', sizeof(ZixTreeNode)); + n->data = e; + n->balance = 0; + if (ti) { + *ti = n; + } + + bool p_height_increased = false; + + // Make p the parent of n + n->parent = p; + if (!p) { + t->root = n; + } else { + if (cmp < 0) { + assert(!p->left); + assert(p->balance == 0 || p->balance == 1); + p->left = n; + --p->balance; + p_height_increased = !p->right; + } else { + assert(!p->right); + assert(p->balance == 0 || p->balance == -1); + p->right = n; + ++p->balance; + p_height_increased = !p->left; + } + } + + DUMP(t); + + // Rebalance if necessary (at most 1 rotation) + assert(!p || p->balance == -1 || p->balance == 0 || p->balance == 1); + if (p && p_height_increased) { + int height_change = 0; + for (ZixTreeNode* i = p; i && i->parent; i = i->parent) { + if (i == i->parent->left) { + if (--i->parent->balance == -2) { + zix_tree_rebalance(t, i->parent, &height_change); + break; + } + } else { + assert(i == i->parent->right); + if (++i->parent->balance == 2) { + zix_tree_rebalance(t, i->parent, &height_change); + break; + } + } + + if (i->parent->balance == 0) { + break; + } + } + } + + DUMP(t); + + ++t->size; + +#ifdef ZIX_TREE_VERIFY + if (!verify(t, t->root)) { + return ZIX_STATUS_ERROR; + } +#endif + + return ZIX_STATUS_SUCCESS; +} + +ZixStatus +zix_tree_remove(ZixTree* t, ZixTreeIter* ti) +{ + ZixTreeNode* const n = ti; + ZixTreeNode** pp = NULL; // parent pointer + ZixTreeNode* to_balance = n->parent; // lowest node to balance + int d_balance = 0; // delta(balance) for n->parent + + DEBUG_PRINTF("*** REMOVE %ld\n", (intptr_t)n->data); + + if ((n == t->root) && !n->left && !n->right) { + t->root = NULL; + if (t->destroy) { + t->destroy(n->data); + } + free(n); + --t->size; + assert(t->size == 0); + return ZIX_STATUS_SUCCESS; + } + + // Set pp to the parent pointer to n, if applicable + if (n->parent) { + assert(n->parent->left == n || n->parent->right == n); + if (n->parent->left == n) { // n is left child + pp = &n->parent->left; + d_balance = 1; + } else { // n is right child + assert(n->parent->right == n); + pp = &n->parent->right; + d_balance = -1; + } + } + + assert(!pp || *pp == n); + + int height_change = 0; + if (!n->left && !n->right) { + // n is a leaf, just remove it + if (pp) { + *pp = NULL; + to_balance = n->parent; + height_change = (!n->parent->left && !n->parent->right) ? -1 : 0; + } + + } else if (!n->left) { + // Replace n with right (only) child + if (pp) { + *pp = n->right; + to_balance = n->parent; + } else { + t->root = n->right; + } + n->right->parent = n->parent; + height_change = -1; + + } else if (!n->right) { + // Replace n with left (only) child + if (pp) { + *pp = n->left; + to_balance = n->parent; + } else { + t->root = n->left; + } + n->left->parent = n->parent; + height_change = -1; + + } else { + // Replace n with in-order successor (leftmost child of right subtree) + ZixTreeNode* replace = n->right; + while (replace->left) { + assert(replace->left->parent == replace); + replace = replace->left; + } + + // Remove replace from parent (replace_p) + if (replace->parent->left == replace) { + height_change = replace->parent->right ? 0 : -1; + d_balance = 1; + to_balance = replace->parent; + replace->parent->left = replace->right; + } else { + assert(replace->parent == n); + height_change = replace->parent->left ? 0 : -1; + d_balance = -1; + to_balance = replace->parent; + replace->parent->right = replace->right; + } + + if (to_balance == n) { + to_balance = replace; + } + + if (replace->right) { + replace->right->parent = replace->parent; + } + + replace->balance = n->balance; + + // Swap node to delete with replace + if (pp) { + *pp = replace; + } else { + assert(t->root == n); + t->root = replace; + } + + replace->parent = n->parent; + replace->left = n->left; + n->left->parent = replace; + replace->right = n->right; + if (n->right) { + n->right->parent = replace; + } + + assert(!replace->parent || replace->parent->left == replace || + replace->parent->right == replace); + } + + // Rebalance starting at to_balance upwards. + for (ZixTreeNode* i = to_balance; i; i = i->parent) { + i->balance += d_balance; + if (d_balance == 0 || i->balance == -1 || i->balance == 1) { + break; + } + + assert(i != n); + i = zix_tree_rebalance(t, i, &height_change); + if (i->balance == 0) { + height_change = -1; + } + + if (i->parent) { + if (i == i->parent->left) { + d_balance = height_change * -1; + } else { + assert(i == i->parent->right); + d_balance = height_change; + } + } + } + + DUMP(t); + + if (t->destroy) { + t->destroy(n->data); + } + free(n); + + --t->size; + +#ifdef ZIX_TREE_VERIFY + if (!verify(t, t->root)) { + return ZIX_STATUS_ERROR; + } +#endif + + return ZIX_STATUS_SUCCESS; +} + +ZixStatus +zix_tree_find(const ZixTree* t, const void* e, ZixTreeIter** ti) +{ + ZixTreeNode* n = t->root; + while (n) { + const int cmp = t->cmp(e, n->data, t->cmp_data); + if (cmp == 0) { + break; + } + + if (cmp < 0) { + n = n->left; + } else { + n = n->right; + } + } + + *ti = n; + return (n) ? ZIX_STATUS_SUCCESS : ZIX_STATUS_NOT_FOUND; +} + +void* +zix_tree_get(const ZixTreeIter* ti) +{ + return ti ? ti->data : NULL; +} + +ZixTreeIter* +zix_tree_begin(ZixTree* t) +{ + if (!t->root) { + return NULL; + } + + ZixTreeNode* n = t->root; + while (n->left) { + n = n->left; + } + + return n; +} + +ZixTreeIter* +zix_tree_end(ZixTree* ZIX_UNUSED(t)) +{ + return NULL; +} + +ZixTreeIter* +zix_tree_rbegin(ZixTree* t) +{ + if (!t->root) { + return NULL; + } + + ZixTreeNode* n = t->root; + while (n->right) { + n = n->right; + } + + return n; +} + +ZixTreeIter* +zix_tree_rend(ZixTree* ZIX_UNUSED(t)) +{ + return NULL; +} + +bool +zix_tree_iter_is_end(const ZixTreeIter* i) +{ + return !i; +} + +bool +zix_tree_iter_is_rend(const ZixTreeIter* i) +{ + return !i; +} + +ZixTreeIter* +zix_tree_iter_next(ZixTreeIter* i) +{ + if (!i) { + return NULL; + } + + if (i->right) { + i = i->right; + while (i->left) { + i = i->left; + } + } else { + while (i->parent && i->parent->right == i) { // i is a right child + i = i->parent; + } + + i = i->parent; + } + + return i; +} + +ZixTreeIter* +zix_tree_iter_prev(ZixTreeIter* i) +{ + if (!i) { + return NULL; + } + + if (i->left) { + i = i->left; + while (i->right) { + i = i->right; + } + + } else { + while (i->parent && i->parent->left == i) { // i is a left child + i = i->parent; + } + + i = i->parent; + } + + return i; +} diff --git a/modules/juce_audio_processors/format_types/lv2/lilv/src/zix/tree.h b/modules/juce_audio_processors/format_types/lv2/lilv/src/zix/tree.h new file mode 100644 index 0000000000..a2d5fe79fe --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lilv/src/zix/tree.h @@ -0,0 +1,164 @@ +/* + Copyright 2011-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef ZIX_TREE_H +#define ZIX_TREE_H + +#include "zix/common.h" + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + @addtogroup zix + @{ + @name Tree + @{ +*/ + +/** + A balanced binary search tree. +*/ +typedef struct ZixTreeImpl ZixTree; + +/** + An iterator over a @ref ZixTree. +*/ +typedef struct ZixTreeNodeImpl ZixTreeIter; + +/** + Create a new (empty) tree. +*/ +ZIX_API +ZixTree* +zix_tree_new(bool allow_duplicates, + ZixComparator cmp, + void* cmp_data, + ZixDestroyFunc destroy); + +/** + Free `t`. +*/ +ZIX_API +void +zix_tree_free(ZixTree* t); + +/** + Return the number of elements in `t`. +*/ +ZIX_PURE_API +size_t +zix_tree_size(const ZixTree* t); + +/** + Insert the element `e` into `t` and point `ti` at the new element. +*/ +ZIX_API +ZixStatus +zix_tree_insert(ZixTree* t, void* e, ZixTreeIter** ti); + +/** + Remove the item pointed at by `ti` from `t`. +*/ +ZIX_API +ZixStatus +zix_tree_remove(ZixTree* t, ZixTreeIter* ti); + +/** + Set `ti` to an element equal to `e` in `t`. + If no such item exists, `ti` is set to NULL. +*/ +ZIX_API +ZixStatus +zix_tree_find(const ZixTree* t, const void* e, ZixTreeIter** ti); + +/** + Return the data associated with the given tree item. +*/ +ZIX_PURE_API +void* +zix_tree_get(const ZixTreeIter* ti); + +/** + Return an iterator to the first (smallest) element in `t`. +*/ +ZIX_PURE_API +ZixTreeIter* +zix_tree_begin(ZixTree* t); + +/** + Return an iterator the the element one past the last element in `t`. +*/ +ZIX_CONST_API +ZixTreeIter* +zix_tree_end(ZixTree* t); + +/** + Return true iff `i` is an iterator to the end of its tree. +*/ +ZIX_CONST_API +bool +zix_tree_iter_is_end(const ZixTreeIter* i); + +/** + Return an iterator to the last (largest) element in `t`. +*/ +ZIX_PURE_API +ZixTreeIter* +zix_tree_rbegin(ZixTree* t); + +/** + Return an iterator the the element one before the first element in `t`. +*/ +ZIX_CONST_API +ZixTreeIter* +zix_tree_rend(ZixTree* t); + +/** + Return true iff `i` is an iterator to the reverse end of its tree. +*/ +ZIX_CONST_API +bool +zix_tree_iter_is_rend(const ZixTreeIter* i); + +/** + Return an iterator that points to the element one past `i`. +*/ +ZIX_PURE_API +ZixTreeIter* +zix_tree_iter_next(ZixTreeIter* i); + +/** + Return an iterator that points to the element one before `i`. +*/ +ZIX_PURE_API +ZixTreeIter* +zix_tree_iter_prev(ZixTreeIter* i); + +/** + @} + @} +*/ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* ZIX_TREE_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/lilv_config.h b/modules/juce_audio_processors/format_types/lv2/lilv_config.h new file mode 100644 index 0000000000..9a10500203 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lilv_config.h @@ -0,0 +1,3 @@ +#pragma once + +#include "juce_lv2_config.h" diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/COPYING b/modules/juce_audio_processors/format_types/lv2/lv2/COPYING new file mode 100644 index 0000000000..4d59452459 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/COPYING @@ -0,0 +1,16 @@ +Copyright 2006-2012 Steve Harris, David Robillard. + +Based on LADSPA, Copyright 2000-2002 Richard W.E. Furse, +Paul Barton-Davis, Stefan Westerfeld. + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/atom/atom-test-utils.c b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/atom/atom-test-utils.c new file mode 100644 index 0000000000..ae368c8938 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/atom/atom-test-utils.c @@ -0,0 +1,73 @@ +/* + Copyright 2012-2018 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "lv2/atom/atom.h" +#include "lv2/atom/forge.h" +#include "lv2/atom/util.h" +#include "lv2/log/log.h" +#include "lv2/urid/urid.h" + +#include +#include +#include + +static char** uris = NULL; +static uint32_t n_uris = 0; + +static char* +copy_string(const char* str) +{ + const size_t len = strlen(str); + char* dup = (char*)malloc(len + 1); + memcpy(dup, str, len + 1); + return dup; +} + +static LV2_URID +urid_map(LV2_URID_Map_Handle handle, const char* uri) +{ + for (uint32_t i = 0; i < n_uris; ++i) { + if (!strcmp(uris[i], uri)) { + return i + 1; + } + } + + uris = (char**)realloc(uris, ++n_uris * sizeof(char*)); + uris[n_uris - 1] = copy_string(uri); + return n_uris; +} + +static void +free_urid_map(void) +{ + for (uint32_t i = 0; i < n_uris; ++i) { + free(uris[i]); + } + + free(uris); +} + +LV2_LOG_FUNC(1, 2) +static int +test_fail(const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + fprintf(stderr, "error: "); + vfprintf(stderr, fmt, args); + va_end(args); + return 1; +} diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/atom/atom-test.c b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/atom/atom-test.c new file mode 100644 index 0000000000..64f895305b --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/atom/atom-test.c @@ -0,0 +1,367 @@ +/* + Copyright 2012-2015 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "lv2/atom/atom-test-utils.c" +#include "lv2/atom/atom.h" +#include "lv2/atom/forge.h" +#include "lv2/atom/util.h" +#include "lv2/urid/urid.h" + +#include +#include +#include +#include +#include + +int +main(void) +{ + LV2_URID_Map map = {NULL, urid_map}; + LV2_Atom_Forge forge; + lv2_atom_forge_init(&forge, &map); + + LV2_URID eg_Object = urid_map(NULL, "http://example.org/Object"); + LV2_URID eg_one = urid_map(NULL, "http://example.org/one"); + LV2_URID eg_two = urid_map(NULL, "http://example.org/two"); + LV2_URID eg_three = urid_map(NULL, "http://example.org/three"); + LV2_URID eg_four = urid_map(NULL, "http://example.org/four"); + LV2_URID eg_true = urid_map(NULL, "http://example.org/true"); + LV2_URID eg_false = urid_map(NULL, "http://example.org/false"); + LV2_URID eg_path = urid_map(NULL, "http://example.org/path"); + LV2_URID eg_uri = urid_map(NULL, "http://example.org/uri"); + LV2_URID eg_urid = urid_map(NULL, "http://example.org/urid"); + LV2_URID eg_string = urid_map(NULL, "http://example.org/string"); + LV2_URID eg_literal = urid_map(NULL, "http://example.org/literal"); + LV2_URID eg_tuple = urid_map(NULL, "http://example.org/tuple"); + LV2_URID eg_vector = urid_map(NULL, "http://example.org/vector"); + LV2_URID eg_vector2 = urid_map(NULL, "http://example.org/vector2"); + LV2_URID eg_seq = urid_map(NULL, "http://example.org/seq"); + +#define BUF_SIZE 1024 +#define NUM_PROPS 15 + + uint8_t buf[BUF_SIZE]; + lv2_atom_forge_set_buffer(&forge, buf, BUF_SIZE); + + LV2_Atom_Forge_Frame obj_frame; + LV2_Atom* obj = lv2_atom_forge_deref( + &forge, lv2_atom_forge_object(&forge, &obj_frame, 0, eg_Object)); + + // eg_one = (Int)1 + lv2_atom_forge_key(&forge, eg_one); + LV2_Atom_Int* one = + (LV2_Atom_Int*)lv2_atom_forge_deref(&forge, lv2_atom_forge_int(&forge, 1)); + if (one->body != 1) { + return test_fail("%d != 1\n", one->body); + } + + // eg_two = (Long)2 + lv2_atom_forge_key(&forge, eg_two); + LV2_Atom_Long* two = (LV2_Atom_Long*)lv2_atom_forge_deref( + &forge, lv2_atom_forge_long(&forge, 2)); + if (two->body != 2) { + return test_fail("%" PRId64 " != 2\n", two->body); + } + + // eg_three = (Float)3.0 + lv2_atom_forge_key(&forge, eg_three); + LV2_Atom_Float* three = (LV2_Atom_Float*)lv2_atom_forge_deref( + &forge, lv2_atom_forge_float(&forge, 3.0f)); + if (three->body != 3) { + return test_fail("%f != 3\n", three->body); + } + + // eg_four = (Double)4.0 + lv2_atom_forge_key(&forge, eg_four); + LV2_Atom_Double* four = (LV2_Atom_Double*)lv2_atom_forge_deref( + &forge, lv2_atom_forge_double(&forge, 4.0)); + if (four->body != 4) { + return test_fail("%f != 4\n", four->body); + } + + // eg_true = (Bool)1 + lv2_atom_forge_key(&forge, eg_true); + LV2_Atom_Bool* t = (LV2_Atom_Bool*)lv2_atom_forge_deref( + &forge, lv2_atom_forge_bool(&forge, true)); + if (t->body != 1) { + return test_fail("%d != 1 (true)\n", t->body); + } + + // eg_false = (Bool)0 + lv2_atom_forge_key(&forge, eg_false); + LV2_Atom_Bool* f = (LV2_Atom_Bool*)lv2_atom_forge_deref( + &forge, lv2_atom_forge_bool(&forge, false)); + if (f->body != 0) { + return test_fail("%d != 0 (false)\n", f->body); + } + + // eg_path = (Path)"/foo/bar" + const char* pstr = "/foo/bar"; + const uint32_t pstr_len = (uint32_t)strlen(pstr); + lv2_atom_forge_key(&forge, eg_path); + LV2_Atom_String* path = (LV2_Atom_String*)lv2_atom_forge_deref( + &forge, lv2_atom_forge_uri(&forge, pstr, pstr_len)); + char* pbody = (char*)LV2_ATOM_BODY(path); + if (strcmp(pbody, pstr)) { + return test_fail("%s != \"%s\"\n", pbody, pstr); + } + + // eg_uri = (URI)"http://example.org/value" + const char* ustr = "http://example.org/value"; + const uint32_t ustr_len = (uint32_t)strlen(ustr); + lv2_atom_forge_key(&forge, eg_uri); + LV2_Atom_String* uri = (LV2_Atom_String*)lv2_atom_forge_deref( + &forge, lv2_atom_forge_uri(&forge, ustr, ustr_len)); + char* ubody = (char*)LV2_ATOM_BODY(uri); + if (strcmp(ubody, ustr)) { + return test_fail("%s != \"%s\"\n", ubody, ustr); + } + + // eg_urid = (URID)"http://example.org/value" + LV2_URID eg_value = urid_map(NULL, "http://example.org/value"); + lv2_atom_forge_key(&forge, eg_urid); + LV2_Atom_URID* urid = (LV2_Atom_URID*)lv2_atom_forge_deref( + &forge, lv2_atom_forge_urid(&forge, eg_value)); + if (urid->body != eg_value) { + return test_fail("%u != %u\n", urid->body, eg_value); + } + + // eg_string = (String)"hello" + lv2_atom_forge_key(&forge, eg_string); + LV2_Atom_String* string = (LV2_Atom_String*)lv2_atom_forge_deref( + &forge, lv2_atom_forge_string(&forge, "hello", strlen("hello"))); + char* sbody = (char*)LV2_ATOM_BODY(string); + if (strcmp(sbody, "hello")) { + return test_fail("%s != \"hello\"\n", sbody); + } + + // eg_literal = (Literal)"hello"@fr + lv2_atom_forge_key(&forge, eg_literal); + LV2_Atom_Literal* literal = (LV2_Atom_Literal*)lv2_atom_forge_deref( + &forge, + lv2_atom_forge_literal(&forge, + "bonjour", + strlen("bonjour"), + 0, + urid_map(NULL, "http://lexvo.org/id/term/fr"))); + char* lbody = (char*)LV2_ATOM_CONTENTS(LV2_Atom_Literal, literal); + if (strcmp(lbody, "bonjour")) { + return test_fail("%s != \"bonjour\"\n", lbody); + } + + // eg_tuple = "foo",true + lv2_atom_forge_key(&forge, eg_tuple); + LV2_Atom_Forge_Frame tuple_frame; + LV2_Atom_Tuple* tuple = (LV2_Atom_Tuple*)lv2_atom_forge_deref( + &forge, lv2_atom_forge_tuple(&forge, &tuple_frame)); + LV2_Atom_String* tup0 = (LV2_Atom_String*)lv2_atom_forge_deref( + &forge, lv2_atom_forge_string(&forge, "foo", strlen("foo"))); + LV2_Atom_Bool* tup1 = (LV2_Atom_Bool*)lv2_atom_forge_deref( + &forge, lv2_atom_forge_bool(&forge, true)); + lv2_atom_forge_pop(&forge, &tuple_frame); + LV2_Atom* i = lv2_atom_tuple_begin(tuple); + if (lv2_atom_tuple_is_end(LV2_ATOM_BODY(tuple), tuple->atom.size, i)) { + return test_fail("Tuple iterator is empty\n"); + } + LV2_Atom* tup0i = i; + if (!lv2_atom_equals((LV2_Atom*)tup0, tup0i)) { + return test_fail("Corrupt tuple element 0\n"); + } + i = lv2_atom_tuple_next(i); + if (lv2_atom_tuple_is_end(LV2_ATOM_BODY(tuple), tuple->atom.size, i)) { + return test_fail("Premature end of tuple iterator\n"); + } + LV2_Atom* tup1i = i; + if (!lv2_atom_equals((LV2_Atom*)tup1, tup1i)) { + return test_fail("Corrupt tuple element 1\n"); + } + i = lv2_atom_tuple_next(i); + if (!lv2_atom_tuple_is_end(LV2_ATOM_BODY(tuple), tuple->atom.size, i)) { + return test_fail("Tuple iter is not at end\n"); + } + + // eg_vector = (Vector)1,2,3,4 + lv2_atom_forge_key(&forge, eg_vector); + int32_t elems[] = {1, 2, 3, 4}; + LV2_Atom_Vector* vector = (LV2_Atom_Vector*)lv2_atom_forge_deref( + &forge, + lv2_atom_forge_vector(&forge, sizeof(int32_t), forge.Int, 4, elems)); + void* vec_body = LV2_ATOM_CONTENTS(LV2_Atom_Vector, vector); + if (memcmp(elems, vec_body, sizeof(elems))) { + return test_fail("Corrupt vector\n"); + } + + // eg_vector2 = (Vector)1,2,3,4 + lv2_atom_forge_key(&forge, eg_vector2); + LV2_Atom_Forge_Frame vec_frame; + LV2_Atom_Vector* vector2 = (LV2_Atom_Vector*)lv2_atom_forge_deref( + &forge, + lv2_atom_forge_vector_head(&forge, &vec_frame, sizeof(int32_t), forge.Int)); + for (unsigned e = 0; e < sizeof(elems) / sizeof(int32_t); ++e) { + lv2_atom_forge_int(&forge, elems[e]); + } + lv2_atom_forge_pop(&forge, &vec_frame); + if (!lv2_atom_equals(&vector->atom, &vector2->atom)) { + return test_fail("Vector != Vector2\n"); + } + + // eg_seq = (Sequence)1, 2 + lv2_atom_forge_key(&forge, eg_seq); + LV2_Atom_Forge_Frame seq_frame; + LV2_Atom_Sequence* seq = (LV2_Atom_Sequence*)lv2_atom_forge_deref( + &forge, lv2_atom_forge_sequence_head(&forge, &seq_frame, 0)); + lv2_atom_forge_frame_time(&forge, 0); + lv2_atom_forge_int(&forge, 1); + lv2_atom_forge_frame_time(&forge, 1); + lv2_atom_forge_int(&forge, 2); + lv2_atom_forge_pop(&forge, &seq_frame); + + lv2_atom_forge_pop(&forge, &obj_frame); + + // Test equality + LV2_Atom_Int itwo = {{forge.Int, sizeof(int32_t)}, 2}; + if (lv2_atom_equals((LV2_Atom*)one, (LV2_Atom*)two)) { + return test_fail("1 == 2.0\n"); + } else if (lv2_atom_equals((LV2_Atom*)one, (LV2_Atom*)&itwo)) { + return test_fail("1 == 2\n"); + } else if (!lv2_atom_equals((LV2_Atom*)one, (LV2_Atom*)one)) { + return test_fail("1 != 1\n"); + } + + unsigned n_events = 0; + LV2_ATOM_SEQUENCE_FOREACH (seq, ev) { + if (ev->time.frames != n_events) { + return test_fail("Corrupt event %u has bad time\n", n_events); + } else if (ev->body.type != forge.Int) { + return test_fail("Corrupt event %u has bad type\n", n_events); + } else if (((LV2_Atom_Int*)&ev->body)->body != (int)n_events + 1) { + return test_fail("Event %u != %u\n", n_events, n_events + 1); + } + ++n_events; + } + + int n_props = 0; + LV2_ATOM_OBJECT_FOREACH ((LV2_Atom_Object*)obj, prop) { + if (!prop->key) { + return test_fail("Corrupt property %d has no key\n", n_props); + } else if (prop->context) { + return test_fail("Corrupt property %d has context\n", n_props); + } + ++n_props; + } + + if (n_props != NUM_PROPS) { + return test_fail( + "Corrupt object has %d properties != %d\n", n_props, NUM_PROPS); + } + + struct { + const LV2_Atom* one; + const LV2_Atom* two; + const LV2_Atom* three; + const LV2_Atom* four; + const LV2_Atom* affirmative; + const LV2_Atom* negative; + const LV2_Atom* path; + const LV2_Atom* uri; + const LV2_Atom* urid; + const LV2_Atom* string; + const LV2_Atom* literal; + const LV2_Atom* tuple; + const LV2_Atom* vector; + const LV2_Atom* vector2; + const LV2_Atom* seq; + } matches; + + memset(&matches, 0, sizeof(matches)); + + LV2_Atom_Object_Query q[] = {{eg_one, &matches.one}, + {eg_two, &matches.two}, + {eg_three, &matches.three}, + {eg_four, &matches.four}, + {eg_true, &matches.affirmative}, + {eg_false, &matches.negative}, + {eg_path, &matches.path}, + {eg_uri, &matches.uri}, + {eg_urid, &matches.urid}, + {eg_string, &matches.string}, + {eg_literal, &matches.literal}, + {eg_tuple, &matches.tuple}, + {eg_vector, &matches.vector}, + {eg_vector2, &matches.vector2}, + {eg_seq, &matches.seq}, + LV2_ATOM_OBJECT_QUERY_END}; + + int n_matches = lv2_atom_object_query((LV2_Atom_Object*)obj, q); + for (int n = 0; n < 2; ++n) { + if (n_matches != n_props) { + return test_fail("Query failed, %d matches != %d\n", n_matches, n_props); + } else if (!lv2_atom_equals((LV2_Atom*)one, matches.one)) { + return test_fail("Bad match one\n"); + } else if (!lv2_atom_equals((LV2_Atom*)two, matches.two)) { + return test_fail("Bad match two\n"); + } else if (!lv2_atom_equals((LV2_Atom*)three, matches.three)) { + return test_fail("Bad match three\n"); + } else if (!lv2_atom_equals((LV2_Atom*)four, matches.four)) { + return test_fail("Bad match four\n"); + } else if (!lv2_atom_equals((LV2_Atom*)t, matches.affirmative)) { + return test_fail("Bad match true\n"); + } else if (!lv2_atom_equals((LV2_Atom*)f, matches.negative)) { + return test_fail("Bad match false\n"); + } else if (!lv2_atom_equals((LV2_Atom*)path, matches.path)) { + return test_fail("Bad match path\n"); + } else if (!lv2_atom_equals((LV2_Atom*)uri, matches.uri)) { + return test_fail("Bad match URI\n"); + } else if (!lv2_atom_equals((LV2_Atom*)string, matches.string)) { + return test_fail("Bad match string\n"); + } else if (!lv2_atom_equals((LV2_Atom*)literal, matches.literal)) { + return test_fail("Bad match literal\n"); + } else if (!lv2_atom_equals((LV2_Atom*)tuple, matches.tuple)) { + return test_fail("Bad match tuple\n"); + } else if (!lv2_atom_equals((LV2_Atom*)vector, matches.vector)) { + return test_fail("Bad match vector\n"); + } else if (!lv2_atom_equals((LV2_Atom*)vector, matches.vector2)) { + return test_fail("Bad match vector2\n"); + } else if (!lv2_atom_equals((LV2_Atom*)seq, matches.seq)) { + return test_fail("Bad match sequence\n"); + } + memset(&matches, 0, sizeof(matches)); + + // clang-format off + n_matches = lv2_atom_object_get((LV2_Atom_Object*)obj, + eg_one, &matches.one, + eg_two, &matches.two, + eg_three, &matches.three, + eg_four, &matches.four, + eg_true, &matches.affirmative, + eg_false, &matches.negative, + eg_path, &matches.path, + eg_uri, &matches.uri, + eg_urid, &matches.urid, + eg_string, &matches.string, + eg_literal, &matches.literal, + eg_tuple, &matches.tuple, + eg_vector, &matches.vector, + eg_vector2, &matches.vector2, + eg_seq, &matches.seq, + 0); + // clang-format on + } + + free_urid_map(); + + return 0; +} diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/atom/atom.h b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/atom/atom.h new file mode 100644 index 0000000000..54831558f3 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/atom/atom.h @@ -0,0 +1,260 @@ +/* + Copyright 2008-2016 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef LV2_ATOM_H +#define LV2_ATOM_H + +/** + @defgroup atom Atom + @ingroup lv2 + + A generic value container and several data types. + + See for details. + + @{ +*/ + +#include + +// clang-format off + +#define LV2_ATOM_URI "http://lv2plug.in/ns/ext/atom" ///< http://lv2plug.in/ns/ext/atom +#define LV2_ATOM_PREFIX LV2_ATOM_URI "#" ///< http://lv2plug.in/ns/ext/atom# + +#define LV2_ATOM__Atom LV2_ATOM_PREFIX "Atom" ///< http://lv2plug.in/ns/ext/atom#Atom +#define LV2_ATOM__AtomPort LV2_ATOM_PREFIX "AtomPort" ///< http://lv2plug.in/ns/ext/atom#AtomPort +#define LV2_ATOM__Blank LV2_ATOM_PREFIX "Blank" ///< http://lv2plug.in/ns/ext/atom#Blank +#define LV2_ATOM__Bool LV2_ATOM_PREFIX "Bool" ///< http://lv2plug.in/ns/ext/atom#Bool +#define LV2_ATOM__Chunk LV2_ATOM_PREFIX "Chunk" ///< http://lv2plug.in/ns/ext/atom#Chunk +#define LV2_ATOM__Double LV2_ATOM_PREFIX "Double" ///< http://lv2plug.in/ns/ext/atom#Double +#define LV2_ATOM__Event LV2_ATOM_PREFIX "Event" ///< http://lv2plug.in/ns/ext/atom#Event +#define LV2_ATOM__Float LV2_ATOM_PREFIX "Float" ///< http://lv2plug.in/ns/ext/atom#Float +#define LV2_ATOM__Int LV2_ATOM_PREFIX "Int" ///< http://lv2plug.in/ns/ext/atom#Int +#define LV2_ATOM__Literal LV2_ATOM_PREFIX "Literal" ///< http://lv2plug.in/ns/ext/atom#Literal +#define LV2_ATOM__Long LV2_ATOM_PREFIX "Long" ///< http://lv2plug.in/ns/ext/atom#Long +#define LV2_ATOM__Number LV2_ATOM_PREFIX "Number" ///< http://lv2plug.in/ns/ext/atom#Number +#define LV2_ATOM__Object LV2_ATOM_PREFIX "Object" ///< http://lv2plug.in/ns/ext/atom#Object +#define LV2_ATOM__Path LV2_ATOM_PREFIX "Path" ///< http://lv2plug.in/ns/ext/atom#Path +#define LV2_ATOM__Property LV2_ATOM_PREFIX "Property" ///< http://lv2plug.in/ns/ext/atom#Property +#define LV2_ATOM__Resource LV2_ATOM_PREFIX "Resource" ///< http://lv2plug.in/ns/ext/atom#Resource +#define LV2_ATOM__Sequence LV2_ATOM_PREFIX "Sequence" ///< http://lv2plug.in/ns/ext/atom#Sequence +#define LV2_ATOM__Sound LV2_ATOM_PREFIX "Sound" ///< http://lv2plug.in/ns/ext/atom#Sound +#define LV2_ATOM__String LV2_ATOM_PREFIX "String" ///< http://lv2plug.in/ns/ext/atom#String +#define LV2_ATOM__Tuple LV2_ATOM_PREFIX "Tuple" ///< http://lv2plug.in/ns/ext/atom#Tuple +#define LV2_ATOM__URI LV2_ATOM_PREFIX "URI" ///< http://lv2plug.in/ns/ext/atom#URI +#define LV2_ATOM__URID LV2_ATOM_PREFIX "URID" ///< http://lv2plug.in/ns/ext/atom#URID +#define LV2_ATOM__Vector LV2_ATOM_PREFIX "Vector" ///< http://lv2plug.in/ns/ext/atom#Vector +#define LV2_ATOM__atomTransfer LV2_ATOM_PREFIX "atomTransfer" ///< http://lv2plug.in/ns/ext/atom#atomTransfer +#define LV2_ATOM__beatTime LV2_ATOM_PREFIX "beatTime" ///< http://lv2plug.in/ns/ext/atom#beatTime +#define LV2_ATOM__bufferType LV2_ATOM_PREFIX "bufferType" ///< http://lv2plug.in/ns/ext/atom#bufferType +#define LV2_ATOM__childType LV2_ATOM_PREFIX "childType" ///< http://lv2plug.in/ns/ext/atom#childType +#define LV2_ATOM__eventTransfer LV2_ATOM_PREFIX "eventTransfer" ///< http://lv2plug.in/ns/ext/atom#eventTransfer +#define LV2_ATOM__frameTime LV2_ATOM_PREFIX "frameTime" ///< http://lv2plug.in/ns/ext/atom#frameTime +#define LV2_ATOM__supports LV2_ATOM_PREFIX "supports" ///< http://lv2plug.in/ns/ext/atom#supports +#define LV2_ATOM__timeUnit LV2_ATOM_PREFIX "timeUnit" ///< http://lv2plug.in/ns/ext/atom#timeUnit + +// clang-format on + +#define LV2_ATOM_REFERENCE_TYPE 0 ///< The special type for a reference atom + +#ifdef __cplusplus +extern "C" { +#endif + +/** @cond */ +/** This expression will fail to compile if double does not fit in 64 bits. */ +typedef char lv2_atom_assert_double_fits_in_64_bits + [((sizeof(double) <= sizeof(uint64_t)) * 2) - 1]; +/** @endcond */ + +/** + Return a pointer to the contents of an Atom. The "contents" of an atom + is the data past the complete type-specific header. + @param type The type of the atom, for example LV2_Atom_String. + @param atom A variable-sized atom. +*/ +#define LV2_ATOM_CONTENTS(type, atom) ((void*)((uint8_t*)(atom) + sizeof(type))) + +/** + Const version of LV2_ATOM_CONTENTS. +*/ +#define LV2_ATOM_CONTENTS_CONST(type, atom) \ + ((const void*)((const uint8_t*)(atom) + sizeof(type))) + +/** + Return a pointer to the body of an Atom. The "body" of an atom is the + data just past the LV2_Atom head (i.e. the same offset for all types). +*/ +#define LV2_ATOM_BODY(atom) LV2_ATOM_CONTENTS(LV2_Atom, atom) + +/** + Const version of LV2_ATOM_BODY. +*/ +#define LV2_ATOM_BODY_CONST(atom) LV2_ATOM_CONTENTS_CONST(LV2_Atom, atom) + +/** The header of an atom:Atom. */ +typedef struct { + uint32_t size; /**< Size in bytes, not including type and size. */ + uint32_t type; /**< Type of this atom (mapped URI). */ +} LV2_Atom; + +/** An atom:Int or atom:Bool. May be cast to LV2_Atom. */ +typedef struct { + LV2_Atom atom; /**< Atom header. */ + int32_t body; /**< Integer value. */ +} LV2_Atom_Int; + +/** An atom:Long. May be cast to LV2_Atom. */ +typedef struct { + LV2_Atom atom; /**< Atom header. */ + int64_t body; /**< Integer value. */ +} LV2_Atom_Long; + +/** An atom:Float. May be cast to LV2_Atom. */ +typedef struct { + LV2_Atom atom; /**< Atom header. */ + float body; /**< Floating point value. */ +} LV2_Atom_Float; + +/** An atom:Double. May be cast to LV2_Atom. */ +typedef struct { + LV2_Atom atom; /**< Atom header. */ + double body; /**< Floating point value. */ +} LV2_Atom_Double; + +/** An atom:Bool. May be cast to LV2_Atom. */ +typedef LV2_Atom_Int LV2_Atom_Bool; + +/** An atom:URID. May be cast to LV2_Atom. */ +typedef struct { + LV2_Atom atom; /**< Atom header. */ + uint32_t body; /**< URID. */ +} LV2_Atom_URID; + +/** An atom:String. May be cast to LV2_Atom. */ +typedef struct { + LV2_Atom atom; /**< Atom header. */ + /* Contents (a null-terminated UTF-8 string) follow here. */ +} LV2_Atom_String; + +/** The body of an atom:Literal. */ +typedef struct { + uint32_t datatype; /**< Datatype URID. */ + uint32_t lang; /**< Language URID. */ + /* Contents (a null-terminated UTF-8 string) follow here. */ +} LV2_Atom_Literal_Body; + +/** An atom:Literal. May be cast to LV2_Atom. */ +typedef struct { + LV2_Atom atom; /**< Atom header. */ + LV2_Atom_Literal_Body body; /**< Body. */ +} LV2_Atom_Literal; + +/** An atom:Tuple. May be cast to LV2_Atom. */ +typedef struct { + LV2_Atom atom; /**< Atom header. */ + /* Contents (a series of complete atoms) follow here. */ +} LV2_Atom_Tuple; + +/** The body of an atom:Vector. */ +typedef struct { + uint32_t child_size; /**< The size of each element in the vector. */ + uint32_t child_type; /**< The type of each element in the vector. */ + /* Contents (a series of packed atom bodies) follow here. */ +} LV2_Atom_Vector_Body; + +/** An atom:Vector. May be cast to LV2_Atom. */ +typedef struct { + LV2_Atom atom; /**< Atom header. */ + LV2_Atom_Vector_Body body; /**< Body. */ +} LV2_Atom_Vector; + +/** The body of an atom:Property (typically in an atom:Object). */ +typedef struct { + uint32_t key; /**< Key (predicate) (mapped URI). */ + uint32_t context; /**< Context URID (may be, and generally is, 0). */ + LV2_Atom value; /**< Value atom header. */ + /* Value atom body follows here. */ +} LV2_Atom_Property_Body; + +/** An atom:Property. May be cast to LV2_Atom. */ +typedef struct { + LV2_Atom atom; /**< Atom header. */ + LV2_Atom_Property_Body body; /**< Body. */ +} LV2_Atom_Property; + +/** The body of an atom:Object. May be cast to LV2_Atom. */ +typedef struct { + uint32_t id; /**< URID, or 0 for blank. */ + uint32_t otype; /**< Type URID (same as rdf:type, for fast dispatch). */ + /* Contents (a series of property bodies) follow here. */ +} LV2_Atom_Object_Body; + +/** An atom:Object. May be cast to LV2_Atom. */ +typedef struct { + LV2_Atom atom; /**< Atom header. */ + LV2_Atom_Object_Body body; /**< Body. */ +} LV2_Atom_Object; + +/** The header of an atom:Event. Note this type is NOT an LV2_Atom. */ +typedef struct { + /** Time stamp. Which type is valid is determined by context. */ + union { + int64_t frames; /**< Time in audio frames. */ + double beats; /**< Time in beats. */ + } time; + LV2_Atom body; /**< Event body atom header. */ + /* Body atom contents follow here. */ +} LV2_Atom_Event; + +/** + The body of an atom:Sequence (a sequence of events). + + The unit field is either a URID that described an appropriate time stamp + type, or may be 0 where a default stamp type is known. For + LV2_Descriptor::run(), the default stamp type is audio frames. + + The contents of a sequence is a series of LV2_Atom_Event, each aligned + to 64-bits, for example: +
+   | Event 1 (size 6)                              | Event 2
+   |       |       |       |       |       |       |       |       |
+   | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
+   |FRAMES |SUBFRMS|TYPE   |SIZE   |DATADATADATAPAD|FRAMES |SUBFRMS|...
+   
+*/ +typedef struct { + uint32_t unit; /**< URID of unit of event time stamps. */ + uint32_t pad; /**< Currently unused. */ + /* Contents (a series of events) follow here. */ +} LV2_Atom_Sequence_Body; + +/** An atom:Sequence. */ +typedef struct { + LV2_Atom atom; /**< Atom header. */ + LV2_Atom_Sequence_Body body; /**< Body. */ +} LV2_Atom_Sequence; + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +/** + @} +*/ + +#endif /* LV2_ATOM_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/atom/atom.meta.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/atom/atom.meta.ttl new file mode 100644 index 0000000000..9d714d8056 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/atom/atom.meta.ttl @@ -0,0 +1,542 @@ +@prefix atom: . +@prefix dcs: . +@prefix doap: . +@prefix foaf: . +@prefix lv2: . +@prefix rdfs: . + + + a doap:Project ; + doap:name "LV2 Atom" ; + doap:shortdesc "A generic value container and several data types." ; + doap:license ; + doap:created "2007-00-00" ; + doap:developer ; + doap:release [ + doap:revision "2.2" ; + doap:created "2019-02-03" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add lv2_atom_object_get_typed() for easy type-safe access to object properties." + ] + ] + ] , [ + doap:revision "2.0" ; + doap:created "2014-08-08" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Deprecate Blank and Resource in favour of just Object." + ] , [ + rdfs:label "Add lv2_atom_forge_is_object_type() and lv2_atom_forge_is_blank() to ease backwards compatibility." + ] , [ + rdfs:label "Add lv2_atom_forge_key() for terser object writing." + ] , [ + rdfs:label "Add lv2_atom_sequence_clear() and lv2_atom_sequence_append_event() helper functions." + ] + ] + ] , [ + doap:revision "1.8" ; + doap:created "2014-01-04" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Make lv2_atom_*_is_end() arguments const." + ] + ] + ] , [ + doap:revision "1.6" ; + doap:created "2013-05-26" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Fix crash in forge.h when pushing atoms to a full buffer." + ] + ] + ] , [ + doap:revision "1.4" ; + doap:created "2013-01-27" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Fix lv2_atom_sequence_end()." + ] , [ + rdfs:label "Remove atom:stringType in favour of owl:onDatatype so generic tools can understand and validate atom literals." + ] , [ + rdfs:label "Improve atom documentation." + ] + ] + ] , [ + doap:revision "1.2" ; + doap:created "2012-10-14" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Fix implicit conversions in forge.h that are invalid in C++11." + ] , [ + rdfs:label "Fix lv2_atom_object_next() on 32-bit platforms." + ] , [ + rdfs:label "Add lv2_atom_object_body_get()." + ] , [ + rdfs:label "Fix outdated documentation in forge.h." + ] , [ + rdfs:label "Use consistent label style." + ] , [ + rdfs:label "Add LV2_ATOM_CONTENTS_CONST and LV2_ATOM_BODY_CONST." + ] + ] + ] , [ + doap:revision "1.0" ; + doap:created "2012-04-17" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Initial release." + ] + ] + ] ; + lv2:documentation """ + +An atom:Atom is a simple generic data container for holding any type of Plain +Old Data (POD). An Atom can contain simple primitive types like integers, +floating point numbers, and strings; as well as structured data like lists and +dictionary-like Objects. Since Atoms are POD, they can be easily copied +(for example, with `memcpy()`) anywhere and are suitable for use in real-time +code. + +Every atom starts with an LV2_Atom header, followed by the contents. This +allows code to process atoms without requiring special code for every type of +data. For example, plugins that mutually understand a type can be used +together in a host that does not understand that type, because the host is only +required to copy atoms, not interpret their contents. Similarly, plugins (such +as routers, delays, or data structures) can meaningfully process atoms of a +type unknown to them. + +Atoms should be used anywhere values of various types must be stored or +transmitted. An atom:AtomPort can be used to transmit atoms via ports. An +atom:AtomPort that contains a atom:Sequence can be used for sample accurate +communication of events, such as MIDI. + +### Serialisation + +Each Atom type defines a binary format for use at runtime, but also a +serialisation that is natural to express in Turtle format. Thus, this +specification defines a powerful real-time appropriate data model, as well as a +portable way to serialise any data in that model. This is particularly useful +for inter-process communication, saving/restoring state, and describing values +in plugin data files. + +### Custom Atom Types + +While it is possible to define new Atom types for any binary format, the +standard types defined here are powerful enough to describe almost anything. +Implementations SHOULD build structures out of the types provided here, rather +than define new binary formats (for example, using atom:Object rather than a +new C `struct` type). Host and tool implementations have support for +serialising all standard types, so new binary formats are an implementation +burden which harms interoperabilty. In particular, plugins SHOULD NOT expect +UI communication or state saving with custom binary types to work. In general, +new Atom types should only be defined where absolutely necessary due to +performance reasons and serialisation is not a concern. + +"""^^lv2:Markdown . + +atom:Atom + lv2:documentation """ + +An LV2_Atom has a 32-bit `size` and `type`, followed by a body of `size` bytes. +Atoms MUST be 64-bit aligned. + +All concrete Atom types (subclasses of this class) MUST define a precise binary +layout for their body. + +The `type` field is the URI of an Atom type mapped to an integer. +Implementations SHOULD gracefully pass through, or ignore, atoms with unknown +types. + +All atoms are POD by definition except references, which as a special case have +`type` 0. An Atom MUST NOT contain a Reference. It is safe to copy any +non-reference Atom with a simple `memcpy`, even if the implementation does not +understand `type`. Though this extension reserves the type 0 for references, +the details of reference handling are currently unspecified. A future revision +of this extension, or a different extension, may define how to use non-POD data +and references. Implementations MUST NOT send references to another +implementation unless the receiver is explicitly known to support references +(e.g. by supporting a feature). + +The special case of a null atom with both `type` and `size` 0 is not considered +a reference. + +"""^^lv2:Markdown . + +atom:Chunk + lv2:documentation """ + +This type is used to indicate a certain amount of space is available. For +example, output ports with a variably sized type are connected to a Chunk so +the plugin knows the size of the buffer available for writing. + +The use of a Chunk should be constrained to a local scope, since +interpreting it is impossible without context. However, if serialised to RDF, +a Chunk may be represented directly as an xsd:base64Binary string, for example: + + :::turtle + [] eg:someChunk "vu/erQ=="^^xsd:base64Binary . + +"""^^lv2:Markdown . + +atom:String + lv2:documentation """ + +The body of an LV2_Atom_String is a C string in UTF-8 encoding, i.e. an array +of bytes (`uint8_t`) terminated with a NULL byte (`'\\0'`). + +This type is for free-form strings, but SHOULD NOT be used for typed data or +text in any language. Use atom:Literal unless translating the string does not +make sense and the string has no meaningful datatype. + +"""^^lv2:Markdown . + +atom:Literal + lv2:documentation """ + +This type is compatible with rdfs:Literal and is capable of expressing a +string in any language or a value of any type. A Literal has a +`datatype` and `lang` followed by string data in UTF-8 +encoding. The length of the string data in bytes is `size - +sizeof(LV2_Atom_Literal)`, including the terminating NULL character. The +`lang` field SHOULD be a URI of the form +`http://lexvo.org/id/iso639-3/LANG` or +`http://lexvo.org/id/iso639-1/LANG` where LANG is a 3-character ISO 693-3 +language code, or a 2-character ISO 693-1 language code, respectively. + +A Literal may have a `datatype` or a `lang`, but never both. + +For example, a Literal can be Hello in English: + + :::c + void set_to_hello_in_english(LV2_Atom_Literal* lit) { + lit->atom.type = map(expand("atom:Literal")); + lit->atom.size = 14; + lit->body.datatype = 0; + lit->body.lang = map("http://lexvo.org/id/iso639-1/en"); + memcpy(LV2_ATOM_CONTENTS(LV2_Atom_Literal, lit), + "Hello", + sizeof("Hello")); // Assumes enough space + } + +or a Turtle string: + + :::c + void set_to_turtle_string(LV2_Atom_Literal* lit, const char* ttl) { + lit->atom.type = map(expand("atom:Literal")); + lit->atom.size = 64; + lit->body.datatype = map("http://www.w3.org/2008/turtle#turtle"); + lit->body.lang = 0; + memcpy(LV2_ATOM_CONTENTS(LV2_Atom_Literal, lit), + ttl, + strlen(ttl) + 1); // Assumes enough space + } + +"""^^lv2:Markdown . + +atom:Path + lv2:documentation """ + +A Path is a URI reference with only a path component: no scheme, authority, +query, or fragment. In particular, paths to files in the same bundle may be +cleanly written in Turtle files as a relative URI. However, implementations +may assume any binary Path (e.g. in an event payload) is a valid file path +which can passed to system functions like fopen() directly, without any +character encoding or escape expansion required. + +Any implemenation that creates a Path atom to transmit to another is +responsible for ensuring it is valid. A Path SHOULD always be absolute, unless +there is some mechanism in place that defines a base path. Since this is not +the case for plugin instances, effectively any Path sent to or received from a +plugin instance MUST be absolute. + +"""^^lv2:Markdown . + +atom:URI + lv2:documentation """ + +This is useful when a URI is needed but mapping is inappropriate, for example +with temporary or relative URIs. Since the ability to distinguish URIs from +plain strings is often necessary, URIs MUST NOT be transmitted as atom:String. + +This is not strictly a URI, since UTF-8 is allowed. Escaping and related +issues are the host's responsibility. + +"""^^lv2:Markdown . + +atom:URID + lv2:documentation """ + +A URID is typically generated with the LV2_URID_Map provided by the host . + +"""^^lv2:Markdown . + +atom:Vector + lv2:documentation """ + +A homogeneous series of atom bodies with equivalent type and size. + +An LV2_Atom_Vector is a 32-bit `child_size` and `child_type` followed by `size +/ child_size` atom bodies. + +For example, an atom:Vector containing 42 elements of type atom:Float: + + :::c + struct VectorOf42Floats { + uint32_t size; // sizeof(LV2_Atom_Vector_Body) + (42 * sizeof(float); + uint32_t type; // map(expand("atom:Vector")) + uint32_t child_size; // sizeof(float) + uint32_t child_type; // map(expand("atom:Float")) + float elems[42]; + }; + +Note that it is possible to construct a valid Atom for each element of the +vector, even by an implementation which does not understand `child_type`. + +If serialised to RDF, a Vector SHOULD have the form: + + :::turtle + eg:someVector + a atom:Vector ; + atom:childType atom:Int ; + rdf:value ( + "1"^^xsd:int + "2"^^xsd:int + "3"^^xsd:int + "4"^^xsd:int + ) . + +"""^^lv2:Markdown . + +atom:Tuple + lv2:documentation """ + +The body of a Tuple is simply a series of complete atoms, each aligned to +64 bits. + +If serialised to RDF, a Tuple SHOULD have the form: + + :::turtle + eg:someVector + a atom:Tuple ; + rdf:value ( + "1"^^xsd:int + "3.5"^^xsd:float + "etc" + ) . + +"""^^lv2:Markdown . + +atom:Property + lv2:documentation """ + +An LV2_Atom_Property has a URID `key` and `context`, and an Atom `value`. This +corresponds to an RDF Property, where the key is the predicate +and the value is the object. + +The `context` field can be used to specify a different context for each +property, where this is useful. Otherwise, it may be 0. + +Properties generally only exist as part of an atom:Object. Accordingly, +they will typically be represented directly as properties in RDF (see +atom:Object). If this is not possible, they may be expressed as partial +reified statements, for example: + + :::turtle + eg:someProperty + rdf:predicate eg:theKey ; + rdf:object eg:theValue . + +"""^^lv2:Markdown . + +atom:Object + lv2:documentation """ + +An Object is an atom with a set of properties. This corresponds to an +RDF Resource, and can be thought of as a dictionary with URID keys. + +An LV2_Atom_Object body has a uint32_t `id` and `type`, followed by a series of +atom:Property bodies (LV2_Atom_Property_Body). The LV2_Atom_Object_Body::otype +field is equivalent to a property with key rdf:type, but is included in the +structure to allow for fast dispatching. + +Code SHOULD check for objects using lv2_atom_forge_is_object() or +lv2_atom_forge_is_blank() if a forge is available, rather than checking the +atom type directly. This will correctly handle the deprecated atom:Resource +and atom:Blank types. + +When serialised to RDF, an Object is represented as a resource, for example: + + :::turtle + eg:someObject + eg:firstPropertyKey "first property value" ; + eg:secondPropertyKey "first loser" ; + eg:andSoOn "and so on" . + +"""^^lv2:Markdown . + +atom:Resource + lv2:documentation """ + +This class is deprecated. Use atom:Object directly instead. + +An atom:Object where the id field is a URID, that is, an Object +with a URI. + +"""^^lv2:Markdown . + +atom:Blank + lv2:documentation """ + +This class is deprecated. Use atom:Object with ID 0 instead. + +An atom:Object where the LV2_Atom_Object::id is a blank node ID (NOT a URI). +The ID of a Blank is valid only within the context the Blank appears in. For +ports this is the context of the associated run() call, i.e. all ports share +the same context so outputs can contain IDs that correspond to IDs of blanks in +the input. + +"""^^lv2:Markdown . + +atom:Sound + lv2:documentation """ + +The format of a atom:Sound is the same as the buffer format for lv2:AudioPort +(except the size may be arbitrary). An atom:Sound inherently depends on the +sample rate, which is assumed to be known from context. Because of this, +directly serialising an atom:Sound is probably a bad idea, use a standard +format like WAV instead. + +"""^^lv2:Markdown . + +atom:Event + lv2:documentation """ + +An Event is typically an element of an atom:Sequence. Note that this is not an Atom type since it begins with a timestamp, not an atom header. + +"""^^lv2:Markdown . + +atom:Sequence + lv2:documentation """ + +A flat sequence of atom:Event, that is, a series of time-stamped Atoms. + +LV2_Atom_Sequence_Body.unit describes the time unit for the contained atoms. +If the unit is known from context (e.g. run() stamps are always audio frames), +this field may be zero. Otherwise, it SHOULD be either units:frame or +units:beat, in which case ev.time.frames or ev.time.beats is valid, +respectively. + +If serialised to RDF, a Sequence has a similar form to atom:Vector, but for +brevity the elements may be assumed to be atom:Event, for example: + + :::turtle + eg:someSequence + a atom:Sequence ; + rdf:value ( + [ + atom:frameTime 1 ; + rdf:value "901A01"^^midi:MidiEvent + ] [ + atom:frameTime 3 ; + rdf:value "902B02"^^midi:MidiEvent + ] + ) . + +"""^^lv2:Markdown . + +atom:AtomPort + lv2:documentation """ + +Ports of this type are connected to an LV2_Atom with a type specified by +atom:bufferType. + +Output ports with a variably sized type MUST be initialised by the host before +every run() to an atom:Chunk with size set to the available space. The plugin +reads this size to know how much space is available for writing. In all cases, +the plugin MUST write a complete atom (including header) to outputs. However, +to be robust, hosts SHOULD initialise output ports to a safe sentinel (e.g. the +null Atom) before calling run(). + +"""^^lv2:Markdown . + +atom:bufferType + lv2:documentation """ + +Indicates that an AtomPort may be connected to a certain Atom type. A port MAY +support several buffer types. The host MUST NOT connect a port to an Atom with +a type not explicitly listed with this property. The value of this property +MUST be a sub-class of atom:Atom. For example, an input port that is connected +directly to an LV2_Atom_Double value is described like so: + + :::turtle + + lv2:port [ + a lv2:InputPort , atom:AtomPort ; + atom:bufferType atom:Double ; + ] . + +This property only describes the types a port may be directly connected to. It +says nothing about the expected contents of containers. For that, use +atom:supports. + +"""^^lv2:Markdown . + +atom:supports + lv2:documentation """ + +This property is defined loosely, it may be used to indicate that anything +supports an Atom type, wherever that may be useful. It applies +recursively where collections are involved. + +In particular, this property can be used to describe which event types are +expected by a port. For example, a port that receives MIDI events is described +like so: + + :::turtle + + lv2:port [ + a lv2:InputPort , atom:AtomPort ; + atom:bufferType atom:Sequence ; + atom:supports midi:MidiEvent ; + ] . + +"""^^lv2:Markdown . + +atom:eventTransfer + lv2:documentation """ + +Transfer of individual events in a port buffer. Useful as the `format` for a +LV2UI_Write_Function. + +This protocol applies to ports which contain events, usually in an +atom:Sequence. The host must transfer each individual event to the recipient. +The format of the received data is an LV2_Atom, there is no timestamp header. + +"""^^lv2:Markdown . + +atom:atomTransfer + lv2:documentation """ + +Transfer of the complete atom in a port buffer. Useful as the `format` for a +LV2UI_Write_Function. + +This protocol applies to atom ports. The host must transfer the complete atom +contained in the port, including header. + +"""^^lv2:Markdown . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/atom/atom.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/atom/atom.ttl new file mode 100644 index 0000000000..ddfb430d28 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/atom/atom.ttl @@ -0,0 +1,247 @@ +@prefix atom: . +@prefix lv2: . +@prefix owl: . +@prefix rdf: . +@prefix rdfs: . +@prefix ui: . +@prefix units: . +@prefix xsd: . + + + a owl:Ontology ; + rdfs:seeAlso , + , + , + ; + rdfs:label "LV2 Atom" ; + rdfs:comment "A generic value container and several data types." . + +atom:cType + a rdf:Property , + owl:DatatypeProperty , + owl:FunctionalProperty ; + rdfs:label "C type" ; + rdfs:comment "The C type that describes the binary representation of an Atom type." ; + rdfs:domain rdfs:Class ; + rdfs:range lv2:Symbol . + +atom:Atom + a rdfs:Class ; + rdfs:label "Atom" ; + rdfs:comment "Abstract base class for all atoms." ; + atom:cType "LV2_Atom" . + +atom:Chunk + a rdfs:Class , + rdfs:Datatype ; + rdfs:subClassOf atom:Atom ; + rdfs:label "Chunk" ; + rdfs:comment "A chunk of memory with undefined contents." ; + owl:onDatatype xsd:base64Binary . + +atom:Number + a rdfs:Class ; + rdfs:subClassOf atom:Atom ; + rdfs:label "Number" ; + rdfs:comment "Base class for numeric types." . + +atom:Int + a rdfs:Class , + rdfs:Datatype ; + rdfs:subClassOf atom:Number ; + rdfs:label "Int" ; + rdfs:comment "A native `int32_t`." ; + atom:cType "LV2_Atom_Int" ; + owl:onDatatype xsd:int . + +atom:Long + a rdfs:Class , + rdfs:Datatype ; + rdfs:subClassOf atom:Number ; + rdfs:label "Long" ; + rdfs:comment "A native `int64_t`." ; + atom:cType "LV2_Atom_Long" ; + owl:onDatatype xsd:long . + +atom:Float + a rdfs:Class , + rdfs:Datatype ; + rdfs:subClassOf atom:Number ; + rdfs:label "Float" ; + rdfs:comment "A native `float`." ; + atom:cType "LV2_Atom_Float" ; + owl:onDatatype xsd:float . + +atom:Double + a rdfs:Class , + rdfs:Datatype ; + rdfs:subClassOf atom:Number ; + rdfs:label "Double" ; + rdfs:comment "A native `double`." ; + atom:cType "LV2_Atom_Double" ; + owl:onDatatype xsd:double . + +atom:Bool + a rdfs:Class , + rdfs:Datatype ; + rdfs:subClassOf atom:Atom ; + rdfs:label "Bool" ; + rdfs:comment "An atom:Int where 0 is false and any other value is true." ; + atom:cType "LV2_Atom_Bool" ; + owl:onDatatype xsd:boolean . + +atom:String + a rdfs:Class , + rdfs:Datatype ; + rdfs:subClassOf atom:Atom ; + rdfs:label "String" ; + rdfs:comment "A UTF-8 string." ; + atom:cType "LV2_Atom_String" ; + owl:onDatatype xsd:string . + +atom:Literal + a rdfs:Class ; + rdfs:subClassOf atom:Atom ; + rdfs:label "Literal" ; + rdfs:comment "A UTF-8 string literal with optional datatype or language." ; + atom:cType "LV2_Atom_Literal" . + +atom:Path + a rdfs:Class , + rdfs:Datatype ; + rdfs:subClassOf atom:URI ; + owl:onDatatype atom:URI ; + rdfs:label "Path" ; + rdfs:comment "A local file path." . + +atom:URI + a rdfs:Class , + rdfs:Datatype ; + rdfs:subClassOf atom:String ; + owl:onDatatype xsd:anyURI ; + rdfs:label "URI" ; + rdfs:comment "A URI string." . + +atom:URID + a rdfs:Class ; + rdfs:subClassOf atom:Atom ; + rdfs:label "URID" ; + rdfs:comment "An unsigned 32-bit integer ID for a URI." ; + atom:cType "LV2_Atom_URID" . + +atom:Vector + a rdfs:Class ; + rdfs:subClassOf atom:Atom ; + rdfs:label "Vector" ; + rdfs:comment "A homogeneous sequence of atom bodies with equivalent type and size." ; + atom:cType "LV2_Atom_Vector" . + +atom:Tuple + a rdfs:Class ; + rdfs:subClassOf atom:Atom ; + rdfs:label "Tuple" ; + rdfs:comment "A sequence of atoms with varying type and size." . + +atom:Property + a rdfs:Class ; + rdfs:subClassOf atom:Atom ; + rdfs:label "Property" ; + rdfs:comment "A property of an atom:Object." ; + atom:cType "LV2_Atom_Property" . + +atom:Object + a rdfs:Class ; + rdfs:subClassOf atom:Atom ; + rdfs:label "Object" ; + rdfs:comment "A collection of properties." ; + atom:cType "LV2_Atom_Object" . + +atom:Resource + a rdfs:Class ; + rdfs:subClassOf atom:Object ; + rdfs:label "Resource" ; + rdfs:comment "A named collection of properties with a URI." ; + owl:deprecated "true"^^xsd:boolean ; + atom:cType "LV2_Atom_Object" . + +atom:Blank + a rdfs:Class ; + rdfs:subClassOf atom:Object ; + rdfs:label "Blank" ; + rdfs:comment "An anonymous collection of properties without a URI." ; + owl:deprecated "true"^^xsd:boolean ; + atom:cType "LV2_Atom_Object" . + +atom:Sound + a rdfs:Class ; + rdfs:subClassOf atom:Vector ; + rdfs:label "Sound" ; + rdfs:comment "A atom:Vector of atom:Float which represents an audio waveform." ; + atom:cType "LV2_Atom_Vector" . + +atom:frameTime + a rdf:Property , + owl:DatatypeProperty , + owl:FunctionalProperty ; + rdfs:range xsd:decimal ; + rdfs:label "frame time" ; + rdfs:comment "A time stamp in audio frames." . + +atom:beatTime + a rdf:Property , + owl:DatatypeProperty , + owl:FunctionalProperty ; + rdfs:range xsd:decimal ; + rdfs:label "beat time" ; + rdfs:comment "A time stamp in beats." . + +atom:Event + a rdfs:Class ; + rdfs:label "Event" ; + atom:cType "LV2_Atom_Event" ; + rdfs:comment "An atom with a time stamp prefix in a sequence." . + +atom:Sequence + a rdfs:Class ; + rdfs:subClassOf atom:Atom ; + rdfs:label "Sequence" ; + atom:cType "LV2_Atom_Sequence" ; + rdfs:comment "A sequence of events." . + +atom:AtomPort + a rdfs:Class ; + rdfs:subClassOf lv2:Port ; + rdfs:label "Atom Port" ; + rdfs:comment "A port which contains an atom:Atom." . + +atom:bufferType + a rdf:Property , + owl:ObjectProperty ; + rdfs:domain atom:AtomPort ; + rdfs:range rdfs:Class ; + rdfs:label "buffer type" ; + rdfs:comment "An atom type that a port may be connected to." . + +atom:childType + a rdf:Property , + owl:ObjectProperty ; + rdfs:label "child type" ; + rdfs:comment "The type of children in a container." . + +atom:supports + a rdf:Property , + owl:ObjectProperty ; + rdfs:label "supports" ; + rdfs:comment "A supported atom type." ; + rdfs:range rdfs:Class . + +atom:eventTransfer + a ui:PortProtocol ; + rdfs:label "event transfer" ; + rdfs:comment "A port protocol for transferring events." . + +atom:atomTransfer + a ui:PortProtocol ; + rdfs:label "atom transfer" ; + rdfs:comment "A port protocol for transferring atoms." . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/atom/forge-overflow-test.c b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/atom/forge-overflow-test.c new file mode 100644 index 0000000000..5741453928 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/atom/forge-overflow-test.c @@ -0,0 +1,235 @@ +/* + Copyright 2019 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "lv2/atom/atom-test-utils.c" +#include "lv2/atom/atom.h" +#include "lv2/atom/forge.h" +#include "lv2/urid/urid.h" + +#include +#include +#include + +static int +test_string_overflow(void) +{ +#define MAX_CHARS 15 + + static const size_t capacity = sizeof(LV2_Atom_String) + MAX_CHARS + 1; + static const char* str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + uint8_t* buf = (uint8_t*)malloc(capacity); + LV2_URID_Map map = {NULL, urid_map}; + LV2_Atom_Forge forge; + lv2_atom_forge_init(&forge, &map); + + // Check that writing increasingly long strings fails at the right point + for (size_t count = 0; count < MAX_CHARS; ++count) { + lv2_atom_forge_set_buffer(&forge, buf, capacity); + + const LV2_Atom_Forge_Ref ref = lv2_atom_forge_string(&forge, str, count); + if (!ref) { + return test_fail("Failed to write %zu byte string\n", count); + } + } + + // Failure writing to an exactly full forge + if (lv2_atom_forge_string(&forge, str, MAX_CHARS + 1)) { + return test_fail("Successfully wrote past end of buffer\n"); + } + + // Failure writing body after successfully writing header + lv2_atom_forge_set_buffer(&forge, buf, sizeof(LV2_Atom) + 1); + if (lv2_atom_forge_string(&forge, "AB", 2)) { + return test_fail("Successfully wrote atom header past end\n"); + } + + free(buf); + return 0; +} + +static int +test_literal_overflow(void) +{ + static const size_t capacity = sizeof(LV2_Atom_Literal) + 2; + + uint8_t* buf = (uint8_t*)malloc(capacity); + LV2_URID_Map map = {NULL, urid_map}; + LV2_Atom_Forge forge; + lv2_atom_forge_init(&forge, &map); + + // Failure in atom header + lv2_atom_forge_set_buffer(&forge, buf, 1); + if (lv2_atom_forge_literal(&forge, "A", 1, 0, 0)) { + return test_fail("Successfully wrote atom header past end\n"); + } + + // Failure in literal header + lv2_atom_forge_set_buffer(&forge, buf, sizeof(LV2_Atom) + 1); + if (lv2_atom_forge_literal(&forge, "A", 1, 0, 0)) { + return test_fail("Successfully wrote literal header past end\n"); + } + + // Success (only room for one character + null terminator) + lv2_atom_forge_set_buffer(&forge, buf, capacity); + if (!lv2_atom_forge_literal(&forge, "A", 1, 0, 0)) { + return test_fail("Failed to write small enough literal\n"); + } + + // Failure in body + lv2_atom_forge_set_buffer(&forge, buf, capacity); + if (lv2_atom_forge_literal(&forge, "AB", 2, 0, 0)) { + return test_fail("Successfully wrote literal body past end\n"); + } + + free(buf); + return 0; +} + +static int +test_sequence_overflow(void) +{ + static const size_t size = sizeof(LV2_Atom_Sequence) + 6 * sizeof(LV2_Atom); + LV2_URID_Map map = {NULL, urid_map}; + + // Test over a range that fails in the sequence header and event components + for (size_t capacity = 1; capacity < size; ++capacity) { + uint8_t* buf = (uint8_t*)malloc(capacity); + + LV2_Atom_Forge forge; + lv2_atom_forge_init(&forge, &map); + lv2_atom_forge_set_buffer(&forge, buf, capacity); + + LV2_Atom_Forge_Frame frame; + LV2_Atom_Forge_Ref ref = lv2_atom_forge_sequence_head(&forge, &frame, 0); + + assert(capacity >= sizeof(LV2_Atom_Sequence) || !frame.ref); + assert(capacity >= sizeof(LV2_Atom_Sequence) || !ref); + (void)ref; + + lv2_atom_forge_frame_time(&forge, 0); + lv2_atom_forge_int(&forge, 42); + lv2_atom_forge_pop(&forge, &frame); + + free(buf); + } + + return 0; +} + +static int +test_vector_head_overflow(void) +{ + static const size_t size = sizeof(LV2_Atom_Vector) + 3 * sizeof(LV2_Atom); + LV2_URID_Map map = {NULL, urid_map}; + + // Test over a range that fails in the vector header and elements + for (size_t capacity = 1; capacity < size; ++capacity) { + uint8_t* buf = (uint8_t*)malloc(capacity); + + LV2_Atom_Forge forge; + lv2_atom_forge_init(&forge, &map); + lv2_atom_forge_set_buffer(&forge, buf, capacity); + + LV2_Atom_Forge_Frame frame; + LV2_Atom_Forge_Ref ref = + lv2_atom_forge_vector_head(&forge, &frame, sizeof(int32_t), forge.Int); + + assert(capacity >= sizeof(LV2_Atom_Vector) || !frame.ref); + assert(capacity >= sizeof(LV2_Atom_Vector) || !ref); + (void)ref; + + lv2_atom_forge_int(&forge, 1); + lv2_atom_forge_int(&forge, 2); + lv2_atom_forge_int(&forge, 3); + lv2_atom_forge_pop(&forge, &frame); + + free(buf); + } + + return 0; +} + +static int +test_vector_overflow(void) +{ + static const size_t size = sizeof(LV2_Atom_Vector) + 3 * sizeof(LV2_Atom); + static const int32_t vec[] = {1, 2, 3}; + LV2_URID_Map map = {NULL, urid_map}; + + // Test over a range that fails in the vector header and elements + for (size_t capacity = 1; capacity < size; ++capacity) { + uint8_t* buf = (uint8_t*)malloc(capacity); + + LV2_Atom_Forge forge; + lv2_atom_forge_init(&forge, &map); + lv2_atom_forge_set_buffer(&forge, buf, capacity); + + LV2_Atom_Forge_Ref ref = + lv2_atom_forge_vector(&forge, sizeof(int32_t), forge.Int, 3, vec); + + assert(capacity >= sizeof(LV2_Atom_Vector) || !ref); + (void)ref; + + free(buf); + } + + return 0; +} + +static int +test_tuple_overflow(void) +{ + static const size_t size = sizeof(LV2_Atom_Tuple) + 3 * sizeof(LV2_Atom); + LV2_URID_Map map = {NULL, urid_map}; + + // Test over a range that fails in the tuple header and elements + for (size_t capacity = 1; capacity < size; ++capacity) { + uint8_t* buf = (uint8_t*)malloc(capacity); + + LV2_Atom_Forge forge; + lv2_atom_forge_init(&forge, &map); + lv2_atom_forge_set_buffer(&forge, buf, capacity); + + LV2_Atom_Forge_Frame frame; + LV2_Atom_Forge_Ref ref = lv2_atom_forge_tuple(&forge, &frame); + + assert(capacity >= sizeof(LV2_Atom_Tuple) || !frame.ref); + assert(capacity >= sizeof(LV2_Atom_Tuple) || !ref); + (void)ref; + + lv2_atom_forge_int(&forge, 1); + lv2_atom_forge_float(&forge, 2.0f); + lv2_atom_forge_string(&forge, "three", 5); + lv2_atom_forge_pop(&forge, &frame); + + free(buf); + } + + return 0; +} + +int +main(void) +{ + const int ret = test_string_overflow() || test_literal_overflow() || + test_sequence_overflow() || test_vector_head_overflow() || + test_vector_overflow() || test_tuple_overflow(); + + free_urid_map(); + + return ret; +} diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/atom/forge.h b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/atom/forge.h new file mode 100644 index 0000000000..714df7e5db --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/atom/forge.h @@ -0,0 +1,683 @@ +/* + Copyright 2008-2016 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +/** + @file forge.h An API for constructing LV2 atoms. + + This file provides an API for constructing Atoms which makes it relatively + simple to build nested atoms of arbitrary complexity without requiring + dynamic memory allocation. + + The API is based on successively appending the appropriate pieces to build a + complete Atom. The size of containers is automatically updated. Functions + that begin a container return (via their frame argument) a stack frame which + must be popped when the container is finished. + + All output is written to a user-provided buffer or sink function. This + makes it popssible to create create atoms on the stack, on the heap, in LV2 + port buffers, in a ringbuffer, or elsewhere, all using the same API. + + This entire API is realtime safe if used with a buffer or a realtime safe + sink, except lv2_atom_forge_init() which is only realtime safe if the URI + map function is. + + Note these functions are all static inline, do not take their address. + + This header is non-normative, it is provided for convenience. +*/ + +#ifndef LV2_ATOM_FORGE_H +#define LV2_ATOM_FORGE_H + +/** + @defgroup forge Forge + @ingroup atom + + An API for constructing LV2 atoms. + + @{ +*/ + +#include "lv2/atom/atom.h" +#include "lv2/atom/util.h" +#include "lv2/core/attributes.h" +#include "lv2/urid/urid.h" + +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +// Disable deprecation warnings for Blank and Resource +LV2_DISABLE_DEPRECATION_WARNINGS + +/** Handle for LV2_Atom_Forge_Sink. */ +typedef void* LV2_Atom_Forge_Sink_Handle; + +/** A reference to a chunk of written output. */ +typedef intptr_t LV2_Atom_Forge_Ref; + +/** Sink function for writing output. See lv2_atom_forge_set_sink(). */ +typedef LV2_Atom_Forge_Ref (*LV2_Atom_Forge_Sink)( + LV2_Atom_Forge_Sink_Handle handle, + const void* buf, + uint32_t size); + +/** Function for resolving a reference. See lv2_atom_forge_set_sink(). */ +typedef LV2_Atom* (*LV2_Atom_Forge_Deref_Func)( + LV2_Atom_Forge_Sink_Handle handle, + LV2_Atom_Forge_Ref ref); + +/** A stack frame used for keeping track of nested Atom containers. */ +typedef struct LV2_Atom_Forge_Frame { + struct LV2_Atom_Forge_Frame* parent; + LV2_Atom_Forge_Ref ref; +} LV2_Atom_Forge_Frame; + +/** A "forge" for creating atoms by appending to a buffer. */ +typedef struct { + uint8_t* buf; + uint32_t offset; + uint32_t size; + + LV2_Atom_Forge_Sink sink; + LV2_Atom_Forge_Deref_Func deref; + LV2_Atom_Forge_Sink_Handle handle; + + LV2_Atom_Forge_Frame* stack; + + LV2_URID Blank LV2_DEPRECATED; + LV2_URID Bool; + LV2_URID Chunk; + LV2_URID Double; + LV2_URID Float; + LV2_URID Int; + LV2_URID Long; + LV2_URID Literal; + LV2_URID Object; + LV2_URID Path; + LV2_URID Property; + LV2_URID Resource LV2_DEPRECATED; + LV2_URID Sequence; + LV2_URID String; + LV2_URID Tuple; + LV2_URID URI; + LV2_URID URID; + LV2_URID Vector; +} LV2_Atom_Forge; + +static inline void +lv2_atom_forge_set_buffer(LV2_Atom_Forge* forge, uint8_t* buf, size_t size); + +/** + Initialise `forge`. + + URIs will be mapped using `map` and stored, a reference to `map` itself is + not held. +*/ +static inline void +lv2_atom_forge_init(LV2_Atom_Forge* forge, LV2_URID_Map* map) +{ + lv2_atom_forge_set_buffer(forge, NULL, 0); + forge->Blank = map->map(map->handle, LV2_ATOM__Blank); + forge->Bool = map->map(map->handle, LV2_ATOM__Bool); + forge->Chunk = map->map(map->handle, LV2_ATOM__Chunk); + forge->Double = map->map(map->handle, LV2_ATOM__Double); + forge->Float = map->map(map->handle, LV2_ATOM__Float); + forge->Int = map->map(map->handle, LV2_ATOM__Int); + forge->Long = map->map(map->handle, LV2_ATOM__Long); + forge->Literal = map->map(map->handle, LV2_ATOM__Literal); + forge->Object = map->map(map->handle, LV2_ATOM__Object); + forge->Path = map->map(map->handle, LV2_ATOM__Path); + forge->Property = map->map(map->handle, LV2_ATOM__Property); + forge->Resource = map->map(map->handle, LV2_ATOM__Resource); + forge->Sequence = map->map(map->handle, LV2_ATOM__Sequence); + forge->String = map->map(map->handle, LV2_ATOM__String); + forge->Tuple = map->map(map->handle, LV2_ATOM__Tuple); + forge->URI = map->map(map->handle, LV2_ATOM__URI); + forge->URID = map->map(map->handle, LV2_ATOM__URID); + forge->Vector = map->map(map->handle, LV2_ATOM__Vector); +} + +/** Access the Atom pointed to by a reference. */ +static inline LV2_Atom* +lv2_atom_forge_deref(LV2_Atom_Forge* forge, LV2_Atom_Forge_Ref ref) +{ + return forge->buf ? (LV2_Atom*)ref : forge->deref(forge->handle, ref); +} + +/** + @name Object Stack + @{ +*/ + +/** + Push a stack frame. + This is done automatically by container functions (which take a stack frame + pointer), but may be called by the user to push the top level container when + writing to an existing Atom. +*/ +static inline LV2_Atom_Forge_Ref +lv2_atom_forge_push(LV2_Atom_Forge* forge, + LV2_Atom_Forge_Frame* frame, + LV2_Atom_Forge_Ref ref) +{ + frame->parent = forge->stack; + frame->ref = ref; + + if (ref) { + forge->stack = frame; // Don't push, so walking the stack is always safe + } + + return ref; +} + +/** Pop a stack frame. This must be called when a container is finished. */ +static inline void +lv2_atom_forge_pop(LV2_Atom_Forge* forge, LV2_Atom_Forge_Frame* frame) +{ + if (frame->ref) { + // If frame has a valid ref, it must be the top of the stack + assert(frame == forge->stack); + forge->stack = frame->parent; + } + // Otherwise, frame was not pushed because of overflow, do nothing +} + +/** Return true iff the top of the stack has the given type. */ +static inline bool +lv2_atom_forge_top_is(LV2_Atom_Forge* forge, uint32_t type) +{ + return forge->stack && forge->stack->ref && + (lv2_atom_forge_deref(forge, forge->stack->ref)->type == type); +} + +/** Return true iff `type` is an atom:Object. */ +static inline bool +lv2_atom_forge_is_object_type(const LV2_Atom_Forge* forge, uint32_t type) +{ + return (type == forge->Object || type == forge->Blank || + type == forge->Resource); +} + +/** Return true iff `type` is an atom:Object with a blank ID. */ +static inline bool +lv2_atom_forge_is_blank(const LV2_Atom_Forge* forge, + uint32_t type, + const LV2_Atom_Object_Body* body) +{ + return (type == forge->Blank || (type == forge->Object && body->id == 0)); +} + +/** + @} + @name Output Configuration + @{ +*/ + +/** Set the output buffer where `forge` will write atoms. */ +static inline void +lv2_atom_forge_set_buffer(LV2_Atom_Forge* forge, uint8_t* buf, size_t size) +{ + forge->buf = buf; + forge->size = (uint32_t)size; + forge->offset = 0; + forge->deref = NULL; + forge->sink = NULL; + forge->handle = NULL; + forge->stack = NULL; +} + +/** + Set the sink function where `forge` will write output. + + The return value of forge functions is an LV2_Atom_Forge_Ref which is an + integer type safe to use as a pointer but is otherwise opaque. The sink + function must return a ref that can be dereferenced to access as least + sizeof(LV2_Atom) bytes of the written data, so sizes can be updated. For + ringbuffers, this should be possible as long as the size of the buffer is a + multiple of sizeof(LV2_Atom), since atoms are always aligned. + + Note that 0 is an invalid reference, so if you are using a buffer offset be + sure to offset it such that 0 is never a valid reference. You will get + confusing errors otherwise. +*/ +static inline void +lv2_atom_forge_set_sink(LV2_Atom_Forge* forge, + LV2_Atom_Forge_Sink sink, + LV2_Atom_Forge_Deref_Func deref, + LV2_Atom_Forge_Sink_Handle handle) +{ + forge->buf = NULL; + forge->size = forge->offset = 0; + forge->deref = deref; + forge->sink = sink; + forge->handle = handle; + forge->stack = NULL; +} + +/** + @} + @name Low Level Output + @{ +*/ + +/** + Write raw output. This is used internally, but is also useful for writing + atom types not explicitly supported by the forge API. Note the caller is + responsible for ensuring the output is approriately padded. +*/ +static inline LV2_Atom_Forge_Ref +lv2_atom_forge_raw(LV2_Atom_Forge* forge, const void* data, uint32_t size) +{ + LV2_Atom_Forge_Ref out = 0; + if (forge->sink) { + out = forge->sink(forge->handle, data, size); + } else { + out = (LV2_Atom_Forge_Ref)forge->buf + forge->offset; + uint8_t* mem = forge->buf + forge->offset; + if (forge->offset + size > forge->size) { + return 0; + } + forge->offset += size; + memcpy(mem, data, size); + } + for (LV2_Atom_Forge_Frame* f = forge->stack; f; f = f->parent) { + lv2_atom_forge_deref(forge, f->ref)->size += size; + } + return out; +} + +/** Pad output accordingly so next write is 64-bit aligned. */ +static inline void +lv2_atom_forge_pad(LV2_Atom_Forge* forge, uint32_t written) +{ + const uint64_t pad = 0; + const uint32_t pad_size = lv2_atom_pad_size(written) - written; + lv2_atom_forge_raw(forge, &pad, pad_size); +} + +/** Write raw output, padding to 64-bits as necessary. */ +static inline LV2_Atom_Forge_Ref +lv2_atom_forge_write(LV2_Atom_Forge* forge, const void* data, uint32_t size) +{ + LV2_Atom_Forge_Ref out = lv2_atom_forge_raw(forge, data, size); + if (out) { + lv2_atom_forge_pad(forge, size); + } + return out; +} + +/** Write a null-terminated string body. */ +static inline LV2_Atom_Forge_Ref +lv2_atom_forge_string_body(LV2_Atom_Forge* forge, const char* str, uint32_t len) +{ + LV2_Atom_Forge_Ref out = lv2_atom_forge_raw(forge, str, len); + if (out && (out = lv2_atom_forge_raw(forge, "", 1))) { + lv2_atom_forge_pad(forge, len + 1); + } + return out; +} + +/** + @} + @name Atom Output + @{ +*/ + +/** Write an atom:Atom header. */ +static inline LV2_Atom_Forge_Ref +lv2_atom_forge_atom(LV2_Atom_Forge* forge, uint32_t size, uint32_t type) +{ + const LV2_Atom a = {size, type}; + return lv2_atom_forge_raw(forge, &a, sizeof(a)); +} + +/** Write a primitive (fixed-size) atom. */ +static inline LV2_Atom_Forge_Ref +lv2_atom_forge_primitive(LV2_Atom_Forge* forge, const LV2_Atom* a) +{ + return ( + lv2_atom_forge_top_is(forge, forge->Vector) + ? lv2_atom_forge_raw(forge, LV2_ATOM_BODY_CONST(a), a->size) + : lv2_atom_forge_write(forge, a, (uint32_t)sizeof(LV2_Atom) + a->size)); +} + +/** Write an atom:Int. */ +static inline LV2_Atom_Forge_Ref +lv2_atom_forge_int(LV2_Atom_Forge* forge, int32_t val) +{ + const LV2_Atom_Int a = {{sizeof(val), forge->Int}, val}; + return lv2_atom_forge_primitive(forge, &a.atom); +} + +/** Write an atom:Long. */ +static inline LV2_Atom_Forge_Ref +lv2_atom_forge_long(LV2_Atom_Forge* forge, int64_t val) +{ + const LV2_Atom_Long a = {{sizeof(val), forge->Long}, val}; + return lv2_atom_forge_primitive(forge, &a.atom); +} + +/** Write an atom:Float. */ +static inline LV2_Atom_Forge_Ref +lv2_atom_forge_float(LV2_Atom_Forge* forge, float val) +{ + const LV2_Atom_Float a = {{sizeof(val), forge->Float}, val}; + return lv2_atom_forge_primitive(forge, &a.atom); +} + +/** Write an atom:Double. */ +static inline LV2_Atom_Forge_Ref +lv2_atom_forge_double(LV2_Atom_Forge* forge, double val) +{ + const LV2_Atom_Double a = {{sizeof(val), forge->Double}, val}; + return lv2_atom_forge_primitive(forge, &a.atom); +} + +/** Write an atom:Bool. */ +static inline LV2_Atom_Forge_Ref +lv2_atom_forge_bool(LV2_Atom_Forge* forge, bool val) +{ + const LV2_Atom_Bool a = {{sizeof(int32_t), forge->Bool}, val ? 1 : 0}; + return lv2_atom_forge_primitive(forge, &a.atom); +} + +/** Write an atom:URID. */ +static inline LV2_Atom_Forge_Ref +lv2_atom_forge_urid(LV2_Atom_Forge* forge, LV2_URID id) +{ + const LV2_Atom_URID a = {{sizeof(id), forge->URID}, id}; + return lv2_atom_forge_primitive(forge, &a.atom); +} + +/** Write an atom compatible with atom:String. Used internally. */ +static inline LV2_Atom_Forge_Ref +lv2_atom_forge_typed_string(LV2_Atom_Forge* forge, + uint32_t type, + const char* str, + uint32_t len) +{ + const LV2_Atom_String a = {{len + 1, type}}; + LV2_Atom_Forge_Ref out = lv2_atom_forge_raw(forge, &a, sizeof(a)); + if (out) { + if (!lv2_atom_forge_string_body(forge, str, len)) { + LV2_Atom* atom = lv2_atom_forge_deref(forge, out); + atom->size = atom->type = 0; + out = 0; + } + } + return out; +} + +/** Write an atom:String. Note that `str` need not be NULL terminated. */ +static inline LV2_Atom_Forge_Ref +lv2_atom_forge_string(LV2_Atom_Forge* forge, const char* str, uint32_t len) +{ + return lv2_atom_forge_typed_string(forge, forge->String, str, len); +} + +/** + Write an atom:URI. Note that `uri` need not be NULL terminated. + This does not map the URI, but writes the complete URI string. To write + a mapped URI, use lv2_atom_forge_urid(). +*/ +static inline LV2_Atom_Forge_Ref +lv2_atom_forge_uri(LV2_Atom_Forge* forge, const char* uri, uint32_t len) +{ + return lv2_atom_forge_typed_string(forge, forge->URI, uri, len); +} + +/** Write an atom:Path. Note that `path` need not be NULL terminated. */ +static inline LV2_Atom_Forge_Ref +lv2_atom_forge_path(LV2_Atom_Forge* forge, const char* path, uint32_t len) +{ + return lv2_atom_forge_typed_string(forge, forge->Path, path, len); +} + +/** Write an atom:Literal. */ +static inline LV2_Atom_Forge_Ref +lv2_atom_forge_literal(LV2_Atom_Forge* forge, + const char* str, + uint32_t len, + uint32_t datatype, + uint32_t lang) +{ + const LV2_Atom_Literal a = { + {(uint32_t)(sizeof(LV2_Atom_Literal) - sizeof(LV2_Atom) + len + 1), + forge->Literal}, + {datatype, lang}}; + LV2_Atom_Forge_Ref out = lv2_atom_forge_raw(forge, &a, sizeof(a)); + if (out) { + if (!lv2_atom_forge_string_body(forge, str, len)) { + LV2_Atom* atom = lv2_atom_forge_deref(forge, out); + atom->size = atom->type = 0; + out = 0; + } + } + return out; +} + +/** Start an atom:Vector. */ +static inline LV2_Atom_Forge_Ref +lv2_atom_forge_vector_head(LV2_Atom_Forge* forge, + LV2_Atom_Forge_Frame* frame, + uint32_t child_size, + uint32_t child_type) +{ + const LV2_Atom_Vector a = {{sizeof(LV2_Atom_Vector_Body), forge->Vector}, + {child_size, child_type}}; + return lv2_atom_forge_push( + forge, frame, lv2_atom_forge_write(forge, &a, sizeof(a))); +} + +/** Write a complete atom:Vector. */ +static inline LV2_Atom_Forge_Ref +lv2_atom_forge_vector(LV2_Atom_Forge* forge, + uint32_t child_size, + uint32_t child_type, + uint32_t n_elems, + const void* elems) +{ + const LV2_Atom_Vector a = { + {(uint32_t)(sizeof(LV2_Atom_Vector_Body) + n_elems * child_size), + forge->Vector}, + {child_size, child_type}}; + LV2_Atom_Forge_Ref out = lv2_atom_forge_write(forge, &a, sizeof(a)); + if (out) { + lv2_atom_forge_write(forge, elems, child_size * n_elems); + } + return out; +} + +/** + Write the header of an atom:Tuple. + + The passed frame will be initialised to represent this tuple. To complete + the tuple, write a sequence of atoms, then pop the frame with + lv2_atom_forge_pop(). + + For example: + @code + // Write tuple (1, 2.0) + LV2_Atom_Forge_Frame frame; + LV2_Atom* tup = (LV2_Atom*)lv2_atom_forge_tuple(forge, &frame); + lv2_atom_forge_int(forge, 1); + lv2_atom_forge_float(forge, 2.0); + lv2_atom_forge_pop(forge, &frame); + @endcode +*/ +static inline LV2_Atom_Forge_Ref +lv2_atom_forge_tuple(LV2_Atom_Forge* forge, LV2_Atom_Forge_Frame* frame) +{ + const LV2_Atom_Tuple a = {{0, forge->Tuple}}; + return lv2_atom_forge_push( + forge, frame, lv2_atom_forge_write(forge, &a, sizeof(a))); +} + +/** + Write the header of an atom:Object. + + The passed frame will be initialised to represent this object. To complete + the object, write a sequence of properties, then pop the frame with + lv2_atom_forge_pop(). + + For example: + @code + LV2_URID eg_Cat = map("http://example.org/Cat"); + LV2_URID eg_name = map("http://example.org/name"); + + // Start object with type eg_Cat and blank ID + LV2_Atom_Forge_Frame frame; + lv2_atom_forge_object(forge, &frame, 0, eg_Cat); + + // Append property eg:name = "Hobbes" + lv2_atom_forge_key(forge, eg_name); + lv2_atom_forge_string(forge, "Hobbes", strlen("Hobbes")); + + // Finish object + lv2_atom_forge_pop(forge, &frame); + @endcode +*/ +static inline LV2_Atom_Forge_Ref +lv2_atom_forge_object(LV2_Atom_Forge* forge, + LV2_Atom_Forge_Frame* frame, + LV2_URID id, + LV2_URID otype) +{ + const LV2_Atom_Object a = { + {(uint32_t)sizeof(LV2_Atom_Object_Body), forge->Object}, {id, otype}}; + return lv2_atom_forge_push( + forge, frame, lv2_atom_forge_write(forge, &a, sizeof(a))); +} + +/** + The same as lv2_atom_forge_object(), but for object:Resource. + + This function is deprecated and should not be used in new code. + Use lv2_atom_forge_object() directly instead. +*/ +LV2_DEPRECATED +static inline LV2_Atom_Forge_Ref +lv2_atom_forge_resource(LV2_Atom_Forge* forge, + LV2_Atom_Forge_Frame* frame, + LV2_URID id, + LV2_URID otype) +{ + const LV2_Atom_Object a = { + {(uint32_t)sizeof(LV2_Atom_Object_Body), forge->Resource}, {id, otype}}; + return lv2_atom_forge_push( + forge, frame, lv2_atom_forge_write(forge, &a, sizeof(a))); +} + +/** + The same as lv2_atom_forge_object(), but for object:Blank. + + This function is deprecated and should not be used in new code. + Use lv2_atom_forge_object() directly instead. +*/ +LV2_DEPRECATED +static inline LV2_Atom_Forge_Ref +lv2_atom_forge_blank(LV2_Atom_Forge* forge, + LV2_Atom_Forge_Frame* frame, + uint32_t id, + LV2_URID otype) +{ + const LV2_Atom_Object a = { + {(uint32_t)sizeof(LV2_Atom_Object_Body), forge->Blank}, {id, otype}}; + return lv2_atom_forge_push( + forge, frame, lv2_atom_forge_write(forge, &a, sizeof(a))); +} + +/** + Write a property key in an Object, to be followed by the value. + + See lv2_atom_forge_object() documentation for an example. +*/ +static inline LV2_Atom_Forge_Ref +lv2_atom_forge_key(LV2_Atom_Forge* forge, LV2_URID key) +{ + const LV2_Atom_Property_Body a = {key, 0, {0, 0}}; + return lv2_atom_forge_write(forge, &a, 2 * (uint32_t)sizeof(uint32_t)); +} + +/** + Write the header for a property body in an object, with context. + + If you do not need the context, which is almost certainly the case, + use the simpler lv2_atom_forge_key() instead. +*/ +static inline LV2_Atom_Forge_Ref +lv2_atom_forge_property_head(LV2_Atom_Forge* forge, + LV2_URID key, + LV2_URID context) +{ + const LV2_Atom_Property_Body a = {key, context, {0, 0}}; + return lv2_atom_forge_write(forge, &a, 2 * (uint32_t)sizeof(uint32_t)); +} + +/** + Write the header for a Sequence. +*/ +static inline LV2_Atom_Forge_Ref +lv2_atom_forge_sequence_head(LV2_Atom_Forge* forge, + LV2_Atom_Forge_Frame* frame, + uint32_t unit) +{ + const LV2_Atom_Sequence a = { + {(uint32_t)sizeof(LV2_Atom_Sequence_Body), forge->Sequence}, {unit, 0}}; + return lv2_atom_forge_push( + forge, frame, lv2_atom_forge_write(forge, &a, sizeof(a))); +} + +/** + Write the time stamp header of an Event (in a Sequence) in audio frames. + After this, call the appropriate forge method(s) to write the body. Note + the returned reference is to an LV2_Event which is NOT an Atom. +*/ +static inline LV2_Atom_Forge_Ref +lv2_atom_forge_frame_time(LV2_Atom_Forge* forge, int64_t frames) +{ + return lv2_atom_forge_write(forge, &frames, sizeof(frames)); +} + +/** + Write the time stamp header of an Event (in a Sequence) in beats. After + this, call the appropriate forge method(s) to write the body. Note the + returned reference is to an LV2_Event which is NOT an Atom. +*/ +static inline LV2_Atom_Forge_Ref +lv2_atom_forge_beat_time(LV2_Atom_Forge* forge, double beats) +{ + return lv2_atom_forge_write(forge, &beats, sizeof(beats)); +} + +LV2_RESTORE_WARNINGS + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +/** + @} + @} +*/ + +#endif /* LV2_ATOM_FORGE_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/atom/manifest.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/atom/manifest.ttl new file mode 100644 index 0000000000..46d6de5a09 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/atom/manifest.ttl @@ -0,0 +1,9 @@ +@prefix lv2: . +@prefix rdfs: . + + + a lv2:Specification ; + lv2:minorVersion 2 ; + lv2:microVersion 2 ; + rdfs:seeAlso . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/atom/util.h b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/atom/util.h new file mode 100644 index 0000000000..f4612e94e0 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/atom/util.h @@ -0,0 +1,523 @@ +/* + Copyright 2008-2015 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef LV2_ATOM_UTIL_H +#define LV2_ATOM_UTIL_H + +/** + @file util.h Helper functions for the LV2 Atom extension. + + Note these functions are all static inline, do not take their address. + + This header is non-normative, it is provided for convenience. +*/ + +/** + @defgroup util Utilities + @ingroup atom + + Utilities for working with atoms. + + @{ +*/ + +#include "lv2/atom/atom.h" + +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** Pad a size to 64 bits. */ +static inline uint32_t +lv2_atom_pad_size(uint32_t size) +{ + return (size + 7U) & (~7U); +} + +/** Return the total size of `atom`, including the header. */ +static inline uint32_t +lv2_atom_total_size(const LV2_Atom* atom) +{ + return (uint32_t)sizeof(LV2_Atom) + atom->size; +} + +/** Return true iff `atom` is null. */ +static inline bool +lv2_atom_is_null(const LV2_Atom* atom) +{ + return !atom || (atom->type == 0 && atom->size == 0); +} + +/** Return true iff `a` is equal to `b`. */ +static inline bool +lv2_atom_equals(const LV2_Atom* a, const LV2_Atom* b) +{ + return (a == b) || ((a->type == b->type) && (a->size == b->size) && + !memcmp(a + 1, b + 1, a->size)); +} + +/** + @name Sequence Iterator + @{ +*/ + +/** Get an iterator pointing to the first event in a Sequence body. */ +static inline LV2_Atom_Event* +lv2_atom_sequence_begin(const LV2_Atom_Sequence_Body* body) +{ + return (LV2_Atom_Event*)(body + 1); +} + +/** Get an iterator pointing to the end of a Sequence body. */ +static inline LV2_Atom_Event* +lv2_atom_sequence_end(const LV2_Atom_Sequence_Body* body, uint32_t size) +{ + return (LV2_Atom_Event*)((const uint8_t*)body + lv2_atom_pad_size(size)); +} + +/** Return true iff `i` has reached the end of `body`. */ +static inline bool +lv2_atom_sequence_is_end(const LV2_Atom_Sequence_Body* body, + uint32_t size, + const LV2_Atom_Event* i) +{ + return (const uint8_t*)i >= ((const uint8_t*)body + size); +} + +/** Return an iterator to the element following `i`. */ +static inline LV2_Atom_Event* +lv2_atom_sequence_next(const LV2_Atom_Event* i) +{ + return (LV2_Atom_Event*)((const uint8_t*)i + sizeof(LV2_Atom_Event) + + lv2_atom_pad_size(i->body.size)); +} + +/** + A macro for iterating over all events in a Sequence. + @param seq The sequence to iterate over + @param iter The name of the iterator + + This macro is used similarly to a for loop (which it expands to), for + example: + + @code + LV2_ATOM_SEQUENCE_FOREACH(sequence, ev) { + // Do something with ev (an LV2_Atom_Event*) here... + } + @endcode +*/ +#define LV2_ATOM_SEQUENCE_FOREACH(seq, iter) \ + for (LV2_Atom_Event * (iter) = lv2_atom_sequence_begin(&(seq)->body); \ + !lv2_atom_sequence_is_end(&(seq)->body, (seq)->atom.size, (iter)); \ + (iter) = lv2_atom_sequence_next(iter)) + +/** Like LV2_ATOM_SEQUENCE_FOREACH but for a headerless sequence body. */ +#define LV2_ATOM_SEQUENCE_BODY_FOREACH(body, size, iter) \ + for (LV2_Atom_Event * (iter) = lv2_atom_sequence_begin(body); \ + !lv2_atom_sequence_is_end(body, size, (iter)); \ + (iter) = lv2_atom_sequence_next(iter)) + +/** + @} + @name Sequence Utilities + @{ +*/ + +/** + Clear all events from `sequence`. + + This simply resets the size field, the other fields are left untouched. +*/ +static inline void +lv2_atom_sequence_clear(LV2_Atom_Sequence* seq) +{ + seq->atom.size = sizeof(LV2_Atom_Sequence_Body); +} + +/** + Append an event at the end of `sequence`. + + @param seq Sequence to append to. + @param capacity Total capacity of the sequence atom + (as set by the host for sequence output ports). + @param event Event to write. + + @return A pointer to the newly written event in `seq`, + or NULL on failure (insufficient space). +*/ +static inline LV2_Atom_Event* +lv2_atom_sequence_append_event(LV2_Atom_Sequence* seq, + uint32_t capacity, + const LV2_Atom_Event* event) +{ + const uint32_t total_size = (uint32_t)sizeof(*event) + event->body.size; + if (capacity - seq->atom.size < total_size) { + return NULL; + } + + LV2_Atom_Event* e = lv2_atom_sequence_end(&seq->body, seq->atom.size); + memcpy(e, event, total_size); + + seq->atom.size += lv2_atom_pad_size(total_size); + + return e; +} + +/** + @} + @name Tuple Iterator + @{ +*/ + +/** Get an iterator pointing to the first element in `tup`. */ +static inline LV2_Atom* +lv2_atom_tuple_begin(const LV2_Atom_Tuple* tup) +{ + return (LV2_Atom*)(LV2_ATOM_BODY(tup)); +} + +/** Return true iff `i` has reached the end of `body`. */ +static inline bool +lv2_atom_tuple_is_end(const void* body, uint32_t size, const LV2_Atom* i) +{ + return (const uint8_t*)i >= ((const uint8_t*)body + size); +} + +/** Return an iterator to the element following `i`. */ +static inline LV2_Atom* +lv2_atom_tuple_next(const LV2_Atom* i) +{ + return (LV2_Atom*)((const uint8_t*)i + sizeof(LV2_Atom) + + lv2_atom_pad_size(i->size)); +} + +/** + A macro for iterating over all properties of a Tuple. + @param tuple The tuple to iterate over + @param iter The name of the iterator + + This macro is used similarly to a for loop (which it expands to), for + example: + + @code + LV2_ATOM_TUPLE_FOREACH(tuple, elem) { + // Do something with elem (an LV2_Atom*) here... + } + @endcode +*/ +#define LV2_ATOM_TUPLE_FOREACH(tuple, iter) \ + for (LV2_Atom * (iter) = lv2_atom_tuple_begin(tuple); \ + !lv2_atom_tuple_is_end( \ + LV2_ATOM_BODY(tuple), (tuple)->atom.size, (iter)); \ + (iter) = lv2_atom_tuple_next(iter)) + +/** Like LV2_ATOM_TUPLE_FOREACH but for a headerless tuple body. */ +#define LV2_ATOM_TUPLE_BODY_FOREACH(body, size, iter) \ + for (LV2_Atom * (iter) = (LV2_Atom*)(body); \ + !lv2_atom_tuple_is_end(body, size, (iter)); \ + (iter) = lv2_atom_tuple_next(iter)) + +/** + @} + @name Object Iterator + @{ +*/ + +/** Return a pointer to the first property in `body`. */ +static inline LV2_Atom_Property_Body* +lv2_atom_object_begin(const LV2_Atom_Object_Body* body) +{ + return (LV2_Atom_Property_Body*)(body + 1); +} + +/** Return true iff `i` has reached the end of `obj`. */ +static inline bool +lv2_atom_object_is_end(const LV2_Atom_Object_Body* body, + uint32_t size, + const LV2_Atom_Property_Body* i) +{ + return (const uint8_t*)i >= ((const uint8_t*)body + size); +} + +/** Return an iterator to the property following `i`. */ +static inline LV2_Atom_Property_Body* +lv2_atom_object_next(const LV2_Atom_Property_Body* i) +{ + const LV2_Atom* const value = + (const LV2_Atom*)((const uint8_t*)i + 2 * sizeof(uint32_t)); + return (LV2_Atom_Property_Body*)((const uint8_t*)i + + lv2_atom_pad_size( + (uint32_t)sizeof(LV2_Atom_Property_Body) + + value->size)); +} + +/** + A macro for iterating over all properties of an Object. + @param obj The object to iterate over + @param iter The name of the iterator + + This macro is used similarly to a for loop (which it expands to), for + example: + + @code + LV2_ATOM_OBJECT_FOREACH(object, i) { + // Do something with i (an LV2_Atom_Property_Body*) here... + } + @endcode +*/ +#define LV2_ATOM_OBJECT_FOREACH(obj, iter) \ + for (LV2_Atom_Property_Body * (iter) = lv2_atom_object_begin(&(obj)->body); \ + !lv2_atom_object_is_end(&(obj)->body, (obj)->atom.size, (iter)); \ + (iter) = lv2_atom_object_next(iter)) + +/** Like LV2_ATOM_OBJECT_FOREACH but for a headerless object body. */ +#define LV2_ATOM_OBJECT_BODY_FOREACH(body, size, iter) \ + for (LV2_Atom_Property_Body * (iter) = lv2_atom_object_begin(body); \ + !lv2_atom_object_is_end(body, size, (iter)); \ + (iter) = lv2_atom_object_next(iter)) + +/** + @} + @name Object Query + @{ +*/ + +/** A single entry in an Object query. */ +typedef struct { + uint32_t key; /**< Key to query (input set by user) */ + const LV2_Atom** value; /**< Found value (output set by query function) */ +} LV2_Atom_Object_Query; + +/** Sentinel for lv2_atom_object_query(). */ +static const LV2_Atom_Object_Query LV2_ATOM_OBJECT_QUERY_END = {0, NULL}; + +/** + Get an object's values for various keys. + + The value pointer of each item in `query` will be set to the location of + the corresponding value in `object`. Every value pointer in `query` MUST + be initialised to NULL. This function reads `object` in a single linear + sweep. By allocating `query` on the stack, objects can be "queried" + quickly without allocating any memory. This function is realtime safe. + + This function can only do "flat" queries, it is not smart enough to match + variables in nested objects. + + For example: + @code + const LV2_Atom* name = NULL; + const LV2_Atom* age = NULL; + LV2_Atom_Object_Query q[] = { + { urids.eg_name, &name }, + { urids.eg_age, &age }, + LV2_ATOM_OBJECT_QUERY_END + }; + lv2_atom_object_query(obj, q); + // name and age are now set to the appropriate values in obj, or NULL. + @endcode +*/ +static inline int +lv2_atom_object_query(const LV2_Atom_Object* object, + LV2_Atom_Object_Query* query) +{ + int matches = 0; + int n_queries = 0; + + /* Count number of query keys so we can short-circuit when done */ + for (LV2_Atom_Object_Query* q = query; q->key; ++q) { + ++n_queries; + } + + LV2_ATOM_OBJECT_FOREACH (object, prop) { + for (LV2_Atom_Object_Query* q = query; q->key; ++q) { + if (q->key == prop->key && !*q->value) { + *q->value = &prop->value; + if (++matches == n_queries) { + return matches; + } + break; + } + } + } + return matches; +} + +/** + Body only version of lv2_atom_object_get(). +*/ +static inline int +lv2_atom_object_body_get(uint32_t size, const LV2_Atom_Object_Body* body, ...) +{ + int matches = 0; + int n_queries = 0; + + /* Count number of keys so we can short-circuit when done */ + va_list args; + va_start(args, body); + for (n_queries = 0; va_arg(args, uint32_t); ++n_queries) { + if (!va_arg(args, const LV2_Atom**)) { + va_end(args); + return -1; + } + } + va_end(args); + + LV2_ATOM_OBJECT_BODY_FOREACH (body, size, prop) { + va_start(args, body); + for (int i = 0; i < n_queries; ++i) { + uint32_t qkey = va_arg(args, uint32_t); + const LV2_Atom** qval = va_arg(args, const LV2_Atom**); + if (qkey == prop->key && !*qval) { + *qval = &prop->value; + if (++matches == n_queries) { + va_end(args); + return matches; + } + break; + } + } + va_end(args); + } + return matches; +} + +/** + Variable argument version of lv2_atom_object_query(). + + This is nicer-looking in code, but a bit more error-prone since it is not + type safe and the argument list must be terminated. + + The arguments should be a series of uint32_t key and const LV2_Atom** value + pairs, terminated by a zero key. The value pointers MUST be initialized to + NULL. For example: + + @code + const LV2_Atom* name = NULL; + const LV2_Atom* age = NULL; + lv2_atom_object_get(obj, + uris.name_key, &name, + uris.age_key, &age, + 0); + @endcode +*/ +static inline int +lv2_atom_object_get(const LV2_Atom_Object* object, ...) +{ + int matches = 0; + int n_queries = 0; + + /* Count number of keys so we can short-circuit when done */ + va_list args; + va_start(args, object); + for (n_queries = 0; va_arg(args, uint32_t); ++n_queries) { + if (!va_arg(args, const LV2_Atom**)) { + va_end(args); + return -1; + } + } + va_end(args); + + LV2_ATOM_OBJECT_FOREACH (object, prop) { + va_start(args, object); + for (int i = 0; i < n_queries; ++i) { + uint32_t qkey = va_arg(args, uint32_t); + const LV2_Atom** qval = va_arg(args, const LV2_Atom**); + if (qkey == prop->key && !*qval) { + *qval = &prop->value; + if (++matches == n_queries) { + va_end(args); + return matches; + } + break; + } + } + va_end(args); + } + return matches; +} + +/** + Variable argument version of lv2_atom_object_query() with types. + + This is like lv2_atom_object_get(), but each entry has an additional + parameter to specify the required type. Only atoms with a matching type + will be selected. + + The arguments should be a series of uint32_t key, const LV2_Atom**, uint32_t + type triples, terminated by a zero key. The value pointers MUST be + initialized to NULL. For example: + + @code + const LV2_Atom_String* name = NULL; + const LV2_Atom_Int* age = NULL; + lv2_atom_object_get(obj, + uris.name_key, &name, uris.atom_String, + uris.age_key, &age, uris.atom_Int + 0); + @endcode +*/ +static inline int +lv2_atom_object_get_typed(const LV2_Atom_Object* object, ...) +{ + int matches = 0; + int n_queries = 0; + + /* Count number of keys so we can short-circuit when done */ + va_list args; + va_start(args, object); + for (n_queries = 0; va_arg(args, uint32_t); ++n_queries) { + if (!va_arg(args, const LV2_Atom**) || !va_arg(args, uint32_t)) { + va_end(args); + return -1; + } + } + va_end(args); + + LV2_ATOM_OBJECT_FOREACH (object, prop) { + va_start(args, object); + for (int i = 0; i < n_queries; ++i) { + const uint32_t qkey = va_arg(args, uint32_t); + const LV2_Atom** qval = va_arg(args, const LV2_Atom**); + const uint32_t qtype = va_arg(args, uint32_t); + if (!*qval && qkey == prop->key && qtype == prop->value.type) { + *qval = &prop->value; + if (++matches == n_queries) { + va_end(args); + return matches; + } + break; + } + } + va_end(args); + } + return matches; +} + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +/** + @} + @} +*/ + +#endif /* LV2_ATOM_UTIL_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/buf-size/buf-size.h b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/buf-size/buf-size.h new file mode 100644 index 0000000000..d96e17d5ac --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/buf-size/buf-size.h @@ -0,0 +1,51 @@ +/* + Copyright 2007-2016 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef LV2_BUF_SIZE_H +#define LV2_BUF_SIZE_H + +/** + @defgroup buf-size Buffer Size + @ingroup lv2 + + Access to, and restrictions on, buffer sizes. + + See for details. + + @{ +*/ + +// clang-format off + +#define LV2_BUF_SIZE_URI "http://lv2plug.in/ns/ext/buf-size" ///< http://lv2plug.in/ns/ext/buf-size +#define LV2_BUF_SIZE_PREFIX LV2_BUF_SIZE_URI "#" ///< http://lv2plug.in/ns/ext/buf-size# + +#define LV2_BUF_SIZE__boundedBlockLength LV2_BUF_SIZE_PREFIX "boundedBlockLength" ///< http://lv2plug.in/ns/ext/buf-size#boundedBlockLength +#define LV2_BUF_SIZE__coarseBlockLength LV2_BUF_SIZE_PREFIX "coarseBlockLength" ///< http://lv2plug.in/ns/ext/buf-size#coarseBlockLength +#define LV2_BUF_SIZE__fixedBlockLength LV2_BUF_SIZE_PREFIX "fixedBlockLength" ///< http://lv2plug.in/ns/ext/buf-size#fixedBlockLength +#define LV2_BUF_SIZE__maxBlockLength LV2_BUF_SIZE_PREFIX "maxBlockLength" ///< http://lv2plug.in/ns/ext/buf-size#maxBlockLength +#define LV2_BUF_SIZE__minBlockLength LV2_BUF_SIZE_PREFIX "minBlockLength" ///< http://lv2plug.in/ns/ext/buf-size#minBlockLength +#define LV2_BUF_SIZE__nominalBlockLength LV2_BUF_SIZE_PREFIX "nominalBlockLength" ///< http://lv2plug.in/ns/ext/buf-size#nominalBlockLength +#define LV2_BUF_SIZE__powerOf2BlockLength LV2_BUF_SIZE_PREFIX "powerOf2BlockLength" ///< http://lv2plug.in/ns/ext/buf-size#powerOf2BlockLength +#define LV2_BUF_SIZE__sequenceSize LV2_BUF_SIZE_PREFIX "sequenceSize" ///< http://lv2plug.in/ns/ext/buf-size#sequenceSize + +// clang-format on + +/** + @} +*/ + +#endif /* LV2_BUF_SIZE_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/buf-size/buf-size.meta.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/buf-size/buf-size.meta.ttl new file mode 100644 index 0000000000..b1d8011c11 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/buf-size/buf-size.meta.ttl @@ -0,0 +1,157 @@ +@prefix bufsz: . +@prefix dcs: . +@prefix doap: . +@prefix foaf: . +@prefix lv2: . +@prefix rdfs: . + + + a doap:Project ; + doap:name "LV2 Buf Size" ; + doap:shortdesc "Access to, and restrictions on, buffer sizes." ; + doap:created "2012-08-07" ; + doap:developer ; + doap:release [ + doap:revision "1.4" ; + doap:created "2015-09-18" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add bufsz:nominalBlockLength option." + ] , [ + rdfs:label "Add bufsz:coarseBlockLength feature." + ] + ] + ] , [ + doap:revision "1.2" ; + doap:created "2012-12-21" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Fix typo in bufsz:sequenceSize label." + ] + ] + ] , [ + doap:revision "1.0" ; + doap:created "2012-10-14" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Initial release." + ] + ] + ] ; + lv2:documentation """ + +This extension defines a facility for plugins to get information about the +block length (the sample_count parameter of LV2_Descriptor::run) and port +buffer sizes, as well as several features which can be used to restrict the +block length. + +This extension defines features and properties but has no special purpose +API of its own. The host provides all the relevant information to the plugin +as [options](options.html). + +To require restrictions on the block length, plugins can require additional +features: bufsz:boundedBlockLength, bufsz:powerOf2BlockLength, and +bufsz:fixedBlockLength. These features are data-only, that is they merely +indicate a restriction and do not carry any data or API. + +"""^^lv2:Markdown . + +bufsz:boundedBlockLength + lv2:documentation """ + +A feature that indicates the host will provide both the bufsz:minBlockLength +and bufsz:maxBlockLength options to the plugin. Plugins that copy data from +audio inputs can require this feature to ensure they know how much space is +required for auxiliary buffers. Note the minimum may be zero, this feature is +mainly useful to ensure a maximum is available. + +All hosts SHOULD support this feature, since it is simple to support and +necessary for any plugins that may need to copy the input. + +"""^^lv2:Markdown . + +bufsz:fixedBlockLength + lv2:documentation """ + +A feature that indicates the host will always call LV2_Descriptor::run() with +the same value for sample_count. This length MUST be provided as the value of +both the bufsz:minBlockLength and bufsz:maxBlockLength options. + +Note that requiring this feature may severely limit the number of hosts capable +of running the plugin. + +"""^^lv2:Markdown . + +bufsz:powerOf2BlockLength + lv2:documentation """ + +A feature that indicates the host will always call LV2_Descriptor::run() with a +power of two sample_count. Note that this feature does not guarantee the value +is the same each call, to guarantee a fixed power of two block length plugins +must require both this feature and bufsz:fixedBlockLength. + +Note that requiring this feature may severely limit the number of hosts capable +of running the plugin. + +"""^^lv2:Markdown . + +bufsz:coarseBlockLength + lv2:documentation """ + +A feature that indicates the plugin prefers coarse, regular block lengths. For +example, plugins that do not implement sample-accurate control use this feature +to indicate that the host should not split the run cycle because controls have +changed. + +Note that this feature is merely a hint, and does not guarantee a fixed block +length. The run cycle may be split for other reasons, and the blocksize itself +may change anytime. + +"""^^lv2:Markdown . + +bufsz:maxBlockLength + lv2:documentation """ + +The maximum block length the host will ever request the plugin to process at +once, that is, the maximum `sample_count` parameter that will ever be passed to +LV2_Descriptor::run(). + +"""^^lv2:Markdown . + +bufsz:minBlockLength + lv2:documentation """ + +The minimum block length the host will ever request the plugin to process at +once, that is, the minimum `sample_count` parameter that will ever be passed to +LV2_Descriptor::run(). + +"""^^lv2:Markdown . + +bufsz:nominalBlockLength + lv2:documentation """ + +The typical block length the host will request the plugin to process at once, +that is, the typical `sample_count` parameter that will be passed to +LV2_Descriptor::run(). This will usually be equivalent, or close to, the +maximum block length, but there are no strong guarantees about this value +whatsoever. Plugins may use this length for optimization purposes, but MUST +NOT assume the host will always process blocks of this length. In particular, +the host MAY process longer blocks. + +"""^^lv2:Markdown . + +bufsz:sequenceSize + lv2:documentation """ + +This should be provided as an option by hosts that support event ports +(including but not limited to MIDI), so plugins have the ability to allocate +auxiliary buffers large enough to copy the input. + +"""^^lv2:Markdown . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/buf-size/buf-size.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/buf-size/buf-size.ttl new file mode 100644 index 0000000000..88631982f9 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/buf-size/buf-size.ttl @@ -0,0 +1,67 @@ +@prefix bufsz: . +@prefix lv2: . +@prefix opts: . +@prefix owl: . +@prefix rdf: . +@prefix rdfs: . +@prefix xsd: . + + + a owl:Ontology ; + rdfs:label "LV2 Buf Size" ; + rdfs:comment "Access to, and restrictions on, buffer sizes." ; + rdfs:seeAlso , + . + +bufsz:boundedBlockLength + a lv2:Feature ; + rdfs:label "bounded block length" ; + rdfs:comment "Block length has lower and upper bounds." . + +bufsz:fixedBlockLength + a lv2:Feature ; + rdfs:label "fixed block length" ; + rdfs:comment "Block length never changes." . + +bufsz:powerOf2BlockLength + a lv2:Feature ; + rdfs:label "power of 2 block length" ; + rdfs:comment "Block length is a power of 2." . + +bufsz:coarseBlockLength + a lv2:Feature ; + rdfs:label "coarse block length" ; + rdfs:comment "Plugin prefers coarse block length without buffer splitting." . + +bufsz:maxBlockLength + a rdf:Property , + owl:DatatypeProperty , + opts:Option ; + rdfs:label "maximum block length" ; + rdfs:comment "Block length has an upper bound." ; + rdfs:range xsd:nonNegativeInteger . + +bufsz:minBlockLength + a rdf:Property , + owl:DatatypeProperty , + opts:Option ; + rdfs:label "minimum block length" ; + rdfs:comment "Block length has a lower bound." ; + rdfs:range xsd:nonNegativeInteger . + +bufsz:nominalBlockLength + a rdf:Property , + owl:DatatypeProperty , + opts:Option ; + rdfs:label "nominal block length" ; + rdfs:comment "Typical block length that will most often be processed." ; + rdfs:range xsd:nonNegativeInteger . + +bufsz:sequenceSize + a rdf:Property , + owl:DatatypeProperty , + opts:Option ; + rdfs:label "sequence size" ; + rdfs:comment "The maximum size of a sequence, in bytes." ; + rdfs:range xsd:nonNegativeInteger . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/buf-size/manifest.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/buf-size/manifest.ttl new file mode 100644 index 0000000000..d242f97b67 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/buf-size/manifest.ttl @@ -0,0 +1,9 @@ +@prefix lv2: . +@prefix rdfs: . + + + a lv2:Specification ; + lv2:minorVersion 1 ; + lv2:microVersion 4 ; + rdfs:seeAlso . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/core/attributes.h b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/core/attributes.h new file mode 100644 index 0000000000..dfcff3a916 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/core/attributes.h @@ -0,0 +1,59 @@ +/* + Copyright 2018 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef LV2_CORE_ATTRIBUTES_H +#define LV2_CORE_ATTRIBUTES_H + +/** + @defgroup attributes Attributes + @ingroup lv2 + + Macros for source code attributes. + + @{ +*/ + +#if defined(__GNUC__) && __GNUC__ > 3 +# define LV2_DEPRECATED __attribute__((__deprecated__)) +#else +# define LV2_DEPRECATED +#endif + +#if defined(__clang__) +# define LV2_DISABLE_DEPRECATION_WARNINGS \ + _Pragma("clang diagnostic push") \ + _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"") +#elif defined(__GNUC__) && __GNUC__ > 4 +# define LV2_DISABLE_DEPRECATION_WARNINGS \ + _Pragma("GCC diagnostic push") \ + _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") +#else +# define LV2_DISABLE_DEPRECATION_WARNINGS +#endif + +#if defined(__clang__) +# define LV2_RESTORE_WARNINGS _Pragma("clang diagnostic pop") +#elif defined(__GNUC__) && __GNUC__ > 4 +# define LV2_RESTORE_WARNINGS _Pragma("GCC diagnostic pop") +#else +# define LV2_RESTORE_WARNINGS +#endif + +/** + @} +*/ + +#endif /* LV2_CORE_ATTRIBUTES_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/core/lv2.h b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/core/lv2.h new file mode 100644 index 0000000000..614ec7652d --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/core/lv2.h @@ -0,0 +1,484 @@ +/* + LV2 - An audio plugin interface specification. + Copyright 2007-2012 Steve Harris, David Robillard. + + Based on LADSPA, Copyright 2000-2002 Richard W.E. Furse, + Paul Barton-Davis, Stefan Westerfeld. + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef LV2_H_INCLUDED +#define LV2_H_INCLUDED + +/** + @defgroup lv2 LV2 + + The LV2 specification. + + @{ +*/ + +/** + @defgroup lv2core LV2 Core + + Core LV2 specification. + + See for details. + + @{ +*/ + +#include + +// clang-format off + +#define LV2_CORE_URI "http://lv2plug.in/ns/lv2core" ///< http://lv2plug.in/ns/lv2core +#define LV2_CORE_PREFIX LV2_CORE_URI "#" ///< http://lv2plug.in/ns/lv2core# + +#define LV2_CORE__AllpassPlugin LV2_CORE_PREFIX "AllpassPlugin" ///< http://lv2plug.in/ns/lv2core#AllpassPlugin +#define LV2_CORE__AmplifierPlugin LV2_CORE_PREFIX "AmplifierPlugin" ///< http://lv2plug.in/ns/lv2core#AmplifierPlugin +#define LV2_CORE__AnalyserPlugin LV2_CORE_PREFIX "AnalyserPlugin" ///< http://lv2plug.in/ns/lv2core#AnalyserPlugin +#define LV2_CORE__AudioPort LV2_CORE_PREFIX "AudioPort" ///< http://lv2plug.in/ns/lv2core#AudioPort +#define LV2_CORE__BandpassPlugin LV2_CORE_PREFIX "BandpassPlugin" ///< http://lv2plug.in/ns/lv2core#BandpassPlugin +#define LV2_CORE__CVPort LV2_CORE_PREFIX "CVPort" ///< http://lv2plug.in/ns/lv2core#CVPort +#define LV2_CORE__ChorusPlugin LV2_CORE_PREFIX "ChorusPlugin" ///< http://lv2plug.in/ns/lv2core#ChorusPlugin +#define LV2_CORE__CombPlugin LV2_CORE_PREFIX "CombPlugin" ///< http://lv2plug.in/ns/lv2core#CombPlugin +#define LV2_CORE__CompressorPlugin LV2_CORE_PREFIX "CompressorPlugin" ///< http://lv2plug.in/ns/lv2core#CompressorPlugin +#define LV2_CORE__ConstantPlugin LV2_CORE_PREFIX "ConstantPlugin" ///< http://lv2plug.in/ns/lv2core#ConstantPlugin +#define LV2_CORE__ControlPort LV2_CORE_PREFIX "ControlPort" ///< http://lv2plug.in/ns/lv2core#ControlPort +#define LV2_CORE__ConverterPlugin LV2_CORE_PREFIX "ConverterPlugin" ///< http://lv2plug.in/ns/lv2core#ConverterPlugin +#define LV2_CORE__DelayPlugin LV2_CORE_PREFIX "DelayPlugin" ///< http://lv2plug.in/ns/lv2core#DelayPlugin +#define LV2_CORE__DistortionPlugin LV2_CORE_PREFIX "DistortionPlugin" ///< http://lv2plug.in/ns/lv2core#DistortionPlugin +#define LV2_CORE__DynamicsPlugin LV2_CORE_PREFIX "DynamicsPlugin" ///< http://lv2plug.in/ns/lv2core#DynamicsPlugin +#define LV2_CORE__EQPlugin LV2_CORE_PREFIX "EQPlugin" ///< http://lv2plug.in/ns/lv2core#EQPlugin +#define LV2_CORE__EnvelopePlugin LV2_CORE_PREFIX "EnvelopePlugin" ///< http://lv2plug.in/ns/lv2core#EnvelopePlugin +#define LV2_CORE__ExpanderPlugin LV2_CORE_PREFIX "ExpanderPlugin" ///< http://lv2plug.in/ns/lv2core#ExpanderPlugin +#define LV2_CORE__ExtensionData LV2_CORE_PREFIX "ExtensionData" ///< http://lv2plug.in/ns/lv2core#ExtensionData +#define LV2_CORE__Feature LV2_CORE_PREFIX "Feature" ///< http://lv2plug.in/ns/lv2core#Feature +#define LV2_CORE__FilterPlugin LV2_CORE_PREFIX "FilterPlugin" ///< http://lv2plug.in/ns/lv2core#FilterPlugin +#define LV2_CORE__FlangerPlugin LV2_CORE_PREFIX "FlangerPlugin" ///< http://lv2plug.in/ns/lv2core#FlangerPlugin +#define LV2_CORE__FunctionPlugin LV2_CORE_PREFIX "FunctionPlugin" ///< http://lv2plug.in/ns/lv2core#FunctionPlugin +#define LV2_CORE__GatePlugin LV2_CORE_PREFIX "GatePlugin" ///< http://lv2plug.in/ns/lv2core#GatePlugin +#define LV2_CORE__GeneratorPlugin LV2_CORE_PREFIX "GeneratorPlugin" ///< http://lv2plug.in/ns/lv2core#GeneratorPlugin +#define LV2_CORE__HighpassPlugin LV2_CORE_PREFIX "HighpassPlugin" ///< http://lv2plug.in/ns/lv2core#HighpassPlugin +#define LV2_CORE__InputPort LV2_CORE_PREFIX "InputPort" ///< http://lv2plug.in/ns/lv2core#InputPort +#define LV2_CORE__InstrumentPlugin LV2_CORE_PREFIX "InstrumentPlugin" ///< http://lv2plug.in/ns/lv2core#InstrumentPlugin +#define LV2_CORE__LimiterPlugin LV2_CORE_PREFIX "LimiterPlugin" ///< http://lv2plug.in/ns/lv2core#LimiterPlugin +#define LV2_CORE__LowpassPlugin LV2_CORE_PREFIX "LowpassPlugin" ///< http://lv2plug.in/ns/lv2core#LowpassPlugin +#define LV2_CORE__MixerPlugin LV2_CORE_PREFIX "MixerPlugin" ///< http://lv2plug.in/ns/lv2core#MixerPlugin +#define LV2_CORE__ModulatorPlugin LV2_CORE_PREFIX "ModulatorPlugin" ///< http://lv2plug.in/ns/lv2core#ModulatorPlugin +#define LV2_CORE__MultiEQPlugin LV2_CORE_PREFIX "MultiEQPlugin" ///< http://lv2plug.in/ns/lv2core#MultiEQPlugin +#define LV2_CORE__OscillatorPlugin LV2_CORE_PREFIX "OscillatorPlugin" ///< http://lv2plug.in/ns/lv2core#OscillatorPlugin +#define LV2_CORE__OutputPort LV2_CORE_PREFIX "OutputPort" ///< http://lv2plug.in/ns/lv2core#OutputPort +#define LV2_CORE__ParaEQPlugin LV2_CORE_PREFIX "ParaEQPlugin" ///< http://lv2plug.in/ns/lv2core#ParaEQPlugin +#define LV2_CORE__PhaserPlugin LV2_CORE_PREFIX "PhaserPlugin" ///< http://lv2plug.in/ns/lv2core#PhaserPlugin +#define LV2_CORE__PitchPlugin LV2_CORE_PREFIX "PitchPlugin" ///< http://lv2plug.in/ns/lv2core#PitchPlugin +#define LV2_CORE__Plugin LV2_CORE_PREFIX "Plugin" ///< http://lv2plug.in/ns/lv2core#Plugin +#define LV2_CORE__PluginBase LV2_CORE_PREFIX "PluginBase" ///< http://lv2plug.in/ns/lv2core#PluginBase +#define LV2_CORE__Point LV2_CORE_PREFIX "Point" ///< http://lv2plug.in/ns/lv2core#Point +#define LV2_CORE__Port LV2_CORE_PREFIX "Port" ///< http://lv2plug.in/ns/lv2core#Port +#define LV2_CORE__PortProperty LV2_CORE_PREFIX "PortProperty" ///< http://lv2plug.in/ns/lv2core#PortProperty +#define LV2_CORE__Resource LV2_CORE_PREFIX "Resource" ///< http://lv2plug.in/ns/lv2core#Resource +#define LV2_CORE__ReverbPlugin LV2_CORE_PREFIX "ReverbPlugin" ///< http://lv2plug.in/ns/lv2core#ReverbPlugin +#define LV2_CORE__ScalePoint LV2_CORE_PREFIX "ScalePoint" ///< http://lv2plug.in/ns/lv2core#ScalePoint +#define LV2_CORE__SimulatorPlugin LV2_CORE_PREFIX "SimulatorPlugin" ///< http://lv2plug.in/ns/lv2core#SimulatorPlugin +#define LV2_CORE__SpatialPlugin LV2_CORE_PREFIX "SpatialPlugin" ///< http://lv2plug.in/ns/lv2core#SpatialPlugin +#define LV2_CORE__Specification LV2_CORE_PREFIX "Specification" ///< http://lv2plug.in/ns/lv2core#Specification +#define LV2_CORE__SpectralPlugin LV2_CORE_PREFIX "SpectralPlugin" ///< http://lv2plug.in/ns/lv2core#SpectralPlugin +#define LV2_CORE__UtilityPlugin LV2_CORE_PREFIX "UtilityPlugin" ///< http://lv2plug.in/ns/lv2core#UtilityPlugin +#define LV2_CORE__WaveshaperPlugin LV2_CORE_PREFIX "WaveshaperPlugin" ///< http://lv2plug.in/ns/lv2core#WaveshaperPlugin +#define LV2_CORE__appliesTo LV2_CORE_PREFIX "appliesTo" ///< http://lv2plug.in/ns/lv2core#appliesTo +#define LV2_CORE__binary LV2_CORE_PREFIX "binary" ///< http://lv2plug.in/ns/lv2core#binary +#define LV2_CORE__connectionOptional LV2_CORE_PREFIX "connectionOptional" ///< http://lv2plug.in/ns/lv2core#connectionOptional +#define LV2_CORE__control LV2_CORE_PREFIX "control" ///< http://lv2plug.in/ns/lv2core#control +#define LV2_CORE__default LV2_CORE_PREFIX "default" ///< http://lv2plug.in/ns/lv2core#default +#define LV2_CORE__designation LV2_CORE_PREFIX "designation" ///< http://lv2plug.in/ns/lv2core#designation +#define LV2_CORE__documentation LV2_CORE_PREFIX "documentation" ///< http://lv2plug.in/ns/lv2core#documentation +#define LV2_CORE__enumeration LV2_CORE_PREFIX "enumeration" ///< http://lv2plug.in/ns/lv2core#enumeration +#define LV2_CORE__extensionData LV2_CORE_PREFIX "extensionData" ///< http://lv2plug.in/ns/lv2core#extensionData +#define LV2_CORE__freeWheeling LV2_CORE_PREFIX "freeWheeling" ///< http://lv2plug.in/ns/lv2core#freeWheeling +#define LV2_CORE__hardRTCapable LV2_CORE_PREFIX "hardRTCapable" ///< http://lv2plug.in/ns/lv2core#hardRTCapable +#define LV2_CORE__inPlaceBroken LV2_CORE_PREFIX "inPlaceBroken" ///< http://lv2plug.in/ns/lv2core#inPlaceBroken +#define LV2_CORE__index LV2_CORE_PREFIX "index" ///< http://lv2plug.in/ns/lv2core#index +#define LV2_CORE__integer LV2_CORE_PREFIX "integer" ///< http://lv2plug.in/ns/lv2core#integer +#define LV2_CORE__isLive LV2_CORE_PREFIX "isLive" ///< http://lv2plug.in/ns/lv2core#isLive +#define LV2_CORE__latency LV2_CORE_PREFIX "latency" ///< http://lv2plug.in/ns/lv2core#latency +#define LV2_CORE__maximum LV2_CORE_PREFIX "maximum" ///< http://lv2plug.in/ns/lv2core#maximum +#define LV2_CORE__microVersion LV2_CORE_PREFIX "microVersion" ///< http://lv2plug.in/ns/lv2core#microVersion +#define LV2_CORE__minimum LV2_CORE_PREFIX "minimum" ///< http://lv2plug.in/ns/lv2core#minimum +#define LV2_CORE__minorVersion LV2_CORE_PREFIX "minorVersion" ///< http://lv2plug.in/ns/lv2core#minorVersion +#define LV2_CORE__name LV2_CORE_PREFIX "name" ///< http://lv2plug.in/ns/lv2core#name +#define LV2_CORE__optionalFeature LV2_CORE_PREFIX "optionalFeature" ///< http://lv2plug.in/ns/lv2core#optionalFeature +#define LV2_CORE__port LV2_CORE_PREFIX "port" ///< http://lv2plug.in/ns/lv2core#port +#define LV2_CORE__portProperty LV2_CORE_PREFIX "portProperty" ///< http://lv2plug.in/ns/lv2core#portProperty +#define LV2_CORE__project LV2_CORE_PREFIX "project" ///< http://lv2plug.in/ns/lv2core#project +#define LV2_CORE__prototype LV2_CORE_PREFIX "prototype" ///< http://lv2plug.in/ns/lv2core#prototype +#define LV2_CORE__reportsLatency LV2_CORE_PREFIX "reportsLatency" ///< http://lv2plug.in/ns/lv2core#reportsLatency +#define LV2_CORE__requiredFeature LV2_CORE_PREFIX "requiredFeature" ///< http://lv2plug.in/ns/lv2core#requiredFeature +#define LV2_CORE__sampleRate LV2_CORE_PREFIX "sampleRate" ///< http://lv2plug.in/ns/lv2core#sampleRate +#define LV2_CORE__scalePoint LV2_CORE_PREFIX "scalePoint" ///< http://lv2plug.in/ns/lv2core#scalePoint +#define LV2_CORE__symbol LV2_CORE_PREFIX "symbol" ///< http://lv2plug.in/ns/lv2core#symbol +#define LV2_CORE__toggled LV2_CORE_PREFIX "toggled" ///< http://lv2plug.in/ns/lv2core#toggled + +// clang-format on + +#ifdef __cplusplus +extern "C" { +#endif + +/** + Plugin Instance Handle. + + This is a handle for one particular instance of a plugin. It is valid to + compare to NULL (or 0 for C++) but otherwise the host MUST NOT attempt to + interpret it. +*/ +typedef void* LV2_Handle; + +/** + Feature. + + Features allow hosts to make additional functionality available to plugins + without requiring modification to the LV2 API. Extensions may define new + features and specify the `URI` and `data` to be used if necessary. + Some features, such as lv2:isLive, do not require the host to pass data. +*/ +typedef struct { + /** + A globally unique, case-sensitive identifier (URI) for this feature. + + This MUST be a valid URI string as defined by RFC 3986. + */ + const char* URI; + + /** + Pointer to arbitrary data. + + The format of this data is defined by the extension which describes the + feature with the given `URI`. + */ + void* data; +} LV2_Feature; + +/** + Plugin Descriptor. + + This structure provides the core functions necessary to instantiate and use + a plugin. +*/ +typedef struct LV2_Descriptor { + /** + A globally unique, case-sensitive identifier for this plugin. + + This MUST be a valid URI string as defined by RFC 3986. All plugins with + the same URI MUST be compatible to some degree, see + http://lv2plug.in/ns/lv2core for details. + */ + const char* URI; + + /** + Instantiate the plugin. + + Note that instance initialisation should generally occur in activate() + rather than here. If a host calls instantiate(), it MUST call cleanup() + at some point in the future. + + @param descriptor Descriptor of the plugin to instantiate. + + @param sample_rate Sample rate, in Hz, for the new plugin instance. + + @param bundle_path Path to the LV2 bundle which contains this plugin + binary. It MUST include the trailing directory separator so that simply + appending a filename will yield the path to that file in the bundle. + + @param features A NULL terminated array of LV2_Feature structs which + represent the features the host supports. Plugins may refuse to + instantiate if required features are not found here. However, hosts MUST + NOT use this as a discovery mechanism: instead, use the RDF data to + determine which features are required and do not attempt to instantiate + unsupported plugins at all. This parameter MUST NOT be NULL, i.e. a host + that supports no features MUST pass a single element array containing + NULL. + + @return A handle for the new plugin instance, or NULL if instantiation + has failed. + */ + LV2_Handle (*instantiate)(const struct LV2_Descriptor* descriptor, + double sample_rate, + const char* bundle_path, + const LV2_Feature* const* features); + + /** + Connect a port on a plugin instance to a memory location. + + Plugin writers should be aware that the host may elect to use the same + buffer for more than one port and even use the same buffer for both + input and output (see lv2:inPlaceBroken in lv2.ttl). + + If the plugin has the feature lv2:hardRTCapable then there are various + things that the plugin MUST NOT do within the connect_port() function; + see lv2core.ttl for details. + + connect_port() MUST be called at least once for each port before run() + is called, unless that port is lv2:connectionOptional. The plugin must + pay careful attention to the block size passed to run() since the block + allocated may only just be large enough to contain the data, and is not + guaranteed to remain constant between run() calls. + + connect_port() may be called more than once for a plugin instance to + allow the host to change the buffers that the plugin is reading or + writing. These calls may be made before or after activate() or + deactivate() calls. + + @param instance Plugin instance containing the port. + + @param port Index of the port to connect. The host MUST NOT try to + connect a port index that is not defined in the plugin's RDF data. If + it does, the plugin's behaviour is undefined (a crash is likely). + + @param data_location Pointer to data of the type defined by the port + type in the plugin's RDF data (for example, an array of float for an + lv2:AudioPort). This pointer must be stored by the plugin instance and + used to read/write data when run() is called. Data present at the time + of the connect_port() call MUST NOT be considered meaningful. + */ + void (*connect_port)(LV2_Handle instance, uint32_t port, void* data_location); + + /** + Initialise a plugin instance and activate it for use. + + This is separated from instantiate() to aid real-time support and so + that hosts can reinitialise a plugin instance by calling deactivate() + and then activate(). In this case the plugin instance MUST reset all + state information dependent on the history of the plugin instance except + for any data locations provided by connect_port(). If there is nothing + for activate() to do then this field may be NULL. + + When present, hosts MUST call this function once before run() is called + for the first time. This call SHOULD be made as close to the run() call + as possible and indicates to real-time plugins that they are now live, + however plugins MUST NOT rely on a prompt call to run() after + activate(). + + The host MUST NOT call activate() again until deactivate() has been + called first. If a host calls activate(), it MUST call deactivate() at + some point in the future. Note that connect_port() may be called before + or after activate(). + */ + void (*activate)(LV2_Handle instance); + + /** + Run a plugin instance for a block. + + Note that if an activate() function exists then it must be called before + run(). If deactivate() is called for a plugin instance then run() may + not be called until activate() has been called again. + + If the plugin has the feature lv2:hardRTCapable then there are various + things that the plugin MUST NOT do within the run() function (see + lv2core.ttl for details). + + As a special case, when `sample_count` is 0, the plugin should update + any output ports that represent a single instant in time (for example, + control ports, but not audio ports). This is particularly useful for + latent plugins, which should update their latency output port so hosts + can pre-roll plugins to compute latency. Plugins MUST NOT crash when + `sample_count` is 0. + + @param instance Instance to be run. + + @param sample_count The block size (in samples) for which the plugin + instance must run. + */ + void (*run)(LV2_Handle instance, uint32_t sample_count); + + /** + Deactivate a plugin instance (counterpart to activate()). + + Hosts MUST deactivate all activated instances after they have been run() + for the last time. This call SHOULD be made as close to the last run() + call as possible and indicates to real-time plugins that they are no + longer live, however plugins MUST NOT rely on prompt deactivation. If + there is nothing for deactivate() to do then this field may be NULL + + Deactivation is not similar to pausing since the plugin instance will be + reinitialised by activate(). However, deactivate() itself MUST NOT fully + reset plugin state. For example, the host may deactivate a plugin, then + store its state (using some extension to do so). + + Hosts MUST NOT call deactivate() unless activate() was previously + called. Note that connect_port() may be called before or after + deactivate(). + */ + void (*deactivate)(LV2_Handle instance); + + /** + Clean up a plugin instance (counterpart to instantiate()). + + Once an instance of a plugin has been finished with it must be deleted + using this function. The instance handle passed ceases to be valid after + this call. + + If activate() was called for a plugin instance then a corresponding call + to deactivate() MUST be made before cleanup() is called. Hosts MUST NOT + call cleanup() unless instantiate() was previously called. + */ + void (*cleanup)(LV2_Handle instance); + + /** + Return additional plugin data defined by some extenion. + + A typical use of this facility is to return a struct containing function + pointers to extend the LV2_Descriptor API. + + The actual type and meaning of the returned object MUST be specified + precisely by the extension. This function MUST return NULL for any + unsupported URI. If a plugin does not support any extension data, this + field may be NULL. + + The host is never responsible for freeing the returned value. + */ + const void* (*extension_data)(const char* uri); +} LV2_Descriptor; + +/** + Helper macro needed for LV2_SYMBOL_EXPORT when using C++. +*/ +#ifdef __cplusplus +# define LV2_SYMBOL_EXTERN extern "C" +#else +# define LV2_SYMBOL_EXTERN +#endif + +/** + Put this (LV2_SYMBOL_EXPORT) before any functions that are to be loaded + by the host as a symbol from the dynamic library. +*/ +#ifdef _WIN32 +# define LV2_SYMBOL_EXPORT LV2_SYMBOL_EXTERN __declspec(dllexport) +#else +# define LV2_SYMBOL_EXPORT \ + LV2_SYMBOL_EXTERN __attribute__((visibility("default"))) +#endif + +/** + Prototype for plugin accessor function. + + Plugins are discovered by hosts using RDF data (not by loading libraries). + See http://lv2plug.in for details on the discovery process, though most + hosts should use an existing library to implement this functionality. + + This is the simple plugin discovery API, suitable for most statically + defined plugins. Advanced plugins that need access to their bundle during + discovery can use lv2_lib_descriptor() instead. Plugin libraries MUST + include a function called "lv2_descriptor" or "lv2_lib_descriptor" with + C-style linkage, but SHOULD provide "lv2_descriptor" wherever possible. + + When it is time to load a plugin (designated by its URI), the host loads the + plugin's library, gets the lv2_descriptor() function from it, and uses this + function to find the LV2_Descriptor for the desired plugin. Plugins are + accessed by index using values from 0 upwards. This function MUST return + NULL for out of range indices, so the host can enumerate plugins by + increasing `index` until NULL is returned. + + Note that `index` has no meaning, hosts MUST NOT depend on it remaining + consistent between loads of the plugin library. +*/ +LV2_SYMBOL_EXPORT +const LV2_Descriptor* +lv2_descriptor(uint32_t index); + +/** + Type of the lv2_descriptor() function in a library (old discovery API). +*/ +typedef const LV2_Descriptor* (*LV2_Descriptor_Function)(uint32_t index); + +/** + Handle for a library descriptor. +*/ +typedef void* LV2_Lib_Handle; + +/** + Descriptor for a plugin library. + + To access a plugin library, the host creates an LV2_Lib_Descriptor via the + lv2_lib_descriptor() function in the shared object. +*/ +typedef struct { + /** + Opaque library data which must be passed as the first parameter to all + the methods of this struct. + */ + LV2_Lib_Handle handle; + + /** + The total size of this struct. This allows for this struct to be + expanded in the future if necessary. This MUST be set by the library to + sizeof(LV2_Lib_Descriptor). The host MUST NOT access any fields of this + struct beyond get_plugin() unless this field indicates they are present. + */ + uint32_t size; + + /** + Destroy this library descriptor and free all related resources. + */ + void (*cleanup)(LV2_Lib_Handle handle); + + /** + Plugin accessor. + + Plugins are accessed by index using values from 0 upwards. Out of range + indices MUST result in this function returning NULL, so the host can + enumerate plugins by increasing `index` until NULL is returned. + */ + const LV2_Descriptor* (*get_plugin)(LV2_Lib_Handle handle, uint32_t index); +} LV2_Lib_Descriptor; + +/** + Prototype for library accessor function. + + This is the more advanced discovery API, which allows plugin libraries to + access their bundles during discovery, which makes it possible for plugins to + be dynamically defined by files in their bundle. This API also has an + explicit cleanup function, removing any need for non-portable shared library + destructors. Simple plugins that do not require these features may use + lv2_descriptor() instead. + + This is the entry point for a plugin library. Hosts load this symbol from + the library and call this function to obtain a library descriptor which can + be used to access all the contained plugins. The returned object must not + be destroyed (using LV2_Lib_Descriptor::cleanup()) until all plugins loaded + from that library have been destroyed. +*/ +LV2_SYMBOL_EXPORT +const LV2_Lib_Descriptor* +lv2_lib_descriptor(const char* bundle_path, const LV2_Feature* const* features); + +/** + Type of the lv2_lib_descriptor() function in an LV2 library. +*/ +typedef const LV2_Lib_Descriptor* (*LV2_Lib_Descriptor_Function)( + const char* bundle_path, + const LV2_Feature* const* features); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +/** + @} + @} +*/ + +#endif /* LV2_H_INCLUDED */ diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/core/lv2_util.h b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/core/lv2_util.h new file mode 100644 index 0000000000..5dab8a024f --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/core/lv2_util.h @@ -0,0 +1,103 @@ +/* + Copyright 2016 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +/** + @defgroup util Utilities + @ingroup lv2core + @{ +*/ + +#include "lv2/core/lv2.h" + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + Return the data for a feature in a features array. + + If the feature is not found, NULL is returned. Note that this function is + only useful for features with data, and can not detect features that are + present but have NULL data. +*/ +static inline void* +lv2_features_data(const LV2_Feature* const* features, const char* const uri) +{ + if (features) { + for (const LV2_Feature* const* f = features; *f; ++f) { + if (!strcmp(uri, (*f)->URI)) { + return (*f)->data; + } + } + } + return NULL; +} + +/** + Query a features array. + + This function allows getting several features in one call, and detect + missing required features, with the same caveat of lv2_features_data(). + + The arguments should be a series of const char* uri, void** data, bool + required, terminated by a NULL URI. The data pointers MUST be initialized + to NULL. For example: + + @code + LV2_URID_Log* log = NULL; + LV2_URID_Map* map = NULL; + const char* missing = lv2_features_query( + features, + LV2_LOG__log, &log, false, + LV2_URID__map, &map, true, + NULL); + @endcode + + @return NULL on success, otherwise the URI of this missing feature. +*/ +static inline const char* +lv2_features_query(const LV2_Feature* const* features, ...) +{ + va_list args; + va_start(args, features); + + const char* uri = NULL; + while ((uri = va_arg(args, const char*))) { + void** data = va_arg(args, void**); + bool required = va_arg(args, int); + + *data = lv2_features_data(features, uri); + if (required && !*data) { + va_end(args); + return uri; + } + } + + va_end(args); + return NULL; +} + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +/** + @} +*/ diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/core/lv2core.meta.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/core/lv2core.meta.ttl new file mode 100644 index 0000000000..465917dc98 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/core/lv2core.meta.ttl @@ -0,0 +1,905 @@ +@prefix lv2: . +@prefix dcs: . +@prefix doap: . +@prefix foaf: . +@prefix rdfs: . + + + a doap:Project ; + doap:license ; + doap:name "LV2" ; + doap:homepage ; + doap:created "2004-04-21" ; + doap:shortdesc "An extensible open standard for audio plugins" ; + doap:programming-language "C" ; + doap:developer , + ; + doap:maintainer ; + doap:release [ + doap:revision "18.0" ; + doap:created "2020-04-26" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add lv2:Markdown datatype." + ] , [ + rdfs:label "Deprecate lv2:reportsLatency." + ] + ] + ] , [ + doap:revision "16.0" ; + doap:created "2019-02-03" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add lv2:MIDIPlugin class." + ] , [ + rdfs:label "Rework port restrictions so that presets can be validated." + ] + ] + ] , [ + doap:revision "14.0" ; + doap:created "2016-09-18" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add lv2_util.h with lv2_features_data() and lv2_features_query()." + ] , [ + rdfs:label "Add lv2:enabled designation." + ] + ] + ] , [ + doap:revision "12.4" ; + doap:created "2015-04-07" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Relax domain of lv2:minimum lv2:maximum and lv2:default so they can be used to describe properties/parameters as well." + ] , [ + rdfs:label "Add extern C and visibility attribute to LV2_SYMBOL_EXPORT." + ] , [ + rdfs:label "Add lv2:isSideChain port property." + ] + ] + ] , [ + doap:revision "12.2" ; + doap:created "2014-08-08" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Clarify lv2_descriptor() and lv2_lib_descriptor() documentation." + ] + ] + ] , [ + doap:revision "12.0" ; + doap:created "2014-01-04" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add lv2:prototype for property inheritance." + ] + ] + ] , [ + doap:revision "10.0" ; + doap:created "2013-02-17" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add lv2:EnvelopePlugin class." + ] , [ + rdfs:label "Add lv2:control for designating primary event-based control ports." + ] , [ + rdfs:label "Set range of lv2:designation to lv2:Designation." + ] , [ + rdfs:label "Make lv2:Parameter rdfs:subClassOf rdf:Property." + ] , [ + rdfs:label "Reserve minor version 0 for unstable development plugins." + ] + ] + ] , [ + doap:revision "8.2" ; + doap:created "2012-10-14" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Use consistent label style." + ] + ] + ] , [ + doap:revision "8.0" ; + doap:created "2012-04-17" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Fix LV2_SYMBOL_EXPORT and lv2_descriptor prototype for Windows." + ] , [ + rdfs:label "Add metadata concept of a designation, a channel or parameter description which can be assigned to ports for more intelligent use by hosts." + ] , [ + rdfs:label "Add new discovery API which allows libraries to read bundle files during discovery, makes library construction/destruction explicit, and adds extensibility to prevent future breakage." + ] , [ + rdfs:label "Relax the range of lv2:index so it can be used for things other than ports." + ] , [ + rdfs:label "Remove lv2:Resource, which turned out to be meaningless." + ] , [ + rdfs:label "Add lv2:CVPort." + ] , [ + rdfs:label "Merge with unified LV2 package." + ] + ] + ] , [ + doap:revision "6.0" ; + doap:created "2011-11-21" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Rename core.lv2 and lv2.ttl to lv2core.lv2 and lv2core.ttl to adhere to modern conventions." + ] , [ + rdfs:label "Add lv2:extensionData and lv2:ExtensionData for plugins to indicate that they support some URI for extension_data()." + ] , [ + rdfs:label "Remove lv2config in favour of the simple convention that specifications install headers to standard URI-based paths." + ] , [ + rdfs:label "Switch to the ISC license, a simple BSD-style license (with permission of all contributors to lv2.h and its ancestor, ladspa.h)." + ] , [ + rdfs:label "Make lv2core.ttl a valid OWL 2 DL ontology." + ] , [ + rdfs:label "Improve documentation." + ] + ] + ] , [ + doap:revision "4.0" ; + doap:created "2011-03-18" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Make doap:license suggested, but not required (for wrappers)." + ] , [ + rdfs:label "Define lv2:binary (MUST be in manifest.ttl)." + ] , [ + rdfs:label "Define lv2:minorVersion and lv2:microVersion (MUST be in manifest.ttl)." + ] , [ + rdfs:label "Define lv2:documentation and use it to document lv2core." + ] , [ + rdfs:label "Add lv2:FunctionPlugin and lv2:ConstantPlugin classes." + ] , [ + rdfs:label "Move lv2:AmplifierPlugin under lv2:DynamicsPlugin." + ] , [ + rdfs:label "Loosen domain of lv2:optionalFeature and lv2:requiredFeature (to allow re-use in extensions)." + ] , [ + rdfs:label "Add generic lv2:Resource and lv2:PluginBase classes." + ] , [ + rdfs:label "Fix definition of lv2:minimum etc. (used for values, not scale points)." + ] , [ + rdfs:label "More precisely define properties with OWL." + ] , [ + rdfs:label "Move project metadata to manifest." + ] , [ + rdfs:label "Add lv2:enumeration port property." + ] , [ + rdfs:label "Define run() pre-roll special case (sample_count == 0)." + ] + ] + ] , [ + doap:revision "3.0" ; + doap:created "2008-11-08" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Require that serialisations refer to ports by symbol rather than index." + ] , [ + rdfs:label "Minor stylistic changes to lv2.ttl." + ] , [ + rdfs:label "No header changes." + ] + ] + ] , [ + doap:revision "2.0" ; + doap:created "2008-02-10" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Initial release." + ] + ] + ] ; + lv2:documentation """ + +LV2 is an interface for writing audio plugins in C or compatible languages, +which can be dynamically loaded into many _host_ applications. This core +specification is simple and minimal, but is designed so that _extensions_ can +be defined to add more advanced features, making it possible to implement +nearly any feature. + +LV2 maintains a strong distinction between code and data. Plugin code is in a +shared library, while data is in a companion data file written in +[Turtle](https://www.w3.org/TR/turtle/). Code, data, and any other resources +(such as waveforms) are shipped together in a bundle directory. The code +contains only the executable portions of the plugin. All other data is +provided in the data file(s). This makes plugin data flexible and extensible, +and allows the host to do everything but run the plugin without loading or +executing any code. Among other advantages, this makes hosts more robust +(broken plugins can't crash a host during discovery) and allows generic tools +written in any language to work with LV2 data. The LV2 specification itself is +distributed in a similar way. + +An LV2 plugin library is suitable for dynamic loading (for example with +`dlopen()`) and provides one or more plugin descriptors via `lv2_descriptor()` +or `lv2_lib_descriptor()`. These can be instantiated to create plugin +instances, which can be run directly on data or connected together to perform +advanced signal processing tasks. + +Plugins communicate via _ports_, which can transmit any type of data. Data is +processed by first connecting each port to a buffer, then repeatedly calling +the `run()` method to process blocks of data. + +This core specification defines two types of port, equivalent to those in +[LADSPA](http://www.ladspa.org/), lv2:ControlPort and lv2:AudioPort, as well as +lv2:CVPort which has the same format as an audio port but is interpreted as +non-audible control data. Audio ports contain arrays with one `float` element +per sample, allowing a block of audio to be processed in a single call to +`run()`. Control ports contain single `float` values, which are fixed and +valid for the duration of the call to `run()`. Thus the _control rate_ is +determined by the block size, which is controlled by the host (and not +necessarily constant). + +### Threading Rules + +To faciliate use in multi-threaded programs, LV2 functions are partitioned into +several threading classes: + +| Discovery Class | Instantiation Class | Audio Class | +|----------------------------------|-------------------------------|------------------------------- | +| lv2_descriptor() | LV2_Descriptor::instantiate() | LV2_Descriptor::run() | +| lv2_lib_descriptor() | LV2_Descriptor::cleanup() | LV2_Descriptor::connect_port() | +| LV2_Descriptor::extension_data() | LV2_Descriptor::activate() | | +| | LV2_Descriptor::deactivate() | | + +Hosts MUST guarantee that: + + * A function in any class is never called concurrently with another function + in that class. + + * A _Discovery_ function is never called concurrently with any other fuction + in the same shared object file. + + * An _Instantiation_ function for an instance is never called concurrently + with any other function for that instance. + +Any simultaneous calls that are not explicitly forbidden by these rules are +allowed. For example, a host may call `run()` for two different plugin +instances simultaneously. + +Plugin functions in any class MUST NOT manipulate any state which might affect +other plugins or the host (beyond the contract of that function), for example +by using non-reentrant global functions. + +Extensions to this specification which add new functions MUST declare in which +of these classes the functions belong, define new classes for them, or +otherwise precisely describe their threading rules. + +"""^^lv2:Markdown . + +lv2:Specification + lv2:documentation """ + +An LV2 specification typically contains a vocabulary description, C headers to +define an API, and any other resources that may be useful. Specifications, +like plugins, are distributed and installed as bundles so that hosts may +discover them. + +"""^^lv2:Markdown . + +lv2:Markdown + lv2:documentation """ + +This datatype is typically used for documentation in +[Markdown](https://daringfireball.net/projects/markdown/syntax) syntax. + +Generally, documentation with this datatype should stay as close to readable +plain text as possible, but may use core Markdown syntax for nicer +presentation. Documentation can assume that basic extensions like codehilite +and tables are available. + +"""^^lv2:Markdown . + +lv2:documentation + lv2:documentation """ + +Relates a Resource to extended documentation. + +LV2 specifications are documented using this property with an lv2:Markdown +datatype. + +If the value has no explicit datatype, it is assumed to be a valid XHTML Basic +1.1 fragment suitable for use as the content of the `body` element of a page. + +XHTML Basic is a W3C Recommendation which defines a simplified subset of XHTML +intended to be reasonable to implement with limited resources, for exampe on +embedded devices. See [XHTML Basic, Section +3](http://www.w3.org/TR/xhtml-basic/#s_xhtmlmodules) for a list of valid tags. + +"""^^lv2:Markdown . + +lv2:PluginBase + lv2:documentation """ + +An abstract plugin-like resource that may not actually be an LV2 plugin, for +example that may not have a lv2:binary. This is useful for describing things +that share common structure with a plugin, but are not themselves an actul +plugin, such as presets. + +"""^^lv2:Markdown . + +lv2:Plugin + lv2:documentation """ + +To be discovered by hosts, plugins MUST explicitly have an rdf:type of lv2:Plugin +in their bundle's manifest, for example: + + :::turtle + a lv2:Plugin . + +Plugins should have a doap:name property that is at most a few words in length +using title capitalization, for example Tape Delay Unit. + +"""^^lv2:Markdown . + +lv2:PortBase + lv2:documentation """ + +Similar to lv2:PluginBase, this is an abstract port-like resource that may not +be a fully specified LV2 port. For example, this is used for preset "ports" +which do not specify an index. + +"""^^lv2:Markdown . + +lv2:Port + lv2:documentation """ + +All LV2 port descriptions MUST have a rdf:type that is one of lv2:Port, +lv2:InputPort or lv2:OutputPort. Additionally, there MUST be at least one +other rdf:type which more precisely describes type of the port, for example +lv2:AudioPort. + +Hosts that do not support a specific port class MUST NOT instantiate the +plugin, unless that port has the lv2:connectionOptional property set. + +A port has two identifiers: a (numeric) index, and a (textual) symbol. The +index can be used as an identifier at run-time, but persistent references to +ports (for example in presets or save files) MUST use the symbol. Only the +symbol is guaranteed to refer to the same port on all plugins with a given URI, +that is the index for a port may differ between plugin binaries. + +"""^^lv2:Markdown . + +lv2:AudioPort + lv2:documentation """ + +Ports of this type are connected to a buffer of `float` audio samples, which +the host guarantees have `sample_count` elements in any call to +LV2_Descriptor::run(). + +Audio samples are normalized between -1.0 and 1.0, though there is no +requirement for samples to be strictly within this range. + +"""^^lv2:Markdown . + +lv2:CVPort + lv2:documentation """ + +Ports of this type have the same buffer format as an lv2:AudioPort, except the +buffer represents audio-rate control data rather than audio. Like a +lv2:ControlPort, a CV port SHOULD have properties describing its value, in +particular lv2:minimum, lv2:maximum, and lv2:default. + +Hosts may present CV ports to users as controls in the same way as control +ports. Conceptually, aside from the buffer format, a CV port is the same as a +control port, so hosts can use all the same properties and expectations. + +In particular, this port type does not imply any range, unit, or meaning for +its values. However, if there is no inherent unit to the values, for example +if the port is used to modulate some other value, then plugins SHOULD use a +normalized range, either from -1.0 to 1.0, or from 0.0 to 1.0. + +It is generally safe to connect an audio output to a CV input, but not +vice-versa. Hosts must take care to prevent data from a CVPort port from being +used as audio. + +"""^^lv2:Markdown . + +lv2:project + lv2:documentation """ + +This property provides a way to group plugins and/or related resources. A +project may have useful metadata common to all plugins (such as homepage, +author, version history) which would be wasteful to list separately for each +plugin. + +Grouping via projects also allows users to find plugins in hosts by project, +which is often how they are remembered. For this reason, a project that +contains plugins SHOULD always have a doap:name. It is also a good idea for +each plugin and the project itself to have an lv2:symbol property, which allows +nice quasi-global identifiers for plugins, for example `myproj.superamp` which +can be useful for display or fast user entry. + +"""^^lv2:Markdown . + +lv2:prototype + lv2:documentation """ + +This property can be used to include common properties in several +descriptions, serving as a sort of template mechanism. If a plugin has a +prototype, then the host must load all the properties for the prototype as if +they were properties of the plugin. That is, if `:plug lv2:prototype :prot`, +then for each triple `:prot p o`, the triple `:plug p o` should be loaded. + +This facility is useful for distributing data-only plugins that rely on a +common binary, for example those where the internal state is loaded from some +other file. Such plugins can refer to a prototype in a template LV2 bundle +which is installed by the corresponding software. + +"""^^lv2:Markdown . + +lv2:minorVersion + lv2:documentation """ + +This, along with lv2:microVersion, is used to distinguish between different +versions of the same resource, for example to load only the bundle with +the most recent version of a plugin. An LV2 version has a minor and micro +number with the usual semantics: + + * The minor version MUST be incremented when backwards (but not forwards) + compatible additions are made, for example the addition of a port to a + plugin. + + * The micro version is incremented for changes which do not affect + compatibility at all, for example bug fixes or documentation updates. + +Note that there is deliberately no major version: all versions with the same +URI are compatible by definition. Replacing a resource with a newer version of +that resource MUST NOT break anything. If a change violates this rule, then +the URI of the resource (which serves as the major version) MUST be changed. + +Plugins and extensions MUST adhere to at least the following rules: + + * All versions of a plugin with a given URI MUST have the same set of + mandatory (not lv2:connectionOptional) ports with respect to lv2:symbol and + rdf:type. In other words, every port on a particular version is guaranteed + to exist on a future version with same lv2:symbol and at least those + rdf:types. + + * New ports MAY be added without changing the plugin URI if and only if they + are lv2:connectionOptional and the minor version is incremented. + + * The minor version MUST be incremented if the index of any port (identified + by its symbol) is changed. + + * All versions of a specification MUST be compatible in the sense that an + implementation of the new version can interoperate with an implementation + of any previous version. + +Anything that depends on a specific version of a plugin (including referencing +ports by index) MUST refer to the plugin by both URI and version. However, +implementations should be tolerant where possible. + +When hosts discover several installed versions of a resource, they SHOULD warn +the user and load only the most recent version. + +An odd minor _or_ micro version, or minor version zero, indicates that the +resource is a development version. Hosts and tools SHOULD clearly indicate +this wherever appropriate. Minor version zero is a special case for +pre-release development of plugins, or experimental plugins that are not +intended for stable use at all. Hosts SHOULD NOT expect such a plugin to +remain compatible with any future version. Where feasible, hosts SHOULD NOT +expose such plugins to users by default, but may provide an option to display +them. + +"""^^lv2:Markdown . + +lv2:microVersion + lv2:documentation """ + +Releases of plugins and extensions MUST be explicitly versioned. Correct +version numbers MUST always be maintained for any versioned resource that is +published. For example, after a release, if a change is made in the development +version in source control, the micro version MUST be incremented (to an odd +number) to distinguish this modified version from the previous release. + +This property describes half of a resource version. For detailed documentation +on LV2 resource versioning, see lv2:minorVersion. + +"""^^lv2:Markdown . + +lv2:binary + lv2:documentation """ + +The value of this property must be the URI of a shared library object, +typically in the same bundle as the data file which contains this property. +The actual type of the library is platform specific. + +This is a required property of a lv2:Plugin which MUST be included in the +bundle's `manifest.ttl` file. The lv2:binary of a lv2:Plugin is the shared +object containing the lv2_descriptor() or lv2_lib_descriptor() function. This +probably may also be used similarly by extensions to relate other resources to +their implementations (it is not implied that a lv2:binary on an arbitrary +resource is an LV2 plugin library). + +"""^^lv2:Markdown . + +lv2:appliesTo + lv2:documentation """ + +This is primarily intended for discovery purposes: bundles that describe +resources that work with particular plugins (like presets or user interfaces) +SHOULD specify this in their `manifest.ttl` so the host can associate them with +the correct plugin. For example: + + :::turtle + + a ext:Thing ; + lv2:appliesTo ; + rdfs:seeAlso . + +Using this pattern is preferable for large amounts of data, since the host may +choose whether/when to load the data. + +"""^^lv2:Markdown . + +lv2:Symbol + lv2:documentation """ + +The first character of a symbol must be one of `_`, `a-z` or `A-Z`, and +subsequent characters may additionally be `0-9`. This is, among other things, +a valid C identifier, and generally compatible in most contexts which have +restrictions on string identifiers, such as file paths. + +"""^^lv2:Markdown . + +lv2:symbol + lv2:documentation """ + +The value of this property MUST be a valid lv2:Symbol, and MUST NOT have a +language tag. + +A symbol is a unique identifier with respect to the parent, for example a +port's symbol is a unique identifiers with respect to its plugin. The plugin +author MUST change the plugin URI if any port symbol is changed or removed. + +"""^^lv2:Markdown . + +lv2:name + lv2:documentation """ + +Unlike lv2:symbol, this is unrestricted, may be translated, and is not relevant +for compatibility. The name is not necessarily unique and MUST NOT be used as +an identifier. + +"""^^lv2:Markdown . + +lv2:shortName + lv2:documentation """ + +This is the same as lv2:name, with the additional requirement that the value is +shorter than 16 characters. + +"""^^lv2:Markdown . + +lv2:Designation + lv2:documentation """ + +A designation is metadata that describes the meaning or role of something. By +assigning a designation to a port using lv2:designation, the port's content +becomes meaningful and can be used more intelligently by the host. + +"""^^lv2:Markdown . + +lv2:Channel + lv2:documentation """ + +A specific channel, for example the left channel of a stereo stream. A +channel may be audio, or another type such as a MIDI control stream. + +"""^^lv2:Markdown . + +lv2:Parameter + lv2:documentation """ + +A parameter is a designation for a control. + +A parameter defines the meaning of a control, not the method of conveying its +value. For example, a parameter could be controlled via a lv2:ControlPort, +messages, or both. + +A lv2:ControlPort can be associated with a parameter using lv2:designation. + +"""^^lv2:Markdown . + +lv2:designation + lv2:documentation """ + +This property is used to give a port's contents a well-defined meaning. For +example, if a port has the designation `eg:gain`, then the value of that port +represents the `eg:gain` of the plugin instance. + +Ports should be given designations whenever possible, particularly if a +suitable designation is already defined. This allows the host to act more +intelligently and provide a more effective user interface. For example, if the +plugin has a BPM parameter, the host may automatically set that parameter to +the current tempo. + +"""^^lv2:Markdown . + +lv2:freeWheeling + lv2:documentation """ + +If true, this means that all processing is happening as quickly as possible, +not in real-time. When free-wheeling there is no relationship between the +passage of real wall-clock time and the passage of time in the data being +processed. + +"""^^lv2:Markdown . + +lv2:enabled + lv2:documentation """ + +If this value is greater than zero, the plugin processes normally. If this +value is zero, the plugin is expected to bypass all signals unmodified. The +plugin must provide a click-free transition between the enabled and disabled +(bypassed) states. + +Values less than zero are reserved for future use (such as click-free +insertion/removal of latent plugins), and should be treated like zero +(bypassed) by current implementations. + +"""^^lv2:Markdown . + +lv2:control + lv2:documentation """ + +This should be used as the lv2:designation of ports that are used to send +commands and receive responses. Typically this will be an event port that +supports some protocol, for example MIDI or LV2 Atoms. + +"""^^lv2:Markdown . + +lv2:Point + lv2:documentation """ + + * A Point MUST have at least one rdfs:label which is a string. + + * A Point MUST have exactly one rdf:value with a type that is compatible with + the type of the corresponding Port. + +"""^^lv2:Markdown . + +lv2:default + lv2:documentation """ + +The host SHOULD set the port to this value initially, and in any situation +where the port value should be cleared or reset. + +"""^^lv2:Markdown . + +lv2:minimum + lv2:documentation """ + +This is a soft limit: the plugin is required to gracefully accept all values in +the range of a port's data type. + +"""^^lv2:Markdown . + +lv2:maximum + lv2:documentation """ + +This is a soft limit: the plugin is required to gracefully accept all values in +the range of a port's data type. + +"""^^lv2:Markdown . + +lv2:optionalFeature + lv2:documentation """ + +To support this feature, the host MUST pass its URI and any additional data to +the plugin in LV2_Descriptor::instantiate(). + +The plugin MUST NOT fail to instantiate if an optional feature is not supported +by the host. + +"""^^lv2:Markdown . + +lv2:requiredFeature + lv2:documentation """ + +To support this feature, the host MUST pass its URI and any additional data to +the plugin in LV2_Descriptor::instantiate(). + +The host MUST check this property before attempting to instantiate a plugin, +and not attempt to instantiate plugins which require features it does not +support. The plugin MUST fail to instantiate if a required feature is not +supported by the host. Note that these rules are intentionally redundant for +resilience: neither host nor plugin should assume that the other does not +violate them. + +"""^^lv2:Markdown . + +lv2:ExtensionData + lv2:documentation """ + +This is additional data that a plugin may return from +LV2_Descriptor::extension_data(). This is generally used to add APIs to extend +that defined by LV2_Descriptor. + +"""^^lv2:Markdown . + +lv2:extensionData + lv2:documentation """ + +If a plugin has a value for this property, it must be a URI that defines the +extension data. The plugin should return the appropriate data when +LV2_Descriptor::extension_data() is called with that URI as a parameter. + +"""^^lv2:Markdown . + +lv2:isLive + lv2:documentation """ + +This feature is for plugins that have time-sensitive internals, for example +communicating in real time over a socket. It indicates to the host that its +input and output must not be cached or subject to significant latency, and that +calls to LV2_Descriptor::run() should be made at a rate that roughly +corresponds to wall clock time (according to the `sample_count` parameter). + +Note that this feature is not related to hard real-time execution +requirements (see lv2:hardRTCapable). + +"""^^lv2:Markdown . + +lv2:inPlaceBroken + lv2:documentation """ + +This feature indicates that the plugin may not work correctly if the host +elects to use the same data location for both input and output. Plugins that +will fail to work correctly if ANY input port is connected to the same location +as ANY output port MUST require this feature. Doing so should be avoided +whenever possible since it prevents hosts from running the plugin on data +in-place. + +"""^^lv2:Markdown . + +lv2:hardRTCapable + lv2:documentation """ + +This feature indicates that the plugin is capable of running in a hard +real-time environment. This should be the case for most audio processors, +so most plugins are expected to have this feature. + +To support this feature, plugins MUST adhere to the following in all of their +audio class functions (LV2_Descriptor::run() and +LV2_Descriptor::connect_port()): + + * There is no use of `malloc()`, `free()` or any other heap memory management + functions. + + * There is no use of any library functions which do not adhere to these + rules. The plugin may assume that the standard C math library functions + are safe. + + * There is no access to files, devices, pipes, sockets, system calls, or any + other mechanism that might result in the process or thread blocking. + + * The maximum amount of time for a `run()` call is bounded by some expression + of the form `A + B * sample_count`, where `A` and `B` are platform specific + constants. Note that this bound does not depend on input signals or plugin + state. + +"""^^lv2:Markdown . + +lv2:portProperty + lv2:documentation """ + +States that a port has a particular lv2:PortProperty. This may be ignored +without catastrophic effects, though it may be useful, for example to provide a +sensible user interface for the port. + +"""^^lv2:Markdown . + +lv2:connectionOptional + lv2:documentation """ + +This property means that the port does not have to be connected to valid data +by the host. To leave a port unconnected, the host MUST explicitly +connect the port to `NULL`. + +"""^^lv2:Markdown . + +lv2:reportsLatency + lv2:documentation """ + +This property indicates that the port is used to express the processing latency +incurred by the plugin, expressed in samples. The latency may be affected by +the current sample rate, plugin settings, or other factors, and may be changed +by the plugin at any time. Where the latency is frequency dependent the plugin +may choose any appropriate value. If a plugin introduces latency it MUST +provide EXACTLY ONE port with this property set. In fuzzy cases the +value should be the most reasonable one based on user expectation of +input/output alignment. For example, musical delay plugins should not report +their delay as latency, since it is an intentional effect that the host should +not compensate for. + +This property is deprecated, use a lv2:designation of lv2:latency instead, +following the same rules as above: + + :::turtle + + lv2:port [ + a lv2:OutputPort , lv2:ControlPort ; + lv2:designation lv2:latency ; + lv2:symbol "latency" ; + ] + +"""^^lv2:Markdown . + +lv2:toggled + lv2:documentation """ + +Indicates that the data item should be considered a boolean toggle. Data less +than or equal to zero should be considered off or false, and data +above zero should be considered on or true. + +"""^^lv2:Markdown . + +lv2:sampleRate + lv2:documentation """ + +Indicates that any specified bounds should be interpreted as multiples of the +sample rate. For example, a frequency range from 0 Hz to the Nyquist frequency +(half the sample rate) can be specified by using this property with lv2:minimum +0.0 and lv2:maximum 0.5. Hosts that support bounds at all MUST support this +property. + +"""^^lv2:Markdown . + +lv2:integer + lv2:documentation """ + +Indicates that all the reasonable values for a port are integers. For such +ports, a user interface should provide a stepped control that only allows +choosing integer values. + +Note that this is only a hint, and that the plugin MUST operate reasonably even +if such a port has a non-integer value. + +"""^^lv2:Markdown . + +lv2:enumeration + lv2:documentation """ + +Indicates that all the rasonable values for a port are defined by +lv2:scalePoint properties. For such ports, a user interface should provide a selector that allows the user to choose any of the scale point values by name. It is recommended to show the value as well if possible. + +Note that this is only a hint, and that the plugin MUST operate reasonably even +if such a port has a value that does not correspond to a scale point. + +"""^^lv2:Markdown . + +lv2:isSideChain + lv2:documentation """ + +Indicates that a port is a sidechain, which affects the output somehow +but should not be considered a part of the main signal chain. Sidechain ports +SHOULD be lv2:connectionOptional, and may be ignored by hosts. + +"""^^lv2:Markdown . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/core/lv2core.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/core/lv2core.ttl new file mode 100644 index 0000000000..f5836c6e4c --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/core/lv2core.ttl @@ -0,0 +1,674 @@ +@prefix doap: . +@prefix lv2: . +@prefix owl: . +@prefix rdf: . +@prefix rdfs: . +@prefix xsd: . + + + a owl:Ontology ; + rdfs:label "LV2" ; + rdfs:comment "An extensible open standard for audio plugins." ; + rdfs:seeAlso , + , + . + +lv2:Specification + a rdfs:Class , + owl:Class ; + rdfs:subClassOf doap:Project ; + rdfs:label "Specification" ; + rdfs:comment "An LV2 specifiation." . + +lv2:Markdown + a rdfs:Datatype ; + owl:onDatatype xsd:string ; + rdfs:label "Markdown" ; + rdfs:comment "A string in Markdown syntax." . + +lv2:documentation + a rdf:Property , + owl:AnnotationProperty ; + rdfs:range rdfs:Literal ; + rdfs:label "documentation" ; + rdfs:comment "Extended documentation." ; + rdfs:seeAlso . + +lv2:PluginBase + a rdfs:Class , + owl:Class ; + rdfs:label "Plugin Base" ; + rdfs:comment "Base class for a plugin-like resource." . + +lv2:Plugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:PluginBase ; + rdfs:label "Plugin" ; + rdfs:comment "An LV2 plugin." ; + rdfs:subClassOf [ + a owl:Restriction ; + owl:onProperty doap:name ; + owl:someValuesFrom rdf:PlainLiteral ; + rdfs:comment "A plugin MUST have at least one untranslated doap:name." + ] , [ + a owl:Restriction ; + owl:onProperty lv2:port ; + owl:allValuesFrom lv2:Port ; + rdfs:comment "All ports on a plugin MUST be fully specified lv2:Port instances." + ] . + +lv2:PortBase + a rdfs:Class , + owl:Class ; + rdfs:label "Port Base" ; + rdfs:comment "Base class for a port-like resource." ; + rdfs:subClassOf [ + a owl:Restriction ; + owl:onProperty lv2:symbol ; + owl:cardinality 1 ; + rdfs:comment "A port MUST have exactly one lv2:symbol." + ] . + +lv2:Port + a rdfs:Class , + owl:Class ; + rdfs:label "Port" ; + rdfs:comment "An LV2 plugin port." ; + rdfs:subClassOf lv2:PortBase , + [ + a owl:Restriction ; + owl:onProperty lv2:name ; + owl:minCardinality 1 ; + rdfs:comment "A port MUST have at least one lv2:name." + ] . + +lv2:InputPort + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:Port ; + rdfs:label "Input Port" ; + rdfs:comment "A port connected to constant data which is read during `run()`." . + +lv2:OutputPort + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:Port ; + rdfs:label "Output Port" ; + rdfs:comment "A port connected to data which is written during `run()`." . + +lv2:ControlPort + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:Port ; + rdfs:label "Control Port" ; + rdfs:comment "A port connected to a single `float`." . + +lv2:AudioPort + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:Port ; + rdfs:label "Audio Port" ; + rdfs:comment "A port connected to an array of float audio samples." . + +lv2:CVPort + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:Port ; + rdfs:label "CV Port" ; + rdfs:comment "A port connected to an array of float control values." . + +lv2:port + a rdf:Property , + owl:ObjectProperty ; + rdfs:domain lv2:PluginBase ; + rdfs:range lv2:PortBase ; + rdfs:label "port" ; + rdfs:comment "A port (input or output) on this plugin." . + +lv2:project + a rdf:Property , + owl:ObjectProperty ; + rdfs:range doap:Project ; + rdfs:label "project" ; + rdfs:comment "The project this is a part of." . + +lv2:prototype + a rdf:Property , + owl:ObjectProperty ; + rdfs:label "prototype" ; + rdfs:comment "The prototype to inherit properties from." . + +lv2:minorVersion + a rdf:Property , + owl:DatatypeProperty ; + rdfs:range xsd:nonNegativeInteger ; + rdfs:label "minor version" ; + rdfs:comment "The minor version of this resource." . + +lv2:microVersion + a rdf:Property , + owl:DatatypeProperty ; + rdfs:range xsd:nonNegativeInteger ; + rdfs:label "micro version" ; + rdfs:comment "The micro version of this resource." . + +lv2:binary + a rdf:Property , + owl:ObjectProperty ; + rdfs:range owl:Thing ; + rdfs:label "binary" ; + rdfs:comment "The binary of this resource." . + +lv2:appliesTo + a rdf:Property , + owl:ObjectProperty ; + rdfs:range lv2:Plugin ; + rdfs:label "applies to" ; + rdfs:comment "The plugin this resource is related to." . + +lv2:index + a rdf:Property , + owl:DatatypeProperty , + owl:FunctionalProperty ; + rdfs:range xsd:unsignedInt ; + rdfs:label "index" ; + rdfs:comment "A non-negative zero-based 32-bit index." . + +lv2:Symbol + a rdfs:Datatype ; + owl:onDatatype xsd:string ; + owl:withRestrictions ( + [ + xsd:pattern "[_a-zA-Z][_a-zA-Z0-9]*" + ] + ) ; + rdfs:label "Symbol" ; + rdfs:comment "A short restricted name used as a strong identifier." . + +lv2:symbol + a rdf:Property , + owl:DatatypeProperty , + owl:FunctionalProperty ; + rdfs:label "symbol" ; + rdfs:range lv2:Symbol , + rdf:PlainLiteral ; + rdfs:comment "The symbol that identifies this resource in the context of its parent." . + +lv2:name + a rdf:Property , + owl:DatatypeProperty ; + rdfs:label "name" ; + rdfs:range xsd:string ; + rdfs:comment "A display name for labeling in a user interface." . + +lv2:shortName + a rdf:Property , + owl:DatatypeProperty ; + rdfs:label "short name" ; + rdfs:range xsd:string ; + rdfs:comment "A short display name for labeling in a user interface." . + +lv2:Designation + a rdfs:Class , + owl:Class ; + rdfs:subClassOf rdf:Property ; + rdfs:label "Designation" ; + rdfs:comment "A designation which defines the meaning of some data." . + +lv2:Channel + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:Designation ; + rdfs:label "Channel" ; + rdfs:comment "An individual channel, such as left or right." . + +lv2:Parameter + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:Designation , + rdf:Property ; + rdfs:label "Parameter" ; + rdfs:comment "A property that is a plugin parameter." . + +lv2:designation + a rdf:Property , + owl:ObjectProperty , + owl:FunctionalProperty ; + rdfs:range rdf:Property ; + rdfs:label "designation" ; + rdfs:comment "The designation that defines the meaning of this input or output." . + +lv2:latency + a rdf:Property , + owl:DatatypeProperty ; + rdfs:range xsd:nonNegativeInteger ; + rdfs:label "latency" ; + rdfs:comment "The latency introduced, in frames." . + +lv2:freeWheeling + a rdf:Property , + owl:DatatypeProperty ; + rdfs:label "free-wheeling" ; + rdfs:range xsd:boolean ; + rdfs:comment "Whether processing is currently free-wheeling." . + +lv2:enabled + a rdf:Property , + owl:DatatypeProperty ; + rdfs:label "enabled" ; + rdfs:range xsd:int ; + rdfs:comment "Whether processing is currently enabled (not bypassed)." . + +lv2:control + a lv2:Channel ; + rdfs:label "control" ; + rdfs:comment "The primary control channel." . + +lv2:Point + a rdfs:Class , + owl:Class ; + rdfs:label "Point" ; + rdfs:comment "An interesting point in a value range." . + +lv2:ScalePoint + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:Point ; + rdfs:label "Scale Point" ; + rdfs:comment "A single `float` Point for control inputs." . + +lv2:scalePoint + a rdf:Property , + owl:ObjectProperty ; + rdfs:range lv2:ScalePoint ; + rdfs:label "scale point" ; + rdfs:comment "A scale point of a port or parameter." . + +lv2:default + a rdf:Property , + owl:DatatypeProperty , + owl:FunctionalProperty ; + rdfs:label "default" ; + rdfs:comment "The default value for this control." . + +lv2:minimum + a rdf:Property , + owl:DatatypeProperty , + owl:FunctionalProperty ; + rdfs:label "minimum" ; + rdfs:comment "The minimum value for this control." . + +lv2:maximum + a rdf:Property , + owl:DatatypeProperty , + owl:FunctionalProperty ; + rdfs:label "maximum" ; + rdfs:comment "The maximum value for this control." . + +lv2:Feature + a rdfs:Class , + owl:Class ; + rdfs:label "Feature" ; + rdfs:comment "An additional feature which may be used or required." . + +lv2:optionalFeature + a rdf:Property , + owl:ObjectProperty ; + rdfs:range lv2:Feature ; + rdfs:label "optional feature" ; + rdfs:comment "An optional feature that is supported if available." . + +lv2:requiredFeature + a rdf:Property , + owl:ObjectProperty ; + rdfs:range lv2:Feature ; + rdfs:label "required feature" ; + rdfs:comment "A required feature that must be available to run." . + +lv2:ExtensionData + a rdfs:Class , + owl:Class ; + rdfs:label "Extension Data" ; + rdfs:comment "Additional data defined by an extension." . + +lv2:extensionData + a rdf:Property , + owl:ObjectProperty ; + rdfs:range lv2:ExtensionData ; + rdfs:label "extension data" ; + rdfs:comment "Extension data provided by a plugin or other binary." . + +lv2:isLive + a lv2:Feature ; + rdfs:label "is live" ; + rdfs:comment "Plugin has a real-time dependency." . + +lv2:inPlaceBroken + a lv2:Feature ; + rdfs:label "in-place broken" ; + rdfs:comment "Plugin requires separate locations for input and output." . + +lv2:hardRTCapable + a lv2:Feature ; + rdfs:label "hard real-time capable" ; + rdfs:comment "Plugin is capable of running in a hard real-time environment." . + +lv2:PortProperty + a rdfs:Class , + owl:Class ; + rdfs:label "Port Property" ; + rdfs:comment "A particular property that a port has." . + +lv2:portProperty + a rdf:Property , + owl:ObjectProperty ; + rdfs:domain lv2:Port ; + rdfs:range lv2:PortProperty ; + rdfs:label "port property" ; + rdfs:comment "A property of this port hosts may find useful." . + +lv2:connectionOptional + a lv2:PortProperty ; + rdfs:label "connection optional" ; + rdfs:comment "The property that this port may be connected to NULL." . + +lv2:reportsLatency + a lv2:PortProperty ; + owl:deprecated "true"^^xsd:boolean ; + rdfs:label "reports latency" ; + rdfs:comment "Control port value is the plugin latency in frames." . + +lv2:toggled + a lv2:PortProperty ; + rdfs:label "toggled" ; + rdfs:comment "Control port value is considered a boolean toggle." . + +lv2:sampleRate + a lv2:PortProperty ; + rdfs:label "sample rate" ; + rdfs:comment "Control port bounds are interpreted as multiples of the sample rate." . + +lv2:integer + a lv2:PortProperty ; + rdfs:label "integer" ; + rdfs:comment "Control port values are treated as integers." . + +lv2:enumeration + a lv2:PortProperty ; + rdfs:label "enumeration" ; + rdfs:comment "Control port scale points represent all useful values." . + +lv2:isSideChain + a lv2:PortProperty ; + rdfs:label "is side-chain" ; + rdfs:comment "Signal for port should not be considered a main input or output." . + +lv2:GeneratorPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:Plugin ; + rdfs:label "Generator Plugin" ; + rdfs:comment "A plugin that generates new sound internally." . + +lv2:InstrumentPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:GeneratorPlugin ; + rdfs:label "Instrument Plugin" ; + rdfs:comment "A plugin intended to be played as a musical instrument." . + +lv2:OscillatorPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:GeneratorPlugin ; + rdfs:label "Oscillator Plugin" ; + rdfs:comment "A plugin that generates output with an oscillator." . + +lv2:UtilityPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:Plugin ; + rdfs:label "Utility Plugin" ; + rdfs:comment "A utility plugin that is not a typical audio effect or generator." . + +lv2:ConverterPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:UtilityPlugin ; + rdfs:label "Converter Plugin" ; + rdfs:comment "A plugin that converts its input into a different form." . + +lv2:AnalyserPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:UtilityPlugin ; + rdfs:label "Analyser Plugin" ; + rdfs:comment "A plugin that analyses its input and emits some useful information." . + +lv2:MixerPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:UtilityPlugin ; + rdfs:label "Mixer Plugin" ; + rdfs:comment "A plugin that mixes some number of inputs into some number of outputs." . + +lv2:SimulatorPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:Plugin ; + rdfs:label "Simulator Plugin" ; + rdfs:comment "A plugin that aims to emulate some environmental effect or musical equipment." . + +lv2:DelayPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:Plugin ; + rdfs:label "Delay Plugin" ; + rdfs:comment "An effect that intentionally delays its input as an effect." . + +lv2:ModulatorPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:Plugin ; + rdfs:label "Modulator Plugin" ; + rdfs:comment "An effect that modulats its input as an effect." . + +lv2:ReverbPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:Plugin , + lv2:SimulatorPlugin , + lv2:DelayPlugin ; + rdfs:label "Reverb Plugin" ; + rdfs:comment "An effect that adds reverberation to its input." . + +lv2:PhaserPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:ModulatorPlugin ; + rdfs:label "Phaser Plugin" ; + rdfs:comment "An effect that periodically sweeps a filter over its input." . + +lv2:FlangerPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:ModulatorPlugin ; + rdfs:label "Flanger Plugin" ; + rdfs:comment "An effect that mixes slightly delayed copies of its input." . + +lv2:ChorusPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:ModulatorPlugin ; + rdfs:label "Chorus Plugin" ; + rdfs:comment "An effect that mixes significantly delayed copies of its input." . + +lv2:FilterPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:Plugin ; + rdfs:label "Filter Plugin" ; + rdfs:comment "An effect that manipulates the frequency spectrum of its input." . + +lv2:LowpassPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:FilterPlugin ; + rdfs:label "Lowpass Filter Plugin" ; + rdfs:comment "A filter that attenuates frequencies above some cutoff." . + +lv2:BandpassPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:FilterPlugin ; + rdfs:label "Bandpass Filter Plugin" ; + rdfs:comment "A filter that attenuates frequencies outside of some band." . + +lv2:HighpassPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:FilterPlugin ; + rdfs:label "Highpass Filter Plugin" ; + rdfs:comment "A filter that attenuates frequencies below some cutoff." . + +lv2:CombPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:FilterPlugin ; + rdfs:label "Comb FilterPlugin" ; + rdfs:comment "A filter that adds a delayed version of its input to itself." . + +lv2:AllpassPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:FilterPlugin ; + rdfs:label "Allpass Plugin" ; + rdfs:comment "A filter that changes the phase relationship between frequency components." . + +lv2:EQPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:FilterPlugin ; + rdfs:label "Equaliser Plugin" ; + rdfs:comment "A plugin that adjusts the balance between frequency components." . + +lv2:ParaEQPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:EQPlugin ; + rdfs:label "Parametric EQ Plugin" ; + rdfs:comment "A plugin that adjusts the balance between configurable frequency components." . + +lv2:MultiEQPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:EQPlugin ; + rdfs:label "Multiband EQ Plugin" ; + rdfs:comment "A plugin that adjusts the balance between a fixed set of frequency components." . + +lv2:SpatialPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:Plugin ; + rdfs:label "Spatial Plugin" ; + rdfs:comment "A plugin that manipulates the position of audio in space." . + +lv2:SpectralPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:Plugin ; + rdfs:label "Spectral Plugin" ; + rdfs:comment "A plugin that alters the spectral properties of audio." . + +lv2:PitchPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:SpectralPlugin ; + rdfs:label "Pitch Shifter Plugin" ; + rdfs:comment "A plugin that shifts the pitch of its input." . + +lv2:AmplifierPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:DynamicsPlugin ; + rdfs:label "Amplifier Plugin" ; + rdfs:comment "A plugin that primarily changes the volume of its input." . + +lv2:EnvelopePlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:DynamicsPlugin ; + rdfs:label "Envelope Plugin" ; + rdfs:comment "A plugin that applies an envelope to its input." . + +lv2:DistortionPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:Plugin ; + rdfs:label "Distortion Plugin" ; + rdfs:comment "A plugin that adds distortion to its input." . + +lv2:WaveshaperPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:DistortionPlugin ; + rdfs:label "Waveshaper Plugin" ; + rdfs:comment "An effect that alters the shape of input waveforms." . + +lv2:DynamicsPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:Plugin ; + rdfs:label "Dynamics Plugin" ; + rdfs:comment "A plugin that alters the envelope or dynamic range of its input." . + +lv2:CompressorPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:DynamicsPlugin ; + rdfs:label "Compressor Plugin" ; + rdfs:comment "A plugin that reduces the dynamic range of its input." . + +lv2:ExpanderPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:DynamicsPlugin ; + rdfs:label "Expander Plugin" ; + rdfs:comment "A plugin that expands the dynamic range of its input." . + +lv2:LimiterPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:DynamicsPlugin ; + rdfs:label "Limiter Plugin" ; + rdfs:comment "A plugin that limits its input to some maximum level." . + +lv2:GatePlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:DynamicsPlugin ; + rdfs:label "Gate Plugin" ; + rdfs:comment "A plugin that attenuates signals below some threshold." . + +lv2:FunctionPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:UtilityPlugin ; + rdfs:label "Function Plugin" ; + rdfs:comment "A plugin whose output is a mathmatical function of its input." . + +lv2:ConstantPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:GeneratorPlugin ; + rdfs:label "Constant Plugin" ; + rdfs:comment "A plugin that emits constant values." . + +lv2:MIDIPlugin + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:Plugin ; + rdfs:label "MIDI Plugin" ; + rdfs:comment "A plugin that primarily processes MIDI messages." . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/core/manifest.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/core/manifest.ttl new file mode 100644 index 0000000000..7f1512bf5a --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/core/manifest.ttl @@ -0,0 +1,15 @@ +@prefix doap: . +@prefix lv2: . +@prefix rdfs: . + + + a lv2:Specification ; + lv2:minorVersion 18 ; + lv2:microVersion 0 ; + rdfs:seeAlso . + + + a doap:Project ; + rdfs:seeAlso , + . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/core/meta.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/core/meta.ttl new file mode 100644 index 0000000000..b626a0c99d --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/core/meta.ttl @@ -0,0 +1,199 @@ +@prefix dcs: . +@prefix doap: . +@prefix lv2: . +@prefix meta: . +@prefix rdf: . +@prefix rdfs: . + + + rdf:value """ +Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. + +THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +""" . + + + a doap:Project ; + lv2:symbol "lv2" ; + rdfs:label "LV2" ; + rdfs:comment "The LV2 Plugin Interface Project." ; + doap:name "LV2" ; + doap:license ; + doap:shortdesc "The LV2 Plugin Interface Project." ; + doap:description "LV2 is a plugin standard for audio systems. It defines a minimal yet extensible C API for plugin code and a format for plugin bundles" ; + doap:created "2006-05-10" ; + doap:homepage ; + doap:mailing-list ; + doap:programming-language "C" ; + doap:repository [ + a doap:SVNRepository ; + doap:location + ] ; + doap:developer , + ; + doap:helper meta:larsl , + meta:bmwiedemann , + meta:gabrbedd , + meta:daste , + meta:kfoltman , + meta:paniq ; + doap:release [ + doap:revision "1.18.2" ; + doap:created "2021-01-07" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "eg-sampler: Save and restore gain parameter value." + ] , [ + rdfs:label "Various code cleanups and infrastructure improvements." + ] + ] + ] , [ + doap:revision "1.18.0" ; + doap:created "2020-04-26" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Improve documentation." + ] , [ + rdfs:label "Separate extended documentation from primary data." + ] + ] + ] , [ + doap:revision "1.16.0" ; + doap:created "2019-02-03" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add core/attributes.h utility header." + ] , [ + rdfs:label "eg-sampler: Add waveform display to UI." + ] , [ + rdfs:label "eg-midigate: Respond to \"all notes off\" MIDI message." + ] , [ + rdfs:label "Simplify use of lv2specgen." + ] , [ + rdfs:label "Add lv2_validate utility." + ] , [ + rdfs:label "Install headers to simpler paths." + ] , [ + rdfs:label "Aggressively deprecate uri-map and event extensions." + ] , [ + rdfs:label "Upgrade build system and fix building with Python 3.7." + ] + ] + ] , [ + doap:revision "1.14.0" ; + doap:created "2016-09-19" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label """eg-scope: Don't feed back UI state updates.""" + ] , [ + rdfs:label "eg-sampler: Fix handling of state file paths." + ] , [ + rdfs:label "eg-sampler: Support thread-safe state restoration." + ] + ] + ] , [ + doap:revision "1.12.0" ; + doap:created "2015-04-07" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "eg-sampler: Support patch:Get, and request initial state from UI." + ] , [ + rdfs:label "eg-sampler: Add gain parameter." + ] , [ + rdfs:label "Fix merging of version histories in specification documentation." + ] , [ + rdfs:label "Improve API documentation." + ] , [ + rdfs:label "Simplify property restrictions by removing redundancy." + ] + ] + ] , [ + doap:revision "1.10.0" ; + doap:created "2014-08-08" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "lv2specgen: Display deprecated warning on classes marked owl:deprecated." + ] , [ + rdfs:label "Fix -Wconversion warnings in headers." + ] , [ + rdfs:label "Upgrade to waf 1.7.16." + ] + ] + ] , [ + doap:revision "1.8.0" ; + doap:created "2014-01-04" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add scope example plugin from Robin Gareus." + ] , [ + rdfs:label "lv2specgen: Fix links to externally defined terms." + ] , [ + rdfs:label "Install lv2specgen for use by other projects." + ] + ] + ] , [ + doap:revision "1.6.0" ; + doap:created "2013-08-09" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Fix port indices of metronome example." + ] , [ + rdfs:label "Fix lv2specgen usage from command line." + ] , [ + rdfs:label "Upgrade to waf 1.7.11." + ] + ] + ] , [ + doap:revision "1.4.0" ; + doap:created "2013-02-17" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add metronome example plugin to demonstrate sample accurate tempo sync." + ] , [ + rdfs:label "Generate book-style HTML documentation from example plugins." + ] + ] + ] , [ + doap:revision "1.2.0" ; + doap:created "2012-10-14" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Move all project metadata for extensions (e.g. change log) to separate files to spare hosts from loading them during discovery." + ] , [ + rdfs:label "Use stricter datatype definitions conformant with the XSD and OWL specifications for better validation." + ] + ] + ] , [ + doap:revision "1.0.0" ; + doap:created "2012-04-16" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label """Initial release as a unified project. Projects can now simply depend on the pkg-config package 'lv2' for all official LV2 APIs.""" + ] , [ + rdfs:label "New extensions: atom, log, parameters, patch, port-groups, port-props, resize-port, state, time, worker." + ] + ] + ] . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/core/people.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/core/people.ttl new file mode 100644 index 0000000000..52d0384e39 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/core/people.ttl @@ -0,0 +1,51 @@ +@prefix foaf: . +@prefix meta: . +@prefix rdfs: . + + + a foaf:Person ; + foaf:name "David Robillard" ; + foaf:mbox ; + rdfs:seeAlso . + + + a foaf:Person ; + foaf:name "Steve Harris" ; + foaf:mbox ; + rdfs:seeAlso . + +meta:larsl + a foaf:Person ; + foaf:name "Lars Luthman" ; + foaf:mbox . + +meta:gabrbedd + a foaf:Person ; + foaf:name "Gabriel M. Beddingfield" ; + foaf:mbox . + +meta:daste + a foaf:Person ; + foaf:name """Stefano D'Angelo""" ; + foaf:mbox . + +meta:kfoltman + a foaf:Person ; + foaf:name "Krzysztof Foltman" ; + foaf:mbox . + +meta:paniq + a foaf:Person ; + foaf:name "Leonard Ritter" ; + foaf:mbox . + +meta:harry + a foaf:Person ; + foaf:name "Harry van Haaren" ; + foaf:mbox . + +meta:bmwiedemann + a foaf:Person ; + foaf:name "Bernhard M. Wiedemann" ; + foaf:mbox . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/data-access/data-access.h b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/data-access/data-access.h new file mode 100644 index 0000000000..de3b6b65b0 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/data-access/data-access.h @@ -0,0 +1,73 @@ +/* + Copyright 2008-2016 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef LV2_DATA_ACCESS_H +#define LV2_DATA_ACCESS_H + +/** + @defgroup data-access Data Access + @ingroup lv2 + + Access to plugin extension_data() for UIs. + + See for details. + + @{ +*/ + +// clang-format off + +#define LV2_DATA_ACCESS_URI "http://lv2plug.in/ns/ext/data-access" ///< http://lv2plug.in/ns/ext/data-access +#define LV2_DATA_ACCESS_PREFIX LV2_DATA_ACCESS_URI "#" ///< http://lv2plug.in/ns/ext/data-access# + +// clang-format on + +#ifdef __cplusplus +extern "C" { +#endif + +/** + The data field of the LV2_Feature for this extension. + + To support this feature the host must pass an LV2_Feature struct to the + instantiate method with URI "http://lv2plug.in/ns/ext/data-access" + and data pointed to an instance of this struct. +*/ +typedef struct { + /** + A pointer to a method the UI can call to get data (of a type specified + by some other extension) from the plugin. + + This call never is never guaranteed to return anything, UIs should + degrade gracefully if direct access to the plugin data is not possible + (in which case this function will return NULL). + + This is for access to large data that can only possibly work if the UI + and plugin are running in the same process. For all other things, use + the normal LV2 UI communication system. + */ + const void* (*data_access)(const char* uri); +} LV2_Extension_Data_Feature; + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +/** + @} +*/ + +#endif /* LV2_DATA_ACCESS_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/data-access/data-access.meta.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/data-access/data-access.meta.ttl new file mode 100644 index 0000000000..3184110c8d --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/data-access/data-access.meta.ttl @@ -0,0 +1,77 @@ +@prefix da: . +@prefix dcs: . +@prefix doap: . +@prefix foaf: . +@prefix lv2: . +@prefix rdfs: . + + + a doap:Project ; + rdfs:seeAlso ; + doap:license ; + doap:name "LV2 Data Access" ; + doap:shortdesc "Provides access to plugin extension data." ; + doap:created "2008-00-00" ; + doap:developer ; + doap:release [ + doap:revision "1.6" ; + doap:created "2012-04-17" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Merge with unified LV2 package." + ] + ] + ] , [ + doap:revision "1.4" ; + doap:created "2011-11-21" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Update packaging." + ] , [ + rdfs:label "Improve documentation." + ] + ] + ] , [ + doap:revision "1.2" ; + doap:created "2011-05-26" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add build system for installation." + ] , [ + rdfs:label "Switch to ISC license." + ] + ] + ] , [ + doap:revision "1.0" ; + doap:created "2010-10-04" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Initial release." + ] + ] + ] ; + lv2:documentation """ + +This extension defines a feature, LV2_Extension_Data_Feature, which provides +access to LV2_Descriptor::extension_data() for plugin UIs or other potentially +remote users of a plugin. + +Note that the use of this extension by UIs violates the important principle of +UI/plugin separation, and is potentially a source of many problems. +Accordingly, **use of this extension is highly discouraged**, and plugins +should not expect hosts to support it, since it is often impossible to do so. + +To support this feature the host must pass an LV2_Feature struct to +LV2_Descriptor::extension_data() with URI LV2_DATA_ACCESS_URI and data pointed +to an instance of LV2_Extension_Data_Feature. + +"""^^lv2:Markdown . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/data-access/data-access.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/data-access/data-access.ttl new file mode 100644 index 0000000000..0185cd0d7f --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/data-access/data-access.ttl @@ -0,0 +1,11 @@ +@prefix da: . +@prefix lv2: . +@prefix rdfs: . + + + a lv2:Feature ; + rdfs:label "data access" ; + rdfs:comment "A feature that provides access to plugin extension data." ; + rdfs:seeAlso , + . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/data-access/manifest.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/data-access/manifest.ttl new file mode 100644 index 0000000000..9585a5e42a --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/data-access/manifest.ttl @@ -0,0 +1,9 @@ +@prefix lv2: . +@prefix rdfs: . + + + a lv2:Specification ; + lv2:minorVersion 1 ; + lv2:microVersion 6 ; + rdfs:seeAlso . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/dynmanifest/dynmanifest.h b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/dynmanifest/dynmanifest.h new file mode 100644 index 0000000000..6181f71e00 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/dynmanifest/dynmanifest.h @@ -0,0 +1,160 @@ +/* + Dynamic manifest specification for LV2 + Copyright 2008-2011 Stefano D'Angelo + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef LV2_DYN_MANIFEST_H_INCLUDED +#define LV2_DYN_MANIFEST_H_INCLUDED + +/** + @defgroup dynmanifest Dynamic Manifest + @ingroup lv2 + + Support for dynamic data generation. + + See for details. + + @{ +*/ + +#include "lv2/core/lv2.h" + +#include + +// clang-format off + +#define LV2_DYN_MANIFEST_URI "http://lv2plug.in/ns/ext/dynmanifest" ///< http://lv2plug.in/ns/ext/dynmanifest +#define LV2_DYN_MANIFEST_PREFIX LV2_DYN_MANIFEST_URI "#" ///< http://lv2plug.in/ns/ext/dynmanifest# + +// clang-format on + +#ifdef __cplusplus +extern "C" { +#endif + +/** + Dynamic manifest generator handle. + + This handle indicates a particular status of a dynamic manifest generator. + The host MUST NOT attempt to interpret it and, unlikely LV2_Handle, it is + NOT even valid to compare this to NULL. The dynamic manifest generator MAY + use it to reference internal data. +*/ +typedef void* LV2_Dyn_Manifest_Handle; + +/** + Generate the dynamic manifest. + + @param handle Pointer to an uninitialized dynamic manifest generator handle. + + @param features NULL terminated array of LV2_Feature structs which represent + the features the host supports. The dynamic manifest generator may refuse to + (re)generate the dynamic manifest if required features are not found here + (however hosts SHOULD NOT use this as a discovery mechanism, instead of + reading the static manifest file). This array must always exist; if a host + has no features, it MUST pass a single element array containing NULL. + + @return 0 on success, otherwise a non-zero error code. The host SHOULD + evaluate the result of the operation by examining the returned value and + MUST NOT try to interpret the value of handle. +*/ +int +lv2_dyn_manifest_open(LV2_Dyn_Manifest_Handle* handle, + const LV2_Feature* const* features); + +/** + Fetch a "list" of subject URIs described in the dynamic manifest. + + The dynamic manifest generator has to fill the resource only with the needed + triples to make the host aware of the "objects" it wants to expose. For + example, if the plugin library exposes a regular LV2 plugin, it should + output only a triple like the following: + + a lv2:Plugin . + + The objects that are elegible for exposure are those that would need to be + represented by a subject node in a static manifest. + + @param handle Dynamic manifest generator handle. + + @param fp FILE * identifying the resource the host has to set up for the + dynamic manifest generator. The host MUST pass a writable, empty resource to + this function, and the dynamic manifest generator MUST ONLY perform write + operations on it at the end of the stream (for example, using only + fprintf(), fwrite() and similar). + + @return 0 on success, otherwise a non-zero error code. +*/ +int +lv2_dyn_manifest_get_subjects(LV2_Dyn_Manifest_Handle handle, FILE* fp); + +/** + Function that fetches data related to a specific URI. + + The dynamic manifest generator has to fill the resource with data related to + object represented by the given URI. For example, if the library exposes a + regular LV2 plugin whose URI, as retrieved by the host using + lv2_dyn_manifest_get_subjects() is http://example.org/plugin then it + should output something like: + +
+   
+       a lv2:Plugin ;
+       doap:name "My Plugin" ;
+       lv2:binary  ;
+       etc:etc "..." .
+   
+ + @param handle Dynamic manifest generator handle. + + @param fp FILE * identifying the resource the host has to set up for the + dynamic manifest generator. The host MUST pass a writable resource to this + function, and the dynamic manifest generator MUST ONLY perform write + operations on it at the current position of the stream (for example, using + only fprintf(), fwrite() and similar). + + @param uri URI to get data about (in the "plain" form, i.e., absolute URI + without Turtle prefixes). + + @return 0 on success, otherwise a non-zero error code. +*/ +int +lv2_dyn_manifest_get_data(LV2_Dyn_Manifest_Handle handle, + FILE* fp, + const char* uri); + +/** + Function that ends the operations on the dynamic manifest generator. + + This function SHOULD be used by the dynamic manifest generator to perform + cleanup operations, etc. + + Once this function is called, referring to handle will cause undefined + behavior. + + @param handle Dynamic manifest generator handle. +*/ +void +lv2_dyn_manifest_close(LV2_Dyn_Manifest_Handle handle); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +/** + @} +*/ + +#endif /* LV2_DYN_MANIFEST_H_INCLUDED */ diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/dynmanifest/dynmanifest.meta.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/dynmanifest/dynmanifest.meta.ttl new file mode 100644 index 0000000000..4e66d98a1b --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/dynmanifest/dynmanifest.meta.ttl @@ -0,0 +1,131 @@ +@prefix dcs: . +@prefix dman: . +@prefix doap: . +@prefix foaf: . +@prefix lv2: . +@prefix rdfs: . + + + a doap:Project ; + doap:license ; + doap:name "LV2 Dynamic Manifest" ; + doap:homepage ; + doap:created "2009-06-13" ; + doap:shortdesc "Support for dynamic manifest data generation." ; + doap:programming-language "C" ; + doap:developer ; + doap:release [ + doap:revision "1.6" ; + doap:created "2012-10-14" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Use consistent label style." + ] + ] + ] , [ + doap:revision "1.4" ; + doap:created "2012-04-17" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Merge with unified LV2 package." + ] + ] + ] , [ + doap:revision "1.2" ; + doap:created "2011-11-21" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Improve documentation." + ] + ] + ] , [ + doap:revision "1.0" ; + doap:created "2010-04-10" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Initial release." + ] + ] + ] ; + lv2:documentation """ + +The LV2 API, on its own, cannot be used to write plugin libraries where data is +dynamically generated at runtime, since LV2 requires needed information to be +provided in one or more static data (RDF) files. This API addresses this +limitation by extending the LV2 API. + +To detect that a plugin library implements a dynamic manifest generator, the +host checks its static manifest for a description like: + + :::turtle + + a dman:DynManifest ; + lv2:binary . + +To load the data, the host loads the library (`mydynmanifest.so` in this +example) as usual and fetches the dynamic Turtle data from it using this API. + +The host is allowed to request regeneration of the dynamic manifest multiple +times, and the plugin library is expected to provide updated data if/when +possible. All data and references provided via this API before the last +regeneration of the dynamic manifest is to be considered invalid by the host, +including plugin descriptors whose URIs were discovered using this API. + +### Accessing Data + +To access data using this API, the host must: + + 1. Call lv2_dyn_manifest_open(). + + 2. Create a `FILE` for functions to write data to (for example with `tmpfile()`). + + 3. Get a list of exposed subject URIs using lv2_dyn_manifest_get_subjects(). + + 4. Call lv2_dyn_manifest_get_data() for each URI of interest to write the + related data to the file. + + 5. Call lv2_dyn_manifest_close(). + + 6. Parse the content of the file(s). + + 7. Remove the file(s). + +Each call to the above mentioned dynamic manifest functions MUST write a +complete, valid Turtle document (including all needed prefix definitions) to +the output FILE. + +Each call to lv2_dyn_manifest_open() causes the (re)generation of the dynamic +manifest data, and invalidates all data fetched before the call. + +In case the plugin library uses this same API to access other dynamic +manifests, it MUST implement some mechanism to avoid potentially endless loops +(such as A loads B, B loads A, etc.) and, in case such a loop is detected, the +operation MUST fail. For this purpose, use of a static boolean flag is +suggested. + +### Threading Rules + +All of the functions defined by this specification belong to the Discovery +class. + + +"""^^lv2:Markdown . + +dman:DynManifest + lv2:documentation """ + +There MUST NOT be any instances of dman:DynManifest in the generated manifest. + +All relative URIs in the generated data MUST be relative to the base path that +would be used to parse a normal LV2 manifest (the bundle path). + +"""^^lv2:Markdown . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/dynmanifest/dynmanifest.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/dynmanifest/dynmanifest.ttl new file mode 100644 index 0000000000..4e2e8309f8 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/dynmanifest/dynmanifest.ttl @@ -0,0 +1,25 @@ +@prefix dman: . +@prefix lv2: . +@prefix owl: . +@prefix rdf: . +@prefix rdfs: . +@prefix xsd: . + + + a owl:Ontology ; + rdfs:label "LV2 Dyn Manifest" ; + rdfs:comment "Support for dynamic manifest data generation." ; + rdfs:seeAlso , + . + +dman:DynManifest + a rdfs:Class ; + rdfs:label "Dynamic Manifest" ; + rdfs:subClassOf [ + a owl:Restriction ; + owl:onProperty lv2:binary ; + owl:minCardinality 1 ; + rdfs:comment "A DynManifest MUST have at least one lv2:binary." + ] ; + rdfs:comment "Dynamic manifest for an LV2 binary." . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/dynmanifest/manifest.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/dynmanifest/manifest.ttl new file mode 100644 index 0000000000..db27a731ab --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/dynmanifest/manifest.ttl @@ -0,0 +1,9 @@ +@prefix lv2: . +@prefix rdfs: . + + + a lv2:Specification ; + lv2:minorVersion 1 ; + lv2:microVersion 6 ; + rdfs:seeAlso . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/event/event-helpers.h b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/event/event-helpers.h new file mode 100644 index 0000000000..1e19e5826d --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/event/event-helpers.h @@ -0,0 +1,255 @@ +/* + Copyright 2008-2015 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef LV2_EVENT_HELPERS_H +#define LV2_EVENT_HELPERS_H + +/** + @file event-helpers.h Helper functions for the LV2 Event extension + . +*/ + +#include "lv2/core/attributes.h" +#include "lv2/event/event.h" + +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +LV2_DISABLE_DEPRECATION_WARNINGS + +/** @file + * Helper functions for the LV2 Event extension + * . + * + * These functions are provided for convenience only, use of them is not + * required for supporting lv2ev (i.e. the events extension is defined by the + * raw buffer format described in lv2_event.h and NOT by this API). + * + * Note that these functions are all static inline which basically means: + * do not take the address of these functions. */ + +/** Pad a size to 64 bits (for event sizes) */ +static inline uint16_t +lv2_event_pad_size(uint16_t size) +{ + return (uint16_t)(size + 7U) & (uint16_t)(~7U); +} + +/** Initialize (empty, reset..) an existing event buffer. + * The contents of buf are ignored entirely and overwritten, except capacity + * which is unmodified. */ +static inline void +lv2_event_buffer_reset(LV2_Event_Buffer* buf, + uint16_t stamp_type, + uint8_t* data) +{ + buf->data = data; + buf->header_size = sizeof(LV2_Event_Buffer); + buf->stamp_type = stamp_type; + buf->event_count = 0; + buf->size = 0; +} + +/** Allocate a new, empty event buffer. */ +static inline LV2_Event_Buffer* +lv2_event_buffer_new(uint32_t capacity, uint16_t stamp_type) +{ + const size_t size = sizeof(LV2_Event_Buffer) + capacity; + LV2_Event_Buffer* buf = (LV2_Event_Buffer*)malloc(size); + if (buf != NULL) { + buf->capacity = capacity; + lv2_event_buffer_reset(buf, stamp_type, (uint8_t*)(buf + 1)); + return buf; + } + return NULL; +} + +/** An iterator over an LV2_Event_Buffer. + * + * Multiple simultaneous read iterators over a single buffer is fine, + * but changing the buffer invalidates all iterators. */ +typedef struct { + LV2_Event_Buffer* buf; + uint32_t offset; +} LV2_Event_Iterator; + +/** Reset an iterator to point to the start of `buf`. + * @return True if `iter` is valid, otherwise false (buffer is empty) */ +static inline bool +lv2_event_begin(LV2_Event_Iterator* iter, LV2_Event_Buffer* buf) +{ + iter->buf = buf; + iter->offset = 0; + return (buf->size > 0); +} + +/** Check if `iter` is valid. + * @return True if `iter` is valid, otherwise false (past end of buffer) */ +static inline bool +lv2_event_is_valid(LV2_Event_Iterator* iter) +{ + return (iter->buf && (iter->offset < iter->buf->size)); +} + +/** Advance `iter` forward one event. + * `iter` must be valid. + * @return True if `iter` is valid, otherwise false (reached end of buffer) */ +static inline bool +lv2_event_increment(LV2_Event_Iterator* iter) +{ + if (!lv2_event_is_valid(iter)) { + return false; + } + + LV2_Event* const ev = (LV2_Event*)(iter->buf->data + iter->offset); + + iter->offset += + lv2_event_pad_size((uint16_t)((uint16_t)sizeof(LV2_Event) + ev->size)); + + return true; +} + +/** Dereference an event iterator (get the event currently pointed at). + * `iter` must be valid. + * `data` if non-NULL, will be set to point to the contents of the event + * returned. + * @return A Pointer to the event `iter` is currently pointing at, or NULL + * if the end of the buffer is reached (in which case `data` is + * also set to NULL). */ +static inline LV2_Event* +lv2_event_get(LV2_Event_Iterator* iter, uint8_t** data) +{ + if (!lv2_event_is_valid(iter)) { + return NULL; + } + + LV2_Event* const ev = (LV2_Event*)(iter->buf->data + iter->offset); + + if (data) { + *data = (uint8_t*)ev + sizeof(LV2_Event); + } + + return ev; +} + +/** Write an event at `iter`. + * The event (if any) pointed to by `iter` will be overwritten, and `iter` + * incremented to point to the following event (i.e. several calls to this + * function can be done in sequence without twiddling iter in-between). + * @return True if event was written, otherwise false (buffer is full). */ +static inline bool +lv2_event_write(LV2_Event_Iterator* iter, + uint32_t frames, + uint32_t subframes, + uint16_t type, + uint16_t size, + const uint8_t* data) +{ + if (!iter->buf) { + return false; + } + + if (iter->buf->capacity - iter->buf->size < sizeof(LV2_Event) + size) { + return false; + } + + LV2_Event* const ev = (LV2_Event*)(iter->buf->data + iter->offset); + + ev->frames = frames; + ev->subframes = subframes; + ev->type = type; + ev->size = size; + memcpy((uint8_t*)ev + sizeof(LV2_Event), data, size); + ++iter->buf->event_count; + + size = lv2_event_pad_size((uint16_t)(sizeof(LV2_Event) + size)); + iter->buf->size += size; + iter->offset += size; + + return true; +} + +/** Reserve space for an event in the buffer and return a pointer to + the memory where the caller can write the event data, or NULL if there + is not enough room in the buffer. */ +static inline uint8_t* +lv2_event_reserve(LV2_Event_Iterator* iter, + uint32_t frames, + uint32_t subframes, + uint16_t type, + uint16_t size) +{ + const uint16_t total_size = (uint16_t)(sizeof(LV2_Event) + size); + if (iter->buf->capacity - iter->buf->size < total_size) { + return NULL; + } + + LV2_Event* const ev = (LV2_Event*)(iter->buf->data + iter->offset); + + ev->frames = frames; + ev->subframes = subframes; + ev->type = type; + ev->size = size; + ++iter->buf->event_count; + + const uint16_t padded_size = lv2_event_pad_size(total_size); + iter->buf->size += padded_size; + iter->offset += padded_size; + + return (uint8_t*)ev + sizeof(LV2_Event); +} + +/** Write an event at `iter`. + * The event (if any) pointed to by `iter` will be overwritten, and `iter` + * incremented to point to the following event (i.e. several calls to this + * function can be done in sequence without twiddling iter in-between). + * @return True if event was written, otherwise false (buffer is full). */ +static inline bool +lv2_event_write_event(LV2_Event_Iterator* iter, + const LV2_Event* ev, + const uint8_t* data) +{ + const uint16_t total_size = (uint16_t)(sizeof(LV2_Event) + ev->size); + if (iter->buf->capacity - iter->buf->size < total_size) { + return false; + } + + LV2_Event* const write_ev = (LV2_Event*)(iter->buf->data + iter->offset); + + *write_ev = *ev; + memcpy((uint8_t*)write_ev + sizeof(LV2_Event), data, ev->size); + ++iter->buf->event_count; + + const uint16_t padded_size = lv2_event_pad_size(total_size); + iter->buf->size += padded_size; + iter->offset += padded_size; + + return true; +} + +LV2_RESTORE_WARNINGS + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* LV2_EVENT_HELPERS_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/event/event.h b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/event/event.h new file mode 100644 index 0000000000..d55d5cf47d --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/event/event.h @@ -0,0 +1,306 @@ +/* + Copyright 2008-2016 David Robillard + Copyright 2006-2007 Lars Luthman + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef LV2_EVENT_H +#define LV2_EVENT_H + +/** + @defgroup event Event + @ingroup lv2 + + Generic time-stamped events. + + See for details. + + @{ +*/ + +// clang-format off + +#define LV2_EVENT_URI "http://lv2plug.in/ns/ext/event" ///< http://lv2plug.in/ns/ext/event +#define LV2_EVENT_PREFIX LV2_EVENT_URI "#" ///< http://lv2plug.in/ns/ext/event# + +#define LV2_EVENT__Event LV2_EVENT_PREFIX "Event" ///< http://lv2plug.in/ns/ext/event#Event +#define LV2_EVENT__EventPort LV2_EVENT_PREFIX "EventPort" ///< http://lv2plug.in/ns/ext/event#EventPort +#define LV2_EVENT__FrameStamp LV2_EVENT_PREFIX "FrameStamp" ///< http://lv2plug.in/ns/ext/event#FrameStamp +#define LV2_EVENT__TimeStamp LV2_EVENT_PREFIX "TimeStamp" ///< http://lv2plug.in/ns/ext/event#TimeStamp +#define LV2_EVENT__generatesTimeStamp LV2_EVENT_PREFIX "generatesTimeStamp" ///< http://lv2plug.in/ns/ext/event#generatesTimeStamp +#define LV2_EVENT__generic LV2_EVENT_PREFIX "generic" ///< http://lv2plug.in/ns/ext/event#generic +#define LV2_EVENT__inheritsEvent LV2_EVENT_PREFIX "inheritsEvent" ///< http://lv2plug.in/ns/ext/event#inheritsEvent +#define LV2_EVENT__inheritsTimeStamp LV2_EVENT_PREFIX "inheritsTimeStamp" ///< http://lv2plug.in/ns/ext/event#inheritsTimeStamp +#define LV2_EVENT__supportsEvent LV2_EVENT_PREFIX "supportsEvent" ///< http://lv2plug.in/ns/ext/event#supportsEvent +#define LV2_EVENT__supportsTimeStamp LV2_EVENT_PREFIX "supportsTimeStamp" ///< http://lv2plug.in/ns/ext/event#supportsTimeStamp + +// clang-format on + +#define LV2_EVENT_AUDIO_STAMP 0 ///< Special timestamp type for audio frames + +#include "lv2/core/attributes.h" + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +LV2_DISABLE_DEPRECATION_WARNINGS + +/** + The best Pulses Per Quarter Note for tempo-based uint32_t timestamps. + Equal to 2^12 * 5 * 7 * 9 * 11 * 13 * 17, which is evenly divisble + by all integers from 1 through 18 inclusive, and powers of 2 up to 2^12. +*/ +LV2_DEPRECATED +static const uint32_t LV2_EVENT_PPQN = 3136573440U; + +/** + An LV2 event (header only). + + LV2 events are generic time-stamped containers for any type of event. + The type field defines the format of a given event's contents. + + This struct defines the header of an LV2 event. An LV2 event is a single + chunk of POD (plain old data), usually contained in a flat buffer (see + LV2_EventBuffer below). Unless a required feature says otherwise, hosts may + assume a deep copy of an LV2 event can be created safely using a simple: + + memcpy(ev_copy, ev, sizeof(LV2_Event) + ev->size); (or equivalent) +*/ +LV2_DEPRECATED +typedef struct { + /** + The frames portion of timestamp. The units used here can optionally be + set for a port (with the lv2ev:timeUnits property), otherwise this is + audio frames, corresponding to the sample_count parameter of the LV2 run + method (frame 0 is the first frame for that call to run). + */ + uint32_t frames; + + /** + The sub-frames portion of timestamp. The units used here can optionally + be set for a port (with the lv2ev:timeUnits property), otherwise this is + 1/(2^32) of an audio frame. + */ + uint32_t subframes; + + /** + The type of this event, as a number which represents some URI + defining an event type. This value MUST be some value previously + returned from a call to the uri_to_id function defined in the LV2 + URI map extension (see lv2_uri_map.h). + There are special rules which must be followed depending on the type + of an event. If the plugin recognizes an event type, the definition + of that event type will describe how to interpret the event, and + any required behaviour. Otherwise, if the type is 0, this event is a + non-POD event and lv2_event_unref MUST be called if the event is + 'dropped' (see above). Even if the plugin does not understand an event, + it may pass the event through to an output by simply copying (and NOT + calling lv2_event_unref). These rules are designed to allow for generic + event handling plugins and large non-POD events, but with minimal hassle + on simple plugins that "don't care" about these more advanced features. + */ + uint16_t type; + + /** + The size of the data portion of this event in bytes, which immediately + follows. The header size (12 bytes) is not included in this value. + */ + uint16_t size; + + /* size bytes of data follow here */ +} LV2_Event; + +/** + A buffer of LV2 events (header only). + + Like events (which this contains) an event buffer is a single chunk of POD: + the entire buffer (including contents) can be copied with a single memcpy. + The first contained event begins sizeof(LV2_EventBuffer) bytes after the + start of this struct. + + After this header, the buffer contains an event header (defined by struct + LV2_Event), followed by that event's contents (padded to 64 bits), followed + by another header, etc: + + | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | + |FRAMES |SUBFRMS|TYP|LEN|DATA..DATA..PAD|FRAMES | ... +*/ +LV2_DEPRECATED +typedef struct { + /** + The contents of the event buffer. This may or may not reside in the + same block of memory as this header, plugins must not assume either. + The host guarantees this points to at least capacity bytes of allocated + memory (though only size bytes of that are valid events). + */ + uint8_t* data; + + /** + The size of this event header in bytes (including everything). + + This is to allow for extending this header in the future without + breaking binary compatibility. Whenever this header is copied, + it MUST be done using this field (and NOT the sizeof this struct). + */ + uint16_t header_size; + + /** + The type of the time stamps for events in this buffer. + As a special exception, '0' always means audio frames and subframes + (1/UINT32_MAX'th of a frame) in the sample rate passed to instantiate. + + INPUTS: The host must set this field to the numeric ID of some URI + defining the meaning of the frames/subframes fields of contained events + (obtained by the LV2 URI Map uri_to_id function with the URI of this + extension as the 'map' argument, see lv2_uri_map.h). The host must + never pass a plugin a buffer which uses a stamp type the plugin does not + 'understand'. The value of this field must never change, except when + connect_port is called on the input port, at which time the host MUST + have set the stamp_type field to the value that will be used for all + subsequent run calls. + + OUTPUTS: The plugin may set this to any value that has been returned + from uri_to_id with the URI of this extension for a 'map' argument. + When connected to a buffer with connect_port, output ports MUST set this + field to the type of time stamp they will be writing. On any call to + connect_port on an event input port, the plugin may change this field on + any output port, it is the responsibility of the host to check if any of + these values have changed and act accordingly. + */ + uint16_t stamp_type; + + /** + The number of events in this buffer. + + INPUTS: The host must set this field to the number of events contained + in the data buffer before calling run(). The plugin must not change + this field. + + OUTPUTS: The plugin must set this field to the number of events it has + written to the buffer before returning from run(). Any initial value + should be ignored by the plugin. + */ + uint32_t event_count; + + /** + The size of the data buffer in bytes. + This is set by the host and must not be changed by the plugin. + The host is allowed to change this between run() calls. + */ + uint32_t capacity; + + /** + The size of the initial portion of the data buffer containing data. + + INPUTS: The host must set this field to the number of bytes used + by all events it has written to the buffer (including headers) + before calling the plugin's run(). + The plugin must not change this field. + + OUTPUTS: The plugin must set this field to the number of bytes + used by all events it has written to the buffer (including headers) + before returning from run(). + Any initial value should be ignored by the plugin. + */ + uint32_t size; +} LV2_Event_Buffer; + +/** + Opaque pointer to host data. +*/ +LV2_DEPRECATED +typedef void* LV2_Event_Callback_Data; + +/** + Non-POD events feature. + + To support this feature the host must pass an LV2_Feature struct to the + plugin's instantiate method with URI "http://lv2plug.in/ns/ext/event" + and data pointed to an instance of this struct. Note this feature + is not mandatory to support the event extension. +*/ +LV2_DEPRECATED +typedef struct { + /** + Opaque pointer to host data. + + The plugin MUST pass this to any call to functions in this struct. + Otherwise, it must not be interpreted in any way. + */ + LV2_Event_Callback_Data callback_data; + + /** + Take a reference to a non-POD event. + + If a plugin receives an event with type 0, it means the event is a + pointer to some object in memory and not a flat sequence of bytes + in the buffer. When receiving a non-POD event, the plugin already + has an implicit reference to the event. If the event is stored AND + passed to an output, lv2_event_ref MUST be called on that event. + If the event is only stored OR passed through, this is not necessary + (as the plugin already has 1 implicit reference). + + @param event An event received at an input that will not be copied to + an output or stored in any way. + + @param context The calling context. Like event types, this is a mapped + URI, see lv2_context.h. Simple plugin with just a run() method should + pass 0 here (the ID of the 'standard' LV2 run context). The host + guarantees that this function is realtime safe iff the context is + realtime safe. + + PLUGINS THAT VIOLATE THESE RULES MAY CAUSE CRASHES AND MEMORY LEAKS. + */ + uint32_t (*lv2_event_ref)(LV2_Event_Callback_Data callback_data, + LV2_Event* event); + + /** + Drop a reference to a non-POD event. + + If a plugin receives an event with type 0, it means the event is a + pointer to some object in memory and not a flat sequence of bytes + in the buffer. If the plugin does not pass the event through to + an output or store it internally somehow, it MUST call this function + on the event (more information on using non-POD events below). + + @param event An event received at an input that will not be copied to an + output or stored in any way. + + @param context The calling context. Like event types, this is a mapped + URI, see lv2_context.h. Simple plugin with just a run() method should + pass 0 here (the ID of the 'standard' LV2 run context). The host + guarantees that this function is realtime safe iff the context is + realtime safe. + + PLUGINS THAT VIOLATE THESE RULES MAY CAUSE CRASHES AND MEMORY LEAKS. + */ + uint32_t (*lv2_event_unref)(LV2_Event_Callback_Data callback_data, + LV2_Event* event); +} LV2_Event_Feature; + +LV2_RESTORE_WARNINGS + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +/** + @} +*/ + +#endif /* LV2_EVENT_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/event/event.meta.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/event/event.meta.ttl new file mode 100644 index 0000000000..ecc40c72c6 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/event/event.meta.ttl @@ -0,0 +1,246 @@ +@prefix dcs: . +@prefix doap: . +@prefix ev: . +@prefix foaf: . +@prefix lv2: . +@prefix rdfs: . + + + a doap:Project ; + doap:license ; + doap:name "LV2 Event" ; + doap:shortdesc "A port-based real-time generic event interface." ; + doap:created "2008-00-00" ; + doap:developer , + ; + doap:release [ + doap:revision "1.12" ; + doap:created "2014-08-08" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Minor documentation improvements." + ] + ] + ] , [ + doap:revision "1.10" ; + doap:created "2013-01-13" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Fix incorrect return type in lv2_event_get()." + ] + ] + ] , [ + doap:revision "1.8" ; + doap:created "2012-10-14" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Make event iterator gracefully handle optional ports." + ] , [ + rdfs:label "Remove asserts from event-helper.h." + ] , [ + rdfs:label "Use more precise domain and range for EventPort properties." + ] , [ + rdfs:label "Use consistent label style." + ] + ] + ] , [ + doap:revision "1.6" ; + doap:created "2012-04-17" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Fix bug in lv2_event_reserve()." + ] , [ + rdfs:label "Fix incorrect ranges of some properties." + ] , [ + rdfs:label "Merge with unified LV2 package." + ] + ] + ] , [ + doap:revision "1.4" ; + doap:created "2011-11-21" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Update packaging." + ] + ] + ] , [ + doap:revision "1.2" ; + doap:created "2011-05-26" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add build system (for installation)." + ] , [ + rdfs:label "Convert documentation to HTML and use lv2:documentation." + ] , [ + rdfs:label "Use lv2:Specification to be discovered as an extension." + ] + ] + ] , [ + doap:revision "1.0" ; + doap:created "2010-11-24" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Initial release." + ] + ] + ] ; + lv2:documentation """ + +This extension is deprecated. New implementations +should use LV2 Atom instead. + +This extension defines a generic time-stamped event port type, which can be +used to create plugins that read and write real-time events, such as MIDI, +OSC, or any other type of event payload. The type(s) of event supported by +a port is defined in the data file for a plugin, for example: + + :::turtle + + lv2:port [ + a ev:EventPort, lv2:InputPort ; + lv2:index 0 ; + ev:supportsEvent ; + lv2:symbol "midi_input" ; + lv2:name "MIDI input" ; + ] . + +"""^^lv2:Markdown . + +ev:EventPort + lv2:documentation """ + +Ports of this type will be connected to a struct of type LV2_Event_Buffer, +defined in event.h. These ports contain a sequence of generic events (possibly +several types mixed in a single stream), the specific types of which are +defined by some URI in another LV2 extension. + +"""^^lv2:Markdown . + +ev:Event + a rdfs:Class ; + rdfs:label "Event" ; + lv2:documentation """ + +An ev:EventPort contains an LV2_Event_Buffer which contains a sequence of these +events. The binary format of LV2 events is defined by the LV2_Event struct in +event.h. + +Specific event types (such as MIDI or OSC) are defined by extensions, and +should be rdfs:subClassOf this class. + +"""^^lv2:Markdown . + +ev:TimeStamp + lv2:documentation """ + +This defines the meaning of the 'frames' and 'subframes' fields of an LV2_Event +(both unsigned 32-bit integers). + +"""^^lv2:Markdown . + +ev:FrameStamp + lv2:documentation """ + +The default time stamp unit for an LV2 event: the frames field represents audio +frames (in the sample rate passed to intantiate), and the subframes field is +1/UINT32_MAX of a frame. + +"""^^lv2:Markdown . + +ev:generic + lv2:documentation """ + +Indicates that this port does something meaningful for any event type. This is +useful for things like event mixers, delays, serialisers, and so on. + +If this property is set, hosts should consider the port suitable for any type +of event. Otherwise, hosts should consider the port 'appropriate' only for the +specific event types listed with :supportsEvent. Note that plugins must +gracefully handle unknown event types whether or not this property is present. + +"""^^lv2:Markdown . + +ev:supportsEvent + lv2:documentation """ + +Indicates that this port supports or "understands" a certain event type. + +For input ports, this means the plugin understands and does something useful +with events of this type. For output ports, this means the plugin may generate +events of this type. If the plugin never actually generates events of this +type, but might pass them through from an input, this property should not be +set (use ev:inheritsEvent for that). + +Plugins with event input ports must always gracefully handle any type of event, +even if it does not 'support' it. This property should always be set for event +types the plugin understands/generates so hosts can discover plugins +appropriate for a given scenario (for example, plugins with a MIDI input). +Hosts are not expected to consider event ports suitable for some type of event +if the relevant :supportsEvent property is not set, unless the ev:generic +property for that port is also set. + + +"""^^lv2:Markdown . + +ev:inheritsEvent + lv2:documentation """ + +Indicates that this output port might pass through events that arrived at some +other input port (or generate an event of the same type as events arriving at +that input). The host must always check the stamp type of all outputs when +connecting an input, but this property should be set whenever it applies. + + +"""^^lv2:Markdown . + +ev:supportsTimeStamp + lv2:documentation """ + +Indicates that this port supports or "understands" a certain time stamp type. +Meaningful only for input ports, the host must never connect a port to an event +buffer with a time stamp type that isn't supported by the port. + +"""^^lv2:Markdown . + +ev:generatesTimeStamp + lv2:documentation """ + +Indicates that this port may output a certain time stamp type, regardless of +the time stamp type of any input ports. + +If the port outputs stamps based on what type inputs are connected to, this +property should not be set (use the ev:inheritsTimeStamp property for that). +Hosts MUST check the time_stamp value of any output port buffers after a call +to connect_port on ANY event input port on the plugin. + +If the plugin changes the stamp_type field of an output event buffer during a +call to run(), the plugin must call the stamp_type_changed function provided by +the host in the LV2_Event_Feature struct, if it is non-NULL. + +"""^^lv2:Markdown . + +ev:inheritsTimeStamp + lv2:documentation """ + +Indicates that this port follows the time stamp type of an input port. + +This property is not necessary, but it should be set for outputs that base +their output type on an input port so the host can make more sense of the +plugin and provide a more sensible interface. + +"""^^lv2:Markdown . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/event/event.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/event/event.ttl new file mode 100644 index 0000000000..de400d4348 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/event/event.ttl @@ -0,0 +1,86 @@ +@prefix ev: . +@prefix lv2: . +@prefix owl: . +@prefix rdf: . +@prefix rdfs: . + + + a owl:Ontology ; + owl:deprecated true ; + rdfs:label "LV2 Event" ; + rdfs:comment "A port-based real-time generic event interface." ; + rdfs:seeAlso , + , + . + +ev:EventPort + a rdfs:Class ; + rdfs:label "Event Port" ; + rdfs:subClassOf lv2:Port ; + rdfs:comment "An LV2 event port." . + +ev:Event + a rdfs:Class ; + rdfs:label "Event" ; + rdfs:comment "A single generic time-stamped event." . + +ev:TimeStamp + a rdfs:Class ; + rdfs:label "Event Time Stamp" ; + rdfs:comment "The time stamp of an Event." . + +ev:FrameStamp + a rdfs:Class ; + rdfs:subClassOf ev:TimeStamp ; + rdfs:label "Audio Frame Time Stamp" ; + rdfs:comment "The default time stamp unit for an event." . + +ev:generic + a lv2:PortProperty ; + rdfs:label "generic event port" ; + rdfs:comment "Port works with generic events." . + +ev:supportsEvent + a rdf:Property , + owl:ObjectProperty ; + rdfs:domain ev:EventPort ; + rdfs:range rdfs:Class ; + rdfs:label "supports event type" ; + rdfs:comment "An event type supported by this port." . + +ev:inheritsEvent + a rdf:Property , + owl:ObjectProperty ; + rdfs:domain ev:EventPort , + lv2:OutputPort ; + rdfs:range lv2:Port ; + rdfs:label "inherits event type" ; + rdfs:comment "Output port inherits event types from an input port." . + +ev:supportsTimeStamp + a rdf:Property , + owl:ObjectProperty ; + rdfs:domain ev:EventPort , + lv2:InputPort ; + rdfs:range rdfs:Class ; + rdfs:label "supports time stamp type" ; + rdfs:comment "A time stamp type suported by this input port." . + +ev:generatesTimeStamp + a rdf:Property , + owl:ObjectProperty ; + rdfs:domain ev:EventPort , + lv2:OutputPort ; + rdfs:range rdfs:Class ; + rdfs:label "generates time stamp type" ; + rdfs:comment "A time stamp type generated by this input port." . + +ev:inheritsTimeStamp + a rdf:Property , + owl:ObjectProperty ; + rdfs:domain ev:EventPort , + lv2:OutputPort ; + rdfs:range lv2:Port ; + rdfs:label "inherits time stamp type" ; + rdfs:comment "Output port inherits time stamp types from an input port." . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/event/manifest.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/event/manifest.ttl new file mode 100644 index 0000000000..0194d71717 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/event/manifest.ttl @@ -0,0 +1,9 @@ +@prefix lv2: . +@prefix rdfs: . + + + a lv2:Specification ; + lv2:minorVersion 1 ; + lv2:microVersion 12 ; + rdfs:seeAlso . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/instance-access/instance-access.h b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/instance-access/instance-access.h new file mode 100644 index 0000000000..2986f6993e --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/instance-access/instance-access.h @@ -0,0 +1,41 @@ +/* + Copyright 2008-2016 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef LV2_INSTANCE_ACCESS_H +#define LV2_INSTANCE_ACCESS_H + +/** + @defgroup instance-access Instance Access + @ingroup lv2 + + Access to the LV2_Handle of a plugin for UIs. + + See for details. + + @{ +*/ + +// clang-format off + +#define LV2_INSTANCE_ACCESS_URI "http://lv2plug.in/ns/ext/instance-access" ///< http://lv2plug.in/ns/ext/instance-access + +// clang-format on + +/** + @} +*/ + +#endif /* LV2_INSTANCE_ACCESS_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/instance-access/instance-access.meta.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/instance-access/instance-access.meta.ttl new file mode 100644 index 0000000000..875e2f842e --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/instance-access/instance-access.meta.ttl @@ -0,0 +1,75 @@ +@prefix dcs: . +@prefix doap: . +@prefix foaf: . +@prefix ia: . +@prefix lv2: . +@prefix rdfs: . + + + a doap:Project ; + doap:license ; + doap:name "LV2 Instance Access" ; + doap:shortdesc "Provides access to the LV2_Handle of a plugin." ; + doap:created "2010-10-04" ; + doap:developer ; + doap:release [ + doap:revision "1.6" ; + doap:created "2012-04-17" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Merge with unified LV2 package." + ] + ] + ] , [ + doap:revision "1.4" ; + doap:created "2011-11-21" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Update packaging." + ] , [ + rdfs:label "Improve documentation." + ] + ] + ] , [ + doap:revision "1.2" ; + doap:created "2011-05-26" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add build system for installation." + ] , [ + rdfs:label "Switch to ISC license." + ] + ] + ] , [ + doap:revision "1.0" ; + doap:created "2010-10-04" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Initial release." + ] + ] + ] ; + lv2:documentation """ + +This extension defines a feature which allows plugin UIs to get a direct handle +to an LV2 plugin instance (LV2_Handle), if possible. + +Note that the use of this extension by UIs violates the important principle of +UI/plugin separation, and is potentially a source of many problems. +Accordingly, **use of this extension is highly discouraged**, and plugins +should not expect hosts to support it, since it is often impossible to do so. + +To support this feature the host must pass an LV2_Feature struct to the UI +instantiate method with URI LV2_INSTANCE_ACCESS_URI and data pointed directly +to the LV2_Handle of the plugin instance. + +"""^^lv2:Markdown . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/instance-access/instance-access.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/instance-access/instance-access.ttl new file mode 100644 index 0000000000..085ae37d81 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/instance-access/instance-access.ttl @@ -0,0 +1,11 @@ +@prefix ia: . +@prefix lv2: . +@prefix rdfs: . + + + a lv2:Feature ; + rdfs:label "instance access" ; + rdfs:comment "A feature that provides access to a plugin instance." ; + rdfs:seeAlso , + . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/instance-access/manifest.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/instance-access/manifest.ttl new file mode 100644 index 0000000000..e6c8810758 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/instance-access/manifest.ttl @@ -0,0 +1,9 @@ +@prefix lv2: . +@prefix rdfs: . + + + a lv2:Specification ; + lv2:minorVersion 1 ; + lv2:microVersion 6 ; + rdfs:seeAlso . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/log/log.h b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/log/log.h new file mode 100644 index 0000000000..cc62befad4 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/log/log.h @@ -0,0 +1,113 @@ +/* + Copyright 2012-2016 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef LV2_LOG_H +#define LV2_LOG_H + +/** + @defgroup log Log + @ingroup lv2 + + Interface for plugins to log via the host. + + See for details. + + @{ +*/ + +// clang-format off + +#define LV2_LOG_URI "http://lv2plug.in/ns/ext/log" ///< http://lv2plug.in/ns/ext/log +#define LV2_LOG_PREFIX LV2_LOG_URI "#" ///< http://lv2plug.in/ns/ext/log# + +#define LV2_LOG__Entry LV2_LOG_PREFIX "Entry" ///< http://lv2plug.in/ns/ext/log#Entry +#define LV2_LOG__Error LV2_LOG_PREFIX "Error" ///< http://lv2plug.in/ns/ext/log#Error +#define LV2_LOG__Note LV2_LOG_PREFIX "Note" ///< http://lv2plug.in/ns/ext/log#Note +#define LV2_LOG__Trace LV2_LOG_PREFIX "Trace" ///< http://lv2plug.in/ns/ext/log#Trace +#define LV2_LOG__Warning LV2_LOG_PREFIX "Warning" ///< http://lv2plug.in/ns/ext/log#Warning +#define LV2_LOG__log LV2_LOG_PREFIX "log" ///< http://lv2plug.in/ns/ext/log#log + +// clang-format on + +#include "lv2/urid/urid.h" + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** @cond */ +#ifdef __GNUC__ +/** Allow type checking of printf-like functions. */ +# define LV2_LOG_FUNC(fmt, arg1) __attribute__((format(printf, fmt, arg1))) +#else +# define LV2_LOG_FUNC(fmt, arg1) +#endif +/** @endcond */ + +/** + Opaque data to host data for LV2_Log_Log. +*/ +typedef void* LV2_Log_Handle; + +/** + Log feature (LV2_LOG__log) +*/ +typedef struct { + /** + Opaque pointer to host data. + + This MUST be passed to methods in this struct whenever they are called. + Otherwise, it must not be interpreted in any way. + */ + LV2_Log_Handle handle; + + /** + Log a message, passing format parameters directly. + + The API of this function matches that of the standard C printf function, + except for the addition of the first two parameters. This function may + be called from any non-realtime context, or from any context if `type` + is @ref LV2_LOG__Trace. + */ + LV2_LOG_FUNC(3, 4) + int (*printf)(LV2_Log_Handle handle, LV2_URID type, const char* fmt, ...); + + /** + Log a message, passing format parameters in a va_list. + + The API of this function matches that of the standard C vprintf + function, except for the addition of the first two parameters. This + function may be called from any non-realtime context, or from any + context if `type` is @ref LV2_LOG__Trace. + */ + LV2_LOG_FUNC(3, 0) + int (*vprintf)(LV2_Log_Handle handle, + LV2_URID type, + const char* fmt, + va_list ap); +} LV2_Log_Log; + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +/** + @} +*/ + +#endif /* LV2_LOG_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/log/log.meta.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/log/log.meta.ttl new file mode 100644 index 0000000000..87cff43864 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/log/log.meta.ttl @@ -0,0 +1,126 @@ +@prefix dcs: . +@prefix doap: . +@prefix foaf: . +@prefix log: . +@prefix lv2: . +@prefix rdfs: . + + + a doap:Project ; + doap:name "LV2 Log" ; + doap:shortdesc "A feature for writing log messages." ; + doap:created "2012-01-12" ; + doap:developer ; + doap:release [ + doap:revision "2.4" ; + doap:created "2016-07-30" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add lv2_log_logger_set_map() for changing the URI map of an existing logger." + ] + ] + ] , [ + doap:revision "2.2" ; + doap:created "2014-01-04" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add missing include string.h to logger.h for memset." + ] + ] + ] , [ + doap:revision "2.0" ; + doap:created "2013-01-08" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add logger convenience API." + ] + ] + ] , [ + doap:revision "1.0" ; + doap:created "2012-04-17" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Initial release." + ] + ] + ] ; + lv2:documentation """ + +This extension defines a feature, log:log, which allows plugins to print log +messages with an API similar to the standard C `printf` function. This allows, +for example, plugin logs to be nicely presented to the user in a graphical user +interface. + +Different log levels are defined by URI and passed as an LV2_URID. This +extensions defines standard levels which are expected to be understood by all +implementations and should be sufficient in most cases, but advanced +implementations may define and use additional levels to suit their needs. + +"""^^lv2:Markdown . + +log:Entry + a rdfs:Class ; + rdfs:label "Log Entry" ; + lv2:documentation """ + +Subclasses of this are passed as the `type` parameter to LV2_Log_Log methods to +describe the nature of the log entry. + +"""^^lv2:Markdown . + +log:Error + lv2:documentation """ + +An error should only be posted when a serious unexpected error occurs, and +should be actively shown to the user by the host. + +"""^^lv2:Markdown . + +log:Note + lv2:documentation """ + +A note records some useful piece of information, but may be ignored. The host +should provide passive access to note entries to the user. + +"""^^lv2:Markdown . + +log:Warning + lv2:documentation """ + +A warning should be posted when an unexpected, but non-critical, error occurs. +The host should provide passive access to warnings entries to the user, but may +also choose to actively show them. + +"""^^lv2:Markdown . + +log:Trace + lv2:documentation """ + +A trace should not be displayed during normal operation, but the host may +implement an option to display them for debugging purposes. + +This entry type is special in that one may be posted in a real-time thread. It +is assumed that if debug tracing is enabled, real-time performance is not a +concern. However, the host MUST guarantee that posting a trace _is_ real-time +safe if debug tracing is not enabled (for example, by simply ignoring the call +as early as possible). + +"""^^lv2:Markdown . + +log:log + lv2:documentation """ + +A feature which plugins may use to log messages. To support this feature, +the host must pass an LV2_Feature to LV2_Descriptor::instantiate() with URI +LV2_LOG__log and data pointed to an instance of LV2_Log_Log. + +"""^^lv2:Markdown . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/log/log.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/log/log.ttl new file mode 100644 index 0000000000..46cba45e65 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/log/log.ttl @@ -0,0 +1,48 @@ +@prefix log: . +@prefix lv2: . +@prefix owl: . +@prefix rdf: . +@prefix rdfs: . +@prefix xsd: . + + + a owl:Ontology ; + rdfs:label "LV2 Log" ; + rdfs:comment "A feature for writing log messages." ; + rdfs:seeAlso , + . + +log:Entry + a rdfs:Class ; + rdfs:label "Entry" ; + rdfs:comment "A log entry." . + +log:Error + a rdfs:Class ; + rdfs:label "Error" ; + rdfs:subClassOf log:Entry ; + rdfs:comment "An error message." . + +log:Note + a rdfs:Class ; + rdfs:label "Note" ; + rdfs:subClassOf log:Entry ; + rdfs:comment "An informative message." . + +log:Warning + a rdfs:Class ; + rdfs:label "Warning" ; + rdfs:subClassOf log:Entry ; + rdfs:comment "A warning message." . + +log:Trace + a rdfs:Class ; + rdfs:label "Trace" ; + rdfs:subClassOf log:Entry ; + rdfs:comment "A debugging trace message." . + +log:log + a lv2:Feature ; + rdfs:label "log" ; + rdfs:comment "Logging feature." . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/log/logger.h b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/log/logger.h new file mode 100644 index 0000000000..e12b0eb4bc --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/log/logger.h @@ -0,0 +1,157 @@ +/* + Copyright 2012-2016 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef LV2_ATOM_LOGGER_H +#define LV2_ATOM_LOGGER_H + +/** + @defgroup logger Logger + @ingroup log + + Convenience API for easy logging in plugin code. This API provides simple + wrappers for logging from a plugin, which automatically fall back to + printing to stderr if host support is unavailabe. + + @{ +*/ + +#include "lv2/log/log.h" +#include "lv2/urid/urid.h" + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + Logger convenience API state. +*/ +typedef struct { + LV2_Log_Log* log; + + LV2_URID Error; + LV2_URID Note; + LV2_URID Trace; + LV2_URID Warning; +} LV2_Log_Logger; + +/** + Set `map` as the URI map for `logger`. + + This affects the message type URIDs (Error, Warning, etc) which are passed + to the log's print functions. +*/ +static inline void +lv2_log_logger_set_map(LV2_Log_Logger* logger, LV2_URID_Map* map) +{ + if (map) { + logger->Error = map->map(map->handle, LV2_LOG__Error); + logger->Note = map->map(map->handle, LV2_LOG__Note); + logger->Trace = map->map(map->handle, LV2_LOG__Trace); + logger->Warning = map->map(map->handle, LV2_LOG__Warning); + } else { + logger->Error = logger->Note = logger->Trace = logger->Warning = 0; + } +} + +/** + Initialise `logger`. + + URIs will be mapped using `map` and stored, a reference to `map` itself is + not held. Both `map` and `log` may be NULL when unsupported by the host, + in which case the implementation will fall back to printing to stderr. +*/ +static inline void +lv2_log_logger_init(LV2_Log_Logger* logger, LV2_URID_Map* map, LV2_Log_Log* log) +{ + logger->log = log; + lv2_log_logger_set_map(logger, map); +} + +/** + Log a message to the host, or stderr if support is unavailable. +*/ +LV2_LOG_FUNC(3, 0) +static inline int +lv2_log_vprintf(LV2_Log_Logger* logger, + LV2_URID type, + const char* fmt, + va_list args) +{ + return ((logger && logger->log) + ? logger->log->vprintf(logger->log->handle, type, fmt, args) + : vfprintf(stderr, fmt, args)); +} + +/** Log an error via lv2_log_vprintf(). */ +LV2_LOG_FUNC(2, 3) +static inline int +lv2_log_error(LV2_Log_Logger* logger, const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + const int ret = lv2_log_vprintf(logger, logger->Error, fmt, args); + va_end(args); + return ret; +} + +/** Log a note via lv2_log_vprintf(). */ +LV2_LOG_FUNC(2, 3) +static inline int +lv2_log_note(LV2_Log_Logger* logger, const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + const int ret = lv2_log_vprintf(logger, logger->Note, fmt, args); + va_end(args); + return ret; +} + +/** Log a trace via lv2_log_vprintf(). */ +LV2_LOG_FUNC(2, 3) +static inline int +lv2_log_trace(LV2_Log_Logger* logger, const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + const int ret = lv2_log_vprintf(logger, logger->Trace, fmt, args); + va_end(args); + return ret; +} + +/** Log a warning via lv2_log_vprintf(). */ +LV2_LOG_FUNC(2, 3) +static inline int +lv2_log_warning(LV2_Log_Logger* logger, const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + const int ret = lv2_log_vprintf(logger, logger->Warning, fmt, args); + va_end(args); + return ret; +} + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +/** + @} +*/ + +#endif /* LV2_LOG_LOGGER_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/log/manifest.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/log/manifest.ttl new file mode 100644 index 0000000000..bcaeff3db1 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/log/manifest.ttl @@ -0,0 +1,9 @@ +@prefix lv2: . +@prefix rdfs: . + + + a lv2:Specification ; + lv2:minorVersion 2 ; + lv2:microVersion 4 ; + rdfs:seeAlso . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/midi/manifest.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/midi/manifest.ttl new file mode 100644 index 0000000000..f14193671a --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/midi/manifest.ttl @@ -0,0 +1,9 @@ +@prefix lv2: . +@prefix rdfs: . + + + a lv2:Specification ; + lv2:minorVersion 1 ; + lv2:microVersion 10 ; + rdfs:seeAlso . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/midi/midi.h b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/midi/midi.h new file mode 100644 index 0000000000..2c70f0cc99 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/midi/midi.h @@ -0,0 +1,248 @@ +/* + Copyright 2012-2016 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef LV2_MIDI_H +#define LV2_MIDI_H + +/** + @defgroup midi MIDI + @ingroup lv2 + + Definitions of standard MIDI messages. + + See for details. + + @{ +*/ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +// clang-format off + +#define LV2_MIDI_URI "http://lv2plug.in/ns/ext/midi" ///< http://lv2plug.in/ns/ext/midi +#define LV2_MIDI_PREFIX LV2_MIDI_URI "#" ///< http://lv2plug.in/ns/ext/midi# + +#define LV2_MIDI__ActiveSense LV2_MIDI_PREFIX "ActiveSense" ///< http://lv2plug.in/ns/ext/midi#ActiveSense +#define LV2_MIDI__Aftertouch LV2_MIDI_PREFIX "Aftertouch" ///< http://lv2plug.in/ns/ext/midi#Aftertouch +#define LV2_MIDI__Bender LV2_MIDI_PREFIX "Bender" ///< http://lv2plug.in/ns/ext/midi#Bender +#define LV2_MIDI__ChannelPressure LV2_MIDI_PREFIX "ChannelPressure" ///< http://lv2plug.in/ns/ext/midi#ChannelPressure +#define LV2_MIDI__Chunk LV2_MIDI_PREFIX "Chunk" ///< http://lv2plug.in/ns/ext/midi#Chunk +#define LV2_MIDI__Clock LV2_MIDI_PREFIX "Clock" ///< http://lv2plug.in/ns/ext/midi#Clock +#define LV2_MIDI__Continue LV2_MIDI_PREFIX "Continue" ///< http://lv2plug.in/ns/ext/midi#Continue +#define LV2_MIDI__Controller LV2_MIDI_PREFIX "Controller" ///< http://lv2plug.in/ns/ext/midi#Controller +#define LV2_MIDI__MidiEvent LV2_MIDI_PREFIX "MidiEvent" ///< http://lv2plug.in/ns/ext/midi#MidiEvent +#define LV2_MIDI__NoteOff LV2_MIDI_PREFIX "NoteOff" ///< http://lv2plug.in/ns/ext/midi#NoteOff +#define LV2_MIDI__NoteOn LV2_MIDI_PREFIX "NoteOn" ///< http://lv2plug.in/ns/ext/midi#NoteOn +#define LV2_MIDI__ProgramChange LV2_MIDI_PREFIX "ProgramChange" ///< http://lv2plug.in/ns/ext/midi#ProgramChange +#define LV2_MIDI__QuarterFrame LV2_MIDI_PREFIX "QuarterFrame" ///< http://lv2plug.in/ns/ext/midi#QuarterFrame +#define LV2_MIDI__Reset LV2_MIDI_PREFIX "Reset" ///< http://lv2plug.in/ns/ext/midi#Reset +#define LV2_MIDI__SongPosition LV2_MIDI_PREFIX "SongPosition" ///< http://lv2plug.in/ns/ext/midi#SongPosition +#define LV2_MIDI__SongSelect LV2_MIDI_PREFIX "SongSelect" ///< http://lv2plug.in/ns/ext/midi#SongSelect +#define LV2_MIDI__Start LV2_MIDI_PREFIX "Start" ///< http://lv2plug.in/ns/ext/midi#Start +#define LV2_MIDI__Stop LV2_MIDI_PREFIX "Stop" ///< http://lv2plug.in/ns/ext/midi#Stop +#define LV2_MIDI__SystemCommon LV2_MIDI_PREFIX "SystemCommon" ///< http://lv2plug.in/ns/ext/midi#SystemCommon +#define LV2_MIDI__SystemExclusive LV2_MIDI_PREFIX "SystemExclusive" ///< http://lv2plug.in/ns/ext/midi#SystemExclusive +#define LV2_MIDI__SystemMessage LV2_MIDI_PREFIX "SystemMessage" ///< http://lv2plug.in/ns/ext/midi#SystemMessage +#define LV2_MIDI__SystemRealtime LV2_MIDI_PREFIX "SystemRealtime" ///< http://lv2plug.in/ns/ext/midi#SystemRealtime +#define LV2_MIDI__Tick LV2_MIDI_PREFIX "Tick" ///< http://lv2plug.in/ns/ext/midi#Tick +#define LV2_MIDI__TuneRequest LV2_MIDI_PREFIX "TuneRequest" ///< http://lv2plug.in/ns/ext/midi#TuneRequest +#define LV2_MIDI__VoiceMessage LV2_MIDI_PREFIX "VoiceMessage" ///< http://lv2plug.in/ns/ext/midi#VoiceMessage +#define LV2_MIDI__benderValue LV2_MIDI_PREFIX "benderValue" ///< http://lv2plug.in/ns/ext/midi#benderValue +#define LV2_MIDI__binding LV2_MIDI_PREFIX "binding" ///< http://lv2plug.in/ns/ext/midi#binding +#define LV2_MIDI__byteNumber LV2_MIDI_PREFIX "byteNumber" ///< http://lv2plug.in/ns/ext/midi#byteNumber +#define LV2_MIDI__channel LV2_MIDI_PREFIX "channel" ///< http://lv2plug.in/ns/ext/midi#channel +#define LV2_MIDI__chunk LV2_MIDI_PREFIX "chunk" ///< http://lv2plug.in/ns/ext/midi#chunk +#define LV2_MIDI__controllerNumber LV2_MIDI_PREFIX "controllerNumber" ///< http://lv2plug.in/ns/ext/midi#controllerNumber +#define LV2_MIDI__controllerValue LV2_MIDI_PREFIX "controllerValue" ///< http://lv2plug.in/ns/ext/midi#controllerValue +#define LV2_MIDI__noteNumber LV2_MIDI_PREFIX "noteNumber" ///< http://lv2plug.in/ns/ext/midi#noteNumber +#define LV2_MIDI__pressure LV2_MIDI_PREFIX "pressure" ///< http://lv2plug.in/ns/ext/midi#pressure +#define LV2_MIDI__programNumber LV2_MIDI_PREFIX "programNumber" ///< http://lv2plug.in/ns/ext/midi#programNumber +#define LV2_MIDI__property LV2_MIDI_PREFIX "property" ///< http://lv2plug.in/ns/ext/midi#property +#define LV2_MIDI__songNumber LV2_MIDI_PREFIX "songNumber" ///< http://lv2plug.in/ns/ext/midi#songNumber +#define LV2_MIDI__songPosition LV2_MIDI_PREFIX "songPosition" ///< http://lv2plug.in/ns/ext/midi#songPosition +#define LV2_MIDI__status LV2_MIDI_PREFIX "status" ///< http://lv2plug.in/ns/ext/midi#status +#define LV2_MIDI__statusMask LV2_MIDI_PREFIX "statusMask" ///< http://lv2plug.in/ns/ext/midi#statusMask +#define LV2_MIDI__velocity LV2_MIDI_PREFIX "velocity" ///< http://lv2plug.in/ns/ext/midi#velocity + +// clang-format on + +/** + MIDI Message Type. + + This includes both voice messages (which have a channel) and system messages + (which do not), as well as a sentinel value for invalid messages. To get + the type of a message suitable for use in a switch statement, use + lv2_midi_get_type() on the status byte. +*/ +typedef enum { + LV2_MIDI_MSG_INVALID = 0, /**< Invalid Message */ + LV2_MIDI_MSG_NOTE_OFF = 0x80, /**< Note Off */ + LV2_MIDI_MSG_NOTE_ON = 0x90, /**< Note On */ + LV2_MIDI_MSG_NOTE_PRESSURE = 0xA0, /**< Note Pressure */ + LV2_MIDI_MSG_CONTROLLER = 0xB0, /**< Controller */ + LV2_MIDI_MSG_PGM_CHANGE = 0xC0, /**< Program Change */ + LV2_MIDI_MSG_CHANNEL_PRESSURE = 0xD0, /**< Channel Pressure */ + LV2_MIDI_MSG_BENDER = 0xE0, /**< Pitch Bender */ + LV2_MIDI_MSG_SYSTEM_EXCLUSIVE = 0xF0, /**< System Exclusive Begin */ + LV2_MIDI_MSG_MTC_QUARTER = 0xF1, /**< MTC Quarter Frame */ + LV2_MIDI_MSG_SONG_POS = 0xF2, /**< Song Position */ + LV2_MIDI_MSG_SONG_SELECT = 0xF3, /**< Song Select */ + LV2_MIDI_MSG_TUNE_REQUEST = 0xF6, /**< Tune Request */ + LV2_MIDI_MSG_CLOCK = 0xF8, /**< Clock */ + LV2_MIDI_MSG_START = 0xFA, /**< Start */ + LV2_MIDI_MSG_CONTINUE = 0xFB, /**< Continue */ + LV2_MIDI_MSG_STOP = 0xFC, /**< Stop */ + LV2_MIDI_MSG_ACTIVE_SENSE = 0xFE, /**< Active Sensing */ + LV2_MIDI_MSG_RESET = 0xFF /**< Reset */ +} LV2_Midi_Message_Type; + +/** + Standard MIDI Controller Numbers. +*/ +typedef enum { + LV2_MIDI_CTL_MSB_BANK = 0x00, /**< Bank Selection */ + LV2_MIDI_CTL_MSB_MODWHEEL = 0x01, /**< Modulation */ + LV2_MIDI_CTL_MSB_BREATH = 0x02, /**< Breath */ + LV2_MIDI_CTL_MSB_FOOT = 0x04, /**< Foot */ + LV2_MIDI_CTL_MSB_PORTAMENTO_TIME = 0x05, /**< Portamento Time */ + LV2_MIDI_CTL_MSB_DATA_ENTRY = 0x06, /**< Data Entry */ + LV2_MIDI_CTL_MSB_MAIN_VOLUME = 0x07, /**< Main Volume */ + LV2_MIDI_CTL_MSB_BALANCE = 0x08, /**< Balance */ + LV2_MIDI_CTL_MSB_PAN = 0x0A, /**< Panpot */ + LV2_MIDI_CTL_MSB_EXPRESSION = 0x0B, /**< Expression */ + LV2_MIDI_CTL_MSB_EFFECT1 = 0x0C, /**< Effect1 */ + LV2_MIDI_CTL_MSB_EFFECT2 = 0x0D, /**< Effect2 */ + LV2_MIDI_CTL_MSB_GENERAL_PURPOSE1 = 0x10, /**< General Purpose 1 */ + LV2_MIDI_CTL_MSB_GENERAL_PURPOSE2 = 0x11, /**< General Purpose 2 */ + LV2_MIDI_CTL_MSB_GENERAL_PURPOSE3 = 0x12, /**< General Purpose 3 */ + LV2_MIDI_CTL_MSB_GENERAL_PURPOSE4 = 0x13, /**< General Purpose 4 */ + LV2_MIDI_CTL_LSB_BANK = 0x20, /**< Bank Selection */ + LV2_MIDI_CTL_LSB_MODWHEEL = 0x21, /**< Modulation */ + LV2_MIDI_CTL_LSB_BREATH = 0x22, /**< Breath */ + LV2_MIDI_CTL_LSB_FOOT = 0x24, /**< Foot */ + LV2_MIDI_CTL_LSB_PORTAMENTO_TIME = 0x25, /**< Portamento Time */ + LV2_MIDI_CTL_LSB_DATA_ENTRY = 0x26, /**< Data Entry */ + LV2_MIDI_CTL_LSB_MAIN_VOLUME = 0x27, /**< Main Volume */ + LV2_MIDI_CTL_LSB_BALANCE = 0x28, /**< Balance */ + LV2_MIDI_CTL_LSB_PAN = 0x2A, /**< Panpot */ + LV2_MIDI_CTL_LSB_EXPRESSION = 0x2B, /**< Expression */ + LV2_MIDI_CTL_LSB_EFFECT1 = 0x2C, /**< Effect1 */ + LV2_MIDI_CTL_LSB_EFFECT2 = 0x2D, /**< Effect2 */ + LV2_MIDI_CTL_LSB_GENERAL_PURPOSE1 = 0x30, /**< General Purpose 1 */ + LV2_MIDI_CTL_LSB_GENERAL_PURPOSE2 = 0x31, /**< General Purpose 2 */ + LV2_MIDI_CTL_LSB_GENERAL_PURPOSE3 = 0x32, /**< General Purpose 3 */ + LV2_MIDI_CTL_LSB_GENERAL_PURPOSE4 = 0x33, /**< General Purpose 4 */ + LV2_MIDI_CTL_SUSTAIN = 0x40, /**< Sustain Pedal */ + LV2_MIDI_CTL_PORTAMENTO = 0x41, /**< Portamento */ + LV2_MIDI_CTL_SOSTENUTO = 0x42, /**< Sostenuto */ + LV2_MIDI_CTL_SOFT_PEDAL = 0x43, /**< Soft Pedal */ + LV2_MIDI_CTL_LEGATO_FOOTSWITCH = 0x44, /**< Legato Foot Switch */ + LV2_MIDI_CTL_HOLD2 = 0x45, /**< Hold2 */ + LV2_MIDI_CTL_SC1_SOUND_VARIATION = 0x46, /**< SC1 Sound Variation */ + LV2_MIDI_CTL_SC2_TIMBRE = 0x47, /**< SC2 Timbre */ + LV2_MIDI_CTL_SC3_RELEASE_TIME = 0x48, /**< SC3 Release Time */ + LV2_MIDI_CTL_SC4_ATTACK_TIME = 0x49, /**< SC4 Attack Time */ + LV2_MIDI_CTL_SC5_BRIGHTNESS = 0x4A, /**< SC5 Brightness */ + LV2_MIDI_CTL_SC6 = 0x4B, /**< SC6 */ + LV2_MIDI_CTL_SC7 = 0x4C, /**< SC7 */ + LV2_MIDI_CTL_SC8 = 0x4D, /**< SC8 */ + LV2_MIDI_CTL_SC9 = 0x4E, /**< SC9 */ + LV2_MIDI_CTL_SC10 = 0x4F, /**< SC10 */ + LV2_MIDI_CTL_GENERAL_PURPOSE5 = 0x50, /**< General Purpose 5 */ + LV2_MIDI_CTL_GENERAL_PURPOSE6 = 0x51, /**< General Purpose 6 */ + LV2_MIDI_CTL_GENERAL_PURPOSE7 = 0x52, /**< General Purpose 7 */ + LV2_MIDI_CTL_GENERAL_PURPOSE8 = 0x53, /**< General Purpose 8 */ + LV2_MIDI_CTL_PORTAMENTO_CONTROL = 0x54, /**< Portamento Control */ + LV2_MIDI_CTL_E1_REVERB_DEPTH = 0x5B, /**< E1 Reverb Depth */ + LV2_MIDI_CTL_E2_TREMOLO_DEPTH = 0x5C, /**< E2 Tremolo Depth */ + LV2_MIDI_CTL_E3_CHORUS_DEPTH = 0x5D, /**< E3 Chorus Depth */ + LV2_MIDI_CTL_E4_DETUNE_DEPTH = 0x5E, /**< E4 Detune Depth */ + LV2_MIDI_CTL_E5_PHASER_DEPTH = 0x5F, /**< E5 Phaser Depth */ + LV2_MIDI_CTL_DATA_INCREMENT = 0x60, /**< Data Increment */ + LV2_MIDI_CTL_DATA_DECREMENT = 0x61, /**< Data Decrement */ + LV2_MIDI_CTL_NRPN_LSB = 0x62, /**< Non-registered Parameter Number */ + LV2_MIDI_CTL_NRPN_MSB = 0x63, /**< Non-registered Parameter Number */ + LV2_MIDI_CTL_RPN_LSB = 0x64, /**< Registered Parameter Number */ + LV2_MIDI_CTL_RPN_MSB = 0x65, /**< Registered Parameter Number */ + LV2_MIDI_CTL_ALL_SOUNDS_OFF = 0x78, /**< All Sounds Off */ + LV2_MIDI_CTL_RESET_CONTROLLERS = 0x79, /**< Reset Controllers */ + LV2_MIDI_CTL_LOCAL_CONTROL_SWITCH = 0x7A, /**< Local Control Switch */ + LV2_MIDI_CTL_ALL_NOTES_OFF = 0x7B, /**< All Notes Off */ + LV2_MIDI_CTL_OMNI_OFF = 0x7C, /**< Omni Off */ + LV2_MIDI_CTL_OMNI_ON = 0x7D, /**< Omni On */ + LV2_MIDI_CTL_MONO1 = 0x7E, /**< Mono1 */ + LV2_MIDI_CTL_MONO2 = 0x7F /**< Mono2 */ +} LV2_Midi_Controller; + +/** + Return true iff `msg` is a MIDI voice message (which has a channel). +*/ +static inline bool +lv2_midi_is_voice_message(const uint8_t* msg) +{ + return msg[0] >= 0x80 && msg[0] < 0xF0; +} + +/** + Return true iff `msg` is a MIDI system message (which has no channel). +*/ +static inline bool +lv2_midi_is_system_message(const uint8_t* msg) +{ + switch (msg[0]) { + case 0xF4: + case 0xF5: + case 0xF7: + case 0xF9: + case 0xFD: + return false; + default: + return (msg[0] & 0xF0) == 0xF0; + } +} + +/** + Return the type of a MIDI message. + @param msg Pointer to the start (status byte) of a MIDI message. +*/ +static inline LV2_Midi_Message_Type +lv2_midi_message_type(const uint8_t* msg) +{ + if (lv2_midi_is_voice_message(msg)) { + return (LV2_Midi_Message_Type)(msg[0] & 0xF0); + } + + if (lv2_midi_is_system_message(msg)) { + return (LV2_Midi_Message_Type)msg[0]; + } + + return LV2_MIDI_MSG_INVALID; +} + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +/** + @} +*/ + +#endif /* LV2_MIDI_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/midi/midi.meta.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/midi/midi.meta.ttl new file mode 100644 index 0000000000..7efdb01757 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/midi/midi.meta.ttl @@ -0,0 +1,153 @@ +@prefix dcs: . +@prefix doap: . +@prefix foaf: . +@prefix lv2: . +@prefix midi: . +@prefix rdfs: . + + + a doap:Project ; + doap:license ; + doap:name "LV2 MIDI" ; + doap:shortdesc "A normalised definition of raw MIDI." ; + doap:maintainer ; + doap:created "2006-00-00" ; + doap:developer , + ; + doap:release [ + doap:revision "1.10" ; + doap:created "2019-02-03" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Fix incorrect range of midi:chunk." + ] + ] + ] , [ + doap:revision "1.8" ; + doap:created "2012-10-14" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Use consistent label style." + ] , [ + rdfs:label "Add midi:binding and midi:channel predicates." + ] , [ + rdfs:label "Add midi:HexByte datatype for status bytes and masks." + ] , [ + rdfs:label "Remove non-standard midi:Tick message type." + ] , [ + rdfs:label "Add C definitions for message types and standard controllers." + ] , [ + rdfs:label "Fix definition of SystemExclusive status byte." + ] + ] + ] , [ + doap:revision "1.6" ; + doap:created "2012-04-17" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add class definitions for various message types." + ] , [ + rdfs:label "Document how to serialise a MidiEvent to a string." + ] , [ + rdfs:label "Merge with unified LV2 package." + ] + ] + ] , [ + doap:revision "1.4" ; + doap:created "2011-11-21" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Update packaging." + ] , [ + rdfs:label "Improve documentation." + ] + ] + ] , [ + doap:revision "1.2" ; + doap:created "2011-05-26" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add build system for installation." + ] , [ + rdfs:label "Switch to ISC license." + ] + ] + ] , [ + doap:revision "1.0" ; + doap:created "2010-10-04" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Initial release." + ] + ] + ] ; + lv2:documentation """ + +This specification defines a data type for a MIDI message, midi:MidiEvent, +which is normalised for fast and convenient real-time processing. MIDI is the +Musical Instrument Digital Interface, a ubiquitous binary standard for +controlling digital music devices. + +For plugins that process MIDI (or other situations where MIDI is sent via a +generic transport) the main type defined here, midi:MidiEvent, can be mapped to +an integer and used as the type of an LV2 [Atom](atom.html#Atom) or +[Event](event.html#Event). + +This specification also defines a complete vocabulary for the MIDI standard, +except for standard controller numbers. These descriptions are detailed enough +to express any MIDI message as properties. + +"""^^lv2:Markdown . + +midi:MidiEvent + lv2:documentation """ + +A single raw MIDI message (a sequence of bytes). + +This is equivalent to a standard MIDI messages, except with the following +restrictions to simplify handling: + + * Running status is not allowed, every message must have its own status byte. + + * Note On messages with velocity 0 are not allowed. These messages are + equivalent to Note Off in standard MIDI streams, but here only proper Note + Off messages are allowed. + + * "Realtime messages" (status bytes 0xF8 to 0xFF) are allowed, but may not + occur inside other messages like they can in standard MIDI streams. + + * All messages are complete valid MIDI messages. This means, for example, + that only the first byte in each event (the status byte) may have the + eighth bit set, that Note On and Note Off events are always 3 bytes long, + etc. + +Where messages are communicated, the writer is responsible for writing valid +messages, and the reader may assume that all events are valid. + +If a midi:MidiEvent is serialised to a string, the format should be +xsd:hexBinary, for example: + + :::turtle + [] eg:someEvent "901A01"^^midi:MidiEvent . + +"""^^lv2:Markdown . + +midi:statusMask + lv2:documentation """ + +This is a status byte with the lower nibble set to zero. + +"""^^lv2:Markdown . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/midi/midi.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/midi/midi.ttl new file mode 100644 index 0000000000..81f3495981 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/midi/midi.ttl @@ -0,0 +1,366 @@ +@prefix atom: . +@prefix ev: . +@prefix lv2: . +@prefix midi: . +@prefix owl: . +@prefix rdf: . +@prefix rdfs: . +@prefix xsd: . + + + a owl:Ontology ; + rdfs:label "LV2 MIDI" ; + rdfs:comment "A normalised definition of raw MIDI." ; + rdfs:seeAlso , + . + +midi:ActiveSense + a rdfs:Class ; + rdfs:subClassOf midi:SystemRealtime ; + rdfs:label "Active Sense" ; + rdfs:comment "MIDI active sense message." ; + midi:status "FE"^^xsd:hexBinary . + +midi:Aftertouch + a rdfs:Class ; + rdfs:subClassOf midi:VoiceMessage ; + rdfs:label "Aftertouch" ; + rdfs:comment "MIDI aftertouch message." ; + midi:statusMask "A0"^^xsd:hexBinary ; + midi:chunk [ + midi:byteNumber 0 ; + midi:property midi:noteNumber + ] , [ + midi:byteNumber 1 ; + midi:property midi:pressure + ] . + +midi:Bender + a rdfs:Class ; + rdfs:subClassOf midi:VoiceMessage ; + rdfs:label "Bender" ; + rdfs:comment "MIDI bender message." ; + midi:statusMask "E0"^^xsd:hexBinary ; + midi:chunk [ + midi:byteNumber 0 , + 1 ; + midi:property midi:benderValue + ] . + +midi:ChannelPressure + a rdfs:Class ; + rdfs:subClassOf midi:VoiceMessage ; + rdfs:label "Channel Pressure" ; + rdfs:comment "MIDI channel pressure message." ; + midi:statusMask "D0"^^xsd:hexBinary ; + midi:chunk [ + midi:byteNumber 0 ; + midi:property midi:pressure + ] . + +midi:Chunk + a rdfs:Class ; + rdfs:label "Chunk" ; + rdfs:comment "A sequence of contiguous bytes in a MIDI message." . + +midi:Clock + a rdfs:Class ; + rdfs:subClassOf midi:SystemRealtime ; + rdfs:label "Clock" ; + rdfs:comment "MIDI clock message." ; + midi:status "F8"^^xsd:hexBinary . + +midi:Continue + a rdfs:Class ; + rdfs:subClassOf midi:SystemRealtime ; + rdfs:label "Continue" ; + rdfs:comment "MIDI continue message." ; + midi:status "FB"^^xsd:hexBinary . + +midi:Controller + a rdfs:Class ; + rdfs:subClassOf midi:VoiceMessage ; + rdfs:label "Controller" ; + rdfs:comment "MIDI controller change message." ; + midi:statusMask "B0"^^xsd:hexBinary ; + midi:chunk [ + midi:byteNumber 0 ; + midi:property midi:controllerNumber + ] , [ + midi:byteNumber 1 ; + midi:property midi:controllerValue + ] . + +midi:HexByte + a rdfs:Datatype ; + owl:onDatatype xsd:hexBinary ; + owl:withRestrictions ( + [ + xsd:maxInclusive "FF" + ] + ) ; + rdfs:label "Hex Byte" ; + rdfs:comment "A hexadecimal byte, which has a value <= FF." . + +midi:MidiEvent + a rdfs:Class , + rdfs:Datatype ; + rdfs:subClassOf ev:Event , + atom:Atom ; + owl:onDatatype xsd:hexBinary ; + rdfs:label "MIDI Message" ; + rdfs:comment "A single raw MIDI message." . + +midi:NoteOff + a rdfs:Class ; + rdfs:subClassOf midi:VoiceMessage ; + rdfs:label "Note Off" ; + rdfs:comment "MIDI note off message." ; + midi:statusMask "80"^^xsd:hexBinary ; + midi:chunk [ + midi:byteNumber 0 ; + midi:property midi:noteNumber + ] , [ + midi:byteNumber 1 ; + midi:property midi:velocity + ] . + +midi:NoteOn + a rdfs:Class ; + rdfs:subClassOf midi:VoiceMessage ; + rdfs:label "Note On" ; + rdfs:comment "MIDI note on message." ; + midi:statusMask "90"^^xsd:hexBinary ; + midi:chunk [ + midi:byteNumber 0 ; + midi:property midi:noteNumber + ] , [ + midi:byteNumber 1 ; + midi:property midi:velocity + ] . + +midi:ProgramChange + a rdfs:Class ; + rdfs:subClassOf midi:VoiceMessage ; + rdfs:label "Program Change" ; + rdfs:comment "MIDI program change message." ; + midi:statusMask "C0"^^xsd:hexBinary ; + midi:chunk [ + midi:byteNumber 0 ; + midi:property midi:programNumber + ] . + +midi:QuarterFrame + a rdfs:Class ; + rdfs:subClassOf midi:SystemCommon ; + rdfs:label "Quarter Frame" ; + rdfs:comment "MIDI quarter frame message." ; + midi:status "F1"^^xsd:hexBinary . + +midi:Reset + a rdfs:Class ; + rdfs:subClassOf midi:SystemRealtime ; + rdfs:label "Reset" ; + rdfs:comment "MIDI reset message." ; + midi:status "FF"^^xsd:hexBinary . + +midi:SongPosition + a rdfs:Class ; + rdfs:subClassOf midi:SystemCommon ; + rdfs:label "Song Position" ; + rdfs:comment "MIDI song position pointer message." ; + midi:status "F2"^^xsd:hexBinary ; + midi:chunk [ + midi:byteNumber 0 , + 1 ; + midi:property midi:songPosition + ] . + +midi:SongSelect + a rdfs:Class ; + rdfs:subClassOf midi:SystemCommon ; + rdfs:label "Song Select" ; + rdfs:comment "MIDI song select message." ; + midi:status "F3"^^xsd:hexBinary . + +midi:Start + a rdfs:Class ; + rdfs:subClassOf midi:SystemRealtime ; + rdfs:label "Start" ; + rdfs:comment "MIDI start message." ; + midi:status "FA"^^xsd:hexBinary . + +midi:Stop + a rdfs:Class ; + rdfs:subClassOf midi:SystemRealtime ; + rdfs:label "Stop" ; + rdfs:comment "MIDI stop message." ; + midi:status "FC"^^xsd:hexBinary . + +midi:SystemCommon + a rdfs:Class ; + rdfs:subClassOf midi:SystemMessage ; + rdfs:label "System Common" ; + rdfs:comment "MIDI system common message." . + +midi:SystemExclusive + a rdfs:Class ; + rdfs:subClassOf midi:SystemMessage ; + rdfs:label "System Exclusive" ; + rdfs:comment "MIDI system exclusive message." ; + midi:status "F0"^^xsd:hexBinary . + +midi:SystemMessage + a rdfs:Class ; + rdfs:subClassOf midi:MidiEvent ; + rdfs:label "System Message" ; + rdfs:comment "MIDI system message." ; + midi:statusMask "F0"^^xsd:hexBinary . + +midi:SystemRealtime + a rdfs:Class ; + rdfs:subClassOf midi:SystemMessage ; + rdfs:label "System Realtime" ; + rdfs:comment "MIDI system realtime message." . + +midi:TuneRequest + a rdfs:Class ; + rdfs:subClassOf midi:SystemCommon ; + rdfs:label "Tune Request" ; + rdfs:comment "MIDI tune request message." ; + midi:status "F6"^^xsd:hexBinary . + +midi:VoiceMessage + a rdfs:Class ; + rdfs:subClassOf midi:MidiEvent ; + rdfs:label "Voice Message" ; + rdfs:comment "MIDI voice message." ; + midi:statusMask "F0"^^xsd:hexBinary . + +midi:benderValue + a rdf:Property , + owl:DatatypeProperty , + owl:FunctionalProperty ; + rdfs:label "bender value" ; + rdfs:range xsd:short ; + rdfs:comment "MIDI pitch bender message (-8192 to 8192)." . + +midi:binding + a rdf:Property , + owl:ObjectProperty ; + rdfs:range midi:MidiEvent ; + rdfs:label "binding" ; + rdfs:comment "The MIDI event to bind a parameter to." . + +midi:byteNumber + a rdf:Property , + owl:DatatypeProperty ; + rdfs:label "byte number" ; + rdfs:domain midi:Chunk ; + rdfs:range xsd:unsignedByte ; + rdfs:comment "The 0-based index of a byte which is part of this chunk." . + +midi:channel + a rdf:Property , + owl:DatatypeProperty , + owl:FunctionalProperty ; + rdfs:label "MIDI channel" ; + rdfs:range xsd:unsignedByte ; + rdfs:comment "The channel number of a MIDI message." . + +midi:chunk + a rdf:Property , + owl:ObjectProperty ; + rdfs:range midi:Chunk ; + rdfs:label "MIDI chunk" ; + rdfs:comment "A chunk of a MIDI message." . + +midi:controllerNumber + a rdf:Property , + owl:DatatypeProperty , + owl:FunctionalProperty ; + rdfs:label "MIDI controller number" ; + rdfs:range xsd:byte ; + rdfs:comment "The numeric ID of a controller (0 to 127)." . + +midi:controllerValue + a rdf:Property , + owl:DatatypeProperty , + owl:FunctionalProperty ; + rdfs:label "MIDI controller value" ; + rdfs:range xsd:byte ; + rdfs:comment "The value of a controller (0 to 127)." . + +midi:noteNumber + a rdf:Property , + owl:DatatypeProperty , + owl:FunctionalProperty ; + rdfs:label "note number" ; + rdfs:range xsd:byte ; + rdfs:comment "The numeric ID of a note (0 to 127)." . + +midi:pressure + a rdf:Property , + owl:DatatypeProperty , + owl:FunctionalProperty ; + rdfs:label "key pressure" ; + rdfs:range xsd:byte ; + rdfs:comment "Key pressure (0 to 127)." . + +midi:programNumber + a rdf:Property , + owl:DatatypeProperty , + owl:FunctionalProperty ; + rdfs:label "program number" ; + rdfs:range xsd:byte ; + rdfs:comment "The numeric ID of a program (0 to 127)." . + +midi:property + a rdf:Property , + owl:ObjectProperty , + owl:FunctionalProperty ; + rdfs:label "property" ; + rdfs:domain midi:Chunk ; + rdfs:range rdf:Property ; + rdfs:comment "The property this chunk represents." . + +midi:songNumber + a rdf:Property , + owl:DatatypeProperty , + owl:FunctionalProperty ; + rdfs:label "song number" ; + rdfs:range xsd:byte ; + rdfs:comment "The numeric ID of a song (0 to 127)." . + +midi:songPosition + a rdf:Property , + owl:DatatypeProperty , + owl:FunctionalProperty ; + rdfs:label "song position" ; + rdfs:range xsd:short ; + rdfs:comment "Song position in MIDI beats (16th notes) (-8192 to 8192)." . + +midi:status + a rdf:Property , + owl:DatatypeProperty , + owl:FunctionalProperty ; + rdfs:label "status byte" ; + rdfs:range midi:HexByte ; + rdfs:comment "The exact status byte for a message of this type." . + +midi:statusMask + a rdf:Property , + owl:DatatypeProperty , + owl:FunctionalProperty ; + rdfs:label "status mask" ; + rdfs:range midi:HexByte ; + rdfs:comment "The status byte for a message of this type on channel 1." . + +midi:velocity + a rdf:Property , + owl:DatatypeProperty , + owl:FunctionalProperty ; + rdfs:label "velocity" ; + rdfs:range midi:HexByte ; + rdfs:comment "The velocity of a note message (0 to 127)." . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/morph/manifest.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/morph/manifest.ttl new file mode 100644 index 0000000000..7c85cfd63c --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/morph/manifest.ttl @@ -0,0 +1,9 @@ +@prefix lv2: . +@prefix rdfs: . + + + a lv2:Specification ; + lv2:minorVersion 1 ; + lv2:microVersion 0 ; + rdfs:seeAlso . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/morph/morph.h b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/morph/morph.h new file mode 100644 index 0000000000..370937aeb9 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/morph/morph.h @@ -0,0 +1,48 @@ +/* + Copyright 2012-2016 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef LV2_MORPH_H +#define LV2_MORPH_H + +/** + @defgroup morph Morph + @ingroup lv2 + + Ports that can dynamically change type. + + See for details. + + @{ +*/ + +// clang-format off + +#define LV2_MORPH_URI "http://lv2plug.in/ns/ext/morph" ///< http://lv2plug.in/ns/ext/morph +#define LV2_MORPH_PREFIX LV2_MORPH_URI "#" ///< http://lv2plug.in/ns/ext/morph# + +#define LV2_MORPH__AutoMorphPort LV2_MORPH_PREFIX "AutoMorphPort" ///< http://lv2plug.in/ns/ext/morph#AutoMorphPort +#define LV2_MORPH__MorphPort LV2_MORPH_PREFIX "MorphPort" ///< http://lv2plug.in/ns/ext/morph#MorphPort +#define LV2_MORPH__interface LV2_MORPH_PREFIX "interface" ///< http://lv2plug.in/ns/ext/morph#interface +#define LV2_MORPH__supportsType LV2_MORPH_PREFIX "supportsType" ///< http://lv2plug.in/ns/ext/morph#supportsType +#define LV2_MORPH__currentType LV2_MORPH_PREFIX "currentType" ///< http://lv2plug.in/ns/ext/morph#currentType + +// clang-format on + +/** + @} +*/ + +#endif /* LV2_MORPH_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/morph/morph.meta.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/morph/morph.meta.ttl new file mode 100644 index 0000000000..c24778315b --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/morph/morph.meta.ttl @@ -0,0 +1,90 @@ +@prefix dcs: . +@prefix doap: . +@prefix foaf: . +@prefix lv2: . +@prefix morph: . +@prefix rdfs: . + + + a doap:Project ; + doap:name "LV2 Morph" ; + doap:shortdesc "Ports that can dynamically change type." ; + doap:created "2012-05-22" ; + doap:developer ; + doap:release [ + doap:revision "1.0" ; + doap:created "2012-10-14" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Initial release." + ] + ] + ] ; + lv2:documentation """ + +This extension defines two port types: morph:MorphPort, which has a +host-configurable type, and morph:AutoMorphPort, which may automatically change +type when a MorphPort's type is changed. These ports always have a default +type and work normally work in hosts that are unaware of this extension. Thus, +this extension provides a backwards compatibility mechanism which allows +plugins to use new port types but gracefully fall back to a default type in +hosts that do not support them. + +This extension only defines port types and properties for describing morph +ports. The actual run-time switching is done via the opts:interface API. + +"""^^lv2:Markdown . + +morph:MorphPort + lv2:documentation """ + +Ports of this type MUST have another type which defines the default buffer +format (for example lv2:ControlPort) but can be dynamically changed to a +different type in hosts that support opts:interface. + +The host may change the type of a MorphPort by setting its morph:currentType +with LV2_Options_Interface::set(). If the plugin has any morph:AutoMorphPort +ports, the host MUST check their types after changing any port type since they +may have changed. + +"""^^lv2:Markdown . + +morph:AutoMorphPort + lv2:documentation """ + +Ports of this type MUST have another type which defines the default buffer +format (for example, lv2:ControlPort) but may dynamically change types based on +the configured types of any morph:MorphPort ports on the same plugin instance. + +The type of a port may only change in response to a host call to +LV2_Options_Interface::set(). Whenever any port type on the instance changes, +the host MUST check the type of all morph:AutoMorphPort ports with +LV2_Options_Interface::get() before calling run() again, since they may have +changed. If the type of any port is zero, it means the current configuration +is invalid and the plugin may not be run (unless that port is +lv2:connectionOptional and connected to NULL). + +This is mainly useful for outputs whose type depends on the type of +corresponding inputs. + +"""^^lv2:Markdown . + +morph:supportsType + lv2:documentation """ + +Indicates that a port supports being switched to a certain type. A MorphPort +MUST list each type it supports being switched to in the plugin data using this +property. + +"""^^lv2:Markdown . + +morph:currentType + lv2:documentation """ + +The currently active type of the port. This is for dynamic use as an option +and SHOULD NOT be listed in the static plugin data. + +"""^^lv2:Markdown . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/morph/morph.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/morph/morph.ttl new file mode 100644 index 0000000000..303293da42 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/morph/morph.ttl @@ -0,0 +1,46 @@ +@prefix doap: . +@prefix foaf: . +@prefix lv2: . +@prefix morph: . +@prefix opts: . +@prefix owl: . +@prefix rdf: . +@prefix rdfs: . +@prefix xsd: . + + + a owl:Ontology ; + rdfs:label "LV2 Morph" ; + rdfs:comment "Ports that can dynamically change type." ; + rdfs:seeAlso , + . + +morph:MorphPort + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:Port ; + rdfs:label "Morph Port" ; + rdfs:comment "A port which can be switched to another type." . + +morph:AutoMorphPort + a rdfs:Class , + owl:Class ; + rdfs:subClassOf lv2:Port ; + rdfs:label "Auto Morph Port" ; + rdfs:comment "A port that can change its type based on that of another." . + +morph:supportsType + a rdf:Property , + owl:ObjectProperty ; + rdfs:domain morph:MorphPort ; + rdfs:label "supports type" ; + rdfs:comment "A type that a port supports being switched to." . + +morph:currentType + a rdf:Property , + opts:Option , + owl:ObjectProperty ; + rdfs:domain morph:MorphPort ; + rdfs:label "current type" ; + rdfs:comment "The currently active type of the port." . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/options/manifest.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/options/manifest.ttl new file mode 100644 index 0000000000..18db4483ce --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/options/manifest.ttl @@ -0,0 +1,9 @@ +@prefix lv2: . +@prefix rdfs: . + + + a lv2:Specification ; + lv2:minorVersion 1 ; + lv2:microVersion 4 ; + rdfs:seeAlso . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/options/options.h b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/options/options.h new file mode 100644 index 0000000000..0d0c16dd5c --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/options/options.h @@ -0,0 +1,149 @@ +/* + Copyright 2012-2016 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef LV2_OPTIONS_H +#define LV2_OPTIONS_H + +/** + @defgroup options Options + @ingroup lv2 + + Instantiation time options. + + See for details. + + @{ +*/ + +#include "lv2/core/lv2.h" +#include "lv2/urid/urid.h" + +#include + +// clang-format off + +#define LV2_OPTIONS_URI "http://lv2plug.in/ns/ext/options" ///< http://lv2plug.in/ns/ext/options +#define LV2_OPTIONS_PREFIX LV2_OPTIONS_URI "#" ///< http://lv2plug.in/ns/ext/options# + +#define LV2_OPTIONS__Option LV2_OPTIONS_PREFIX "Option" ///< http://lv2plug.in/ns/ext/options#Option +#define LV2_OPTIONS__interface LV2_OPTIONS_PREFIX "interface" ///< http://lv2plug.in/ns/ext/options#interface +#define LV2_OPTIONS__options LV2_OPTIONS_PREFIX "options" ///< http://lv2plug.in/ns/ext/options#options +#define LV2_OPTIONS__requiredOption LV2_OPTIONS_PREFIX "requiredOption" ///< http://lv2plug.in/ns/ext/options#requiredOption +#define LV2_OPTIONS__supportedOption LV2_OPTIONS_PREFIX "supportedOption" ///< http://lv2plug.in/ns/ext/options#supportedOption + +// clang-format on + +#ifdef __cplusplus +extern "C" { +#endif + +/** + The context of an Option, which defines the subject it applies to. +*/ +typedef enum { + /** + This option applies to the instance itself. The subject must be + ignored. + */ + LV2_OPTIONS_INSTANCE, + + /** + This option applies to some named resource. The subject is a URI mapped + to an integer (a LV2_URID, like the key) + */ + LV2_OPTIONS_RESOURCE, + + /** + This option applies to some blank node. The subject is a blank node + identifier, which is valid only within the current local scope. + */ + LV2_OPTIONS_BLANK, + + /** + This option applies to a port on the instance. The subject is the + port's index. + */ + LV2_OPTIONS_PORT +} LV2_Options_Context; + +/** + An option. + + This is a property with a subject, also known as a triple or statement. + + This struct is useful anywhere a statement needs to be passed where no + memory ownership issues are present (since the value is a const pointer). + + Options can be passed to an instance via the feature LV2_OPTIONS__options + with data pointed to an array of options terminated by a zeroed option, or + accessed/manipulated using LV2_Options_Interface. +*/ +typedef struct { + LV2_Options_Context context; /**< Context (type of subject). */ + uint32_t subject; /**< Subject. */ + LV2_URID key; /**< Key (property). */ + uint32_t size; /**< Size of value in bytes. */ + LV2_URID type; /**< Type of value (datatype). */ + const void* value; /**< Pointer to value (object). */ +} LV2_Options_Option; + +/** A status code for option functions. */ +typedef enum { + LV2_OPTIONS_SUCCESS = 0, /**< Completed successfully. */ + LV2_OPTIONS_ERR_UNKNOWN = 1, /**< Unknown error. */ + LV2_OPTIONS_ERR_BAD_SUBJECT = 1 << 1, /**< Invalid/unsupported subject. */ + LV2_OPTIONS_ERR_BAD_KEY = 1 << 2, /**< Invalid/unsupported key. */ + LV2_OPTIONS_ERR_BAD_VALUE = 1 << 3 /**< Invalid/unsupported value. */ +} LV2_Options_Status; + +/** + Interface for dynamically setting options (LV2_OPTIONS__interface). +*/ +typedef struct { + /** + Get the given options. + + Each element of the passed options array MUST have type, subject, and + key set. All other fields (size, type, value) MUST be initialised to + zero, and are set to the option value if such an option is found. + + This function is in the "instantiation" LV2 threading class, so no other + instance functions may be called concurrently. + + @return Bitwise OR of LV2_Options_Status values. + */ + uint32_t (*get)(LV2_Handle instance, LV2_Options_Option* options); + + /** + Set the given options. + + This function is in the "instantiation" LV2 threading class, so no other + instance functions may be called concurrently. + + @return Bitwise OR of LV2_Options_Status values. + */ + uint32_t (*set)(LV2_Handle instance, const LV2_Options_Option* options); +} LV2_Options_Interface; + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +/** + @} +*/ + +#endif /* LV2_OPTIONS_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/options/options.meta.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/options/options.meta.ttl new file mode 100644 index 0000000000..838b0b94b9 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/options/options.meta.ttl @@ -0,0 +1,129 @@ +@prefix dcs: . +@prefix doap: . +@prefix foaf: . +@prefix lv2: . +@prefix opts: . +@prefix rdf: . +@prefix rdfs: . + + + a doap:Project ; + doap:name "LV2 Options" ; + doap:shortdesc "Runtime options for LV2 plugins and UIs." ; + doap:created "2012-08-20" ; + doap:developer ; + doap:release [ + doap:revision "1.4" ; + doap:created "2019-02-03" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Relax range of opts:requiredOption and opts:supportedOption" + ] + ] + ] , [ + doap:revision "1.2" ; + doap:created "2013-01-10" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Set the range of opts:requiredOption and opts:supportedOption to opts:Option." + ] + ] + ] , [ + doap:revision "1.0" ; + doap:created "2012-10-14" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Initial release." + ] + ] + ] ; + lv2:documentation """ + +This extension defines a facility for options, which are values the host +passes to a plugin or UI at run time. + +There are two facilities for passing options to an instance: opts:options +allows passing options at instantiation time, and the opts:interface interface +allows options to be dynamically set and retrieved after instantiation. + +Note that this extension is only for allowing hosts to configure plugins, and +is not a live control mechanism. For real-time control, use event-based +control via an atom:AtomPort with an atom:Sequence buffer. + +Instances may indicate they require an option with the opts:requiredOption +property, or that they optionally support an option with the +opts:supportedOption property. + +"""^^lv2:Markdown . + +opts:Option + lv2:documentation """ + +It is not required for a property to explicitly be an Option in order to be +used as such. However, properties which are primarily intended for use as +options, or are at least particularly useful as options, should be explicitly +given this type for documentation purposes, and to assist hosts in discovering +option definitions. + +"""^^lv2:Markdown . + +opts:interface + lv2:documentation """ + +An interface (LV2_Options_Interface) for dynamically setting and getting +options. Note that this is intended for use by the host for configuring +plugins only, and is not a live plugin control mechanism. + +The plugin data file should advertise this interface like so: + + :::turtle + @prefix opts: . + + + a lv2:Plugin ; + lv2:extensionData opts:interface . + +"""^^lv2:Markdown . + +opts:options + lv2:documentation """ + +To implement this feature, hosts MUST pass an LV2_Feature to the appropriate +instantiate method with this URI and data pointed to an array of +LV2_Options_Option terminated by an element with both key and value set to +zero. The instance should cast this data pointer to `const +LV2_Options_Option*` and scan the array for any options of interest. The +instance MUST NOT modify the options array in any way. + +Note that requiring this feature may reduce the number of compatible hosts. +Unless some options are strictly required by the instance, this feature SHOULD +be listed as an lv2:optionalFeature. + +"""^^lv2:Markdown . + +opts:requiredOption + lv2:documentation """ + +The host MUST pass a value for the specified option via opts:options during +instantiation. + +Note that use of this property may reduce the number of compatible hosts. +Wherever possible, it is better to list options with opts:supportedOption and +fall back to a reasonable default value if it is not provided. + +"""^^lv2:Markdown . + +opts:supportedOption + lv2:documentation """ + +The host SHOULD provide a value for the specified option if one is known, or +provide the user an opportunity to specify one if possible. + +"""^^lv2:Markdown . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/options/options.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/options/options.ttl new file mode 100644 index 0000000000..f8388da82b --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/options/options.ttl @@ -0,0 +1,44 @@ +@prefix lv2: . +@prefix opts: . +@prefix owl: . +@prefix rdf: . +@prefix rdfs: . +@prefix xsd: . + + + a owl:Ontology ; + rdfs:label "LV2 Options" ; + rdfs:comment "Runtime options for LV2 plugins and UIs." ; + rdfs:seeAlso , + . + +opts:Option + a rdfs:Class ; + rdfs:label "Option" ; + rdfs:subClassOf rdf:Property ; + rdfs:comment "A value for a static option passed to an instance." . + +opts:interface + a lv2:ExtensionData ; + rdfs:label "interface" ; + rdfs:comment "An interface for dynamically setting and getting options." . + +opts:options + a lv2:Feature ; + rdfs:label "options" ; + rdfs:comment "The feature used to provide options to an instance." . + +opts:requiredOption + a rdf:Property , + owl:ObjectProperty ; + rdfs:range rdf:Property ; + rdfs:label "required option" ; + rdfs:comment "An option required by the instance to function at all." . + +opts:supportedOption + a rdf:Property , + owl:ObjectProperty ; + rdfs:range rdf:Property ; + rdfs:label "supported option" ; + rdfs:comment "An option supported or by the instance." . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/parameters/manifest.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/parameters/manifest.ttl new file mode 100644 index 0000000000..57f5d2e171 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/parameters/manifest.ttl @@ -0,0 +1,9 @@ +@prefix lv2: . +@prefix rdfs: . + + + a lv2:Specification ; + lv2:minorVersion 1 ; + lv2:microVersion 4 ; + rdfs:seeAlso . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/parameters/parameters.h b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/parameters/parameters.h new file mode 100644 index 0000000000..66a756151f --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/parameters/parameters.h @@ -0,0 +1,68 @@ +/* + Copyright 2012-2016 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef LV2_PARAMETERS_H +#define LV2_PARAMETERS_H + +/** + @defgroup parameters Parameters + @ingroup lv2 + + Common parameters for audio processing. + + See for details. + + @{ +*/ + +// clang-format off + +#define LV2_PARAMETERS_URI "http://lv2plug.in/ns/ext/parameters" ///< http://lv2plug.in/ns/ext/parameters +#define LV2_PARAMETERS_PREFIX LV2_PARAMETERS_URI "#" ///< http://lv2plug.in/ns/ext/parameters# + +#define LV2_PARAMETERS__CompressorControls LV2_PARAMETERS_PREFIX "CompressorControls" ///< http://lv2plug.in/ns/ext/parameters#CompressorControls +#define LV2_PARAMETERS__ControlGroup LV2_PARAMETERS_PREFIX "ControlGroup" ///< http://lv2plug.in/ns/ext/parameters#ControlGroup +#define LV2_PARAMETERS__EnvelopeControls LV2_PARAMETERS_PREFIX "EnvelopeControls" ///< http://lv2plug.in/ns/ext/parameters#EnvelopeControls +#define LV2_PARAMETERS__FilterControls LV2_PARAMETERS_PREFIX "FilterControls" ///< http://lv2plug.in/ns/ext/parameters#FilterControls +#define LV2_PARAMETERS__OscillatorControls LV2_PARAMETERS_PREFIX "OscillatorControls" ///< http://lv2plug.in/ns/ext/parameters#OscillatorControls +#define LV2_PARAMETERS__amplitude LV2_PARAMETERS_PREFIX "amplitude" ///< http://lv2plug.in/ns/ext/parameters#amplitude +#define LV2_PARAMETERS__attack LV2_PARAMETERS_PREFIX "attack" ///< http://lv2plug.in/ns/ext/parameters#attack +#define LV2_PARAMETERS__bypass LV2_PARAMETERS_PREFIX "bypass" ///< http://lv2plug.in/ns/ext/parameters#bypass +#define LV2_PARAMETERS__cutoffFrequency LV2_PARAMETERS_PREFIX "cutoffFrequency" ///< http://lv2plug.in/ns/ext/parameters#cutoffFrequency +#define LV2_PARAMETERS__decay LV2_PARAMETERS_PREFIX "decay" ///< http://lv2plug.in/ns/ext/parameters#decay +#define LV2_PARAMETERS__delay LV2_PARAMETERS_PREFIX "delay" ///< http://lv2plug.in/ns/ext/parameters#delay +#define LV2_PARAMETERS__dryLevel LV2_PARAMETERS_PREFIX "dryLevel" ///< http://lv2plug.in/ns/ext/parameters#dryLevel +#define LV2_PARAMETERS__frequency LV2_PARAMETERS_PREFIX "frequency" ///< http://lv2plug.in/ns/ext/parameters#frequency +#define LV2_PARAMETERS__gain LV2_PARAMETERS_PREFIX "gain" ///< http://lv2plug.in/ns/ext/parameters#gain +#define LV2_PARAMETERS__hold LV2_PARAMETERS_PREFIX "hold" ///< http://lv2plug.in/ns/ext/parameters#hold +#define LV2_PARAMETERS__pulseWidth LV2_PARAMETERS_PREFIX "pulseWidth" ///< http://lv2plug.in/ns/ext/parameters#pulseWidth +#define LV2_PARAMETERS__ratio LV2_PARAMETERS_PREFIX "ratio" ///< http://lv2plug.in/ns/ext/parameters#ratio +#define LV2_PARAMETERS__release LV2_PARAMETERS_PREFIX "release" ///< http://lv2plug.in/ns/ext/parameters#release +#define LV2_PARAMETERS__resonance LV2_PARAMETERS_PREFIX "resonance" ///< http://lv2plug.in/ns/ext/parameters#resonance +#define LV2_PARAMETERS__sampleRate LV2_PARAMETERS_PREFIX "sampleRate" ///< http://lv2plug.in/ns/ext/parameters#sampleRate +#define LV2_PARAMETERS__sustain LV2_PARAMETERS_PREFIX "sustain" ///< http://lv2plug.in/ns/ext/parameters#sustain +#define LV2_PARAMETERS__threshold LV2_PARAMETERS_PREFIX "threshold" ///< http://lv2plug.in/ns/ext/parameters#threshold +#define LV2_PARAMETERS__waveform LV2_PARAMETERS_PREFIX "waveform" ///< http://lv2plug.in/ns/ext/parameters#waveform +#define LV2_PARAMETERS__wetDryRatio LV2_PARAMETERS_PREFIX "wetDryRatio" ///< http://lv2plug.in/ns/ext/parameters#wetDryRatio +#define LV2_PARAMETERS__wetLevel LV2_PARAMETERS_PREFIX "wetLevel" ///< http://lv2plug.in/ns/ext/parameters#wetLevel + +// clang-format on + +/** + @} +*/ + +#endif /* LV2_PARAMETERS_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/parameters/parameters.meta.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/parameters/parameters.meta.ttl new file mode 100644 index 0000000000..9d7c623f19 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/parameters/parameters.meta.ttl @@ -0,0 +1,75 @@ +@prefix dcs: . +@prefix doap: . +@prefix foaf: . +@prefix lv2: . +@prefix param: . +@prefix rdfs: . + + + a doap:Project ; + doap:name "LV2 Parameters" ; + doap:release [ + doap:revision "1.4" ; + doap:created "2015-04-07" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add range to parameters so hosts know how to control them." + ] + ] + ] , [ + doap:revision "1.2" ; + doap:created "2012-10-14" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Use consistent label style." + ] , [ + rdfs:label "Add param:sampleRate." + ] , [ + rdfs:label "Add parameters.h of URI defines for convenience." + ] + ] + ] , [ + doap:revision "1.0" ; + doap:created "2012-04-17" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Initial release." + ] + ] + ] ; + doap:created "2009-00-00" ; + doap:shortdesc "Common parameters for audio processing." ; + doap:maintainer ; + doap:developer ; + lv2:documentation """ + +This is a vocabulary for parameters that are common in audio processing +software. A parameter is purely a metadata concept, unrelated to any +particular code mechanism. Parameters are used to assign meaning to controls +(for example, using lv2:designation for ports) so they can be used more +intelligently or presented to the user more efficiently. + +"""^^lv2:Markdown . + +param:wetDryRatio + a lv2:Parameter ; + rdfs:label "wet/dry ratio" ; + lv2:documentation """ + +The ratio between processed and bypass components in output signal. The dry +and wet percentages can be calculated from the following equations: + + :::c + dry = (wetDryRatio.maximum - wetDryRatio.value) / wetDryRatio.maximum + wet = wetDryRatio.value / wetDryRatio.maximum + +Typically, maximum value of 1 or 100 and minimum value of 0 should be used. + +"""^^lv2:Markdown . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/parameters/parameters.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/parameters/parameters.ttl new file mode 100644 index 0000000000..5c3dadf9aa --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/parameters/parameters.ttl @@ -0,0 +1,202 @@ +@prefix atom: . +@prefix lv2: . +@prefix owl: . +@prefix param: . +@prefix pg: . +@prefix rdf: . +@prefix rdfs: . +@prefix units: . + + + a owl:Ontology ; + rdfs:label "LV2 Parameters" ; + rdfs:comment "Common parameters for audio processing." ; + rdfs:seeAlso . + +param:ControlGroup + a rdfs:Class ; + rdfs:subClassOf pg:Group ; + rdfs:label "Control Group" ; + rdfs:comment "A group representing a set of associated controls." . + +param:amplitude + a lv2:Parameter ; + rdfs:range atom:Float ; + rdfs:label "amplitude" ; + rdfs:comment "An amplitude as a factor, where 0 is silent and 1 is unity." . + +param:attack + a lv2:Parameter ; + rdfs:range atom:Float ; + rdfs:label "attack" ; + rdfs:comment "The duration of an envelope attack stage." . + +param:cutoffFrequency + a lv2:Parameter ; + rdfs:range atom:Float ; + rdfs:label "cutoff frequency" ; + rdfs:comment "The cutoff frequency, typically in Hz, for a filter." . + +param:decay + a lv2:Parameter ; + rdfs:range atom:Float ; + rdfs:label "decay" ; + rdfs:comment "The duration of an envelope decay stage." . + +param:delay + a lv2:Parameter ; + rdfs:range atom:Float ; + rdfs:label "delay" ; + rdfs:comment "The duration of an envelope delay stage." . + +param:frequency + a lv2:Parameter ; + rdfs:range atom:Float ; + rdfs:label "frequency" ; + rdfs:comment "A frequency, typically in Hz." . + +param:hold + a lv2:Parameter ; + rdfs:range atom:Float ; + rdfs:label "hold" ; + rdfs:comment "The duration of an envelope hold stage." . + +param:pulseWidth + a lv2:Parameter ; + rdfs:range atom:Float ; + rdfs:label "pulse width" ; + rdfs:comment "The width of a pulse of a rectangular waveform." . + +param:ratio + a lv2:Parameter ; + rdfs:range atom:Float ; + rdfs:label "ratio" ; + rdfs:comment "Compression ratio." . + +param:release + a lv2:Parameter ; + rdfs:range atom:Float ; + rdfs:label "release" ; + rdfs:comment "The duration of an envelope release stage." . + +param:resonance + a lv2:Parameter ; + rdfs:range atom:Float ; + rdfs:label "resonance" ; + rdfs:comment "The resonance of a filter." . + +param:sustain + a lv2:Parameter ; + rdfs:label "sustain" ; + rdfs:range atom:Float ; + rdfs:comment "The level of an envelope sustain stage as a factor." . + +param:threshold + a lv2:Parameter ; + rdfs:range atom:Float ; + rdfs:label "threshold" ; + rdfs:comment "Compression threshold." . + +param:waveform + a lv2:Parameter ; + rdfs:range atom:Float ; + rdfs:label "waveform" ; + rdfs:comment """The waveform "fader" for oscillators or modulators that have several.""" . + +param:gain + a lv2:Parameter ; + rdfs:range atom:Float ; + lv2:default 0.0 ; + lv2:minimum -20.0 ; + lv2:maximum 20.0 ; + units:unit units:db ; + rdfs:label "gain" ; + rdfs:comment "Gain in decibels." . + +param:wetDryRatio + a lv2:Parameter ; + rdfs:label "wet/dry ratio" ; + rdfs:comment "The ratio between processed and bypassed levels in the output." . + +param:wetLevel + a lv2:Parameter ; + rdfs:label "wet level" ; + rdfs:comment "The level of the processed component of a signal." . + +param:dryLevel + a lv2:Parameter ; + rdfs:label "dry level" ; + rdfs:comment "The level of the unprocessed component of a signal." . + +param:bypass + a lv2:Parameter ; + rdfs:label "bypass" ; + rdfs:comment "A boolean parameter that disables processing if true." . + +param:sampleRate + a lv2:Parameter ; + rdfs:label "sample rate" ; + rdfs:comment "A sample rate in Hz." . + +param:EnvelopeControls + a rdfs:Class ; + rdfs:subClassOf param:ControlGroup ; + rdfs:label "Envelope Controls" ; + rdfs:comment "Typical controls for a DAHDSR envelope." ; + pg:element [ + lv2:index 0 ; + lv2:designation param:delay + ] , [ + lv2:index 1 ; + lv2:designation param:attack + ] , [ + lv2:index 2 ; + lv2:designation param:hold + ] , [ + lv2:index 3 ; + lv2:designation param:decay + ] , [ + lv2:index 4 ; + lv2:designation param:sustain + ] , [ + lv2:index 5 ; + lv2:designation param:release + ] . + +param:OscillatorControls + a rdfs:Class ; + rdfs:subClassOf param:ControlGroup ; + rdfs:label "Oscillator Controls" ; + rdfs:comment "Typical controls for an oscillator." ; + pg:element [ + lv2:designation param:frequency + ] , [ + lv2:designation param:amplitude + ] , [ + lv2:designation param:waveform + ] , [ + lv2:designation param:pulseWidth + ] . + +param:FilterControls + a rdfs:Class ; + rdfs:subClassOf param:ControlGroup ; + rdfs:label "Filter Controls" ; + rdfs:comment "Typical controls for a filter." ; + pg:element [ + lv2:designation param:cutoffFrequency + ] , [ + lv2:designation param:resonance + ] . + +param:CompressorControls + a rdfs:Class ; + rdfs:subClassOf param:ControlGroup ; + rdfs:label "Compressor Controls" ; + rdfs:comment "Typical controls for a compressor." ; + pg:element [ + lv2:designation param:threshold + ] , [ + lv2:designation param:ratio + ] . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/patch/manifest.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/patch/manifest.ttl new file mode 100644 index 0000000000..25150bdda9 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/patch/manifest.ttl @@ -0,0 +1,9 @@ +@prefix lv2: . +@prefix rdfs: . + + + a lv2:Specification ; + lv2:minorVersion 2 ; + lv2:microVersion 8 ; + rdfs:seeAlso . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/patch/patch.h b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/patch/patch.h new file mode 100644 index 0000000000..4db2abbde8 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/patch/patch.h @@ -0,0 +1,73 @@ +/* + Copyright 2012-2016 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef LV2_PATCH_H +#define LV2_PATCH_H + +/** + @defgroup patch Patch + @ingroup lv2 + + Messages for accessing and manipulating properties. + + Note the patch extension is purely data, this header merely defines URIs for + convenience. + + See for details. + + @{ +*/ + +// clang-format off + +#define LV2_PATCH_URI "http://lv2plug.in/ns/ext/patch" ///< http://lv2plug.in/ns/ext/patch +#define LV2_PATCH_PREFIX LV2_PATCH_URI "#" ///< http://lv2plug.in/ns/ext/patch# + +#define LV2_PATCH__Ack LV2_PATCH_PREFIX "Ack" ///< http://lv2plug.in/ns/ext/patch#Ack +#define LV2_PATCH__Delete LV2_PATCH_PREFIX "Delete" ///< http://lv2plug.in/ns/ext/patch#Delete +#define LV2_PATCH__Copy LV2_PATCH_PREFIX "Copy" ///< http://lv2plug.in/ns/ext/patch#Copy +#define LV2_PATCH__Error LV2_PATCH_PREFIX "Error" ///< http://lv2plug.in/ns/ext/patch#Error +#define LV2_PATCH__Get LV2_PATCH_PREFIX "Get" ///< http://lv2plug.in/ns/ext/patch#Get +#define LV2_PATCH__Message LV2_PATCH_PREFIX "Message" ///< http://lv2plug.in/ns/ext/patch#Message +#define LV2_PATCH__Move LV2_PATCH_PREFIX "Move" ///< http://lv2plug.in/ns/ext/patch#Move +#define LV2_PATCH__Patch LV2_PATCH_PREFIX "Patch" ///< http://lv2plug.in/ns/ext/patch#Patch +#define LV2_PATCH__Post LV2_PATCH_PREFIX "Post" ///< http://lv2plug.in/ns/ext/patch#Post +#define LV2_PATCH__Put LV2_PATCH_PREFIX "Put" ///< http://lv2plug.in/ns/ext/patch#Put +#define LV2_PATCH__Request LV2_PATCH_PREFIX "Request" ///< http://lv2plug.in/ns/ext/patch#Request +#define LV2_PATCH__Response LV2_PATCH_PREFIX "Response" ///< http://lv2plug.in/ns/ext/patch#Response +#define LV2_PATCH__Set LV2_PATCH_PREFIX "Set" ///< http://lv2plug.in/ns/ext/patch#Set +#define LV2_PATCH__accept LV2_PATCH_PREFIX "accept" ///< http://lv2plug.in/ns/ext/patch#accept +#define LV2_PATCH__add LV2_PATCH_PREFIX "add" ///< http://lv2plug.in/ns/ext/patch#add +#define LV2_PATCH__body LV2_PATCH_PREFIX "body" ///< http://lv2plug.in/ns/ext/patch#body +#define LV2_PATCH__context LV2_PATCH_PREFIX "context" ///< http://lv2plug.in/ns/ext/patch#context +#define LV2_PATCH__destination LV2_PATCH_PREFIX "destination" ///< http://lv2plug.in/ns/ext/patch#destination +#define LV2_PATCH__property LV2_PATCH_PREFIX "property" ///< http://lv2plug.in/ns/ext/patch#property +#define LV2_PATCH__readable LV2_PATCH_PREFIX "readable" ///< http://lv2plug.in/ns/ext/patch#readable +#define LV2_PATCH__remove LV2_PATCH_PREFIX "remove" ///< http://lv2plug.in/ns/ext/patch#remove +#define LV2_PATCH__request LV2_PATCH_PREFIX "request" ///< http://lv2plug.in/ns/ext/patch#request +#define LV2_PATCH__subject LV2_PATCH_PREFIX "subject" ///< http://lv2plug.in/ns/ext/patch#subject +#define LV2_PATCH__sequenceNumber LV2_PATCH_PREFIX "sequenceNumber" ///< http://lv2plug.in/ns/ext/patch#sequenceNumber +#define LV2_PATCH__value LV2_PATCH_PREFIX "value" ///< http://lv2plug.in/ns/ext/patch#value +#define LV2_PATCH__wildcard LV2_PATCH_PREFIX "wildcard" ///< http://lv2plug.in/ns/ext/patch#wildcard +#define LV2_PATCH__writable LV2_PATCH_PREFIX "writable" ///< http://lv2plug.in/ns/ext/patch#writable + +// clang-format on + +/** + @} +*/ + +#endif /* LV2_PATCH_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/patch/patch.meta.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/patch/patch.meta.ttl new file mode 100644 index 0000000000..ebe0807180 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/patch/patch.meta.ttl @@ -0,0 +1,374 @@ +@prefix dcs: . +@prefix doap: . +@prefix foaf: . +@prefix lv2: . +@prefix patch: . +@prefix rdfs: . + + + a doap:Project ; + doap:created "2012-02-09" ; + doap:license ; + doap:developer ; + doap:name "LV2 Patch" ; + doap:shortdesc "A protocol for accessing and manipulating properties." ; + doap:release [ + doap:revision "2.8" ; + doap:created "2020-04-26" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Fix incorrect type of patch:sequenceNumber." + ] + ] + ] , [ + doap:revision "2.6" ; + doap:created "2019-02-03" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add patch:accept property." + ] , [ + rdfs:label "Add patch:context property." + ] + ] + ] , [ + doap:revision "2.4" ; + doap:created "2015-04-07" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Define patch:Get with no subject to implicitly apply to reciever. This can be used by UIs to get an initial description of a plugin." + ] , [ + rdfs:label "Add patch:Copy method." + ] + ] + ] , [ + doap:revision "2.2" ; + doap:created "2014-08-08" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add patch:sequenceNumber for associating replies with requests." + ] + ] + ] , [ + doap:revision "2.0" ; + doap:created "2013-01-10" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Make patch:Set a compact message for setting one property." + ] , [ + rdfs:label "Add patch:readable and patch:writable for describing available properties." + ] + ] + ] , [ + doap:revision "1.0" ; + doap:created "2012-04-17" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Initial release." + ] + ] + ] ; + lv2:documentation """ + +This is a vocabulary for messages that access and manipulate properties. +It can be used as a dynamic control interface for plugins, +or anything else with a property-based model. + +The key underlying concept here is to control things by manipulating arbitrary properties, +rather than by calling application-specific methods. +This allows implementations to understand what messages do +(at least in a mechanical sense), +which makes things like caching, proxying, or undo relatively straightforward to implement. +Note, however, that this is only conceptual: +there is no requirement to implement a general property store. +Typically, a plugin will understand a fixed set of properties that represent its parameters or other internal state, and ignore everything else. + +This protocol is syntax-agnostic, +and [homoiconic](https://en.wikipedia.org/wiki/Homoiconicity) +in the sense that the messages use the same format as the data they manipulate. +In particular, messages can be serialised as a binary [Object](atom.html#Object) for realtime plugin control, +or as Turtle for saving to a file, +sending over a network, +printing for debugging purposes, +and so on. + +This specification only defines a semantic protocol, +there is no corresponding API. +It can be used with the [Atom](atom.html) extension to control plugins which support message-based parameters as defined by the [Parameters](parameters.html) extension. + +For example, if a plugin defines a `eg:volume` parameter, it can be controlled by the host by sending a patch:Set message to the plugin instance: + + :::turtle + [ + a patch:Set ; + patch:property eg:volume ; + patch:value 11.0 ; + ] + +Similarly, the host could get the current value for this parameter by sending a patch:Get message: + + :::turtle + [ + a patch:Get ; + patch:property eg:volume ; + ] + +The plugin would then respond with the same patch:Set message as above. +In this case, the plugin instance is implicitly the patch:subject, +but a specific subject can also be given for deeper control. + +"""^^lv2:Markdown . + +patch:Copy + lv2:documentation """ + +After this, the destination has the same description as the subject, +and the subject is unchanged. + +It is an error if the subject does not exist, +or if the destination already exists. + +Multiple subjects may be given if the destination is a container, +but the semantics of this case are application-defined. + +"""^^lv2:Markdown . + +patch:Get + lv2:documentation """ + +If a patch:property is given, +then the receiver should respond with a patch:Set message that gives only that property. + +Otherwise, it should respond with a [concise bounded description](http://www.w3.org/Submission/CBD/) in a patch:Put message, +that is, a description that recursively includes any blank node values. + +If a patch:subject is given, then the response should have the same subject. +If no subject is given, then the receiver is implicitly the subject. + +If a patch:request node or a patch:sequenceNumber is given, +then the response should be a patch:Response and have the same property. +If neither is given, then the receiver can respond with a simple patch:Put message. +For example: + + :::turtle + [] + a patch:Get ; + patch:subject eg:something . + +Could result in: + + :::turtle + [] + a patch:Put ; + patch:subject eg:something ; + patch:body [ + eg:name "Something" ; + eg:ratio 1.6180339887 ; + ] . + +"""^^lv2:Markdown . + +patch:Insert + lv2:documentation """ + +If the subject does not exist, it is created. If the subject does already +exist, it is added to. + +This request only adds properties, it never removes them. The user must take +care that multiple values are not set for properties which should only have +one. + +"""^^lv2:Markdown . + +patch:Message + lv2:documentation """ + +This is an abstract base class for all patch messages. Concrete messages are +either a patch:Request or a patch:Response. + +"""^^lv2:Markdown . + +patch:Move + lv2:documentation """ + +After this, the destination has the description that the subject had, and the +subject no longer exists. + +It is an error if the subject does not exist, or if the destination already +exists. + +"""^^lv2:Markdown . + +patch:Patch + lv2:documentation """ + +This method always has at least one subject, and exactly one patch:add and +patch:remove property. The value of patch:add and patch:remove are nodes which +have the properties to add or remove from the subject(s), respectively. The +special value patch:wildcard may be used as the value of a remove property to +remove all properties with the given predicate. For example: + + :::turtle + [] + a patch:Patch ; + patch:subject ; + patch:add [ + eg:name "New name" ; + eg:age 42 ; + ] ; + patch:remove [ + eg:name "Old name" ; + eg:age patch:wildcard ; # Remove all old eg:age properties + ] . + +"""^^lv2:Markdown . + +patch:Put + lv2:documentation """ + +If the subject does not already exist, it is created. If the subject does +already exist, the patch:body is considered an updated version of it, and the +previous version is replaced. + + :::turtle + [] + a patch:Put ; + patch:subject ; + patch:body [ + eg:name "New name" ; + eg:age 42 ; + ] . + +"""^^lv2:Markdown . + +patch:Request + a rdfs:Class ; + rdfs:label "Request" ; + rdfs:subClassOf patch:Message ; + lv2:documentation """ + +A request may have a patch:subject property, which specifies the resource that +the request applies to. The subject may be omitted in contexts where it is +implicit, for example if the recipient is the subject. + +"""^^lv2:Markdown . + +patch:Set + lv2:documentation """ + +This is equivalent to a patch:Patch which removes _all_ pre-existing values for +the property before setting the new value. For example: + + :::turtle + [] + a patch:Set ; + patch:subject ; + patch:property eg:name ; + patch:value "New name" . + +Which is equivalent to: + + :::turtle + [] + a patch:Patch ; + patch:subject ; + patch:add [ + eg:name "New name" ; + ] ; + patch:remove [ + eg:name patch:wildcard ; + ] . + +"""^^lv2:Markdown . + +patch:body + lv2:documentation """ + +The details of this property's value depend on the type of message it is a part +of. + +"""^^lv2:Markdown . + +patch:context + lv2:documentation """ + +For example, a plugin may have a special context for ephemeral properties which +are only relevant during the lifetime of the instance and should not be saved +in state. + +The specific uses for contexts are application specific. However, the context +MUST be a URI, and can be interpreted as the ID of a data model where +properties should be stored. Implementations MAY have special support for +contexts, for example by storing in a quad store or serializing to a format +that supports multiple RDF graphs such as TriG. + +"""^^lv2:Markdown . + +patch:readable + lv2:documentation """ + +See the similar patch:writable property for details. + +"""^^lv2:Markdown . + +patch:request + lv2:documentation """ + +This can be used if referring directly to the URI or blank node ID of the +request is possible. Otherwise, use patch:sequenceNumber. + +"""^^lv2:Markdown . + +patch:sequenceNumber + lv2:documentation """ + +This property is used to associate replies with requests when it is not +feasible to refer to request URIs with patch:request. A patch:Response with a +given sequence number is the reply to the previously send patch:Request with +the same sequence number. + +The special sequence number 0 means that no reply is desired. + +"""^^lv2:Markdown . + +patch:wildcard + lv2:documentation """ + +This makes it possible to describe the removal of all values for a given +property. + +"""^^lv2:Markdown . + +patch:writable + lv2:documentation """ + +This is used to list properties that can be changed, for example to allow user +interfaces to present appropriate controls. For example: + + :::turtle + @prefix eg: . + @prefix rdf: . + @prefix rdfs: . + @prefix xsd: . + + eg:title + a rdf:Property ; + rdfs:label "title" ; + rdfs:range xsd:string . + + eg:plugin + patch:writable eg:title . + +"""^^lv2:Markdown . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/patch/patch.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/patch/patch.ttl new file mode 100644 index 0000000000..780400947e --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/patch/patch.ttl @@ -0,0 +1,251 @@ +@prefix foaf: . +@prefix owl: . +@prefix patch: . +@prefix rdf: . +@prefix rdfs: . +@prefix xsd: . + + + a owl:Ontology ; + rdfs:seeAlso , + ; + rdfs:label "LV2 Patch" ; + rdfs:comment "A protocol for accessing and manipulating properties." . + +patch:Ack + a rdfs:Class ; + rdfs:subClassOf patch:Response ; + rdfs:label "Ack" ; + rdfs:comment "An acknowledgement that a request was successful." . + +patch:Copy + a rdfs:Class ; + rdfs:subClassOf patch:Request ; + rdfs:label "Copy" ; + rdfs:subClassOf [ + a owl:Restriction ; + owl:minCardinality 1 ; + owl:onProperty patch:subject + ] , [ + a owl:Restriction ; + owl:cardinality 1 ; + owl:onProperty patch:destination + ] ; + rdfs:comment "A request to copy the patch:subject to the patch:destination." . + +patch:Delete + a rdfs:Class ; + rdfs:subClassOf patch:Request ; + rdfs:label "Delete" ; + rdfs:comment "Request that the patch:subject or subjects be deleted." . + +patch:Error + a rdfs:Class ; + rdfs:subClassOf patch:Response ; + rdfs:label "Error" ; + rdfs:comment "A response indicating an error processing a request." . + +patch:Get + a rdfs:Class ; + rdfs:subClassOf patch:Request ; + rdfs:label "Get" ; + rdfs:comment "A request for a description of the patch:subject." . + +patch:Insert + a rdfs:Class ; + rdfs:subClassOf patch:Request ; + rdfs:label "Insert" ; + rdfs:subClassOf [ + a owl:Restriction ; + owl:cardinality 1 ; + owl:onProperty patch:subject + ] ; + rdfs:comment "A request to insert a patch:body into the patch:subject." . + +patch:Message + a rdfs:Class ; + rdfs:label "Patch Message" ; + rdfs:comment "A patch message." . + +patch:Move + a rdfs:Class ; + rdfs:subClassOf patch:Request ; + rdfs:label "Move" ; + rdfs:subClassOf [ + a owl:Restriction ; + owl:cardinality 1 ; + owl:onProperty patch:subject + ] , [ + a owl:Restriction ; + owl:cardinality 1 ; + owl:onProperty patch:destination + ] ; + rdfs:comment "A request to move the patch:subject to the patch:destination." . + +patch:Patch + a rdfs:Class ; + rdfs:subClassOf patch:Request , + [ + a owl:Restriction ; + owl:minCardinality 1 ; + owl:onProperty patch:subject + ] , [ + a owl:Restriction ; + owl:cardinality 1 ; + owl:onProperty patch:add + ] , [ + a owl:Restriction ; + owl:cardinality 1 ; + owl:onProperty patch:remove + ] ; + rdfs:label "Patch" ; + rdfs:comment "A request to add and/or remove properties of the patch:subject." . + +patch:Put + a rdfs:Class ; + rdfs:subClassOf patch:Request ; + rdfs:label "Put" ; + rdfs:subClassOf [ + a owl:Restriction ; + owl:cardinality 1 ; + owl:onProperty patch:subject + ] ; + rdfs:comment "A request to put the patch:body as the patch:subject." . + +patch:Request + a rdfs:Class ; + rdfs:label "Request" ; + rdfs:subClassOf patch:Message ; + rdfs:comment "A patch request message." . + +patch:Response + a rdfs:Class ; + rdfs:subClassOf patch:Message ; + rdfs:label "Response" ; + rdfs:comment "A response to a patch:Request." . + +patch:Set + a rdfs:Class ; + rdfs:label "Set" ; + rdfs:subClassOf patch:Request , + [ + a owl:Restriction ; + owl:cardinality 1 ; + owl:onProperty patch:property + ] , [ + a owl:Restriction ; + owl:cardinality 1 ; + owl:onProperty patch:value + ] ; + rdfs:comment "A compact request to set a property to a value." . + +patch:accept + a rdf:Property , + owl:ObjectProperty ; + rdfs:label "accept" ; + rdfs:domain patch:Request ; + rdfs:range rdfs:Class ; + rdfs:comment "An accepted type for a response." . + +patch:add + a rdf:Property , + owl:ObjectProperty , + owl:FunctionalProperty ; + rdfs:domain patch:Patch ; + rdfs:range rdfs:Resource ; + rdfs:label "add" ; + rdfs:comment "The properties to add to the subject." . + +patch:body + a rdf:Property , + owl:ObjectProperty , + owl:FunctionalProperty ; + rdfs:domain patch:Message ; + rdfs:label "body" ; + rdfs:comment "The body of a message." . + +patch:context + a rdf:Property , + owl:ObjectProperty ; + rdfs:domain patch:Message ; + rdfs:label "context" ; + rdfs:comment "The context of properties in this message." . + +patch:destination + a rdf:Property , + owl:ObjectProperty , + owl:FunctionalProperty ; + rdfs:domain patch:Message ; + rdfs:label "destination" ; + rdfs:comment "The destination to move the patch:subject to." . + +patch:property + a rdf:Property , + owl:ObjectProperty ; + rdfs:label "property" ; + rdfs:domain patch:Message ; + rdfs:range rdf:Property ; + rdfs:comment "The property for a patch:Set or patch:Get message." . + +patch:readable + a rdf:Property , + owl:ObjectProperty ; + rdfs:label "readable" ; + rdfs:range rdf:Property ; + rdfs:comment "A property that can be read with a patch:Get message." . + +patch:remove + a rdf:Property , + owl:ObjectProperty , + owl:FunctionalProperty ; + rdfs:label "remove" ; + rdfs:domain patch:Patch ; + rdfs:range rdfs:Resource ; + rdfs:comment "The properties to remove from the subject." . + +patch:request + a rdf:Property , + owl:ObjectProperty , + owl:FunctionalProperty ; + rdfs:label "request" ; + rdfs:domain patch:Response ; + rdfs:range patch:Request ; + rdfs:comment "The request this is a response to." . + +patch:sequenceNumber + a rdf:Property , + owl:DatatypeProperty , + owl:FunctionalProperty ; + rdfs:label "sequence number" ; + rdfs:domain patch:Message ; + rdfs:range xsd:int ; + rdfs:comment "The sequence number of a request or response." . + +patch:subject + a rdf:Property , + owl:ObjectProperty , + owl:FunctionalProperty ; + rdfs:domain patch:Message ; + rdfs:label "subject" ; + rdfs:comment "The subject this message applies to." . + +patch:value + a rdf:Property , + owl:DatatypeProperty ; + rdfs:label "value" ; + rdfs:domain patch:Set ; + rdfs:range rdf:Property ; + rdfs:comment "The value of a property in a patch:Set message." . + +patch:wildcard + a rdfs:Resource ; + rdfs:label "wildcard" ; + rdfs:comment "A wildcard that matches any resource." . + +patch:writable + a rdf:Property , + owl:ObjectProperty ; + rdfs:label "writable" ; + rdfs:range rdf:Property ; + rdfs:comment "A property that can be set with a patch:Set or patch:Patch message." . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/port-groups/manifest.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/port-groups/manifest.ttl new file mode 100644 index 0000000000..a887cb0f37 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/port-groups/manifest.ttl @@ -0,0 +1,9 @@ +@prefix lv2: . +@prefix rdfs: . + + + a lv2:Specification ; + lv2:minorVersion 1 ; + lv2:microVersion 4 ; + rdfs:seeAlso . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/port-groups/port-groups.h b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/port-groups/port-groups.h new file mode 100644 index 0000000000..0ff25c7f8a --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/port-groups/port-groups.h @@ -0,0 +1,77 @@ +/* + Copyright 2012-2016 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef LV2_PORT_GROUPS_H +#define LV2_PORT_GROUPS_H + +/** + @defgroup port-groups Port Groups + @ingroup lv2 + + Multi-channel groups of LV2 ports. + + See for details. + + @{ +*/ + +// clang-format off + +#define LV2_PORT_GROUPS_URI "http://lv2plug.in/ns/ext/port-groups" ///< http://lv2plug.in/ns/ext/port-groups +#define LV2_PORT_GROUPS_PREFIX LV2_PORT_GROUPS_URI "#" ///< http://lv2plug.in/ns/ext/port-groups# + +#define LV2_PORT_GROUPS__DiscreteGroup LV2_PORT_GROUPS_PREFIX "DiscreteGroup" ///< http://lv2plug.in/ns/ext/port-groups#DiscreteGroup +#define LV2_PORT_GROUPS__Element LV2_PORT_GROUPS_PREFIX "Element" ///< http://lv2plug.in/ns/ext/port-groups#Element +#define LV2_PORT_GROUPS__FivePointOneGroup LV2_PORT_GROUPS_PREFIX "FivePointOneGroup" ///< http://lv2plug.in/ns/ext/port-groups#FivePointOneGroup +#define LV2_PORT_GROUPS__FivePointZeroGroup LV2_PORT_GROUPS_PREFIX "FivePointZeroGroup" ///< http://lv2plug.in/ns/ext/port-groups#FivePointZeroGroup +#define LV2_PORT_GROUPS__FourPointZeroGroup LV2_PORT_GROUPS_PREFIX "FourPointZeroGroup" ///< http://lv2plug.in/ns/ext/port-groups#FourPointZeroGroup +#define LV2_PORT_GROUPS__Group LV2_PORT_GROUPS_PREFIX "Group" ///< http://lv2plug.in/ns/ext/port-groups#Group +#define LV2_PORT_GROUPS__InputGroup LV2_PORT_GROUPS_PREFIX "InputGroup" ///< http://lv2plug.in/ns/ext/port-groups#InputGroup +#define LV2_PORT_GROUPS__MidSideGroup LV2_PORT_GROUPS_PREFIX "MidSideGroup" ///< http://lv2plug.in/ns/ext/port-groups#MidSideGroup +#define LV2_PORT_GROUPS__MonoGroup LV2_PORT_GROUPS_PREFIX "MonoGroup" ///< http://lv2plug.in/ns/ext/port-groups#MonoGroup +#define LV2_PORT_GROUPS__OutputGroup LV2_PORT_GROUPS_PREFIX "OutputGroup" ///< http://lv2plug.in/ns/ext/port-groups#OutputGroup +#define LV2_PORT_GROUPS__SevenPointOneGroup LV2_PORT_GROUPS_PREFIX "SevenPointOneGroup" ///< http://lv2plug.in/ns/ext/port-groups#SevenPointOneGroup +#define LV2_PORT_GROUPS__SevenPointOneWideGroup LV2_PORT_GROUPS_PREFIX "SevenPointOneWideGroup" ///< http://lv2plug.in/ns/ext/port-groups#SevenPointOneWideGroup +#define LV2_PORT_GROUPS__SixPointOneGroup LV2_PORT_GROUPS_PREFIX "SixPointOneGroup" ///< http://lv2plug.in/ns/ext/port-groups#SixPointOneGroup +#define LV2_PORT_GROUPS__StereoGroup LV2_PORT_GROUPS_PREFIX "StereoGroup" ///< http://lv2plug.in/ns/ext/port-groups#StereoGroup +#define LV2_PORT_GROUPS__ThreePointZeroGroup LV2_PORT_GROUPS_PREFIX "ThreePointZeroGroup" ///< http://lv2plug.in/ns/ext/port-groups#ThreePointZeroGroup +#define LV2_PORT_GROUPS__center LV2_PORT_GROUPS_PREFIX "center" ///< http://lv2plug.in/ns/ext/port-groups#center +#define LV2_PORT_GROUPS__centerLeft LV2_PORT_GROUPS_PREFIX "centerLeft" ///< http://lv2plug.in/ns/ext/port-groups#centerLeft +#define LV2_PORT_GROUPS__centerRight LV2_PORT_GROUPS_PREFIX "centerRight" ///< http://lv2plug.in/ns/ext/port-groups#centerRight +#define LV2_PORT_GROUPS__element LV2_PORT_GROUPS_PREFIX "element" ///< http://lv2plug.in/ns/ext/port-groups#element +#define LV2_PORT_GROUPS__group LV2_PORT_GROUPS_PREFIX "group" ///< http://lv2plug.in/ns/ext/port-groups#group +#define LV2_PORT_GROUPS__left LV2_PORT_GROUPS_PREFIX "left" ///< http://lv2plug.in/ns/ext/port-groups#left +#define LV2_PORT_GROUPS__lowFrequencyEffects LV2_PORT_GROUPS_PREFIX "lowFrequencyEffects" ///< http://lv2plug.in/ns/ext/port-groups#lowFrequencyEffects +#define LV2_PORT_GROUPS__mainInput LV2_PORT_GROUPS_PREFIX "mainInput" ///< http://lv2plug.in/ns/ext/port-groups#mainInput +#define LV2_PORT_GROUPS__mainOutput LV2_PORT_GROUPS_PREFIX "mainOutput" ///< http://lv2plug.in/ns/ext/port-groups#mainOutput +#define LV2_PORT_GROUPS__rearCenter LV2_PORT_GROUPS_PREFIX "rearCenter" ///< http://lv2plug.in/ns/ext/port-groups#rearCenter +#define LV2_PORT_GROUPS__rearLeft LV2_PORT_GROUPS_PREFIX "rearLeft" ///< http://lv2plug.in/ns/ext/port-groups#rearLeft +#define LV2_PORT_GROUPS__rearRight LV2_PORT_GROUPS_PREFIX "rearRight" ///< http://lv2plug.in/ns/ext/port-groups#rearRight +#define LV2_PORT_GROUPS__right LV2_PORT_GROUPS_PREFIX "right" ///< http://lv2plug.in/ns/ext/port-groups#right +#define LV2_PORT_GROUPS__side LV2_PORT_GROUPS_PREFIX "side" ///< http://lv2plug.in/ns/ext/port-groups#side +#define LV2_PORT_GROUPS__sideChainOf LV2_PORT_GROUPS_PREFIX "sideChainOf" ///< http://lv2plug.in/ns/ext/port-groups#sideChainOf +#define LV2_PORT_GROUPS__sideLeft LV2_PORT_GROUPS_PREFIX "sideLeft" ///< http://lv2plug.in/ns/ext/port-groups#sideLeft +#define LV2_PORT_GROUPS__sideRight LV2_PORT_GROUPS_PREFIX "sideRight" ///< http://lv2plug.in/ns/ext/port-groups#sideRight +#define LV2_PORT_GROUPS__source LV2_PORT_GROUPS_PREFIX "source" ///< http://lv2plug.in/ns/ext/port-groups#source +#define LV2_PORT_GROUPS__subGroupOf LV2_PORT_GROUPS_PREFIX "subGroupOf" ///< http://lv2plug.in/ns/ext/port-groups#subGroupOf + +// clang-format on + +/** + @} +*/ + +#endif /* LV2_PORT_GROUPS_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/port-groups/port-groups.meta.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/port-groups/port-groups.meta.ttl new file mode 100644 index 0000000000..67408ec2e5 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/port-groups/port-groups.meta.ttl @@ -0,0 +1,144 @@ +@prefix dcs: . +@prefix doap: . +@prefix foaf: . +@prefix lv2: . +@prefix pg: . +@prefix rdfs: . + + + a doap:Project ; + doap:license ; + doap:name "LV2 Port Groups" ; + doap:shortdesc "Multi-channel groups of LV2 ports." ; + doap:created "2008-00-00" ; + doap:developer , + ; + doap:release [ + doap:revision "1.4" ; + doap:created "2020-04-26" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Replace broken links with detailed Ambisonic channel descriptions." + ] , [ + rdfs:label "Remove incorrect type of pg:letterCode." + ] + ] + ] , [ + doap:revision "1.2" ; + doap:created "2012-10-14" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Use consistent label style." + ] + ] + ] , [ + doap:revision "1.0" ; + doap:created "2012-04-17" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Initial release." + ] + ] + ] . + +pg:Group + lv2:documentation """ + +A group logically combines ports which should be considered part of the same +stream. For example, two audio ports in a group may form a stereo stream. + +Like ports, groups have a lv2:symbol that is unique within the context of the +plugin, where group symbols and port symbols reside in the same namespace. In +other words, a group on a plugin MUST NOT have the same symbol as any other +group or port on that plugin. This makes it possible to uniquely reference a +port or group on a plugin with a single identifier and no context. + +Group definitions may be shared across plugins for brevity. For example, a +plugin collection may define a single URI for a pg:StereoGroup with the symbol +"input" and use it in many plugins. + +"""^^lv2:Markdown . + +pg:sideChainOf + lv2:documentation """ + +Indicates that this port or group should be considered a "side chain" of some +other port or group. The precise definition of "side chain" depends on the +plugin, but in general this group should be considered a modifier to some other +group, rather than an independent input itself. + +"""^^lv2:Markdown . + +pg:subGroupOf + lv2:documentation """ + +Indicates that this group is a child of another group. This property has no +meaning with respect to plugin execution, but the host may find this +information useful to provide a better user interface. Note that being a +sub-group does not relax the restriction that the group MUST have a unique +symbol with respect to the plugin. + +"""^^lv2:Markdown . + +pg:source + lv2:documentation """ + +Indicates that this port or group should be considered the "result" of some +other port or group. This property only makes sense on groups with outputs +when the source is a group with inputs. This can be used to convey a +relationship between corresponding input and output groups with different +types, for example in a mono to stereo plugin. + +"""^^lv2:Markdown . + +pg:mainInput + lv2:documentation """ + +Indicates that this group should be considered the "main" input, i.e. the +primary task is processing the signal in this group. A plugin MUST NOT have +more than one pg:mainInput property. + +"""^^lv2:Markdown . + +pg:mainOutput + lv2:documentation """ + +Indicates that this group should be considered the "main" output. The main +output group SHOULD have the main input group as a pg:source. + +"""^^lv2:Markdown . + +pg:group + lv2:documentation """ + +Indicates that this port is a part of a group of ports on the plugin. The port +should also have an lv2:designation property to define its designation within +that group. + +"""^^lv2:Markdown . + +pg:DiscreteGroup + lv2:documentation """ + +These groups are divided into channels where each represents a particular +speaker location. The position of sound in one of these groups depends on a +particular speaker configuration. + +"""^^lv2:Markdown . + +pg:AmbisonicGroup + lv2:documentation """ + +These groups are divided into channels which together represent a position in +an abstract n-dimensional space. The position of sound in one of these groups +does not depend on a particular speaker configuration; a decoder can be used to +convert an ambisonic stream for any speaker configuration. + +"""^^lv2:Markdown . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/port-groups/port-groups.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/port-groups/port-groups.ttl new file mode 100644 index 0000000000..d248f28bfb --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/port-groups/port-groups.ttl @@ -0,0 +1,807 @@ +@prefix lv2: . +@prefix owl: . +@prefix pg: . +@prefix rdf: . +@prefix rdfs: . +@prefix xsd: . + + + a owl:Ontology ; + rdfs:label "LV2 Port Groups" ; + rdfs:comment "Multi-channel groups of LV2 ports." ; + rdfs:seeAlso . + +pg:Group + a rdfs:Class ; + rdfs:label "Port Group" ; + rdfs:subClassOf [ + a owl:Restriction ; + owl:onProperty lv2:symbol ; + owl:cardinality 1 ; + rdfs:comment "A Group MUST have exactly one string lv2:symbol." + ] ; + rdfs:comment "A set of ports that are logicaly grouped together." . + +pg:InputGroup + a rdfs:Class ; + rdfs:subClassOf pg:Group ; + rdfs:label "Input Group" ; + rdfs:comment "A group which contains exclusively inputs." . + +pg:OutputGroup + a rdfs:Class ; + rdfs:subClassOf pg:Group ; + rdfs:label "Output Group" ; + rdfs:comment "A group which contains exclusively outputs." . + +pg:Element + a rdfs:Class ; + rdfs:label "Element" ; + rdfs:comment "An ordered element of a group." ; + rdfs:subClassOf [ + a owl:Restriction ; + owl:onProperty lv2:designation ; + owl:cardinality 1 ; + rdfs:comment "An element MUST have exactly one lv2:designation." + ] ; + rdfs:comment "An element of a group, with a designation and optional index." . + +pg:element + a rdf:Property , + owl:ObjectProperty ; + rdfs:range pg:Element ; + rdfs:label "element" ; + rdfs:comment "An element within a port group." . + +pg:sideChainOf + a rdf:Property , + owl:ObjectProperty ; + rdfs:label "side-chain of" ; + rdfs:comment "Port or group is a side chain of another." . + +pg:subGroupOf + a rdf:Property , + owl:ObjectProperty , + owl:FunctionalProperty ; + rdfs:domain pg:Group ; + rdfs:range pg:Group ; + rdfs:label "sub-group of" ; + rdfs:comment "Group is a child of another group." . + +pg:source + a rdf:Property , + owl:ObjectProperty ; + rdfs:domain pg:OutputGroup ; + rdfs:range pg:InputGroup ; + rdfs:label "source" ; + rdfs:comment "Port or group that this group is the output of." . + +pg:mainInput + a rdf:Property , + owl:ObjectProperty , + owl:FunctionalProperty ; + rdfs:domain lv2:Plugin ; + rdfs:range pg:InputGroup ; + rdfs:label "main input" ; + rdfs:comment "Input group that is the primary input of the plugin." . + +pg:mainOutput + a rdf:Property , + owl:ObjectProperty , + owl:FunctionalProperty ; + rdfs:domain lv2:Plugin ; + rdfs:range pg:OutputGroup ; + rdfs:label "main output" ; + rdfs:comment "Output group that is the primary output of the plugin." . + +pg:group + a rdf:Property , + owl:ObjectProperty , + owl:FunctionalProperty ; + rdfs:domain lv2:Port ; + rdfs:range pg:Group ; + rdfs:label "group" ; + rdfs:comment "Group that this port is a part of." . + +pg:DiscreteGroup + a rdfs:Class ; + rdfs:subClassOf pg:Group ; + rdfs:label "Discrete Group" ; + rdfs:comment "A group of discrete channels." . + +pg:left + a lv2:Channel ; + rdfs:label "left" ; + rdfs:comment "The left channel of a stereo audio group." . + +pg:right + a lv2:Channel ; + rdfs:label "right" ; + rdfs:comment "The right channel of a stereo audio group." . + +pg:center + a lv2:Channel ; + rdfs:label "center" ; + rdfs:comment "The center channel of a discrete audio group." . + +pg:side + a lv2:Channel ; + rdfs:label "side" ; + rdfs:comment "The side channel of a mid-side audio group." . + +pg:centerLeft + a lv2:Channel ; + rdfs:label "center left" ; + rdfs:comment "The center-left channel of a 7.1 wide surround sound group." . + +pg:centerRight + a lv2:Channel ; + rdfs:label "center right" ; + rdfs:comment "The center-right channel of a 7.1 wide surround sound group." . + +pg:sideLeft + a lv2:Channel ; + rdfs:label "side left" ; + rdfs:comment "The side-left channel of a 6.1 or 7.1 surround sound group." . + +pg:sideRight + a lv2:Channel ; + rdfs:label "side right" ; + rdfs:comment "The side-right channel of a 6.1 or 7.1 surround sound group." . + +pg:rearLeft + a lv2:Channel ; + rdfs:label "rear left" ; + rdfs:comment "The rear-left channel of a surround sound group." . + +pg:rearRight + a lv2:Channel ; + rdfs:label "rear right" ; + rdfs:comment "The rear-right channel of a surround sound group." . + +pg:rearCenter + a lv2:Channel ; + rdfs:label "rear center" ; + rdfs:comment "The rear-center channel of a surround sound group." . + +pg:lowFrequencyEffects + a lv2:Channel ; + rdfs:label "low-frequency effects" ; + rdfs:comment "The LFE channel of a *.1 surround sound group." . + +pg:MonoGroup + a rdfs:Class ; + rdfs:subClassOf pg:DiscreteGroup ; + rdfs:label "Mono" ; + rdfs:comment "A single channel audio group." ; + pg:element [ + lv2:index 0 ; + lv2:designation pg:center + ] . + +pg:StereoGroup + a rdfs:Class ; + rdfs:subClassOf pg:DiscreteGroup ; + rdfs:label "Stereo" ; + rdfs:comment "A 2-channel discrete stereo audio group." ; + pg:element [ + lv2:index 0 ; + lv2:designation pg:left + ] , [ + lv2:index 1 ; + lv2:designation pg:right + ] . + +pg:MidSideGroup + a rdfs:Class ; + rdfs:subClassOf pg:DiscreteGroup ; + rdfs:label "Mid-Side Stereo" ; + rdfs:comment "A 2-channel mid-side stereo audio group." ; + pg:element [ + lv2:index 0 ; + lv2:designation pg:center + ] , [ + lv2:index 1 ; + lv2:designation pg:side + ] . + +pg:ThreePointZeroGroup + a rdfs:Class ; + rdfs:subClassOf pg:DiscreteGroup ; + rdfs:label "3.0 Surround" ; + rdfs:comment "A 3.0 discrete surround sound group." ; + pg:element [ + lv2:index 0 ; + lv2:designation pg:left + ] , [ + lv2:index 1 ; + lv2:designation pg:right + ] , [ + lv2:index 2 ; + lv2:designation pg:rearCenter + ] . + +pg:FourPointZeroGroup + a rdfs:Class ; + rdfs:subClassOf pg:DiscreteGroup ; + rdfs:label "4.0 Surround" ; + rdfs:comment "A 4.0 (Quadraphonic) discrete surround sound group." ; + pg:element [ + lv2:index 0 ; + lv2:designation pg:left + ] , [ + lv2:index 1 ; + lv2:designation pg:center + ] , [ + lv2:index 2 ; + lv2:designation pg:right + ] , [ + lv2:index 3 ; + lv2:designation pg:rearCenter + ] . + +pg:FivePointZeroGroup + a rdfs:Class ; + rdfs:subClassOf pg:DiscreteGroup ; + rdfs:label "5.0 Surround" ; + rdfs:comment "A 5.0 (3-2 stereo) discrete surround sound group." ; + pg:element [ + lv2:index 0 ; + lv2:designation pg:left + ] , [ + lv2:index 1 ; + lv2:designation pg:center + ] , [ + lv2:index 2 ; + lv2:designation pg:right + ] , [ + lv2:index 3 ; + lv2:designation pg:rearLeft + ] , [ + lv2:index 4 ; + lv2:designation pg:rearRight + ] . + +pg:FivePointOneGroup + a rdfs:Class ; + rdfs:subClassOf pg:DiscreteGroup ; + rdfs:label "5.1 Surround" ; + rdfs:comment "A 5.1 (3-2 stereo with sub) discrete surround sound group." ; + pg:element [ + lv2:index 0 ; + lv2:designation pg:left + ] , [ + lv2:index 1 ; + lv2:designation pg:center + ] , [ + lv2:index 2 ; + lv2:designation pg:right + ] , [ + lv2:index 3 ; + lv2:designation pg:rearLeft + ] , [ + lv2:index 4 ; + lv2:designation pg:rearRight + ] , [ + lv2:index 5 ; + lv2:designation pg:lowFrequencyEffects + ] . + +pg:SixPointOneGroup + a rdfs:Class ; + rdfs:subClassOf pg:DiscreteGroup ; + rdfs:label "6.1 Surround" ; + rdfs:comment "A 6.1 discrete surround sound group." ; + pg:element [ + lv2:index 0 ; + lv2:designation pg:left + ] , [ + lv2:index 1 ; + lv2:designation pg:center + ] , [ + lv2:index 2 ; + lv2:designation pg:right + ] , [ + lv2:index 3 ; + lv2:designation pg:sideLeft + ] , [ + lv2:index 4 ; + lv2:designation pg:sideRight + ] , [ + lv2:index 5 ; + lv2:designation pg:rearCenter + ] , [ + lv2:index 6 ; + lv2:designation pg:lowFrequencyEffects + ] . + +pg:SevenPointOneGroup + a rdfs:Class ; + rdfs:subClassOf pg:DiscreteGroup ; + rdfs:label "7.1 Surround" ; + rdfs:comment "A 7.1 discrete surround sound group." ; + pg:element [ + lv2:index 0 ; + lv2:designation pg:left + ] , [ + lv2:index 1 ; + lv2:designation pg:center + ] , [ + lv2:index 2 ; + lv2:designation pg:right + ] , [ + lv2:index 3 ; + lv2:designation pg:sideLeft + ] , [ + lv2:index 4 ; + lv2:designation pg:sideRight + ] , [ + lv2:index 5 ; + lv2:designation pg:rearLeft + ] , [ + lv2:index 6 ; + lv2:designation pg:rearRight + ] , [ + lv2:index 7 ; + lv2:designation pg:lowFrequencyEffects + ] . + +pg:SevenPointOneWideGroup + a rdfs:Class ; + rdfs:subClassOf pg:DiscreteGroup ; + rdfs:label "7.1 Surround (Wide)" ; + rdfs:comment "A 7.1 wide discrete surround sound group." ; + pg:element [ + lv2:index 0 ; + lv2:designation pg:left + ] , [ + lv2:index 1 ; + lv2:designation pg:centerLeft + ] , [ + lv2:index 2 ; + lv2:designation pg:center + ] , [ + lv2:index 3 ; + lv2:designation pg:centerRight + ] , [ + lv2:index 4 ; + lv2:designation pg:right + ] , [ + lv2:index 5 ; + lv2:designation pg:rearLeft + ] , [ + lv2:index 6 ; + lv2:designation pg:rearRight + ] , [ + lv2:index 7 ; + lv2:designation pg:lowFrequencyEffects + ] . + +pg:letterCode + a rdf:Property , + owl:DatatypeProperty ; + rdfs:domain lv2:Channel ; + rdfs:range rdf:PlainLiteral ; + rdfs:label "ambisonic letter code" ; + rdfs:comment "The YuMa letter code for an Ambisonic channel." . + +pg:harmonicDegree + a rdf:Property , + owl:DatatypeProperty ; + rdfs:domain lv2:Channel ; + rdfs:range xsd:integer ; + rdfs:label "harmonic degree" ; + rdfs:comment "The degree coefficient (l) of the spherical harmonic for an Ambisonic channel." . + +pg:harmonicIndex + a rdf:Property , + owl:DatatypeProperty ; + rdfs:domain lv2:Channel ; + rdfs:range xsd:integer ; + rdfs:label "harmonic index" ; + rdfs:comment "The index coefficient (m) of the spherical harmonic for an Ambisonic channel." . + +pg:ACN0 + a lv2:Channel ; + pg:letterCode "W" ; + pg:harmonicDegree 0 ; + pg:harmonicIndex 0 ; + rdfs:label "ACN0" ; + rdfs:comment "Ambisonic channel 0 (W): degree 0, index 0." . + +pg:ACN1 + a lv2:Channel ; + pg:letterCode "Y" ; + pg:harmonicDegree 1 ; + pg:harmonicIndex -1 ; + rdfs:label "ACN1" ; + rdfs:comment "Ambisonic channel 1 (Y): degree 1, index -1." . + +pg:ACN2 + a lv2:Channel ; + pg:letterCode "Z" ; + pg:harmonicDegree 1 ; + pg:harmonicIndex 0 ; + rdfs:label "ACN2" ; + rdfs:comment "Ambisonic channel 2 (Z): degree 1, index 0." . + +pg:ACN3 + a lv2:Channel ; + pg:letterCode "X" ; + pg:harmonicDegree 1 ; + pg:harmonicIndex 1 ; + rdfs:label "ACN3" ; + rdfs:comment "Ambisonic channel 3 (X): degree 1, index 1." . + +pg:ACN4 + a lv2:Channel ; + pg:letterCode "V" ; + pg:harmonicDegree 2 ; + pg:harmonicIndex -2 ; + rdfs:label "ACN4" ; + rdfs:comment "Ambisonic channel 4 (V): degree 2, index -2." . + +pg:ACN5 + a lv2:Channel ; + pg:letterCode "T" ; + pg:harmonicDegree 2 ; + pg:harmonicIndex -1 ; + rdfs:label "ACN5" ; + rdfs:comment "Ambisonic channel 5 (T): degree 2, index -1." . + +pg:ACN6 + a lv2:Channel ; + pg:letterCode "R" ; + pg:harmonicDegree 2 ; + pg:harmonicIndex 0 ; + rdfs:label "ACN6" ; + rdfs:comment "Ambisonic channel 6 (R): degree 2, index 0." . + +pg:ACN7 + a lv2:Channel ; + pg:letterCode "S" ; + pg:harmonicDegree 2 ; + pg:harmonicIndex 1 ; + rdfs:label "ACN7" ; + rdfs:comment "Ambisonic channel 7 (S): degree 2, index 1." . + +pg:ACN8 + a lv2:Channel ; + pg:letterCode "U" ; + pg:harmonicDegree 2 ; + pg:harmonicIndex 2 ; + rdfs:label "ACN8" ; + rdfs:comment "Ambisonic channel 8 (U): degree 2, index 2." . + +pg:ACN9 + a lv2:Channel ; + pg:letterCode "Q" ; + pg:harmonicDegree 3 ; + pg:harmonicIndex -3 ; + rdfs:label "ACN9" ; + rdfs:comment "Ambisonic channel 9 (Q): degree 3, index -3." . + +pg:ACN10 + a lv2:Channel ; + pg:letterCode "O" ; + pg:harmonicDegree 3 ; + pg:harmonicIndex -2 ; + rdfs:label "ACN10" ; + rdfs:comment "Ambisonic channel 10 (O): degree 3, index -2." . + +pg:ACN11 + a lv2:Channel ; + pg:letterCode "M" ; + pg:harmonicDegree 3 ; + pg:harmonicIndex -1 ; + rdfs:label "ACN11" ; + rdfs:comment "Ambisonic channel 11 (M): degree 3, index -1." . + +pg:ACN12 + a lv2:Channel ; + pg:letterCode "K" ; + pg:harmonicDegree 3 ; + pg:harmonicIndex 0 ; + rdfs:label "ACN12" ; + rdfs:comment "Ambisonic channel 12 (K): degree 3, index 0." . + +pg:ACN13 + a lv2:Channel ; + pg:letterCode "L" ; + pg:harmonicDegree 3 ; + pg:harmonicIndex 1 ; + rdfs:label "ACN13" ; + rdfs:comment "Ambisonic channel 13 (L): degree 3, index 1." . + +pg:ACN14 + a lv2:Channel ; + pg:letterCode "N" ; + pg:harmonicDegree 3 ; + pg:harmonicIndex 2 ; + rdfs:label "ACN14" ; + rdfs:comment "Ambisonic channel 14 (N): degree 3, index 2." . + +pg:ACN15 + a lv2:Channel ; + pg:letterCode "P" ; + pg:harmonicDegree 3 ; + pg:harmonicIndex 3 ; + rdfs:label "ACN15" ; + rdfs:comment "Ambisonic channel 15 (P): degree 3, index 3." . + +pg:AmbisonicGroup + a rdfs:Class ; + rdfs:subClassOf pg:Group ; + rdfs:label "Ambisonic Group" ; + rdfs:comment "A group of Ambisonic channels." . + +pg:AmbisonicBH1P0Group + a rdfs:Class ; + rdfs:subClassOf pg:AmbisonicGroup ; + rdfs:label "Ambisonic BH1P0" ; + rdfs:comment "Ambisonic B stream of horizontal order 1 and peripheral order 0." ; + pg:element [ + lv2:index 0 ; + lv2:designation pg:ACN0 + ] , [ + lv2:index 1 ; + lv2:designation pg:ACN1 + ] , [ + lv2:index 2 ; + lv2:designation pg:ACN3 + ] . + +pg:AmbisonicBH1P1Group + a rdfs:Class ; + rdfs:subClassOf pg:AmbisonicGroup ; + rdfs:label "Ambisonic BH1P1" ; + rdfs:comment "Ambisonic B stream of horizontal order 1 and peripheral order 1." ; + pg:element [ + lv2:index 0 ; + lv2:designation pg:ACN0 + ] , [ + lv2:index 1 ; + lv2:designation pg:ACN1 + ] , [ + lv2:index 2 ; + lv2:designation pg:ACN2 + ] , [ + lv2:index 3 ; + lv2:designation pg:ACN3 + ] . + +pg:AmbisonicBH2P0Group + a rdfs:Class ; + rdfs:subClassOf pg:AmbisonicGroup ; + rdfs:label "Ambisonic BH2P0" ; + rdfs:comment "Ambisonic B stream of horizontal order 2 and peripheral order 0." ; + pg:element [ + lv2:index 0 ; + lv2:designation pg:ACN0 + ] , [ + lv2:index 1 ; + lv2:designation pg:ACN1 + ] , [ + lv2:index 2 ; + lv2:designation pg:ACN3 + ] , [ + lv2:index 3 ; + lv2:designation pg:ACN4 + ] , [ + lv2:index 4 ; + lv2:designation pg:ACN8 + ] . + +pg:AmbisonicBH2P1Group + a rdfs:Class ; + rdfs:subClassOf pg:AmbisonicGroup ; + rdfs:label "Ambisonic BH2P1" ; + rdfs:comment "Ambisonic B stream of horizontal order 2 and peripheral order 1." ; + pg:element [ + lv2:index 0 ; + lv2:designation pg:ACN0 + ] , [ + lv2:index 1 ; + lv2:designation pg:ACN1 + ] , [ + lv2:index 2 ; + lv2:designation pg:ACN2 + ] , [ + lv2:index 3 ; + lv2:designation pg:ACN3 + ] , [ + lv2:index 4 ; + lv2:designation pg:ACN4 + ] , [ + lv2:index 5 ; + lv2:designation pg:ACN8 + ] . + +pg:AmbisonicBH2P2Group + a rdfs:Class ; + rdfs:subClassOf pg:AmbisonicGroup ; + rdfs:label "Ambisonic BH2P2" ; + rdfs:comment "Ambisonic B stream of horizontal order 2 and peripheral order 2." ; + pg:element [ + lv2:index 0 ; + lv2:designation pg:ACN0 + ] , [ + lv2:index 1 ; + lv2:designation pg:ACN1 + ] , [ + lv2:index 2 ; + lv2:designation pg:ACN2 + ] , [ + lv2:index 3 ; + lv2:designation pg:ACN3 + ] , [ + lv2:index 4 ; + lv2:designation pg:ACN4 + ] , [ + lv2:index 5 ; + lv2:designation pg:ACN5 + ] , [ + lv2:index 6 ; + lv2:designation pg:ACN6 + ] , [ + lv2:index 7 ; + lv2:designation pg:ACN7 + ] , [ + lv2:index 8 ; + lv2:designation pg:ACN8 + ] . + +pg:AmbisonicBH3P0Group + a rdfs:Class ; + rdfs:subClassOf pg:AmbisonicGroup ; + rdfs:label "Ambisonic BH3P0" ; + rdfs:comment "Ambisonic B stream of horizontal order 3 and peripheral order 0." ; + pg:element [ + lv2:index 0 ; + lv2:designation pg:ACN0 + ] , [ + lv2:index 1 ; + lv2:designation pg:ACN1 + ] , [ + lv2:index 2 ; + lv2:designation pg:ACN3 + ] , [ + lv2:index 3 ; + lv2:designation pg:ACN4 + ] , [ + lv2:index 4 ; + lv2:designation pg:ACN8 + ] , [ + lv2:index 5 ; + lv2:designation pg:ACN9 + ] , [ + lv2:index 6 ; + lv2:designation pg:ACN15 + ] . + +pg:AmbisonicBH3P1Group + a rdfs:Class ; + rdfs:subClassOf pg:AmbisonicGroup ; + rdfs:label "Ambisonic BH3P1" ; + rdfs:comment "Ambisonic B stream of horizontal order 3 and peripheral order 1." ; + pg:element [ + lv2:index 0 ; + lv2:designation pg:ACN0 + ] , [ + lv2:index 1 ; + lv2:designation pg:ACN1 + ] , [ + lv2:index 2 ; + lv2:designation pg:ACN2 + ] , [ + lv2:index 3 ; + lv2:designation pg:ACN3 + ] , [ + lv2:index 4 ; + lv2:designation pg:ACN4 + ] , [ + lv2:index 5 ; + lv2:designation pg:ACN8 + ] , [ + lv2:index 6 ; + lv2:designation pg:ACN9 + ] , [ + lv2:index 7 ; + lv2:designation pg:ACN15 + ] . + +pg:AmbisonicBH3P2Group + a rdfs:Class ; + rdfs:subClassOf pg:AmbisonicGroup ; + rdfs:label "Ambisonic BH3P2" ; + rdfs:comment "Ambisonic B stream of horizontal order 3 and peripheral order 2." ; + pg:element [ + lv2:index 0 ; + lv2:designation pg:ACN0 + ] , [ + lv2:index 1 ; + lv2:designation pg:ACN1 + ] , [ + lv2:index 2 ; + lv2:designation pg:ACN2 + ] , [ + lv2:index 3 ; + lv2:designation pg:ACN3 + ] , [ + lv2:index 4 ; + lv2:designation pg:ACN4 + ] , [ + lv2:index 5 ; + lv2:designation pg:ACN5 + ] , [ + lv2:index 6 ; + lv2:designation pg:ACN6 + ] , [ + lv2:index 7 ; + lv2:designation pg:ACN7 + ] , [ + lv2:index 8 ; + lv2:designation pg:ACN8 + ] , [ + lv2:index 9 ; + lv2:designation pg:ACN9 + ] , [ + lv2:index 10 ; + lv2:designation pg:ACN15 + ] . + +pg:AmbisonicBH3P3Group + a rdfs:Class ; + rdfs:subClassOf pg:AmbisonicGroup ; + rdfs:label "Ambisonic BH3P3" ; + rdfs:comment "Ambisonic B stream of horizontal order 3 and peripheral order 3." ; + pg:element [ + lv2:index 0 ; + lv2:designation pg:ACN0 + ] , [ + lv2:index 1 ; + lv2:designation pg:ACN1 + ] , [ + lv2:index 2 ; + lv2:designation pg:ACN2 + ] , [ + lv2:index 3 ; + lv2:designation pg:ACN3 + ] , [ + lv2:index 4 ; + lv2:designation pg:ACN4 + ] , [ + lv2:index 5 ; + lv2:designation pg:ACN5 + ] , [ + lv2:index 6 ; + lv2:designation pg:ACN6 + ] , [ + lv2:index 7 ; + lv2:designation pg:ACN7 + ] , [ + lv2:index 8 ; + lv2:designation pg:ACN8 + ] , [ + lv2:index 9 ; + lv2:designation pg:ACN9 + ] , [ + lv2:index 10 ; + lv2:designation pg:ACN10 + ] , [ + lv2:index 11 ; + lv2:designation pg:ACN11 + ] , [ + lv2:index 12 ; + lv2:designation pg:ACN12 + ] , [ + lv2:index 13 ; + lv2:designation pg:ACN13 + ] , [ + lv2:index 14 ; + lv2:designation pg:ACN14 + ] , [ + lv2:index 15 ; + lv2:designation pg:ACN15 + ] . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/port-props/manifest.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/port-props/manifest.ttl new file mode 100644 index 0000000000..45f598d75e --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/port-props/manifest.ttl @@ -0,0 +1,9 @@ +@prefix lv2: . +@prefix rdfs: . + + + a lv2:Specification ; + lv2:minorVersion 1 ; + lv2:microVersion 2 ; + rdfs:seeAlso . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/port-props/port-props.h b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/port-props/port-props.h new file mode 100644 index 0000000000..ff4adcd8fe --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/port-props/port-props.h @@ -0,0 +1,53 @@ +/* + Copyright 2012-2016 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef LV2_PORT_PROPS_H +#define LV2_PORT_PROPS_H + +/** + @defgroup port-props Port Properties + @ingroup lv2 + + Various port properties. + + @{ +*/ + +// clang-format off + +#define LV2_PORT_PROPS_URI "http://lv2plug.in/ns/ext/port-props" ///< http://lv2plug.in/ns/ext/port-props +#define LV2_PORT_PROPS_PREFIX LV2_PORT_PROPS_URI "#" ///< http://lv2plug.in/ns/ext/port-props# + +#define LV2_PORT_PROPS__causesArtifacts LV2_PORT_PROPS_PREFIX "causesArtifacts" ///< http://lv2plug.in/ns/ext/port-props#causesArtifacts +#define LV2_PORT_PROPS__continuousCV LV2_PORT_PROPS_PREFIX "continuousCV" ///< http://lv2plug.in/ns/ext/port-props#continuousCV +#define LV2_PORT_PROPS__discreteCV LV2_PORT_PROPS_PREFIX "discreteCV" ///< http://lv2plug.in/ns/ext/port-props#discreteCV +#define LV2_PORT_PROPS__displayPriority LV2_PORT_PROPS_PREFIX "displayPriority" ///< http://lv2plug.in/ns/ext/port-props#displayPriority +#define LV2_PORT_PROPS__expensive LV2_PORT_PROPS_PREFIX "expensive" ///< http://lv2plug.in/ns/ext/port-props#expensive +#define LV2_PORT_PROPS__hasStrictBounds LV2_PORT_PROPS_PREFIX "hasStrictBounds" ///< http://lv2plug.in/ns/ext/port-props#hasStrictBounds +#define LV2_PORT_PROPS__logarithmic LV2_PORT_PROPS_PREFIX "logarithmic" ///< http://lv2plug.in/ns/ext/port-props#logarithmic +#define LV2_PORT_PROPS__notAutomatic LV2_PORT_PROPS_PREFIX "notAutomatic" ///< http://lv2plug.in/ns/ext/port-props#notAutomatic +#define LV2_PORT_PROPS__notOnGUI LV2_PORT_PROPS_PREFIX "notOnGUI" ///< http://lv2plug.in/ns/ext/port-props#notOnGUI +#define LV2_PORT_PROPS__rangeSteps LV2_PORT_PROPS_PREFIX "rangeSteps" ///< http://lv2plug.in/ns/ext/port-props#rangeSteps +#define LV2_PORT_PROPS__supportsStrictBounds LV2_PORT_PROPS_PREFIX "supportsStrictBounds" ///< http://lv2plug.in/ns/ext/port-props#supportsStrictBounds +#define LV2_PORT_PROPS__trigger LV2_PORT_PROPS_PREFIX "trigger" ///< http://lv2plug.in/ns/ext/port-props#trigger + +// clang-format on + +/** + @} +*/ + +#endif /* LV2_PORT_PROPS_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/port-props/port-props.meta.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/port-props/port-props.meta.ttl new file mode 100644 index 0000000000..7077e4b512 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/port-props/port-props.meta.ttl @@ -0,0 +1,202 @@ +@prefix dcs: . +@prefix doap: . +@prefix foaf: . +@prefix lv2: . +@prefix pprops: . +@prefix rdf: . +@prefix rdfs: . + + + a doap:Project ; + doap:name "LV2 Port Properties" ; + doap:created "2009-01-01" ; + doap:shortdesc "Various properties for LV2 plugin ports." ; + doap:maintainer ; + doap:developer ; + doap:release [ + doap:revision "1.2" ; + doap:created "2012-10-14" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Use consistent label style." + ] + ] + ] , [ + doap:revision "1.0" ; + doap:created "2012-04-17" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Initial release." + ] + ] + ] ; + lv2:documentation """ + +This vocabulary defines various properties for plugin ports, which can be used +to better describe how a plugin can be controlled. Using this metadata, hosts +can build better UIs for plugins, and provide more advanced automatic +functionality. + +"""^^lv2:Markdown . + +pprops:trigger + lv2:documentation """ + +Indicates that the data item corresponds to a momentary event that has been +detected (control output ports) or is to be triggered (control input ports). +For input ports, the port needs to be reset to lv2:default value after run() +function of the plugin has returned. If the control port is assigned a GUI +widget by the host, the widget should be of auto-off (momentary, one-shot) type +- for example, a push button if the port is also declared as lv2:toggled, or a +series of push button or auto-clear input box with a "Send" button if the port +is also lv2:integer. + +"""^^lv2:Markdown . + +pprops:supportsStrictBounds + lv2:documentation """ + +Indicates use of host support for pprops:hasStrictBounds port property. A +plugin that specifies it as optional feature can omit value clamping for +hasStrictBounds ports, if the feature is supported by the host. When specified +as required feature, it indicates that the plugin does not do any clamping for +input ports that have a pprops:hasStrictBounds property. + +"""^^lv2:Markdown . + +pprops:hasStrictBounds + lv2:documentation """ + +For hosts that support pprops:supportsStrictBounds, this indicates that the +value of the port should never exceed the port's minimum and maximum control +points. For input ports, it moves the responsibility for limiting the range of +values to host, if it supports pprops:supportsStrictBounds. For output ports, +it indicates that values within specified range are to be expected, and +breaking that should be considered by the host as error in plugin +implementation. + +"""^^lv2:Markdown . + +pprops:expensive + lv2:documentation """ + +Input ports only. Indicates that any changes to the port value may trigger +expensive background calculation (for example, regeneration of lookup tables in +a background thread). Any value changes may have not have immediate effect, or +may cause silence or diminished-quality version of the output until background +processing is finished. Ports having this property are typically not well +suited for connection to outputs of other plugins, and should not be offered as +connection targets or for automation by default. + +"""^^lv2:Markdown . + +pprops:causesArtifacts + lv2:documentation """ + +Input ports only. Indicates that any changes to the port value may produce +slight artifacts to produced audio signals (zipper noise and other results of +signal discontinuities). Connecting ports of this type to continuous signals +is not recommended, and when presenting a list of automation targets, those +ports may be marked as artifact-producing. + +"""^^lv2:Markdown . + +pprops:continuousCV + lv2:documentation """ + +Indicates that the port carries a "smooth" modulation signal. Control input +ports of this type are well-suited for being connected to sources of smooth +signals (knobs with smoothing, modulation rate oscillators, output ports with +continuousCV type, etc.). Typically, the plugin with ports which have this +property will implement appropriate smoothing to avoid audio artifacts. For +output ports, this property suggests the value of the port is likely to change +frequently, and describes a smooth signal (so successive values may be +considered points along a curve). + +"""^^lv2:Markdown . + +pprops:discreteCV + lv2:documentation """ + +Indicates that the port carries a "discrete" modulation signal. Input ports of +this type are well-suited for being connected to sources of discrete signals +(switches, buttons, classifiers, event detectors, etc.). May be combined with +pprops:trigger property. For output ports, this property suggests the value of +the port describe discrete values that should be interpreted as steps (and not +points along a curve). + +"""^^lv2:Markdown . + +pprops:logarithmic + lv2:documentation """ + +Indicates that port value behaviour within specified range (bounds) is a value +using logarithmic scale. The lower and upper bounds must be specified, and +must be of the same sign. + +"""^^lv2:Markdown . + +pprops:notAutomatic + lv2:documentation """ + +Indicates that the port is not primarily intended to be fed with modulation +signals from external sources (other plugins, etc.). It is merely a UI hint +and hosts may allow the user to override it. + +"""^^lv2:Markdown . + +pprops:notOnGUI + lv2:documentation """ + +Indicates that the port is not primarily intended to be represented by a +separate control in the user interface window (or any similar mechanism used +for direct, immediate control of control ports). It is merely a UI hint and +hosts may allow the user to override it. + +"""^^lv2:Markdown . + +pprops:displayPriority + lv2:documentation """ + +Indicates how important a port is to controlling the plugin. If a host can +only display some ports of a plugin, it should prefer ports with a higher +display priority. Priorities do not need to be unique, and are only meaningful +when compared to each other. + +"""^^lv2:Markdown . + +pprops:rangeSteps + lv2:documentation """ + +This value indicates into how many evenly-divided points the (control) port +range should be divided for step-wise control. This may be used for changing +the value with step-based controllers like arrow keys, mouse wheel, rotary +encoders, and so on. + +Note that when used with a pprops:logarithmic port, the steps are logarithmic +too, and port value can be calculated as: + + :::c + value = lower * pow(upper / lower, step / (steps - 1)) + +and the step from value is: + + :::c + step = (steps - 1) * log(value / lower) / log(upper / lower) + +where: + + * `value` is the port value. + + * `step` is the step number (0..steps). + + * `steps` is the number of steps (= value of :rangeSteps property). + + * `lower` and upper are the bounds. + +"""^^lv2:Markdown . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/port-props/port-props.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/port-props/port-props.ttl new file mode 100644 index 0000000000..1ddeed074b --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/port-props/port-props.ttl @@ -0,0 +1,79 @@ +@prefix lv2: . +@prefix owl: . +@prefix pprops: . +@prefix rdf: . +@prefix rdfs: . +@prefix xsd: . + + + a owl:Ontology ; + rdfs:label "LV2 Port Properties" ; + rdfs:comment "Various properties for LV2 plugin ports." ; + rdfs:seeAlso . + +pprops:trigger + a lv2:PortProperty ; + rdfs:label "trigger" ; + rdfs:comment "Port is a momentary trigger." . + +pprops:supportsStrictBounds + a lv2:Feature ; + rdfs:label "supports strict bounds" ; + rdfs:comment "A feature indicating plugin support for strict port bounds." . + +pprops:hasStrictBounds + a lv2:PortProperty ; + rdfs:label "has strict bounds" ; + rdfs:comment "Port has strict bounds which are not internally clamped." . + +pprops:expensive + a lv2:PortProperty ; + rdfs:label "changes are expensive" ; + rdfs:comment "Input port is expensive to change." . + +pprops:causesArtifacts + a lv2:PortProperty ; + rdfs:label "changes cause artifacts" ; + rdfs:comment "Input port causes audible artifacts when changed." . + +pprops:continuousCV + a lv2:PortProperty ; + rdfs:label "smooth modulation signal" ; + rdfs:comment "Port carries a smooth modulation signal." . + +pprops:discreteCV + a lv2:PortProperty ; + rdfs:label "discrete modulation signal" ; + rdfs:comment "Port carries a discrete modulation signal." . + +pprops:logarithmic + a lv2:PortProperty ; + rdfs:label "logarithmic" ; + rdfs:comment "Port value is logarithmic." . + +pprops:notAutomatic + a lv2:PortProperty ; + rdfs:label "not automatic" ; + rdfs:comment "Port that is not intended to be fed with a modulation signal." . + +pprops:notOnGUI + a lv2:PortProperty ; + rdfs:label "not on GUI" ; + rdfs:comment "Port that should not be displayed on a GUI." . + +pprops:displayPriority + a rdf:Property , + owl:DatatypeProperty ; + rdfs:domain lv2:Port ; + rdfs:range xsd:nonNegativeInteger ; + rdfs:label "display priority" ; + rdfs:comment "A priority ranking this port in importance to its plugin." . + +pprops:rangeSteps + a rdf:Property , + owl:DatatypeProperty ; + rdfs:domain lv2:Port ; + rdfs:range xsd:nonNegativeInteger ; + rdfs:label "range steps" ; + rdfs:comment "The number of even steps the range should be divided into." . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/presets/manifest.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/presets/manifest.ttl new file mode 100644 index 0000000000..b9cacf570f --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/presets/manifest.ttl @@ -0,0 +1,9 @@ +@prefix lv2: . +@prefix rdfs: . + + + a lv2:Specification ; + lv2:minorVersion 2 ; + lv2:microVersion 8 ; + rdfs:seeAlso . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/presets/presets.h b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/presets/presets.h new file mode 100644 index 0000000000..716ab3278c --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/presets/presets.h @@ -0,0 +1,48 @@ +/* + Copyright 2012-2016 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef LV2_PRESETS_H +#define LV2_PRESETS_H + +/** + @defgroup presets Presets + @ingroup lv2 + + Presets for plugins. + + See for details. + + @{ +*/ + +// clang-format off + +#define LV2_PRESETS_URI "http://lv2plug.in/ns/ext/presets" ///< http://lv2plug.in/ns/ext/presets +#define LV2_PRESETS_PREFIX LV2_PRESETS_URI "#" ///< http://lv2plug.in/ns/ext/presets# + +#define LV2_PRESETS__Bank LV2_PRESETS_PREFIX "Bank" ///< http://lv2plug.in/ns/ext/presets#Bank +#define LV2_PRESETS__Preset LV2_PRESETS_PREFIX "Preset" ///< http://lv2plug.in/ns/ext/presets#Preset +#define LV2_PRESETS__bank LV2_PRESETS_PREFIX "bank" ///< http://lv2plug.in/ns/ext/presets#bank +#define LV2_PRESETS__preset LV2_PRESETS_PREFIX "preset" ///< http://lv2plug.in/ns/ext/presets#preset +#define LV2_PRESETS__value LV2_PRESETS_PREFIX "value" ///< http://lv2plug.in/ns/ext/presets#value + +// clang-format on + +/** + @} +*/ + +#endif /* LV2_PRESETS_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/presets/presets.meta.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/presets/presets.meta.ttl new file mode 100644 index 0000000000..8ff25c15ba --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/presets/presets.meta.ttl @@ -0,0 +1,132 @@ +@prefix dcs: . +@prefix doap: . +@prefix foaf: . +@prefix lv2: . +@prefix pset: . +@prefix rdfs: . + + + a doap:Project ; + doap:license ; + doap:name "LV2 Presets" ; + doap:shortdesc "Presets for LV2 plugins." ; + doap:created "2009-00-00" ; + doap:developer ; + doap:release [ + doap:revision "2.8" ; + doap:created "2012-10-14" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Use consistent label style." + ] , [ + rdfs:label "Add preset banks." + ] + ] + ] , [ + doap:revision "2.6" ; + doap:created "2012-04-17" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add pset:preset property for describing the preset currently applied to a plugin instance." + ] , [ + rdfs:label "Remove pset:appliesTo property, use lv2:appliesTo instead." + ] , [ + rdfs:label "Merge with unified LV2 package." + ] + ] + ] , [ + doap:revision "2.2" ; + doap:created "2011-11-21" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Update packaging." + ] , [ + rdfs:label "Improve documentation." + ] + ] + ] , [ + doap:revision "2.0" ; + doap:created "2010-10-04" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Initial release." + ] + ] + ] ; + lv2:documentation """ + +This is a vocabulary for LV2 plugin presets, that is, named sets of control +values and possibly other state. The structure of a pset:Preset is +deliberately identical to that of an lv2:Plugin, and can be thought of as a +plugin template or overlay. + +Presets may be defined in any bundle, including the plugin's bundle, separate +third party preset bundles, or user preset bundles saved by hosts. Since +preset data tends to be large, it is recommended that plugins describe presets +in a separate file(s) to avoid slowing down hosts. The `manifest.ttl` of a +bundle containing presets should list them like so: + + :::turtle + eg:mypreset + a pset:Preset ; + lv2:appliesTo eg:myplugin ; + rdfs:seeAlso . + +"""^^lv2:Markdown . + +pset:Preset + lv2:documentation """ + +The structure of a Preset deliberately mirrors that of a plugin, so existing +predicates can be used to describe any data associated with the preset. For +example: + + :::turtle + @prefix eg: . + + eg:mypreset + a pset:Preset ; + rdfs:label "One louder" ; + lv2:appliesTo eg:myplugin ; + lv2:port [ + lv2:symbol "volume1" ; + pset:value 11.0 + ] , [ + lv2:symbol "volume2" ; + pset:value 11.0 + ] . + +A Preset SHOULD have at least one lv2:appliesTo property. Each Port on a +Preset MUST have at least a lv2:symbol property and a pset:value property. + +Hosts SHOULD save user presets to a bundle in the user-local LV2 directory (for +example `~/.lv2`) with a name like `_.preset.lv2` +(for example `LV2_Amp_At_Eleven.preset.lv2`), where names are transformed to be +valid LV2 symbols for maximum compatibility. + +"""^^lv2:Markdown . + +pset:value + lv2:documentation """ + +This property is used in a similar way to lv2:default. + +"""^^lv2:Markdown . + +pset:preset + lv2:documentation """ + +Specifies the preset currently applied to a plugin instance. This property may +be useful for saving state, or notifying a plugin instance at run-time about a +preset change. + +"""^^lv2:Markdown . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/presets/presets.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/presets/presets.ttl new file mode 100644 index 0000000000..c1caf43c41 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/presets/presets.ttl @@ -0,0 +1,60 @@ +@prefix lv2: . +@prefix owl: . +@prefix pset: . +@prefix rdf: . +@prefix rdfs: . +@prefix xsd: . + + + a owl:Ontology ; + rdfs:label "LV2 Presets" ; + rdfs:comment "Presets for LV2 plugins." ; + rdfs:seeAlso . + +pset:Bank + a rdfs:Class ; + rdfs:label "Bank" ; + rdfs:subClassOf [ + a owl:Restriction ; + owl:onProperty rdfs:label ; + owl:someValuesFrom xsd:string ; + rdfs:comment "A Bank MUST have at least one string rdfs:label." + ] ; + rdfs:comment "A bank of presets." . + +pset:Preset + a rdfs:Class ; + rdfs:subClassOf lv2:PluginBase ; + rdfs:label "Preset" ; + rdfs:comment "A preset for an LV2 plugin." ; + rdfs:subClassOf [ + a owl:Restriction ; + owl:onProperty rdfs:label ; + owl:someValuesFrom xsd:string ; + rdfs:comment "A Preset MUST have at least one string rdfs:label." + ] . + +pset:bank + a rdf:Property , + owl:ObjectProperty ; + rdfs:domain pset:Preset ; + rdfs:range pset:Bank ; + rdfs:label "bank" ; + rdfs:comment "The bank this preset belongs to." . + +pset:value + a rdf:Property , + owl:DatatypeProperty , + owl:FunctionalProperty ; + rdfs:domain lv2:PortBase ; + rdfs:label "value" ; + rdfs:comment "The value of a port in a preset." . + +pset:preset + a rdf:Property , + owl:ObjectProperty ; + rdfs:domain lv2:PluginBase ; + rdfs:range pset:Preset ; + rdfs:label "preset" ; + rdfs:comment "The preset currently applied to a plugin instance." . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/resize-port/manifest.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/resize-port/manifest.ttl new file mode 100644 index 0000000000..9fae8b854b --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/resize-port/manifest.ttl @@ -0,0 +1,9 @@ +@prefix lv2: . +@prefix rdfs: . + + + a lv2:Specification ; + lv2:minorVersion 1 ; + lv2:microVersion 0 ; + rdfs:seeAlso . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/resize-port/resize-port.h b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/resize-port/resize-port.h new file mode 100644 index 0000000000..a3a11c4d6b --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/resize-port/resize-port.h @@ -0,0 +1,89 @@ +/* + Copyright 2007-2016 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef LV2_RESIZE_PORT_H +#define LV2_RESIZE_PORT_H + +/** + @defgroup resize-port Resize Port + @ingroup lv2 + + Dynamically sized LV2 port buffers. + + @{ +*/ + +#include +#include + +// clang-format off + +#define LV2_RESIZE_PORT_URI "http://lv2plug.in/ns/ext/resize-port" ///< http://lv2plug.in/ns/ext/resize-port +#define LV2_RESIZE_PORT_PREFIX LV2_RESIZE_PORT_URI "#" ///< http://lv2plug.in/ns/ext/resize-port# + +#define LV2_RESIZE_PORT__asLargeAs LV2_RESIZE_PORT_PREFIX "asLargeAs" ///< http://lv2plug.in/ns/ext/resize-port#asLargeAs +#define LV2_RESIZE_PORT__minimumSize LV2_RESIZE_PORT_PREFIX "minimumSize" ///< http://lv2plug.in/ns/ext/resize-port#minimumSize +#define LV2_RESIZE_PORT__resize LV2_RESIZE_PORT_PREFIX "resize" ///< http://lv2plug.in/ns/ext/resize-port#resize + +// clang-format on + +#ifdef __cplusplus +extern "C" { +#endif + +/** A status code for state functions. */ +typedef enum { + LV2_RESIZE_PORT_SUCCESS = 0, /**< Completed successfully. */ + LV2_RESIZE_PORT_ERR_UNKNOWN = 1, /**< Unknown error. */ + LV2_RESIZE_PORT_ERR_NO_SPACE = 2 /**< Insufficient space. */ +} LV2_Resize_Port_Status; + +/** Opaque data for resize method. */ +typedef void* LV2_Resize_Port_Feature_Data; + +/** Host feature to allow plugins to resize their port buffers. */ +typedef struct { + /** Opaque data for resize method. */ + LV2_Resize_Port_Feature_Data data; + + /** + Resize a port buffer to at least `size` bytes. + + This function MAY return an error, in which case the port buffer was not + resized and the port is still connected to the same location. Plugins + MUST gracefully handle this situation. + + This function is in the audio threading class. + + The host MUST preserve the contents of the port buffer when resizing. + + Plugins MAY resize a port many times in a single run callback. Hosts + SHOULD make this as inexpensive as possible. + */ + LV2_Resize_Port_Status (*resize)(LV2_Resize_Port_Feature_Data data, + uint32_t index, + size_t size); +} LV2_Resize_Port_Resize; + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +/** + @} +*/ + +#endif /* LV2_RESIZE_PORT_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/resize-port/resize-port.meta.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/resize-port/resize-port.meta.ttl new file mode 100644 index 0000000000..d44620cced --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/resize-port/resize-port.meta.ttl @@ -0,0 +1,74 @@ +@prefix dcs: . +@prefix doap: . +@prefix foaf: . +@prefix lv2: . +@prefix rdfs: . +@prefix rsz: . + + + a doap:Project ; + doap:name "LV2 Resize Port" ; + doap:shortdesc "Dynamically sized LV2 port buffers." ; + doap:created "2007-00-00" ; + doap:developer ; + doap:release [ + doap:revision "1.0" ; + doap:created "2012-04-17" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Initial release." + ] + ] + ] ; + lv2:documentation """ + +This extension defines a feature, rsz:resize, which allows plugins to +dynamically resize their output port buffers. + +In addition to the dynamic feature, there are properties which describe the +space required for a particular port buffer which can be used statically in +data files. + +"""^^lv2:Markdown . + +rsz:resize + lv2:documentation """ + +A feature to resize output port buffers in LV2_Plugin_Descriptor::run(). + +To support this feature, the host must pass an LV2_Feature to the plugin's +instantiate method with URI LV2_RESIZE_PORT__resize and a pointer to a +LV2_Resize_Port_Resize structure. This structure provides a resize_port +function which plugins may use to resize output port buffers as necessary. + +"""^^lv2:Markdown . + +rsz:asLargeAs + lv2:documentation """ + +Indicates that a port requires at least as much buffer space as the port with +the given symbol on the same plugin instance. This may be used for any ports, +but is generally most useful to indicate an output port must be at least as +large as some input port (because it will copy from it). If a port is +asLargeAs several ports, it is asLargeAs the largest such port (not the sum of +those ports' sizes). + +The host guarantees that whenever an ObjectPort's run method is called, any +output `O` that is rsz:asLargeAs an input `I` is connected to a buffer large +enough to copy `I`, or `NULL` if the port is lv2:connectionOptional. + +"""^^lv2:Markdown . + +rsz:minimumSize + lv2:documentation """ + +Indicates that a port requires a buffer at least this large, in bytes. Any +host that supports the resize-port feature MUST connect any port with a +minimumSize specified to a buffer at least as large as the value given for this +property. Any host, especially those that do NOT support dynamic port +resizing, SHOULD do so or reduced functionality may result. + +"""^^lv2:Markdown . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/resize-port/resize-port.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/resize-port/resize-port.ttl new file mode 100644 index 0000000000..29cd18b3de --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/resize-port/resize-port.ttl @@ -0,0 +1,36 @@ +@prefix lv2: . +@prefix owl: . +@prefix rdf: . +@prefix rdfs: . +@prefix rsz: . +@prefix xsd: . + + + a owl:Ontology ; + rdfs:label "LV2 Resize Port" ; + rdfs:comment "Dynamically sized LV2 port buffers." ; + rdfs:seeAlso , + . + +rsz:resize + a lv2:Feature ; + rdfs:label "resize" ; + rdfs:comment "A feature for resizing output port buffers." . + +rsz:asLargeAs + a rdf:Property , + owl:DatatypeProperty ; + rdfs:domain lv2:Port ; + rdfs:range lv2:Symbol ; + rdfs:label "as large as" ; + rdfs:comment "Port that this port must have at least as much buffer space as." . + +rsz:minimumSize + a rdf:Property , + owl:DatatypeProperty , + owl:FunctionalProperty ; + rdfs:domain lv2:Port ; + rdfs:range xsd:nonNegativeInteger ; + rdfs:label "minimum size" ; + rdfs:comment "Minimum buffer size required by a port, in bytes." . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/state/manifest.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/state/manifest.ttl new file mode 100644 index 0000000000..02e7aa1388 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/state/manifest.ttl @@ -0,0 +1,9 @@ +@prefix lv2: . +@prefix rdfs: . + + + a lv2:Specification ; + lv2:minorVersion 2 ; + lv2:microVersion 8 ; + rdfs:seeAlso . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/state/state.h b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/state/state.h new file mode 100644 index 0000000000..70972d841b --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/state/state.h @@ -0,0 +1,392 @@ +/* + Copyright 2010-2016 David Robillard + Copyright 2010 Leonard Ritter + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef LV2_STATE_H +#define LV2_STATE_H + +/** + @defgroup state State + @ingroup lv2 + + An interface for LV2 plugins to save and restore state. + + See for details. + + @{ +*/ + +#include "lv2/core/lv2.h" + +#include +#include + +// clang-format off + +#define LV2_STATE_URI "http://lv2plug.in/ns/ext/state" ///< http://lv2plug.in/ns/ext/state +#define LV2_STATE_PREFIX LV2_STATE_URI "#" ///< http://lv2plug.in/ns/ext/state# + +#define LV2_STATE__State LV2_STATE_PREFIX "State" ///< http://lv2plug.in/ns/ext/state#State +#define LV2_STATE__interface LV2_STATE_PREFIX "interface" ///< http://lv2plug.in/ns/ext/state#interface +#define LV2_STATE__loadDefaultState LV2_STATE_PREFIX "loadDefaultState" ///< http://lv2plug.in/ns/ext/state#loadDefaultState +#define LV2_STATE__freePath LV2_STATE_PREFIX "freePath" ///< http://lv2plug.in/ns/ext/state#freePath +#define LV2_STATE__makePath LV2_STATE_PREFIX "makePath" ///< http://lv2plug.in/ns/ext/state#makePath +#define LV2_STATE__mapPath LV2_STATE_PREFIX "mapPath" ///< http://lv2plug.in/ns/ext/state#mapPath +#define LV2_STATE__state LV2_STATE_PREFIX "state" ///< http://lv2plug.in/ns/ext/state#state +#define LV2_STATE__threadSafeRestore LV2_STATE_PREFIX "threadSafeRestore" ///< http://lv2plug.in/ns/ext/state#threadSafeRestore +#define LV2_STATE__StateChanged LV2_STATE_PREFIX "StateChanged" ///< http://lv2plug.in/ns/ext/state#StateChanged + +// clang-format on + +#ifdef __cplusplus +extern "C" { +#endif + +typedef void* LV2_State_Handle; ///< Opaque handle for state save/restore +typedef void* + LV2_State_Free_Path_Handle; ///< Opaque handle for state:freePath feature +typedef void* + LV2_State_Map_Path_Handle; ///< Opaque handle for state:mapPath feature +typedef void* + LV2_State_Make_Path_Handle; ///< Opaque handle for state:makePath feature + +/** + Flags describing value characteristics. + + These flags are used along with the value's type URI to determine how to + (de-)serialise the value data, or whether it is even possible to do so. +*/ +typedef enum { + /** + Plain Old Data. + + Values with this flag contain no pointers or references to other areas + of memory. It is safe to copy POD values with a simple memcpy and store + them for the duration of the process. A POD value is not necessarily + safe to trasmit between processes or machines (for example, filenames + are POD), see LV2_STATE_IS_PORTABLE for details. + + Implementations MUST NOT attempt to copy or serialise a non-POD value if + they do not understand its type (and thus know how to correctly do so). + */ + LV2_STATE_IS_POD = 1, + + /** + Portable (architecture independent) data. + + Values with this flag are in a format that is usable on any + architecture. A portable value saved on one machine can be restored on + another machine regardless of architecture. The format of portable + values MUST NOT depend on architecture-specific properties like + endianness or alignment. Portable values MUST NOT contain filenames. + */ + LV2_STATE_IS_PORTABLE = 1 << 1, + + /** + Native data. + + This flag is used by the host to indicate that the saved data is only + going to be used locally in the currently running process (for things + like instance duplication or snapshots), so the plugin should use the + most efficient representation possible and not worry about serialisation + and portability. + */ + LV2_STATE_IS_NATIVE = 1 << 2 +} LV2_State_Flags; + +/** A status code for state functions. */ +typedef enum { + LV2_STATE_SUCCESS = 0, /**< Completed successfully. */ + LV2_STATE_ERR_UNKNOWN = 1, /**< Unknown error. */ + LV2_STATE_ERR_BAD_TYPE = 2, /**< Failed due to unsupported type. */ + LV2_STATE_ERR_BAD_FLAGS = 3, /**< Failed due to unsupported flags. */ + LV2_STATE_ERR_NO_FEATURE = 4, /**< Failed due to missing features. */ + LV2_STATE_ERR_NO_PROPERTY = 5, /**< Failed due to missing property. */ + LV2_STATE_ERR_NO_SPACE = 6 /**< Failed due to insufficient space. */ +} LV2_State_Status; + +/** + A host-provided function to store a property. + @param handle Must be the handle passed to LV2_State_Interface.save(). + @param key The key to store `value` under (URID). + @param value Pointer to the value to be stored. + @param size The size of `value` in bytes. + @param type The type of `value` (URID). + @param flags LV2_State_Flags for `value`. + @return 0 on success, otherwise a non-zero error code. + + The host passes a callback of this type to LV2_State_Interface.save(). This + callback is called repeatedly by the plugin to store all the properties that + describe its current state. + + DO NOT INVENT NONSENSE URI SCHEMES FOR THE KEY. Best is to use keys from + existing vocabularies. If nothing appropriate is available, use http URIs + that point to somewhere you can host documents so documentation can be made + resolvable (typically a child of the plugin or project URI). If this is not + possible, invent a URN scheme, e.g. urn:myproj:whatever. The plugin MUST + NOT pass an invalid URI key. + + The host MAY fail to store a property for whatever reason, but SHOULD + store any property that is LV2_STATE_IS_POD and LV2_STATE_IS_PORTABLE. + Implementations SHOULD use the types from the LV2 Atom extension + (http://lv2plug.in/ns/ext/atom) wherever possible. The plugin SHOULD + attempt to fall-back and avoid the error if possible. + + Note that `size` MUST be > 0, and `value` MUST point to a valid region of + memory `size` bytes long (this is required to make restore unambiguous). + + The plugin MUST NOT attempt to use this function outside of the + LV2_State_Interface.restore() context. +*/ +typedef LV2_State_Status (*LV2_State_Store_Function)(LV2_State_Handle handle, + uint32_t key, + const void* value, + size_t size, + uint32_t type, + uint32_t flags); + +/** + A host-provided function to retrieve a property. + @param handle Must be the handle passed to LV2_State_Interface.restore(). + @param key The key of the property to retrieve (URID). + @param size (Output) If non-NULL, set to the size of the restored value. + @param type (Output) If non-NULL, set to the type of the restored value. + @param flags (Output) If non-NULL, set to the flags for the restored value. + @return A pointer to the restored value (object), or NULL if no value + has been stored under `key`. + + A callback of this type is passed by the host to + LV2_State_Interface.restore(). This callback is called repeatedly by the + plugin to retrieve any properties it requires to restore its state. + + The returned value MUST remain valid until LV2_State_Interface.restore() + returns. The plugin MUST NOT attempt to use this function, or any value + returned from it, outside of the LV2_State_Interface.restore() context. +*/ +typedef const void* (*LV2_State_Retrieve_Function)(LV2_State_Handle handle, + uint32_t key, + size_t* size, + uint32_t* type, + uint32_t* flags); + +/** + LV2 Plugin State Interface. + + When the plugin's extension_data is called with argument + LV2_STATE__interface, the plugin MUST return an LV2_State_Interface + structure, which remains valid for the lifetime of the plugin. + + The host can use the contained function pointers to save and restore the + state of a plugin instance at any time, provided the threading restrictions + of the functions are met. + + Stored data is only guaranteed to be compatible between instances of plugins + with the same URI (i.e. if a change to a plugin would cause a fatal error + when restoring state saved by a previous version of that plugin, the plugin + URI MUST change just as it must when ports change incompatibly). Plugin + authors should consider this possibility, and always store sensible data + with meaningful types to avoid such problems in the future. +*/ +typedef struct { + /** + Save plugin state using a host-provided `store` callback. + + @param instance The instance handle of the plugin. + @param store The host-provided store callback. + @param handle An opaque pointer to host data which MUST be passed as the + handle parameter to `store` if it is called. + @param flags Flags describing desired properties of this save. These + flags may be used to determine the most appropriate values to store. + @param features Extensible parameter for passing any additional + features to be used for this save. + + The plugin is expected to store everything necessary to completely + restore its state later. Plugins SHOULD store simple POD data whenever + possible, and consider the possibility of state being restored much + later on a different machine. + + The `handle` pointer and `store` function MUST NOT be used + beyond the scope of save(). + + This function has its own special threading class: it may not be called + concurrently with any "Instantiation" function, but it may be called + concurrently with functions in any other class, unless the definition of + that class prohibits it (for example, it may not be called concurrently + with a "Discovery" function, but it may be called concurrently with an + "Audio" function. The plugin is responsible for any locking or + lock-free techniques necessary to make this possible. + + Note that in the simple case where state is only modified by restore(), + there are no synchronization issues since save() is never called + concurrently with restore() (though run() may read it during a save). + + Plugins that dynamically modify state while running, however, must take + care to do so in such a way that a concurrent call to save() will save a + consistent representation of plugin state for a single instant in time. + */ + LV2_State_Status (*save)(LV2_Handle instance, + LV2_State_Store_Function store, + LV2_State_Handle handle, + uint32_t flags, + const LV2_Feature* const* features); + + /** + Restore plugin state using a host-provided `retrieve` callback. + + @param instance The instance handle of the plugin. + @param retrieve The host-provided retrieve callback. + @param handle An opaque pointer to host data which MUST be passed as the + handle parameter to `retrieve` if it is called. + @param flags Currently unused. + @param features Extensible parameter for passing any additional + features to be used for this restore. + + The plugin MAY assume a restored value was set by a previous call to + LV2_State_Interface.save() by a plugin with the same URI. + + The plugin MUST gracefully fall back to a default value when a value can + not be retrieved. This allows the host to reset the plugin state with + an empty map. + + The `handle` pointer and `store` function MUST NOT be used + beyond the scope of restore(). + + This function is in the "Instantiation" threading class as defined by + LV2. This means it MUST NOT be called concurrently with any other + function on the same plugin instance. + */ + LV2_State_Status (*restore)(LV2_Handle instance, + LV2_State_Retrieve_Function retrieve, + LV2_State_Handle handle, + uint32_t flags, + const LV2_Feature* const* features); +} LV2_State_Interface; + +/** + Feature data for state:mapPath (@ref LV2_STATE__mapPath). +*/ +typedef struct { + /** + Opaque host data. + */ + LV2_State_Map_Path_Handle handle; + + /** + Map an absolute path to an abstract path for use in plugin state. + @param handle MUST be the `handle` member of this struct. + @param absolute_path The absolute path of a file. + @return An abstract path suitable for use in plugin state. + + The plugin MUST use this function to map any paths that will be stored + in plugin state. The returned value is an abstract path which MAY not + be an actual file system path; absolute_path() MUST be used to map + it to an actual path in order to use the file. + + Plugins MUST NOT make any assumptions about abstract paths except that + they can be mapped back to the absolute path of the "same" file (though + not necessarily the same original path) using absolute_path(). + + This function may only be called within the context of + LV2_State_Interface methods. The caller must free the returned value + with LV2_State_Free_Path.free_path(). + */ + char* (*abstract_path)(LV2_State_Map_Path_Handle handle, + const char* absolute_path); + + /** + Map an abstract path from plugin state to an absolute path. + @param handle MUST be the `handle` member of this struct. + @param abstract_path An abstract path (typically from plugin state). + @return An absolute file system path. + + The plugin MUST use this function in order to actually open or otherwise + use any paths loaded from plugin state. + + This function may only be called within the context of + LV2_State_Interface methods. The caller must free the returned value + with LV2_State_Free_Path.free_path(). + */ + char* (*absolute_path)(LV2_State_Map_Path_Handle handle, + const char* abstract_path); +} LV2_State_Map_Path; + +/** + Feature data for state:makePath (@ref LV2_STATE__makePath). +*/ +typedef struct { + /** + Opaque host data. + */ + LV2_State_Make_Path_Handle handle; + + /** + Return a path the plugin may use to create a new file. + @param handle MUST be the `handle` member of this struct. + @param path The path of the new file within a namespace unique to this + plugin instance. + @return The absolute path to use for the new file. + + This function can be used by plugins to create files and directories, + either at state saving time (if this feature is passed to + LV2_State_Interface.save()) or any time (if this feature is passed to + LV2_Descriptor.instantiate()). + + The host MUST do whatever is necessary for the plugin to be able to + create a file at the returned path (for example, using fopen()), + including creating any leading directories. + + If this function is passed to LV2_Descriptor.instantiate(), it may be + called from any non-realtime context. If it is passed to + LV2_State_Interface.save(), it may only be called within the dynamic + scope of that function call. + + The caller must free the returned value with + LV2_State_Free_Path.free_path(). + */ + char* (*path)(LV2_State_Make_Path_Handle handle, const char* path); +} LV2_State_Make_Path; + +/** + Feature data for state:freePath (@ref LV2_STATE__freePath). +*/ +typedef struct { + /** + Opaque host data. + */ + LV2_State_Free_Path_Handle handle; + + /** + Free a path returned by a state feature. + + @param handle MUST be the `handle` member of this struct. + @param path The path previously returned by a state feature. + + This function can be used by plugins to free paths allocated by the host + and returned by state features (LV2_State_Map_Path.abstract_path(), + LV2_State_Map_Path.absolute_path(), and LV2_State_Make_Path.path()). + */ + void (*free_path)(LV2_State_Free_Path_Handle handle, char* path); +} LV2_State_Free_Path; + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +/** + @} +*/ + +#endif /* LV2_STATE_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/state/state.meta.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/state/state.meta.ttl new file mode 100644 index 0000000000..6f54641dd7 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/state/state.meta.ttl @@ -0,0 +1,467 @@ +@prefix dcs: . +@prefix doap: . +@prefix foaf: . +@prefix lv2: . +@prefix rdfs: . +@prefix state: . + + + a doap:Project ; + doap:created "2010-11-09" ; + doap:name "LV2 State" ; + doap:shortdesc "An interface for LV2 plugins to save and restore state." ; + doap:license ; + doap:developer , + ; + doap:maintainer ; + doap:release [ + doap:revision "2.8" ; + doap:created "2021-01-07" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Fix state:StateChanged URI in metadata and documentation." + ] + ] + ] , [ + doap:revision "2.6" ; + doap:created "2020-04-26" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add state:freePath feature." + ] + ] + ] , [ + doap:revision "2.4" ; + doap:created "2019-02-03" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add state:StateChanged for notification events." + ] + ] + ] , [ + doap:revision "2.2" ; + doap:created "2016-07-31" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add LV2_STATE_ERR_NO_SPACE status flag." + ] , [ + rdfs:label "Add state:threadSafeRestore feature for dropout-free state restoration." + ] + ] + ] , [ + doap:revision "2.0" ; + doap:created "2013-01-16" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add state:loadDefaultState feature so plugins can have their default state loaded without hard-coding default state as a special case." + ] + ] + ] , [ + doap:revision "1.2" ; + doap:created "2012-10-14" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Use consistent label style." + ] + ] + ] , [ + doap:revision "1.0" ; + doap:created "2012-04-17" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Initial release." + ] + ] + ] ; + lv2:documentation """ + +This extension defines a simple mechanism that allows hosts to save and restore +a plugin instance's state. The goal is for an instance's state to be +completely described by port values and a simple dictionary. + +The state defined here is conceptually a key:value dictionary, with URI keys +and values of any type. For performance reasons the key and value type are +actually a "URID", a URI mapped to an integer. A single key:value pair is +called a "property". + +This state model is simple yet has many benefits: + + * Both fast and extensible thanks to URID keys. + + * No limitations on possible value types. + + * Easy to serialise in almost any format. + + * Easy to store in a typical "map" or "dictionary" data structure. + + * Elegantly described in Turtle, so state can be described in LV2 data files + (including presets). + + * Does not impose any file formats, data structures, or file system + requirements. + + * Suitable for portable persistent state as well as fast in-memory snapshots. + + * Keys _may_ be well-defined and used meaningfully across several + implementations. + + * State _may_ be dynamic, but plugins are not required to have a dynamic + dictionary data structure available. + +To implement state, the plugin provides a state:interface to the host. To save +or restore, the host calls LV2_State_Interface::save() or +LV2_State_Interface::restore(), passing a callback to be used for handling a +single property. The host is free to implement property storage and retrieval +in any way. + +Since value types are defined by URI, any type is possible. However, a set of +standard types is defined by the [LV2 Atom](atom.html) extension. Use of these +types is recommended. Hosts MUST implement at least +[atom:String](atom.html#String), which is simply a C string. + +### Referring to Files + +Plugins may need to refer to existing files (such as loaded samples) in their +state. This is done by storing the file's path as a property just like any +other value. However, there are some rules which MUST be followed when storing +paths, see state:mapPath for details. Plugins MUST use the type +[atom:Path](atom.html#Path) for all paths in their state. + +Plugins are strongly encouraged to avoid creating files, instead storing all +state as properties. However, occasionally the ability to create files is +necessary. To make this possible, the host can provide the feature +state:makePath which allocates paths for plugin-created files. Plugins MUST +NOT create files in any other locations. + +### Plugin Code Example + + :::c + + /* Namespace for this plugin's keys. This SHOULD be something that could be + published as a document, even if that document does not exist right now. + */ + #define NS_MY "http://example.org/myplugin/schema#" + + #define DEFAULT_GREETING "Hello" + + LV2_Handle + my_instantiate(...) + { + MyPlugin* plugin = ...; + plugin->uris.atom_String = map_uri(LV2_ATOM__String); + plugin->uris.my_greeting = map_uri(NS_MY "greeting"); + plugin->state.greeting = strdup(DEFAULT_GREETING); + return plugin; + } + + LV2_State_Status + my_save(LV2_Handle instance, + LV2_State_Store_Function store, + LV2_State_Handle handle, + uint32_t flags, + const LV2_Feature *const * features) + { + MyPlugin* plugin = (MyPlugin*)instance; + const char* greeting = plugin->state.greeting; + + store(handle, + plugin->uris.my_greeting, + greeting, + strlen(greeting) + 1, // Careful! Need space for terminator + plugin->uris.atom_String, + LV2_STATE_IS_POD | LV2_STATE_IS_PORTABLE); + + return LV2_STATE_SUCCESS; + } + + LV2_State_Status + my_restore(LV2_Handle instance, + LV2_State_Retrieve_Function retrieve, + LV2_State_Handle handle, + uint32_t flags, + const LV2_Feature *const * features) + { + MyPlugin* plugin = (MyPlugin*)instance; + + size_t size; + uint32_t type; + uint32_t flags; + const char* greeting = retrieve( + handle, plugin->uris.my_greeting, &size, &type, &flags); + + if (greeting) { + free(plugin->state->greeting); + plugin->state->greeting = strdup(greeting); + } else { + plugin->state->greeting = strdup(DEFAULT_GREETING); + } + + return LV2_STATE_SUCCESS; + } + + const void* + my_extension_data(const char* uri) + { + static const LV2_State_Interface state_iface = { my_save, my_restore }; + if (!strcmp(uri, LV2_STATE__interface)) { + return &state_iface; + } + } + +### Host Code Example + + :::c + LV2_State_Status + store_callback(LV2_State_Handle handle, + uint32_t key, + const void* value, + size_t size, + uint32_t type, + uint32_t flags) + { + if ((flags & LV2_STATE_IS_POD)) { + // We only care about POD since we're keeping state in memory only. + // Disk or network use would also require LV2_STATE_IS_PORTABLE. + Map* state_map = (Map*)handle; + state_map->insert(key, Value(copy(value), size, type)); + return LV2_STATE_SUCCESS;; + } else { + return LV2_STATE_ERR_BAD_FLAGS; // Non-POD events are unsupported + } + } + + Map + get_plugin_state(LV2_Handle instance) + { + LV2_State* state = instance.extension_data(LV2_STATE__interface); + + // Request a fast/native/POD save, since we're just copying in memory + Map state_map; + state.save(instance, store_callback, &state_map, + LV2_STATE_IS_POD|LV2_STATE_IS_NATIVE); + + return state_map; + } + +### Extensions to this Specification + +It is likely that other interfaces for working with plugin state will be +developed as needed. This is encouraged, however everything SHOULD work within +the state _model_ defined here. That is, **do not complicate the state +model**. Implementations can assume the following: + + * The current port values and state dictionary completely describe a plugin + instance, at least well enough that saving and restoring will yield an + "identical" instance from the user's perspective. + + * Hosts are not expected to save and/or restore any other attributes of a + plugin instance. + +### The "Property Principle" + +The main benefit of this meaningful state model is that it can double as a +plugin control/query mechanism. For plugins that require more advanced control +than simple control ports, instead of defining a set of commands, define +properties whose values can be set appropriately. This provides both a way to +control and save that state "for free", since there is no need to define +commands _and_ a set of properties for storing their effects. In particular, +this is a good way for UIs to achieve more advanced control of plugins. + +This "property principle" is summed up in the phrase: "Don't stop; set playing +to false". + +This extension does not define a dynamic mechanism for state access and +manipulation. The [LV2 Patch](patch.html) extension defines a generic set of +messages which can be used to access or manipulate properties, and the [LV2 +Atom](atom.html) extension defines a port type and data container capable of +transmitting those messages. + +"""^^lv2:Markdown . + +state:interface + lv2:documentation """ + +A structure (LV2_State_Interface) which contains functions to be called by the +host to save and restore state. In order to support this extension, the plugin +must return a valid LV2_State_Interface from LV2_Descriptor::extension_data() +when it is called with URI LV2_STATE__interface. + +The plugin data file should describe this like so: + + :::turtle + @prefix state: . + + + a lv2:Plugin ; + lv2:extensionData state:interface . + +"""^^lv2:Markdown . + +state:State + lv2:documentation """ + +This type should be used wherever instance state is described. The properties +of a resource with this type correspond directly to the properties of the state +dictionary (except the property that states it has this type). + +"""^^lv2:Markdown . + +state:loadDefaultState + lv2:documentation """ + +This feature indicates that the plugin has default state listed with the +state:state property which should be loaded by the host before running the +plugin. Requiring this feature allows plugins to implement a single state +loading mechanism which works for initialisation as well as restoration, +without having to hard-code default state. + +To support this feature, the host MUST restore the default state after +instantiating the plugin but before calling run(). + +"""^^lv2:Markdown . + +state:state + lv2:documentation """ + +This property may be used anywhere a state needs to be described, for example: + + :::turtle + @prefix eg: . + + + state:state [ + eg:somekey "some value" ; + eg:someotherkey "some other value" ; + eg:favourite-number 2 + ] . + +"""^^lv2:Markdown . + +state:mapPath + lv2:documentation """ + +This feature maps absolute paths to/from abstract paths which are stored +in state. To support this feature a host must pass an LV2_Feature with URI +LV2_STATE__mapPath and data pointed to an LV2_State_Map_Path to the plugin's +LV2_State_Interface methods. + +The plugin MUST map _all_ paths stored in its state (including those inside any +files). This is necessary so that hosts can handle file system references +correctly, for example to share common files, or bundle state for distribution +or archival. + +For example, a plugin may write a path to a state file like so: + + :::c + void write_path(LV2_State_Map_Path* map_path, FILE* myfile, const char* path) + { + char* abstract_path = map_path->abstract_path(map_path->handle, path); + fprintf(myfile, "%s", abstract_path); + free(abstract_path); + } + +Then, later reload the path like so: + + :::c + char* read_path(LV2_State_Map_Path* map_path, FILE* myfile) + { + /* Obviously this is not production quality code! */ + char abstract_path[1024]; + fscanf(myfile, "%s", abstract_path); + return map_path->absolute_path(map_path->handle, abstract_path); + } + +"""^^lv2:Markdown . + +state:makePath + lv2:documentation """ + +This feature allows plugins to create new files and/or directories. To support +this feature the host passes an LV2_Feature with URI LV2_STATE__makePath and +data pointed to an LV2_State_Make_Path to the plugin. The host may make this +feature available only during save by passing it to +LV2_State_Interface::save(), or available any time by passing it to +LV2_Descriptor::instantiate(). If passed to LV2_State_Interface::save(), the +feature MUST NOT be used beyond the scope of that call. + +The plugin is guaranteed a hierarchical namespace unique to that plugin +instance, and may expect the returned path to have the requested path as a +suffix. There is one such namespace, even if the feature is passed to both +LV2_Descriptor::instantiate() and LV2_State_Interface::save(). Beyond this, +the plugin MUST NOT make any assumptions about the returned paths. + +Like any other paths, the plugin MUST map these paths using state:mapPath +before storing them in state. The plugin MUST NOT assume these paths will be +available across a save/restore otherwise, that is, only mapped paths saved to +state are persistent, any other created paths are temporary. + +For example, a plugin may create a file in a subdirectory like so: + + :::c + char* save_myfile(LV2_State_Make_Path* make_path) + { + char* path = make_path->path(make_path->handle, "foo/bar/myfile.txt"); + FILE* myfile = fopen(path, 'w'); + fprintf(myfile, "I am some data"); + fclose(myfile); + return path; + } + +"""^^lv2:Markdown . + +state:threadSafeRestore + lv2:documentation """ + +If a plugin supports this feature, its LV2_State_Interface::restore method is +thread-safe and may be called concurrently with audio class functions. + +To support this feature, the host MUST pass a +[work:schedule](worker.html#schedule) feature to the restore method, which will +be used to complete the state restoration. The usual mechanics of the worker +apply: the host will call the plugin's work method, which emits a response +which is later applied in the audio thread. + +The host is not required to block audio processing while restore() and work() +load the state, so this feature allows state to be restored without dropouts. + +"""^^lv2:Markdown . + +state:freePath + lv2:documentation """ + +This feature provides a function that can be used by plugins to free paths that +were allocated by the host via other state features (state:mapPath and +state:makePath). + +"""^^lv2:Markdown . + +state:StateChanged + lv2:documentation """ + +A notification that the internal state of the plugin has been changed in a way +that the host can not otherwise know about. + +This is a one-way notification, intended to be used as the type of an +[Object](atom.html#Object) sent from plugins when necessary. + +Plugins SHOULD emit such an event whenever a change has occurred that would +result in a different state being saved, but not when the host explicity makes +a change which it knows is likely to have that effect, such as changing a +parameter. + +"""^^lv2:Markdown . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/state/state.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/state/state.ttl new file mode 100644 index 0000000000..19ccaa22bb --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/state/state.ttl @@ -0,0 +1,60 @@ +@prefix lv2: . +@prefix owl: . +@prefix rdf: . +@prefix rdfs: . +@prefix state: . + + + a owl:Ontology ; + rdfs:label "LV2 State" ; + rdfs:comment "An interface for LV2 plugins to save and restore state." ; + rdfs:seeAlso , + . + +state:interface + a lv2:ExtensionData ; + rdfs:label "interface" ; + rdfs:comment "A plugin interface for saving and restoring state." . + +state:State + a rdfs:Class ; + rdfs:label "State" ; + rdfs:comment "LV2 plugin state." . + +state:loadDefaultState + a lv2:Feature ; + rdfs:label "load default state" ; + rdfs:comment "A feature indicating that the plugin has default state." . + +state:state + a rdf:Property , + owl:ObjectProperty ; + rdfs:label "state" ; + rdfs:range state:State ; + rdfs:comment "The state of an LV2 plugin instance." . + +state:mapPath + a lv2:Feature ; + rdfs:label "map path" ; + rdfs:comment "A feature for mapping between absolute and abstract file paths." . + +state:makePath + a lv2:Feature ; + rdfs:label "make path" ; + rdfs:comment "A feature for creating new files and directories." . + +state:threadSafeRestore + a lv2:Feature ; + rdfs:label "thread-safe restore" ; + rdfs:comment "A feature indicating support for thread-safe state restoration." . + +state:freePath + a lv2:Feature ; + rdfs:label "free path" ; + rdfs:comment "A feature for freeing paths allocated by the host." . + +state:StateChanged + a rdfs:Class ; + rdfs:label "State Changed" ; + rdfs:comment "A notification that the internal state of the plugin has changed." . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/time/manifest.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/time/manifest.ttl new file mode 100644 index 0000000000..d80aa752de --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/time/manifest.ttl @@ -0,0 +1,9 @@ +@prefix lv2: . +@prefix rdfs: . + + + a lv2:Specification ; + lv2:minorVersion 1 ; + lv2:microVersion 6 ; + rdfs:seeAlso . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/time/time.h b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/time/time.h new file mode 100644 index 0000000000..1dce2199e1 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/time/time.h @@ -0,0 +1,59 @@ +/* + Copyright 2011-2016 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef LV2_TIME_H +#define LV2_TIME_H + +/** + @defgroup time Time + @ingroup lv2 + + Properties for describing time. + + Note the time extension is purely data, this header merely defines URIs for + convenience. + + See for details. + + @{ +*/ + +// clang-format off + +#define LV2_TIME_URI "http://lv2plug.in/ns/ext/time" ///< http://lv2plug.in/ns/ext/time +#define LV2_TIME_PREFIX LV2_TIME_URI "#" ///< http://lv2plug.in/ns/ext/time# + +#define LV2_TIME__Time LV2_TIME_PREFIX "Time" ///< http://lv2plug.in/ns/ext/time#Time +#define LV2_TIME__Position LV2_TIME_PREFIX "Position" ///< http://lv2plug.in/ns/ext/time#Position +#define LV2_TIME__Rate LV2_TIME_PREFIX "Rate" ///< http://lv2plug.in/ns/ext/time#Rate +#define LV2_TIME__position LV2_TIME_PREFIX "position" ///< http://lv2plug.in/ns/ext/time#position +#define LV2_TIME__barBeat LV2_TIME_PREFIX "barBeat" ///< http://lv2plug.in/ns/ext/time#barBeat +#define LV2_TIME__bar LV2_TIME_PREFIX "bar" ///< http://lv2plug.in/ns/ext/time#bar +#define LV2_TIME__beat LV2_TIME_PREFIX "beat" ///< http://lv2plug.in/ns/ext/time#beat +#define LV2_TIME__beatUnit LV2_TIME_PREFIX "beatUnit" ///< http://lv2plug.in/ns/ext/time#beatUnit +#define LV2_TIME__beatsPerBar LV2_TIME_PREFIX "beatsPerBar" ///< http://lv2plug.in/ns/ext/time#beatsPerBar +#define LV2_TIME__beatsPerMinute LV2_TIME_PREFIX "beatsPerMinute" ///< http://lv2plug.in/ns/ext/time#beatsPerMinute +#define LV2_TIME__frame LV2_TIME_PREFIX "frame" ///< http://lv2plug.in/ns/ext/time#frame +#define LV2_TIME__framesPerSecond LV2_TIME_PREFIX "framesPerSecond" ///< http://lv2plug.in/ns/ext/time#framesPerSecond +#define LV2_TIME__speed LV2_TIME_PREFIX "speed" ///< http://lv2plug.in/ns/ext/time#speed + +// clang-format on + +/** + @} +*/ + +#endif /* LV2_TIME_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/time/time.meta.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/time/time.meta.ttl new file mode 100644 index 0000000000..2b99cb772d --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/time/time.meta.ttl @@ -0,0 +1,112 @@ +@prefix dcs: . +@prefix doap: . +@prefix foaf: . +@prefix lv2: . +@prefix rdfs: . +@prefix time: . + + + a doap:Project ; + doap:name "LV2 Time" ; + doap:shortdesc "A vocabulary for describing musical time." ; + doap:created "2011-10-05" ; + doap:developer ; + doap:release [ + doap:revision "1.6" ; + doap:created "2019-02-03" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Clarify time:beat origin." + ] + ] + ] , [ + doap:revision "1.4" ; + doap:created "2016-07-31" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Define LV2_TIME_PREFIX." + ] + ] + ] , [ + doap:revision "1.2" ; + doap:created "2012-10-14" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Use consistent label style." + ] + ] + ] , [ + doap:revision "1.0" ; + doap:created "2012-04-17" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Initial release." + ] + ] + ] ; + lv2:documentation """ + +This is a vocabulary for describing a position in time and the speed of time +passage, in both real and musical terms. + +In addition to real time (based on seconds), two units of time are used: +_frames_ and _beats_. A frame is a numbered quantum of time. Frame time is +related to real-time by the _frame rate_ or _sample rate_, +time:framesPerSecond. A beat is a single pulse of musical time. Beat time is +related to real-time by the _tempo_, time:beatsPerMinute. + +Musical time additionally has a _meter_ which describes passage of time in +terms of musical _bars_. A bar is a higher level grouping of beats. The meter +describes how many beats are in one bar. + +"""^^lv2:Markdown . + +time:Position + lv2:documentation """ + +A point in time and/or the speed at which time is passing. A position is both +a point and a speed, which precisely defines a time within a timeline. + +"""^^lv2:Markdown . + +time:Rate + lv2:documentation """ + +The rate of passage of time in terms of one unit with respect to another. + +"""^^lv2:Markdown . + +time:beat + lv2:documentation """ + +This is not the beat within a bar like time:barBeat, but relative to the same +origin as time:bar and monotonically increases unless the transport is +repositioned. + +"""^^lv2:Markdown . + +time:beatUnit + lv2:documentation """ + +Beat unit, the note value that counts as one beat. This is the bottom number +in a time signature: 2 for half note, 4 for quarter note, and so on. + +"""^^lv2:Markdown . + +time:speed + lv2:documentation """ + +The rate of the progress of time as a fraction of normal speed. For example, a +rate of 0.0 is stopped, 1.0 is rolling at normal speed, 0.5 is rolling at half +speed, -1.0 is reverse, and so on. + +"""^^lv2:Markdown . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/time/time.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/time/time.ttl new file mode 100644 index 0000000000..a4085c60f8 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/time/time.ttl @@ -0,0 +1,122 @@ +@prefix owl: . +@prefix rdf: . +@prefix rdfs: . +@prefix time: . +@prefix xsd: . + + + a owl:Ontology ; + rdfs:label "LV2 Time" ; + rdfs:comment "A vocabulary for describing musical time." ; + rdfs:seeAlso , + . + +time:Time + a rdfs:Class , + owl:Class ; + rdfs:subClassOf time:Position ; + rdfs:label "Time" ; + rdfs:comment "A point in time in some unit/dimension." . + +time:Position + a rdfs:Class , + owl:Class ; + rdfs:label "Position" ; + rdfs:comment "A point in time and/or the speed at which time is passing." . + +time:Rate + a rdfs:Class , + owl:Class ; + rdfs:subClassOf time:Position ; + rdfs:label "Rate" ; + rdfs:comment "The rate of passage of time." . + +time:position + a rdf:Property , + owl:ObjectProperty , + owl:FunctionalProperty ; + rdfs:range time:Position ; + rdfs:label "position" ; + rdfs:comment "A musical position." . + +time:barBeat + a rdf:Property , + owl:DatatypeProperty , + owl:FunctionalProperty ; + rdfs:domain time:Time ; + rdfs:range xsd:float ; + rdfs:label "beat within bar" ; + rdfs:comment "The beat number within the bar, from 0 to time:beatsPerBar." . + +time:bar + a rdf:Property , + owl:DatatypeProperty , + owl:FunctionalProperty ; + rdfs:domain time:Time ; + rdfs:range xsd:long ; + rdfs:label "bar" ; + rdfs:comment "A musical bar or measure." . + +time:beat + a rdf:Property , + owl:DatatypeProperty , + owl:FunctionalProperty ; + rdfs:domain time:Time ; + rdfs:range xsd:double ; + rdfs:label "beat" ; + rdfs:comment "The global running beat number." . + +time:beatUnit + a rdf:Property , + owl:DatatypeProperty , + owl:FunctionalProperty ; + rdfs:domain time:Rate ; + rdfs:range xsd:nonNegativeInteger ; + rdfs:label "beat unit" ; + rdfs:comment "The note value that counts as one beat." . + +time:beatsPerBar + a rdf:Property , + owl:DatatypeProperty , + owl:FunctionalProperty ; + rdfs:domain time:Rate ; + rdfs:range xsd:float ; + rdfs:label "beats per bar" ; + rdfs:comment "The number of beats in one bar." . + +time:beatsPerMinute + a rdf:Property , + owl:DatatypeProperty , + owl:FunctionalProperty ; + rdfs:domain time:Rate ; + rdfs:range xsd:float ; + rdfs:label "beats per minute" ; + rdfs:comment "Tempo in beats per minute." . + +time:frame + a rdf:Property , + owl:DatatypeProperty , + owl:FunctionalProperty ; + rdfs:domain time:Time ; + rdfs:range xsd:long ; + rdfs:label "frame" ; + rdfs:comment "A time stamp in audio frames." . + +time:framesPerSecond + a rdf:Property , + owl:DatatypeProperty , + owl:FunctionalProperty ; + rdfs:domain time:Rate ; + rdfs:range xsd:float ; + rdfs:label "frames per second" ; + rdfs:comment "Frame rate in frames per second." . + +time:speed + a rdf:Property , + owl:DatatypeProperty , + owl:FunctionalProperty ; + rdfs:domain time:Rate ; + rdfs:range xsd:float ; + rdfs:label "speed" ; + rdfs:comment "The rate of the progress of time as a fraction of normal speed." . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/ui/manifest.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/ui/manifest.ttl new file mode 100644 index 0000000000..9f259adf87 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/ui/manifest.ttl @@ -0,0 +1,9 @@ +@prefix lv2: . +@prefix rdfs: . + + + a lv2:Specification ; + lv2:minorVersion 2 ; + lv2:microVersion 22 ; + rdfs:seeAlso . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/ui/ui.h b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/ui/ui.h new file mode 100644 index 0000000000..61b3b54b05 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/ui/ui.h @@ -0,0 +1,543 @@ +/* + LV2 UI Extension + Copyright 2009-2016 David Robillard + Copyright 2006-2011 Lars Luthman + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef LV2_UI_H +#define LV2_UI_H + +/** + @defgroup ui User Interfaces + @ingroup lv2 + + User interfaces of any type for plugins. + + See for details. + + @{ +*/ + +#include "lv2/core/lv2.h" +#include "lv2/urid/urid.h" + +#include +#include + +// clang-format off + +#define LV2_UI_URI "http://lv2plug.in/ns/extensions/ui" ///< http://lv2plug.in/ns/extensions/ui +#define LV2_UI_PREFIX LV2_UI_URI "#" ///< http://lv2plug.in/ns/extensions/ui# + +#define LV2_UI__CocoaUI LV2_UI_PREFIX "CocoaUI" ///< http://lv2plug.in/ns/extensions/ui#CocoaUI +#define LV2_UI__Gtk3UI LV2_UI_PREFIX "Gtk3UI" ///< http://lv2plug.in/ns/extensions/ui#Gtk3UI +#define LV2_UI__GtkUI LV2_UI_PREFIX "GtkUI" ///< http://lv2plug.in/ns/extensions/ui#GtkUI +#define LV2_UI__PortNotification LV2_UI_PREFIX "PortNotification" ///< http://lv2plug.in/ns/extensions/ui#PortNotification +#define LV2_UI__PortProtocol LV2_UI_PREFIX "PortProtocol" ///< http://lv2plug.in/ns/extensions/ui#PortProtocol +#define LV2_UI__Qt4UI LV2_UI_PREFIX "Qt4UI" ///< http://lv2plug.in/ns/extensions/ui#Qt4UI +#define LV2_UI__Qt5UI LV2_UI_PREFIX "Qt5UI" ///< http://lv2plug.in/ns/extensions/ui#Qt5UI +#define LV2_UI__UI LV2_UI_PREFIX "UI" ///< http://lv2plug.in/ns/extensions/ui#UI +#define LV2_UI__WindowsUI LV2_UI_PREFIX "WindowsUI" ///< http://lv2plug.in/ns/extensions/ui#WindowsUI +#define LV2_UI__X11UI LV2_UI_PREFIX "X11UI" ///< http://lv2plug.in/ns/extensions/ui#X11UI +#define LV2_UI__binary LV2_UI_PREFIX "binary" ///< http://lv2plug.in/ns/extensions/ui#binary +#define LV2_UI__fixedSize LV2_UI_PREFIX "fixedSize" ///< http://lv2plug.in/ns/extensions/ui#fixedSize +#define LV2_UI__idleInterface LV2_UI_PREFIX "idleInterface" ///< http://lv2plug.in/ns/extensions/ui#idleInterface +#define LV2_UI__noUserResize LV2_UI_PREFIX "noUserResize" ///< http://lv2plug.in/ns/extensions/ui#noUserResize +#define LV2_UI__notifyType LV2_UI_PREFIX "notifyType" ///< http://lv2plug.in/ns/extensions/ui#notifyType +#define LV2_UI__parent LV2_UI_PREFIX "parent" ///< http://lv2plug.in/ns/extensions/ui#parent +#define LV2_UI__plugin LV2_UI_PREFIX "plugin" ///< http://lv2plug.in/ns/extensions/ui#plugin +#define LV2_UI__portIndex LV2_UI_PREFIX "portIndex" ///< http://lv2plug.in/ns/extensions/ui#portIndex +#define LV2_UI__portMap LV2_UI_PREFIX "portMap" ///< http://lv2plug.in/ns/extensions/ui#portMap +#define LV2_UI__portNotification LV2_UI_PREFIX "portNotification" ///< http://lv2plug.in/ns/extensions/ui#portNotification +#define LV2_UI__portSubscribe LV2_UI_PREFIX "portSubscribe" ///< http://lv2plug.in/ns/extensions/ui#portSubscribe +#define LV2_UI__protocol LV2_UI_PREFIX "protocol" ///< http://lv2plug.in/ns/extensions/ui#protocol +#define LV2_UI__requestValue LV2_UI_PREFIX "requestValue" ///< http://lv2plug.in/ns/extensions/ui#requestValue +#define LV2_UI__floatProtocol LV2_UI_PREFIX "floatProtocol" ///< http://lv2plug.in/ns/extensions/ui#floatProtocol +#define LV2_UI__peakProtocol LV2_UI_PREFIX "peakProtocol" ///< http://lv2plug.in/ns/extensions/ui#peakProtocol +#define LV2_UI__resize LV2_UI_PREFIX "resize" ///< http://lv2plug.in/ns/extensions/ui#resize +#define LV2_UI__showInterface LV2_UI_PREFIX "showInterface" ///< http://lv2plug.in/ns/extensions/ui#showInterface +#define LV2_UI__touch LV2_UI_PREFIX "touch" ///< http://lv2plug.in/ns/extensions/ui#touch +#define LV2_UI__ui LV2_UI_PREFIX "ui" ///< http://lv2plug.in/ns/extensions/ui#ui +#define LV2_UI__updateRate LV2_UI_PREFIX "updateRate" ///< http://lv2plug.in/ns/extensions/ui#updateRate +#define LV2_UI__windowTitle LV2_UI_PREFIX "windowTitle" ///< http://lv2plug.in/ns/extensions/ui#windowTitle +#define LV2_UI__scaleFactor LV2_UI_PREFIX "scaleFactor" ///< http://lv2plug.in/ns/extensions/ui#scaleFactor +#define LV2_UI__foregroundColor LV2_UI_PREFIX "foregroundColor" ///< http://lv2plug.in/ns/extensions/ui#foregroundColor +#define LV2_UI__backgroundColor LV2_UI_PREFIX "backgroundColor" ///< http://lv2plug.in/ns/extensions/ui#backgroundColor + +// clang-format on + +/** + The index returned by LV2UI_Port_Map::port_index() for unknown ports. +*/ +#define LV2UI_INVALID_PORT_INDEX ((uint32_t)-1) + +#ifdef __cplusplus +extern "C" { +#endif + +/** + A pointer to some widget or other type of UI handle. + + The actual type is defined by the type of the UI. +*/ +typedef void* LV2UI_Widget; + +/** + A pointer to UI instance internals. + + The host may compare this to NULL, but otherwise MUST NOT interpret it. +*/ +typedef void* LV2UI_Handle; + +/** + A pointer to a controller provided by the host. + + The UI may compare this to NULL, but otherwise MUST NOT interpret it. +*/ +typedef void* LV2UI_Controller; + +/** + A pointer to opaque data for a feature. +*/ +typedef void* LV2UI_Feature_Handle; + +/** + A host-provided function that sends data to a plugin's input ports. + + @param controller The opaque controller pointer passed to + LV2UI_Descriptor::instantiate(). + + @param port_index Index of the port to update. + + @param buffer Buffer containing `buffer_size` bytes of data. + + @param buffer_size Size of `buffer` in bytes. + + @param port_protocol Either 0 or the URID for a ui:PortProtocol. If 0, the + protocol is implicitly ui:floatProtocol, the port MUST be an lv2:ControlPort + input, `buffer` MUST point to a single float value, and `buffer_size` MUST + be sizeof(float). The UI SHOULD NOT use a protocol not supported by the + host, but the host MUST gracefully ignore any protocol it does not + understand. +*/ +typedef void (*LV2UI_Write_Function)(LV2UI_Controller controller, + uint32_t port_index, + uint32_t buffer_size, + uint32_t port_protocol, + const void* buffer); + +/** + A plugin UI. + + A pointer to an object of this type is returned by the lv2ui_descriptor() + function. +*/ +typedef struct LV2UI_Descriptor { + /** + The URI for this UI (not for the plugin it controls). + */ + const char* URI; + + /** + Create a new UI and return a handle to it. This function works + similarly to LV2_Descriptor::instantiate(). + + @param descriptor The descriptor for the UI to instantiate. + + @param plugin_uri The URI of the plugin that this UI will control. + + @param bundle_path The path to the bundle containing this UI, including + the trailing directory separator. + + @param write_function A function that the UI can use to send data to the + plugin's input ports. + + @param controller A handle for the UI instance to be passed as the + first parameter of UI methods. + + @param widget (output) widget pointer. The UI points this at its main + widget, which has the type defined by the UI type in the data file. + + @param features An array of LV2_Feature pointers. The host must pass + all feature URIs that it and the UI supports and any additional data, as + in LV2_Descriptor::instantiate(). Note that UI features and plugin + features are not necessarily the same. + + */ + LV2UI_Handle (*instantiate)(const struct LV2UI_Descriptor* descriptor, + const char* plugin_uri, + const char* bundle_path, + LV2UI_Write_Function write_function, + LV2UI_Controller controller, + LV2UI_Widget* widget, + const LV2_Feature* const* features); + + /** + Destroy the UI. The host must not try to access the widget after + calling this function. + */ + void (*cleanup)(LV2UI_Handle ui); + + /** + Tell the UI that something interesting has happened at a plugin port. + + What is "interesting" and how it is written to `buffer` is defined by + `format`, which has the same meaning as in LV2UI_Write_Function(). + Format 0 is a special case for lv2:ControlPort, where this function + should be called when the port value changes (but not necessarily for + every change), `buffer_size` must be sizeof(float), and `buffer` + points to a single IEEE-754 float. + + By default, the host should only call this function for lv2:ControlPort + inputs. However, the UI can request updates for other ports statically + with ui:portNotification or dynamicaly with ui:portSubscribe. + + The UI MUST NOT retain any reference to `buffer` after this function + returns, it is only valid for the duration of the call. + + This member may be NULL if the UI is not interested in any port events. + */ + void (*port_event)(LV2UI_Handle ui, + uint32_t port_index, + uint32_t buffer_size, + uint32_t format, + const void* buffer); + + /** + Return a data structure associated with an extension URI, typically an + interface struct with additional function pointers + + This member may be set to NULL if the UI is not interested in supporting + any extensions. This is similar to LV2_Descriptor::extension_data(). + + */ + const void* (*extension_data)(const char* uri); +} LV2UI_Descriptor; + +/** + Feature/interface for resizable UIs (LV2_UI__resize). + + This structure is used in two ways: as a feature passed by the host via + LV2UI_Descriptor::instantiate(), or as an interface provided by a UI via + LV2UI_Descriptor::extension_data()). +*/ +typedef struct { + /** + Pointer to opaque data which must be passed to ui_resize(). + */ + LV2UI_Feature_Handle handle; + + /** + Request/advertise a size change. + + When provided by the host, the UI may call this function to inform the + host about the size of the UI. + + When provided by the UI, the host may call this function to notify the + UI that it should change its size accordingly. In this case, the host + must pass the LV2UI_Handle to provide access to the UI instance. + + @return 0 on success. + */ + int (*ui_resize)(LV2UI_Feature_Handle handle, int width, int height); +} LV2UI_Resize; + +/** + Feature to map port symbols to UIs. + + This can be used by the UI to get the index for a port with the given + symbol. This makes it possible to implement and distribute a UI separately + from the plugin (since symbol, unlike index, is a stable port identifier). +*/ +typedef struct { + /** + Pointer to opaque data which must be passed to port_index(). + */ + LV2UI_Feature_Handle handle; + + /** + Get the index for the port with the given `symbol`. + + @return The index of the port, or LV2UI_INVALID_PORT_INDEX if no such + port is found. + */ + uint32_t (*port_index)(LV2UI_Feature_Handle handle, const char* symbol); +} LV2UI_Port_Map; + +/** + Feature to subscribe to port updates (LV2_UI__portSubscribe). +*/ +typedef struct { + /** + Pointer to opaque data which must be passed to subscribe() and + unsubscribe(). + */ + LV2UI_Feature_Handle handle; + + /** + Subscribe to updates for a port. + + This means that the host will call the UI's port_event() function when + the port value changes (as defined by protocol). + + Calling this function with the same `port_index` and `port_protocol` + as an already active subscription has no effect. + + @param handle The handle field of this struct. + @param port_index The index of the port. + @param port_protocol The URID of the ui:PortProtocol. + @param features Features for this subscription. + @return 0 on success. + */ + uint32_t (*subscribe)(LV2UI_Feature_Handle handle, + uint32_t port_index, + uint32_t port_protocol, + const LV2_Feature* const* features); + + /** + Unsubscribe from updates for a port. + + This means that the host will cease calling calling port_event() when + the port value changes. + + Calling this function with a `port_index` and `port_protocol` that + does not refer to an active port subscription has no effect. + + @param handle The handle field of this struct. + @param port_index The index of the port. + @param port_protocol The URID of the ui:PortProtocol. + @param features Features for this subscription. + @return 0 on success. + */ + uint32_t (*unsubscribe)(LV2UI_Feature_Handle handle, + uint32_t port_index, + uint32_t port_protocol, + const LV2_Feature* const* features); +} LV2UI_Port_Subscribe; + +/** + A feature to notify the host that the user has grabbed a UI control. +*/ +typedef struct { + /** + Pointer to opaque data which must be passed to touch(). + */ + LV2UI_Feature_Handle handle; + + /** + Notify the host that a control has been grabbed or released. + + The host should cease automating the port or otherwise manipulating the + port value until the control has been ungrabbed. + + @param handle The handle field of this struct. + @param port_index The index of the port associated with the control. + @param grabbed If true, the control has been grabbed, otherwise the + control has been released. + */ + void (*touch)(LV2UI_Feature_Handle handle, uint32_t port_index, bool grabbed); +} LV2UI_Touch; + +/** + A status code for LV2UI_Request_Value::request(). +*/ +typedef enum { + /** + Completed successfully. + + The host will set the parameter later if the user choses a new value. + */ + LV2UI_REQUEST_VALUE_SUCCESS, + + /** + Parameter already being requested. + + The host is already requesting a parameter from the user (for example, a + dialog is visible), or the UI is otherwise busy and can not make this + request. + */ + LV2UI_REQUEST_VALUE_BUSY, + + /** + Unknown parameter. + + The host is not aware of this parameter, and is not able to set a new + value for it. + */ + LV2UI_REQUEST_VALUE_ERR_UNKNOWN, + + /** + Unsupported parameter. + + The host knows about this parameter, but does not support requesting a + new value for it from the user. This is likely because the host does + not have UI support for choosing a value with the appropriate type. + */ + LV2UI_REQUEST_VALUE_ERR_UNSUPPORTED +} LV2UI_Request_Value_Status; + +/** + A feature to request a new parameter value from the host. +*/ +typedef struct { + /** + Pointer to opaque data which must be passed to request(). + */ + LV2UI_Feature_Handle handle; + + /** + Request a value for a parameter from the host. + + This is mainly used by UIs to request values for complex parameters that + don't change often, such as file paths, but it may be used to request + any parameter value. + + This function returns immediately, and the return value indicates + whether the host can fulfill the request. The host may notify the + plugin about the new parameter value, for example when a file is + selected by the user, via the usual mechanism. Typically, the host will + send a message to the plugin that sets the new parameter value, and the + plugin will notify the UI via a message as usual for any other parameter + change. + + To provide an appropriate UI, the host can determine details about the + parameter from the plugin data as usual. The additional parameters of + this function provide support for more advanced use cases, but in the + simple common case, the plugin will simply pass the key of the desired + parameter and zero for everything else. + + @param handle The handle field of this struct. + + @param key The URID of the parameter. + + @param type The optional type of the value to request. This can be used + to request a specific value type for parameters that support several. + If non-zero, it must be the URID of an instance of rdfs:Class or + rdfs:Datatype. + + @param features Additional features for this request, or NULL. + + @return A status code which is 0 on success. + */ + LV2UI_Request_Value_Status (*request)(LV2UI_Feature_Handle handle, + LV2_URID key, + LV2_URID type, + const LV2_Feature* const* features); + +} LV2UI_Request_Value; + +/** + UI Idle Interface (LV2_UI__idleInterface) + + UIs can provide this interface to have an idle() callback called by the host + rapidly to update the UI. +*/ +typedef struct { + /** + Run a single iteration of the UI's idle loop. + + This will be called rapidly in the UI thread at a rate appropriate + for a toolkit main loop. There are no precise timing guarantees, but + the host should attempt to call idle() at a high enough rate for smooth + animation, at least 30Hz. + + @return non-zero if the UI has been closed, in which case the host + should stop calling idle(), and can either completely destroy the UI, or + re-show it and resume calling idle(). + */ + int (*idle)(LV2UI_Handle ui); +} LV2UI_Idle_Interface; + +/** + UI Show Interface (LV2_UI__showInterface) + + UIs can provide this interface to show and hide a window, which allows them + to function in hosts unable to embed their widget. This allows any UI to + provide a fallback for embedding that works in any host. + + If used: + - The host MUST use LV2UI_Idle_Interface to drive the UI. + - The UI MUST return non-zero from LV2UI_Idle_Interface::idle() when it has + been closed. + - If idle() returns non-zero, the host MUST call hide() and stop calling + idle(). It MAY later call show() then resume calling idle(). +*/ +typedef struct { + /** + Show a window for this UI. + + The window title MAY have been passed by the host to + LV2UI_Descriptor::instantiate() as an LV2_Options_Option with key + LV2_UI__windowTitle. + + @return 0 on success, or anything else to stop being called. + */ + int (*show)(LV2UI_Handle ui); + + /** + Hide the window for this UI. + + @return 0 on success, or anything else to stop being called. + */ + int (*hide)(LV2UI_Handle ui); +} LV2UI_Show_Interface; + +/** + Peak data for a slice of time, the update format for ui:peakProtocol. +*/ +typedef struct { + /** + The start of the measurement period. This is just a running counter + that is only meaningful in comparison to previous values and must not be + interpreted as an absolute time. + */ + uint32_t period_start; + + /** + The size of the measurement period, in the same units as period_start. + */ + uint32_t period_size; + + /** + The peak value for the measurement period. This should be the maximal + value for abs(sample) over all the samples in the period. + */ + float peak; +} LV2UI_Peak_Data; + +/** + Prototype for UI accessor function. + + This is the entry point to a UI library, which works in the same way as + lv2_descriptor() but for UIs rather than plugins. +*/ +LV2_SYMBOL_EXPORT +const LV2UI_Descriptor* +lv2ui_descriptor(uint32_t index); + +/** + The type of the lv2ui_descriptor() function. +*/ +typedef const LV2UI_Descriptor* (*LV2UI_DescriptorFunction)(uint32_t index); + +#ifdef __cplusplus +} +#endif + +/** + @} +*/ + +#endif /* LV2_UI_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/ui/ui.meta.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/ui/ui.meta.ttl new file mode 100644 index 0000000000..cb92a8650e --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/ui/ui.meta.ttl @@ -0,0 +1,627 @@ +@prefix dcs: . +@prefix doap: . +@prefix foaf: . +@prefix lv2: . +@prefix rdf: . +@prefix rdfs: . +@prefix ui: . + + + a doap:Project ; + doap:license ; + doap:name "LV2 UI" ; + doap:shortdesc "User interfaces for LV2 plugins." ; + doap:created "2006-00-00" ; + doap:developer ; + doap:maintainer ; + doap:release [ + doap:revision "2.22" ; + doap:created "2020-04-26" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add ui:requestValue feature." + ] , [ + rdfs:label "Add ui:scaleFactor, ui:foregroundColor, and ui:backgroundColor properties." + ] , [ + rdfs:label "Deprecate ui:binary." + ] + ] + ] , [ + doap:revision "2.20" ; + doap:created "2015-07-25" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Improve documentation." + ] , [ + rdfs:label "Add missing property labels." + ] + ] + ] , [ + doap:revision "2.18" ; + doap:created "2014-08-08" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add show interface so UIs can gracefully degrade to separate windows if hosts can not use their widget directly." + ] , [ + rdfs:label "Fix identifier typos in documentation." + ] + ] + ] , [ + doap:revision "2.16" ; + doap:created "2014-01-04" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Fix LV2_UI_INVALID_PORT_INDEX identifier in documentation." + ] + ] + ] , [ + doap:revision "2.14" ; + doap:created "2013-03-18" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add idle interface so native UIs and foreign toolkits can drive their event loops." + ] , [ + rdfs:label "Add ui:updateRate property." + ] + ] + ] , [ + doap:revision "2.12" ; + doap:created "2012-12-01" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Fix incorrect linker flag in ui:makeSONameResident documentation." + ] + ] + ] , [ + doap:revision "2.10" ; + doap:created "2012-10-14" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add types for WindowsUI, CocoaUI, and Gtk3UI." + ] , [ + rdfs:label "Use consistent label style." + ] , [ + rdfs:label "Add missing LV2_SYMBOL_EXPORT declaration for lv2ui_descriptor prototype." + ] + ] + ] , [ + doap:revision "2.8" ; + doap:created "2012-04-17" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add ui:parent and ui:resize." + ] , [ + rdfs:label "Add support for referring to ports by symbol." + ] , [ + rdfs:label "Add ui:portMap for accessing ports by symbol, allowing for UIs to be distributed separately from plugins." + ] , [ + rdfs:label "Add port protocols and a dynamic notification subscription mechanism, for more flexible communication, and audio port metering without control port kludges." + ] , [ + rdfs:label "Add touch feature to notify the host that the user has grabbed a control." + ] , [ + rdfs:label "Merge with unified LV2 package." + ] + ] + ] , [ + doap:revision "2.4" ; + doap:created "2011-11-21" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Deprecate ui:makeSONameResident." + ] , [ + rdfs:label "Add Qt4 and X11 widget types." + ] , [ + rdfs:label "Install header to URI-based system path." + ] , [ + rdfs:label "Add pkg-config file." + ] , [ + rdfs:label "Make ui.ttl a valid OWL 2 DL ontology." + ] + ] + ] , [ + doap:revision "2.2" ; + doap:created "2011-05-26" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add build system (for installation)." + ] , [ + rdfs:label "Convert documentation to HTML and use lv2:documentation." + ] , [ + rdfs:label "Use lv2:Specification to be discovered as an extension." + ] + ] + ] , [ + doap:revision "2.0" ; + doap:created "2010-10-06" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Initial release." + ] + ] + ] ; + lv2:documentation """ + +This extension makes it possible to create user interfaces for LV2 plugins. + +UIs are implemented as an LV2UI_Descriptor loaded via lv2ui_descriptor() in a +shared library, and are distributed in bundles just like plugins. + +UIs are associated with plugins in data: + + :::turtle + @prefix ui: . + + ui:ui . + a ui:X11UI ; + lv2:binary . + +where `http://my.plugin` is the URI of the plugin, `http://my.pluginui` is the +URI of the plugin UI and `myui.so` is the relative URI to the shared object +file. + +While it is possible to have the plugin UI and the plugin in the same shared +object file, it is a good idea to keep them separate so that hosts can avoid +loading the UI code if they do not need it. A UI MUST specify its class in the +RDF data (`ui:X11UI` in the above example). The class defines what type the UI +is, for example what graphics toolkit it uses. Any type of UI class can be +defined separately from this extension. + +It is possible to have multiple UIs for the same plugin, or to have the UI for +a plugin in a different bundle from the actual plugin. This allows plugin UIs +to be written independently. + +Note that the process that loads the shared object file containing the UI code +and the process that loads the shared object file containing the actual plugin +implementation are not necessarily the same process (and not even necessarily +on the same machine). This means that plugin and UI code MUST NOT use +singletons and global variables and expect them to refer to the same objects in +the UI and the actual plugin. The function callback interface defined in this +header is the only method of communication between UIs and plugin instances +(extensions may define more, though this is discouraged unless absolutely +necessary since the significant benefits of network transparency and +serialisability are lost). + +UI functionality may be extended via features, much like plugins: + + :::turtle + lv2:requiredFeature . + lv2:optionalFeature . + +The rules for a UI with a required or optional feature are identical to those +of lv2:Plugin instances: if a UI declares a feature as required, the host is +NOT allowed to load it unless it supports that feature; and if it does support +a feature, it MUST pass an appropriate LV2_Feature struct to the UI's +instantiate() method. This extension defines several standard features for +common functionality. + +UIs written to this specification do not need to be thread-safe. All functions +may only be called in the UI thread. There is only one UI thread (for +toolkits, the one the UI main loop runs in). There is no requirement that a +UI actually be a graphical widget. + +Note that UIs are completely separate from plugins. From the plugin's +perspective, control from a UI is indistinguishable from any other control, as +it all occcurs via ports. + +"""^^lv2:Markdown . + +ui:GtkUI + lv2:documentation """ + +The host must guarantee that the Gtk+ 2 library has been initialised and the +Glib main loop is running before the UI is instantiated. Note that this UI +type is not suitable for binary distribution since multiple versions of Gtk can +not be used in the same process. + +"""^^lv2:Markdown . + +ui:Gtk3UI + lv2:documentation """ + +The host must guarantee that the Gtk+ 3 library has been initialised and the +Glib main loop is running before the UI is instantiated. Note that this UI +type is not suitable for binary distribution since multiple versions of Gtk can +not be used in the same process. + +"""^^lv2:Markdown . + +ui:Qt4UI + lv2:documentation """ + +The host must guarantee that the Qt4 library has been initialised and the Qt4 +main loop is running before the UI is instantiated. Note that this UI type is +not suitable for binary distribution since multiple versions of Qt can not be +used in the same process. + +"""^^lv2:Markdown . + +ui:Qt5UI + lv2:documentation """ + +The host must guarantee that the Qt5 library has been initialised and the Qt5 +main loop is running before the UI is instantiated. Note that this UI type is +not suitable for binary distribution since multiple versions of Qt can not be +used in the same process. + +"""^^lv2:Markdown . + +ui:X11UI + lv2:documentation """ + +Note that the LV2UI_Widget for this type of UI is not a pointer, but should be +interpreted directly as an X11 `Window` ID. This is the native UI type on most +POSIX systems. + +"""^^lv2:Markdown . + +ui:WindowsUI + lv2:documentation """ + +Note that the LV2UI_Widget for this type of UI is not a pointer, but should be +interpreted directly as a `HWND`. This is the native UI type on Microsoft +Windows. + +"""^^lv2:Markdown . + +ui:CocoaUI + lv2:documentation """ + +This is the native UI type on Mac OS X. + +"""^^lv2:Markdown . + +ui:binary + lv2:documentation """ + +This property is redundant and deprecated: new UIs should use lv2:binary +instead, however hosts must still support ui:binary. + +"""^^lv2:Markdown . + +ui:makeSONameResident + lv2:documentation """ + +This feature was intended to support UIs that link against toolkit libraries +which may not be unloaded during the lifetime of the host. This is better +achieved by using the appropriate flags when linking the UI, for example `gcc +-Wl,-z,nodelete`. + +"""^^lv2:Markdown . + +ui:noUserResize + lv2:documentation """ + +If a UI has this feature, it indicates that it does not make sense to let the +user resize the main widget, and the host should prevent that. This feature +may not make sense for all UI types. The data pointer for this feature should +always be set to NULL. + +"""^^lv2:Markdown . + +ui:fixedSize + lv2:documentation """ + +If a UI has this feature, it indicates the same thing as ui:noUserResize, and +additionally that the UI will not resize itself on its own. That is, the UI +will always remain the same size. This feature may not make sense for all UI +types. The data pointer for this feature should always be set to NULL. + +"""^^lv2:Markdown . + +ui:scaleFactor + lv2:documentation """ + +HiDPI (High Dots Per Inch) displays have a high resolution on a relatively +small form factor. Many systems use a scale factor to compensate for this, so +for example, a scale factor of 2 means things are drawn twice as large as +normal. + +Hosts can pass this as an option to UIs to communicate this information, so +that the UI can draw at an appropriate scale. + +"""^^lv2:Markdown . + +ui:backgroundColor + lv2:documentation """ + +The background color of the host's UI. The host can pass this as an option so +that UIs can integrate nicely with the host window around it. + +Hosts should pass this as an option to UIs with an integer RGBA32 color value. +If the value is a 32-bit integer, it is guaranteed to be in RGBA32 format, but +the UI must check the value type and not assume this, to allow for other types +of color in the future. + +"""^^lv2:Markdown . + +ui:foregroundColor + lv2:documentation """ + +The foreground color of the host's UI. The host can pass this as an option so +that UIs can integrate nicely with the host window around it. + +Hosts should pass this as an option to UIs with an integer RGBA32 color value. +If the value is a 32-bit integer, it is guaranteed to be in RGBA32 format, but +the UI must check the value type and not assume this, to allow for other types +of color in the future. + +"""^^lv2:Markdown . + +ui:parent + lv2:documentation """ + +This feature can be used to pass a parent that the UI should be a child of. +The format of data pointer of this feature is determined by the UI type, and is +generally the same type as the LV2UI_Widget that the UI would return. For +example, for a ui:X11UI, the parent should be a `Window`. This is particularly +useful for embedding, where the parent often must be known at construction time +for embedding to work correctly. + +UIs should not _require_ this feature unless it is necessary for them to work +at all, but hosts should provide it whenever possible. + +"""^^lv2:Markdown . + +ui:PortNotification + lv2:documentation """ + +This describes which ports the host must send notifications to the UI about. +The port must be specific either by index, using the ui:portIndex property, or +symbol, using the lv2:symbol property. Since port indices are not guaranteed +to be stable, using the symbol is recommended, and the inde MUST NOT be used +except by UIs that are shipped in the same bundle as the corresponding plugin. + +"""^^lv2:Markdown . + +ui:portNotification + lv2:documentation """ + +Specifies that a UI should receive notifications about changes to a particular +port's value via LV2UI_Descriptor::port_event(). + +For example: + + :::turtle + eg:ui + a ui:X11UI ; + ui:portNotification [ + ui:plugin eg:plugin ; + lv2:symbol "gain" ; + ui:protocol ui:floatProtocol + ] . + +"""^^lv2:Markdown . + +ui:notifyType + lv2:documentation """ + +Specifies a particular type that the UI should be notified of. + +This is useful for message-based ports where several types of data can be +present, but only some are interesting to the UI. For example, if UI control +is multiplexed in the same port as MIDI, this property can be used to ensure +that only the relevant events are forwarded to the UI, and not potentially high +frequency MIDI data. + +"""^^lv2:Markdown . + +ui:resize + lv2:documentation """ + +This feature corresponds to the LV2UI_Resize struct, which should be passed +with the URI LV2_UI__resize. This struct may also be provided by the UI as +extension data using the same URI, in which case it is used by the host to +request that the UI change its size. + +"""^^lv2:Markdown . + +ui:portMap + lv2:documentation """ + +This makes it possible to implement and distribute UIs separately from the +plugin binaries they control. + +This feature corresponds to the LV2UI_Port_Index struct, which should be passed +with the URI LV2_UI__portIndex. + +"""^^lv2:Markdown . + +ui:portSubscribe + lv2:documentation """ + +This makes it possible for a UI to explicitly request a particular style of +update from a port at run-time, in a more flexible and powerful way than +listing subscriptions statically allows. + +This feature corresponds to the LV2UI_Port_Subscribe struct, which should be +passed with the URI LV2_UI__portSubscribe. + +"""^^lv2:Markdown . + +ui:touch + lv2:documentation """ + +This is useful for automation, so the host can allow the user to take control +of a port, even if that port would otherwise be automated (much like grabbing a +physical motorised fader). + +It can also be used for MIDI learn or in any other situation where the host +needs to do something with a particular control and it would be convenient for +the user to select it directly from the plugin UI. + +This feature corresponds to the LV2UI_Touch struct, which should be passed with +the URI LV2_UI__touch. + +"""^^lv2:Markdown . + +ui:requestValue + lv2:documentation """ + +This allows a plugin UI to request a new parameter value using the host's UI, +for example by showing a dialog or integrating with the host's built-in content +browser. This should only be used for complex parameter types where the plugin +UI is not capable of showing the expected native platform or host interface to +choose a value, such as file path parameters. + +This feature corresponds to the LV2UI_Request_Value struct, which should be +passed with the URI LV2_UI__requestValue. + +"""^^lv2:Markdown . + +ui:idleInterface + lv2:documentation """ + +To support this feature, the UI should list it as a lv2:optionalFeature or +lv2:requiredFeature in its data, and also as lv2:extensionData. When the UI's +extension_data() is called with this URI (LV2_UI__idleInterface), it should +return a pointer to an LV2UI_Idle_Interface. + +To indicate support, the host should pass a feature to instantiate() with this +URI, with NULL for data. + +"""^^lv2:Markdown . + +ui:showInterface + lv2:documentation """ + +This allows UIs to gracefully degrade to separate windows when the host is +unable to embed the UI widget for whatever reason. When the UI's +extension_data() is called with this URI (LV2_UI__showInterface), it should +return a pointer to an LV2UI_Show_Interface. + +"""^^lv2:Markdown . + +ui:PortProtocol + lv2:documentation """ + +A PortProtocol MUST define: + +Port Type +: Which plugin port types the buffer type is valid for. + +Feature Data +: What data (if any) should be passed in the LV2_Feature. + +A PortProtocol for an output port MUST define: + +Update Frequency +: When the host should call port_event(). + +Update Format +: The format of the data in the buffer passed to port_event(). + +Options +: The format of the options passed to subscribe() and unsubscribe(). + +A PortProtocol for an input port MUST define: + +Write Format +: The format of the data in the buffer passed to write_port(). + +Write Effect +: What happens when the UI calls write_port(). + +For an example, see ui:floatProtocol or ui:peakProtocol. + +PortProtocol is a subclass of lv2:Feature, so UIs use lv2:optionalFeature and +lv2:requiredFeature to specify which PortProtocols they want to use. + +"""^^lv2:Markdown . + +ui:floatProtocol + lv2:documentation """ + +A protocol for transferring single floating point values. The rules for this +protocol are: + +Port Type +: lv2:ControlPort + +Feature Data +: None. + +Update Frequency +: The host SHOULD call port_event() as soon as possible when the port value has + changed, but there are no timing guarantees. + +Update Format +: A single float. + +Options +: None. + +Write Format +: A single float. + +Write Effect +: The host SHOULD set the port to the received value as soon as possible, but + there is no guarantee that run() actually sees this value. + +"""^^lv2:Markdown . + +ui:peakProtocol + lv2:documentation """ + +This port protocol defines a way for the host to send continuous peak +measurements of the audio signal at a certain port to the UI. The intended use +is visualisation, for example an animated meter widget that shows the level of +the audio input or output. + +A contiguous sequence of audio samples for which a peak value has been computed +is called a _measurement period_. + +The rules for this protocol are: + +Port Type +: lv2:AudioPort + +Feature Data +: None. + +Update Frequency +: The host SHOULD call port_event() at regular intervals. The measurement + periods used for calls to port_event() for the same port SHOULD be + contiguous (i.e. the measurement period for one call should begin right + after the end of the measurement period for the previous call ends) unless + the UI has removed and re-added the port subscription between those calls. + However, UIs MUST NOT depend on either the regularity of the calls or the + contiguity of the measurement periods; hosts may change the call rate or + skip calls for performance or other reasons. Measurement periods for + different calls to port_event() for the same port MUST NOT overlap. + +Update Format +: A single LV2UI_Peak_Data object. + +Options +: None. + +Write Format +: None. + +Write Effect +: None. + +"""^^lv2:Markdown . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/ui/ui.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/ui/ui.ttl new file mode 100644 index 0000000000..61f8bcac73 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/ui/ui.ttl @@ -0,0 +1,248 @@ +@prefix lv2: . +@prefix opts: . +@prefix owl: . +@prefix rdf: . +@prefix rdfs: . +@prefix ui: . +@prefix xsd: . + + + a owl:Ontology ; + rdfs:label "LV2 UI" ; + rdfs:comment "User interfaces for LV2 plugins." ; + owl:imports ; + rdfs:seeAlso , + . + +ui:UI + a rdfs:Class , + owl:Class ; + rdfs:label "User Interface" ; + rdfs:comment "A UI for an LV2 plugin." . + +ui:GtkUI + a rdfs:Class , + owl:Class ; + rdfs:subClassOf ui:UI ; + rdfs:label "GTK2 UI" ; + rdfs:comment "A UI where the widget is a pointer to a Gtk+ 2.0 GtkWidget." . + +ui:Gtk3UI + a rdfs:Class , + owl:Class ; + rdfs:subClassOf ui:UI ; + rdfs:label "GTK3 UI" ; + rdfs:comment "A UI where the widget is a pointer to a Gtk+ 3.0 GtkWidget." . + +ui:Qt4UI + a rdfs:Class , + owl:Class ; + rdfs:subClassOf ui:UI ; + rdfs:label "Qt4 UI" ; + rdfs:comment "A UI where the widget is a pointer to a Qt4 QWidget." . + +ui:Qt5UI + a rdfs:Class , + owl:Class ; + rdfs:subClassOf ui:UI ; + rdfs:label "Qt5 UI" ; + rdfs:comment "A UI where the widget is a pointer to a Qt5 QWidget." . + +ui:X11UI + a rdfs:Class , + owl:Class ; + rdfs:subClassOf ui:UI ; + rdfs:label "X11 UI" ; + rdfs:comment "A UI where the widget is an X11 Window window ID." . + +ui:WindowsUI + a rdfs:Class , + owl:Class ; + rdfs:subClassOf ui:UI ; + rdfs:label "Windows UI" ; + rdfs:comment "A UI where the widget is a Windows HWND window ID." . + +ui:CocoaUI + a rdfs:Class , + owl:Class ; + rdfs:subClassOf ui:UI ; + rdfs:label "Cocoa UI" ; + rdfs:comment "A UI where the widget is a pointer to a NSView." . + +ui:ui + a rdf:Property , + owl:ObjectProperty ; + rdfs:domain lv2:Plugin ; + rdfs:range ui:UI ; + rdfs:label "user interface" ; + rdfs:comment "Relates a plugin to a UI that applies to it." . + +ui:binary + a rdf:Property , + owl:ObjectProperty ; + owl:sameAs lv2:binary ; + owl:deprecated "true"^^xsd:boolean ; + rdfs:label "binary" ; + rdfs:comment "The shared library that a UI resides in." . + +ui:makeSONameResident + a lv2:Feature ; + owl:deprecated "true"^^xsd:boolean ; + rdfs:label "make SO name resident" ; + rdfs:comment "UI binary must not be unloaded." . + +ui:noUserResize + a lv2:Feature ; + rdfs:label "no user resize" ; + rdfs:comment "Non-resizable UI." . + +ui:fixedSize + a lv2:Feature ; + rdfs:label "fixed size" ; + rdfs:comment "Non-resizable UI that will never resize itself." . + +ui:scaleFactor + a rdf:Property , + owl:DatatypeProperty , + opts:Option ; + rdfs:range xsd:float ; + rdfs:label "scale factor" ; + rdfs:comment "Scale factor for high resolution screens." . + +ui:backgroundColor + a rdf:Property , + owl:DatatypeProperty , + opts:Option ; + rdfs:label "background color" ; + rdfs:comment """The background color of the host's UI.""" . + +ui:foregroundColor + a rdf:Property , + owl:DatatypeProperty , + opts:Option ; + rdfs:label "foreground color" ; + rdfs:comment """The foreground color of the host's UI.""" . + +ui:parent + a lv2:Feature ; + rdfs:label "parent" ; + rdfs:comment "The parent for a UI." . + +ui:PortNotification + a rdfs:Class , + owl:Class ; + rdfs:subClassOf [ + a owl:Restriction ; + owl:onProperty ui:plugin ; + owl:cardinality 1 ; + rdfs:comment "A PortNotification MUST have exactly one ui:plugin." + ] ; + rdfs:label "Port Notification" ; + rdfs:comment "A description of port updates that a host must send a UI." . + +ui:portNotification + a rdf:Property , + owl:ObjectProperty ; + rdfs:domain ui:UI ; + rdfs:range ui:PortNotification ; + rdfs:label "port notification" ; + rdfs:comment "Specifies a port notification that is required by a UI." . + +ui:plugin + a rdf:Property , + owl:ObjectProperty ; + rdfs:domain ui:PortNotification ; + rdfs:range lv2:Plugin ; + rdfs:label "plugin" ; + rdfs:comment "The plugin a portNotification applies to." . + +ui:portIndex + a rdf:Property , + owl:DatatypeProperty ; + rdfs:domain ui:PortNotification ; + rdfs:range xsd:decimal ; + rdfs:label "port index" ; + rdfs:comment "The index of the port a portNotification applies to." . + +ui:notifyType + a rdf:Property , + owl:ObjectProperty ; + rdfs:domain ui:PortNotification ; + rdfs:label "notify type" ; + rdfs:comment "A particular type that the UI should be notified of." . + +ui:resize + a lv2:Feature , + lv2:ExtensionData ; + rdfs:label "resize" ; + rdfs:comment """A feature that control of, and notifications about, a UI's size.""" . + +ui:portMap + a lv2:Feature ; + rdfs:label "port map" ; + rdfs:comment "A feature for accessing the index of a port by symbol." . + +ui:portSubscribe + a lv2:Feature ; + rdfs:label "port subscribe" ; + rdfs:comment "A feature for dynamically subscribing to updates from a port." . + +ui:touch + a lv2:Feature ; + rdfs:label "touch" ; + rdfs:comment "A feature to notify that the user has grabbed a port control." . + +ui:requestValue + a lv2:Feature ; + rdfs:label "request value" ; + rdfs:comment "A feature to request a parameter value from the user via the host." . + +ui:idleInterface + a lv2:Feature , + lv2:ExtensionData ; + rdfs:label "idle interface" ; + rdfs:comment "A feature that provides a callback for the host to drive the UI." . + +ui:showInterface + a lv2:ExtensionData ; + rdfs:label "show interface" ; + rdfs:comment "An interface for showing and hiding a window for a UI." . + +ui:windowTitle + a rdf:Property , + owl:DatatypeProperty ; + rdfs:range xsd:string ; + rdfs:label "window title" ; + rdfs:comment "The title for the window shown by LV2UI_Show_Interface." . + +ui:updateRate + a rdf:Property , + owl:DatatypeProperty ; + rdfs:range xsd:float ; + rdfs:label "update rate" ; + rdfs:comment "The target rate, in Hz, to send updates to the UI." . + +ui:protocol + a rdf:Property , + owl:ObjectProperty ; + rdfs:domain ui:PortNotification ; + rdfs:range ui:PortProtocol ; + rdfs:label "protocol" ; + rdfs:comment "The protocol to be used for this notification." . + +ui:PortProtocol + a rdfs:Class ; + rdfs:subClassOf lv2:Feature ; + rdfs:label "Port Protocol" ; + rdfs:comment "A method to communicate port data between a UI and plugin." . + +ui:floatProtocol + a ui:PortProtocol ; + rdfs:label "float protocol" ; + rdfs:comment "A protocol for transferring single floating point values." . + +ui:peakProtocol + a ui:PortProtocol ; + rdfs:label "peak protocol" ; + rdfs:comment "A protocol for sending continuous peak measurements of an audio signal." . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/units/manifest.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/units/manifest.ttl new file mode 100644 index 0000000000..c6c9286bd3 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/units/manifest.ttl @@ -0,0 +1,9 @@ +@prefix lv2: . +@prefix rdfs: . + + + a lv2:Specification ; + lv2:minorVersion 5 ; + lv2:microVersion 12 ; + rdfs:seeAlso . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/units/units.h b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/units/units.h new file mode 100644 index 0000000000..09ae16befc --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/units/units.h @@ -0,0 +1,75 @@ +/* + Copyright 2012-2016 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef LV2_UNITS_H +#define LV2_UNITS_H + +/** + @defgroup units Units + @ingroup lv2 + + Units for LV2 values. + + See for details. + + @{ +*/ + +// clang-format off + +#define LV2_UNITS_URI "http://lv2plug.in/ns/extensions/units" ///< http://lv2plug.in/ns/extensions/units +#define LV2_UNITS_PREFIX LV2_UNITS_URI "#" ///< http://lv2plug.in/ns/extensions/units# + +#define LV2_UNITS__Conversion LV2_UNITS_PREFIX "Conversion" ///< http://lv2plug.in/ns/extensions/units#Conversion +#define LV2_UNITS__Unit LV2_UNITS_PREFIX "Unit" ///< http://lv2plug.in/ns/extensions/units#Unit +#define LV2_UNITS__bar LV2_UNITS_PREFIX "bar" ///< http://lv2plug.in/ns/extensions/units#bar +#define LV2_UNITS__beat LV2_UNITS_PREFIX "beat" ///< http://lv2plug.in/ns/extensions/units#beat +#define LV2_UNITS__bpm LV2_UNITS_PREFIX "bpm" ///< http://lv2plug.in/ns/extensions/units#bpm +#define LV2_UNITS__cent LV2_UNITS_PREFIX "cent" ///< http://lv2plug.in/ns/extensions/units#cent +#define LV2_UNITS__cm LV2_UNITS_PREFIX "cm" ///< http://lv2plug.in/ns/extensions/units#cm +#define LV2_UNITS__coef LV2_UNITS_PREFIX "coef" ///< http://lv2plug.in/ns/extensions/units#coef +#define LV2_UNITS__conversion LV2_UNITS_PREFIX "conversion" ///< http://lv2plug.in/ns/extensions/units#conversion +#define LV2_UNITS__db LV2_UNITS_PREFIX "db" ///< http://lv2plug.in/ns/extensions/units#db +#define LV2_UNITS__degree LV2_UNITS_PREFIX "degree" ///< http://lv2plug.in/ns/extensions/units#degree +#define LV2_UNITS__frame LV2_UNITS_PREFIX "frame" ///< http://lv2plug.in/ns/extensions/units#frame +#define LV2_UNITS__hz LV2_UNITS_PREFIX "hz" ///< http://lv2plug.in/ns/extensions/units#hz +#define LV2_UNITS__inch LV2_UNITS_PREFIX "inch" ///< http://lv2plug.in/ns/extensions/units#inch +#define LV2_UNITS__khz LV2_UNITS_PREFIX "khz" ///< http://lv2plug.in/ns/extensions/units#khz +#define LV2_UNITS__km LV2_UNITS_PREFIX "km" ///< http://lv2plug.in/ns/extensions/units#km +#define LV2_UNITS__m LV2_UNITS_PREFIX "m" ///< http://lv2plug.in/ns/extensions/units#m +#define LV2_UNITS__mhz LV2_UNITS_PREFIX "mhz" ///< http://lv2plug.in/ns/extensions/units#mhz +#define LV2_UNITS__midiNote LV2_UNITS_PREFIX "midiNote" ///< http://lv2plug.in/ns/extensions/units#midiNote +#define LV2_UNITS__mile LV2_UNITS_PREFIX "mile" ///< http://lv2plug.in/ns/extensions/units#mile +#define LV2_UNITS__min LV2_UNITS_PREFIX "min" ///< http://lv2plug.in/ns/extensions/units#min +#define LV2_UNITS__mm LV2_UNITS_PREFIX "mm" ///< http://lv2plug.in/ns/extensions/units#mm +#define LV2_UNITS__ms LV2_UNITS_PREFIX "ms" ///< http://lv2plug.in/ns/extensions/units#ms +#define LV2_UNITS__name LV2_UNITS_PREFIX "name" ///< http://lv2plug.in/ns/extensions/units#name +#define LV2_UNITS__oct LV2_UNITS_PREFIX "oct" ///< http://lv2plug.in/ns/extensions/units#oct +#define LV2_UNITS__pc LV2_UNITS_PREFIX "pc" ///< http://lv2plug.in/ns/extensions/units#pc +#define LV2_UNITS__prefixConversion LV2_UNITS_PREFIX "prefixConversion" ///< http://lv2plug.in/ns/extensions/units#prefixConversion +#define LV2_UNITS__render LV2_UNITS_PREFIX "render" ///< http://lv2plug.in/ns/extensions/units#render +#define LV2_UNITS__s LV2_UNITS_PREFIX "s" ///< http://lv2plug.in/ns/extensions/units#s +#define LV2_UNITS__semitone12TET LV2_UNITS_PREFIX "semitone12TET" ///< http://lv2plug.in/ns/extensions/units#semitone12TET +#define LV2_UNITS__symbol LV2_UNITS_PREFIX "symbol" ///< http://lv2plug.in/ns/extensions/units#symbol +#define LV2_UNITS__unit LV2_UNITS_PREFIX "unit" ///< http://lv2plug.in/ns/extensions/units#unit + +// clang-format on + +/** + @} +*/ + +#endif /* LV2_UNITS_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/units/units.meta.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/units/units.meta.ttl new file mode 100644 index 0000000000..a85704692e --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/units/units.meta.ttl @@ -0,0 +1,154 @@ +@prefix dcs: . +@prefix doap: . +@prefix foaf: . +@prefix lv2: . +@prefix rdfs: . +@prefix units: . + + + a doap:Project ; + doap:name "LV2 Units" ; + doap:shortdesc "Units for LV2 values." ; + doap:created "2007-02-06" ; + doap:homepage ; + doap:license ; + doap:release [ + doap:revision "5.12" ; + doap:created "2019-02-03" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Fix outdated port description in documentation." + ] , [ + rdfs:label "Remove overly restrictive domain from units:unit." + ] + ] + ] , [ + doap:revision "5.10" ; + doap:created "2015-04-07" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Fix non-existent port type in examples." + ] , [ + rdfs:label "Add lv2:Parameter to domain of units:unit." + ] + ] + ] , [ + doap:revision "5.8" ; + doap:created "2012-10-14" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Remove units:name in favour of rdfs:label." + ] , [ + rdfs:label "Use consistent label style." + ] + ] + ] , [ + doap:revision "5.6" ; + doap:created "2012-04-17" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add unit for audio frames." + ] , [ + rdfs:label "Add header of URI defines." + ] , [ + rdfs:label "Merge with unified LV2 package." + ] + ] + ] , [ + doap:revision "5.4" ; + doap:created "2011-11-21" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Make units.ttl a valid OWL 2 DL ontology." + ] , [ + rdfs:label "Define used but undefined resources (units:name, units:render, units:symbol, units:Conversion, units:conversion, units:prefixConversion, units:to, and units:factor)." + ] , [ + rdfs:label "Update packaging." + ] , [ + rdfs:label "Improve documentation." + ] + ] + ] , [ + doap:revision "5.2" ; + doap:created "2010-10-05" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add build system (for installation)." + ] , [ + rdfs:label "Convert documentation to HTML and use lv2:documentation." + ] + ] + ] , [ + doap:revision "5.0" ; + doap:created "2010-10-05" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Initial release." + ] , [ + rdfs:label "Define used but undefined resources (units:name, units:render, units:symbol, units:Conversion, units:conversion, units:prefixConversion, units:to, and units:factor)." + ] , [ + rdfs:label "Update packaging." + ] , [ + rdfs:label "Improve documentation." + ] + ] + ] ; + doap:developer ; + doap:maintainer ; + lv2:documentation """ + +This is a vocabulary for units typically used for control values in audio +processing. + +For example, to say that a gain control is in decibels: + + :::turtle + @prefix units: . + @prefix eg: . + + eg:plugin lv2:port [ + a lv2:ControlPort , lv2:InputPort ; + lv2:index 0 ; + lv2:symbol "gain" ; + lv2:name "Gain" ; + units:unit units:db + ] . + +Using the same form, plugins may also specify one-off units inline, to give +better display hints to hosts: + + :::turtle + eg:plugin lv2:port [ + a lv2:ControlPort , lv2:InputPort ; + lv2:index 0 ; + lv2:symbol "frob" ; + lv2:name "frob level" ; + units:unit [ + a units:Unit ; + rdfs:label "frobnication" ; + units:symbol "fr" ; + units:render "%f f" + ] + ] . + +It is also possible to define conversions between various units, which makes it +possible for hosts to automatically convert between units where possible. The +units defined in this extension include conversion definitions where it makes +sense to do so. + +"""^^lv2:Markdown . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/units/units.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/units/units.ttl new file mode 100644 index 0000000000..959c063983 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/units/units.ttl @@ -0,0 +1,379 @@ +@prefix owl: . +@prefix rdf: . +@prefix rdfs: . +@prefix units: . +@prefix xsd: . + + + a owl:Ontology ; + rdfs:label "LV2 Units" ; + rdfs:comment "Units for LV2 values." ; + rdfs:seeAlso , + . + +units:Unit + a rdfs:Class , + owl:Class ; + rdfs:label "Unit" ; + rdfs:comment "A unit for a control value." . + +units:unit + a rdf:Property , + owl:ObjectProperty ; + rdfs:range units:Unit ; + rdfs:label "unit" ; + rdfs:comment "The unit used by the value of a port or parameter." . + +units:render + a rdf:Property , + owl:DatatypeProperty ; + rdfs:label "unit format string" ; + rdfs:domain units:Unit ; + rdfs:range xsd:string ; + rdfs:comment """A printf format string for rendering a value (e.g., "%f dB").""" . + +units:symbol + a rdf:Property , + owl:DatatypeProperty ; + rdfs:label "unit symbol" ; + rdfs:domain units:Unit ; + rdfs:range xsd:string ; + rdfs:comment """The abbreviated symbol for this unit (e.g., "dB").""" . + +units:Conversion + a rdfs:Class , + owl:Class ; + rdfs:subClassOf [ + a owl:Restriction ; + owl:onProperty units:to ; + owl:cardinality 1 ; + rdfs:comment "A conversion MUST have exactly 1 units:to property." + ] ; + rdfs:label "Conversion" ; + rdfs:comment "A conversion from one unit to another." . + +units:conversion + a rdf:Property , + owl:ObjectProperty ; + rdfs:domain units:Unit ; + rdfs:range units:Conversion ; + rdfs:label "conversion" ; + rdfs:comment "A conversion from this unit to another." . + +units:prefixConversion + a rdf:Property , + owl:ObjectProperty ; + rdfs:subPropertyOf units:conversion ; + rdfs:domain units:Unit ; + rdfs:range units:Conversion ; + rdfs:label "prefix conversion" ; + rdfs:comment "A conversion from this unit to another with the same base but a different prefix." . + +units:to + a rdf:Property , + owl:ObjectProperty ; + rdfs:domain units:Conversion ; + rdfs:range units:Unit ; + rdfs:label "conversion target" ; + rdfs:comment "The target unit this conversion converts to." . + +units:factor + a rdf:Property , + owl:DatatypeProperty ; + rdfs:domain units:Conversion ; + rdfs:label "conversion factor" ; + rdfs:comment "The factor to multiply the source value by in order to convert to the target unit." . + +units:s + a units:Unit ; + units:conversion [ + units:factor 0.0166666666 ; + units:to units:min + ] ; + rdfs:label "seconds" ; + rdfs:comment "Seconds, the SI base unit for time." ; + units:prefixConversion [ + units:factor 1000 ; + units:to units:ms + ] ; + units:render "%f s" ; + units:symbol "s" . + +units:ms + a units:Unit ; + rdfs:label "milliseconds" ; + rdfs:comment "Milliseconds (thousandths of seconds)." ; + units:prefixConversion [ + units:factor 0.001 ; + units:to units:s + ] ; + units:render "%f ms" ; + units:symbol "ms" . + +units:min + a units:Unit ; + units:conversion [ + units:factor 60.0 ; + units:to units:s + ] ; + rdfs:label "minutes" ; + rdfs:comment "Minutes (60s of seconds and 60ths of an hour)." ; + units:render "%f mins" ; + units:symbol "min" . + +units:bar + a units:Unit ; + rdfs:label "bars" ; + rdfs:comment "Musical bars or measures." ; + units:render "%f bars" ; + units:symbol "bars" . + +units:beat + a units:Unit ; + rdfs:label "beats" ; + rdfs:comment "Musical beats." ; + units:render "%f beats" ; + units:symbol "beats" . + +units:frame + a units:Unit ; + rdfs:label "audio frames" ; + rdfs:comment "Audio frames or samples." ; + units:render "%f frames" ; + units:symbol "frames" . + +units:m + a units:Unit ; + units:conversion [ + units:factor 39.37 ; + units:to units:inch + ] ; + rdfs:label "metres" ; + rdfs:comment "Metres, the SI base unit for length." ; + units:prefixConversion [ + units:factor 100 ; + units:to units:cm + ] , [ + units:factor 1000 ; + units:to units:mm + ] , [ + units:factor 0.001 ; + units:to units:km + ] ; + units:render "%f m" ; + units:symbol "m" . + +units:cm + a units:Unit ; + units:conversion [ + units:factor 0.3937 ; + units:to units:inch + ] ; + rdfs:label "centimetres" ; + rdfs:comment "Centimetres (hundredths of metres)." ; + units:prefixConversion [ + units:factor 0.01 ; + units:to units:m + ] , [ + units:factor 10 ; + units:to units:mm + ] , [ + units:factor 0.00001 ; + units:to units:km + ] ; + units:render "%f cm" ; + units:symbol "cm" . + +units:mm + a units:Unit ; + units:conversion [ + units:factor 0.03937 ; + units:to units:inch + ] ; + rdfs:label "millimetres" ; + rdfs:comment "Millimetres (thousandths of metres)." ; + units:prefixConversion [ + units:factor 0.001 ; + units:to units:m + ] , [ + units:factor 0.1 ; + units:to units:cm + ] , [ + units:factor 0.000001 ; + units:to units:km + ] ; + units:render "%f mm" ; + units:symbol "mm" . + +units:km + a units:Unit ; + units:conversion [ + units:factor 0.62138818 ; + units:to units:mile + ] ; + rdfs:label "kilometres" ; + rdfs:comment "Kilometres (thousands of metres)." ; + units:prefixConversion [ + units:factor 1000 ; + units:to units:m + ] , [ + units:factor 100000 ; + units:to units:cm + ] , [ + units:factor 1000000 ; + units:to units:mm + ] ; + units:render "%f km" ; + units:symbol "km" . + +units:inch + a units:Unit ; + units:conversion [ + units:factor 0.0254 ; + units:to units:m + ] ; + rdfs:label "inches" ; + rdfs:comment "An inch, defined as exactly 0.0254 metres." ; + units:render "%f\"" ; + units:symbol "in" . + +units:mile + a units:Unit ; + units:conversion [ + units:factor 1609.344 ; + units:to units:m + ] ; + rdfs:label "miles" ; + rdfs:comment "A mile, defined as exactly 1609.344 metres." ; + units:render "%f mi" ; + units:symbol "mi" . + +units:db + a units:Unit ; + rdfs:label "decibels" ; + rdfs:comment "Decibels, a logarithmic relative unit where 0 is unity." ; + units:render "%f dB" ; + units:symbol "dB" . + +units:pc + a units:Unit ; + units:conversion [ + units:factor 0.01 ; + units:to units:coef + ] ; + rdfs:label "percent" ; + rdfs:comment "Percentage, a ratio as a fraction of 100." ; + units:render "%f%%" ; + units:symbol "%" . + +units:coef + a units:Unit ; + units:conversion [ + units:factor 100 ; + units:to units:pc + ] ; + rdfs:label "coefficient" ; + rdfs:comment "A scale coefficient where 1 is unity, or 100 percent." ; + units:render "* %f" ; + units:symbol "" . + +units:hz + a units:Unit ; + rdfs:label "hertz" ; + rdfs:comment "Hertz, or inverse seconds, the SI derived unit for frequency." ; + units:prefixConversion [ + units:factor 0.001 ; + units:to units:khz + ] , [ + units:factor 0.000001 ; + units:to units:mhz + ] ; + units:render "%f Hz" ; + units:symbol "Hz" . + +units:khz + a units:Unit ; + rdfs:label "kilohertz" ; + rdfs:comment "Kilohertz (thousands of Hertz)." ; + units:prefixConversion [ + units:factor 1000 ; + units:to units:hz + ] , [ + units:factor 0.001 ; + units:to units:mhz + ] ; + units:render "%f kHz" ; + units:symbol "kHz" . + +units:mhz + a units:Unit ; + rdfs:label "megahertz" ; + rdfs:comment "Megahertz (millions of Hertz)." ; + units:prefixConversion [ + units:factor 1000000 ; + units:to units:hz + ] , [ + units:factor 0.001 ; + units:to units:khz + ] ; + units:render "%f MHz" ; + units:symbol "MHz" . + +units:bpm + a units:Unit ; + rdfs:label "beats per minute" ; + rdfs:comment "Beats Per Minute (BPM), the standard unit for musical tempo." ; + units:prefixConversion [ + units:factor 0.0166666666 ; + units:to units:hz + ] ; + units:render "%f BPM" ; + units:symbol "BPM" . + +units:oct + a units:Unit ; + units:conversion [ + units:factor 12.0 ; + units:to units:semitone12TET + ] ; + rdfs:label "octaves" ; + rdfs:comment "Octaves, relative musical pitch where +1 octave doubles the frequency." ; + units:render "%f octaves" ; + units:symbol "oct" . + +units:cent + a units:Unit ; + units:conversion [ + units:factor 0.01 ; + units:to units:semitone12TET + ] ; + rdfs:label "cents" ; + rdfs:comment "Cents (hundredths of semitones)." ; + units:render "%f ct" ; + units:symbol "ct" . + +units:semitone12TET + a units:Unit ; + units:conversion [ + units:factor 0.083333333 ; + units:to units:oct + ] ; + rdfs:label "semitones" ; + rdfs:comment "A semitone in the 12-tone equal temperament scale." ; + units:render "%f semi" ; + units:symbol "semi" . + +units:degree + a units:Unit ; + rdfs:label "degrees" ; + rdfs:comment "An angle where 360 degrees is one full rotation." ; + units:render "%f deg" ; + units:symbol "deg" . + +units:midiNote + a units:Unit ; + rdfs:label "MIDI note" ; + rdfs:comment "A MIDI note number." ; + units:render "MIDI note %d" ; + units:symbol "note" . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/uri-map/manifest.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/uri-map/manifest.ttl new file mode 100644 index 0000000000..a64e4fbda8 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/uri-map/manifest.ttl @@ -0,0 +1,9 @@ +@prefix lv2: . +@prefix rdfs: . + + + a lv2:Specification ; + lv2:minorVersion 1 ; + lv2:microVersion 6 ; + rdfs:seeAlso . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/uri-map/uri-map.h b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/uri-map/uri-map.h new file mode 100644 index 0000000000..47cde1cd55 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/uri-map/uri-map.h @@ -0,0 +1,121 @@ +/* + Copyright 2008-2016 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef LV2_URI_MAP_H +#define LV2_URI_MAP_H + +/** + @defgroup uri-map URI Map + @ingroup lv2 + + A feature for mapping URIs to integers. + + This extension defines a simple mechanism for plugins to map URIs to + integers, usually for performance reasons (e.g. processing events typed by + URIs in real time). The expected use case is for plugins to map URIs to + integers for things they 'understand' at instantiation time, and store those + values for use in the audio thread without doing any string comparison. + This allows the extensibility of RDF with the performance of integers (or + centrally defined enumerations). + + See for details. + + @{ +*/ + +// clang-format off + +#define LV2_URI_MAP_URI "http://lv2plug.in/ns/ext/uri-map" ///< http://lv2plug.in/ns/ext/uri-map +#define LV2_URI_MAP_PREFIX LV2_URI_MAP_URI "#" ///< http://lv2plug.in/ns/ext/uri-map# + +// clang-format on + +#include "lv2/core/attributes.h" + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +LV2_DISABLE_DEPRECATION_WARNINGS + +/** + Opaque pointer to host data. +*/ +LV2_DEPRECATED +typedef void* LV2_URI_Map_Callback_Data; + +/** + URI Map Feature. + + To support this feature the host must pass an LV2_Feature struct to the + plugin's instantiate method with URI "http://lv2plug.in/ns/ext/uri-map" + and data pointed to an instance of this struct. +*/ +LV2_DEPRECATED +typedef struct { + /** + Opaque pointer to host data. + + The plugin MUST pass this to any call to functions in this struct. + Otherwise, it must not be interpreted in any way. + */ + LV2_URI_Map_Callback_Data callback_data; + + /** + Get the numeric ID of a URI from the host. + + @param callback_data Must be the callback_data member of this struct. + @param map The 'context' of this URI. Certain extensions may define a + URI that must be passed here with certain restrictions on the return + value (e.g. limited range). This value may be NULL if the plugin needs + an ID for a URI in general. Extensions SHOULD NOT define a context + unless there is a specific need to do so, e.g. to restrict the range of + the returned value. + @param uri The URI to be mapped to an integer ID. + + This function is referentially transparent; any number of calls with the + same arguments is guaranteed to return the same value over the life of a + plugin instance (though the same URI may return different values with a + different map parameter). However, this function is not necessarily very + fast: plugins SHOULD cache any IDs they might need in performance + critical situations. + + The return value 0 is reserved and indicates that an ID for that URI + could not be created for whatever reason. Extensions MAY define more + precisely what this means in a certain context, but in general plugins + SHOULD handle this situation as gracefully as possible. However, hosts + SHOULD NOT return 0 from this function in non-exceptional circumstances + (e.g. the URI map SHOULD be dynamic). Hosts that statically support only + a fixed set of URIs should not expect plugins to function correctly. + */ + uint32_t (*uri_to_id)(LV2_URI_Map_Callback_Data callback_data, + const char* map, + const char* uri); +} LV2_URI_Map_Feature; + +LV2_RESTORE_WARNINGS + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +/** + @} +*/ + +#endif /* LV2_URI_MAP_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/uri-map/uri-map.meta.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/uri-map/uri-map.meta.ttl new file mode 100644 index 0000000000..acd1c26716 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/uri-map/uri-map.meta.ttl @@ -0,0 +1,72 @@ +@prefix lv2: . +@prefix dcs: . +@prefix doap: . +@prefix foaf: . +@prefix rdfs: . + + + a doap:Project ; + doap:maintainer ; + doap:created "2008-00-00" ; + doap:developer , + ; + doap:license ; + doap:name "LV2 URI Map" ; + doap:shortdesc "A feature for mapping URIs to integers." ; + doap:release [ + doap:revision "1.6" ; + doap:created "2012-04-17" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Merge with unified LV2 package." + ] + ] + ] , [ + doap:revision "1.4" ; + doap:created "2011-11-21" ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Update packaging." + ] , [ + rdfs:label "Deprecate uri-map in favour of urid." + ] + ] + ] , [ + doap:revision "1.2" ; + doap:created "2011-05-26" ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add build system (for installation)." + ] , [ + rdfs:label "Mark up documentation in HTML using lv2:documentation." + ] + ] + ] , [ + doap:revision "1.0" ; + doap:created "2010-10-18" ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Initial release." + ] + ] + ] ; + lv2:documentation """ + +This extension is deprecated. New implementations +should use [LV2 URID](urid.html) instead. + +This extension defines a simple mechanism for plugins to map URIs to integers, +usually for performance reasons (e.g. processing events typed by URIs in real +time). The expected use case is for plugins to map URIs to integers for things +they 'understand' at instantiation time, and store those values for use in the +audio thread without doing any string comparison. This allows the +extensibility of RDF with the performance of integers (or centrally defined +enumerations). + +"""^^lv2:Markdown . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/uri-map/uri-map.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/uri-map/uri-map.ttl new file mode 100644 index 0000000000..bfb0d0b612 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/uri-map/uri-map.ttl @@ -0,0 +1,14 @@ +@prefix lv2: . +@prefix owl: . +@prefix rdf: . +@prefix rdfs: . +@prefix umap: . + + + a lv2:Feature ; + owl:deprecated true ; + rdfs:label "LV2 URI Map" ; + rdfs:comment "A feature for mapping URIs to integers." ; + rdfs:seeAlso , + . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/urid/manifest.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/urid/manifest.ttl new file mode 100644 index 0000000000..772e2b6cee --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/urid/manifest.ttl @@ -0,0 +1,9 @@ +@prefix lv2: . +@prefix rdfs: . + + + a lv2:Specification ; + lv2:minorVersion 1 ; + lv2:microVersion 4 ; + rdfs:seeAlso . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/urid/urid.h b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/urid/urid.h new file mode 100644 index 0000000000..b537d14e56 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/urid/urid.h @@ -0,0 +1,140 @@ +/* + Copyright 2008-2016 David Robillard + Copyright 2011 Gabriel M. Beddingfield + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef LV2_URID_H +#define LV2_URID_H + +/** + @defgroup urid URID + @ingroup lv2 + + Features for mapping URIs to and from integers. + + See for details. + + @{ +*/ + +// clang-format off + +#define LV2_URID_URI "http://lv2plug.in/ns/ext/urid" ///< http://lv2plug.in/ns/ext/urid +#define LV2_URID_PREFIX LV2_URID_URI "#" ///< http://lv2plug.in/ns/ext/urid# + +#define LV2_URID__map LV2_URID_PREFIX "map" ///< http://lv2plug.in/ns/ext/urid#map +#define LV2_URID__unmap LV2_URID_PREFIX "unmap" ///< http://lv2plug.in/ns/ext/urid#unmap + +#define LV2_URID_MAP_URI LV2_URID__map ///< Legacy +#define LV2_URID_UNMAP_URI LV2_URID__unmap ///< Legacy + +// clang-format on + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + Opaque pointer to host data for LV2_URID_Map. +*/ +typedef void* LV2_URID_Map_Handle; + +/** + Opaque pointer to host data for LV2_URID_Unmap. +*/ +typedef void* LV2_URID_Unmap_Handle; + +/** + URI mapped to an integer. +*/ +typedef uint32_t LV2_URID; + +/** + URID Map Feature (LV2_URID__map) +*/ +typedef struct { + /** + Opaque pointer to host data. + + This MUST be passed to map_uri() whenever it is called. + Otherwise, it must not be interpreted in any way. + */ + LV2_URID_Map_Handle handle; + + /** + Get the numeric ID of a URI. + + If the ID does not already exist, it will be created. + + This function is referentially transparent; any number of calls with the + same arguments is guaranteed to return the same value over the life of a + plugin instance. Note, however, that several URIs MAY resolve to the + same ID if the host considers those URIs equivalent. + + This function is not necessarily very fast or RT-safe: plugins SHOULD + cache any IDs they might need in performance critical situations. + + The return value 0 is reserved and indicates that an ID for that URI + could not be created for whatever reason. However, hosts SHOULD NOT + return 0 from this function in non-exceptional circumstances (i.e. the + URI map SHOULD be dynamic). + + @param handle Must be the callback_data member of this struct. + @param uri The URI to be mapped to an integer ID. + */ + LV2_URID (*map)(LV2_URID_Map_Handle handle, const char* uri); +} LV2_URID_Map; + +/** + URI Unmap Feature (LV2_URID__unmap) +*/ +typedef struct { + /** + Opaque pointer to host data. + + This MUST be passed to unmap() whenever it is called. + Otherwise, it must not be interpreted in any way. + */ + LV2_URID_Unmap_Handle handle; + + /** + Get the URI for a previously mapped numeric ID. + + Returns NULL if `urid` is not yet mapped. Otherwise, the corresponding + URI is returned in a canonical form. This MAY not be the exact same + string that was originally passed to LV2_URID_Map::map(), but it MUST be + an identical URI according to the URI syntax specification (RFC3986). A + non-NULL return for a given `urid` will always be the same for the life + of the plugin. Plugins that intend to perform string comparison on + unmapped URIs SHOULD first canonicalise URI strings with a call to + map_uri() followed by a call to unmap_uri(). + + @param handle Must be the callback_data member of this struct. + @param urid The ID to be mapped back to the URI string. + */ + const char* (*unmap)(LV2_URID_Unmap_Handle handle, LV2_URID urid); +} LV2_URID_Unmap; + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +/** + @} +*/ + +#endif /* LV2_URID_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/urid/urid.meta.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/urid/urid.meta.ttl new file mode 100644 index 0000000000..b2a74fa0b8 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/urid/urid.meta.ttl @@ -0,0 +1,84 @@ +@prefix dcs: . +@prefix doap: . +@prefix foaf: . +@prefix lv2: . +@prefix rdfs: . +@prefix urid: . + + + a doap:Project ; + doap:license ; + doap:name "LV2 URID" ; + doap:shortdesc "Features for mapping URIs to and from integers." ; + doap:created "2011-07-22" ; + doap:developer ; + doap:maintainer ; + doap:release [ + doap:revision "1.4" ; + doap:created "2012-10-14" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Fix typo in urid:unmap documentation." + ] + ] + ] , [ + doap:revision "1.2" ; + doap:created "2012-04-17" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Add feature struct names." + ] , [ + rdfs:label "Merge with unified LV2 package." + ] + ] + ] , [ + doap:revision "1.0" ; + doap:created "2011-11-21" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Initial release." + ] + ] + ] ; + lv2:documentation """ + +This extension defines a simple mechanism for plugins to map URIs to and from +integers. This is usually used for performance reasons, for example for +processing events with URI types in real-time audio code). Typically, plugins +map URIs to integers for things they "understand" at instantiation time, and +store those values for use in the audio thread without doing any string +comparison. This allows for the extensibility of RDF but with the performance +of integers. + +This extension is intended as an improved and simplified replacement for the +[uri-map](uri-map.html) extension, since the `map` context parameter there has +proven problematic. This extension is functionally equivalent to the uri-map +extension with a NULL context. New implementations are encouraged to use this +extension for URI mapping. + +"""^^lv2:Markdown . + +urid:map + lv2:documentation """ + +To support this feature, the host must pass an LV2_Feature to +LV2_Descriptor::instantiate() with URI LV2_URID__map and data pointed to an +instance of LV2_URID_Map. + +"""^^lv2:Markdown . + +urid:unmap + lv2:documentation """ + +To support this feature, the host must pass an LV2_Feature to +LV2_Descriptor::instantiate() with URI LV2_URID__unmap and data pointed to an +instance of LV2_URID_Unmap. + +"""^^lv2:Markdown . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/urid/urid.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/urid/urid.ttl new file mode 100644 index 0000000000..2c44b561fd --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/urid/urid.ttl @@ -0,0 +1,22 @@ +@prefix lv2: . +@prefix owl: . +@prefix rdfs: . +@prefix urid: . + + + a owl:Ontology ; + rdfs:label "LV2 URID" ; + rdfs:comment "Features for mapping URIs to and from integers." ; + rdfs:seeAlso , + . + +urid:map + a lv2:Feature ; + rdfs:label "map" ; + rdfs:comment "A feature to map URI strings to integer URIDs." . + +urid:unmap + a lv2:Feature ; + rdfs:label "unmap" ; + rdfs:comment "A feature to unmap URIDs back to strings." . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/worker/manifest.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/worker/manifest.ttl new file mode 100644 index 0000000000..692720db2b --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/worker/manifest.ttl @@ -0,0 +1,9 @@ +@prefix lv2: . +@prefix rdfs: . + + + a lv2:Specification ; + lv2:minorVersion 1 ; + lv2:microVersion 2 ; + rdfs:seeAlso . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/worker/worker.h b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/worker/worker.h new file mode 100644 index 0000000000..4fd89f9fbf --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/worker/worker.h @@ -0,0 +1,183 @@ +/* + Copyright 2012-2016 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef LV2_WORKER_H +#define LV2_WORKER_H + +/** + @defgroup worker Worker + @ingroup lv2 + + Support for non-realtime plugin operations. + + See for details. + + @{ +*/ + +#include "lv2/core/lv2.h" + +#include + +// clang-format off + +#define LV2_WORKER_URI "http://lv2plug.in/ns/ext/worker" ///< http://lv2plug.in/ns/ext/worker +#define LV2_WORKER_PREFIX LV2_WORKER_URI "#" ///< http://lv2plug.in/ns/ext/worker# + +#define LV2_WORKER__interface LV2_WORKER_PREFIX "interface" ///< http://lv2plug.in/ns/ext/worker#interface +#define LV2_WORKER__schedule LV2_WORKER_PREFIX "schedule" ///< http://lv2plug.in/ns/ext/worker#schedule + +// clang-format on + +#ifdef __cplusplus +extern "C" { +#endif + +/** + Status code for worker functions. +*/ +typedef enum { + LV2_WORKER_SUCCESS = 0, /**< Completed successfully. */ + LV2_WORKER_ERR_UNKNOWN = 1, /**< Unknown error. */ + LV2_WORKER_ERR_NO_SPACE = 2 /**< Failed due to lack of space. */ +} LV2_Worker_Status; + +/** Opaque handle for LV2_Worker_Interface::work(). */ +typedef void* LV2_Worker_Respond_Handle; + +/** + A function to respond to run() from the worker method. + + The `data` MUST be safe for the host to copy and later pass to + work_response(), and the host MUST guarantee that it will be eventually + passed to work_response() if this function returns LV2_WORKER_SUCCESS. +*/ +typedef LV2_Worker_Status (*LV2_Worker_Respond_Function)( + LV2_Worker_Respond_Handle handle, + uint32_t size, + const void* data); + +/** + Plugin Worker Interface. + + This is the interface provided by the plugin to implement a worker method. + The plugin's extension_data() method should return an LV2_Worker_Interface + when called with LV2_WORKER__interface as its argument. +*/ +typedef struct { + /** + The worker method. This is called by the host in a non-realtime context + as requested, possibly with an arbitrary message to handle. + + A response can be sent to run() using `respond`. The plugin MUST NOT + make any assumptions about which thread calls this method, except that + there are no real-time requirements and only one call may be executed at + a time. That is, the host MAY call this method from any non-real-time + thread, but MUST NOT make concurrent calls to this method from several + threads. + + @param instance The LV2 instance this is a method on. + @param respond A function for sending a response to run(). + @param handle Must be passed to `respond` if it is called. + @param size The size of `data`. + @param data Data from run(), or NULL. + */ + LV2_Worker_Status (*work)(LV2_Handle instance, + LV2_Worker_Respond_Function respond, + LV2_Worker_Respond_Handle handle, + uint32_t size, + const void* data); + + /** + Handle a response from the worker. This is called by the host in the + run() context when a response from the worker is ready. + + @param instance The LV2 instance this is a method on. + @param size The size of `body`. + @param body Message body, or NULL. + */ + LV2_Worker_Status (*work_response)(LV2_Handle instance, + uint32_t size, + const void* body); + + /** + Called when all responses for this cycle have been delivered. + + Since work_response() may be called after run() finished, this provides + a hook for code that must run after the cycle is completed. + + This field may be NULL if the plugin has no use for it. Otherwise, the + host MUST call it after every run(), regardless of whether or not any + responses were sent that cycle. + */ + LV2_Worker_Status (*end_run)(LV2_Handle instance); +} LV2_Worker_Interface; + +/** Opaque handle for LV2_Worker_Schedule. */ +typedef void* LV2_Worker_Schedule_Handle; + +/** + Schedule Worker Host Feature. + + The host passes this feature to provide a schedule_work() function, which + the plugin can use to schedule a worker call from run(). +*/ +typedef struct { + /** + Opaque host data. + */ + LV2_Worker_Schedule_Handle handle; + + /** + Request from run() that the host call the worker. + + This function is in the audio threading class. It should be called from + run() to request that the host call the work() method in a non-realtime + context with the given arguments. + + This function is always safe to call from run(), but it is not + guaranteed that the worker is actually called from a different thread. + In particular, when free-wheeling (for example, during offline + rendering), the worker may be executed immediately. This allows + single-threaded processing with sample accuracy and avoids timing + problems when run() is executing much faster or slower than real-time. + + Plugins SHOULD be written in such a way that if the worker runs + immediately, and responses from the worker are delivered immediately, + the effect of the work takes place immediately with sample accuracy. + + The `data` MUST be safe for the host to copy and later pass to work(), + and the host MUST guarantee that it will be eventually passed to work() + if this function returns LV2_WORKER_SUCCESS. + + @param handle The handle field of this struct. + @param size The size of `data`. + @param data Message to pass to work(), or NULL. + */ + LV2_Worker_Status (*schedule_work)(LV2_Worker_Schedule_Handle handle, + uint32_t size, + const void* data); +} LV2_Worker_Schedule; + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +/** + @} +*/ + +#endif /* LV2_WORKER_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/worker/worker.meta.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/worker/worker.meta.ttl new file mode 100644 index 0000000000..2fc51bc8aa --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/worker/worker.meta.ttl @@ -0,0 +1,82 @@ +@prefix dcs: . +@prefix doap: . +@prefix foaf: . +@prefix lv2: . +@prefix rdfs: . +@prefix work: . + + + a doap:Project ; + doap:name "LV2 Worker" ; + doap:shortdesc "Support for doing non-realtime work in plugins." ; + doap:created "2012-03-22" ; + doap:developer ; + doap:release [ + doap:revision "1.2" ; + doap:created "2020-04-26" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Improve documentation." + ] + ] + ] , [ + doap:revision "1.0" ; + doap:created "2012-04-17" ; + doap:file-release ; + dcs:blame ; + dcs:changeset [ + dcs:item [ + rdfs:label "Initial release." + ] + ] + ] ; + lv2:documentation """ + +This extension allows plugins to schedule work that must be performed in +another thread. Plugins can use this interface to safely perform work that is +not real-time safe, and receive the result in the run context. The details of +threading are managed by the host, allowing plugins to be simple and portable +while using resources more efficiently. + +This interface is designed to gracefully support single-threaded synchronous +execution, which allows the same code to work with sample accuracy for offline +rendering. For example, a sampler plugin may schedule a sample to be loaded +from disk in another thread. During live execution, the host will call the +plugin's work method from another thread, and deliver the result to the audio +thread when it is finished. However, during offline export, the +scheduled load would be executed immediately in the same thread. This +enables reproducible offline rendering, where any changes affect the output +immediately regardless of how long the work takes to execute. + +"""^^lv2:Markdown . + +work:interface + lv2:documentation """ + +The work interface provided by a plugin, LV2_Worker_Interface. + + :::turtle + + @prefix work: . + + + a lv2:Plugin ; + lv2:extensionData work:interface . + +"""^^lv2:Markdown . + +work:schedule + lv2:documentation """ + +The work scheduling feature provided by a host, LV2_Worker_Schedule. + +If the host passes this feature to LV2_Descriptor::instantiate, the plugin MAY +use it to schedule work in the audio thread, and MUST NOT call it in any other +context. Hosts MAY pass this feature to other functions as well, in which case +the plugin MAY use it to schedule work in the calling context. The plugin MUST +NOT assume any relationship between different schedule features. + +"""^^lv2:Markdown . + diff --git a/modules/juce_audio_processors/format_types/lv2/lv2/lv2/worker/worker.ttl b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/worker/worker.ttl new file mode 100644 index 0000000000..cb8a81a706 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/lv2/lv2/worker/worker.ttl @@ -0,0 +1,25 @@ +@prefix lv2: . +@prefix owl: . +@prefix rdf: . +@prefix rdfs: . +@prefix work: . +@prefix xsd: . + + + a owl:Ontology ; + rdfs:label "LV2 Worker" ; + rdfs:comment "Support for doing non-realtime work in plugins." ; + owl:imports ; + rdfs:seeAlso , + . + +work:interface + a lv2:ExtensionData ; + rdfs:label "work interface" ; + rdfs:comment "The work interface provided by a plugin." . + +work:schedule + a lv2:Feature ; + rdfs:label "work schedule" ; + rdfs:comment "The work scheduling feature provided by a host." . + diff --git a/modules/juce_audio_processors/format_types/lv2/serd/COPYING b/modules/juce_audio_processors/format_types/lv2/serd/COPYING new file mode 100644 index 0000000000..9748bb93d7 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/serd/COPYING @@ -0,0 +1,13 @@ +Copyright 2011-2021 David Robillard + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. \ No newline at end of file diff --git a/modules/juce_audio_processors/format_types/lv2/serd/serd/serd.h b/modules/juce_audio_processors/format_types/lv2/serd/serd/serd.h new file mode 100644 index 0000000000..32488e18c1 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/serd/serd/serd.h @@ -0,0 +1,1059 @@ +/* + Copyright 2011-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +/// @file serd.h API for Serd, a lightweight RDF syntax library + +#ifndef SERD_SERD_H +#define SERD_SERD_H + +#include +#include +#include +#include +#include + +#if defined(_WIN32) && !defined(SERD_STATIC) && defined(SERD_INTERNAL) +# define SERD_API __declspec(dllexport) +#elif defined(_WIN32) && !defined(SERD_STATIC) +# define SERD_API __declspec(dllimport) +#elif defined(__GNUC__) +# define SERD_API __attribute__((visibility("default"))) +#else +# define SERD_API +#endif + +#ifdef __GNUC__ +# define SERD_PURE_FUNC __attribute__((pure)) +# define SERD_CONST_FUNC __attribute__((const)) +#else +# define SERD_PURE_FUNC +# define SERD_CONST_FUNC +#endif + +#if defined(__clang__) && __clang_major__ >= 7 +# define SERD_NONNULL _Nonnull +# define SERD_NULLABLE _Nullable +# define SERD_ALLOCATED _Null_unspecified +#else +# define SERD_NONNULL +# define SERD_NULLABLE +# define SERD_ALLOCATED +#endif + +#define SERD_PURE_API \ + SERD_API \ + SERD_PURE_FUNC + +#define SERD_CONST_API \ + SERD_API \ + SERD_CONST_FUNC + +#ifndef SERD_DISABLE_DEPRECATED +# if defined(__clang__) && __clang_major__ >= 7 +# define SERD_DEPRECATED_BY(rep) __attribute__((deprecated("", rep))) +# elif defined(__GNUC__) && __GNUC__ > 4 +# define SERD_DEPRECATED_BY(rep) __attribute__((deprecated("Use " rep))) +# elif defined(__GNUC__) +# define SERD_DEPRECATED_BY(rep) __attribute__((deprecated)) +# else +# define SERD_DEPRECATED_BY(rep) +# endif +#endif + +#ifdef __cplusplus +extern "C" { +# if defined(__GNUC__) +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant" +# endif +#endif + +/** + @defgroup serd Serd C API + This is the complete public C API of serd. + @{ +*/ + +/// Lexical environment for relative URIs or CURIEs (base URI and namespaces) +typedef struct SerdEnvImpl SerdEnv; + +/// Streaming parser that reads a text stream and writes to a statement sink +typedef struct SerdReaderImpl SerdReader; + +/// Streaming serialiser that writes a text stream as statements are pushed +typedef struct SerdWriterImpl SerdWriter; + +/// Return status code +typedef enum { + SERD_SUCCESS, ///< No error + SERD_FAILURE, ///< Non-fatal failure + SERD_ERR_UNKNOWN, ///< Unknown error + SERD_ERR_BAD_SYNTAX, ///< Invalid syntax + SERD_ERR_BAD_ARG, ///< Invalid argument + SERD_ERR_NOT_FOUND, ///< Not found + SERD_ERR_ID_CLASH, ///< Encountered clashing blank node IDs + SERD_ERR_BAD_CURIE, ///< Invalid CURIE (e.g. prefix does not exist) + SERD_ERR_INTERNAL ///< Unexpected internal error (should not happen) +} SerdStatus; + +/// RDF syntax type +typedef enum { + SERD_TURTLE = 1, ///< Terse triples http://www.w3.org/TR/turtle + SERD_NTRIPLES = 2, ///< Line-based triples http://www.w3.org/TR/n-triples/ + SERD_NQUADS = 3, ///< Line-based quads http://www.w3.org/TR/n-quads/ + SERD_TRIG = 4 ///< Terse quads http://www.w3.org/TR/trig/ +} SerdSyntax; + +/// Flags indicating inline abbreviation information for a statement +typedef enum { + SERD_EMPTY_S = 1u << 1u, ///< Empty blank node subject + SERD_EMPTY_O = 1u << 2u, ///< Empty blank node object + SERD_ANON_S_BEGIN = 1u << 3u, ///< Start of anonymous subject + SERD_ANON_O_BEGIN = 1u << 4u, ///< Start of anonymous object + SERD_ANON_CONT = 1u << 5u, ///< Continuation of anonymous node + SERD_LIST_S_BEGIN = 1u << 6u, ///< Start of list subject + SERD_LIST_O_BEGIN = 1u << 7u, ///< Start of list object + SERD_LIST_CONT = 1u << 8u ///< Continuation of list +} SerdStatementFlag; + +/// Bitwise OR of SerdStatementFlag values +typedef uint32_t SerdStatementFlags; + +/** + Type of a node. + + An RDF node, in the abstract sense, can be either a resource, literal, or a + blank. This type is more precise, because syntactically there are two ways + to refer to a resource (by URI or CURIE). + + There are also two ways to refer to a blank node in syntax (by ID or + anonymously), but this is handled by statement flags rather than distinct + node types. +*/ +typedef enum { + /** + The type of a nonexistent node. + + This type is useful as a sentinel, but is never emitted by the reader. + */ + SERD_NOTHING = 0, + + /** + Literal value. + + A literal optionally has either a language, or a datatype (not both). + */ + SERD_LITERAL = 1, + + /** + URI (absolute or relative). + + Value is an unquoted URI string, which is either a relative reference + with respect to the current base URI (e.g. "foo/bar"), or an absolute + URI (e.g. "http://example.org/foo"). + @see [RFC3986](http://tools.ietf.org/html/rfc3986) + */ + SERD_URI = 2, + + /** + CURIE, a shortened URI. + + Value is an unquoted CURIE string relative to the current environment, + e.g. "rdf:type". @see [CURIE Syntax 1.0](http://www.w3.org/TR/curie) + */ + SERD_CURIE = 3, + + /** + A blank node. + + Value is a blank node ID without any syntactic prefix, like "id3", which + is meaningful only within this serialisation. @see [RDF 1.1 + Turtle](http://www.w3.org/TR/turtle/#grammar-production-BLANK_NODE_LABEL) + */ + SERD_BLANK = 4 +} SerdType; + +/// Flags indicating certain string properties relevant to serialisation +typedef enum { + SERD_HAS_NEWLINE = 1u << 0u, ///< Contains line breaks ('\\n' or '\\r') + SERD_HAS_QUOTE = 1u << 1u ///< Contains quotes ('"') +} SerdNodeFlag; + +/// Bitwise OR of SerdNodeFlag values +typedef uint32_t SerdNodeFlags; + +/// A syntactic RDF node +typedef struct { + const uint8_t* SERD_NULLABLE buf; ///< Value string + size_t n_bytes; ///< Size in bytes (excluding null) + size_t n_chars; ///< String length (excluding null) + SerdNodeFlags flags; ///< Node flags (string properties) + SerdType type; ///< Node type +} SerdNode; + +/// An unterminated string fragment +typedef struct { + const uint8_t* SERD_NULLABLE buf; ///< Start of chunk + size_t len; ///< Length of chunk in bytes +} SerdChunk; + +/// An error description +typedef struct { + SerdStatus status; ///< Error code + const uint8_t* SERD_NULLABLE filename; ///< File with error + unsigned line; ///< Line in file with error or 0 + unsigned col; ///< Column in file with error + const char* SERD_NONNULL fmt; ///< Printf-style format string + va_list* SERD_NONNULL args; ///< Arguments for fmt +} SerdError; + +/** + A parsed URI + + This struct directly refers to chunks in other strings, it does not own any + memory itself. Thus, URIs can be parsed and/or resolved against a base URI + in-place without allocating memory. +*/ +typedef struct { + SerdChunk scheme; ///< Scheme + SerdChunk authority; ///< Authority + SerdChunk path_base; ///< Path prefix if relative + SerdChunk path; ///< Path suffix + SerdChunk query; ///< Query + SerdChunk fragment; ///< Fragment +} SerdURI; + +/** + Syntax style options. + + These flags allow more precise control of writer output style. Note that + some options are only supported for some syntaxes, for example, NTriples + does not support abbreviation and is always ASCII. +*/ +typedef enum { + SERD_STYLE_ABBREVIATED = 1u << 0u, ///< Abbreviate triples when possible. + SERD_STYLE_ASCII = 1u << 1u, ///< Escape all non-ASCII characters. + SERD_STYLE_RESOLVED = 1u << 2u, ///< Resolve URIs against base URI. + SERD_STYLE_CURIED = 1u << 3u, ///< Shorten URIs into CURIEs. + SERD_STYLE_BULK = 1u << 4u, ///< Write output in pages. +} SerdStyle; + +/** + Free memory allocated by Serd + + This function exists because some systems require memory allocated by a + library to be freed by code in the same library. It is otherwise equivalent + to the standard C free() function. +*/ +SERD_API +void +serd_free(void* SERD_NULLABLE ptr); + +/** + @defgroup serd_string String Utilities + @{ +*/ + +/// Return a string describing a status code +SERD_CONST_API +const uint8_t* SERD_NONNULL +serd_strerror(SerdStatus status); + +/** + Measure a UTF-8 string. + + @return Length of `str` in characters (except NULL). + @param str A null-terminated UTF-8 string. + @param n_bytes (Output) Set to the size of `str` in bytes (except NULL). + @param flags (Output) Set to the applicable flags. +*/ +SERD_API +size_t +serd_strlen(const uint8_t* SERD_NONNULL str, + size_t* SERD_NULLABLE n_bytes, + SerdNodeFlags* SERD_NULLABLE flags); + +/** + Parse a string to a double. + + The API of this function is identical to the standard C strtod function, + except this function is locale-independent and always matches the lexical + format used in the Turtle grammar (the decimal point is always "."). +*/ +SERD_API +double +serd_strtod(const char* SERD_NONNULL str, + char* SERD_NONNULL* SERD_NULLABLE endptr); + +/** + Decode a base64 string. + + This function can be used to deserialise a blob node created with + serd_node_new_blob(). + + @param str Base64 string to decode. + @param len The length of `str`. + @param size Set to the size of the returned blob in bytes. + @return A newly allocated blob which must be freed with serd_free(). +*/ +SERD_API +void* SERD_ALLOCATED +serd_base64_decode(const uint8_t* SERD_NONNULL str, + size_t len, + size_t* SERD_NONNULL size); + +/** + @} + @defgroup serd_streams Byte Streams + @{ +*/ + +/** + Function to detect I/O stream errors. + + Identical semantics to `ferror`. + + @return Non-zero if `stream` has encountered an error. +*/ +typedef int (*SerdStreamErrorFunc)(void* SERD_NONNULL stream); + +/** + Source function for raw string input. + + Identical semantics to `fread`, but may set errno for more informative error + reporting than supported by SerdStreamErrorFunc. + + @param buf Output buffer. + @param size Size of a single element of data in bytes (always 1). + @param nmemb Number of elements to read. + @param stream Stream to read from (FILE* for fread). + @return Number of elements (bytes) read. +*/ +typedef size_t (*SerdSource)(void* SERD_NONNULL buf, + size_t size, + size_t nmemb, + void* SERD_NONNULL stream); + +/// Sink function for raw string output +typedef size_t (*SerdSink)(const void* SERD_NONNULL buf, + size_t len, + void* SERD_NONNULL stream); + +/** + @} + @defgroup serd_uri URI + @{ +*/ + +static const SerdURI SERD_URI_NULL = + {{NULL, 0}, {NULL, 0}, {NULL, 0}, {NULL, 0}, {NULL, 0}, {NULL, 0}}; + +#ifndef SERD_DISABLE_DEPRECATED + +/** + Return the local path for `uri`, or NULL if `uri` is not a file URI. + Note this (inappropriately named) function only removes the file scheme if + necessary, and returns `uri` unmodified if it is an absolute path. Percent + encoding and other issues are not handled, to properly convert a file URI to + a path, use serd_file_uri_parse(). +*/ +SERD_API +SERD_DEPRECATED_BY("serd_file_uri_parse") +const uint8_t* SERD_NULLABLE +serd_uri_to_path(const uint8_t* SERD_NONNULL uri); + +#endif + +/** + Get the unescaped path and hostname from a file URI. + + The returned path and `*hostname` must be freed with serd_free(). + + @param uri A file URI. + @param hostname If non-NULL, set to the hostname, if present. + @return The path component of the URI. +*/ +SERD_API +uint8_t* SERD_NULLABLE +serd_file_uri_parse(const uint8_t* SERD_NONNULL uri, + uint8_t* SERD_NONNULL* SERD_NULLABLE hostname); + +/// Return true iff `utf8` starts with a valid URI scheme +SERD_PURE_API +bool +serd_uri_string_has_scheme(const uint8_t* SERD_NULLABLE utf8); + +/// Parse `utf8`, writing result to `out` +SERD_API +SerdStatus +serd_uri_parse(const uint8_t* SERD_NONNULL utf8, SerdURI* SERD_NONNULL out); + +/** + Set target `t` to reference `r` resolved against `base`. + + @see [RFC3986 5.2.2](http://tools.ietf.org/html/rfc3986#section-5.2.2) +*/ +SERD_API +void +serd_uri_resolve(const SerdURI* SERD_NONNULL r, + const SerdURI* SERD_NONNULL base, + SerdURI* SERD_NONNULL t); + +/// Serialise `uri` with a series of calls to `sink` +SERD_API +size_t +serd_uri_serialise(const SerdURI* SERD_NONNULL uri, + SerdSink SERD_NONNULL sink, + void* SERD_NONNULL stream); + +/** + Serialise `uri` relative to `base` with a series of calls to `sink` + + The `uri` is written as a relative URI iff if it a child of `base` and + `root`. The optional `root` parameter must be a prefix of `base` and can be + used keep up-references ("../") within a certain namespace. +*/ +SERD_API +size_t +serd_uri_serialise_relative(const SerdURI* SERD_NONNULL uri, + const SerdURI* SERD_NULLABLE base, + const SerdURI* SERD_NULLABLE root, + SerdSink SERD_NONNULL sink, + void* SERD_NONNULL stream); + +/** + @} + @defgroup serd_node Node + @{ +*/ + +static const SerdNode SERD_NODE_NULL = {NULL, 0, 0, 0, SERD_NOTHING}; + +/** + Make a (shallow) node from `str`. + + This measures, but does not copy, `str`. No memory is allocated. +*/ +SERD_API +SerdNode +serd_node_from_string(SerdType type, const uint8_t* SERD_NULLABLE str); + +/** + Make a (shallow) node from a prefix of `str`. + + This measures, but does not copy, `str`. No memory is allocated. + Note that the returned node may not be null terminated. +*/ +SERD_API +SerdNode +serd_node_from_substring(SerdType type, + const uint8_t* SERD_NULLABLE str, + size_t len); + +/// Simple wrapper for serd_node_new_uri() to resolve a URI node +SERD_API +SerdNode +serd_node_new_uri_from_node(const SerdNode* SERD_NONNULL uri_node, + const SerdURI* SERD_NULLABLE base, + SerdURI* SERD_NULLABLE out); + +/// Simple wrapper for serd_node_new_uri() to resolve a URI string +SERD_API +SerdNode +serd_node_new_uri_from_string(const uint8_t* SERD_NULLABLE str, + const SerdURI* SERD_NULLABLE base, + SerdURI* SERD_NULLABLE out); + +/** + Create a new file URI node from a file system path and optional hostname. + + Backslashes in Windows paths will be converted and '%' will always be + percent encoded. If `escape` is true, all other invalid characters will be + percent encoded as well. + + If `path` is relative, `hostname` is ignored. + If `out` is not NULL, it will be set to the parsed URI. +*/ +SERD_API +SerdNode +serd_node_new_file_uri(const uint8_t* SERD_NONNULL path, + const uint8_t* SERD_NULLABLE hostname, + SerdURI* SERD_NULLABLE out, + bool escape); + +/** + Create a new node by serialising `uri` into a new string. + + @param uri The URI to serialise. + + @param base Base URI to resolve `uri` against (or NULL for no resolution). + + @param out Set to the parsing of the new URI (i.e. points only to + memory owned by the new returned node). +*/ +SERD_API +SerdNode +serd_node_new_uri(const SerdURI* SERD_NONNULL uri, + const SerdURI* SERD_NULLABLE base, + SerdURI* SERD_NULLABLE out); + +/** + Create a new node by serialising `uri` into a new relative URI. + + @param uri The URI to serialise. + + @param base Base URI to make `uri` relative to, if possible. + + @param root Root URI for resolution (see serd_uri_serialise_relative()). + + @param out Set to the parsing of the new URI (i.e. points only to + memory owned by the new returned node). +*/ +SERD_API +SerdNode +serd_node_new_relative_uri(const SerdURI* SERD_NONNULL uri, + const SerdURI* SERD_NULLABLE base, + const SerdURI* SERD_NULLABLE root, + SerdURI* SERD_NULLABLE out); + +/** + Create a new node by serialising `d` into an xsd:decimal string + + The resulting node will always contain a `.', start with a digit, and end + with a digit (i.e. will have a leading and/or trailing `0' if necessary). + It will never be in scientific notation. A maximum of `frac_digits` digits + will be written after the decimal point, but trailing zeros will + automatically be omitted (except one if `d` is a round integer). + + Note that about 16 and 8 fractional digits are required to precisely + represent a double and float, respectively. + + @param d The value for the new node. + @param frac_digits The maximum number of digits after the decimal place. +*/ +SERD_API +SerdNode +serd_node_new_decimal(double d, unsigned frac_digits); + +/// Create a new node by serialising `i` into an xsd:integer string +SERD_API +SerdNode +serd_node_new_integer(int64_t i); + +/** + Create a node by serialising `buf` into an xsd:base64Binary string + + This function can be used to make a serialisable node out of arbitrary + binary data, which can be decoded using serd_base64_decode(). + + @param buf Raw binary input data. + @param size Size of `buf`. + @param wrap_lines Wrap lines at 76 characters to conform to RFC 2045. +*/ +SERD_API +SerdNode +serd_node_new_blob(const void* SERD_NONNULL buf, size_t size, bool wrap_lines); + +/** + Make a deep copy of `node`. + + @return a node that the caller must free with serd_node_free(). +*/ +SERD_API +SerdNode +serd_node_copy(const SerdNode* SERD_NULLABLE node); + +/// Return true iff `a` is equal to `b` +SERD_PURE_API +bool +serd_node_equals(const SerdNode* SERD_NONNULL a, + const SerdNode* SERD_NONNULL b); + +/** + Free any data owned by `node`. + + Note that if `node` is itself dynamically allocated (which is not the case + for nodes created internally by serd), it will not be freed. +*/ +SERD_API +void +serd_node_free(SerdNode* SERD_NULLABLE node); + +/** + @} + @defgroup serd_event Event Handlers + @{ +*/ + +/** + Sink (callback) for errors. + + @param handle Handle for user data. + @param error Error description. +*/ +typedef SerdStatus (*SerdErrorSink)(void* SERD_NULLABLE handle, + const SerdError* SERD_NONNULL error); + +/** + Sink (callback) for base URI changes + + Called whenever the base URI of the serialisation changes. +*/ +typedef SerdStatus (*SerdBaseSink)(void* SERD_NULLABLE handle, + const SerdNode* SERD_NONNULL uri); + +/** + Sink (callback) for namespace definitions + + Called whenever a prefix is defined in the serialisation. +*/ +typedef SerdStatus (*SerdPrefixSink)(void* SERD_NULLABLE handle, + const SerdNode* SERD_NONNULL name, + const SerdNode* SERD_NONNULL uri); + +/** + Sink (callback) for statements + + Called for every RDF statement in the serialisation. +*/ +typedef SerdStatus (*SerdStatementSink)( + void* SERD_NULLABLE handle, + SerdStatementFlags flags, + const SerdNode* SERD_NULLABLE graph, + const SerdNode* SERD_NONNULL subject, + const SerdNode* SERD_NONNULL predicate, + const SerdNode* SERD_NONNULL object, + const SerdNode* SERD_NULLABLE object_datatype, + const SerdNode* SERD_NULLABLE object_lang); + +/** + Sink (callback) for anonymous node end markers + + This is called to indicate that the anonymous node with the given + `value` will no longer be referred to by any future statements + (i.e. the anonymous serialisation of the node is finished). +*/ +typedef SerdStatus (*SerdEndSink)(void* SERD_NULLABLE handle, + const SerdNode* SERD_NONNULL node); + +/** + @} + @defgroup serd_env Environment + @{ +*/ + +/// Create a new environment +SERD_API +SerdEnv* SERD_ALLOCATED +serd_env_new(const SerdNode* SERD_NULLABLE base_uri); + +/// Free `env` +SERD_API +void +serd_env_free(SerdEnv* SERD_NULLABLE env); + +/// Get the current base URI +SERD_API +const SerdNode* SERD_NONNULL +serd_env_get_base_uri(const SerdEnv* SERD_NONNULL env, + SerdURI* SERD_NULLABLE out); + +/// Set the current base URI +SERD_API +SerdStatus +serd_env_set_base_uri(SerdEnv* SERD_NONNULL env, + const SerdNode* SERD_NULLABLE uri); + +/** + Set a namespace prefix + + A namespace prefix is used to expand CURIE nodes, for example, with the + prefix "xsd" set to "http://www.w3.org/2001/XMLSchema#", "xsd:decimal" will + expand to "http://www.w3.org/2001/XMLSchema#decimal". +*/ +SERD_API +SerdStatus +serd_env_set_prefix(SerdEnv* SERD_NONNULL env, + const SerdNode* SERD_NONNULL name, + const SerdNode* SERD_NONNULL uri); + +/// Set a namespace prefix +SERD_API +SerdStatus +serd_env_set_prefix_from_strings(SerdEnv* SERD_NONNULL env, + const uint8_t* SERD_NONNULL name, + const uint8_t* SERD_NONNULL uri); + +/// Qualify `uri` into a CURIE if possible +SERD_API +bool +serd_env_qualify(const SerdEnv* SERD_NONNULL env, + const SerdNode* SERD_NONNULL uri, + SerdNode* SERD_NONNULL prefix, + SerdChunk* SERD_NONNULL suffix); + +/** + Expand `curie`. + + Errors: SERD_ERR_BAD_ARG if `curie` is not valid, or SERD_ERR_BAD_CURIE if + prefix is not defined in `env`. +*/ +SERD_API +SerdStatus +serd_env_expand(const SerdEnv* SERD_NONNULL env, + const SerdNode* SERD_NONNULL curie, + SerdChunk* SERD_NONNULL uri_prefix, + SerdChunk* SERD_NONNULL uri_suffix); + +/** + Expand `node`, which must be a CURIE or URI, to a full URI. + + Returns null if `node` can not be expanded. +*/ +SERD_API +SerdNode +serd_env_expand_node(const SerdEnv* SERD_NONNULL env, + const SerdNode* SERD_NONNULL node); + +/// Call `func` for each prefix defined in `env` +SERD_API +void +serd_env_foreach(const SerdEnv* SERD_NONNULL env, + SerdPrefixSink SERD_NONNULL func, + void* SERD_NULLABLE handle); + +/** + @} + @defgroup serd_reader Reader + @{ +*/ + +/// Create a new RDF reader +SERD_API +SerdReader* SERD_ALLOCATED +serd_reader_new(SerdSyntax syntax, + void* SERD_NULLABLE handle, + void (*SERD_NULLABLE free_handle)(void* SERD_NULLABLE), + SerdBaseSink SERD_NULLABLE base_sink, + SerdPrefixSink SERD_NULLABLE prefix_sink, + SerdStatementSink SERD_NULLABLE statement_sink, + SerdEndSink SERD_NULLABLE end_sink); + +/** + Enable or disable strict parsing + + The reader is non-strict (lax) by default, which will tolerate URIs with + invalid characters. Setting strict will fail when parsing such files. An + error is printed for invalid input in either case. +*/ +SERD_API +void +serd_reader_set_strict(SerdReader* SERD_NONNULL reader, bool strict); + +/** + Set a function to be called when errors occur during reading. + + The `error_sink` will be called with `handle` as its first argument. If + no error function is set, errors are printed to stderr in GCC style. +*/ +SERD_API +void +serd_reader_set_error_sink(SerdReader* SERD_NONNULL reader, + SerdErrorSink SERD_NULLABLE error_sink, + void* SERD_NULLABLE error_handle); + +/// Return the `handle` passed to serd_reader_new() +SERD_PURE_API +void* SERD_NULLABLE +serd_reader_get_handle(const SerdReader* SERD_NONNULL reader); + +/** + Set a prefix to be added to all blank node identifiers. + + This is useful when multiple files are to be parsed into the same output (a + model or a file). Since Serd preserves blank node IDs, this could cause + conflicts where two non-equivalent blank nodes are merged, resulting in + corrupt data. By setting a unique blank node prefix for each parsed file, + this can be avoided, while preserving blank node names. +*/ +SERD_API +void +serd_reader_add_blank_prefix(SerdReader* SERD_NONNULL reader, + const uint8_t* SERD_NULLABLE prefix); + +/** + Set the URI of the default graph. + + If this is set, the reader will emit quads with the graph set to the given + node for any statements that are not in a named graph (which is currently + all of them since Serd currently does not support any graph syntaxes). +*/ +SERD_API +void +serd_reader_set_default_graph(SerdReader* SERD_NONNULL reader, + const SerdNode* SERD_NULLABLE graph); + +/// Read a file at a given `uri` +SERD_API +SerdStatus +serd_reader_read_file(SerdReader* SERD_NONNULL reader, + const uint8_t* SERD_NONNULL uri); + +/** + Start an incremental read from a file handle. + + Iff `bulk` is true, `file` will be read a page at a time. This is more + efficient, but uses a page of memory and means that an entire page of input + must be ready before any callbacks will fire. To react as soon as input + arrives, set `bulk` to false. +*/ +SERD_API +SerdStatus +serd_reader_start_stream(SerdReader* SERD_NONNULL reader, + FILE* SERD_NONNULL file, + const uint8_t* SERD_NULLABLE name, + bool bulk); + +/** + Start an incremental read from a user-specified source. + + The `read_func` is guaranteed to only be called for `page_size` elements + with size 1 (i.e. `page_size` bytes). +*/ +SERD_API +SerdStatus +serd_reader_start_source_stream(SerdReader* SERD_NONNULL reader, + SerdSource SERD_NONNULL read_func, + SerdStreamErrorFunc SERD_NONNULL error_func, + void* SERD_NONNULL stream, + const uint8_t* SERD_NULLABLE name, + size_t page_size); + +/** + Read a single "chunk" of data during an incremental read + + This function will read a single top level description, and return. This + may be a directive, statement, or several statements; essentially it reads + until a '.' is encountered. This is particularly useful for reading + directly from a pipe or socket. +*/ +SERD_API +SerdStatus +serd_reader_read_chunk(SerdReader* SERD_NONNULL reader); + +/// Finish an incremental read from a file handle +SERD_API +SerdStatus +serd_reader_end_stream(SerdReader* SERD_NONNULL reader); + +/// Read `file` +SERD_API +SerdStatus +serd_reader_read_file_handle(SerdReader* SERD_NONNULL reader, + FILE* SERD_NONNULL file, + const uint8_t* SERD_NULLABLE name); + +/// Read a user-specified byte source +SERD_API +SerdStatus +serd_reader_read_source(SerdReader* SERD_NONNULL reader, + SerdSource SERD_NONNULL source, + SerdStreamErrorFunc SERD_NONNULL error, + void* SERD_NONNULL stream, + const uint8_t* SERD_NULLABLE name, + size_t page_size); + +/// Read `utf8` +SERD_API +SerdStatus +serd_reader_read_string(SerdReader* SERD_NONNULL reader, + const uint8_t* SERD_NONNULL utf8); + +/// Free `reader` +SERD_API +void +serd_reader_free(SerdReader* SERD_NULLABLE reader); + +/** + @} + @defgroup serd_writer Writer + @{ +*/ + +/// Create a new RDF writer +SERD_API +SerdWriter* SERD_ALLOCATED +serd_writer_new(SerdSyntax syntax, + SerdStyle style, + SerdEnv* SERD_NONNULL env, + const SerdURI* SERD_NULLABLE base_uri, + SerdSink SERD_NONNULL ssink, + void* SERD_NULLABLE stream); + +/// Free `writer` +SERD_API +void +serd_writer_free(SerdWriter* SERD_NULLABLE writer); + +/// Return the env used by `writer` +SERD_PURE_API +SerdEnv* SERD_NONNULL +serd_writer_get_env(SerdWriter* SERD_NONNULL writer); + +/** + A convenience sink function for writing to a FILE*. + + This function can be used as a SerdSink when writing to a FILE*. The + `stream` parameter must be a FILE* opened for writing. +*/ +SERD_API +size_t +serd_file_sink(const void* SERD_NONNULL buf, + size_t len, + void* SERD_NONNULL stream); + +/** + A convenience sink function for writing to a string + + This function can be used as a SerdSink to write to a SerdChunk which is + resized as necessary with realloc(). The `stream` parameter must point to + an initialized SerdChunk. When the write is finished, the string should be + retrieved with serd_chunk_sink_finish(). +*/ +SERD_API +size_t +serd_chunk_sink(const void* SERD_NONNULL buf, + size_t len, + void* SERD_NONNULL stream); + +/** + Finish a serialisation to a chunk with serd_chunk_sink(). + + The returned string is the result of the serialisation, which is null + terminated (by this function) and owned by the caller. +*/ +SERD_API +uint8_t* SERD_NULLABLE +serd_chunk_sink_finish(SerdChunk* SERD_NONNULL stream); + +/** + Set a function to be called when errors occur during writing. + + The `error_sink` will be called with `handle` as its first argument. If + no error function is set, errors are printed to stderr. +*/ +SERD_API +void +serd_writer_set_error_sink(SerdWriter* SERD_NONNULL writer, + SerdErrorSink SERD_NONNULL error_sink, + void* SERD_NULLABLE error_handle); + +/** + Set a prefix to be removed from matching blank node identifiers + + This is the counterpart to serd_reader_add_blank_prefix() which can be used + to "undo" added prefixes. +*/ +SERD_API +void +serd_writer_chop_blank_prefix(SerdWriter* SERD_NONNULL writer, + const uint8_t* SERD_NULLABLE prefix); + +/** + Set the current output base URI, and emit a directive if applicable. + + Note this function can be safely casted to SerdBaseSink. +*/ +SERD_API +SerdStatus +serd_writer_set_base_uri(SerdWriter* SERD_NONNULL writer, + const SerdNode* SERD_NULLABLE uri); + +/** + Set the current root URI. + + The root URI should be a prefix of the base URI. The path of the root URI + is the highest path any relative up-reference can refer to. For example, + with root and base , + will be written as <../>, but will be + written non-relatively as . If the root is not explicitly set, + it defaults to the base URI, so no up-references will be created at all. +*/ +SERD_API +SerdStatus +serd_writer_set_root_uri(SerdWriter* SERD_NONNULL writer, + const SerdNode* SERD_NULLABLE uri); + +/** + Set a namespace prefix (and emit directive if applicable). + + Note this function can be safely casted to SerdPrefixSink. +*/ +SERD_API +SerdStatus +serd_writer_set_prefix(SerdWriter* SERD_NONNULL writer, + const SerdNode* SERD_NONNULL name, + const SerdNode* SERD_NONNULL uri); + +/** + Write a statement. + + Note this function can be safely casted to SerdStatementSink. +*/ +SERD_API +SerdStatus +serd_writer_write_statement(SerdWriter* SERD_NONNULL writer, + SerdStatementFlags flags, + const SerdNode* SERD_NULLABLE graph, + const SerdNode* SERD_NONNULL subject, + const SerdNode* SERD_NONNULL predicate, + const SerdNode* SERD_NONNULL object, + const SerdNode* SERD_NULLABLE datatype, + const SerdNode* SERD_NULLABLE lang); + +/** + Mark the end of an anonymous node's description. + + Note this function can be safely casted to SerdEndSink. +*/ +SERD_API +SerdStatus +serd_writer_end_anon(SerdWriter* SERD_NONNULL writer, + const SerdNode* SERD_NULLABLE node); + +/** + Finish a write + + This flushes any pending output, for example terminating punctuation, so + that the output is a complete document. +*/ +SERD_API +SerdStatus +serd_writer_finish(SerdWriter* SERD_NONNULL writer); + +/** + @} + @} +*/ + +#ifdef __cplusplus +# if defined(__GNUC__) +# pragma GCC diagnostic pop +# endif +} /* extern "C" */ +#endif + +#endif /* SERD_SERD_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/serd/src/.clang-tidy b/modules/juce_audio_processors/format_types/lv2/serd/src/.clang-tidy new file mode 100644 index 0000000000..5be374708e --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/serd/src/.clang-tidy @@ -0,0 +1,18 @@ +Checks: > + *, + -*-magic-numbers, + -*-uppercase-literal-suffix, + -bugprone-branch-clone, + -bugprone-reserved-identifier, + -bugprone-suspicious-string-compare, + -cert-dcl37-c, + -cert-dcl51-cpp, + -clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling, + -google-readability-todo, + -hicpp-signed-bitwise, + -llvm-header-guard, + -llvmlibc-*, + -misc-no-recursion, +WarningsAsErrors: '*' +HeaderFilterRegex: '.*' +FormatStyle: file diff --git a/modules/juce_audio_processors/format_types/lv2/serd/src/attributes.h b/modules/juce_audio_processors/format_types/lv2/serd/src/attributes.h new file mode 100644 index 0000000000..8628a86863 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/serd/src/attributes.h @@ -0,0 +1,26 @@ +/* + Copyright 2019-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef SERD_ATTRIBUTES_H +#define SERD_ATTRIBUTES_H + +#ifdef __GNUC__ +# define SERD_MALLOC_FUNC __attribute__((malloc)) +#else +# define SERD_MALLOC_FUNC +#endif + +#endif // SERD_ATTRIBUTES_H diff --git a/modules/juce_audio_processors/format_types/lv2/serd/src/base64.c b/modules/juce_audio_processors/format_types/lv2/serd/src/base64.c new file mode 100644 index 0000000000..bca3dfd379 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/serd/src/base64.c @@ -0,0 +1,132 @@ +/* + Copyright 2011-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "base64.h" + +#include "serd_internal.h" +#include "string_utils.h" + +#include "serd/serd.h" + +#include +#include +#include +#include + +/** + Base64 encoding table. + + @see RFC3548 S3. +*/ +static const uint8_t b64_map[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +/** + Base64 decoding table. + + This is indexed by encoded characters and returns the numeric value used + for decoding, shifted up by 47 to be in the range of printable ASCII. + A '$' is a placeholder for characters not in the base64 alphabet. +*/ +static const char b64_unmap[] = + "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$m$$$ncdefghijkl$$$$$$" + "$/0123456789:;<=>?@ABCDEFGH$$$$$$IJKLMNOPQRSTUVWXYZ[\\]^_`ab$$$$" + "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" + "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"; + +/** Encode 3 raw bytes to 4 base64 characters. */ +static inline void +encode_chunk(uint8_t out[4], const uint8_t in[3], size_t n_in) +{ + out[0] = b64_map[in[0] >> 2]; + out[1] = b64_map[((in[0] & 0x03) << 4) | ((in[1] & 0xF0) >> 4)]; + + out[2] = (n_in > 1) ? (b64_map[((in[1] & 0x0F) << 2) | ((in[2] & 0xC0) >> 6)]) + : (uint8_t)'='; + + out[3] = ((n_in > 2) ? b64_map[in[2] & 0x3F] : (uint8_t)'='); +} + +size_t +serd_base64_get_length(const size_t size, const bool wrap_lines) +{ + return (size + 2) / 3 * 4 + (wrap_lines * ((size - 1) / 57)); +} + +bool +serd_base64_encode(uint8_t* const str, + const void* const buf, + const size_t size, + const bool wrap_lines) +{ + bool has_newline = false; + + for (size_t i = 0, j = 0; i < size; i += 3, j += 4) { + uint8_t in[4] = {0, 0, 0, 0}; + size_t n_in = MIN(3, size - i); + memcpy(in, (const uint8_t*)buf + i, n_in); + + if (wrap_lines && i > 0 && (i % 57) == 0) { + str[j++] = '\n'; + has_newline = true; + } + + encode_chunk(str + j, in, n_in); + } + + return has_newline; +} + +static inline uint8_t +unmap(const uint8_t in) +{ + return (uint8_t)(b64_unmap[in] - 47); +} + +/** Decode 4 base64 characters to 3 raw bytes. */ +static inline size_t +decode_chunk(const uint8_t in[4], uint8_t out[3]) +{ + out[0] = (uint8_t)(((unmap(in[0]) << 2)) | unmap(in[1]) >> 4); + out[1] = (uint8_t)(((unmap(in[1]) << 4) & 0xF0) | unmap(in[2]) >> 2); + out[2] = (uint8_t)(((unmap(in[2]) << 6) & 0xC0) | unmap(in[3])); + return 1 + (in[2] != '=') + ((in[2] != '=') && (in[3] != '=')); +} + +void* +serd_base64_decode(const uint8_t* str, size_t len, size_t* size) +{ + void* buf = malloc((len * 3) / 4 + 2); + + *size = 0; + for (size_t i = 0, j = 0; i < len; j += 3) { + uint8_t in[] = "===="; + size_t n_in = 0; + for (; i < len && n_in < 4; ++n_in) { + for (; i < len && !is_base64(str[i]); ++i) { + // Skip junk + } + + in[n_in] = str[i++]; + } + + if (n_in > 1) { + *size += decode_chunk(in, (uint8_t*)buf + j); + } + } + + return buf; +} diff --git a/modules/juce_audio_processors/format_types/lv2/serd/src/base64.h b/modules/juce_audio_processors/format_types/lv2/serd/src/base64.h new file mode 100644 index 0000000000..8ff6acc492 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/serd/src/base64.h @@ -0,0 +1,48 @@ +/* + Copyright 2011-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef SERD_BASE64_H +#define SERD_BASE64_H + +#include "serd/serd.h" + +#include +#include +#include + +/** + Return the number of bytes required to encode `size` bytes in base64. + + @param size The number of input (binary) bytes to encode. + @param wrap_lines Wrap lines at 76 characters to conform to RFC 2045. + @return The length of the base64 encoding, excluding null terminator. +*/ +SERD_CONST_FUNC size_t +serd_base64_get_length(size_t size, bool wrap_lines); + +/** + Encode `size` bytes of `buf` into `str`, which must be large enough. + + @param str Output string buffer. + @param buf Input binary data. + @param size Number of bytes to encode from `buf`. + @param wrap_lines Wrap lines at 76 characters to conform to RFC 2045. + @return True iff `str` contains newlines. +*/ +bool +serd_base64_encode(uint8_t* str, const void* buf, size_t size, bool wrap_lines); + +#endif // SERD_BASE64_H diff --git a/modules/juce_audio_processors/format_types/lv2/serd/src/byte_sink.h b/modules/juce_audio_processors/format_types/lv2/serd/src/byte_sink.h new file mode 100644 index 0000000000..dc55ba1e12 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/serd/src/byte_sink.h @@ -0,0 +1,98 @@ +/* + Copyright 2011-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef SERD_BYTE_SINK_H +#define SERD_BYTE_SINK_H + +#include "serd_internal.h" +#include "system.h" + +#include "serd/serd.h" + +#include +#include +#include + +typedef struct SerdByteSinkImpl { + SerdSink sink; + void* stream; + uint8_t* buf; + size_t size; + size_t block_size; +} SerdByteSink; + +static inline SerdByteSink +serd_byte_sink_new(SerdSink sink, void* stream, size_t block_size) +{ + SerdByteSink bsink = {sink, stream, NULL, 0, block_size}; + + if (block_size > 1) { + bsink.buf = (uint8_t*)serd_allocate_buffer(block_size); + } + + return bsink; +} + +static inline void +serd_byte_sink_flush(SerdByteSink* bsink) +{ + if (bsink->block_size > 1 && bsink->size > 0) { + bsink->sink(bsink->buf, bsink->size, bsink->stream); + bsink->size = 0; + } +} + +static inline void +serd_byte_sink_free(SerdByteSink* bsink) +{ + serd_byte_sink_flush(bsink); + serd_free_aligned(bsink->buf); + bsink->buf = NULL; +} + +static inline size_t +serd_byte_sink_write(const void* buf, size_t len, SerdByteSink* bsink) +{ + if (len == 0) { + return 0; + } + + if (bsink->block_size == 1) { + return bsink->sink(buf, len, bsink->stream); + } + + const size_t orig_len = len; + while (len) { + const size_t space = bsink->block_size - bsink->size; + const size_t n = MIN(space, len); + + // Write as much as possible into the remaining buffer space + memcpy(bsink->buf + bsink->size, buf, n); + bsink->size += n; + buf = (const uint8_t*)buf + n; + len -= n; + + // Flush page if buffer is full + if (bsink->size == bsink->block_size) { + bsink->sink(bsink->buf, bsink->block_size, bsink->stream); + bsink->size = 0; + } + } + + return orig_len; +} + +#endif // SERD_BYTE_SINK_H diff --git a/modules/juce_audio_processors/format_types/lv2/serd/src/byte_source.c b/modules/juce_audio_processors/format_types/lv2/serd/src/byte_source.c new file mode 100644 index 0000000000..9f7e4e59d8 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/serd/src/byte_source.c @@ -0,0 +1,112 @@ +/* + Copyright 2011-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "byte_source.h" + +#include "system.h" + +#include "serd/serd.h" + +#include +#include +#include + +SerdStatus +serd_byte_source_page(SerdByteSource* source) +{ + source->read_head = 0; + const size_t n_read = + source->read_func(source->file_buf, 1, source->page_size, source->stream); + + if (n_read == 0) { + source->file_buf[0] = '\0'; + source->eof = true; + return (source->error_func(source->stream) ? SERD_ERR_UNKNOWN + : SERD_FAILURE); + } + + if (n_read < source->page_size) { + source->file_buf[n_read] = '\0'; + source->buf_size = n_read; + } + + return SERD_SUCCESS; +} + +SerdStatus +serd_byte_source_open_source(SerdByteSource* source, + SerdSource read_func, + SerdStreamErrorFunc error_func, + void* stream, + const uint8_t* name, + size_t page_size) +{ + const Cursor cur = {name, 1, 1}; + + memset(source, '\0', sizeof(*source)); + source->stream = stream; + source->from_stream = true; + source->page_size = page_size; + source->buf_size = page_size; + source->cur = cur; + source->error_func = error_func; + source->read_func = read_func; + + if (page_size > 1) { + source->file_buf = (uint8_t*)serd_allocate_buffer(page_size); + source->read_buf = source->file_buf; + memset(source->file_buf, '\0', page_size); + } else { + source->read_buf = &source->read_byte; + } + + return SERD_SUCCESS; +} + +SerdStatus +serd_byte_source_prepare(SerdByteSource* source) +{ + source->prepared = true; + + if (source->from_stream) { + return (source->page_size > 1 ? serd_byte_source_page(source) + : serd_byte_source_advance(source)); + } + + return SERD_SUCCESS; +} + +SerdStatus +serd_byte_source_open_string(SerdByteSource* source, const uint8_t* utf8) +{ + const Cursor cur = {(const uint8_t*)"(string)", 1, 1}; + + memset(source, '\0', sizeof(*source)); + source->cur = cur; + source->read_buf = utf8; + return SERD_SUCCESS; +} + +SerdStatus +serd_byte_source_close(SerdByteSource* source) +{ + if (source->page_size > 1) { + serd_free_aligned(source->file_buf); + } + + memset(source, '\0', sizeof(*source)); + return SERD_SUCCESS; +} diff --git a/modules/juce_audio_processors/format_types/lv2/serd/src/byte_source.h b/modules/juce_audio_processors/format_types/lv2/serd/src/byte_source.h new file mode 100644 index 0000000000..48794a3763 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/serd/src/byte_source.h @@ -0,0 +1,120 @@ +/* + Copyright 2011-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef SERD_BYTE_SOURCE_H +#define SERD_BYTE_SOURCE_H + +#include "serd/serd.h" + +#include +#include +#include +#include +#include + +typedef struct { + const uint8_t* filename; + unsigned line; + unsigned col; +} Cursor; + +typedef struct { + SerdSource read_func; ///< Read function (e.g. fread) + SerdStreamErrorFunc error_func; ///< Error function (e.g. ferror) + void* stream; ///< Stream (e.g. FILE) + size_t page_size; ///< Number of bytes to read at a time + size_t buf_size; ///< Number of bytes in file_buf + Cursor cur; ///< Cursor for error reporting + uint8_t* file_buf; ///< Buffer iff reading pages from a file + const uint8_t* read_buf; ///< Pointer to file_buf or read_byte + size_t read_head; ///< Offset into read_buf + uint8_t read_byte; ///< 1-byte 'buffer' used when not paging + bool from_stream; ///< True iff reading from `stream` + bool prepared; ///< True iff prepared for reading + bool eof; ///< True iff end of file reached +} SerdByteSource; + +SerdStatus +serd_byte_source_open_file(SerdByteSource* source, FILE* file, bool bulk); + +SerdStatus +serd_byte_source_open_string(SerdByteSource* source, const uint8_t* utf8); + +SerdStatus +serd_byte_source_open_source(SerdByteSource* source, + SerdSource read_func, + SerdStreamErrorFunc error_func, + void* stream, + const uint8_t* name, + size_t page_size); + +SerdStatus +serd_byte_source_close(SerdByteSource* source); + +SerdStatus +serd_byte_source_prepare(SerdByteSource* source); + +SerdStatus +serd_byte_source_page(SerdByteSource* source); + +static inline SERD_PURE_FUNC uint8_t +serd_byte_source_peek(SerdByteSource* source) +{ + assert(source->prepared); + return source->read_buf[source->read_head]; +} + +static inline SerdStatus +serd_byte_source_advance(SerdByteSource* source) +{ + SerdStatus st = SERD_SUCCESS; + + switch (serd_byte_source_peek(source)) { + case '\n': + ++source->cur.line; + source->cur.col = 0; + break; + default: + ++source->cur.col; + } + + const bool was_eof = source->eof; + if (source->from_stream) { + source->eof = false; + if (source->page_size > 1) { + if (++source->read_head == source->page_size) { + st = serd_byte_source_page(source); + } else if (source->read_head == source->buf_size) { + source->eof = true; + } + } else { + if (!source->read_func(&source->read_byte, 1, 1, source->stream)) { + source->eof = true; + st = + source->error_func(source->stream) ? SERD_ERR_UNKNOWN : SERD_FAILURE; + } + } + } else if (!source->eof) { + ++source->read_head; // Move to next character in string + if (source->read_buf[source->read_head] == '\0') { + source->eof = true; + } + } + + return (was_eof && source->eof) ? SERD_FAILURE : st; +} + +#endif // SERD_BYTE_SOURCE_H diff --git a/modules/juce_audio_processors/format_types/lv2/serd/src/env.c b/modules/juce_audio_processors/format_types/lv2/serd/src/env.c new file mode 100644 index 0000000000..67fa9caffb --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/serd/src/env.c @@ -0,0 +1,256 @@ +/* + Copyright 2011-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "serd/serd.h" + +#include +#include +#include +#include +#include + +typedef struct { + SerdNode name; + SerdNode uri; +} SerdPrefix; + +struct SerdEnvImpl { + SerdPrefix* prefixes; + size_t n_prefixes; + SerdNode base_uri_node; + SerdURI base_uri; +}; + +SerdEnv* +serd_env_new(const SerdNode* base_uri) +{ + SerdEnv* env = (SerdEnv*)calloc(1, sizeof(struct SerdEnvImpl)); + if (env && base_uri) { + serd_env_set_base_uri(env, base_uri); + } + + return env; +} + +void +serd_env_free(SerdEnv* env) +{ + if (!env) { + return; + } + + for (size_t i = 0; i < env->n_prefixes; ++i) { + serd_node_free(&env->prefixes[i].name); + serd_node_free(&env->prefixes[i].uri); + } + + free(env->prefixes); + serd_node_free(&env->base_uri_node); + free(env); +} + +const SerdNode* +serd_env_get_base_uri(const SerdEnv* env, SerdURI* out) +{ + if (out) { + *out = env->base_uri; + } + + return &env->base_uri_node; +} + +SerdStatus +serd_env_set_base_uri(SerdEnv* env, const SerdNode* uri) +{ + if (!env || (uri && uri->type != SERD_URI)) { + return SERD_ERR_BAD_ARG; + } + + if (!uri || !uri->buf) { + serd_node_free(&env->base_uri_node); + env->base_uri_node = SERD_NODE_NULL; + env->base_uri = SERD_URI_NULL; + return SERD_SUCCESS; + } + + // Resolve base URI and create a new node and URI for it + SerdURI base_uri; + SerdNode base_uri_node = + serd_node_new_uri_from_node(uri, &env->base_uri, &base_uri); + + // Replace the current base URI + serd_node_free(&env->base_uri_node); + env->base_uri_node = base_uri_node; + env->base_uri = base_uri; + + return SERD_SUCCESS; +} + +static inline SERD_PURE_FUNC SerdPrefix* +serd_env_find(const SerdEnv* env, const uint8_t* name, size_t name_len) +{ + for (size_t i = 0; i < env->n_prefixes; ++i) { + const SerdNode* const prefix_name = &env->prefixes[i].name; + if (prefix_name->n_bytes == name_len) { + if (!memcmp(prefix_name->buf, name, name_len)) { + return &env->prefixes[i]; + } + } + } + + return NULL; +} + +static void +serd_env_add(SerdEnv* env, const SerdNode* name, const SerdNode* uri) +{ + SerdPrefix* const prefix = serd_env_find(env, name->buf, name->n_bytes); + if (prefix) { + if (!serd_node_equals(&prefix->uri, uri)) { + SerdNode old_prefix_uri = prefix->uri; + prefix->uri = serd_node_copy(uri); + serd_node_free(&old_prefix_uri); + } + } else { + env->prefixes = (SerdPrefix*)realloc( + env->prefixes, (++env->n_prefixes) * sizeof(SerdPrefix)); + env->prefixes[env->n_prefixes - 1].name = serd_node_copy(name); + env->prefixes[env->n_prefixes - 1].uri = serd_node_copy(uri); + } +} + +SerdStatus +serd_env_set_prefix(SerdEnv* env, const SerdNode* name, const SerdNode* uri) +{ + if (!name->buf || uri->type != SERD_URI) { + return SERD_ERR_BAD_ARG; + } + + if (serd_uri_string_has_scheme(uri->buf)) { + // Set prefix to absolute URI + serd_env_add(env, name, uri); + } else { + // Resolve relative URI and create a new node and URI for it + SerdURI abs_uri; + SerdNode abs_uri_node = + serd_node_new_uri_from_node(uri, &env->base_uri, &abs_uri); + + // Set prefix to resolved (absolute) URI + serd_env_add(env, name, &abs_uri_node); + serd_node_free(&abs_uri_node); + } + + return SERD_SUCCESS; +} + +SerdStatus +serd_env_set_prefix_from_strings(SerdEnv* env, + const uint8_t* name, + const uint8_t* uri) +{ + const SerdNode name_node = serd_node_from_string(SERD_LITERAL, name); + const SerdNode uri_node = serd_node_from_string(SERD_URI, uri); + + return serd_env_set_prefix(env, &name_node, &uri_node); +} + +bool +serd_env_qualify(const SerdEnv* env, + const SerdNode* uri, + SerdNode* prefix, + SerdChunk* suffix) +{ + for (size_t i = 0; i < env->n_prefixes; ++i) { + const SerdNode* const prefix_uri = &env->prefixes[i].uri; + if (uri->n_bytes >= prefix_uri->n_bytes) { + if (!strncmp((const char*)uri->buf, + (const char*)prefix_uri->buf, + prefix_uri->n_bytes)) { + *prefix = env->prefixes[i].name; + suffix->buf = uri->buf + prefix_uri->n_bytes; + suffix->len = uri->n_bytes - prefix_uri->n_bytes; + return true; + } + } + } + return false; +} + +SerdStatus +serd_env_expand(const SerdEnv* env, + const SerdNode* curie, + SerdChunk* uri_prefix, + SerdChunk* uri_suffix) +{ + const uint8_t* const colon = + (const uint8_t*)memchr(curie->buf, ':', curie->n_bytes + 1); + if (curie->type != SERD_CURIE || !colon) { + return SERD_ERR_BAD_ARG; + } + + const size_t name_len = (size_t)(colon - curie->buf); + const SerdPrefix* const prefix = serd_env_find(env, curie->buf, name_len); + if (prefix) { + uri_prefix->buf = prefix->uri.buf; + uri_prefix->len = prefix->uri.n_bytes; + uri_suffix->buf = colon + 1; + uri_suffix->len = curie->n_bytes - name_len - 1; + return SERD_SUCCESS; + } + return SERD_ERR_BAD_CURIE; +} + +SerdNode +serd_env_expand_node(const SerdEnv* env, const SerdNode* node) +{ + switch (node->type) { + case SERD_NOTHING: + case SERD_LITERAL: + break; + + case SERD_URI: { + SerdURI ignored; + return serd_node_new_uri_from_node(node, &env->base_uri, &ignored); + } + + case SERD_CURIE: { + SerdChunk prefix; + SerdChunk suffix; + if (serd_env_expand(env, node, &prefix, &suffix)) { + return SERD_NODE_NULL; + } + const size_t len = prefix.len + suffix.len; + uint8_t* buf = (uint8_t*)malloc(len + 1); + SerdNode ret = {buf, len, 0, 0, SERD_URI}; + snprintf((char*)buf, len + 1, "%s%s", prefix.buf, suffix.buf); + ret.n_chars = serd_strlen(buf, NULL, NULL); + return ret; + } + + case SERD_BLANK: + break; + } + + return SERD_NODE_NULL; +} + +void +serd_env_foreach(const SerdEnv* env, SerdPrefixSink func, void* handle) +{ + for (size_t i = 0; i < env->n_prefixes; ++i) { + func(handle, &env->prefixes[i].name, &env->prefixes[i].uri); + } +} diff --git a/modules/juce_audio_processors/format_types/lv2/serd/src/n3.c b/modules/juce_audio_processors/format_types/lv2/serd/src/n3.c new file mode 100644 index 0000000000..9aae8a19d2 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/serd/src/n3.c @@ -0,0 +1,1720 @@ +/* + Copyright 2011-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "byte_source.h" +#include "reader.h" +#include "serd_internal.h" +#include "stack.h" +#include "string_utils.h" +#include "uri_utils.h" + +#include "serd/serd.h" + +#include +#include +#include +#include +#include +#include + +#define TRY(st, exp) \ + do { \ + if (((st) = (exp))) { \ + return (st); \ + } \ + } while (0) + +static inline bool +fancy_syntax(const SerdReader* reader) +{ + return reader->syntax == SERD_TURTLE || reader->syntax == SERD_TRIG; +} + +static SerdStatus +read_collection(SerdReader* reader, ReadContext ctx, Ref* dest); + +static SerdStatus +read_predicateObjectList(SerdReader* reader, ReadContext ctx, bool* ate_dot); + +static inline uint8_t +read_HEX(SerdReader* reader) +{ + const int c = peek_byte(reader); + if (is_xdigit(c)) { + return (uint8_t)eat_byte_safe(reader, c); + } + + r_err(reader, SERD_ERR_BAD_SYNTAX, "invalid hexadecimal digit `%c'\n", c); + return 0; +} + +// Read UCHAR escape, initial \ is already eaten by caller +static inline SerdStatus +read_UCHAR(SerdReader* reader, Ref dest, uint32_t* char_code) +{ + const int b = peek_byte(reader); + unsigned length = 0; + switch (b) { + case 'U': + length = 8; + break; + case 'u': + length = 4; + break; + default: + return SERD_ERR_BAD_SYNTAX; + } + + eat_byte_safe(reader, b); + + uint8_t buf[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; + for (unsigned i = 0; i < length; ++i) { + if (!(buf[i] = read_HEX(reader))) { + return SERD_ERR_BAD_SYNTAX; + } + } + + char* endptr = NULL; + const uint32_t code = (uint32_t)strtoul((const char*)buf, &endptr, 16); + assert(endptr == (char*)buf + length); + + unsigned size = 0; + if (code < 0x00000080) { + size = 1; + } else if (code < 0x00000800) { + size = 2; + } else if (code < 0x00010000) { + size = 3; + } else if (code < 0x00110000) { + size = 4; + } else { + r_err(reader, + SERD_ERR_BAD_SYNTAX, + "unicode character 0x%X out of range\n", + code); + push_bytes(reader, dest, replacement_char, 3); + *char_code = 0xFFFD; + return SERD_SUCCESS; + } + + // Build output in buf + // (Note # of bytes = # of leading 1 bits in first byte) + uint32_t c = code; + switch (size) { + case 4: + buf[3] = (uint8_t)(0x80u | (c & 0x3Fu)); + c >>= 6; + c |= (16 << 12); // set bit 4 + /* fallthru */ + case 3: + buf[2] = (uint8_t)(0x80u | (c & 0x3Fu)); + c >>= 6; + c |= (32 << 6); // set bit 5 + /* fallthru */ + case 2: + buf[1] = (uint8_t)(0x80u | (c & 0x3Fu)); + c >>= 6; + c |= 0xC0; // set bits 6 and 7 + /* fallthru */ + case 1: + buf[0] = (uint8_t)c; + /* fallthru */ + default: + break; + } + + push_bytes(reader, dest, buf, size); + *char_code = code; + return SERD_SUCCESS; +} + +// Read ECHAR escape, initial \ is already eaten by caller +static inline SerdStatus +read_ECHAR(SerdReader* reader, Ref dest, SerdNodeFlags* flags) +{ + const int c = peek_byte(reader); + switch (c) { + case 't': + eat_byte_safe(reader, 't'); + push_byte(reader, dest, '\t'); + return SERD_SUCCESS; + case 'b': + eat_byte_safe(reader, 'b'); + push_byte(reader, dest, '\b'); + return SERD_SUCCESS; + case 'n': + *flags |= SERD_HAS_NEWLINE; + eat_byte_safe(reader, 'n'); + push_byte(reader, dest, '\n'); + return SERD_SUCCESS; + case 'r': + *flags |= SERD_HAS_NEWLINE; + eat_byte_safe(reader, 'r'); + push_byte(reader, dest, '\r'); + return SERD_SUCCESS; + case 'f': + eat_byte_safe(reader, 'f'); + push_byte(reader, dest, '\f'); + return SERD_SUCCESS; + case '\\': + case '"': + case '\'': + push_byte(reader, dest, eat_byte_safe(reader, c)); + return SERD_SUCCESS; + default: + return SERD_ERR_BAD_SYNTAX; + } +} + +static inline SerdStatus +bad_char(SerdReader* reader, const char* fmt, uint8_t c) +{ + // Skip bytes until the next start byte + for (int b = peek_byte(reader); b != EOF && ((uint8_t)b & 0x80);) { + eat_byte_safe(reader, b); + b = peek_byte(reader); + } + + r_err(reader, SERD_ERR_BAD_SYNTAX, fmt, c); + return reader->strict ? SERD_ERR_BAD_SYNTAX : SERD_FAILURE; +} + +static SerdStatus +read_utf8_bytes(SerdReader* reader, uint8_t bytes[4], uint32_t* size, uint8_t c) +{ + *size = utf8_num_bytes(c); + if (*size <= 1 || *size > 4) { + return bad_char(reader, "invalid UTF-8 start 0x%X\n", c); + } + + bytes[0] = c; + for (unsigned i = 1; i < *size; ++i) { + const int b = peek_byte(reader); + if (b == EOF || ((uint8_t)b & 0x80) == 0) { + return bad_char(reader, "invalid UTF-8 continuation 0x%X\n", (uint8_t)b); + } + + eat_byte_safe(reader, b); + bytes[i] = (uint8_t)b; + } + + return SERD_SUCCESS; +} + +static SerdStatus +read_utf8_character(SerdReader* reader, Ref dest, uint8_t c) +{ + uint32_t size = 0; + uint8_t bytes[4] = {0, 0, 0, 0}; + SerdStatus st = read_utf8_bytes(reader, bytes, &size, c); + if (st) { + push_bytes(reader, dest, replacement_char, 3); + } else { + push_bytes(reader, dest, bytes, size); + } + + return st; +} + +static SerdStatus +read_utf8_code(SerdReader* reader, Ref dest, uint32_t* code, uint8_t c) +{ + uint32_t size = 0; + uint8_t bytes[4] = {0, 0, 0, 0}; + SerdStatus st = read_utf8_bytes(reader, bytes, &size, c); + if (st) { + push_bytes(reader, dest, replacement_char, 3); + return st; + } + + push_bytes(reader, dest, bytes, size); + *code = parse_counted_utf8_char(bytes, size); + return st; +} + +// Read one character (possibly multi-byte) +// The first byte, c, has already been eaten by caller +static inline SerdStatus +read_character(SerdReader* reader, Ref dest, SerdNodeFlags* flags, uint8_t c) +{ + if (!(c & 0x80)) { + switch (c) { + case 0xA: + case 0xD: + *flags |= SERD_HAS_NEWLINE; + break; + case '"': + case '\'': + *flags |= SERD_HAS_QUOTE; + break; + default: + break; + } + return push_byte(reader, dest, c); + } + + return read_utf8_character(reader, dest, c); +} + +// [10] comment ::= '#' ( [^#xA #xD] )* +static void +read_comment(SerdReader* reader) +{ + eat_byte_safe(reader, '#'); + int c = 0; + while (((c = peek_byte(reader)) != 0xA) && c != 0xD && c != EOF && c) { + eat_byte_safe(reader, c); + } +} + +// [24] ws ::= #x9 | #xA | #xD | #x20 | comment +static inline bool +read_ws(SerdReader* reader) +{ + const int c = peek_byte(reader); + switch (c) { + case 0x9: + case 0xA: + case 0xD: + case 0x20: + eat_byte_safe(reader, c); + return true; + case '#': + read_comment(reader); + return true; + default: + return false; + } +} + +static inline bool +read_ws_star(SerdReader* reader) +{ + while (read_ws(reader)) { + } + + return true; +} + +static inline bool +peek_delim(SerdReader* reader, const char delim) +{ + read_ws_star(reader); + return peek_byte(reader) == delim; +} + +static inline bool +eat_delim(SerdReader* reader, const char delim) +{ + if (peek_delim(reader, delim)) { + eat_byte_safe(reader, delim); + return read_ws_star(reader); + } + + return false; +} + +// STRING_LITERAL_LONG_QUOTE and STRING_LITERAL_LONG_SINGLE_QUOTE +// Initial triple quotes are already eaten by caller +static SerdStatus +read_STRING_LITERAL_LONG(SerdReader* reader, + Ref ref, + SerdNodeFlags* flags, + uint8_t q) +{ + SerdStatus st = SERD_SUCCESS; + + while (!(st && reader->strict)) { + const int c = peek_byte(reader); + if (c == '\\') { + eat_byte_safe(reader, c); + uint32_t code = 0; + if ((st = read_ECHAR(reader, ref, flags)) && + (st = read_UCHAR(reader, ref, &code))) { + return r_err(reader, st, "invalid escape `\\%c'\n", peek_byte(reader)); + } + } else if (c == q) { + eat_byte_safe(reader, q); + const int q2 = eat_byte_safe(reader, peek_byte(reader)); + const int q3 = peek_byte(reader); + if (q2 == q && q3 == q) { // End of string + eat_byte_safe(reader, q3); + break; + } + *flags |= SERD_HAS_QUOTE; + push_byte(reader, ref, c); + st = read_character(reader, ref, flags, (uint8_t)q2); + } else if (c == EOF) { + return r_err(reader, SERD_ERR_BAD_SYNTAX, "end of file in long string\n"); + } else { + st = + read_character(reader, ref, flags, (uint8_t)eat_byte_safe(reader, c)); + } + } + + return (st && reader->strict) ? st : SERD_SUCCESS; +} + +// STRING_LITERAL_QUOTE and STRING_LITERAL_SINGLE_QUOTE +// Initial quote is already eaten by caller +static SerdStatus +read_STRING_LITERAL(SerdReader* reader, + Ref ref, + SerdNodeFlags* flags, + uint8_t q) +{ + SerdStatus st = SERD_SUCCESS; + + while (!(st && reader->strict)) { + const int c = peek_byte(reader); + uint32_t code = 0; + switch (c) { + case EOF: + return r_err( + reader, SERD_ERR_BAD_SYNTAX, "end of file in short string\n"); + case '\n': + case '\r': + return r_err(reader, SERD_ERR_BAD_SYNTAX, "line end in short string\n"); + case '\\': + eat_byte_safe(reader, c); + if ((st = read_ECHAR(reader, ref, flags)) && + (st = read_UCHAR(reader, ref, &code))) { + return r_err(reader, st, "invalid escape `\\%c'\n", peek_byte(reader)); + } + break; + default: + if (c == q) { + eat_byte_check(reader, q); + return SERD_SUCCESS; + } else { + st = + read_character(reader, ref, flags, (uint8_t)eat_byte_safe(reader, c)); + } + } + } + + return st ? st + : (eat_byte_check(reader, q) ? SERD_SUCCESS : SERD_ERR_BAD_SYNTAX); +} + +static SerdStatus +read_String(SerdReader* reader, Ref node, SerdNodeFlags* flags) +{ + const int q1 = peek_byte(reader); + eat_byte_safe(reader, q1); + + const int q2 = peek_byte(reader); + if (q2 == EOF) { + return r_err(reader, SERD_ERR_BAD_SYNTAX, "unexpected end of file\n"); + } + + if (q2 != q1) { // Short string (not triple quoted) + return read_STRING_LITERAL(reader, node, flags, (uint8_t)q1); + } + + eat_byte_safe(reader, q2); + const int q3 = peek_byte(reader); + if (q3 == EOF) { + return r_err(reader, SERD_ERR_BAD_SYNTAX, "unexpected end of file\n"); + } + + if (q3 != q1) { // Empty short string ("" or '') + return SERD_SUCCESS; + } + + if (!fancy_syntax(reader)) { + return r_err( + reader, SERD_ERR_BAD_SYNTAX, "syntax does not support long literals\n"); + } + + eat_byte_safe(reader, q3); + return read_STRING_LITERAL_LONG(reader, node, flags, (uint8_t)q1); +} + +static inline bool +is_PN_CHARS_BASE(const uint32_t c) +{ + return ((c >= 0x00C0 && c <= 0x00D6) || (c >= 0x00D8 && c <= 0x00F6) || + (c >= 0x00F8 && c <= 0x02FF) || (c >= 0x0370 && c <= 0x037D) || + (c >= 0x037F && c <= 0x1FFF) || (c >= 0x200C && c <= 0x200D) || + (c >= 0x2070 && c <= 0x218F) || (c >= 0x2C00 && c <= 0x2FEF) || + (c >= 0x3001 && c <= 0xD7FF) || (c >= 0xF900 && c <= 0xFDCF) || + (c >= 0xFDF0 && c <= 0xFFFD) || (c >= 0x10000 && c <= 0xEFFFF)); +} + +static SerdStatus +read_PN_CHARS_BASE(SerdReader* reader, Ref dest) +{ + uint32_t code = 0; + const int c = peek_byte(reader); + SerdStatus st = SERD_SUCCESS; + if (is_alpha(c)) { + push_byte(reader, dest, eat_byte_safe(reader, c)); + } else if (c == EOF || !(c & 0x80)) { + return SERD_FAILURE; + } else if ((st = read_utf8_code( + reader, dest, &code, (uint8_t)eat_byte_safe(reader, c)))) { + return st; + } else if (!is_PN_CHARS_BASE(code)) { + r_err( + reader, SERD_ERR_BAD_SYNTAX, "invalid character U+%04X in name\n", code); + if (reader->strict) { + return SERD_ERR_BAD_SYNTAX; + } + } + return st; +} + +static inline bool +is_PN_CHARS(const uint32_t c) +{ + return (is_PN_CHARS_BASE(c) || c == 0xB7 || (c >= 0x0300 && c <= 0x036F) || + (c >= 0x203F && c <= 0x2040)); +} + +static SerdStatus +read_PN_CHARS(SerdReader* reader, Ref dest) +{ + uint32_t code = 0; + const int c = peek_byte(reader); + SerdStatus st = SERD_SUCCESS; + if (is_alpha(c) || is_digit(c) || c == '_' || c == '-') { + push_byte(reader, dest, eat_byte_safe(reader, c)); + } else if (c == EOF || !(c & 0x80)) { + return SERD_FAILURE; + } else if ((st = read_utf8_code( + reader, dest, &code, (uint8_t)eat_byte_safe(reader, c)))) { + return st; + } else if (!is_PN_CHARS(code)) { + return r_err( + reader, SERD_ERR_BAD_SYNTAX, "invalid character U+%04X in name\n", code); + } + return st; +} + +static SerdStatus +read_PERCENT(SerdReader* reader, Ref dest) +{ + push_byte(reader, dest, eat_byte_safe(reader, '%')); + const uint8_t h1 = read_HEX(reader); + const uint8_t h2 = read_HEX(reader); + if (h1 && h2) { + push_byte(reader, dest, h1); + return push_byte(reader, dest, h2); + } + + return SERD_ERR_BAD_SYNTAX; +} + +static SerdStatus +read_PN_LOCAL_ESC(SerdReader* reader, Ref dest) +{ + eat_byte_safe(reader, '\\'); + + const int c = peek_byte(reader); + switch (c) { + case '!': + case '#': + case '$': + case '%': + case '&': + case '\'': + case '(': + case ')': + case '*': + case '+': + case ',': + case '-': + case '.': + case '/': + case ';': + case '=': + case '?': + case '@': + case '_': + case '~': + push_byte(reader, dest, eat_byte_safe(reader, c)); + break; + default: + return r_err(reader, SERD_ERR_BAD_SYNTAX, "invalid escape\n"); + } + + return SERD_SUCCESS; +} + +static SerdStatus +read_PLX(SerdReader* reader, Ref dest) +{ + const int c = peek_byte(reader); + switch (c) { + case '%': + return read_PERCENT(reader, dest); + case '\\': + return read_PN_LOCAL_ESC(reader, dest); + default: + return SERD_FAILURE; + } +} + +static SerdStatus +read_PN_LOCAL(SerdReader* reader, Ref dest, bool* ate_dot) +{ + int c = peek_byte(reader); + SerdStatus st = SERD_SUCCESS; + bool trailing_unescaped_dot = false; + switch (c) { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + case ':': + case '_': + push_byte(reader, dest, eat_byte_safe(reader, c)); + break; + default: + if ((st = read_PLX(reader, dest)) > SERD_FAILURE) { + return r_err(reader, st, "bad escape\n"); + } else if (st != SERD_SUCCESS && read_PN_CHARS_BASE(reader, dest)) { + return SERD_FAILURE; + } + } + + while ((c = peek_byte(reader))) { // Middle: (PN_CHARS | '.' | ':')* + if (c == '.' || c == ':') { + push_byte(reader, dest, eat_byte_safe(reader, c)); + } else if ((st = read_PLX(reader, dest)) > SERD_FAILURE) { + return r_err(reader, SERD_ERR_BAD_SYNTAX, "bad escape\n"); + } else if (st != SERD_SUCCESS && (st = read_PN_CHARS(reader, dest))) { + break; + } + trailing_unescaped_dot = (c == '.'); + } + + SerdNode* const n = deref(reader, dest); + if (trailing_unescaped_dot) { + // Ate trailing dot, pop it from stack/node and inform caller + --n->n_bytes; + serd_stack_pop(&reader->stack, 1); + *ate_dot = true; + } + + return (st > SERD_FAILURE) ? st : SERD_SUCCESS; +} + +// Read the remainder of a PN_PREFIX after some initial characters +static SerdStatus +read_PN_PREFIX_tail(SerdReader* reader, Ref dest) +{ + int c = 0; + while ((c = peek_byte(reader))) { // Middle: (PN_CHARS | '.')* + if (c == '.') { + push_byte(reader, dest, eat_byte_safe(reader, c)); + } else if (read_PN_CHARS(reader, dest)) { + break; + } + } + + const SerdNode* const n = deref(reader, dest); + if (n->buf[n->n_bytes - 1] == '.' && read_PN_CHARS(reader, dest)) { + return r_err(reader, SERD_ERR_BAD_SYNTAX, "prefix ends with `.'\n"); + } + + return SERD_SUCCESS; +} + +static SerdStatus +read_PN_PREFIX(SerdReader* reader, Ref dest) +{ + if (!read_PN_CHARS_BASE(reader, dest)) { + return read_PN_PREFIX_tail(reader, dest); + } + + return SERD_FAILURE; +} + +static SerdStatus +read_LANGTAG(SerdReader* reader, Ref* dest) +{ + int c = peek_byte(reader); + if (!is_alpha(c)) { + return r_err(reader, SERD_ERR_BAD_SYNTAX, "unexpected `%c'\n", c); + } + + *dest = push_node(reader, SERD_LITERAL, "", 0); + + SerdStatus st = SERD_SUCCESS; + TRY(st, push_byte(reader, *dest, eat_byte_safe(reader, c))); + while ((c = peek_byte(reader)) && is_alpha(c)) { + TRY(st, push_byte(reader, *dest, eat_byte_safe(reader, c))); + } + + while (peek_byte(reader) == '-') { + TRY(st, push_byte(reader, *dest, eat_byte_safe(reader, '-'))); + while ((c = peek_byte(reader)) && (is_alpha(c) || is_digit(c))) { + TRY(st, push_byte(reader, *dest, eat_byte_safe(reader, c))); + } + } + + return SERD_SUCCESS; +} + +static SerdStatus +read_IRIREF_scheme(SerdReader* reader, Ref dest) +{ + int c = peek_byte(reader); + if (!is_alpha(c)) { + return r_err(reader, SERD_ERR_BAD_SYNTAX, "bad IRI scheme start `%c'\n", c); + } + + while ((c = peek_byte(reader)) != EOF) { + if (c == '>') { + return r_err(reader, SERD_ERR_BAD_SYNTAX, "missing IRI scheme\n"); + } + + if (!is_uri_scheme_char(c)) { + return r_err(reader, + SERD_ERR_BAD_SYNTAX, + "bad IRI scheme char U+%04X (%c)\n", + (unsigned)c, + (char)c); + } + + push_byte(reader, dest, eat_byte_safe(reader, c)); + if (c == ':') { + return SERD_SUCCESS; // End of scheme + } + } + + return r_err(reader, SERD_ERR_BAD_SYNTAX, "unexpected end of file\n"); +} + +static SerdStatus +read_IRIREF(SerdReader* reader, Ref* dest) +{ + if (!eat_byte_check(reader, '<')) { + return SERD_ERR_BAD_SYNTAX; + } + + *dest = push_node(reader, SERD_URI, "", 0); + + if (!fancy_syntax(reader) && read_IRIREF_scheme(reader, *dest)) { + *dest = pop_node(reader, *dest); + return r_err(reader, SERD_ERR_BAD_SYNTAX, "expected IRI scheme\n"); + } + + SerdStatus st = SERD_SUCCESS; + uint32_t code = 0; + while (!st) { + const int c = eat_byte_safe(reader, peek_byte(reader)); + switch (c) { + case '"': + case '<': + *dest = pop_node(reader, *dest); + return r_err( + reader, SERD_ERR_BAD_SYNTAX, "invalid IRI character `%c'\n", c); + + case '>': + return SERD_SUCCESS; + + case '\\': + if (read_UCHAR(reader, *dest, &code)) { + *dest = pop_node(reader, *dest); + return r_err(reader, SERD_ERR_BAD_SYNTAX, "invalid IRI escape\n"); + } + + switch (code) { + case 0: + case ' ': + case '<': + case '>': + *dest = pop_node(reader, *dest); + return r_err(reader, + SERD_ERR_BAD_SYNTAX, + "invalid escaped IRI character U+%04X\n", + code); + default: + break; + } + break; + + case '^': + case '`': + case '{': + case '|': + case '}': + *dest = pop_node(reader, *dest); + return r_err( + reader, SERD_ERR_BAD_SYNTAX, "invalid IRI character `%c'\n", c); + + default: + if (c <= 0x20) { + r_err(reader, + SERD_ERR_BAD_SYNTAX, + "invalid IRI character (escape %%%02X)\n", + (unsigned)c); + if (reader->strict) { + *dest = pop_node(reader, *dest); + return SERD_ERR_BAD_SYNTAX; + } + st = SERD_FAILURE; + push_byte(reader, *dest, c); + } else if (!(c & 0x80)) { + push_byte(reader, *dest, c); + } else if (read_utf8_character(reader, *dest, (uint8_t)c)) { + if (reader->strict) { + *dest = pop_node(reader, *dest); + return SERD_ERR_BAD_SYNTAX; + } + } + } + } + + *dest = pop_node(reader, *dest); + return st; +} + +static SerdStatus +read_PrefixedName(SerdReader* reader, Ref dest, bool read_prefix, bool* ate_dot) +{ + SerdStatus st = SERD_SUCCESS; + if (read_prefix && ((st = read_PN_PREFIX(reader, dest)) > SERD_FAILURE)) { + return st; + } + + if (peek_byte(reader) != ':') { + return SERD_FAILURE; + } + + push_byte(reader, dest, eat_byte_safe(reader, ':')); + + st = read_PN_LOCAL(reader, dest, ate_dot); + + return (st > SERD_FAILURE) ? st : SERD_SUCCESS; +} + +static SerdStatus +read_0_9(SerdReader* reader, Ref str, bool at_least_one) +{ + unsigned count = 0; + SerdStatus st = SERD_SUCCESS; + for (int c = 0; is_digit((c = peek_byte(reader))); ++count) { + TRY(st, push_byte(reader, str, eat_byte_safe(reader, c))); + } + + if (at_least_one && count == 0) { + return r_err(reader, SERD_ERR_BAD_SYNTAX, "expected digit\n"); + } + + return SERD_SUCCESS; +} + +static SerdStatus +read_number(SerdReader* reader, Ref* dest, Ref* datatype, bool* ate_dot) +{ +#define XSD_DECIMAL NS_XSD "decimal" +#define XSD_DOUBLE NS_XSD "double" +#define XSD_INTEGER NS_XSD "integer" + + *dest = push_node(reader, SERD_LITERAL, "", 0); + + SerdStatus st = SERD_SUCCESS; + int c = peek_byte(reader); + bool has_decimal = false; + if (c == '-' || c == '+') { + push_byte(reader, *dest, eat_byte_safe(reader, c)); + } + if ((c = peek_byte(reader)) == '.') { + has_decimal = true; + // decimal case 2 (e.g. '.0' or `-.0' or `+.0') + push_byte(reader, *dest, eat_byte_safe(reader, c)); + TRY(st, read_0_9(reader, *dest, true)); + } else { + // all other cases ::= ( '-' | '+' ) [0-9]+ ( . )? ( [0-9]+ )? ... + TRY(st, read_0_9(reader, *dest, true)); + if ((c = peek_byte(reader)) == '.') { + has_decimal = true; + + // Annoyingly, dot can be end of statement, so tentatively eat + eat_byte_safe(reader, c); + c = peek_byte(reader); + if (!is_digit(c) && c != 'e' && c != 'E') { + *ate_dot = true; // Force caller to deal with stupid grammar + return SERD_SUCCESS; // Next byte is not a number character + } + + push_byte(reader, *dest, '.'); + read_0_9(reader, *dest, false); + } + } + c = peek_byte(reader); + if (c == 'e' || c == 'E') { + // double + push_byte(reader, *dest, eat_byte_safe(reader, c)); + switch ((c = peek_byte(reader))) { + case '+': + case '-': + push_byte(reader, *dest, eat_byte_safe(reader, c)); + default: + break; + } + TRY(st, read_0_9(reader, *dest, true)); + *datatype = push_node(reader, SERD_URI, XSD_DOUBLE, sizeof(XSD_DOUBLE) - 1); + } else if (has_decimal) { + *datatype = + push_node(reader, SERD_URI, XSD_DECIMAL, sizeof(XSD_DECIMAL) - 1); + } else { + *datatype = + push_node(reader, SERD_URI, XSD_INTEGER, sizeof(XSD_INTEGER) - 1); + } + + return SERD_SUCCESS; +} + +static SerdStatus +read_iri(SerdReader* reader, Ref* dest, bool* ate_dot) +{ + switch (peek_byte(reader)) { + case '<': + return read_IRIREF(reader, dest); + default: + *dest = push_node(reader, SERD_CURIE, "", 0); + return read_PrefixedName(reader, *dest, true, ate_dot); + } +} + +static SerdStatus +read_literal(SerdReader* reader, + Ref* dest, + Ref* datatype, + Ref* lang, + SerdNodeFlags* flags, + bool* ate_dot) +{ + *dest = push_node(reader, SERD_LITERAL, "", 0); + + SerdStatus st = read_String(reader, *dest, flags); + if (st) { + *dest = pop_node(reader, *dest); + return st; + } + + switch (peek_byte(reader)) { + case '@': + eat_byte_safe(reader, '@'); + if ((st = read_LANGTAG(reader, lang))) { + *datatype = pop_node(reader, *datatype); + *lang = pop_node(reader, *lang); + *dest = pop_node(reader, *dest); + return r_err(reader, st, "bad literal\n"); + } + break; + case '^': + eat_byte_safe(reader, '^'); + eat_byte_check(reader, '^'); + if ((st = read_iri(reader, datatype, ate_dot))) { + *datatype = pop_node(reader, *datatype); + *lang = pop_node(reader, *lang); + *dest = pop_node(reader, *dest); + return r_err(reader, st, "bad literal\n"); + } + break; + } + + return SERD_SUCCESS; +} + +static SerdStatus +read_verb(SerdReader* reader, Ref* dest) +{ + if (peek_byte(reader) == '<') { + return read_IRIREF(reader, dest); + } + + /* Either a qname, or "a". Read the prefix first, and if it is in fact + "a", produce that instead. + */ + *dest = push_node(reader, SERD_CURIE, "", 0); + + SerdStatus st = read_PN_PREFIX(reader, *dest); + bool ate_dot = false; + SerdNode* node = deref(reader, *dest); + const int next = peek_byte(reader); + if (!st && node->n_bytes == 1 && node->buf[0] == 'a' && next != ':' && + !is_PN_CHARS_BASE((uint32_t)next)) { + pop_node(reader, *dest); + *dest = push_node(reader, SERD_URI, NS_RDF "type", 47); + return SERD_SUCCESS; + } + + if (st > SERD_FAILURE || read_PrefixedName(reader, *dest, false, &ate_dot) || + ate_dot) { + *dest = pop_node(reader, *dest); + return r_err(reader, SERD_ERR_BAD_SYNTAX, "bad verb\n"); + } + + return SERD_SUCCESS; +} + +static SerdStatus +read_BLANK_NODE_LABEL(SerdReader* reader, Ref* dest, bool* ate_dot) +{ + eat_byte_safe(reader, '_'); + eat_byte_check(reader, ':'); + + const Ref ref = *dest = + push_node(reader, + SERD_BLANK, + reader->bprefix ? (char*)reader->bprefix : "", + reader->bprefix_len); + + int c = peek_byte(reader); // First: (PN_CHARS | '_' | [0-9]) + if (is_digit(c) || c == '_') { + push_byte(reader, ref, eat_byte_safe(reader, c)); + } else if (read_PN_CHARS(reader, ref)) { + *dest = pop_node(reader, *dest); + return r_err(reader, SERD_ERR_BAD_SYNTAX, "invalid name start\n"); + } + + while ((c = peek_byte(reader))) { // Middle: (PN_CHARS | '.')* + if (c == '.') { + push_byte(reader, ref, eat_byte_safe(reader, c)); + } else if (read_PN_CHARS(reader, ref)) { + break; + } + } + + SerdNode* n = deref(reader, ref); + if (n->buf[n->n_bytes - 1] == '.' && read_PN_CHARS(reader, ref)) { + // Ate trailing dot, pop it from stack/node and inform caller + --n->n_bytes; + serd_stack_pop(&reader->stack, 1); + *ate_dot = true; + } + + if (fancy_syntax(reader)) { + if (is_digit(n->buf[reader->bprefix_len + 1])) { + if ((n->buf[reader->bprefix_len]) == 'b') { + ((char*)n->buf)[reader->bprefix_len] = 'B'; // Prevent clash + reader->seen_genid = true; + } else if (reader->seen_genid && n->buf[reader->bprefix_len] == 'B') { + *dest = pop_node(reader, *dest); + return r_err(reader, + SERD_ERR_ID_CLASH, + "found both `b' and `B' blank IDs, prefix required\n"); + } + } + } + + return SERD_SUCCESS; +} + +static Ref +read_blankName(SerdReader* reader) +{ + eat_byte_safe(reader, '='); + if (eat_byte_check(reader, '=') != '=') { + r_err(reader, SERD_ERR_BAD_SYNTAX, "expected `='\n"); + return 0; + } + + Ref subject = 0; + bool ate_dot = false; + read_ws_star(reader); + read_iri(reader, &subject, &ate_dot); + return subject; +} + +static SerdStatus +read_anon(SerdReader* reader, ReadContext ctx, bool subject, Ref* dest) +{ + const SerdStatementFlags old_flags = *ctx.flags; + bool empty = false; + eat_byte_safe(reader, '['); + if ((empty = peek_delim(reader, ']'))) { + *ctx.flags |= (subject) ? SERD_EMPTY_S : SERD_EMPTY_O; + } else { + *ctx.flags |= (subject) ? SERD_ANON_S_BEGIN : SERD_ANON_O_BEGIN; + if (peek_delim(reader, '=')) { + if (!(*dest = read_blankName(reader)) || !eat_delim(reader, ';')) { + return SERD_ERR_BAD_SYNTAX; + } + } + } + + if (!*dest) { + *dest = blank_id(reader); + } + + SerdStatus st = SERD_SUCCESS; + if (ctx.subject) { + TRY(st, emit_statement(reader, ctx, *dest, 0, 0)); + } + + ctx.subject = *dest; + if (!empty) { + *ctx.flags &= ~(unsigned)SERD_LIST_CONT; + if (!subject) { + *ctx.flags |= SERD_ANON_CONT; + } + + bool ate_dot_in_list = false; + read_predicateObjectList(reader, ctx, &ate_dot_in_list); + if (ate_dot_in_list) { + return r_err(reader, SERD_ERR_BAD_SYNTAX, "`.' inside blank\n"); + } + + read_ws_star(reader); + if (reader->end_sink) { + reader->end_sink(reader->handle, deref(reader, *dest)); + } + + *ctx.flags = old_flags; + } + + return (eat_byte_check(reader, ']') == ']') ? SERD_SUCCESS + : SERD_ERR_BAD_SYNTAX; +} + +/* If emit is true: recurses, calling statement_sink for every statement + encountered, and leaves stack in original calling state (i.e. pops + everything it pushes). */ +static SerdStatus +read_object(SerdReader* reader, ReadContext* ctx, bool emit, bool* ate_dot) +{ + static const char* const XSD_BOOLEAN = NS_XSD "boolean"; + static const size_t XSD_BOOLEAN_LEN = 40; + +#ifndef NDEBUG + const size_t orig_stack_size = reader->stack.size; +#endif + + SerdStatus ret = SERD_FAILURE; + + bool simple = (ctx->subject != 0); + SerdNode* node = NULL; + Ref o = 0; + Ref datatype = 0; + Ref lang = 0; + uint32_t flags = 0; + const int c = peek_byte(reader); + if (!fancy_syntax(reader)) { + switch (c) { + case '"': + case ':': + case '<': + case '_': + break; + default: + return r_err(reader, SERD_ERR_BAD_SYNTAX, "expected: ':', '<', or '_'\n"); + } + } + switch (c) { + case EOF: + case ')': + return r_err(reader, SERD_ERR_BAD_SYNTAX, "expected object\n"); + case '[': + simple = false; + ret = read_anon(reader, *ctx, false, &o); + break; + case '(': + simple = false; + ret = read_collection(reader, *ctx, &o); + break; + case '_': + ret = read_BLANK_NODE_LABEL(reader, &o, ate_dot); + break; + case '<': + case ':': + ret = read_iri(reader, &o, ate_dot); + break; + case '+': + case '-': + case '.': + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + ret = read_number(reader, &o, &datatype, ate_dot); + break; + case '\"': + case '\'': + ret = read_literal(reader, &o, &datatype, &lang, &flags, ate_dot); + break; + default: + /* Either a boolean literal, or a qname. Read the prefix first, and if + it is in fact a "true" or "false" literal, produce that instead. + */ + o = push_node(reader, SERD_CURIE, "", 0); + while (!read_PN_CHARS_BASE(reader, o)) { + } + node = deref(reader, o); + if ((node->n_bytes == 4 && !memcmp(node->buf, "true", 4)) || + (node->n_bytes == 5 && !memcmp(node->buf, "false", 5))) { + node->type = SERD_LITERAL; + datatype = push_node(reader, SERD_URI, XSD_BOOLEAN, XSD_BOOLEAN_LEN); + ret = SERD_SUCCESS; + } else if (read_PN_PREFIX_tail(reader, o) > SERD_FAILURE) { + ret = SERD_ERR_BAD_SYNTAX; + } else { + if ((ret = read_PrefixedName(reader, o, false, ate_dot))) { + ret = ret > SERD_FAILURE ? ret : SERD_ERR_BAD_SYNTAX; + pop_node(reader, o); + return r_err(reader, ret, "expected prefixed name\n"); + } + } + } + + if (!ret && simple && o) { + deref(reader, o)->flags = flags; + } + + if (!ret && emit && simple) { + ret = emit_statement(reader, *ctx, o, datatype, lang); + } else if (!ret && !emit) { + ctx->object = o; + ctx->datatype = datatype; + ctx->lang = lang; + return SERD_SUCCESS; + } + + pop_node(reader, lang); + pop_node(reader, datatype); + pop_node(reader, o); +#ifndef NDEBUG + assert(reader->stack.size == orig_stack_size); +#endif + return ret; +} + +static SerdStatus +read_objectList(SerdReader* reader, ReadContext ctx, bool* ate_dot) +{ + SerdStatus st = SERD_SUCCESS; + TRY(st, read_object(reader, &ctx, true, ate_dot)); + if (!fancy_syntax(reader) && peek_delim(reader, ',')) { + return r_err( + reader, SERD_ERR_BAD_SYNTAX, "syntax does not support abbreviation\n"); + } + + while (!*ate_dot && eat_delim(reader, ',')) { + st = read_object(reader, &ctx, true, ate_dot); + } + + return st; +} + +static SerdStatus +read_predicateObjectList(SerdReader* reader, ReadContext ctx, bool* ate_dot) +{ + SerdStatus st = SERD_SUCCESS; + while (!(st = read_verb(reader, &ctx.predicate)) && read_ws_star(reader) && + !(st = read_objectList(reader, ctx, ate_dot))) { + ctx.predicate = pop_node(reader, ctx.predicate); + if (*ate_dot) { + return SERD_SUCCESS; + } + + bool ate_semi = false; + int c = 0; + do { + read_ws_star(reader); + switch (c = peek_byte(reader)) { + case EOF: + return r_err(reader, SERD_ERR_BAD_SYNTAX, "unexpected end of file\n"); + case '.': + case ']': + case '}': + return SERD_SUCCESS; + case ';': + eat_byte_safe(reader, c); + ate_semi = true; + } + } while (c == ';'); + + if (!ate_semi) { + return r_err(reader, SERD_ERR_BAD_SYNTAX, "missing ';' or '.'\n"); + } + } + + ctx.predicate = pop_node(reader, ctx.predicate); + return st; +} + +static SerdStatus +end_collection(SerdReader* reader, + ReadContext ctx, + Ref n1, + Ref n2, + SerdStatus st) +{ + pop_node(reader, n2); + pop_node(reader, n1); + *ctx.flags &= ~(unsigned)SERD_LIST_CONT; + if (!st) { + return (eat_byte_check(reader, ')') == ')') ? SERD_SUCCESS + : SERD_ERR_BAD_SYNTAX; + } + + return st; +} + +static SerdStatus +read_collection(SerdReader* reader, ReadContext ctx, Ref* dest) +{ + SerdStatus st = SERD_SUCCESS; + eat_byte_safe(reader, '('); + + bool end = peek_delim(reader, ')'); + + *dest = end ? reader->rdf_nil : blank_id(reader); + if (ctx.subject) { + // subject predicate _:head + *ctx.flags |= (end ? 0 : SERD_LIST_O_BEGIN); + TRY(st, emit_statement(reader, ctx, *dest, 0, 0)); + *ctx.flags |= SERD_LIST_CONT; + } else { + *ctx.flags |= (end ? 0 : SERD_LIST_S_BEGIN); + } + + if (end) { + return end_collection(reader, ctx, 0, 0, st); + } + + /* The order of node allocation here is necessarily not in stack order, + so we create two nodes and recycle them throughout. */ + Ref n1 = push_node_padded(reader, genid_size(reader), SERD_BLANK, "", 0); + Ref n2 = 0; + Ref node = n1; + Ref rest = 0; + + ctx.subject = *dest; + while (!peek_delim(reader, ')')) { + // _:node rdf:first object + ctx.predicate = reader->rdf_first; + bool ate_dot = false; + if ((st = read_object(reader, &ctx, true, &ate_dot)) || ate_dot) { + return end_collection(reader, ctx, n1, n2, st); + } + + if (!(end = peek_delim(reader, ')'))) { + /* Give rest a new ID. Done as late as possible to ensure it is + used and > IDs generated by read_object above. */ + if (!rest) { + rest = n2 = blank_id(reader); // First pass, push + } else { + set_blank_id(reader, rest, genid_size(reader)); + } + } + + // _:node rdf:rest _:rest + *ctx.flags |= SERD_LIST_CONT; + ctx.predicate = reader->rdf_rest; + TRY(st, emit_statement(reader, ctx, (end ? reader->rdf_nil : rest), 0, 0)); + + ctx.subject = rest; // _:node = _:rest + rest = node; // _:rest = (old)_:node + node = ctx.subject; // invariant + } + + return end_collection(reader, ctx, n1, n2, st); +} + +static SerdStatus +read_subject(SerdReader* reader, ReadContext ctx, Ref* dest, int* s_type) +{ + SerdStatus st = SERD_SUCCESS; + bool ate_dot = false; + switch ((*s_type = peek_byte(reader))) { + case '[': + read_anon(reader, ctx, true, dest); + break; + case '(': + st = read_collection(reader, ctx, dest); + break; + case '_': + st = read_BLANK_NODE_LABEL(reader, dest, &ate_dot); + break; + default: + st = read_iri(reader, dest, &ate_dot); + } + + if (ate_dot) { + pop_node(reader, *dest); + return r_err(reader, SERD_ERR_BAD_SYNTAX, "subject ends with `.'\n"); + } + + return st; +} + +static SerdStatus +read_labelOrSubject(SerdReader* reader, Ref* dest) +{ + bool ate_dot = false; + switch (peek_byte(reader)) { + case '[': + eat_byte_safe(reader, '['); + read_ws_star(reader); + if (!eat_byte_check(reader, ']')) { + return SERD_ERR_BAD_SYNTAX; + } + *dest = blank_id(reader); + return SERD_SUCCESS; + case '_': + return read_BLANK_NODE_LABEL(reader, dest, &ate_dot); + default: + if (!read_iri(reader, dest, &ate_dot)) { + return SERD_SUCCESS; + } else { + return r_err(reader, SERD_ERR_BAD_SYNTAX, "expected label or subject\n"); + } + } +} + +static SerdStatus +read_triples(SerdReader* reader, ReadContext ctx, bool* ate_dot) +{ + SerdStatus st = SERD_FAILURE; + if (ctx.subject) { + read_ws_star(reader); + switch (peek_byte(reader)) { + case '.': + *ate_dot = eat_byte_safe(reader, '.'); + return SERD_FAILURE; + case '}': + return SERD_FAILURE; + } + st = read_predicateObjectList(reader, ctx, ate_dot); + } + + ctx.subject = ctx.predicate = 0; + return st > SERD_FAILURE ? st : SERD_SUCCESS; +} + +static SerdStatus +read_base(SerdReader* reader, bool sparql, bool token) +{ + SerdStatus st = SERD_SUCCESS; + if (token) { + TRY(st, eat_string(reader, "base", 4)); + } + + read_ws_star(reader); + + Ref uri = 0; + TRY(st, read_IRIREF(reader, &uri)); + if (reader->base_sink) { + TRY(st, reader->base_sink(reader->handle, deref(reader, uri))); + } + pop_node(reader, uri); + + read_ws_star(reader); + if (!sparql) { + return eat_byte_check(reader, '.') ? SERD_SUCCESS : SERD_ERR_BAD_SYNTAX; + } + + if (peek_byte(reader) == '.') { + return r_err(reader, SERD_ERR_BAD_SYNTAX, "full stop after SPARQL BASE\n"); + } + + return SERD_SUCCESS; +} + +static SerdStatus +read_prefixID(SerdReader* reader, bool sparql, bool token) +{ + SerdStatus st = SERD_SUCCESS; + if (token) { + TRY(st, eat_string(reader, "prefix", 6)); + } + + read_ws_star(reader); + Ref name = push_node(reader, SERD_LITERAL, "", 0); + if ((st = read_PN_PREFIX(reader, name)) > SERD_FAILURE) { + return st; + } + + if (eat_byte_check(reader, ':') != ':') { + pop_node(reader, name); + return SERD_ERR_BAD_SYNTAX; + } + + read_ws_star(reader); + Ref uri = 0; + TRY(st, read_IRIREF(reader, &uri)); + + if (reader->prefix_sink) { + st = reader->prefix_sink( + reader->handle, deref(reader, name), deref(reader, uri)); + } + + pop_node(reader, uri); + pop_node(reader, name); + if (!sparql) { + read_ws_star(reader); + st = eat_byte_check(reader, '.') ? SERD_SUCCESS : SERD_ERR_BAD_SYNTAX; + } + + return st; +} + +static SerdStatus +read_directive(SerdReader* reader) +{ + const bool sparql = peek_byte(reader) != '@'; + if (!sparql) { + eat_byte_safe(reader, '@'); + switch (peek_byte(reader)) { + case 'B': + case 'P': + return r_err(reader, SERD_ERR_BAD_SYNTAX, "uppercase directive\n"); + } + } + + switch (peek_byte(reader)) { + case 'B': + case 'b': + return read_base(reader, sparql, true); + case 'P': + case 'p': + return read_prefixID(reader, sparql, true); + default: + break; + } + + return r_err(reader, SERD_ERR_BAD_SYNTAX, "invalid directive\n"); +} + +static SerdStatus +read_wrappedGraph(SerdReader* reader, ReadContext* ctx) +{ + if (!eat_byte_check(reader, '{')) { + return SERD_ERR_BAD_SYNTAX; + } + + read_ws_star(reader); + while (peek_byte(reader) != '}') { + bool ate_dot = false; + int s_type = 0; + ctx->subject = 0; + SerdStatus st = read_subject(reader, *ctx, &ctx->subject, &s_type); + if (st) { + return r_err(reader, SERD_ERR_BAD_SYNTAX, "bad subject\n"); + } + + if (read_triples(reader, *ctx, &ate_dot) && s_type != '[') { + return r_err( + reader, SERD_ERR_BAD_SYNTAX, "missing predicate object list\n"); + } + + pop_node(reader, ctx->subject); + read_ws_star(reader); + if (peek_byte(reader) == '.') { + eat_byte_safe(reader, '.'); + } + read_ws_star(reader); + } + + eat_byte_safe(reader, '}'); + read_ws_star(reader); + if (peek_byte(reader) == '.') { + return r_err(reader, SERD_ERR_BAD_SYNTAX, "graph followed by `.'\n"); + } + + return SERD_SUCCESS; +} + +static int +tokcmp(SerdReader* reader, Ref ref, const char* tok, size_t n) +{ + SerdNode* node = deref(reader, ref); + if (!node || node->n_bytes != n) { + return -1; + } + + return serd_strncasecmp((const char*)node->buf, tok, n); +} + +SerdStatus +read_n3_statement(SerdReader* reader) +{ + SerdStatementFlags flags = 0; + ReadContext ctx = {0, 0, 0, 0, 0, 0, &flags}; + bool ate_dot = false; + int s_type = 0; + SerdStatus st = SERD_SUCCESS; + read_ws_star(reader); + switch (peek_byte(reader)) { + case '\0': + eat_byte_safe(reader, '\0'); + return SERD_FAILURE; + case EOF: + return SERD_FAILURE; + case '@': + if (!fancy_syntax(reader)) { + return r_err( + reader, SERD_ERR_BAD_SYNTAX, "syntax does not support directives\n"); + } + TRY(st, read_directive(reader)); + read_ws_star(reader); + break; + case '{': + if (reader->syntax == SERD_TRIG) { + TRY(st, read_wrappedGraph(reader, &ctx)); + read_ws_star(reader); + } else { + return r_err( + reader, SERD_ERR_BAD_SYNTAX, "syntax does not support graphs\n"); + } + break; + default: + if ((st = read_subject(reader, ctx, &ctx.subject, &s_type)) > + SERD_FAILURE) { + return st; + } + + if (!tokcmp(reader, ctx.subject, "base", 4)) { + st = read_base(reader, true, false); + } else if (!tokcmp(reader, ctx.subject, "prefix", 6)) { + st = read_prefixID(reader, true, false); + } else if (!tokcmp(reader, ctx.subject, "graph", 5)) { + read_ws_star(reader); + TRY(st, read_labelOrSubject(reader, &ctx.graph)); + read_ws_star(reader); + TRY(st, read_wrappedGraph(reader, &ctx)); + pop_node(reader, ctx.graph); + ctx.graph = 0; + read_ws_star(reader); + } else if (read_ws_star(reader) && peek_byte(reader) == '{') { + if (s_type == '(' || (s_type == '[' && !*ctx.flags)) { + return r_err(reader, SERD_ERR_BAD_SYNTAX, "invalid graph name\n"); + } + ctx.graph = ctx.subject; + ctx.subject = 0; + TRY(st, read_wrappedGraph(reader, &ctx)); + pop_node(reader, ctx.graph); + read_ws_star(reader); + } else if ((st = read_triples(reader, ctx, &ate_dot))) { + if (st == SERD_FAILURE && s_type == '[') { + return SERD_SUCCESS; + } + + if (ate_dot) { + return r_err( + reader, SERD_ERR_BAD_SYNTAX, "unexpected end of statement\n"); + } + + return st > SERD_FAILURE ? st : SERD_ERR_BAD_SYNTAX; + } else if (!ate_dot) { + read_ws_star(reader); + st = (eat_byte_check(reader, '.') == '.') ? SERD_SUCCESS + : SERD_ERR_BAD_SYNTAX; + } + break; + } + return st; +} + +static void +skip_until(SerdReader* reader, uint8_t byte) +{ + for (int c = 0; (c = peek_byte(reader)) && c != byte;) { + eat_byte_safe(reader, c); + } +} + +SerdStatus +read_turtleTrigDoc(SerdReader* reader) +{ + while (!reader->source.eof) { + const SerdStatus st = read_n3_statement(reader); + if (st > SERD_FAILURE) { + if (reader->strict) { + return st; + } + skip_until(reader, '\n'); + } + } + + return SERD_SUCCESS; +} + +SerdStatus +read_nquadsDoc(SerdReader* reader) +{ + SerdStatus st = SERD_SUCCESS; + while (!reader->source.eof) { + SerdStatementFlags flags = 0; + ReadContext ctx = {0, 0, 0, 0, 0, 0, &flags}; + bool ate_dot = false; + int s_type = 0; + read_ws_star(reader); + if (peek_byte(reader) == EOF) { + break; + } + + if (peek_byte(reader) == '@') { + return r_err( + reader, SERD_ERR_BAD_SYNTAX, "syntax does not support directives\n"); + } + + // subject predicate object + if ((st = read_subject(reader, ctx, &ctx.subject, &s_type)) || + !read_ws_star(reader) || (st = read_IRIREF(reader, &ctx.predicate)) || + !read_ws_star(reader) || + (st = read_object(reader, &ctx, false, &ate_dot))) { + return st; + } + + if (!ate_dot) { // graphLabel? + read_ws_star(reader); + switch (peek_byte(reader)) { + case '.': + break; + case '_': + TRY(st, read_BLANK_NODE_LABEL(reader, &ctx.graph, &ate_dot)); + break; + default: + TRY(st, read_IRIREF(reader, &ctx.graph)); + } + + // Terminating '.' + read_ws_star(reader); + if (!eat_byte_check(reader, '.')) { + return SERD_ERR_BAD_SYNTAX; + } + } + + TRY(st, emit_statement(reader, ctx, ctx.object, ctx.datatype, ctx.lang)); + + pop_node(reader, ctx.graph); + pop_node(reader, ctx.lang); + pop_node(reader, ctx.datatype); + pop_node(reader, ctx.object); + } + return SERD_SUCCESS; +} diff --git a/modules/juce_audio_processors/format_types/lv2/serd/src/node.c b/modules/juce_audio_processors/format_types/lv2/serd/src/node.c new file mode 100644 index 0000000000..02aedbf270 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/serd/src/node.c @@ -0,0 +1,393 @@ +/* + Copyright 2011-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "node.h" + +#include "base64.h" +#include "string_utils.h" + +#include "serd/serd.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef _WIN32 +# ifndef isnan +# define isnan(x) _isnan(x) +# endif +# ifndef isinf +# define isinf(x) (!_finite(x)) +# endif +#endif + +SerdNode +serd_node_from_string(SerdType type, const uint8_t* str) +{ + if (!str) { + return SERD_NODE_NULL; + } + + SerdNodeFlags flags = 0; + size_t buf_n_bytes = 0; + const size_t buf_n_chars = serd_strlen(str, &buf_n_bytes, &flags); + SerdNode ret = {str, buf_n_bytes, buf_n_chars, flags, type}; + return ret; +} + +SerdNode +serd_node_from_substring(SerdType type, const uint8_t* str, const size_t len) +{ + if (!str) { + return SERD_NODE_NULL; + } + + SerdNodeFlags flags = 0; + size_t buf_n_bytes = 0; + const size_t buf_n_chars = serd_substrlen(str, len, &buf_n_bytes, &flags); + assert(buf_n_bytes <= len); + SerdNode ret = {str, buf_n_bytes, buf_n_chars, flags, type}; + return ret; +} + +SerdNode +serd_node_copy(const SerdNode* node) +{ + if (!node || !node->buf) { + return SERD_NODE_NULL; + } + + SerdNode copy = *node; + uint8_t* buf = (uint8_t*)malloc(copy.n_bytes + 1); + memcpy(buf, node->buf, copy.n_bytes + 1); + copy.buf = buf; + return copy; +} + +bool +serd_node_equals(const SerdNode* a, const SerdNode* b) +{ + return (a == b) || + (a->type == b->type && a->n_bytes == b->n_bytes && + a->n_chars == b->n_chars && + ((a->buf == b->buf) || + !memcmp((const char*)a->buf, (const char*)b->buf, a->n_bytes + 1))); +} + +static size_t +serd_uri_string_length(const SerdURI* uri) +{ + size_t len = uri->path_base.len; + +#define ADD_LEN(field, n_delims) \ + if ((field).len) { \ + len += (field).len + (n_delims); \ + } + + ADD_LEN(uri->path, 1) // + possible leading `/' + ADD_LEN(uri->scheme, 1) // + trailing `:' + ADD_LEN(uri->authority, 2) // + leading `//' + ADD_LEN(uri->query, 1) // + leading `?' + ADD_LEN(uri->fragment, 1) // + leading `#' + + return len + 2; // + 2 for authority `//' +} + +static size_t +string_sink(const void* buf, size_t len, void* stream) +{ + uint8_t** ptr = (uint8_t**)stream; + memcpy(*ptr, buf, len); + *ptr += len; + return len; +} + +SerdNode +serd_node_new_uri_from_node(const SerdNode* uri_node, + const SerdURI* base, + SerdURI* out) +{ + return (uri_node->type == SERD_URI && uri_node->buf) + ? serd_node_new_uri_from_string(uri_node->buf, base, out) + : SERD_NODE_NULL; +} + +SerdNode +serd_node_new_uri_from_string(const uint8_t* str, + const SerdURI* base, + SerdURI* out) +{ + if (!str || str[0] == '\0') { + // Empty URI => Base URI, or nothing if no base is given + return base ? serd_node_new_uri(base, NULL, out) : SERD_NODE_NULL; + } + + SerdURI uri; + serd_uri_parse(str, &uri); + return serd_node_new_uri(&uri, base, out); // Resolve/Serialise +} + +static inline bool +is_uri_path_char(const uint8_t c) +{ + if (is_alpha(c) || is_digit(c)) { + return true; + } + + switch (c) { + // unreserved: + case '-': + case '.': + case '_': + case '~': + // pchar: + case ':': + case '@': + // separator: + case '/': + // sub-delimeters: + case '!': + case '$': + case '&': + case '\'': + case '(': + case ')': + case '*': + case '+': + case ',': + case ';': + case '=': + return true; + default: + return false; + } +} + +SerdNode +serd_node_new_file_uri(const uint8_t* path, + const uint8_t* hostname, + SerdURI* out, + bool escape) +{ + const size_t path_len = strlen((const char*)path); + const size_t hostname_len = hostname ? strlen((const char*)hostname) : 0; + const bool is_windows = is_windows_path(path); + size_t uri_len = 0; + uint8_t* uri = NULL; + + if (path[0] == '/' || is_windows) { + uri_len = strlen("file://") + hostname_len + is_windows; + uri = (uint8_t*)calloc(uri_len + 1, 1); + + memcpy(uri, "file://", 7); + + if (hostname) { + memcpy(uri + 7, hostname, hostname_len); + } + + if (is_windows) { + ((char*)uri)[7 + hostname_len] = '/'; + } + } + + SerdChunk chunk = {uri, uri_len}; + for (size_t i = 0; i < path_len; ++i) { + if (is_windows && path[i] == '\\') { + serd_chunk_sink("/", 1, &chunk); + } else if (path[i] == '%') { + serd_chunk_sink("%%", 2, &chunk); + } else if (!escape || is_uri_path_char(path[i])) { + serd_chunk_sink(path + i, 1, &chunk); + } else { + char escape_str[4] = {'%', 0, 0, 0}; + snprintf(escape_str + 1, sizeof(escape_str) - 1, "%X", (unsigned)path[i]); + serd_chunk_sink(escape_str, 3, &chunk); + } + } + + serd_chunk_sink_finish(&chunk); + + if (out) { + serd_uri_parse(chunk.buf, out); + } + + return serd_node_from_substring(SERD_URI, chunk.buf, chunk.len); +} + +SerdNode +serd_node_new_uri(const SerdURI* uri, const SerdURI* base, SerdURI* out) +{ + SerdURI abs_uri = *uri; + if (base) { + serd_uri_resolve(uri, base, &abs_uri); + } + + const size_t len = serd_uri_string_length(&abs_uri); + uint8_t* buf = (uint8_t*)malloc(len + 1); + SerdNode node = {buf, 0, 0, 0, SERD_URI}; + uint8_t* ptr = buf; + const size_t actual_len = serd_uri_serialise(&abs_uri, string_sink, &ptr); + + buf[actual_len] = '\0'; + node.n_bytes = actual_len; + node.n_chars = serd_strlen(buf, NULL, NULL); + + if (out) { + serd_uri_parse(buf, out); // TODO: cleverly avoid double parse + } + + return node; +} + +SerdNode +serd_node_new_relative_uri(const SerdURI* uri, + const SerdURI* base, + const SerdURI* root, + SerdURI* out) +{ + const size_t uri_len = serd_uri_string_length(uri); + const size_t base_len = serd_uri_string_length(base); + uint8_t* buf = (uint8_t*)malloc(uri_len + base_len + 1); + SerdNode node = {buf, 0, 0, 0, SERD_URI}; + uint8_t* ptr = buf; + const size_t actual_len = + serd_uri_serialise_relative(uri, base, root, string_sink, &ptr); + + buf[actual_len] = '\0'; + node.n_bytes = actual_len; + node.n_chars = serd_strlen(buf, NULL, NULL); + + if (out) { + serd_uri_parse(buf, out); // TODO: cleverly avoid double parse + } + + return node; +} + +static inline unsigned +serd_digits(double abs) +{ + const double lg = ceil(log10(floor(abs) + 1.0)); + return lg < 1.0 ? 1U : (unsigned)lg; +} + +SerdNode +serd_node_new_decimal(double d, unsigned frac_digits) +{ + if (isnan(d) || isinf(d)) { + return SERD_NODE_NULL; + } + + const double abs_d = fabs(d); + const unsigned int_digits = serd_digits(abs_d); + char* buf = (char*)calloc(int_digits + frac_digits + 3, 1); + SerdNode node = {(const uint8_t*)buf, 0, 0, 0, SERD_LITERAL}; + const double int_part = floor(abs_d); + + // Point s to decimal point location + char* s = buf + int_digits; + if (d < 0.0) { + *buf = '-'; + ++s; + } + + // Write integer part (right to left) + char* t = s - 1; + uint64_t dec = (uint64_t)int_part; + do { + *t-- = (char)('0' + dec % 10); + } while ((dec /= 10) > 0); + + *s++ = '.'; + + // Write fractional part (right to left) + double frac_part = fabs(d - int_part); + if (frac_part < DBL_EPSILON) { + *s++ = '0'; + node.n_bytes = node.n_chars = (size_t)(s - buf); + } else { + uint64_t frac = (uint64_t)llround(frac_part * pow(10.0, (int)frac_digits)); + s += frac_digits - 1; + unsigned i = 0; + + // Skip trailing zeros + for (; i < frac_digits - 1 && !(frac % 10); ++i, --s, frac /= 10) { + } + + node.n_bytes = node.n_chars = (size_t)(s - buf) + 1u; + + // Write digits from last trailing zero to decimal point + for (; i < frac_digits; ++i) { + *s-- = (char)('0' + (frac % 10)); + frac /= 10; + } + } + + return node; +} + +SerdNode +serd_node_new_integer(int64_t i) +{ + uint64_t abs_i = (i < 0) ? -i : i; + const unsigned digits = serd_digits((double)abs_i); + char* buf = (char*)calloc(digits + 2, 1); + SerdNode node = {(const uint8_t*)buf, 0, 0, 0, SERD_LITERAL}; + + // Point s to the end + char* s = buf + digits - 1; + if (i < 0) { + *buf = '-'; + ++s; + } + + node.n_bytes = node.n_chars = (size_t)(s - buf) + 1u; + + // Write integer part (right to left) + do { + *s-- = (char)('0' + (abs_i % 10)); + } while ((abs_i /= 10) > 0); + + return node; +} + +SerdNode +serd_node_new_blob(const void* buf, size_t size, bool wrap_lines) +{ + const size_t len = serd_base64_get_length(size, wrap_lines); + uint8_t* str = (uint8_t*)calloc(len + 2, 1); + SerdNode node = {str, len, len, 0, SERD_LITERAL}; + + if (serd_base64_encode(str, buf, size, wrap_lines)) { + node.flags |= SERD_HAS_NEWLINE; + } + + return node; +} + +void +serd_node_free(SerdNode* node) +{ + if (node && node->buf) { + free((uint8_t*)node->buf); + node->buf = NULL; + } +} diff --git a/modules/juce_audio_processors/format_types/lv2/serd/src/node.h b/modules/juce_audio_processors/format_types/lv2/serd/src/node.h new file mode 100644 index 0000000000..60f460dd3c --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/serd/src/node.h @@ -0,0 +1,48 @@ +/* + Copyright 2011-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef SERD_NODE_H +#define SERD_NODE_H + +#include "serd/serd.h" + +#include + +struct SerdNodeImpl { + size_t n_bytes; /**< Size in bytes (not including null) */ + SerdNodeFlags flags; /**< Node flags (e.g. string properties) */ + SerdType type; /**< Node type */ +}; + +static inline char* +serd_node_buffer(SerdNode* node) +{ + return (char*)(node + 1); +} + +static inline const char* +serd_node_buffer_c(const SerdNode* node) +{ + return (const char*)(node + 1); +} + +SerdNode* +serd_node_malloc(size_t n_bytes, SerdNodeFlags flags, SerdType type); + +void +serd_node_set(SerdNode** dst, const SerdNode* src); + +#endif // SERD_NODE_H diff --git a/modules/juce_audio_processors/format_types/lv2/serd/src/reader.c b/modules/juce_audio_processors/format_types/lv2/serd/src/reader.c new file mode 100644 index 0000000000..e21b6aa53e --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/serd/src/reader.c @@ -0,0 +1,425 @@ +/* + Copyright 2011-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "byte_source.h" +#include "reader.h" +#include "stack.h" +#include "system.h" + +#include "serd_internal.h" + +#include +#include +#include +#include +#include +#include + +SerdStatus +r_err(SerdReader* reader, SerdStatus st, const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + const Cursor* const cur = &reader->source.cur; + const SerdError e = {st, cur->filename, cur->line, cur->col, fmt, &args}; + serd_error(reader->error_sink, reader->error_handle, &e); + va_end(args); + return st; +} + +void +set_blank_id(SerdReader* reader, Ref ref, size_t buf_size) +{ + SerdNode* node = deref(reader, ref); + const char* prefix = reader->bprefix ? (const char*)reader->bprefix : ""; + node->n_bytes = node->n_chars = (size_t)snprintf( + (char*)node->buf, buf_size, "%sb%u", prefix, reader->next_id++); +} + +size_t +genid_size(SerdReader* reader) +{ + return reader->bprefix_len + 1 + 10 + 1; // + "b" + UINT32_MAX + \0 +} + +Ref +blank_id(SerdReader* reader) +{ + Ref ref = push_node_padded(reader, genid_size(reader), SERD_BLANK, "", 0); + set_blank_id(reader, ref, genid_size(reader)); + return ref; +} + +/** fread-like wrapper for getc (which is faster). */ +static size_t +serd_file_read_byte(void* buf, size_t size, size_t nmemb, void* stream) +{ + (void)size; + (void)nmemb; + + const int c = getc((FILE*)stream); + if (c == EOF) { + *((uint8_t*)buf) = 0; + return 0; + } + *((uint8_t*)buf) = (uint8_t)c; + return 1; +} + +Ref +push_node_padded(SerdReader* reader, + size_t maxlen, + SerdType type, + const char* str, + size_t n_bytes) +{ + void* mem = serd_stack_push_aligned( + &reader->stack, sizeof(SerdNode) + maxlen + 1, sizeof(SerdNode)); + + SerdNode* const node = (SerdNode*)mem; + node->n_bytes = node->n_chars = n_bytes; + node->flags = 0; + node->type = type; + node->buf = NULL; + + uint8_t* buf = (uint8_t*)(node + 1); + memcpy(buf, str, n_bytes + 1); + +#ifdef SERD_STACK_CHECK + reader->allocs = (Ref*)realloc(reader->allocs, + sizeof(reader->allocs) * (++reader->n_allocs)); + reader->allocs[reader->n_allocs - 1] = ((uint8_t*)mem - reader->stack.buf); +#endif + return (Ref)((uint8_t*)node - reader->stack.buf); +} + +Ref +push_node(SerdReader* reader, SerdType type, const char* str, size_t n_bytes) +{ + return push_node_padded(reader, n_bytes, type, str, n_bytes); +} + +SerdNode* +deref(SerdReader* reader, const Ref ref) +{ + if (ref) { + SerdNode* node = (SerdNode*)(reader->stack.buf + ref); + node->buf = (uint8_t*)node + sizeof(SerdNode); + return node; + } + return NULL; +} + +Ref +pop_node(SerdReader* reader, Ref ref) +{ + if (ref && ref != reader->rdf_first && ref != reader->rdf_rest && + ref != reader->rdf_nil) { +#ifdef SERD_STACK_CHECK + SERD_STACK_ASSERT_TOP(reader, ref); + --reader->n_allocs; +#endif + SerdNode* const node = deref(reader, ref); + uint8_t* const top = reader->stack.buf + reader->stack.size; + serd_stack_pop_aligned(&reader->stack, (size_t)(top - (uint8_t*)node)); + } + return 0; +} + +SerdStatus +emit_statement(SerdReader* reader, ReadContext ctx, Ref o, Ref d, Ref l) +{ + SerdNode* graph = deref(reader, ctx.graph); + if (!graph && reader->default_graph.buf) { + graph = &reader->default_graph; + } + + const SerdStatus st = !reader->statement_sink + ? SERD_SUCCESS + : reader->statement_sink(reader->handle, + *ctx.flags, + graph, + deref(reader, ctx.subject), + deref(reader, ctx.predicate), + deref(reader, o), + deref(reader, d), + deref(reader, l)); + + *ctx.flags &= SERD_ANON_CONT | SERD_LIST_CONT; // Preserve only cont flags + return st; +} + +static SerdStatus +read_statement(SerdReader* reader) +{ + return read_n3_statement(reader); +} + +static SerdStatus +read_doc(SerdReader* reader) +{ + return ((reader->syntax == SERD_NQUADS) ? read_nquadsDoc(reader) + : read_turtleTrigDoc(reader)); +} + +SerdReader* +serd_reader_new(SerdSyntax syntax, + void* handle, + void (*free_handle)(void*), + SerdBaseSink base_sink, + SerdPrefixSink prefix_sink, + SerdStatementSink statement_sink, + SerdEndSink end_sink) +{ + SerdReader* me = (SerdReader*)calloc(1, sizeof(SerdReader)); + me->handle = handle; + me->free_handle = free_handle; + me->base_sink = base_sink; + me->prefix_sink = prefix_sink; + me->statement_sink = statement_sink; + me->end_sink = end_sink; + me->default_graph = SERD_NODE_NULL; + me->stack = serd_stack_new(SERD_PAGE_SIZE); + me->syntax = syntax; + me->next_id = 1; + me->strict = true; + + me->rdf_first = push_node(me, SERD_URI, NS_RDF "first", 48); + me->rdf_rest = push_node(me, SERD_URI, NS_RDF "rest", 47); + me->rdf_nil = push_node(me, SERD_URI, NS_RDF "nil", 46); + + return me; +} + +void +serd_reader_set_strict(SerdReader* reader, bool strict) +{ + reader->strict = strict; +} + +void +serd_reader_set_error_sink(SerdReader* reader, + SerdErrorSink error_sink, + void* error_handle) +{ + reader->error_sink = error_sink; + reader->error_handle = error_handle; +} + +void +serd_reader_free(SerdReader* reader) +{ + if (!reader) { + return; + } + + pop_node(reader, reader->rdf_nil); + pop_node(reader, reader->rdf_rest); + pop_node(reader, reader->rdf_first); + serd_node_free(&reader->default_graph); + +#ifdef SERD_STACK_CHECK + free(reader->allocs); +#endif + free(reader->stack.buf); + free(reader->bprefix); + if (reader->free_handle) { + reader->free_handle(reader->handle); + } + free(reader); +} + +void* +serd_reader_get_handle(const SerdReader* reader) +{ + return reader->handle; +} + +void +serd_reader_add_blank_prefix(SerdReader* reader, const uint8_t* prefix) +{ + free(reader->bprefix); + reader->bprefix_len = 0; + reader->bprefix = NULL; + + const size_t prefix_len = prefix ? strlen((const char*)prefix) : 0; + if (prefix_len) { + reader->bprefix_len = prefix_len; + reader->bprefix = (uint8_t*)malloc(reader->bprefix_len + 1); + memcpy(reader->bprefix, prefix, reader->bprefix_len + 1); + } +} + +void +serd_reader_set_default_graph(SerdReader* reader, const SerdNode* graph) +{ + serd_node_free(&reader->default_graph); + reader->default_graph = serd_node_copy(graph); +} + +SerdStatus +serd_reader_read_file(SerdReader* reader, const uint8_t* uri) +{ + uint8_t* const path = serd_file_uri_parse(uri, NULL); + if (!path) { + return SERD_ERR_BAD_ARG; + } + + FILE* fd = serd_fopen((const char*)path, "rb"); + if (!fd) { + serd_free(path); + return SERD_ERR_UNKNOWN; + } + + SerdStatus ret = serd_reader_read_file_handle(reader, fd, path); + fclose(fd); + free(path); + return ret; +} + +static SerdStatus +skip_bom(SerdReader* me) +{ + if (serd_byte_source_peek(&me->source) == 0xEF) { + serd_byte_source_advance(&me->source); + if (serd_byte_source_peek(&me->source) != 0xBB || + serd_byte_source_advance(&me->source) || + serd_byte_source_peek(&me->source) != 0xBF || + serd_byte_source_advance(&me->source)) { + r_err(me, SERD_ERR_BAD_SYNTAX, "corrupt byte order mark\n"); + return SERD_ERR_BAD_SYNTAX; + } + } + + return SERD_SUCCESS; +} + +SerdStatus +serd_reader_start_stream(SerdReader* reader, + FILE* file, + const uint8_t* name, + bool bulk) +{ + return serd_reader_start_source_stream(reader, + bulk ? (SerdSource)fread + : serd_file_read_byte, + (SerdStreamErrorFunc)ferror, + file, + name, + bulk ? SERD_PAGE_SIZE : 1); +} + +SerdStatus +serd_reader_start_source_stream(SerdReader* reader, + SerdSource read_func, + SerdStreamErrorFunc error_func, + void* stream, + const uint8_t* name, + size_t page_size) +{ + return serd_byte_source_open_source( + &reader->source, read_func, error_func, stream, name, page_size); +} + +static SerdStatus +serd_reader_prepare(SerdReader* reader) +{ + SerdStatus st = serd_byte_source_prepare(&reader->source); + if (st == SERD_SUCCESS) { + st = skip_bom(reader); + } else if (st == SERD_FAILURE) { + reader->source.eof = true; + } else { + r_err(reader, st, "read error: %s\n", strerror(errno)); + } + return st; +} + +SerdStatus +serd_reader_read_chunk(SerdReader* reader) +{ + SerdStatus st = SERD_SUCCESS; + if (!reader->source.prepared) { + st = serd_reader_prepare(reader); + } else if (reader->source.eof) { + st = serd_byte_source_advance(&reader->source); + } + + if (peek_byte(reader) == 0) { + // Skip leading null byte, for reading from a null-delimited socket + eat_byte_safe(reader, 0); + } + + return st ? st : read_statement(reader); +} + +SerdStatus +serd_reader_end_stream(SerdReader* reader) +{ + return serd_byte_source_close(&reader->source); +} + +SerdStatus +serd_reader_read_file_handle(SerdReader* reader, + FILE* file, + const uint8_t* name) +{ + return serd_reader_read_source(reader, + (SerdSource)fread, + (SerdStreamErrorFunc)ferror, + file, + name, + SERD_PAGE_SIZE); +} + +SerdStatus +serd_reader_read_source(SerdReader* reader, + SerdSource source, + SerdStreamErrorFunc error, + void* stream, + const uint8_t* name, + size_t page_size) +{ + SerdStatus st = serd_reader_start_source_stream( + reader, source, error, stream, name, page_size); + + if (st || (st = serd_reader_prepare(reader))) { + serd_reader_end_stream(reader); + return st; + } + + if ((st = read_doc(reader))) { + serd_reader_end_stream(reader); + return st; + } + + return serd_reader_end_stream(reader); +} + +SerdStatus +serd_reader_read_string(SerdReader* reader, const uint8_t* utf8) +{ + serd_byte_source_open_string(&reader->source, utf8); + + SerdStatus st = serd_reader_prepare(reader); + if (!st) { + st = read_doc(reader); + } + + serd_byte_source_close(&reader->source); + + return st; +} diff --git a/modules/juce_audio_processors/format_types/lv2/serd/src/reader.h b/modules/juce_audio_processors/format_types/lv2/serd/src/reader.h new file mode 100644 index 0000000000..3a8991f4da --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/serd/src/reader.h @@ -0,0 +1,196 @@ +/* + Copyright 2011-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef SERD_READER_H +#define SERD_READER_H + +#include "byte_source.h" +#include "stack.h" + +#include "serd/serd.h" + +#include +#include +#include +#include + +#if defined(__GNUC__) +# define SERD_LOG_FUNC(fmt, arg1) __attribute__((format(printf, fmt, arg1))) +#else +# define SERD_LOG_FUNC(fmt, arg1) +#endif + +#ifdef SERD_STACK_CHECK +# define SERD_STACK_ASSERT_TOP(reader, ref) \ + assert(ref == reader->allocs[reader->n_allocs - 1]); +#else +# define SERD_STACK_ASSERT_TOP(reader, ref) +#endif + +/* Reference to a node in the stack (we can not use pointers since the + stack may be reallocated, invalidating any pointers to elements). +*/ +typedef size_t Ref; + +typedef struct { + Ref graph; + Ref subject; + Ref predicate; + Ref object; + Ref datatype; + Ref lang; + SerdStatementFlags* flags; +} ReadContext; + +struct SerdReaderImpl { + void* handle; + void (*free_handle)(void* ptr); + SerdBaseSink base_sink; + SerdPrefixSink prefix_sink; + SerdStatementSink statement_sink; + SerdEndSink end_sink; + SerdErrorSink error_sink; + void* error_handle; + Ref rdf_first; + Ref rdf_rest; + Ref rdf_nil; + SerdNode default_graph; + SerdByteSource source; + SerdStack stack; + SerdSyntax syntax; + unsigned next_id; + uint8_t* buf; + uint8_t* bprefix; + size_t bprefix_len; + bool strict; ///< True iff strict parsing + bool seen_genid; +#ifdef SERD_STACK_CHECK + Ref* allocs; ///< Stack of push offsets + size_t n_allocs; ///< Number of stack pushes +#endif +}; + +SERD_LOG_FUNC(3, 4) +SerdStatus +r_err(SerdReader* reader, SerdStatus st, const char* fmt, ...); + +Ref +push_node_padded(SerdReader* reader, + size_t maxlen, + SerdType type, + const char* str, + size_t n_bytes); + +Ref +push_node(SerdReader* reader, SerdType type, const char* str, size_t n_bytes); + +SERD_PURE_FUNC size_t +genid_size(SerdReader* reader); + +Ref +blank_id(SerdReader* reader); + +void +set_blank_id(SerdReader* reader, Ref ref, size_t buf_size); + +SerdNode* +deref(SerdReader* reader, Ref ref); + +Ref +pop_node(SerdReader* reader, Ref ref); + +SerdStatus +emit_statement(SerdReader* reader, ReadContext ctx, Ref o, Ref d, Ref l); + +SerdStatus +read_n3_statement(SerdReader* reader); + +SerdStatus +read_nquadsDoc(SerdReader* reader); + +SerdStatus +read_turtleTrigDoc(SerdReader* reader); + +static inline int +peek_byte(SerdReader* reader) +{ + SerdByteSource* source = &reader->source; + + return source->eof ? EOF : (int)source->read_buf[source->read_head]; +} + +static inline int +eat_byte_safe(SerdReader* reader, const int byte) +{ + (void)byte; + + const int c = peek_byte(reader); + assert(c == byte); + + serd_byte_source_advance(&reader->source); + return c; +} + +static inline int +eat_byte_check(SerdReader* reader, const int byte) +{ + const int c = peek_byte(reader); + if (c != byte) { + r_err(reader, SERD_ERR_BAD_SYNTAX, "expected `%c', not `%c'\n", byte, c); + return 0; + } + return eat_byte_safe(reader, byte); +} + +static inline SerdStatus +eat_string(SerdReader* reader, const char* str, unsigned n) +{ + for (unsigned i = 0; i < n; ++i) { + if (!eat_byte_check(reader, ((const uint8_t*)str)[i])) { + return SERD_ERR_BAD_SYNTAX; + } + } + return SERD_SUCCESS; +} + +static inline SerdStatus +push_byte(SerdReader* reader, Ref ref, const int c) +{ + assert(c != EOF); + SERD_STACK_ASSERT_TOP(reader, ref); + + uint8_t* const s = (uint8_t*)serd_stack_push(&reader->stack, 1); + SerdNode* const node = (SerdNode*)(reader->stack.buf + ref); + + ++node->n_bytes; + if (!(c & 0x80)) { // Starts with 0 bit, start of new character + ++node->n_chars; + } + + *(s - 1) = (uint8_t)c; + *s = '\0'; + return SERD_SUCCESS; +} + +static inline void +push_bytes(SerdReader* reader, Ref ref, const uint8_t* bytes, unsigned len) +{ + for (unsigned i = 0; i < len; ++i) { + push_byte(reader, ref, bytes[i]); + } +} + +#endif // SERD_READER_H diff --git a/modules/juce_audio_processors/format_types/lv2/serd/src/serd_config.h b/modules/juce_audio_processors/format_types/lv2/serd/src/serd_config.h new file mode 100644 index 0000000000..349d71245c --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/serd/src/serd_config.h @@ -0,0 +1,98 @@ +/* + Copyright 2021 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +/* + Configuration header that defines reasonable defaults at compile time. + + This allows compile-time configuration from the command line (typically via + the build system) while still allowing the source to be built without any + configuration. The build system can define SERD_NO_DEFAULT_CONFIG to disable + defaults, in which case it must define things like HAVE_FEATURE to enable + features. The design here ensures that compiler warnings or + include-what-you-use will catch any mistakes. +*/ + +#ifndef SERD_CONFIG_H +#define SERD_CONFIG_H + +// Define version unconditionally so a warning will catch a mismatch +#define SERD_VERSION "0.30.10" + +#if !defined(SERD_NO_DEFAULT_CONFIG) + +// We need unistd.h to check _POSIX_VERSION +# ifndef SERD_NO_POSIX +# ifdef __has_include +# if __has_include() +# include +# endif +# elif defined(__unix__) +# include +# endif +# endif + +// POSIX.1-2001: fileno() +# ifndef HAVE_FILENO +# if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L +# define HAVE_FILENO +# endif +# endif + +// POSIX.1-2001: posix_fadvise() +# ifndef HAVE_POSIX_FADVISE +# ifndef __APPLE__ +# if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L +# define HAVE_POSIX_FADVISE +# endif +# endif +# endif + +// POSIX.1-2001: posix_memalign() +# ifndef HAVE_POSIX_MEMALIGN +# if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L +# define HAVE_POSIX_MEMALIGN +# endif +# endif + +#endif // !defined(SERD_NO_DEFAULT_CONFIG) + +/* + Make corresponding USE_FEATURE defines based on the HAVE_FEATURE defines from + above or the command line. The code checks for these using #if (not #ifdef), + so there will be an undefined warning if it checks for an unknown feature, + and this header is always required by any code that checks for features, even + if the build system defines them all. +*/ + +#ifdef HAVE_FILENO +# define USE_FILENO 1 +#else +# define USE_FILENO 0 +#endif + +#ifdef HAVE_POSIX_FADVISE +# define USE_POSIX_FADVISE 1 +#else +# define USE_POSIX_FADVISE 0 +#endif + +#ifdef HAVE_POSIX_MEMALIGN +# define USE_POSIX_MEMALIGN 1 +#else +# define USE_POSIX_MEMALIGN 0 +#endif + +#endif // SERD_CONFIG_H diff --git a/modules/juce_audio_processors/format_types/lv2/serd/src/serd_internal.h b/modules/juce_audio_processors/format_types/lv2/serd/src/serd_internal.h new file mode 100644 index 0000000000..139e665932 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/serd/src/serd_internal.h @@ -0,0 +1,46 @@ +/* + Copyright 2011-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef SERD_INTERNAL_H +#define SERD_INTERNAL_H + +#include "serd/serd.h" + +#include + +#define NS_XSD "http://www.w3.org/2001/XMLSchema#" +#define NS_RDF "http://www.w3.org/1999/02/22-rdf-syntax-ns#" + +#define SERD_PAGE_SIZE 4096 + +#ifndef MIN +# define MIN(a, b) (((a) < (b)) ? (a) : (b)) +#endif + +/* Error reporting */ + +static inline void +serd_error(SerdErrorSink error_sink, void* handle, const SerdError* e) +{ + if (error_sink) { + error_sink(handle, e); + } else { + fprintf(stderr, "error: %s:%u:%u: ", e->filename, e->line, e->col); + vfprintf(stderr, e->fmt, *e->args); + } +} + +#endif // SERD_INTERNAL_H diff --git a/modules/juce_audio_processors/format_types/lv2/serd/src/serdi.c b/modules/juce_audio_processors/format_types/lv2/serd/src/serdi.c new file mode 100644 index 0000000000..a5c3c0daa9 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/serd/src/serdi.c @@ -0,0 +1,377 @@ +/* + Copyright 2011-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#define _POSIX_C_SOURCE 200809L /* for fileno and posix_fadvise */ + +#include "serd_config.h" +#include "string_utils.h" + +#include "serd/serd.h" + +#ifdef _WIN32 +# ifdef _MSC_VER +# define WIN32_LEAN_AND_MEAN 1 +# endif +# include +# include +#endif + +#if USE_POSIX_FADVISE && USE_FILENO +# include +#endif + +#include +#include +#include +#include +#include +#include + +#define SERDI_ERROR(msg) fprintf(stderr, "serdi: " msg) +#define SERDI_ERRORF(fmt, ...) fprintf(stderr, "serdi: " fmt, __VA_ARGS__) + +typedef struct { + SerdSyntax syntax; + const char* name; + const char* extension; +} Syntax; + +static const Syntax syntaxes[] = {{SERD_TURTLE, "turtle", ".ttl"}, + {SERD_NTRIPLES, "ntriples", ".nt"}, + {SERD_NQUADS, "nquads", ".nq"}, + {SERD_TRIG, "trig", ".trig"}, + {(SerdSyntax)0, NULL, NULL}}; + +static SerdSyntax +get_syntax(const char* name) +{ + for (const Syntax* s = syntaxes; s->name; ++s) { + if (!serd_strncasecmp(s->name, name, strlen(name))) { + return s->syntax; + } + } + + SERDI_ERRORF("unknown syntax `%s'\n", name); + return (SerdSyntax)0; +} + +static SERD_PURE_FUNC SerdSyntax +guess_syntax(const char* filename) +{ + const char* ext = strrchr(filename, '.'); + if (ext) { + for (const Syntax* s = syntaxes; s->name; ++s) { + if (!serd_strncasecmp(s->extension, ext, strlen(ext))) { + return s->syntax; + } + } + } + + return (SerdSyntax)0; +} + +static int +print_version(void) +{ + printf("serdi " SERD_VERSION " \n"); + printf("Copyright 2011-2021 David Robillard .\n" + "License: \n" + "This is free software; you are free to change and redistribute it." + "\nThere is NO WARRANTY, to the extent permitted by law.\n"); + return 0; +} + +static int +print_usage(const char* name, bool error) +{ + FILE* const os = error ? stderr : stdout; + fprintf(os, "%s", error ? "\n" : ""); + fprintf(os, "Usage: %s [OPTION]... INPUT [BASE_URI]\n", name); + fprintf(os, "Read and write RDF syntax.\n"); + fprintf(os, "Use - for INPUT to read from standard input.\n\n"); + fprintf(os, " -a Write ASCII output if possible.\n"); + fprintf(os, " -b Fast bulk output for large serialisations.\n"); + fprintf(os, " -c PREFIX Chop PREFIX from matching blank node IDs.\n"); + fprintf(os, " -e Eat input one character at a time.\n"); + fprintf(os, " -f Keep full URIs in input (don't qualify).\n"); + fprintf(os, " -h Display this help and exit.\n"); + fprintf(os, " -i SYNTAX Input syntax: turtle/ntriples/trig/nquads.\n"); + fprintf(os, " -l Lax (non-strict) parsing.\n"); + fprintf(os, " -o SYNTAX Output syntax: turtle/ntriples/nquads.\n"); + fprintf(os, " -p PREFIX Add PREFIX to blank node IDs.\n"); + fprintf(os, " -q Suppress all output except data.\n"); + fprintf(os, " -r ROOT_URI Keep relative URIs within ROOT_URI.\n"); + fprintf(os, " -s INPUT Parse INPUT as string (terminates options).\n"); + fprintf(os, " -v Display version information and exit.\n"); + return error ? 1 : 0; +} + +static int +missing_arg(const char* name, char opt) +{ + SERDI_ERRORF("option requires an argument -- '%c'\n", opt); + return print_usage(name, true); +} + +static SerdStatus +quiet_error_sink(void* handle, const SerdError* e) +{ + (void)handle; + (void)e; + return SERD_SUCCESS; +} + +static inline FILE* +serd_fopen(const char* path, const char* mode) +{ + FILE* fd = fopen(path, mode); + if (!fd) { + SERDI_ERRORF("failed to open file %s (%s)\n", path, strerror(errno)); + return NULL; + } + +#if USE_POSIX_FADVISE && USE_FILENO + posix_fadvise(fileno(fd), 0, 0, POSIX_FADV_SEQUENTIAL | POSIX_FADV_NOREUSE); +#endif + + return fd; +} + +static SerdStyle +choose_style(const SerdSyntax input_syntax, + const SerdSyntax output_syntax, + const bool ascii, + const bool bulk_write, + const bool full_uris) +{ + unsigned output_style = 0u; + if (output_syntax == SERD_NTRIPLES || ascii) { + output_style |= SERD_STYLE_ASCII; + } else if (output_syntax == SERD_TURTLE) { + output_style |= SERD_STYLE_ABBREVIATED; + if (!full_uris) { + output_style |= SERD_STYLE_CURIED; + } + } + + if ((input_syntax == SERD_TURTLE || input_syntax == SERD_TRIG) || + (output_style & SERD_STYLE_CURIED)) { + // Base URI may change and/or we're abbreviating URIs, so must resolve + output_style |= SERD_STYLE_RESOLVED; + } + + if (bulk_write) { + output_style |= SERD_STYLE_BULK; + } + + return (SerdStyle)output_style; +} + +int +main(int argc, char** argv) +{ + if (argc < 2) { + return print_usage(argv[0], true); + } + + FILE* in_fd = NULL; + SerdSyntax input_syntax = (SerdSyntax)0; + SerdSyntax output_syntax = (SerdSyntax)0; + bool from_file = true; + bool ascii = false; + bool bulk_read = true; + bool bulk_write = false; + bool full_uris = false; + bool lax = false; + bool quiet = false; + const uint8_t* in_name = NULL; + const uint8_t* add_prefix = NULL; + const uint8_t* chop_prefix = NULL; + const uint8_t* root_uri = NULL; + int a = 1; + for (; a < argc && argv[a][0] == '-'; ++a) { + if (argv[a][1] == '\0') { + in_name = (const uint8_t*)"(stdin)"; + in_fd = stdin; + break; + } + + if (argv[a][1] == 'a') { + ascii = true; + } else if (argv[a][1] == 'b') { + bulk_write = true; + } else if (argv[a][1] == 'e') { + bulk_read = false; + } else if (argv[a][1] == 'f') { + full_uris = true; + } else if (argv[a][1] == 'h') { + return print_usage(argv[0], false); + } else if (argv[a][1] == 'l') { + lax = true; + } else if (argv[a][1] == 'q') { + quiet = true; + } else if (argv[a][1] == 'v') { + return print_version(); + } else if (argv[a][1] == 's') { + in_name = (const uint8_t*)"(string)"; + from_file = false; + ++a; + break; + } else if (argv[a][1] == 'i') { + if (++a == argc) { + return missing_arg(argv[0], 'i'); + } + + if (!(input_syntax = get_syntax(argv[a]))) { + return print_usage(argv[0], true); + } + } else if (argv[a][1] == 'o') { + if (++a == argc) { + return missing_arg(argv[0], 'o'); + } + + if (!(output_syntax = get_syntax(argv[a]))) { + return print_usage(argv[0], true); + } + } else if (argv[a][1] == 'p') { + if (++a == argc) { + return missing_arg(argv[0], 'p'); + } + + add_prefix = (const uint8_t*)argv[a]; + } else if (argv[a][1] == 'c') { + if (++a == argc) { + return missing_arg(argv[0], 'c'); + } + + chop_prefix = (const uint8_t*)argv[a]; + } else if (argv[a][1] == 'r') { + if (++a == argc) { + return missing_arg(argv[0], 'r'); + } + + root_uri = (const uint8_t*)argv[a]; + } else { + SERDI_ERRORF("invalid option -- '%s'\n", argv[a] + 1); + return print_usage(argv[0], true); + } + } + + if (a == argc) { + SERDI_ERROR("missing input\n"); + return 1; + } + +#ifdef _WIN32 + _setmode(_fileno(stdin), _O_BINARY); + _setmode(_fileno(stdout), _O_BINARY); +#endif + + uint8_t* input_path = NULL; + const uint8_t* input = (const uint8_t*)argv[a++]; + if (from_file) { + in_name = in_name ? in_name : input; + if (!in_fd) { + if (!strncmp((const char*)input, "file:", 5)) { + input_path = serd_file_uri_parse(input, NULL); + input = input_path; + } + if (!input || !(in_fd = serd_fopen((const char*)input, "rb"))) { + return 1; + } + } + } + + if (!input_syntax && !(input_syntax = guess_syntax((const char*)in_name))) { + input_syntax = SERD_TRIG; + } + + if (!output_syntax) { + output_syntax = + ((input_syntax == SERD_TURTLE || input_syntax == SERD_NTRIPLES) + ? SERD_NTRIPLES + : SERD_NQUADS); + } + + const SerdStyle output_style = + choose_style(input_syntax, output_syntax, ascii, bulk_write, full_uris); + + SerdURI base_uri = SERD_URI_NULL; + SerdNode base = SERD_NODE_NULL; + if (a < argc) { // Base URI given on command line + base = + serd_node_new_uri_from_string((const uint8_t*)argv[a], NULL, &base_uri); + } else if (from_file && in_fd != stdin) { // Use input file URI + base = serd_node_new_file_uri(input, NULL, &base_uri, true); + } + + FILE* const out_fd = stdout; + SerdEnv* const env = serd_env_new(&base); + + SerdWriter* const writer = serd_writer_new( + output_syntax, output_style, env, &base_uri, serd_file_sink, out_fd); + + SerdReader* const reader = + serd_reader_new(input_syntax, + writer, + NULL, + (SerdBaseSink)serd_writer_set_base_uri, + (SerdPrefixSink)serd_writer_set_prefix, + (SerdStatementSink)serd_writer_write_statement, + (SerdEndSink)serd_writer_end_anon); + + serd_reader_set_strict(reader, !lax); + if (quiet) { + serd_reader_set_error_sink(reader, quiet_error_sink, NULL); + serd_writer_set_error_sink(writer, quiet_error_sink, NULL); + } + + SerdNode root = serd_node_from_string(SERD_URI, root_uri); + serd_writer_set_root_uri(writer, &root); + serd_writer_chop_blank_prefix(writer, chop_prefix); + serd_reader_add_blank_prefix(reader, add_prefix); + + SerdStatus st = SERD_SUCCESS; + if (!from_file) { + st = serd_reader_read_string(reader, input); + } else if (bulk_read) { + st = serd_reader_read_file_handle(reader, in_fd, in_name); + } else { + st = serd_reader_start_stream(reader, in_fd, in_name, false); + while (!st) { + st = serd_reader_read_chunk(reader); + } + serd_reader_end_stream(reader); + } + + serd_reader_free(reader); + serd_writer_finish(writer); + serd_writer_free(writer); + serd_env_free(env); + serd_node_free(&base); + free(input_path); + + if (from_file) { + fclose(in_fd); + } + + if (fclose(out_fd)) { + perror("serdi: write error"); + st = SERD_ERR_UNKNOWN; + } + + return (st > SERD_FAILURE) ? 1 : 0; +} diff --git a/modules/juce_audio_processors/format_types/lv2/serd/src/stack.h b/modules/juce_audio_processors/format_types/lv2/serd/src/stack.h new file mode 100644 index 0000000000..fef33390e6 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/serd/src/stack.h @@ -0,0 +1,119 @@ +/* + Copyright 2011-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef SERD_STACK_H +#define SERD_STACK_H + +#include +#include +#include +#include +#include + +/** An offset to start the stack at. Note 0 is reserved for NULL. */ +#define SERD_STACK_BOTTOM sizeof(void*) + +/** A dynamic stack in memory. */ +typedef struct { + uint8_t* buf; ///< Stack memory + size_t buf_size; ///< Allocated size of buf (>= size) + size_t size; ///< Conceptual size of stack in buf +} SerdStack; + +/** An offset to start the stack at. Note 0 is reserved for NULL. */ +#define SERD_STACK_BOTTOM sizeof(void*) + +static inline SerdStack +serd_stack_new(size_t size) +{ + SerdStack stack; + stack.buf = (uint8_t*)calloc(size, 1); + stack.buf_size = size; + stack.size = SERD_STACK_BOTTOM; + return stack; +} + +static inline bool +serd_stack_is_empty(SerdStack* stack) +{ + return stack->size <= SERD_STACK_BOTTOM; +} + +static inline void +serd_stack_free(SerdStack* stack) +{ + free(stack->buf); + stack->buf = NULL; + stack->buf_size = 0; + stack->size = 0; +} + +static inline void* +serd_stack_push(SerdStack* stack, size_t n_bytes) +{ + const size_t new_size = stack->size + n_bytes; + if (stack->buf_size < new_size) { + stack->buf_size += (stack->buf_size >> 1); // *= 1.5 + stack->buf = (uint8_t*)realloc(stack->buf, stack->buf_size); + } + + uint8_t* const ret = (stack->buf + stack->size); + + stack->size = new_size; + return ret; +} + +static inline void +serd_stack_pop(SerdStack* stack, size_t n_bytes) +{ + assert(stack->size >= n_bytes); + stack->size -= n_bytes; +} + +static inline void* +serd_stack_push_aligned(SerdStack* stack, size_t n_bytes, size_t align) +{ + // Push one byte to ensure space for a pad count + serd_stack_push(stack, 1); + + // Push padding if necessary + const size_t pad = align - stack->size % align; + if (pad > 0) { + serd_stack_push(stack, pad); + } + + // Set top of stack to pad count so we can properly pop later + assert(pad < UINT8_MAX); + stack->buf[stack->size - 1] = (uint8_t)pad; + + // Push requested space at aligned location + return serd_stack_push(stack, n_bytes); +} + +static inline void +serd_stack_pop_aligned(SerdStack* stack, size_t n_bytes) +{ + // Pop requested space down to aligned location + serd_stack_pop(stack, n_bytes); + + // Get amount of padding from top of stack + const uint8_t pad = stack->buf[stack->size - 1]; + + // Pop padding and pad count + serd_stack_pop(stack, pad + 1u); +} + +#endif // SERD_STACK_H diff --git a/modules/juce_audio_processors/format_types/lv2/serd/src/string.c b/modules/juce_audio_processors/format_types/lv2/serd/src/string.c new file mode 100644 index 0000000000..b83ad58fb6 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/serd/src/string.c @@ -0,0 +1,179 @@ +/* + Copyright 2011-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "string_utils.h" + +#include "serd/serd.h" + +#include +#include +#include + +void +serd_free(void* ptr) +{ + free(ptr); +} + +const uint8_t* +serd_strerror(SerdStatus status) +{ + switch (status) { + case SERD_SUCCESS: + return (const uint8_t*)"Success"; + case SERD_FAILURE: + return (const uint8_t*)"Non-fatal failure"; + case SERD_ERR_UNKNOWN: + return (const uint8_t*)"Unknown error"; + case SERD_ERR_BAD_SYNTAX: + return (const uint8_t*)"Invalid syntax"; + case SERD_ERR_BAD_ARG: + return (const uint8_t*)"Invalid argument"; + case SERD_ERR_NOT_FOUND: + return (const uint8_t*)"Not found"; + case SERD_ERR_ID_CLASH: + return (const uint8_t*)"Blank node ID clash"; + case SERD_ERR_BAD_CURIE: + return (const uint8_t*)"Invalid CURIE"; + case SERD_ERR_INTERNAL: + return (const uint8_t*)"Internal error"; + default: + break; + } + return (const uint8_t*)"Unknown error"; // never reached +} + +static inline void +serd_update_flags(const uint8_t c, SerdNodeFlags* const flags) +{ + switch (c) { + case '\r': + case '\n': + *flags |= SERD_HAS_NEWLINE; + break; + case '"': + *flags |= SERD_HAS_QUOTE; + default: + break; + } +} + +size_t +serd_substrlen(const uint8_t* const str, + const size_t len, + size_t* const n_bytes, + SerdNodeFlags* const flags) +{ + size_t n_chars = 0; + size_t i = 0; + SerdNodeFlags f = 0; + for (; i < len && str[i]; ++i) { + if ((str[i] & 0xC0) != 0x80) { // Start of new character + ++n_chars; + serd_update_flags(str[i], &f); + } + } + if (n_bytes) { + *n_bytes = i; + } + if (flags) { + *flags = f; + } + return n_chars; +} + +size_t +serd_strlen(const uint8_t* str, size_t* n_bytes, SerdNodeFlags* flags) +{ + size_t n_chars = 0; + size_t i = 0; + SerdNodeFlags f = 0; + for (; str[i]; ++i) { + if ((str[i] & 0xC0) != 0x80) { // Start of new character + ++n_chars; + serd_update_flags(str[i], &f); + } + } + if (n_bytes) { + *n_bytes = i; + } + if (flags) { + *flags = f; + } + return n_chars; +} + +static inline double +read_sign(const char** sptr) +{ + double sign = 1.0; + switch (**sptr) { + case '-': + sign = -1.0; + // fallthru + case '+': + ++(*sptr); + // fallthru + default: + return sign; + } +} + +double +serd_strtod(const char* str, char** endptr) +{ + double result = 0.0; + + // Point s at the first non-whitespace character + const char* s = str; + while (is_space(*s)) { + ++s; + } + + // Read leading sign if necessary + const double sign = read_sign(&s); + + // Parse integer part + for (; is_digit(*s); ++s) { + result = (result * 10.0) + (*s - '0'); + } + + // Parse fractional part + if (*s == '.') { + double denom = 10.0; + for (++s; is_digit(*s); ++s) { + result += (*s - '0') / denom; + denom *= 10.0; + } + } + + // Parse exponent + if (*s == 'e' || *s == 'E') { + ++s; + double expt = 0.0; + double expt_sign = read_sign(&s); + for (; is_digit(*s); ++s) { + expt = (expt * 10.0) + (*s - '0'); + } + result *= pow(10, expt * expt_sign); + } + + if (endptr) { + *endptr = (char*)s; + } + + return result * sign; +} diff --git a/modules/juce_audio_processors/format_types/lv2/serd/src/string_utils.h b/modules/juce_audio_processors/format_types/lv2/serd/src/string_utils.h new file mode 100644 index 0000000000..ac2b40c598 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/serd/src/string_utils.h @@ -0,0 +1,173 @@ +/* + Copyright 2011-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef SERD_STRING_UTILS_H +#define SERD_STRING_UTILS_H + +#include "serd/serd.h" + +#include +#include +#include + +/** Unicode replacement character in UTF-8 */ +static const uint8_t replacement_char[] = {0xEF, 0xBF, 0xBD}; + +/** Return true if `c` lies within [`min`...`max`] (inclusive) */ +static inline bool +in_range(const int c, const int min, const int max) +{ + return (c >= min && c <= max); +} + +/** RFC2234: ALPHA ::= %x41-5A / %x61-7A ; A-Z / a-z */ +static inline bool +is_alpha(const int c) +{ + return in_range(c, 'A', 'Z') || in_range(c, 'a', 'z'); +} + +/** RFC2234: DIGIT ::= %x30-39 ; 0-9 */ +static inline bool +is_digit(const int c) +{ + return in_range(c, '0', '9'); +} + +/* RFC2234: HEXDIG ::= DIGIT / "A" / "B" / "C" / "D" / "E" / "F" */ +static inline bool +is_hexdig(const int c) +{ + return is_digit(c) || in_range(c, 'A', 'F'); +} + +/* Turtle / JSON / C: XDIGIT ::= DIGIT / A-F / a-f */ +static inline bool +is_xdigit(const int c) +{ + return is_hexdig(c) || in_range(c, 'a', 'f'); +} + +static inline bool +is_space(const char c) +{ + switch (c) { + case ' ': + case '\f': + case '\n': + case '\r': + case '\t': + case '\v': + return true; + default: + return false; + } +} + +static inline bool +is_print(const int c) +{ + return c >= 0x20 && c <= 0x7E; +} + +static inline bool +is_base64(const uint8_t c) +{ + return is_alpha(c) || is_digit(c) || c == '+' || c == '/' || c == '='; +} + +static inline bool +is_windows_path(const uint8_t* path) +{ + return is_alpha(path[0]) && (path[1] == ':' || path[1] == '|') && + (path[2] == '/' || path[2] == '\\'); +} + +size_t +serd_substrlen(const uint8_t* str, + size_t len, + size_t* n_bytes, + SerdNodeFlags* flags); + +static inline char +serd_to_upper(const char c) +{ + return (char)((c >= 'a' && c <= 'z') ? c - 32 : c); +} + +static inline int +serd_strncasecmp(const char* s1, const char* s2, size_t n) +{ + for (; n > 0 && *s2; s1++, s2++, --n) { + if (serd_to_upper(*s1) != serd_to_upper(*s2)) { + return ((*(const uint8_t*)s1 < *(const uint8_t*)s2) ? -1 : +1); + } + } + + return 0; +} + +static inline uint32_t +utf8_num_bytes(const uint8_t c) +{ + if ((c & 0x80) == 0) { // Starts with `0' + return 1; + } + + if ((c & 0xE0) == 0xC0) { // Starts with `110' + return 2; + } + + if ((c & 0xF0) == 0xE0) { // Starts with `1110' + return 3; + } + + if ((c & 0xF8) == 0xF0) { // Starts with `11110' + return 4; + } + + return 0; +} + +/// Return the code point of a UTF-8 character with known length +static inline uint32_t +parse_counted_utf8_char(const uint8_t* utf8, size_t size) +{ + uint32_t c = utf8[0] & ((1u << (8 - size)) - 1); + for (size_t i = 1; i < size; ++i) { + const uint8_t in = utf8[i] & 0x3F; + c = (c << 6) | in; + } + return c; +} + +/// Parse a UTF-8 character, set *size to the length, and return the code point +static inline uint32_t +parse_utf8_char(const uint8_t* utf8, size_t* size) +{ + switch (*size = utf8_num_bytes(utf8[0])) { + case 1: + case 2: + case 3: + case 4: + return parse_counted_utf8_char(utf8, *size); + default: + *size = 0; + return 0; + } +} + +#endif // SERD_STRING_UTILS_H diff --git a/modules/juce_audio_processors/format_types/lv2/serd/src/system.c b/modules/juce_audio_processors/format_types/lv2/serd/src/system.c new file mode 100644 index 0000000000..6bc93ca6be --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/serd/src/system.c @@ -0,0 +1,82 @@ +/* + Copyright 2011-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#define _POSIX_C_SOURCE 200809L /* for posix_memalign and posix_fadvise */ + +#include "system.h" + +#include "serd_config.h" +#include "serd_internal.h" + +#if USE_POSIX_FADVISE && USE_FILENO +# include +#endif + +#ifdef _WIN32 +# include +#endif + +#include +#include +#include +#include + +FILE* +serd_fopen(const char* path, const char* mode) +{ + FILE* fd = fopen(path, mode); + if (!fd) { + fprintf( + stderr, "error: failed to open file %s (%s)\n", path, strerror(errno)); + return NULL; + } + +#if USE_POSIX_FADVISE && USE_FILENO + posix_fadvise(fileno(fd), 0, 0, POSIX_FADV_SEQUENTIAL); +#endif + return fd; +} + +void* +serd_malloc_aligned(const size_t alignment, const size_t size) +{ +#if defined(_WIN32) + return _aligned_malloc(size, alignment); +#elif USE_POSIX_MEMALIGN + void* ptr = NULL; + const int ret = posix_memalign(&ptr, alignment, size); + return ret ? NULL : ptr; +#else + (void)alignment; + return malloc(size); +#endif +} + +void* +serd_allocate_buffer(const size_t size) +{ + return serd_malloc_aligned(SERD_PAGE_SIZE, size); +} + +void +serd_free_aligned(void* const ptr) +{ +#ifdef _WIN32 + _aligned_free(ptr); +#else + free(ptr); +#endif +} diff --git a/modules/juce_audio_processors/format_types/lv2/serd/src/system.h b/modules/juce_audio_processors/format_types/lv2/serd/src/system.h new file mode 100644 index 0000000000..fdfab4a458 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/serd/src/system.h @@ -0,0 +1,40 @@ +/* + Copyright 2011-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef SERD_SYSTEM_H +#define SERD_SYSTEM_H + +#include "attributes.h" + +#include + +/// Open a file configured for fast sequential reading +FILE* +serd_fopen(const char* path, const char* mode); + +/// Allocate a buffer aligned to `alignment` bytes +SERD_MALLOC_FUNC void* +serd_malloc_aligned(size_t alignment, size_t size); + +/// Allocate an aligned buffer for I/O +SERD_MALLOC_FUNC void* +serd_allocate_buffer(size_t size); + +/// Free a buffer allocated with an aligned allocation function +void +serd_free_aligned(void* ptr); + +#endif // SERD_SYSTEM_H diff --git a/modules/juce_audio_processors/format_types/lv2/serd/src/uri.c b/modules/juce_audio_processors/format_types/lv2/serd/src/uri.c new file mode 100644 index 0000000000..b70992ffc0 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/serd/src/uri.c @@ -0,0 +1,506 @@ +/* + Copyright 2011-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "string_utils.h" +#include "uri_utils.h" + +#include "serd/serd.h" + +#include +#include +#include +#include +#include + +const uint8_t* +serd_uri_to_path(const uint8_t* uri) +{ + const uint8_t* path = uri; + if (!is_windows_path(uri) && serd_uri_string_has_scheme(uri)) { + if (strncmp((const char*)uri, "file:", 5)) { + fprintf(stderr, "Non-file URI `%s'\n", uri); + return NULL; + } + + if (!strncmp((const char*)uri, "file://localhost/", 17)) { + path = uri + 16; + } else if (!strncmp((const char*)uri, "file://", 7)) { + path = uri + 7; + } else { + fprintf(stderr, "Invalid file URI `%s'\n", uri); + return NULL; + } + + if (is_windows_path(path + 1)) { + ++path; // Special case for terrible Windows file URIs + } + } + return path; +} + +uint8_t* +serd_file_uri_parse(const uint8_t* uri, uint8_t** hostname) +{ + const uint8_t* path = uri; + if (hostname) { + *hostname = NULL; + } + if (!strncmp((const char*)uri, "file://", 7)) { + const uint8_t* auth = uri + 7; + if (*auth == '/') { // No hostname + path = auth; + } else { // Has hostname + if (!(path = (const uint8_t*)strchr((const char*)auth, '/'))) { + return NULL; + } + + if (hostname) { + *hostname = (uint8_t*)calloc((size_t)(path - auth + 1), 1); + memcpy(*hostname, auth, (size_t)(path - auth)); + } + } + } + + if (is_windows_path(path + 1)) { + ++path; + } + + SerdChunk chunk = {NULL, 0}; + for (const uint8_t* s = path; *s; ++s) { + if (*s == '%') { + if (*(s + 1) == '%') { + serd_chunk_sink("%", 1, &chunk); + ++s; + } else if (is_hexdig(*(s + 1)) && is_hexdig(*(s + 2))) { + const uint8_t code[3] = {*(s + 1), *(s + 2), 0}; + const uint8_t c = (uint8_t)strtoul((const char*)code, NULL, 16); + serd_chunk_sink(&c, 1, &chunk); + s += 2; + } else { + s += 2; // Junk escape, ignore + } + } else { + serd_chunk_sink(s, 1, &chunk); + } + } + + return serd_chunk_sink_finish(&chunk); +} + +bool +serd_uri_string_has_scheme(const uint8_t* utf8) +{ + // RFC3986: scheme ::= ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) + if (!utf8 || !is_alpha(utf8[0])) { + return false; // Invalid scheme initial character, URI is relative + } + + for (uint8_t c = 0; (c = *++utf8) != '\0';) { + if (!is_uri_scheme_char(c)) { + return false; + } + + if (c == ':') { + return true; // End of scheme + } + } + + return false; +} + +SerdStatus +serd_uri_parse(const uint8_t* utf8, SerdURI* out) +{ + *out = SERD_URI_NULL; + + const uint8_t* ptr = utf8; + + /* See http://tools.ietf.org/html/rfc3986#section-3 + URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ] + */ + + /* S3.1: scheme ::= ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) */ + if (is_alpha(*ptr)) { + for (uint8_t c = *++ptr; true; c = *++ptr) { + switch (c) { + case '\0': + case '/': + case '?': + case '#': + ptr = utf8; + goto path; // Relative URI (starts with path by definition) + case ':': + out->scheme.buf = utf8; + out->scheme.len = (size_t)((ptr++) - utf8); + goto maybe_authority; // URI with scheme + case '+': + case '-': + case '.': + continue; + default: + if (is_alpha(c) || is_digit(c)) { + continue; + } + } + } + } + + /* S3.2: The authority component is preceded by a double slash ("//") + and is terminated by the next slash ("/"), question mark ("?"), + or number sign ("#") character, or by the end of the URI. + */ +maybe_authority: + if (*ptr == '/' && *(ptr + 1) == '/') { + ptr += 2; + out->authority.buf = ptr; + for (uint8_t c = 0; (c = *ptr) != '\0'; ++ptr) { + switch (c) { + case '/': + goto path; + case '?': + goto query; + case '#': + goto fragment; + default: + ++out->authority.len; + } + } + } + + /* RFC3986 S3.3: The path is terminated by the first question mark ("?") + or number sign ("#") character, or by the end of the URI. + */ +path: + switch (*ptr) { + case '?': + goto query; + case '#': + goto fragment; + case '\0': + goto end; + default: + break; + } + out->path.buf = ptr; + out->path.len = 0; + for (uint8_t c = 0; (c = *ptr) != '\0'; ++ptr) { + switch (c) { + case '?': + goto query; + case '#': + goto fragment; + default: + ++out->path.len; + } + } + + /* RFC3986 S3.4: The query component is indicated by the first question + mark ("?") character and terminated by a number sign ("#") character + or by the end of the URI. + */ +query: + if (*ptr == '?') { + out->query.buf = ++ptr; + for (uint8_t c = 0; (c = *ptr) != '\0'; ++ptr) { + if (c == '#') { + goto fragment; + } + ++out->query.len; + } + } + + /* RFC3986 S3.5: A fragment identifier component is indicated by the + presence of a number sign ("#") character and terminated by the end + of the URI. + */ +fragment: + if (*ptr == '#') { + out->fragment.buf = ptr; + while (*ptr++ != '\0') { + ++out->fragment.len; + } + } + +end: + return SERD_SUCCESS; +} + +/** + Remove leading dot components from `path`. + See http://tools.ietf.org/html/rfc3986#section-5.2.3 + @param up Set to the number of up-references (e.g. "../") trimmed + @return A pointer to the new start of `path` +*/ +static const uint8_t* +remove_dot_segments(const uint8_t* path, size_t len, size_t* up) +{ + const uint8_t* begin = path; + const uint8_t* const end = path + len; + + *up = 0; + while (begin < end) { + switch (begin[0]) { + case '.': + switch (begin[1]) { + case '/': + begin += 2; // Chop leading "./" + break; + case '.': + switch (begin[2]) { + case '\0': + ++*up; + begin += 2; // Chop input ".." + break; + case '/': + ++*up; + begin += 3; // Chop leading "../" + break; + default: + return begin; + } + break; + case '\0': + return ++begin; // Chop input "." + default: + return begin; + } + break; + + case '/': + switch (begin[1]) { + case '.': + switch (begin[2]) { + case '/': + begin += 2; // Leading "/./" => "/" + break; + case '.': + switch (begin[3]) { + case '/': + ++*up; + begin += 3; // Leading "/../" => "/" + } + break; + default: + return begin; + } + } + return begin; + + default: + return begin; // Finished chopping dot components + } + } + + return begin; +} + +/// Merge `base` and `path` in-place +static void +merge(SerdChunk* base, SerdChunk* path) +{ + size_t up = 0; + const uint8_t* begin = remove_dot_segments(path->buf, path->len, &up); + const uint8_t* end = path->buf + path->len; + + if (base->len) { + // Find the up'th last slash + const uint8_t* base_last = (base->buf + base->len - 1); + ++up; + do { + if (*base_last == '/') { + --up; + } + } while (up > 0 && (--base_last > base->buf)); + + // Set path prefix + base->len = (size_t)(base_last - base->buf + 1); + } + + // Set path suffix + path->buf = begin; + path->len = (size_t)(end - begin); +} + +/// See http://tools.ietf.org/html/rfc3986#section-5.2.2 +void +serd_uri_resolve(const SerdURI* r, const SerdURI* base, SerdURI* t) +{ + if (!base->scheme.len) { + *t = *r; // Don't resolve against non-absolute URIs + return; + } + + t->path_base.buf = NULL; + t->path_base.len = 0; + if (r->scheme.len) { + *t = *r; + } else { + if (r->authority.len) { + t->authority = r->authority; + t->path = r->path; + t->query = r->query; + } else { + t->path = r->path; + if (!r->path.len) { + t->path_base = base->path; + if (r->query.len) { + t->query = r->query; + } else { + t->query = base->query; + } + } else { + if (r->path.buf[0] != '/') { + t->path_base = base->path; + } + merge(&t->path_base, &t->path); + t->query = r->query; + } + t->authority = base->authority; + } + t->scheme = base->scheme; + t->fragment = r->fragment; + } +} + +/** Write the path of `uri` starting at index `i` */ +static size_t +write_path_tail(SerdSink sink, void* stream, const SerdURI* uri, size_t i) +{ + size_t len = 0; + if (i < uri->path_base.len) { + len += sink(uri->path_base.buf + i, uri->path_base.len - i, stream); + } + + if (uri->path.buf) { + if (i < uri->path_base.len) { + len += sink(uri->path.buf, uri->path.len, stream); + } else { + const size_t j = (i - uri->path_base.len); + len += sink(uri->path.buf + j, uri->path.len - j, stream); + } + } + + return len; +} + +/** Write the path of `uri` relative to the path of `base`. */ +static size_t +write_rel_path(SerdSink sink, + void* stream, + const SerdURI* uri, + const SerdURI* base) +{ + const size_t path_len = uri_path_len(uri); + const size_t base_len = uri_path_len(base); + const size_t min_len = (path_len < base_len) ? path_len : base_len; + + // Find the last separator common to both paths + size_t last_shared_sep = 0; + size_t i = 0; + for (; i < min_len && uri_path_at(uri, i) == uri_path_at(base, i); ++i) { + if (uri_path_at(uri, i) == '/') { + last_shared_sep = i; + } + } + + if (i == path_len && i == base_len) { // Paths are identical + return 0; + } + + // Find the number of up references ("..") required + size_t up = 0; + for (size_t s = last_shared_sep + 1; s < base_len; ++s) { + if (uri_path_at(base, s) == '/') { + ++up; + } + } + + // Write up references + size_t len = 0; + for (size_t u = 0; u < up; ++u) { + len += sink("../", 3, stream); + } + + if (last_shared_sep == 0 && up == 0) { + len += sink("/", 1, stream); + } + + // Write suffix + return len + write_path_tail(sink, stream, uri, last_shared_sep + 1); +} + +static uint8_t +serd_uri_path_starts_without_slash(const SerdURI* uri) +{ + return ((uri->path_base.len || uri->path.len) && + ((!uri->path_base.len || uri->path_base.buf[0] != '/') && + (!uri->path.len || uri->path.buf[0] != '/'))); +} + +/// See http://tools.ietf.org/html/rfc3986#section-5.3 +size_t +serd_uri_serialise_relative(const SerdURI* uri, + const SerdURI* base, + const SerdURI* root, + SerdSink sink, + void* stream) +{ + size_t len = 0; + const bool relative = + root ? uri_is_under(uri, root) : uri_is_related(uri, base); + + if (relative) { + len = write_rel_path(sink, stream, uri, base); + } + + if (!relative || (!len && base->query.buf)) { + if (uri->scheme.buf) { + len += sink(uri->scheme.buf, uri->scheme.len, stream); + len += sink(":", 1, stream); + } + if (uri->authority.buf) { + len += sink("//", 2, stream); + len += sink(uri->authority.buf, uri->authority.len, stream); + if (uri->authority.len > 0 && + uri->authority.buf[uri->authority.len - 1] != '/' && + serd_uri_path_starts_without_slash(uri)) { + // Special case: ensure path begins with a slash + // https://tools.ietf.org/html/rfc3986#section-3.2 + len += sink("/", 1, stream); + } + } + len += write_path_tail(sink, stream, uri, 0); + } + + if (uri->query.buf) { + len += sink("?", 1, stream); + len += sink(uri->query.buf, uri->query.len, stream); + } + + if (uri->fragment.buf) { + // Note uri->fragment.buf includes the leading `#' + len += sink(uri->fragment.buf, uri->fragment.len, stream); + } + + return len; +} + +/// See http://tools.ietf.org/html/rfc3986#section-5.3 +size_t +serd_uri_serialise(const SerdURI* uri, SerdSink sink, void* stream) +{ + return serd_uri_serialise_relative(uri, NULL, NULL, sink, stream); +} diff --git a/modules/juce_audio_processors/format_types/lv2/serd/src/uri_utils.h b/modules/juce_audio_processors/format_types/lv2/serd/src/uri_utils.h new file mode 100644 index 0000000000..2dd4924fe8 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/serd/src/uri_utils.h @@ -0,0 +1,110 @@ +/* + Copyright 2011-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef SERD_URI_UTILS_H +#define SERD_URI_UTILS_H + +#include "serd/serd.h" + +#include "string_utils.h" + +#include +#include +#include + +static inline bool +chunk_equals(const SerdChunk* a, const SerdChunk* b) +{ + return a->len == b->len && + !strncmp((const char*)a->buf, (const char*)b->buf, a->len); +} + +static inline size_t +uri_path_len(const SerdURI* uri) +{ + return uri->path_base.len + uri->path.len; +} + +static inline uint8_t +uri_path_at(const SerdURI* uri, size_t i) +{ + return (i < uri->path_base.len) ? uri->path_base.buf[i] + : uri->path.buf[i - uri->path_base.len]; +} + +/** + Return the index of the first differing character after the last root slash, + or zero if `uri` is not under `root`. +*/ +static inline SERD_PURE_FUNC size_t +uri_rooted_index(const SerdURI* uri, const SerdURI* root) +{ + if (!root || !root->scheme.len || + !chunk_equals(&root->scheme, &uri->scheme) || + !chunk_equals(&root->authority, &uri->authority)) { + return 0; + } + + bool differ = false; + const size_t path_len = uri_path_len(uri); + const size_t root_len = uri_path_len(root); + size_t last_root_slash = 0; + for (size_t i = 0; i < path_len && i < root_len; ++i) { + const uint8_t u = uri_path_at(uri, i); + const uint8_t r = uri_path_at(root, i); + + differ = differ || u != r; + if (r == '/') { + last_root_slash = i; + if (differ) { + return 0; + } + } + } + + return last_root_slash + 1; +} + +/** Return true iff `uri` shares path components with `root` */ +static inline SERD_PURE_FUNC bool +uri_is_related(const SerdURI* uri, const SerdURI* root) +{ + return uri_rooted_index(uri, root) > 0; +} + +/** Return true iff `uri` is within the base of `root` */ +static inline SERD_PURE_FUNC bool +uri_is_under(const SerdURI* uri, const SerdURI* root) +{ + const size_t index = uri_rooted_index(uri, root); + return index > 0 && uri->path.len > index; +} + +static inline bool +is_uri_scheme_char(const int c) +{ + switch (c) { + case ':': + case '+': + case '-': + case '.': + return true; + default: + return is_alpha(c) || is_digit(c); + } +} + +#endif // SERD_URI_UTILS_H diff --git a/modules/juce_audio_processors/format_types/lv2/serd/src/writer.c b/modules/juce_audio_processors/format_types/lv2/serd/src/writer.c new file mode 100644 index 0000000000..82ac5eeb04 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/serd/src/writer.c @@ -0,0 +1,1114 @@ +/* + Copyright 2011-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "byte_sink.h" +#include "serd_internal.h" +#include "stack.h" +#include "string_utils.h" +#include "uri_utils.h" + +#include "serd/serd.h" + +#include +#include +#include +#include +#include +#include +#include + +typedef enum { + FIELD_NONE, + FIELD_SUBJECT, + FIELD_PREDICATE, + FIELD_OBJECT, + FIELD_GRAPH +} Field; + +typedef struct { + SerdNode graph; + SerdNode subject; + SerdNode predicate; +} WriteContext; + +static const WriteContext WRITE_CONTEXT_NULL = {{0, 0, 0, 0, SERD_NOTHING}, + {0, 0, 0, 0, SERD_NOTHING}, + {0, 0, 0, 0, SERD_NOTHING}}; + +typedef enum { + SEP_NONE, + SEP_END_S, ///< End of a subject ('.') + SEP_END_P, ///< End of a predicate (';') + SEP_END_O, ///< End of an object (',') + SEP_S_P, ///< Between a subject and predicate (whitespace) + SEP_P_O, ///< Between a predicate and object (whitespace) + SEP_ANON_BEGIN, ///< Start of anonymous node ('[') + SEP_ANON_END, ///< End of anonymous node (']') + SEP_LIST_BEGIN, ///< Start of list ('(') + SEP_LIST_SEP, ///< List separator (whitespace) + SEP_LIST_END, ///< End of list (')') + SEP_GRAPH_BEGIN, ///< Start of graph ('{') + SEP_GRAPH_END, ///< End of graph ('}') + SEP_URI_BEGIN, ///< URI start quote ('<') + SEP_URI_END ///< URI end quote ('>') +} Sep; + +typedef struct { + const char* str; ///< Sep string + uint8_t len; ///< Length of sep string + uint8_t space_before; ///< Newline before sep + uint8_t space_after_node; ///< Newline after sep if after node + uint8_t space_after_sep; ///< Newline after sep if after sep +} SepRule; + +static const SepRule rules[] = {{NULL, 0, 0, 0, 0}, + {" .\n\n", 4, 0, 0, 0}, + {" ;", 2, 0, 1, 1}, + {" ,", 2, 0, 1, 0}, + {NULL, 0, 0, 1, 0}, + {" ", 1, 0, 0, 0}, + {"[", 1, 0, 1, 1}, + {"]", 1, 1, 0, 0}, + {"(", 1, 0, 0, 0}, + {NULL, 0, 0, 1, 0}, + {")", 1, 1, 0, 0}, + {" {", 2, 0, 1, 1}, + {" }", 2, 0, 1, 1}, + {"<", 1, 0, 0, 0}, + {">", 1, 0, 0, 0}, + {"\n", 1, 0, 1, 0}}; + +struct SerdWriterImpl { + SerdSyntax syntax; + SerdStyle style; + SerdEnv* env; + SerdNode root_node; + SerdURI root_uri; + SerdURI base_uri; + SerdStack anon_stack; + SerdByteSink byte_sink; + SerdErrorSink error_sink; + void* error_handle; + WriteContext context; + SerdNode list_subj; + unsigned list_depth; + unsigned indent; + uint8_t* bprefix; + size_t bprefix_len; + Sep last_sep; + bool empty; +}; + +typedef enum { WRITE_STRING, WRITE_LONG_STRING } TextContext; + +static bool +write_node(SerdWriter* writer, + const SerdNode* node, + const SerdNode* datatype, + const SerdNode* lang, + Field field, + SerdStatementFlags flags); + +static bool +supports_abbrev(const SerdWriter* writer) +{ + return writer->syntax == SERD_TURTLE || writer->syntax == SERD_TRIG; +} + +static bool +supports_uriref(const SerdWriter* writer) +{ + return writer->syntax == SERD_TURTLE || writer->syntax == SERD_TRIG; +} + +static void +w_err(SerdWriter* writer, SerdStatus st, const char* fmt, ...) +{ + /* TODO: This results in errors with no file information, which is not + helpful when re-serializing a file (particularly for "undefined + namespace prefix" errors. The statement sink API needs to be changed to + add a Cursor parameter so the source can notify the writer of the + statement origin for better error reporting. */ + + va_list args; + va_start(args, fmt); + const SerdError e = {st, (const uint8_t*)"", 0, 0, fmt, &args}; + serd_error(writer->error_sink, writer->error_handle, &e); + va_end(args); +} + +static inline SERD_PURE_FUNC WriteContext* +anon_stack_top(SerdWriter* writer) +{ + assert(!serd_stack_is_empty(&writer->anon_stack)); + return (WriteContext*)(writer->anon_stack.buf + writer->anon_stack.size - + sizeof(WriteContext)); +} + +static void +copy_node(SerdNode* dst, const SerdNode* src) +{ + if (src) { + dst->buf = (uint8_t*)realloc((char*)dst->buf, src->n_bytes + 1); + dst->n_bytes = src->n_bytes; + dst->n_chars = src->n_chars; + dst->flags = src->flags; + dst->type = src->type; + memcpy((char*)dst->buf, src->buf, src->n_bytes + 1); + } else { + dst->type = SERD_NOTHING; + } +} + +static inline size_t +sink(const void* buf, size_t len, SerdWriter* writer) +{ + return serd_byte_sink_write(buf, len, &writer->byte_sink); +} + +// Write a single character, as an escape for single byte characters +// (Caller prints any single byte characters that don't need escaping) +static size_t +write_character(SerdWriter* writer, const uint8_t* utf8, size_t* size) +{ + char escape[11] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + const uint32_t c = parse_utf8_char(utf8, size); + switch (*size) { + case 0: + w_err(writer, SERD_ERR_BAD_ARG, "invalid UTF-8: %X\n", utf8[0]); + return sink(replacement_char, sizeof(replacement_char), writer); + case 1: + snprintf(escape, sizeof(escape), "\\u%04X", utf8[0]); + return sink(escape, 6, writer); + default: + break; + } + + if (!(writer->style & SERD_STYLE_ASCII)) { + // Write UTF-8 character directly to UTF-8 output + return sink(utf8, *size, writer); + } + + if (c <= 0xFFFF) { + snprintf(escape, sizeof(escape), "\\u%04X", c); + return sink(escape, 6, writer); + } + + snprintf(escape, sizeof(escape), "\\U%08X", c); + return sink(escape, 10, writer); +} + +static inline bool +uri_must_escape(const uint8_t c) +{ + switch (c) { + case ' ': + case '"': + case '<': + case '>': + case '\\': + case '^': + case '`': + case '{': + case '|': + case '}': + return true; + default: + return !in_range(c, 0x20, 0x7E); + } +} + +static size_t +write_uri(SerdWriter* writer, const uint8_t* utf8, size_t n_bytes) +{ + size_t len = 0; + for (size_t i = 0; i < n_bytes;) { + size_t j = i; // Index of next character that must be escaped + for (; j < n_bytes; ++j) { + if (uri_must_escape(utf8[j])) { + break; + } + } + + // Bulk write all characters up to this special one + len += sink(&utf8[i], j - i, writer); + if ((i = j) == n_bytes) { + break; // Reached end + } + + // Write UTF-8 character + size_t size = 0; + len += write_character(writer, utf8 + i, &size); + i += size; + if (size == 0) { + // Corrupt input, scan to start of next character + for (++i; i < n_bytes && (utf8[i] & 0x80); ++i) { + } + } + } + + return len; +} + +static bool +lname_must_escape(const uint8_t c) +{ + /* This arbitrary list of characters, most of which have nothing to do with + Turtle, must be handled as special cases here because the RDF and SPARQL + WGs are apparently intent on making the once elegant Turtle a baroque + and inconsistent mess, throwing elegance and extensibility completely + out the window for no good reason. + + Note '-', '.', and '_' are also in PN_LOCAL_ESC, but are valid unescaped + in local names, so they are not escaped here. */ + + switch (c) { + case '\'': + case '!': + case '#': + case '$': + case '%': + case '&': + case '(': + case ')': + case '*': + case '+': + case ',': + case '/': + case ';': + case '=': + case '?': + case '@': + case '~': + return true; + default: + break; + } + return false; +} + +static size_t +write_lname(SerdWriter* writer, const uint8_t* utf8, size_t n_bytes) +{ + size_t len = 0; + for (size_t i = 0; i < n_bytes; ++i) { + size_t j = i; // Index of next character that must be escaped + for (; j < n_bytes; ++j) { + if (lname_must_escape(utf8[j])) { + break; + } + } + + // Bulk write all characters up to this special one + len += sink(&utf8[i], j - i, writer); + if ((i = j) == n_bytes) { + break; // Reached end + } + + // Write escape + len += sink("\\", 1, writer); + len += sink(&utf8[i], 1, writer); + } + + return len; +} + +static size_t +write_text(SerdWriter* writer, + TextContext ctx, + const uint8_t* utf8, + size_t n_bytes) +{ + size_t len = 0; + for (size_t i = 0; i < n_bytes;) { + // Fast bulk write for long strings of printable ASCII + size_t j = i; + for (; j < n_bytes; ++j) { + if (utf8[j] == '\\' || utf8[j] == '"' || + (!in_range(utf8[j], 0x20, 0x7E))) { + break; + } + } + + len += sink(&utf8[i], j - i, writer); + if ((i = j) == n_bytes) { + break; // Reached end + } + + const uint8_t in = utf8[i++]; + if (ctx == WRITE_LONG_STRING) { + switch (in) { + case '\\': + len += sink("\\\\", 2, writer); + continue; + case '\b': + len += sink("\\b", 2, writer); + continue; + case '\n': + case '\r': + case '\t': + case '\f': + len += sink(&in, 1, writer); // Write character as-is + continue; + case '\"': + if (i == n_bytes) { // '"' at string end + len += sink("\\\"", 2, writer); + } else { + len += sink(&in, 1, writer); + } + continue; + default: + break; + } + } else if (ctx == WRITE_STRING) { + switch (in) { + case '\\': + len += sink("\\\\", 2, writer); + continue; + case '\n': + len += sink("\\n", 2, writer); + continue; + case '\r': + len += sink("\\r", 2, writer); + continue; + case '\t': + len += sink("\\t", 2, writer); + continue; + case '"': + len += sink("\\\"", 2, writer); + continue; + default: + break; + } + if (writer->syntax == SERD_TURTLE) { + switch (in) { + case '\b': + len += sink("\\b", 2, writer); + continue; + case '\f': + len += sink("\\f", 2, writer); + continue; + default: + break; + } + } + } + + // Write UTF-8 character + size_t size = 0; + len += write_character(writer, utf8 + i - 1, &size); + if (size == 0) { + // Corrupt input, scan to start of next character + for (; i < n_bytes && (utf8[i] & 0x80); ++i) { + } + } else { + i += size - 1; + } + } + + return len; +} + +static size_t +uri_sink(const void* buf, size_t len, void* stream) +{ + return write_uri((SerdWriter*)stream, (const uint8_t*)buf, len); +} + +static void +write_newline(SerdWriter* writer) +{ + sink("\n", 1, writer); + for (unsigned i = 0; i < writer->indent; ++i) { + sink("\t", 1, writer); + } +} + +static bool +write_sep(SerdWriter* writer, const Sep sep) +{ + const SepRule* rule = &rules[sep]; + if (rule->space_before) { + write_newline(writer); + } + + if (rule->str) { + sink(rule->str, rule->len, writer); + } + + if ((writer->last_sep && rule->space_after_sep) || + (!writer->last_sep && rule->space_after_node)) { + write_newline(writer); + } else if (writer->last_sep && rule->space_after_node) { + sink(" ", 1, writer); + } + + writer->last_sep = sep; + return true; +} + +static SerdStatus +reset_context(SerdWriter* writer, bool graph) +{ + if (graph) { + writer->context.graph.type = SERD_NOTHING; + } + + writer->context.subject.type = SERD_NOTHING; + writer->context.predicate.type = SERD_NOTHING; + writer->empty = false; + return SERD_SUCCESS; +} + +static SerdStatus +free_context(SerdWriter* writer) +{ + serd_node_free(&writer->context.graph); + serd_node_free(&writer->context.subject); + serd_node_free(&writer->context.predicate); + return reset_context(writer, true); +} + +static bool +is_inline_start(const SerdWriter* writer, Field field, SerdStatementFlags flags) +{ + return (supports_abbrev(writer) && + ((field == FIELD_SUBJECT && (flags & SERD_ANON_S_BEGIN)) || + (field == FIELD_OBJECT && (flags & SERD_ANON_O_BEGIN)))); +} + +static bool +write_literal(SerdWriter* writer, + const SerdNode* node, + const SerdNode* datatype, + const SerdNode* lang, + SerdStatementFlags flags) +{ + if (supports_abbrev(writer) && datatype && datatype->buf) { + const char* type_uri = (const char*)datatype->buf; + if (!strncmp(type_uri, NS_XSD, sizeof(NS_XSD) - 1) && + (!strcmp(type_uri + sizeof(NS_XSD) - 1, "boolean") || + !strcmp(type_uri + sizeof(NS_XSD) - 1, "integer"))) { + sink(node->buf, node->n_bytes, writer); + return true; + } + + if (!strncmp(type_uri, NS_XSD, sizeof(NS_XSD) - 1) && + !strcmp(type_uri + sizeof(NS_XSD) - 1, "decimal") && + strchr((const char*)node->buf, '.') && + node->buf[node->n_bytes - 1] != '.') { + /* xsd:decimal literals without trailing digits, e.g. "5.", can + not be written bare in Turtle. We could add a 0 which is + prettier, but changes the text and breaks round tripping. + */ + sink(node->buf, node->n_bytes, writer); + return true; + } + } + + if (supports_abbrev(writer) && + (node->flags & (SERD_HAS_NEWLINE | SERD_HAS_QUOTE))) { + sink("\"\"\"", 3, writer); + write_text(writer, WRITE_LONG_STRING, node->buf, node->n_bytes); + sink("\"\"\"", 3, writer); + } else { + sink("\"", 1, writer); + write_text(writer, WRITE_STRING, node->buf, node->n_bytes); + sink("\"", 1, writer); + } + if (lang && lang->buf) { + sink("@", 1, writer); + sink(lang->buf, lang->n_bytes, writer); + } else if (datatype && datatype->buf) { + sink("^^", 2, writer); + return write_node(writer, datatype, NULL, NULL, FIELD_NONE, flags); + } + return true; +} + +// Return true iff `buf` is a valid prefixed name suffix +static inline bool +is_name(const uint8_t* buf, const size_t len) +{ + // TODO: This is more strict than it should be + for (size_t i = 0; i < len; ++i) { + if (!(is_alpha(buf[i]) || is_digit(buf[i]))) { + return false; + } + } + + return true; +} + +static bool +write_uri_node(SerdWriter* const writer, + const SerdNode* node, + const Field field, + const SerdStatementFlags flags) +{ + SerdNode prefix; + SerdChunk suffix; + + if (is_inline_start(writer, field, flags)) { + ++writer->indent; + write_sep(writer, SEP_ANON_BEGIN); + sink("== ", 3, writer); + } + + const bool has_scheme = serd_uri_string_has_scheme(node->buf); + if (supports_abbrev(writer)) { + if (field == FIELD_PREDICATE && + !strcmp((const char*)node->buf, NS_RDF "type")) { + return sink("a", 1, writer) == 1; + } + + if (!strcmp((const char*)node->buf, NS_RDF "nil")) { + return sink("()", 2, writer) == 2; + } + + if (has_scheme && (writer->style & SERD_STYLE_CURIED) && + serd_env_qualify(writer->env, node, &prefix, &suffix) && + is_name(suffix.buf, suffix.len)) { + write_uri(writer, prefix.buf, prefix.n_bytes); + sink(":", 1, writer); + write_uri(writer, suffix.buf, suffix.len); + return true; + } + } + + if (!has_scheme && !supports_uriref(writer) && + !serd_env_get_base_uri(writer->env, NULL)->buf) { + w_err(writer, + SERD_ERR_BAD_ARG, + "syntax does not support URI reference <%s>\n", + node->buf); + return false; + } + + write_sep(writer, SEP_URI_BEGIN); + if (writer->style & SERD_STYLE_RESOLVED) { + SerdURI in_base_uri; + SerdURI uri; + SerdURI abs_uri; + serd_env_get_base_uri(writer->env, &in_base_uri); + serd_uri_parse(node->buf, &uri); + serd_uri_resolve(&uri, &in_base_uri, &abs_uri); + bool rooted = uri_is_under(&writer->base_uri, &writer->root_uri); + SerdURI* root = rooted ? &writer->root_uri : &writer->base_uri; + if (!uri_is_under(&abs_uri, root) || writer->syntax == SERD_NTRIPLES || + writer->syntax == SERD_NQUADS) { + serd_uri_serialise(&abs_uri, uri_sink, writer); + } else { + serd_uri_serialise_relative( + &uri, &writer->base_uri, root, uri_sink, writer); + } + } else { + write_uri(writer, node->buf, node->n_bytes); + } + + write_sep(writer, SEP_URI_END); + if (is_inline_start(writer, field, flags)) { + sink(" ;", 2, writer); + write_newline(writer); + } + + return true; +} + +static bool +write_curie(SerdWriter* const writer, + const SerdNode* node, + const Field field, + const SerdStatementFlags flags) +{ + SerdChunk prefix = {NULL, 0}; + SerdChunk suffix = {NULL, 0}; + SerdStatus st = SERD_SUCCESS; + + switch (writer->syntax) { + case SERD_NTRIPLES: + case SERD_NQUADS: + if ((st = serd_env_expand(writer->env, node, &prefix, &suffix))) { + w_err(writer, st, "undefined namespace prefix `%s'\n", node->buf); + return false; + } + write_sep(writer, SEP_URI_BEGIN); + write_uri(writer, prefix.buf, prefix.len); + write_uri(writer, suffix.buf, suffix.len); + write_sep(writer, SEP_URI_END); + break; + case SERD_TURTLE: + case SERD_TRIG: + if (is_inline_start(writer, field, flags)) { + ++writer->indent; + write_sep(writer, SEP_ANON_BEGIN); + sink("== ", 3, writer); + } + write_lname(writer, node->buf, node->n_bytes); + if (is_inline_start(writer, field, flags)) { + sink(" ;", 2, writer); + write_newline(writer); + } + } + + return true; +} + +static bool +write_blank(SerdWriter* const writer, + const SerdNode* node, + const Field field, + const SerdStatementFlags flags) +{ + if (supports_abbrev(writer)) { + if (is_inline_start(writer, field, flags)) { + ++writer->indent; + return write_sep(writer, SEP_ANON_BEGIN); + } + + if (field == FIELD_SUBJECT && (flags & SERD_LIST_S_BEGIN)) { + assert(writer->list_depth == 0); + copy_node(&writer->list_subj, node); + ++writer->list_depth; + ++writer->indent; + return write_sep(writer, SEP_LIST_BEGIN); + } + + if (field == FIELD_OBJECT && (flags & SERD_LIST_O_BEGIN)) { + ++writer->indent; + ++writer->list_depth; + return write_sep(writer, SEP_LIST_BEGIN); + } + + if ((field == FIELD_SUBJECT && (flags & SERD_EMPTY_S)) || + (field == FIELD_OBJECT && (flags & SERD_EMPTY_O))) { + return sink("[]", 2, writer) == 2; + } + } + + sink("_:", 2, writer); + if (writer->bprefix && !strncmp((const char*)node->buf, + (const char*)writer->bprefix, + writer->bprefix_len)) { + sink(node->buf + writer->bprefix_len, + node->n_bytes - writer->bprefix_len, + writer); + } else { + sink(node->buf, node->n_bytes, writer); + } + + return true; +} + +static bool +write_node(SerdWriter* writer, + const SerdNode* node, + const SerdNode* datatype, + const SerdNode* lang, + Field field, + SerdStatementFlags flags) +{ + bool ret = false; + switch (node->type) { + case SERD_NOTHING: + break; + case SERD_LITERAL: + ret = write_literal(writer, node, datatype, lang, flags); + break; + case SERD_URI: + ret = write_uri_node(writer, node, field, flags); + break; + case SERD_CURIE: + ret = write_curie(writer, node, field, flags); + break; + case SERD_BLANK: + ret = write_blank(writer, node, field, flags); + break; + } + + writer->last_sep = SEP_NONE; + return ret; +} + +static inline bool +is_resource(const SerdNode* node) +{ + return node && node->buf && node->type > SERD_LITERAL; +} + +static void +write_pred(SerdWriter* writer, SerdStatementFlags flags, const SerdNode* pred) +{ + write_node(writer, pred, NULL, NULL, FIELD_PREDICATE, flags); + write_sep(writer, SEP_P_O); + copy_node(&writer->context.predicate, pred); +} + +static bool +write_list_obj(SerdWriter* writer, + SerdStatementFlags flags, + const SerdNode* predicate, + const SerdNode* object, + const SerdNode* datatype, + const SerdNode* lang) +{ + if (!strcmp((const char*)object->buf, NS_RDF "nil")) { + --writer->indent; + write_sep(writer, SEP_LIST_END); + return true; + } + + if (!strcmp((const char*)predicate->buf, NS_RDF "first")) { + write_sep(writer, SEP_LIST_SEP); + write_node(writer, object, datatype, lang, FIELD_OBJECT, flags); + } + + return false; +} + +SerdStatus +serd_writer_write_statement(SerdWriter* writer, + SerdStatementFlags flags, + const SerdNode* graph, + const SerdNode* subject, + const SerdNode* predicate, + const SerdNode* object, + const SerdNode* datatype, + const SerdNode* lang) +{ + if (!is_resource(subject) || !is_resource(predicate) || !object || + !object->buf) { + return SERD_ERR_BAD_ARG; + } + +#define TRY(write_result) \ + do { \ + if (!(write_result)) { \ + return SERD_ERR_UNKNOWN; \ + } \ + } while (0) + + if (writer->syntax == SERD_NTRIPLES || writer->syntax == SERD_NQUADS) { + TRY(write_node(writer, subject, NULL, NULL, FIELD_SUBJECT, flags)); + sink(" ", 1, writer); + TRY(write_node(writer, predicate, NULL, NULL, FIELD_PREDICATE, flags)); + sink(" ", 1, writer); + TRY(write_node(writer, object, datatype, lang, FIELD_OBJECT, flags)); + if (writer->syntax == SERD_NQUADS && graph) { + sink(" ", 1, writer); + TRY(write_node(writer, graph, datatype, lang, FIELD_GRAPH, flags)); + } + sink(" .\n", 3, writer); + return SERD_SUCCESS; + } + + if ((graph && !serd_node_equals(graph, &writer->context.graph)) || + (!graph && writer->context.graph.type)) { + writer->indent = 0; + + if (writer->context.subject.type) { + write_sep(writer, SEP_END_S); + } + + if (writer->context.graph.type) { + write_sep(writer, SEP_GRAPH_END); + } + + reset_context(writer, true); + if (graph) { + TRY(write_node(writer, graph, datatype, lang, FIELD_GRAPH, flags)); + ++writer->indent; + write_sep(writer, SEP_GRAPH_BEGIN); + copy_node(&writer->context.graph, graph); + } + } + + if ((flags & SERD_LIST_CONT)) { + if (write_list_obj(writer, flags, predicate, object, datatype, lang)) { + // Reached end of list + if (--writer->list_depth == 0 && writer->list_subj.type) { + reset_context(writer, false); + serd_node_free(&writer->context.subject); + writer->context.subject = writer->list_subj; + writer->list_subj = SERD_NODE_NULL; + } + return SERD_SUCCESS; + } + } else if (serd_node_equals(subject, &writer->context.subject)) { + if (serd_node_equals(predicate, &writer->context.predicate)) { + // Abbreviate S P + if (!(flags & SERD_ANON_O_BEGIN)) { + ++writer->indent; + } + write_sep(writer, SEP_END_O); + write_node(writer, object, datatype, lang, FIELD_OBJECT, flags); + if (!(flags & SERD_ANON_O_BEGIN)) { + --writer->indent; + } + } else { + // Abbreviate S + Sep sep = writer->context.predicate.type ? SEP_END_P : SEP_S_P; + write_sep(writer, sep); + write_pred(writer, flags, predicate); + write_node(writer, object, datatype, lang, FIELD_OBJECT, flags); + } + } else { + // No abbreviation + if (writer->context.subject.type) { + assert(writer->indent > 0); + --writer->indent; + if (serd_stack_is_empty(&writer->anon_stack)) { + write_sep(writer, SEP_END_S); + } + } else if (!writer->empty) { + write_sep(writer, SEP_S_P); + } + + if (!(flags & SERD_ANON_CONT)) { + write_node(writer, subject, NULL, NULL, FIELD_SUBJECT, flags); + ++writer->indent; + write_sep(writer, SEP_S_P); + } else { + ++writer->indent; + } + + reset_context(writer, false); + copy_node(&writer->context.subject, subject); + + if (!(flags & SERD_LIST_S_BEGIN)) { + write_pred(writer, flags, predicate); + } + + write_node(writer, object, datatype, lang, FIELD_OBJECT, flags); + } + + if (flags & (SERD_ANON_S_BEGIN | SERD_ANON_O_BEGIN)) { + WriteContext* ctx = + (WriteContext*)serd_stack_push(&writer->anon_stack, sizeof(WriteContext)); + *ctx = writer->context; + WriteContext new_context = { + serd_node_copy(graph), serd_node_copy(subject), SERD_NODE_NULL}; + if ((flags & SERD_ANON_S_BEGIN)) { + new_context.predicate = serd_node_copy(predicate); + } + writer->context = new_context; + } else { + copy_node(&writer->context.graph, graph); + copy_node(&writer->context.subject, subject); + copy_node(&writer->context.predicate, predicate); + } + + return SERD_SUCCESS; +} + +SerdStatus +serd_writer_end_anon(SerdWriter* writer, const SerdNode* node) +{ + if (writer->syntax == SERD_NTRIPLES || writer->syntax == SERD_NQUADS) { + return SERD_SUCCESS; + } + + if (serd_stack_is_empty(&writer->anon_stack) || writer->indent == 0) { + w_err(writer, SERD_ERR_UNKNOWN, "unexpected end of anonymous node\n"); + return SERD_ERR_UNKNOWN; + } + + --writer->indent; + write_sep(writer, SEP_ANON_END); + free_context(writer); + writer->context = *anon_stack_top(writer); + serd_stack_pop(&writer->anon_stack, sizeof(WriteContext)); + const bool is_subject = serd_node_equals(node, &writer->context.subject); + if (is_subject) { + copy_node(&writer->context.subject, node); + writer->context.predicate.type = SERD_NOTHING; + } + + return SERD_SUCCESS; +} + +SerdStatus +serd_writer_finish(SerdWriter* writer) +{ + if (writer->context.subject.type) { + write_sep(writer, SEP_END_S); + } + + if (writer->context.graph.type) { + write_sep(writer, SEP_GRAPH_END); + } + + serd_byte_sink_flush(&writer->byte_sink); + writer->indent = 0; + return free_context(writer); +} + +SerdWriter* +serd_writer_new(SerdSyntax syntax, + SerdStyle style, + SerdEnv* env, + const SerdURI* base_uri, + SerdSink ssink, + void* stream) +{ + const WriteContext context = WRITE_CONTEXT_NULL; + SerdWriter* writer = (SerdWriter*)calloc(1, sizeof(SerdWriter)); + + writer->syntax = syntax; + writer->style = style; + writer->env = env; + writer->root_node = SERD_NODE_NULL; + writer->root_uri = SERD_URI_NULL; + writer->base_uri = base_uri ? *base_uri : SERD_URI_NULL; + writer->anon_stack = serd_stack_new(4 * sizeof(WriteContext)); + writer->context = context; + writer->list_subj = SERD_NODE_NULL; + writer->empty = true; + writer->byte_sink = serd_byte_sink_new( + ssink, stream, (style & SERD_STYLE_BULK) ? SERD_PAGE_SIZE : 1); + + return writer; +} + +void +serd_writer_set_error_sink(SerdWriter* writer, + SerdErrorSink error_sink, + void* error_handle) +{ + writer->error_sink = error_sink; + writer->error_handle = error_handle; +} + +void +serd_writer_chop_blank_prefix(SerdWriter* writer, const uint8_t* prefix) +{ + free(writer->bprefix); + writer->bprefix_len = 0; + writer->bprefix = NULL; + + const size_t prefix_len = prefix ? strlen((const char*)prefix) : 0; + if (prefix_len) { + writer->bprefix_len = prefix_len; + writer->bprefix = (uint8_t*)malloc(writer->bprefix_len + 1); + memcpy(writer->bprefix, prefix, writer->bprefix_len + 1); + } +} + +SerdStatus +serd_writer_set_base_uri(SerdWriter* writer, const SerdNode* uri) +{ + if (!serd_env_set_base_uri(writer->env, uri)) { + serd_env_get_base_uri(writer->env, &writer->base_uri); + + if (writer->syntax == SERD_TURTLE || writer->syntax == SERD_TRIG) { + if (writer->context.graph.type || writer->context.subject.type) { + sink(" .\n\n", 4, writer); + reset_context(writer, true); + } + sink("@base <", 7, writer); + sink(uri->buf, uri->n_bytes, writer); + sink("> .\n", 4, writer); + } + writer->indent = 0; + return reset_context(writer, true); + } + + return SERD_ERR_UNKNOWN; +} + +SerdStatus +serd_writer_set_root_uri(SerdWriter* writer, const SerdNode* uri) +{ + serd_node_free(&writer->root_node); + + if (uri && uri->buf) { + writer->root_node = serd_node_copy(uri); + serd_uri_parse(uri->buf, &writer->root_uri); + } else { + writer->root_node = SERD_NODE_NULL; + writer->root_uri = SERD_URI_NULL; + } + + return SERD_SUCCESS; +} + +SerdStatus +serd_writer_set_prefix(SerdWriter* writer, + const SerdNode* name, + const SerdNode* uri) +{ + if (!serd_env_set_prefix(writer->env, name, uri)) { + if (writer->syntax == SERD_TURTLE || writer->syntax == SERD_TRIG) { + if (writer->context.graph.type || writer->context.subject.type) { + sink(" .\n\n", 4, writer); + reset_context(writer, true); + } + sink("@prefix ", 8, writer); + sink(name->buf, name->n_bytes, writer); + sink(": <", 3, writer); + write_uri(writer, uri->buf, uri->n_bytes); + sink("> .\n", 4, writer); + } + writer->indent = 0; + return reset_context(writer, true); + } + + return SERD_ERR_UNKNOWN; +} + +void +serd_writer_free(SerdWriter* writer) +{ + if (!writer) { + return; + } + + serd_writer_finish(writer); + serd_stack_free(&writer->anon_stack); + free(writer->bprefix); + serd_byte_sink_free(&writer->byte_sink); + serd_node_free(&writer->root_node); + free(writer); +} + +SerdEnv* +serd_writer_get_env(SerdWriter* writer) +{ + return writer->env; +} + +size_t +serd_file_sink(const void* buf, size_t len, void* stream) +{ + return fwrite(buf, 1, len, (FILE*)stream); +} + +size_t +serd_chunk_sink(const void* buf, size_t len, void* stream) +{ + SerdChunk* chunk = (SerdChunk*)stream; + chunk->buf = (uint8_t*)realloc((uint8_t*)chunk->buf, chunk->len + len); + memcpy((uint8_t*)chunk->buf + chunk->len, buf, len); + chunk->len += len; + return len; +} + +uint8_t* +serd_chunk_sink_finish(SerdChunk* stream) +{ + serd_chunk_sink("", 1, stream); + return (uint8_t*)stream->buf; +} diff --git a/modules/juce_audio_processors/format_types/lv2/serd_config.h b/modules/juce_audio_processors/format_types/lv2/serd_config.h new file mode 100644 index 0000000000..9a10500203 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/serd_config.h @@ -0,0 +1,3 @@ +#pragma once + +#include "juce_lv2_config.h" diff --git a/modules/juce_audio_processors/format_types/lv2/sord/COPYING b/modules/juce_audio_processors/format_types/lv2/sord/COPYING new file mode 100644 index 0000000000..9748bb93d7 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/sord/COPYING @@ -0,0 +1,13 @@ +Copyright 2011-2021 David Robillard + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. \ No newline at end of file diff --git a/modules/juce_audio_processors/format_types/lv2/sord/sord/sord.h b/modules/juce_audio_processors/format_types/lv2/sord/sord/sord.h new file mode 100644 index 0000000000..a327031143 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/sord/sord/sord.h @@ -0,0 +1,642 @@ +/* + Copyright 2011-2016 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +/** + @file sord.h API for Sord, a lightweight RDF model library. +*/ + +#ifndef SORD_SORD_H +#define SORD_SORD_H + +#include "serd/serd.h" + +#include +#include +#include + +#if defined(_WIN32) && !defined(SORD_STATIC) && defined(SORD_INTERNAL) +# define SORD_API __declspec(dllexport) +#elif defined(_WIN32) && !defined(SORD_STATIC) +# define SORD_API __declspec(dllimport) +#elif defined(__GNUC__) +# define SORD_API __attribute__((visibility("default"))) +#else +# define SORD_API +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +/** + @defgroup sord Sord + A lightweight RDF model library. + + Sord stores RDF (subject object predicate context) quads, where the context + may be omitted (to represent triples in the default graph). + @{ +*/ + +/** + Sord World. + The World represents all library state, including interned strings. +*/ +typedef struct SordWorldImpl SordWorld; + +/** + Sord Model. + + A model is an indexed set of Quads (i.e. it can contain several RDF + graphs). It may be searched using various patterns depending on which + indices are enabled. +*/ +typedef struct SordModelImpl SordModel; + +/** + Model Inserter. + + An inserter is used for writing statements to a model using the Serd sink + interface. This makes it simple to write to a model directly using a + SerdReader, or any other code that writes statements to a SerdStatementSink. +*/ +typedef struct SordInserterImpl SordInserter; + +/** + Model Iterator. +*/ +typedef struct SordIterImpl SordIter; + +/** + RDF Node. + A Node is a component of a Quad. Nodes may be URIs, blank nodes, or + (in the case of quad objects only) string literals. Literal nodes may + have an associate language or datatype (but not both). +*/ +typedef struct SordNodeImpl SordNode; + +/** + Quad of nodes (a statement), or a quad pattern. + + Nodes are ordered (S P O G). The ID of the default graph is 0. +*/ +typedef const SordNode* SordQuad[4]; + +/** + Index into a SordQuad. +*/ +typedef enum { + SORD_SUBJECT = 0, /**< Subject */ + SORD_PREDICATE = 1, /**< Predicate ("key") */ + SORD_OBJECT = 2, /**< Object ("value") */ + SORD_GRAPH = 3 /**< Graph ("context") */ +} SordQuadIndex; + +/** + Type of a node. +*/ +typedef enum { + SORD_URI = 1, /**< URI */ + SORD_BLANK = 2, /**< Blank node identifier */ + SORD_LITERAL = 3 /**< Literal (string with optional lang or datatype) */ +} SordNodeType; + +/** + Indexing option. +*/ +typedef enum { + SORD_SPO = 1, /**< Subject, Predicate, Object */ + SORD_SOP = 1 << 1, /**< Subject, Object, Predicate */ + SORD_OPS = 1 << 2, /**< Object, Predicate, Subject */ + SORD_OSP = 1 << 3, /**< Object, Subject, Predicate */ + SORD_PSO = 1 << 4, /**< Predicate, Subject, Object */ + SORD_POS = 1 << 5 /**< Predicate, Object, Subject */ +} SordIndexOption; + +/** + @name World + @{ +*/ + +/** + Create a new Sord World. + It is safe to use multiple worlds in one process, though no data + (e.g. nodes) can be shared between worlds, and this should be avoided if + possible for performance reasons. +*/ +SORD_API +SordWorld* +sord_world_new(void); + +/** + Free `world`. +*/ +SORD_API +void +sord_world_free(SordWorld* world); + +/** + Set a function to be called when errors occur. + + The `error_sink` will be called with `handle` as its first argument. If + no error function is set, errors are printed to stderr. +*/ +SORD_API +void +sord_world_set_error_sink(SordWorld* world, + SerdErrorSink error_sink, + void* handle); + +/** + @} + @name Node + @{ +*/ + +/** + Get a URI node from a string. + + Note this function measures `str`, which is a common bottleneck. + Use sord_node_from_serd_node() instead if `str` is already measured. +*/ +SORD_API +SordNode* +sord_new_uri(SordWorld* world, const uint8_t* uri); + +/** + Get a URI node from a relative URI string. +*/ +SORD_API +SordNode* +sord_new_relative_uri(SordWorld* world, + const uint8_t* uri, + const uint8_t* base_uri); + +/** + Get a blank node from a string. + + Note this function measures `str`, which is a common bottleneck. + Use sord_node_from_serd_node() instead if `str` is already measured. +*/ +SORD_API +SordNode* +sord_new_blank(SordWorld* world, const uint8_t* str); + +/** + Get a literal node from a string. + + Note this function measures `str`, which is a common bottleneck. + Use sord_node_from_serd_node() instead if `str` is already measured. +*/ +SORD_API +SordNode* +sord_new_literal(SordWorld* world, + SordNode* datatype, + const uint8_t* str, + const char* lang); + +/** + Copy a node (obtain a reference). + + Node that since nodes are interned and reference counted, this does not + actually create a deep copy of `node`. +*/ +SORD_API +SordNode* +sord_node_copy(const SordNode* node); + +/** + Free a node (drop a reference). +*/ +SORD_API +void +sord_node_free(SordWorld* world, SordNode* node); + +/** + Return the type of a node (SORD_URI, SORD_BLANK, or SORD_LITERAL). +*/ +SORD_API +SordNodeType +sord_node_get_type(const SordNode* node); + +/** + Return the string value of a node. +*/ +SORD_API +const uint8_t* +sord_node_get_string(const SordNode* node); + +/** + Return the string value of a node, and set `bytes` to its length in bytes. +*/ +SORD_API +const uint8_t* +sord_node_get_string_counted(const SordNode* node, size_t* bytes); + +/** + Return the string value of a node, and set `bytes` to its length in bytes, + and `count` to its length in characters. +*/ +SORD_API +const uint8_t* +sord_node_get_string_measured(const SordNode* node, + size_t* bytes, + size_t* chars); + +/** + Return the language of a literal node (or NULL). +*/ +SORD_API +const char* +sord_node_get_language(const SordNode* node); + +/** + Return the datatype URI of a literal node (or NULL). +*/ +SORD_API +SordNode* +sord_node_get_datatype(const SordNode* node); + +/** + Return the flags (string attributes) of a node. +*/ +SORD_API +SerdNodeFlags +sord_node_get_flags(const SordNode* node); + +/** + Return true iff node can be serialised as an inline object. + + More specifically, this returns true iff the node is the object field + of exactly one statement, and therefore can be inlined since it needn't + be referred to by name. +*/ +SORD_API +bool +sord_node_is_inline_object(const SordNode* node); + +/** + Return true iff `a` is equal to `b`. + + Note this is much faster than comparing the node's strings. +*/ +SORD_API +bool +sord_node_equals(const SordNode* a, const SordNode* b); + +/** + Return a SordNode as a SerdNode. + + The returned node is shared and must not be freed or modified. +*/ +SORD_API +const SerdNode* +sord_node_to_serd_node(const SordNode* node); + +/** + Create a new SordNode from a SerdNode. + + The returned node must be freed using sord_node_free(). +*/ +SORD_API +SordNode* +sord_node_from_serd_node(SordWorld* world, + SerdEnv* env, + const SerdNode* node, + const SerdNode* datatype, + const SerdNode* lang); + +/** + @} + @name Model + @{ +*/ + +/** + Create a new model. + + @param world The world in which to make this model. + + @param indices SordIndexOption flags (e.g. SORD_SPO|SORD_OPS). Be sure to + enable an index where the most significant node(s) are not variables in your + queries (e.g. to make (? P O) queries, enable either SORD_OPS or SORD_POS). + + @param graphs If true, store (and index) graph contexts. +*/ +SORD_API +SordModel* +sord_new(SordWorld* world, unsigned indices, bool graphs); + +/** + Close and free `model`. +*/ +SORD_API +void +sord_free(SordModel* model); + +/** + Get the world associated with `model`. +*/ +SORD_API +SordWorld* +sord_get_world(SordModel* model); + +/** + Return the number of nodes stored in `world`. + + Nodes are included in this count iff they are a part of a quad in `world`. +*/ +SORD_API +size_t +sord_num_nodes(const SordWorld* world); + +/** + Return the number of quads stored in `model`. +*/ +SORD_API +size_t +sord_num_quads(const SordModel* model); + +/** + Return an iterator to the start of `model`. +*/ +SORD_API +SordIter* +sord_begin(const SordModel* model); + +/** + Search for statements by a quad pattern. + @return an iterator to the first match, or NULL if no matches found. +*/ +SORD_API +SordIter* +sord_find(SordModel* model, const SordQuad pat); + +/** + Search for statements by nodes. + @return an iterator to the first match, or NULL if no matches found. +*/ +SORD_API +SordIter* +sord_search(SordModel* model, + const SordNode* s, + const SordNode* p, + const SordNode* o, + const SordNode* g); +/** + Search for a single node that matches a pattern. + Exactly one of `s`, `p`, `o` must be NULL. + This function is mainly useful for predicates that only have one value. + The returned node must be freed using sord_node_free(). + @return the first matching node, or NULL if no matches are found. +*/ +SORD_API +SordNode* +sord_get(SordModel* model, + const SordNode* s, + const SordNode* p, + const SordNode* o, + const SordNode* g); + +/** + Return true iff a statement exists. +*/ +SORD_API +bool +sord_ask(SordModel* model, + const SordNode* s, + const SordNode* p, + const SordNode* o, + const SordNode* g); + +/** + Return the number of matching statements. +*/ +SORD_API +uint64_t +sord_count(SordModel* model, + const SordNode* s, + const SordNode* p, + const SordNode* o, + const SordNode* g); + +/** + Check if `model` contains a triple pattern. + + @return true if `model` contains a match for `pat`, otherwise false. +*/ +SORD_API +bool +sord_contains(SordModel* model, const SordQuad pat); + +/** + Add a quad to a model. + + Calling this function invalidates all iterators on `model`. + + @return true on success, false, on error. +*/ +SORD_API +bool +sord_add(SordModel* model, const SordQuad tup); + +/** + Remove a quad from a model. + + Calling this function invalidates all iterators on `model`. To remove quads + while iterating, use sord_erase() instead. +*/ +SORD_API +void +sord_remove(SordModel* model, const SordQuad tup); + +/** + Remove a quad from a model via an iterator. + + Calling this function invalidates all iterators on `model` except `iter`. + + @param model The model which `iter` points to. + @param iter Iterator to the element to erase, which is incremented to the + next value on return. +*/ +SORD_API +SerdStatus +sord_erase(SordModel* model, SordIter* iter); + +/** + @} + @name Inserter + @{ +*/ + +/** + Create an inserter for writing statements to a model. +*/ +SORD_API +SordInserter* +sord_inserter_new(SordModel* model, SerdEnv* env); + +/** + Free an inserter. +*/ +SORD_API +void +sord_inserter_free(SordInserter* inserter); + +/** + Set the current base URI for writing to the model. + + Note this function can be safely casted to SerdBaseSink. +*/ +SORD_API +SerdStatus +sord_inserter_set_base_uri(SordInserter* inserter, const SerdNode* uri); + +/** + Set a namespace prefix for writing to the model. + + Note this function can be safely casted to SerdPrefixSink. +*/ +SORD_API +SerdStatus +sord_inserter_set_prefix(SordInserter* inserter, + const SerdNode* name, + const SerdNode* uri); + +/** + Write a statement to the model. + + Note this function can be safely casted to SerdStatementSink. +*/ +SORD_API +SerdStatus +sord_inserter_write_statement(SordInserter* inserter, + SerdStatementFlags flags, + const SerdNode* graph, + const SerdNode* subject, + const SerdNode* predicate, + const SerdNode* object, + const SerdNode* object_datatype, + const SerdNode* object_lang); + +/** + @} + @name Iteration + @{ +*/ + +/** + Set `quad` to the quad pointed to by `iter`. +*/ +SORD_API +void +sord_iter_get(const SordIter* iter, SordQuad tup); + +/** + Return a field of the quad pointed to by `iter`. + + Returns NULL if `iter` is NULL or is at the end. +*/ +SORD_API +const SordNode* +sord_iter_get_node(const SordIter* iter, SordQuadIndex index); + +/** + Return the store pointed to by `iter`. +*/ +SORD_API +const SordModel* +sord_iter_get_model(SordIter* iter); + +/** + Increment `iter` to point to the next statement. +*/ +SORD_API +bool +sord_iter_next(SordIter* iter); + +/** + Return true iff `iter` is at the end of its range. +*/ +SORD_API +bool +sord_iter_end(const SordIter* iter); + +/** + Free `iter`. +*/ +SORD_API +void +sord_iter_free(SordIter* iter); + +/** + @} + @name Utilities + @{ +*/ + +/** + Match two quads (using ID comparison only). + + This function is a straightforward and fast equivalence match with wildcard + support (ID 0 is a wildcard). It does not actually read node data. + @return true iff `x` and `y` match. +*/ +SORD_API +bool +sord_quad_match(const SordQuad x, const SordQuad y); + +/** + @} + @name Serialisation + @{ +*/ + +/** + Return a reader that will read into `model`. +*/ +SORD_API +SerdReader* +sord_new_reader(SordModel* model, + SerdEnv* env, + SerdSyntax syntax, + SordNode* graph); + +/** + Write a model to a writer. +*/ +SORD_API +bool +sord_write(SordModel* model, SerdWriter* writer, SordNode* graph); + +/** + Write a range to a writer. + + This increments `iter` to its end, then frees it. +*/ +SORD_API +bool +sord_write_iter(SordIter* iter, SerdWriter* writer); + +/** + @} + @} +*/ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* SORD_SORD_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/sord/sord/sordmm.hpp b/modules/juce_audio_processors/format_types/lv2/sord/sord/sordmm.hpp new file mode 100644 index 0000000000..32027cde1e --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/sord/sord/sordmm.hpp @@ -0,0 +1,720 @@ +/* + Copyright 2011-2013 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +/** + @file sordmm.hpp + Public Sord C++ API. +*/ + +#ifndef SORD_SORDMM_HPP +#define SORD_SORDMM_HPP + +#if defined(__clang__) +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" +#endif + +#include "serd/serd.h" +#include "sord/sord.h" + +#if defined(__clang__) +# pragma clang diagnostic pop +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define SORD_NS_XSD "http://www.w3.org/2001/XMLSchema#" + +namespace Sord { + +/** Utility base class to prevent copying or moving. */ +class Noncopyable +{ +public: + Noncopyable() = default; + ~Noncopyable() = default; + + Noncopyable(const Noncopyable&) = delete; + const Noncopyable& operator=(const Noncopyable&) = delete; + + Noncopyable(Noncopyable&&) = delete; + Noncopyable& operator=(Noncopyable&&) = delete; +}; + +/** C++ wrapper for a Sord object. */ +template +class Wrapper +{ +public: + inline Wrapper(T* c_obj = nullptr) + : _c_obj(c_obj) + {} + + inline T* c_obj() { return _c_obj; } + inline const T* c_obj() const { return _c_obj; } + +protected: + T* _c_obj; +}; + +/** Collection of RDF namespaces with prefixes. */ +class Namespaces : public Wrapper +{ +public: + Namespaces() + : Wrapper(serd_env_new(nullptr)) + {} + + ~Namespaces() { serd_env_free(_c_obj); } + + static inline SerdNode string_to_node(SerdType type, const std::string& s) + { + SerdNode ret = {reinterpret_cast(s.c_str()), + s.length(), + s.length(), + 0, + type}; + return ret; + } + + inline void add(const std::string& name, const std::string& uri) + { + const SerdNode name_node = string_to_node(SERD_LITERAL, name); + const SerdNode uri_node = string_to_node(SERD_URI, uri); + serd_env_set_prefix(_c_obj, &name_node, &uri_node); + } + + inline std::string qualify(std::string uri) const + { + const SerdNode uri_node = string_to_node(SERD_URI, uri); + SerdNode prefix; + SerdChunk suffix; + if (serd_env_qualify(_c_obj, &uri_node, &prefix, &suffix)) { + std::string ret(reinterpret_cast(prefix.buf), + prefix.n_bytes); + ret.append(":").append(reinterpret_cast(suffix.buf), + suffix.len); + return ret; + } + return uri; + } + + inline std::string expand(const std::string& curie) const + { + assert(curie.find(':') != std::string::npos); + SerdNode curie_node = string_to_node(SERD_CURIE, curie); + SerdChunk uri_prefix; + SerdChunk uri_suffix; + if (!serd_env_expand(_c_obj, &curie_node, &uri_prefix, &uri_suffix)) { + std::string ret(reinterpret_cast(uri_prefix.buf), + uri_prefix.len); + ret.append(reinterpret_cast(uri_suffix.buf), uri_suffix.len); + return ret; + } + std::cerr << "CURIE `" << curie << "' has unknown prefix." << std::endl; + return curie; + } +}; + +/** Sord library state. */ +class World + : public Noncopyable + , public Wrapper +{ +public: + inline World() + : _next_blank_id(0) + { + _c_obj = sord_world_new(); + } + + inline ~World() { sord_world_free(_c_obj); } + + inline uint64_t blank_id() { return _next_blank_id++; } + + inline void add_prefix(const std::string& prefix, const std::string& uri) + { + _prefixes.add(prefix, uri); + } + + inline const Namespaces& prefixes() const { return _prefixes; } + inline Namespaces& prefixes() { return _prefixes; } + inline SordWorld* world() { return _c_obj; } + +private: + Namespaces _prefixes; + std::set _blank_ids; + uint64_t _next_blank_id; +}; + +/** An RDF Node (resource, literal, etc) + */ +class Node : public Wrapper +{ +public: + enum Type { + UNKNOWN = 0, + URI = SORD_URI, + BLANK = SORD_BLANK, + LITERAL = SORD_LITERAL + }; + + inline Node() + : Wrapper(nullptr) + , _world(nullptr) + {} + + inline Node(World& world, Type t, const std::string& s); + inline Node(World& world); + inline Node(World& world, const SordNode* node); + inline Node(World& world, SordNode* node, bool copy = false); + inline Node(const Node& other); + inline ~Node(); + + inline Type type() const + { + return _c_obj ? static_cast(sord_node_get_type(_c_obj)) : UNKNOWN; + } + + inline const SordNode* get_node() const { return _c_obj; } + inline SordNode* get_node() { return _c_obj; } + + const SerdNode* to_serd_node() const + { + return sord_node_to_serd_node(_c_obj); + } + + inline bool is_valid() const { return type() != UNKNOWN; } + + inline bool operator<(const Node& other) const + { + if (type() != other.type()) { + return type() < other.type(); + } else { + return to_string() < other.to_string(); + } + } + + Node& operator=(const Node& other) + { + if (&other != this) { + if (_c_obj) { + sord_node_free(_world->c_obj(), _c_obj); + } + _world = other._world; + _c_obj = other._c_obj ? sord_node_copy(other._c_obj) : nullptr; + } + return *this; + } + + inline bool operator==(const Node& other) const + { + return sord_node_equals(_c_obj, other._c_obj); + } + + inline const uint8_t* to_u_string() const; + inline const char* to_c_string() const; + inline std::string to_string() const; + + inline bool is_literal_type(const char* type_uri) const; + + inline bool is_uri() const { return _c_obj && type() == URI; } + inline bool is_blank() const { return _c_obj && type() == BLANK; } + inline bool is_int() const { return is_literal_type(SORD_NS_XSD "integer"); } + inline bool is_float() const + { + return is_literal_type(SORD_NS_XSD "decimal"); + } + inline bool is_bool() const { return is_literal_type(SORD_NS_XSD "boolean"); } + + inline int to_int() const; + inline float to_float() const; + inline bool to_bool() const; + + inline static Node blank_id(World& world, const std::string& base = "b") + { + const uint64_t num = world.blank_id(); + std::ostringstream ss; + ss << base << num; + return Node(world, Node::BLANK, ss.str()); + } + +private: + World* _world; +}; + +inline std::ostream& +operator<<(std::ostream& os, const Node& node) +{ + return os << node.to_string(); +} + +class URI : public Node +{ +public: + inline URI(World& world, const std::string& s) + : Node(world, Node::URI, s) + {} + + inline URI(World& world, const std::string& s, const std::string& base) + : Node( + world, + sord_new_relative_uri(world.world(), + reinterpret_cast(s.c_str()), + reinterpret_cast(base.c_str()))) + {} +}; + +class Curie : public Node +{ +public: + inline Curie(World& world, const std::string& s) + : Node(world, Node::URI, world.prefixes().expand(s)) + {} +}; + +class Literal : public Node +{ +public: + inline Literal(World& world, const std::string& s) + : Node(world, Node::LITERAL, s) + {} + + static inline Node decimal(World& world, double d, unsigned frac_digits) + { + const SerdNode val = serd_node_new_decimal(d, frac_digits); + const SerdNode type = serd_node_from_string( + SERD_URI, reinterpret_cast(SORD_NS_XSD "decimal")); + + return Node( + world, + sord_node_from_serd_node( + world.c_obj(), world.prefixes().c_obj(), &val, &type, nullptr), + false); + } + + static inline Node integer(World& world, int64_t i) + { + const SerdNode val = serd_node_new_integer(i); + const SerdNode type = serd_node_from_string( + SERD_URI, reinterpret_cast(SORD_NS_XSD "integer")); + + return Node( + world, + sord_node_from_serd_node( + world.c_obj(), world.prefixes().c_obj(), &val, &type, nullptr), + false); + } +}; + +inline Node::Node(World& world, Type type, const std::string& s) + : _world(&world) +{ + switch (type) { + case URI: + _c_obj = sord_new_uri(world.world(), + reinterpret_cast(s.c_str())); + break; + case LITERAL: + _c_obj = sord_new_literal(world.world(), + nullptr, + reinterpret_cast(s.c_str()), + nullptr); + break; + case BLANK: + _c_obj = sord_new_blank(world.world(), + reinterpret_cast(s.c_str())); + break; + default: + _c_obj = nullptr; + } + + assert(this->type() == type); +} + +inline Node::Node(World& world) + : _world(&world) +{ + Node me = blank_id(world); + *this = me; +} + +inline Node::Node(World& world, const SordNode* node) + : _world(&world) +{ + _c_obj = sord_node_copy(node); +} + +inline Node::Node(World& world, SordNode* node, bool copy) + : _world(&world) +{ + _c_obj = copy ? sord_node_copy(node) : node; +} + +inline Node::Node(const Node& other) // NOLINT(bugprone-copy-constructor-init) + : Wrapper() + , _world(other._world) +{ + if (_world) { + _c_obj = other._c_obj ? sord_node_copy(other._c_obj) : nullptr; + } + + assert((!_c_obj && !other._c_obj) || to_string() == other.to_string()); +} + +inline Node::~Node() +{ + if (_world) { + sord_node_free(_world->c_obj(), _c_obj); + } +} + +inline std::string +Node::to_string() const +{ + return _c_obj ? reinterpret_cast(sord_node_get_string(_c_obj)) + : ""; +} + +inline const char* +Node::to_c_string() const +{ + return reinterpret_cast(sord_node_get_string(_c_obj)); +} + +inline const uint8_t* +Node::to_u_string() const +{ + return sord_node_get_string(_c_obj); +} + +inline bool +Node::is_literal_type(const char* type_uri) const +{ + if (_c_obj && sord_node_get_type(_c_obj) == SORD_LITERAL) { + const SordNode* datatype = sord_node_get_datatype(_c_obj); + if (datatype && + !strcmp(reinterpret_cast(sord_node_get_string(datatype)), + type_uri)) { + return true; + } + } + return false; +} + +inline int +Node::to_int() const +{ + assert(is_int()); + char* endptr = nullptr; + return strtol( + reinterpret_cast(sord_node_get_string(_c_obj)), &endptr, 10); +} + +inline float +Node::to_float() const +{ + assert(is_float()); + return serd_strtod( + reinterpret_cast(sord_node_get_string(_c_obj)), nullptr); +} + +inline bool +Node::to_bool() const +{ + assert(is_bool()); + return !strcmp(reinterpret_cast(sord_node_get_string(_c_obj)), + "true"); +} + +struct Iter : public Wrapper { + inline Iter(World& world, SordIter* c_obj) + : Wrapper(c_obj) + , _world(world) + {} + + Iter(const Iter&) = delete; + Iter& operator=(const Iter&) = delete; + + inline Iter(Iter&& iter) noexcept + : Wrapper(iter) + , _world(iter._world) + {} + + inline ~Iter() { sord_iter_free(_c_obj); } + + inline bool end() const { return sord_iter_end(_c_obj); } + inline bool next() const { return sord_iter_next(_c_obj); } + inline Iter& operator++() + { + assert(!end()); + next(); + return *this; + } + inline Node get_subject() const + { + SordQuad quad; + sord_iter_get(_c_obj, quad); + return Node(_world, quad[SORD_SUBJECT]); + } + inline Node get_predicate() const + { + SordQuad quad; + sord_iter_get(_c_obj, quad); + return Node(_world, quad[SORD_PREDICATE]); + } + inline Node get_object() const + { + SordQuad quad; + sord_iter_get(_c_obj, quad); + return Node(_world, quad[SORD_OBJECT]); + } + World& _world; +}; + +/** An RDF Model (collection of triples). + */ +class Model + : public Noncopyable + , public Wrapper +{ +public: + inline Model(World& world, + const std::string& base_uri, + unsigned indices = (SORD_SPO | SORD_OPS), + bool graphs = true); + + inline ~Model(); + + inline const Node& base_uri() const { return _base; } + + size_t num_quads() const { return sord_num_quads(_c_obj); } + + inline void load_file(SerdEnv* env, + SerdSyntax syntax, + const std::string& uri, + const std::string& base_uri = ""); + + inline void load_string(SerdEnv* env, + SerdSyntax syntax, + const char* str, + size_t len, + const std::string& base_uri); + + inline SerdStatus write_to_file(const std::string& uri, + SerdSyntax syntax = SERD_TURTLE, + SerdStyle style = static_cast( + SERD_STYLE_ABBREVIATED | SERD_STYLE_CURIED | + SERD_STYLE_RESOLVED)); + + inline std::string write_to_string( + const std::string& base_uri, + SerdSyntax syntax = SERD_TURTLE, + SerdStyle style = static_cast(SERD_STYLE_ABBREVIATED | + SERD_STYLE_CURIED | + SERD_STYLE_RESOLVED)); + + inline void add_statement(const Node& subject, + const Node& predicate, + const Node& object); + + inline Iter find(const Node& subject, + const Node& predicate, + const Node& object); + + inline Node get(const Node& subject, + const Node& predicate, + const Node& object); + + inline World& world() const { return _world; } + +private: + World& _world; + Node _base; +}; + +/** Create an empty in-memory RDF model. + */ +inline Model::Model(World& world, + const std::string& base_uri, + unsigned indices, + bool graphs) + : _world(world) + , _base(world, Node::URI, base_uri) +{ + _c_obj = sord_new(_world.world(), indices, graphs); +} + +inline void +Model::load_string(SerdEnv* env, + SerdSyntax syntax, + const char* str, + size_t /*len*/, + const std::string& /*base_uri*/) +{ + SerdReader* reader = sord_new_reader(_c_obj, env, syntax, nullptr); + serd_reader_read_string(reader, reinterpret_cast(str)); + serd_reader_free(reader); +} + +inline Model::~Model() +{ + sord_free(_c_obj); +} + +inline void +Model::load_file(SerdEnv* env, + SerdSyntax syntax, + const std::string& data_uri, + const std::string& /*base_uri*/) +{ + uint8_t* path = serd_file_uri_parse( + reinterpret_cast(data_uri.c_str()), nullptr); + if (!path) { + fprintf(stderr, "Failed to parse file URI <%s>\n", data_uri.c_str()); + return; + } + + // FIXME: blank prefix parameter? + SerdReader* reader = sord_new_reader(_c_obj, env, syntax, nullptr); + serd_reader_read_file(reader, path); + serd_reader_free(reader); + serd_free(path); +} + +inline SerdStatus +Model::write_to_file(const std::string& uri, SerdSyntax syntax, SerdStyle style) +{ + uint8_t* path = + serd_file_uri_parse(reinterpret_cast(uri.c_str()), nullptr); + if (!path) { + fprintf(stderr, "Failed to parse file URI <%s>\n", uri.c_str()); + return SERD_ERR_BAD_ARG; + } + + FILE* const fd = fopen(reinterpret_cast(path), "w"); + if (!fd) { + fprintf(stderr, "Failed to open file %s\n", path); + serd_free(path); + return SERD_ERR_UNKNOWN; + } + serd_free(path); + + SerdURI base_uri = SERD_URI_NULL; + if (serd_uri_parse(reinterpret_cast(uri.c_str()), + &base_uri)) { + fprintf(stderr, "Invalid base URI <%s>\n", uri.c_str()); + fclose(fd); + return SERD_ERR_BAD_ARG; + } + + SerdWriter* writer = serd_writer_new( + syntax, style, _world.prefixes().c_obj(), &base_uri, serd_file_sink, fd); + + serd_env_foreach(_world.prefixes().c_obj(), + reinterpret_cast(serd_writer_set_prefix), + writer); + + sord_write(_c_obj, writer, nullptr); + serd_writer_free(writer); + fclose(fd); + + return SERD_SUCCESS; +} + +extern "C" { + +static size_t +string_sink(const void* buf, size_t len, void* stream) +{ + try { + auto* str = static_cast(stream); + str->append(static_cast(buf), len); + return len; + } catch (...) { + return 0; + } +} +} + +inline std::string +Model::write_to_string(const std::string& base_uri_str, + SerdSyntax syntax, + SerdStyle style) +{ + SerdURI base_uri = SERD_URI_NULL; + if (serd_uri_parse(reinterpret_cast(base_uri_str.c_str()), + &base_uri)) { + fprintf(stderr, "Invalid base URI <%s>\n", base_uri_str.c_str()); + return ""; + } + + std::string ret; + + SerdWriter* writer = serd_writer_new( + syntax, style, _world.prefixes().c_obj(), &base_uri, string_sink, &ret); + + const SerdNode base_uri_node = serd_node_from_string( + SERD_URI, reinterpret_cast(base_uri_str.c_str())); + serd_writer_set_base_uri(writer, &base_uri_node); + + serd_env_foreach(_world.prefixes().c_obj(), + reinterpret_cast(serd_writer_set_prefix), + writer); + + sord_write(_c_obj, writer, nullptr); + + serd_writer_free(writer); + return ret; +} + +inline void +Model::add_statement(const Node& subject, + const Node& predicate, + const Node& object) +{ + SordQuad quad = {subject.c_obj(), predicate.c_obj(), object.c_obj(), nullptr}; + + sord_add(_c_obj, quad); +} + +inline Iter +Model::find(const Node& subject, const Node& predicate, const Node& object) +{ + SordQuad quad = {subject.c_obj(), predicate.c_obj(), object.c_obj(), nullptr}; + + return Iter(_world, sord_find(_c_obj, quad)); +} + +inline Node +Model::get(const Node& subject, const Node& predicate, const Node& object) +{ + SordNode* c_node = sord_get( + _c_obj, subject.c_obj(), predicate.c_obj(), object.c_obj(), nullptr); + return Node(_world, c_node, false); +} + +} // namespace Sord + +#endif // SORD_SORDMM_HPP diff --git a/modules/juce_audio_processors/format_types/lv2/sord/src/sord.c b/modules/juce_audio_processors/format_types/lv2/sord/src/sord.c new file mode 100644 index 0000000000..05e822c90c --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/sord/src/sord.c @@ -0,0 +1,1368 @@ +/* + Copyright 2011-2016 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "sord_config.h" // IWYU pragma: keep +#include "sord_internal.h" + +#include "serd/serd.h" +#include "sord/sord.h" + +#define ZIX_INLINE +#include "zix/btree.c" +#include "zix/btree.h" +#include "zix/common.h" +#include "zix/digest.c" +#include "zix/hash.c" +#include "zix/hash.h" + +#include +#include +#include +#include +#include +#include +#include + +#ifdef __GNUC__ +# define SORD_LOG_FUNC(fmt, arg1) __attribute__((format(printf, fmt, arg1))) +#else +# define SORD_LOG_FUNC(fmt, arg1) +#endif + +#define SORD_LOG(prefix, ...) fprintf(stderr, "[Sord::" prefix "] " __VA_ARGS__) + +#ifdef SORD_DEBUG_ITER +# define SORD_ITER_LOG(...) SORD_LOG("iter", __VA_ARGS__) +#else +# define SORD_ITER_LOG(...) +#endif +#ifdef SORD_DEBUG_SEARCH +# define SORD_FIND_LOG(...) SORD_LOG("search", __VA_ARGS__) +#else +# define SORD_FIND_LOG(...) +#endif +#ifdef SORD_DEBUG_WRITE +# define SORD_WRITE_LOG(...) SORD_LOG("write", __VA_ARGS__) +#else +# define SORD_WRITE_LOG(...) +#endif + +#define NUM_ORDERS 12 +#define STATEMENT_LEN 3 +#define TUP_LEN (STATEMENT_LEN + 1) +#define DEFAULT_ORDER SPO +#define DEFAULT_GRAPH_ORDER GSPO + +#define TUP_FMT "(%s %s %s %s)" +#define TUP_FMT_ELEM(e) ((e) ? sord_node_get_string(e) : (const uint8_t*)"*") +#define TUP_FMT_ARGS(t) \ + TUP_FMT_ELEM((t)[0]), TUP_FMT_ELEM((t)[1]), TUP_FMT_ELEM((t)[2]), \ + TUP_FMT_ELEM((t)[3]) + +#define TUP_G 3 + +/** Triple ordering */ +typedef enum { + SPO, ///< Subject, Predicate, Object + SOP, ///< Subject, Object, Predicate + OPS, ///< Object, Predicate, Subject + OSP, ///< Object, Subject, Predicate + PSO, ///< Predicate, Subject, Object + POS, ///< Predicate, Object, Subject + GSPO, ///< Graph, Subject, Predicate, Object + GSOP, ///< Graph, Subject, Object, Predicate + GOPS, ///< Graph, Object, Predicate, Subject + GOSP, ///< Graph, Object, Subject, Predicate + GPSO, ///< Graph, Predicate, Subject, Object + GPOS ///< Graph, Predicate, Object, Subject +} SordOrder; + +#ifdef SORD_DEBUG_SEARCH +/** String name of each ordering (array indexed by SordOrder) */ +static const char* const order_names[NUM_ORDERS] = {"spo", + "sop", + "ops", + "osp", + "pso", + "pos", + "gspo", + "gsop", + "gops", + "gosp", + "gpso", + "gpos"}; +#endif + +/** + Quads of indices for each order, from most to least significant + (array indexed by SordOrder) +*/ +static const int orderings[NUM_ORDERS][TUP_LEN] = { + {0, 1, 2, 3}, // SPO + {0, 2, 1, 3}, // SOP + {2, 1, 0, 3}, // OPS + {2, 0, 1, 3}, // OSP + {1, 0, 2, 3}, // PSO + {1, 2, 0, 3}, // POS + {3, 0, 1, 2}, // GSPO + {3, 0, 2, 1}, // GSOP + {3, 2, 1, 0}, // GOPS + {3, 2, 0, 1}, // GOSP + {3, 1, 0, 2}, // GPSO + {3, 1, 2, 0} // GPOS +}; + +/** World */ +struct SordWorldImpl { + ZixHash* nodes; + SerdErrorSink error_sink; + void* error_handle; +}; + +/** Store */ +struct SordModelImpl { + SordWorld* world; + + /** Index for each possible triple ordering (may or may not exist). + * Each index is a tree of SordQuad with the appropriate ordering. + */ + ZixBTree* indices[NUM_ORDERS]; + + size_t n_quads; + size_t n_iters; +}; + +/** Mode for searching or iteration */ +typedef enum { + ALL, ///< Iterate over entire store + SINGLE, ///< Iteration over a single element (exact search) + RANGE, ///< Iterate over range with equal prefix + FILTER_RANGE, ///< Iterate over range with equal prefix, filtering + FILTER_ALL ///< Iterate to end of store, filtering +} SearchMode; + +/** Iterator over some range of a store */ +struct SordIterImpl { + const SordModel* sord; ///< Model being iterated over + ZixBTreeIter* cur; ///< Current DB cursor + SordQuad pat; ///< Pattern (in ordering order) + SordOrder order; ///< Store order (which index) + SearchMode mode; ///< Iteration mode + int n_prefix; ///< Prefix for RANGE and FILTER_RANGE + bool end; ///< True iff reached end + bool skip_graphs; ///< Iteration should ignore graphs +}; + +static uint32_t +sord_node_hash(const void* n) +{ + const SordNode* node = (const SordNode*)n; + uint32_t hash = zix_digest_start(); + hash = zix_digest_add(hash, node->node.buf, node->node.n_bytes); + hash = zix_digest_add(hash, &node->node.type, sizeof(node->node.type)); + if (node->node.type == SERD_LITERAL) { + hash = zix_digest_add(hash, &node->meta.lit, sizeof(node->meta.lit)); + } + return hash; +} + +static bool +sord_node_hash_equal(const void* a, const void* b) +{ + const SordNode* a_node = (const SordNode*)a; + const SordNode* b_node = (const SordNode*)b; + return (a_node == b_node) || + ((a_node->node.type == b_node->node.type) && + (a_node->node.type != SERD_LITERAL || + (a_node->meta.lit.datatype == b_node->meta.lit.datatype && + !strncmp(a_node->meta.lit.lang, + b_node->meta.lit.lang, + sizeof(a_node->meta.lit.lang)))) && + (serd_node_equals(&a_node->node, &b_node->node))); +} + +SORD_LOG_FUNC(3, 4) +static void +error(SordWorld* world, SerdStatus st, const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + const SerdError e = {st, NULL, 0, 0, fmt, &args}; + if (world->error_sink) { + world->error_sink(world->error_handle, &e); + } else { + fprintf(stderr, "error: "); + vfprintf(stderr, fmt, args); + } + va_end(args); +} + +SordWorld* +sord_world_new(void) +{ + SordWorld* world = (SordWorld*)malloc(sizeof(SordWorld)); + world->error_sink = NULL; + world->error_handle = NULL; + + world->nodes = + zix_hash_new(sord_node_hash, sord_node_hash_equal, sizeof(SordNode)); + + return world; +} + +static void +free_node_entry(void* value, void* user_data) +{ + SordNode* node = (SordNode*)value; + if (node->node.type == SERD_LITERAL) { + sord_node_free((SordWorld*)user_data, node->meta.lit.datatype); + } + free((uint8_t*)node->node.buf); +} + +void +sord_world_free(SordWorld* world) +{ + zix_hash_foreach(world->nodes, free_node_entry, world); + zix_hash_free(world->nodes); + free(world); +} + +void +sord_world_set_error_sink(SordWorld* world, + SerdErrorSink error_sink, + void* handle) +{ + world->error_sink = error_sink; + world->error_handle = handle; +} + +static inline int +sord_node_compare_literal(const SordNode* a, const SordNode* b) +{ + const int cmp = strcmp((const char*)sord_node_get_string(a), + (const char*)sord_node_get_string(b)); + if (cmp != 0) { + return cmp; + } + + const bool a_has_lang = a->meta.lit.lang[0]; + const bool b_has_lang = b->meta.lit.lang[0]; + const bool a_has_datatype = a->meta.lit.datatype; + const bool b_has_datatype = b->meta.lit.datatype; + const bool a_has_meta = a_has_lang || a_has_datatype; + const bool b_has_meta = b_has_lang || b_has_datatype; + + assert(!(a_has_lang && a_has_datatype)); + assert(!(b_has_lang && b_has_datatype)); + + if (!a_has_meta && !b_has_meta) { + return 0; + } else if (!a_has_meta || (a_has_lang && b_has_datatype)) { + return -1; + } else if (!b_has_meta || (a_has_datatype && b_has_lang)) { + return 1; + } else if (a_has_lang) { + assert(b_has_lang); + return strcmp(a->meta.lit.lang, b->meta.lit.lang); + } + + assert(a_has_datatype); + assert(b_has_datatype); + return strcmp((const char*)a->meta.lit.datatype->node.buf, + (const char*)b->meta.lit.datatype->node.buf); +} + +/** Compare nodes, considering NULL a wildcard match. */ +static inline int +sord_node_compare(const SordNode* a, const SordNode* b) +{ + if (a == b || !a || !b) { + return 0; // Exact or wildcard match + } else if (a->node.type != b->node.type) { + return (a->node.type < b->node.type) ? -1 : 1; + } + + int cmp = 0; + switch (a->node.type) { + case SERD_URI: + case SERD_BLANK: + return strcmp((const char*)a->node.buf, (const char*)b->node.buf); + case SERD_LITERAL: + cmp = sord_node_compare_literal(a, b); + break; + default: + break; + } + return cmp; +} + +bool +sord_node_equals(const SordNode* a, const SordNode* b) +{ + return a == b; // Nodes are interned +} + +/** Return true iff IDs are equivalent, or one is a wildcard */ +static inline bool +sord_id_match(const SordNode* a, const SordNode* b) +{ + return !a || !b || (a == b); +} + +static inline bool +sord_quad_match_inline(const SordQuad x, const SordQuad y) +{ + return sord_id_match(x[0], y[0]) && sord_id_match(x[1], y[1]) && + sord_id_match(x[2], y[2]) && sord_id_match(x[3], y[3]); +} + +bool +sord_quad_match(const SordQuad x, const SordQuad y) +{ + return sord_quad_match_inline(x, y); +} + +/** + Compare two quad IDs lexicographically. + NULL IDs (equal to 0) are treated as wildcards, always less than every + other possible ID, except itself. +*/ +static int +sord_quad_compare(const void* x_ptr, const void* y_ptr, const void* user_data) +{ + const int* const ordering = (const int*)user_data; + const SordNode* const* const x = (const SordNode* const*)x_ptr; + const SordNode* const* const y = (const SordNode* const*)y_ptr; + + for (int i = 0; i < TUP_LEN; ++i) { + const int idx = ordering[i]; + const int cmp = sord_node_compare(x[idx], y[idx]); + if (cmp) { + return cmp; + } + } + + return 0; +} + +static inline bool +sord_iter_forward(SordIter* iter) +{ + if (!iter->skip_graphs) { + zix_btree_iter_increment(iter->cur); + return zix_btree_iter_is_end(iter->cur); + } + + SordNode** key = (SordNode**)zix_btree_get(iter->cur); + const SordQuad initial = {key[0], key[1], key[2], key[3]}; + zix_btree_iter_increment(iter->cur); + while (!zix_btree_iter_is_end(iter->cur)) { + key = (SordNode**)zix_btree_get(iter->cur); + for (int i = 0; i < 3; ++i) { + if (key[i] != initial[i]) { + return false; + } + } + + zix_btree_iter_increment(iter->cur); + } + + return true; +} + +/** + Seek forward as necessary until `iter` points at a match. + @return true iff iterator reached end of valid range. +*/ +static inline bool +sord_iter_seek_match(SordIter* iter) +{ + for (iter->end = true; !zix_btree_iter_is_end(iter->cur); + sord_iter_forward(iter)) { + const SordNode** const key = (const SordNode**)zix_btree_get(iter->cur); + if (sord_quad_match_inline(key, iter->pat)) { + return (iter->end = false); + } + } + return true; +} + +/** + Seek forward as necessary until `iter` points at a match, or the prefix + no longer matches iter->pat. + @return true iff iterator reached end of valid range. +*/ +static inline bool +sord_iter_seek_match_range(SordIter* iter) +{ + assert(!iter->end); + + do { + const SordNode** key = (const SordNode**)zix_btree_get(iter->cur); + + if (sord_quad_match_inline(key, iter->pat)) { + return false; // Found match + } + + for (int i = 0; i < iter->n_prefix; ++i) { + const int idx = orderings[iter->order][i]; + if (!sord_id_match(key[idx], iter->pat[idx])) { + iter->end = true; // Reached end of valid range + return true; + } + } + } while (!sord_iter_forward(iter)); + + return (iter->end = true); // Reached end +} + +static SordIter* +sord_iter_new(const SordModel* sord, + ZixBTreeIter* cur, + const SordQuad pat, + SordOrder order, + SearchMode mode, + int n_prefix) +{ + SordIter* iter = (SordIter*)malloc(sizeof(SordIter)); + iter->sord = sord; + iter->cur = cur; + iter->order = order; + iter->mode = mode; + iter->n_prefix = n_prefix; + iter->end = false; + iter->skip_graphs = order < GSPO; + for (int i = 0; i < TUP_LEN; ++i) { + iter->pat[i] = pat[i]; + } + + switch (iter->mode) { + case ALL: + case SINGLE: + case RANGE: + assert(sord_quad_match_inline((const SordNode**)zix_btree_get(iter->cur), + iter->pat)); + break; + case FILTER_RANGE: + sord_iter_seek_match_range(iter); + break; + case FILTER_ALL: + sord_iter_seek_match(iter); + break; + } + +#ifdef SORD_DEBUG_ITER + SordQuad value; + sord_iter_get(iter, value); + SORD_ITER_LOG("New %p pat=" TUP_FMT " cur=" TUP_FMT " end=%d skip=%d\n", + (void*)iter, + TUP_FMT_ARGS(pat), + TUP_FMT_ARGS(value), + iter->end, + iter->skip_graphs); +#endif + + ++((SordModel*)sord)->n_iters; + return iter; +} + +const SordModel* +sord_iter_get_model(SordIter* iter) +{ + return iter->sord; +} + +void +sord_iter_get(const SordIter* iter, SordQuad tup) +{ + SordNode** key = (SordNode**)zix_btree_get(iter->cur); + for (int i = 0; i < TUP_LEN; ++i) { + tup[i] = key[i]; + } +} + +const SordNode* +sord_iter_get_node(const SordIter* iter, SordQuadIndex index) +{ + return (!sord_iter_end(iter) ? ((SordNode**)zix_btree_get(iter->cur))[index] + : NULL); +} + +static bool +sord_iter_scan_next(SordIter* iter) +{ + if (iter->end) { + return true; + } + + const SordNode** key; + if (!iter->end) { + switch (iter->mode) { + case ALL: + // At the end if the cursor is (assigned above) + break; + case SINGLE: + iter->end = true; + SORD_ITER_LOG("%p reached single end\n", (void*)iter); + break; + case RANGE: + SORD_ITER_LOG("%p range next\n", (void*)iter); + // At the end if the MSNs no longer match + key = (const SordNode**)zix_btree_get(iter->cur); + assert(key); + for (int i = 0; i < iter->n_prefix; ++i) { + const int idx = orderings[iter->order][i]; + if (!sord_id_match(key[idx], iter->pat[idx])) { + iter->end = true; + SORD_ITER_LOG("%p reached non-match end\n", (void*)iter); + break; + } + } + break; + case FILTER_RANGE: + // Seek forward to next match, stopping if prefix changes + sord_iter_seek_match_range(iter); + break; + case FILTER_ALL: + // Seek forward to next match + sord_iter_seek_match(iter); + break; + } + } else { + SORD_ITER_LOG("%p reached index end\n", (void*)iter); + } + + if (iter->end) { + SORD_ITER_LOG("%p Reached end\n", (void*)iter); + return true; + } else { +#ifdef SORD_DEBUG_ITER + SordQuad tup; + sord_iter_get(iter, tup); + SORD_ITER_LOG( + "%p Increment to " TUP_FMT "\n", (void*)iter, TUP_FMT_ARGS(tup)); +#endif + return false; + } +} + +bool +sord_iter_next(SordIter* iter) +{ + if (iter->end) { + return true; + } + + iter->end = sord_iter_forward(iter); + return sord_iter_scan_next(iter); +} + +bool +sord_iter_end(const SordIter* iter) +{ + return !iter || iter->end; +} + +void +sord_iter_free(SordIter* iter) +{ + SORD_ITER_LOG("%p Free\n", (void*)iter); + if (iter) { + --((SordModel*)iter->sord)->n_iters; + zix_btree_iter_free(iter->cur); + free(iter); + } +} + +/** + Return true iff `sord` has an index for `order`. + If `graphs` is true, `order` will be modified to be the + corresponding order with a G prepended (so G will be the MSN). +*/ +static inline bool +sord_has_index(SordModel* model, SordOrder* order, int* n_prefix, bool graphs) +{ + if (graphs) { + *order = (SordOrder)(*order + GSPO); + *n_prefix += 1; + } + + return model->indices[*order]; +} + +/** + Return the best available index for a pattern. + @param pat Pattern in standard (S P O G) order + @param mode Set to the (best) iteration mode for iterating over results + @param n_prefix Set to the length of the range prefix + (for `mode` == RANGE and `mode` == FILTER_RANGE) +*/ +static inline SordOrder +sord_best_index(SordModel* sord, + const SordQuad pat, + SearchMode* mode, + int* n_prefix) +{ + const bool graph_search = (pat[TUP_G] != 0); + + const unsigned sig = (pat[0] ? 1 : 0) * 0x100 + (pat[1] ? 1 : 0) * 0x010 + + (pat[2] ? 1 : 0) * 0x001; + + SordOrder good[2] = {(SordOrder)-1, (SordOrder)-1}; + +#define PAT_CASE(sig, m, g0, g1, np) \ + case sig: \ + *mode = m; \ + good[0] = g0; \ + good[1] = g1; \ + *n_prefix = np; \ + break + + // Good orderings that don't require filtering + *mode = RANGE; + *n_prefix = 0; + switch (sig) { + case 0x000: + assert(graph_search); + *mode = RANGE; + *n_prefix = 1; + return DEFAULT_GRAPH_ORDER; + case 0x111: + *mode = SINGLE; + return graph_search ? DEFAULT_GRAPH_ORDER : DEFAULT_ORDER; + + PAT_CASE(0x001, RANGE, OPS, OSP, 1); + PAT_CASE(0x010, RANGE, POS, PSO, 1); + PAT_CASE(0x011, RANGE, OPS, POS, 2); + PAT_CASE(0x100, RANGE, SPO, SOP, 1); + PAT_CASE(0x101, RANGE, SOP, OSP, 2); + PAT_CASE(0x110, RANGE, SPO, PSO, 2); + } + + if (*mode == RANGE) { + if (sord_has_index(sord, &good[0], n_prefix, graph_search)) { + return good[0]; + } else if (sord_has_index(sord, &good[1], n_prefix, graph_search)) { + return good[1]; + } + } + + // Not so good orderings that require filtering, but can + // still be constrained to a range + switch (sig) { + PAT_CASE(0x011, FILTER_RANGE, OSP, PSO, 1); + PAT_CASE(0x101, FILTER_RANGE, SPO, OPS, 1); + // SPO is always present, so 0x110 is never reached here + default: + break; + } + + if (*mode == FILTER_RANGE) { + if (sord_has_index(sord, &good[0], n_prefix, graph_search)) { + return good[0]; + } else if (sord_has_index(sord, &good[1], n_prefix, graph_search)) { + return good[1]; + } + } + + if (graph_search) { + *mode = FILTER_RANGE; + *n_prefix = 1; + return DEFAULT_GRAPH_ORDER; + } else { + *mode = FILTER_ALL; + return DEFAULT_ORDER; + } +} + +SordModel* +sord_new(SordWorld* world, unsigned indices, bool graphs) +{ + SordModel* model = (SordModel*)malloc(sizeof(struct SordModelImpl)); + model->world = world; + model->n_quads = 0; + model->n_iters = 0; + + for (unsigned i = 0; i < (NUM_ORDERS / 2); ++i) { + const int* const ordering = orderings[i]; + const int* const g_ordering = orderings[i + (NUM_ORDERS / 2)]; + + if (indices & (1 << i)) { + model->indices[i] = + zix_btree_new(sord_quad_compare, (void*)ordering, NULL); + if (graphs) { + model->indices[i + (NUM_ORDERS / 2)] = + zix_btree_new(sord_quad_compare, (void*)g_ordering, NULL); + } else { + model->indices[i + (NUM_ORDERS / 2)] = NULL; + } + } else { + model->indices[i] = NULL; + model->indices[i + (NUM_ORDERS / 2)] = NULL; + } + } + + if (!model->indices[DEFAULT_ORDER]) { + model->indices[DEFAULT_ORDER] = + zix_btree_new(sord_quad_compare, (void*)orderings[DEFAULT_ORDER], NULL); + } + if (graphs && !model->indices[DEFAULT_GRAPH_ORDER]) { + model->indices[DEFAULT_GRAPH_ORDER] = zix_btree_new( + sord_quad_compare, (void*)orderings[DEFAULT_GRAPH_ORDER], NULL); + } + + return model; +} + +static void +sord_node_free_internal(SordWorld* world, SordNode* node) +{ + assert(node->refs == 0); + + // If you hit this, the world has probably been destroyed too early + assert(world); + + // Cache pointer to buffer to free after node removal and destruction + const uint8_t* const buf = node->node.buf; + + // Remove node from hash (which frees the node) + if (zix_hash_remove(world->nodes, node)) { + error(world, SERD_ERR_INTERNAL, "failed to remove node from hash\n"); + } + + // Free buffer + free((uint8_t*)buf); +} + +static void +sord_add_quad_ref(SordModel* model, const SordNode* node, SordQuadIndex i) +{ + if (node) { + assert(node->refs > 0); + ++((SordNode*)node)->refs; + if (node->node.type != SERD_LITERAL && i == SORD_OBJECT) { + ++((SordNode*)node)->meta.res.refs_as_obj; + } + } +} + +static void +sord_drop_quad_ref(SordModel* model, const SordNode* node, SordQuadIndex i) +{ + if (!node) { + return; + } + + assert(node->refs > 0); + if (node->node.type != SERD_LITERAL && i == SORD_OBJECT) { + assert(node->meta.res.refs_as_obj > 0); + --((SordNode*)node)->meta.res.refs_as_obj; + } + if (--((SordNode*)node)->refs == 0) { + sord_node_free_internal(sord_get_world(model), (SordNode*)node); + } +} + +void +sord_free(SordModel* model) +{ + if (!model) { + return; + } + + // Free nodes + SordQuad tup; + SordIter* i = sord_begin(model); + for (; !sord_iter_end(i); sord_iter_next(i)) { + sord_iter_get(i, tup); + for (int t = 0; t < TUP_LEN; ++t) { + sord_drop_quad_ref(model, tup[t], (SordQuadIndex)t); + } + } + sord_iter_free(i); + + // Free quads + ZixBTreeIter* t = zix_btree_begin(model->indices[DEFAULT_ORDER]); + for (; !zix_btree_iter_is_end(t); zix_btree_iter_increment(t)) { + free(zix_btree_get(t)); + } + zix_btree_iter_free(t); + + // Free indices + for (unsigned o = 0; o < NUM_ORDERS; ++o) { + if (model->indices[o]) { + zix_btree_free(model->indices[o]); + } + } + + free(model); +} + +SordWorld* +sord_get_world(SordModel* model) +{ + return model->world; +} + +size_t +sord_num_quads(const SordModel* model) +{ + return model->n_quads; +} + +size_t +sord_num_nodes(const SordWorld* world) +{ + return zix_hash_size(world->nodes); +} + +SordIter* +sord_begin(const SordModel* model) +{ + if (sord_num_quads(model) == 0) { + return NULL; + } else { + ZixBTreeIter* cur = zix_btree_begin(model->indices[DEFAULT_ORDER]); + SordQuad pat = {0, 0, 0, 0}; + return sord_iter_new(model, cur, pat, DEFAULT_ORDER, ALL, 0); + } +} + +SordIter* +sord_find(SordModel* model, const SordQuad pat) +{ + if (!pat[0] && !pat[1] && !pat[2] && !pat[3]) { + return sord_begin(model); + } + + SearchMode mode; + int n_prefix; + const SordOrder index_order = sord_best_index(model, pat, &mode, &n_prefix); + + SORD_FIND_LOG("Find " TUP_FMT " index=%s mode=%u n_prefix=%d\n", + TUP_FMT_ARGS(pat), + order_names[index_order], + mode, + n_prefix); + + if (pat[0] && pat[1] && pat[2] && pat[3]) { + mode = SINGLE; // No duplicate quads (Sord is a set) + } + + ZixBTree* const db = model->indices[index_order]; + ZixBTreeIter* cur = NULL; + + if (mode == FILTER_ALL) { + // No prefix shared with an index at all, linear search (worst case) + cur = zix_btree_begin(db); + } else if (mode == FILTER_RANGE) { + /* Some prefix, but filtering still required. Build a search pattern + with only the prefix to find the lower bound in log time. */ + SordQuad prefix_pat = {NULL, NULL, NULL, NULL}; + const int* const ordering = orderings[index_order]; + for (int i = 0; i < n_prefix; ++i) { + prefix_pat[ordering[i]] = pat[ordering[i]]; + } + zix_btree_lower_bound(db, prefix_pat, &cur); + } else { + // Ideal case, pattern matches an index with no filtering required + zix_btree_lower_bound(db, pat, &cur); + } + + if (zix_btree_iter_is_end(cur)) { + SORD_FIND_LOG("No match found\n"); + zix_btree_iter_free(cur); + return NULL; + } + const SordNode** const key = (const SordNode**)zix_btree_get(cur); + if (!key || ((mode == RANGE || mode == SINGLE) && + !sord_quad_match_inline(pat, key))) { + SORD_FIND_LOG("No match found\n"); + zix_btree_iter_free(cur); + return NULL; + } + + return sord_iter_new(model, cur, pat, index_order, mode, n_prefix); +} + +SordIter* +sord_search(SordModel* model, + const SordNode* s, + const SordNode* p, + const SordNode* o, + const SordNode* g) +{ + SordQuad pat = {s, p, o, g}; + return sord_find(model, pat); +} + +SordNode* +sord_get(SordModel* model, + const SordNode* s, + const SordNode* p, + const SordNode* o, + const SordNode* g) +{ + if ((bool)s + (bool)p + (bool)o != 2) { + return NULL; + } + + SordIter* i = sord_search(model, s, p, o, g); + SordNode* ret = NULL; + if (!s) { + ret = sord_node_copy(sord_iter_get_node(i, SORD_SUBJECT)); + } else if (!p) { + ret = sord_node_copy(sord_iter_get_node(i, SORD_PREDICATE)); + } else if (!o) { + ret = sord_node_copy(sord_iter_get_node(i, SORD_OBJECT)); + } + + sord_iter_free(i); + return ret; +} + +bool +sord_ask(SordModel* model, + const SordNode* s, + const SordNode* p, + const SordNode* o, + const SordNode* g) +{ + SordQuad pat = {s, p, o, g}; + return sord_contains(model, pat); +} + +uint64_t +sord_count(SordModel* model, + const SordNode* s, + const SordNode* p, + const SordNode* o, + const SordNode* g) +{ + SordIter* i = sord_search(model, s, p, o, g); + uint64_t n = 0; + for (; !sord_iter_end(i); sord_iter_next(i)) { + ++n; + } + sord_iter_free(i); + return n; +} + +bool +sord_contains(SordModel* model, const SordQuad pat) +{ + SordIter* iter = sord_find(model, pat); + bool ret = (iter != NULL); + sord_iter_free(iter); + return ret; +} + +static uint8_t* +sord_strndup(const uint8_t* str, size_t len) +{ + uint8_t* dup = (uint8_t*)malloc(len + 1); + memcpy(dup, str, len + 1); + return dup; +} + +SordNodeType +sord_node_get_type(const SordNode* node) +{ + switch (node->node.type) { + case SERD_URI: + return SORD_URI; + case SERD_BLANK: + return SORD_BLANK; + default: + return SORD_LITERAL; + } + SORD_UNREACHABLE(); +} + +const uint8_t* +sord_node_get_string(const SordNode* node) +{ + return node->node.buf; +} + +const uint8_t* +sord_node_get_string_counted(const SordNode* node, size_t* bytes) +{ + *bytes = node->node.n_bytes; + return node->node.buf; +} + +const uint8_t* +sord_node_get_string_measured(const SordNode* node, + size_t* bytes, + size_t* chars) +{ + *bytes = node->node.n_bytes; + *chars = node->node.n_chars; + return node->node.buf; +} + +const char* +sord_node_get_language(const SordNode* node) +{ + if (node->node.type != SERD_LITERAL || !node->meta.lit.lang[0]) { + return NULL; + } + return node->meta.lit.lang; +} + +SordNode* +sord_node_get_datatype(const SordNode* node) +{ + return (node->node.type == SERD_LITERAL) ? node->meta.lit.datatype : NULL; +} + +SerdNodeFlags +sord_node_get_flags(const SordNode* node) +{ + return node->node.flags; +} + +bool +sord_node_is_inline_object(const SordNode* node) +{ + return (node->node.type == SERD_BLANK) && (node->meta.res.refs_as_obj == 1); +} + +static SordNode* +sord_insert_node(SordWorld* world, const SordNode* key, bool copy) +{ + SordNode* node = NULL; + ZixStatus st = zix_hash_insert(world->nodes, key, (void**)&node); + switch (st) { + case ZIX_STATUS_EXISTS: + ++node->refs; + break; + case ZIX_STATUS_SUCCESS: + assert(node->refs == 1); + if (copy) { + node->node.buf = sord_strndup(node->node.buf, node->node.n_bytes); + } + if (node->node.type == SERD_LITERAL) { + node->meta.lit.datatype = sord_node_copy(node->meta.lit.datatype); + } + return node; + default: + error( + world, SERD_ERR_INTERNAL, "error inserting node `%s'\n", key->node.buf); + } + + if (!copy) { + // Free the buffer we would have copied if a new node was created + free((uint8_t*)key->node.buf); + } + + return node; +} + +static SordNode* +sord_new_uri_counted(SordWorld* world, + const uint8_t* str, + size_t n_bytes, + size_t n_chars, + bool copy) +{ + if (!serd_uri_string_has_scheme(str)) { + error(world, SERD_ERR_BAD_ARG, "attempt to map invalid URI `%s'\n", str); + return NULL; // Can't intern relative URIs + } + + const SordNode key = {{str, n_bytes, n_chars, 0, SERD_URI}, 1, {{0}}}; + + return sord_insert_node(world, &key, copy); +} + +SordNode* +sord_new_uri(SordWorld* world, const uint8_t* uri) +{ + const SerdNode node = serd_node_from_string(SERD_URI, uri); + return sord_new_uri_counted(world, uri, node.n_bytes, node.n_chars, true); +} + +SordNode* +sord_new_relative_uri(SordWorld* world, + const uint8_t* uri, + const uint8_t* base_uri) +{ + if (serd_uri_string_has_scheme(uri)) { + return sord_new_uri(world, uri); + } + SerdURI buri = SERD_URI_NULL; + SerdNode base = serd_node_new_uri_from_string(base_uri, NULL, &buri); + SerdNode node = serd_node_new_uri_from_string(uri, &buri, NULL); + + SordNode* ret = + sord_new_uri_counted(world, node.buf, node.n_bytes, node.n_chars, false); + + serd_node_free(&base); + return ret; +} + +static SordNode* +sord_new_blank_counted(SordWorld* world, + const uint8_t* str, + size_t n_bytes, + size_t n_chars) +{ + const SordNode key = {{str, n_bytes, n_chars, 0, SERD_BLANK}, 1, {{0}}}; + + return sord_insert_node(world, &key, true); +} + +SordNode* +sord_new_blank(SordWorld* world, const uint8_t* str) +{ + const SerdNode node = serd_node_from_string(SERD_URI, str); + return sord_new_blank_counted(world, str, node.n_bytes, node.n_chars); +} + +static SordNode* +sord_new_literal_counted(SordWorld* world, + SordNode* datatype, + const uint8_t* str, + size_t n_bytes, + size_t n_chars, + SerdNodeFlags flags, + const char* lang) +{ + SordNode key = {{str, n_bytes, n_chars, flags, SERD_LITERAL}, 1, {{0}}}; + key.meta.lit.datatype = sord_node_copy(datatype); + memset(key.meta.lit.lang, 0, sizeof(key.meta.lit.lang)); + if (lang) { + strncpy(key.meta.lit.lang, lang, sizeof(key.meta.lit.lang) - 1); + } + + return sord_insert_node(world, &key, true); +} + +SordNode* +sord_new_literal(SordWorld* world, + SordNode* datatype, + const uint8_t* str, + const char* lang) +{ + SerdNodeFlags flags = 0; + size_t n_bytes = 0; + size_t n_chars = serd_strlen(str, &n_bytes, &flags); + return sord_new_literal_counted( + world, datatype, str, n_bytes, n_chars, flags, lang); +} + +SordNode* +sord_node_from_serd_node(SordWorld* world, + SerdEnv* env, + const SerdNode* node, + const SerdNode* datatype, + const SerdNode* lang) +{ + if (!node) { + return NULL; + } + + SordNode* datatype_node = NULL; + SordNode* ret = NULL; + switch (node->type) { + case SERD_NOTHING: + return NULL; + case SERD_LITERAL: + datatype_node = sord_node_from_serd_node(world, env, datatype, NULL, NULL); + ret = sord_new_literal_counted(world, + datatype_node, + node->buf, + node->n_bytes, + node->n_chars, + node->flags, + lang ? (const char*)lang->buf : NULL); + sord_node_free(world, datatype_node); + return ret; + case SERD_URI: + if (serd_uri_string_has_scheme(node->buf)) { + return sord_new_uri_counted( + world, node->buf, node->n_bytes, node->n_chars, true); + } else { + SerdURI base_uri; + serd_env_get_base_uri(env, &base_uri); + SerdURI abs_uri; + SerdNode abs_uri_node = + serd_node_new_uri_from_node(node, &base_uri, &abs_uri); + ret = sord_new_uri_counted(world, + abs_uri_node.buf, + abs_uri_node.n_bytes, + abs_uri_node.n_chars, + true); + serd_node_free(&abs_uri_node); + return ret; + } + case SERD_CURIE: { + SerdChunk uri_prefix; + SerdChunk uri_suffix; + if (serd_env_expand(env, node, &uri_prefix, &uri_suffix)) { + error( + world, SERD_ERR_BAD_CURIE, "failed to expand CURIE `%s'\n", node->buf); + return NULL; + } + const size_t uri_len = uri_prefix.len + uri_suffix.len; + uint8_t* buf = (uint8_t*)malloc(uri_len + 1); + memcpy(buf, uri_prefix.buf, uri_prefix.len); + memcpy(buf + uri_prefix.len, uri_suffix.buf, uri_suffix.len); + buf[uri_len] = '\0'; + ret = sord_new_uri_counted( + world, buf, uri_len, serd_strlen(buf, NULL, NULL), false); + return ret; + } + case SERD_BLANK: + return sord_new_blank_counted( + world, node->buf, node->n_bytes, node->n_chars); + } + return NULL; +} + +const SerdNode* +sord_node_to_serd_node(const SordNode* node) +{ + return node ? &node->node : &SERD_NODE_NULL; +} + +void +sord_node_free(SordWorld* world, SordNode* node) +{ + if (!node) { + return; + } else if (node->refs == 0) { + error(world, SERD_ERR_BAD_ARG, "attempt to free garbage node\n"); + } else if (--node->refs == 0) { + sord_node_free_internal(world, node); + } +} + +SordNode* +sord_node_copy(const SordNode* node) +{ + SordNode* copy = (SordNode*)node; + if (copy) { + ++copy->refs; + } + return copy; +} + +static inline bool +sord_add_to_index(SordModel* model, const SordNode** tup, SordOrder order) +{ + return !zix_btree_insert(model->indices[order], tup); +} + +bool +sord_add(SordModel* model, const SordQuad tup) +{ + SORD_WRITE_LOG("Add " TUP_FMT "\n", TUP_FMT_ARGS(tup)); + if (!tup[0] || !tup[1] || !tup[2]) { + error( + model->world, SERD_ERR_BAD_ARG, "attempt to add quad with NULL field\n"); + return false; + } else if (model->n_iters > 0) { + error(model->world, SERD_ERR_BAD_ARG, "added tuple during iteration\n"); + } + + const SordNode** quad = (const SordNode**)malloc(sizeof(SordQuad)); + memcpy(quad, tup, sizeof(SordQuad)); + + for (unsigned i = 0; i < NUM_ORDERS; ++i) { + if (model->indices[i] && (i < GSPO || tup[3])) { + if (!sord_add_to_index(model, quad, (SordOrder)i)) { + assert(i == 0); // Assuming index coherency + free(quad); + return false; // Quad already stored, do nothing + } + } + } + + for (int i = 0; i < TUP_LEN; ++i) { + sord_add_quad_ref(model, tup[i], (SordQuadIndex)i); + } + + ++model->n_quads; + return true; +} + +void +sord_remove(SordModel* model, const SordQuad tup) +{ + SORD_WRITE_LOG("Remove " TUP_FMT "\n", TUP_FMT_ARGS(tup)); + if (model->n_iters > 0) { + error(model->world, SERD_ERR_BAD_ARG, "remove with iterator\n"); + } + + SordNode* quad = NULL; + for (unsigned i = 0; i < NUM_ORDERS; ++i) { + if (model->indices[i] && (i < GSPO || tup[3])) { + if (zix_btree_remove(model->indices[i], tup, (void**)&quad, NULL)) { + assert(i == 0); // Assuming index coherency + return; // Quad not found, do nothing + } + } + } + + free(quad); + + for (int i = 0; i < TUP_LEN; ++i) { + sord_drop_quad_ref(model, tup[i], (SordQuadIndex)i); + } + + --model->n_quads; +} + +SerdStatus +sord_erase(SordModel* model, SordIter* iter) +{ + if (model->n_iters > 1) { + error(model->world, SERD_ERR_BAD_ARG, "erased with many iterators\n"); + return SERD_ERR_BAD_ARG; + } + + SordQuad tup; + sord_iter_get(iter, tup); + + SORD_WRITE_LOG("Remove " TUP_FMT "\n", TUP_FMT_ARGS(tup)); + + SordNode* quad = NULL; + for (unsigned i = 0; i < NUM_ORDERS; ++i) { + if (model->indices[i] && (i < GSPO || tup[3])) { + if (zix_btree_remove(model->indices[i], + tup, + (void**)&quad, + i == iter->order ? &iter->cur : NULL)) { + return (i == 0) ? SERD_ERR_NOT_FOUND : SERD_ERR_INTERNAL; + } + } + } + iter->end = zix_btree_iter_is_end(iter->cur); + sord_iter_scan_next(iter); + + free(quad); + + for (int i = 0; i < TUP_LEN; ++i) { + sord_drop_quad_ref(model, tup[i], (SordQuadIndex)i); + } + + --model->n_quads; + return SERD_SUCCESS; +} diff --git a/modules/juce_audio_processors/format_types/lv2/sord/src/sord_config.h b/modules/juce_audio_processors/format_types/lv2/sord/src/sord_config.h new file mode 100644 index 0000000000..801f968105 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/sord/src/sord_config.h @@ -0,0 +1,61 @@ +/* + Copyright 2021 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +/* + Configuration header that defines reasonable defaults at compile time. + + This allows compile-time configuration from the command line (typically via + the build system) while still allowing the source to be built without any + configuration. The build system can define SORD_NO_DEFAULT_CONFIG to disable + defaults, in which case it must define things like HAVE_FEATURE to enable + features. The design here ensures that compiler warnings or + include-what-you-use will catch any mistakes. +*/ + +#ifndef SORD_CONFIG_H +#define SORD_CONFIG_H + +// Define version unconditionally so a warning will catch a mismatch +#define SORD_VERSION "0.16.9" + +#if !defined(SORD_NO_DEFAULT_CONFIG) + +// The validator uses PCRE for literal pattern matching +# ifndef HAVE_PCRE +# ifdef __has_include +# if __has_include() +# define HAVE_PCRE 1 +# endif +# endif +# endif + +#endif // !defined(SORD_NO_DEFAULT_CONFIG) + +/* + Make corresponding USE_FEATURE defines based on the HAVE_FEATURE defines from + above or the command line. The code checks for these using #if (not #ifdef), + so there will be an undefined warning if it checks for an unknown feature, + and this header is always required by any code that checks for features, even + if the build system defines them all. +*/ + +#ifdef HAVE_PCRE +# define USE_PCRE 1 +#else +# define USE_PCRE 1 +#endif + +#endif // SORD_CONFIG_H diff --git a/modules/juce_audio_processors/format_types/lv2/sord/src/sord_internal.h b/modules/juce_audio_processors/format_types/lv2/sord/src/sord_internal.h new file mode 100644 index 0000000000..4aef846149 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/sord/src/sord_internal.h @@ -0,0 +1,53 @@ +/* + Copyright 2011-2015 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef SORD_SORD_INTERNAL_H +#define SORD_SORD_INTERNAL_H + +#include "serd/serd.h" +#include "sord/sord.h" + +#include + +#if defined(__clang__) || (defined(__GNUC__) && __GNUC__ > 4) +# define SORD_UNREACHABLE() __builtin_unreachable() +#else +# include +# define SORD_UNREACHABLE() assert(0) +#endif + +/** Resource node metadata */ +typedef struct { + size_t refs_as_obj; ///< References as a quad object +} SordResourceMetadata; + +/** Literal node metadata */ +typedef struct { + SordNode* datatype; ///< Optional literal data type URI + char lang[16]; ///< Optional language tag +} SordLiteralMetadata; + +/** Node */ +struct SordNodeImpl { + SerdNode node; ///< Serd node + size_t refs; ///< Reference count (# of containing quads) + union { + SordResourceMetadata res; + SordLiteralMetadata lit; + } meta; +}; + +#endif /* SORD_SORD_INTERNAL_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/sord/src/sord_test.c b/modules/juce_audio_processors/format_types/lv2/sord/src/sord_test.c new file mode 100644 index 0000000000..7e7a6c89e4 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/sord/src/sord_test.c @@ -0,0 +1,767 @@ +/* + Copyright 2011-2016 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "serd/serd.h" +#include "sord/sord.h" + +#include +#include +#include +#include +#include +#include +#include + +#ifdef __GNUC__ +# define SORD_LOG_FUNC(fmt, arg1) __attribute__((format(printf, fmt, arg1))) +#else +# define SORD_LOG_FUNC(fmt, arg1) +#endif + +static const int DIGITS = 3; +static const unsigned n_objects_per = 2; + +static int n_expected_errors = 0; + +typedef struct { + SordQuad query; + int expected_num_results; +} QueryTest; + +#define USTR(s) ((const uint8_t*)(s)) + +static SordNode* +uri(SordWorld* world, int num) +{ + if (num == 0) { + return 0; + } + + char str[] = "eg:000"; + char* uri_num = str + 3; // First `0' + snprintf(uri_num, DIGITS + 1, "%0*d", DIGITS, num); + return sord_new_uri(world, (const uint8_t*)str); +} + +SORD_LOG_FUNC(1, 2) +static int +test_fail(const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + fprintf(stderr, "error: "); + vfprintf(stderr, fmt, args); + va_end(args); + return 1; +} + +static int +generate(SordWorld* world, SordModel* sord, size_t n_quads, SordNode* graph) +{ + fprintf(stderr, + "Generating %zu (S P *) quads with %u objects each\n", + n_quads, + n_objects_per); + + for (size_t i = 0; i < n_quads; ++i) { + int num = (i * n_objects_per) + 1; + + SordNode* ids[2 + n_objects_per]; + for (unsigned j = 0; j < 2 + n_objects_per; ++j) { + ids[j] = uri(world, num++); + } + + for (unsigned j = 0; j < n_objects_per; ++j) { + SordQuad tup = {ids[0], ids[1], ids[2 + j], graph}; + if (!sord_add(sord, tup)) { + return test_fail("Fail: Failed to add quad\n"); + } + } + + for (unsigned j = 0; j < 2 + n_objects_per; ++j) { + sord_node_free(world, ids[j]); + } + } + + // Add some literals + + // (98 4 "hello") and (98 4 "hello"^^<5>) + SordQuad tup = {0, 0, 0, 0}; + tup[0] = uri(world, 98); + tup[1] = uri(world, 4); + tup[2] = sord_new_literal(world, 0, USTR("hello"), NULL); + tup[3] = graph; + sord_add(sord, tup); + sord_node_free(world, (SordNode*)tup[2]); + tup[2] = sord_new_literal(world, uri(world, 5), USTR("hello"), NULL); + if (!sord_add(sord, tup)) { + return test_fail("Failed to add typed literal\n"); + } + + // (96 4 "hello"^^<4>) and (96 4 "hello"^^<5>) + tup[0] = uri(world, 96); + tup[1] = uri(world, 4); + tup[2] = sord_new_literal(world, uri(world, 4), USTR("hello"), NULL); + tup[3] = graph; + sord_add(sord, tup); + sord_node_free(world, (SordNode*)tup[2]); + tup[2] = sord_new_literal(world, uri(world, 5), USTR("hello"), NULL); + if (!sord_add(sord, tup)) { + return test_fail("Failed to add typed literal\n"); + } + + // (94 5 "hello") and (94 5 "hello"@en-gb) + tup[0] = uri(world, 94); + tup[1] = uri(world, 5); + tup[2] = sord_new_literal(world, 0, USTR("hello"), NULL); + tup[3] = graph; + sord_add(sord, tup); + sord_node_free(world, (SordNode*)tup[2]); + tup[2] = sord_new_literal(world, NULL, USTR("hello"), "en-gb"); + if (!sord_add(sord, tup)) { + return test_fail("Failed to add literal with language\n"); + } + + // (92 6 "hello"@en-us) and (92 5 "hello"@en-gb) + tup[0] = uri(world, 92); + tup[1] = uri(world, 6); + tup[2] = sord_new_literal(world, 0, USTR("hello"), "en-us"); + tup[3] = graph; + sord_add(sord, tup); + sord_node_free(world, (SordNode*)tup[2]); + tup[2] = sord_new_literal(world, NULL, USTR("hello"), "en-gb"); + if (!sord_add(sord, tup)) { + return test_fail("Failed to add literal with language\n"); + } + + sord_node_free(world, (SordNode*)tup[0]); + sord_node_free(world, (SordNode*)tup[2]); + tup[0] = uri(world, 14); + tup[2] = sord_new_literal(world, 0, USTR("bonjour"), "fr"); + sord_add(sord, tup); + sord_node_free(world, (SordNode*)tup[2]); + tup[2] = sord_new_literal(world, 0, USTR("salut"), "fr"); + sord_add(sord, tup); + + // Attempt to add some duplicates + if (sord_add(sord, tup)) { + return test_fail("Fail: Successfully added duplicate quad\n"); + } + if (sord_add(sord, tup)) { + return test_fail("Fail: Successfully added duplicate quad\n"); + } + + // Add a blank node subject + sord_node_free(world, (SordNode*)tup[0]); + tup[0] = sord_new_blank(world, USTR("ablank")); + sord_add(sord, tup); + + sord_node_free(world, (SordNode*)tup[1]); + sord_node_free(world, (SordNode*)tup[2]); + tup[1] = uri(world, 6); + tup[2] = uri(world, 7); + sord_add(sord, tup); + sord_node_free(world, (SordNode*)tup[0]); + sord_node_free(world, (SordNode*)tup[1]); + sord_node_free(world, (SordNode*)tup[2]); + + return EXIT_SUCCESS; +} + +#define TUP_FMT "(%6s %6s %6s)" +#define TUP_FMT_ARGS(t) \ + ((t)[0] ? sord_node_get_string((t)[0]) : USTR("*")), \ + ((t)[1] ? sord_node_get_string((t)[1]) : USTR("*")), \ + ((t)[2] ? sord_node_get_string((t)[2]) : USTR("*")) + +static int +test_read(SordWorld* world, SordModel* sord, SordNode* g, const size_t n_quads) +{ + int ret = EXIT_SUCCESS; + + SordQuad id; + + SordIter* iter = sord_begin(sord); + if (sord_iter_get_model(iter) != sord) { + return test_fail("Fail: Iterator has incorrect sord pointer\n"); + } + + for (; !sord_iter_end(iter); sord_iter_next(iter)) { + sord_iter_get(iter, id); + } + + // Attempt to increment past end + if (!sord_iter_next(iter)) { + return test_fail("Fail: Successfully incremented past end\n"); + } + + sord_iter_free(iter); + + const uint8_t* s = USTR("hello"); + SordNode* plain_hello = sord_new_literal(world, 0, s, NULL); + SordNode* type4_hello = sord_new_literal(world, uri(world, 4), s, NULL); + SordNode* type5_hello = sord_new_literal(world, uri(world, 5), s, NULL); + SordNode* gb_hello = sord_new_literal(world, NULL, s, "en-gb"); + SordNode* us_hello = sord_new_literal(world, NULL, s, "en-us"); + +#define NUM_PATTERNS 18 + + QueryTest patterns[NUM_PATTERNS] = { + {{0, 0, 0}, (int)(n_quads * n_objects_per) + 12}, + {{uri(world, 1), 0, 0}, 2}, + {{uri(world, 9), uri(world, 9), uri(world, 9)}, 0}, + {{uri(world, 1), uri(world, 2), uri(world, 4)}, 1}, + {{uri(world, 3), uri(world, 4), uri(world, 0)}, 2}, + {{uri(world, 0), uri(world, 2), uri(world, 4)}, 1}, + {{uri(world, 0), uri(world, 0), uri(world, 4)}, 1}, + {{uri(world, 1), uri(world, 0), uri(world, 0)}, 2}, + {{uri(world, 1), uri(world, 0), uri(world, 4)}, 1}, + {{uri(world, 0), uri(world, 2), uri(world, 0)}, 2}, + {{uri(world, 98), uri(world, 4), plain_hello}, 1}, + {{uri(world, 98), uri(world, 4), type5_hello}, 1}, + {{uri(world, 96), uri(world, 4), type4_hello}, 1}, + {{uri(world, 96), uri(world, 4), type5_hello}, 1}, + {{uri(world, 94), uri(world, 5), plain_hello}, 1}, + {{uri(world, 94), uri(world, 5), gb_hello}, 1}, + {{uri(world, 92), uri(world, 6), gb_hello}, 1}, + {{uri(world, 92), uri(world, 6), us_hello}, 1}}; + + SordQuad match = {uri(world, 1), uri(world, 2), uri(world, 4), g}; + if (!sord_contains(sord, match)) { + return test_fail("Fail: No match for " TUP_FMT "\n", TUP_FMT_ARGS(match)); + } + + SordQuad nomatch = {uri(world, 1), uri(world, 2), uri(world, 9), g}; + if (sord_contains(sord, nomatch)) { + return test_fail("Fail: False match for " TUP_FMT "\n", + TUP_FMT_ARGS(nomatch)); + } + + if (sord_get(sord, NULL, NULL, uri(world, 3), g)) { + return test_fail("Fail: Get *,*,3 succeeded\n"); + } else if (!sord_node_equals( + sord_get(sord, uri(world, 1), uri(world, 2), NULL, g), + uri(world, 3))) { + return test_fail("Fail: Get 1,2,* != 3\n"); + } else if (!sord_node_equals( + sord_get(sord, uri(world, 1), NULL, uri(world, 3), g), + uri(world, 2))) { + return test_fail("Fail: Get 1,*,3 != 2\n"); + } else if (!sord_node_equals( + sord_get(sord, NULL, uri(world, 2), uri(world, 3), g), + uri(world, 1))) { + return test_fail("Fail: Get *,2,3 != 1\n"); + } + + for (unsigned i = 0; i < NUM_PATTERNS; ++i) { + QueryTest test = patterns[i]; + SordQuad pat = {test.query[0], test.query[1], test.query[2], g}; + fprintf(stderr, "Query " TUP_FMT "... ", TUP_FMT_ARGS(pat)); + + iter = sord_find(sord, pat); + int num_results = 0; + for (; !sord_iter_end(iter); sord_iter_next(iter)) { + sord_iter_get(iter, id); + ++num_results; + if (!sord_quad_match(pat, id)) { + sord_iter_free(iter); + return test_fail("Fail: Query result " TUP_FMT + " does not match pattern\n", + TUP_FMT_ARGS(id)); + } + } + sord_iter_free(iter); + if (num_results != test.expected_num_results) { + return test_fail("Fail: Expected %d results, got %d\n", + test.expected_num_results, + num_results); + } + fprintf(stderr, "OK (%i matches)\n", test.expected_num_results); + } + + // Query blank node subject + SordQuad pat = {sord_new_blank(world, USTR("ablank")), 0, 0}; + if (!pat[0]) { + return test_fail("Blank node subject lost\n"); + } + fprintf(stderr, "Query " TUP_FMT "... ", TUP_FMT_ARGS(pat)); + iter = sord_find(sord, pat); + int num_results = 0; + for (; !sord_iter_end(iter); sord_iter_next(iter)) { + sord_iter_get(iter, id); + ++num_results; + if (!sord_quad_match(pat, id)) { + sord_iter_free(iter); + return test_fail("Fail: Query result " TUP_FMT + " does not match pattern\n", + TUP_FMT_ARGS(id)); + } + } + fprintf(stderr, "OK\n"); + sord_node_free(world, (SordNode*)pat[0]); + sord_iter_free(iter); + if (num_results != 2) { + return test_fail("Blank node subject query failed\n"); + } + + // Test nested queries + fprintf(stderr, "Nested Queries... "); + const SordNode* last_subject = 0; + iter = sord_search(sord, NULL, NULL, NULL, NULL); + for (; !sord_iter_end(iter); sord_iter_next(iter)) { + sord_iter_get(iter, id); + if (id[0] == last_subject) { + continue; + } + + SordQuad subpat = {id[0], 0, 0}; + SordIter* subiter = sord_find(sord, subpat); + unsigned num_sub_results = 0; + if (sord_iter_get_node(subiter, SORD_SUBJECT) != id[0]) { + return test_fail("Fail: Incorrect initial submatch\n"); + } + for (; !sord_iter_end(subiter); sord_iter_next(subiter)) { + SordQuad subid; + sord_iter_get(subiter, subid); + if (!sord_quad_match(subpat, subid)) { + sord_iter_free(iter); + sord_iter_free(subiter); + return test_fail("Fail: Nested query result does not match pattern\n"); + } + ++num_sub_results; + } + sord_iter_free(subiter); + if (num_sub_results != n_objects_per) { + return test_fail("Fail: Nested query " TUP_FMT " failed" + " (%u results, expected %u)\n", + TUP_FMT_ARGS(subpat), + num_sub_results, + n_objects_per); + } + + uint64_t count = sord_count(sord, id[0], 0, 0, 0); + if (count != num_sub_results) { + return test_fail("Fail: Query " TUP_FMT " sord_count() %" PRIu64 + "does not match result count %u\n", + TUP_FMT_ARGS(subpat), + count, + num_sub_results); + } + + last_subject = id[0]; + } + fprintf(stderr, "OK\n\n"); + sord_iter_free(iter); + + return ret; +} + +static SerdStatus +unexpected_error(void* handle, const SerdError* error) +{ + fprintf(stderr, "unexpected error: "); + vfprintf(stderr, error->fmt, *error->args); + return SERD_SUCCESS; +} + +static SerdStatus +expected_error(void* handle, const SerdError* error) +{ + fprintf(stderr, "expected error: "); + vfprintf(stderr, error->fmt, *error->args); + ++n_expected_errors; + return SERD_SUCCESS; +} + +static int +finished(SordWorld* world, SordModel* sord, int status) +{ + sord_free(sord); + sord_world_free(world); + return status; +} + +int +main(int argc, char** argv) +{ + static const size_t n_quads = 300; + + sord_free(NULL); // Shouldn't crash + + SordWorld* world = sord_world_new(); + + // Attempt to create invalid URI + fprintf(stderr, "expected "); + SordNode* bad_uri = sord_new_uri(world, USTR("noscheme")); + if (bad_uri) { + return test_fail("Successfully created invalid URI \"noscheme\"\n"); + } + sord_node_free(world, bad_uri); + + sord_world_set_error_sink(world, expected_error, NULL); + + // Attempt to create invalid CURIE + SerdNode base = serd_node_from_string(SERD_URI, USTR("http://example.org/")); + SerdEnv* env = serd_env_new(&base); + SerdNode sbad = serd_node_from_string(SERD_CURIE, USTR("bad:")); + SordNode* bad = sord_node_from_serd_node(world, env, &sbad, NULL, NULL); + if (bad) { + return test_fail("Successfully created CURIE with bad namespace\n"); + } + sord_node_free(world, bad); + serd_env_free(env); + + // Attempt to create node from garbage + SerdNode junk = SERD_NODE_NULL; + junk.type = (SerdType)1234; + if (sord_node_from_serd_node(world, env, &junk, NULL, NULL)) { + return test_fail("Successfully created node from garbage serd node\n"); + } + + // Attempt to create NULL node + SordNode* nil_node = + sord_node_from_serd_node(world, NULL, &SERD_NODE_NULL, NULL, NULL); + if (nil_node) { + return test_fail("Successfully created NULL node\n"); + } + sord_node_free(world, nil_node); + + // Check node flags are set properly + SordNode* with_newline = sord_new_literal(world, NULL, USTR("a\nb"), NULL); + if (!(sord_node_get_flags(with_newline) & SERD_HAS_NEWLINE)) { + return test_fail("Newline flag not set\n"); + } + SordNode* with_quote = sord_new_literal(world, NULL, USTR("a\"b"), NULL); + if (!(sord_node_get_flags(with_quote) & SERD_HAS_QUOTE)) { + return test_fail("Quote flag not set\n"); + } + + // Create with minimal indexing + SordModel* sord = sord_new(world, SORD_SPO, false); + generate(world, sord, n_quads, NULL); + + if (test_read(world, sord, NULL, n_quads)) { + sord_free(sord); + sord_world_free(world); + return EXIT_FAILURE; + } + + // Check adding tuples with NULL fields fails + sord_world_set_error_sink(world, expected_error, NULL); + const size_t initial_num_quads = sord_num_quads(sord); + SordQuad tup = {0, 0, 0, 0}; + if (sord_add(sord, tup)) { + return test_fail("Added NULL tuple\n"); + } + tup[0] = uri(world, 1); + if (sord_add(sord, tup)) { + return test_fail("Added tuple with NULL P and O\n"); + } + tup[1] = uri(world, 2); + if (sord_add(sord, tup)) { + return test_fail("Added tuple with NULL O\n"); + } + + if (sord_num_quads(sord) != initial_num_quads) { + return test_fail( + "Num quads %zu != %zu\n", sord_num_quads(sord), initial_num_quads); + } + + // Check adding tuples with an active iterator fails + SordIter* iter = sord_begin(sord); + tup[2] = uri(world, 3); + if (sord_add(sord, tup)) { + return test_fail("Added tuple with active iterator\n"); + } + + // Check removing tuples with several active iterator fails + SordIter* iter2 = sord_begin(sord); + if (!sord_erase(sord, iter)) { + return test_fail("Erased tuple with several active iterators\n"); + } + n_expected_errors = 0; + sord_remove(sord, tup); + if (n_expected_errors != 1) { + return test_fail("Removed tuple with several active iterators\n"); + } + sord_iter_free(iter); + sord_iter_free(iter2); + + sord_world_set_error_sink(world, unexpected_error, NULL); + + // Check interning merges equivalent values + SordNode* uri_id = sord_new_uri(world, USTR("http://example.org")); + SordNode* blank_id = sord_new_blank(world, USTR("testblank")); + SordNode* lit_id = sord_new_literal(world, uri_id, USTR("hello"), NULL); + if (sord_node_get_type(uri_id) != SORD_URI) { + return test_fail("URI node has incorrect type\n"); + } else if (sord_node_get_type(blank_id) != SORD_BLANK) { + return test_fail("Blank node has incorrect type\n"); + } else if (sord_node_get_type(lit_id) != SORD_LITERAL) { + return test_fail("Literal node has incorrect type\n"); + } + + const size_t initial_num_nodes = sord_num_nodes(world); + + SordNode* uri_id2 = sord_new_uri(world, USTR("http://example.org")); + SordNode* blank_id2 = sord_new_blank(world, USTR("testblank")); + SordNode* lit_id2 = sord_new_literal(world, uri_id, USTR("hello"), NULL); + if (uri_id2 != uri_id || !sord_node_equals(uri_id2, uri_id)) { + fprintf(stderr, "Fail: URI interning failed (duplicates)\n"); + return finished(world, sord, EXIT_FAILURE); + } else if (blank_id2 != blank_id || !sord_node_equals(blank_id2, blank_id)) { + fprintf(stderr, "Fail: Blank node interning failed (duplicates)\n"); + return finished(world, sord, EXIT_FAILURE); + } else if (lit_id2 != lit_id || !sord_node_equals(lit_id2, lit_id)) { + fprintf(stderr, "Fail: Literal interning failed (duplicates)\n"); + return finished(world, sord, EXIT_FAILURE); + } + + if (sord_num_nodes(world) != initial_num_nodes) { + return test_fail( + "Num nodes %zu != %zu\n", sord_num_nodes(world), initial_num_nodes); + } + + const uint8_t ni_hao[] = {0xE4, 0xBD, 0xA0, 0xE5, 0xA5, 0xBD, 0}; + SordNode* chello = sord_new_literal(world, NULL, ni_hao, "cmn"); + + // Test literal length + size_t n_bytes; + size_t n_chars; + const uint8_t* str = sord_node_get_string_counted(lit_id2, &n_bytes); + if (strcmp((const char*)str, "hello")) { + return test_fail("Literal node corrupt\n"); + } else if (n_bytes != strlen("hello")) { + return test_fail("ASCII literal byte count incorrect\n"); + } + + str = sord_node_get_string_measured(lit_id2, &n_bytes, &n_chars); + if (n_bytes != strlen("hello") || n_chars != strlen("hello")) { + return test_fail("ASCII literal measured length incorrect\n"); + } else if (strcmp((const char*)str, "hello")) { + return test_fail("ASCII literal string incorrect\n"); + } + + str = sord_node_get_string_measured(chello, &n_bytes, &n_chars); + if (n_bytes != 6) { + return test_fail("Multi-byte literal byte count incorrect\n"); + } else if (n_chars != 2) { + return test_fail("Multi-byte literal character count incorrect\n"); + } else if (strcmp((const char*)str, (const char*)ni_hao)) { + return test_fail("Multi-byte literal string incorrect\n"); + } + + // Check interning doesn't clash non-equivalent values + SordNode* uri_id3 = sord_new_uri(world, USTR("http://example.orgX")); + SordNode* blank_id3 = sord_new_blank(world, USTR("testblankX")); + SordNode* lit_id3 = sord_new_literal(world, uri_id, USTR("helloX"), NULL); + if (uri_id3 == uri_id || sord_node_equals(uri_id3, uri_id)) { + fprintf(stderr, "Fail: URI interning failed (clash)\n"); + return finished(world, sord, EXIT_FAILURE); + } else if (blank_id3 == blank_id || sord_node_equals(blank_id3, blank_id)) { + fprintf(stderr, "Fail: Blank node interning failed (clash)\n"); + return finished(world, sord, EXIT_FAILURE); + } else if (lit_id3 == lit_id || sord_node_equals(lit_id3, lit_id)) { + fprintf(stderr, "Fail: Literal interning failed (clash)\n"); + return finished(world, sord, EXIT_FAILURE); + } + + // Check literal interning + SordNode* lit4 = sord_new_literal(world, NULL, USTR("hello"), NULL); + SordNode* lit5 = sord_new_literal(world, uri_id2, USTR("hello"), NULL); + SordNode* lit6 = sord_new_literal(world, NULL, USTR("hello"), "en-ca"); + if (lit4 == lit5 || sord_node_equals(lit4, lit5) || lit4 == lit6 || + sord_node_equals(lit4, lit6) || lit5 == lit6 || + sord_node_equals(lit5, lit6)) { + fprintf(stderr, "Fail: Literal interning failed (type/lang clash)\n"); + return finished(world, sord, EXIT_FAILURE); + } + + // Check relative URI construction + SordNode* reluri = + sord_new_relative_uri(world, USTR("a/b"), USTR("http://example.org/")); + if (strcmp((const char*)sord_node_get_string(reluri), + "http://example.org/a/b")) { + fprintf(stderr, + "Fail: Bad relative URI constructed: <%s>\n", + sord_node_get_string(reluri)); + return finished(world, sord, EXIT_FAILURE); + } + SordNode* reluri2 = sord_new_relative_uri( + world, USTR("http://drobilla.net/"), USTR("http://example.org/")); + if (strcmp((const char*)sord_node_get_string(reluri2), + "http://drobilla.net/")) { + fprintf(stderr, + "Fail: Bad relative URI constructed: <%s>\n", + sord_node_get_string(reluri)); + return finished(world, sord, EXIT_FAILURE); + } + + // Check comparison with NULL + sord_node_free(world, uri_id); + sord_node_free(world, blank_id); + sord_node_free(world, lit_id); + sord_node_free(world, uri_id2); + sord_node_free(world, blank_id2); + sord_node_free(world, lit_id2); + sord_node_free(world, uri_id3); + sord_node_free(world, blank_id3); + sord_node_free(world, lit_id3); + sord_free(sord); + + static const char* const index_names[6] = { + "spo", "sop", "ops", "osp", "pso", "pos"}; + + for (int i = 0; i < 6; ++i) { + sord = sord_new(world, (1 << i), false); + printf("Testing Index `%s'\n", index_names[i]); + generate(world, sord, n_quads, 0); + if (test_read(world, sord, 0, n_quads)) { + return finished(world, sord, EXIT_FAILURE); + } + sord_free(sord); + } + + static const char* const graph_index_names[6] = { + "gspo", "gsop", "gops", "gosp", "gpso", "gpos"}; + + for (int i = 0; i < 6; ++i) { + sord = sord_new(world, (1 << i), true); + printf("Testing Index `%s'\n", graph_index_names[i]); + SordNode* graph = uri(world, 42); + generate(world, sord, n_quads, graph); + if (test_read(world, sord, graph, n_quads)) { + return finished(world, sord, EXIT_FAILURE); + } + sord_free(sord); + } + + // Test removing + sord = sord_new(world, SORD_SPO, true); + tup[0] = uri(world, 1); + tup[1] = uri(world, 2); + tup[2] = sord_new_literal(world, 0, USTR("hello"), NULL); + tup[3] = 0; + sord_add(sord, tup); + if (!sord_ask(sord, tup[0], tup[1], tup[2], tup[3])) { + fprintf(stderr, "Failed to add tuple\n"); + return finished(world, sord, EXIT_FAILURE); + } + sord_node_free(world, (SordNode*)tup[2]); + tup[2] = sord_new_literal(world, 0, USTR("hi"), NULL); + sord_add(sord, tup); + sord_remove(sord, tup); + if (sord_num_quads(sord) != 1) { + fprintf( + stderr, "Remove failed (%zu quads, expected 1)\n", sord_num_quads(sord)); + return finished(world, sord, EXIT_FAILURE); + } + + iter = sord_find(sord, tup); + if (!sord_iter_end(iter)) { + fprintf(stderr, "Found removed tuple\n"); + return finished(world, sord, EXIT_FAILURE); + } + sord_iter_free(iter); + + // Test double remove (silent success) + sord_remove(sord, tup); + + // Load a couple graphs + SordNode* graph42 = uri(world, 42); + SordNode* graph43 = uri(world, 43); + generate(world, sord, 1, graph42); + generate(world, sord, 1, graph43); + + // Remove one graph via iterator + SerdStatus st; + iter = sord_search(sord, NULL, NULL, NULL, graph43); + while (!sord_iter_end(iter)) { + if ((st = sord_erase(sord, iter))) { + fprintf(stderr, "Remove by iterator failed (%s)\n", serd_strerror(st)); + return finished(world, sord, EXIT_FAILURE); + } + } + sord_iter_free(iter); + + // Erase the first tuple (an element in the default graph) + iter = sord_begin(sord); + if (sord_erase(sord, iter)) { + return test_fail("Failed to erase begin iterator on non-empty model\n"); + } + sord_iter_free(iter); + + // Ensure only the other graph is left + SordQuad quad; + SordQuad pat = {0, 0, 0, graph42}; + for (iter = sord_begin(sord); !sord_iter_end(iter); sord_iter_next(iter)) { + sord_iter_get(iter, quad); + if (!sord_quad_match(quad, pat)) { + fprintf(stderr, "Graph removal via iteration failed\n"); + return finished(world, sord, EXIT_FAILURE); + } + } + sord_iter_free(iter); + + // Load file into two separate graphs + sord_free(sord); + sord = sord_new(world, SORD_SPO, true); + env = serd_env_new(&base); + SordNode* graph1 = sord_new_uri(world, USTR("http://example.org/graph1")); + SordNode* graph2 = sord_new_uri(world, USTR("http://example.org/graph2")); + SerdReader* reader = sord_new_reader(sord, env, SERD_TURTLE, graph1); + if ((st = serd_reader_read_string(reader, USTR("

.")))) { + fprintf(stderr, "Failed to read string (%s)\n", serd_strerror(st)); + return finished(world, sord, EXIT_FAILURE); + } + serd_reader_free(reader); + reader = sord_new_reader(sord, env, SERD_TURTLE, graph2); + if ((st = serd_reader_read_string(reader, USTR("

.")))) { + fprintf(stderr, "Failed to re-read string (%s)\n", serd_strerror(st)); + return finished(world, sord, EXIT_FAILURE); + } + serd_reader_free(reader); + serd_env_free(env); + + // Ensure we only see triple once + size_t n_triples = 0; + for (iter = sord_begin(sord); !sord_iter_end(iter); sord_iter_next(iter)) { + fprintf(stderr, + "%s %s %s %s\n", + sord_node_get_string(sord_iter_get_node(iter, SORD_SUBJECT)), + sord_node_get_string(sord_iter_get_node(iter, SORD_PREDICATE)), + sord_node_get_string(sord_iter_get_node(iter, SORD_OBJECT)), + sord_node_get_string(sord_iter_get_node(iter, SORD_GRAPH))); + + ++n_triples; + } + sord_iter_free(iter); + if (n_triples != 1) { + fprintf(stderr, "Found duplicate triple\n"); + return finished(world, sord, EXIT_FAILURE); + } + + // Test SPO iteration on an SOP indexed store + sord_free(sord); + sord = sord_new(world, SORD_SOP, false); + generate(world, sord, 1, graph42); + for (iter = sord_begin(sord); !sord_iter_end(iter); sord_iter_next(iter)) { + ++n_triples; + } + sord_iter_free(iter); + + return finished(world, sord, EXIT_SUCCESS); +} diff --git a/modules/juce_audio_processors/format_types/lv2/sord/src/sord_validate.c b/modules/juce_audio_processors/format_types/lv2/sord/src/sord_validate.c new file mode 100644 index 0000000000..e30924a33e --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/sord/src/sord_validate.c @@ -0,0 +1,804 @@ +/* + Copyright 2012-2021 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#define _BSD_SOURCE 1 // for realpath +#define _DEFAULT_SOURCE 1 // for realpath + +#include "serd/serd.h" +#include "sord/sord.h" +#include "sord_config.h" + +#if USE_PCRE +# include +#endif + +#ifdef _WIN32 +# include +#endif + +#include +#include +#include +#include +#include +#include + +#ifdef __GNUC__ +# define SORD_LOG_FUNC(fmt, arg1) __attribute__((format(printf, fmt, arg1))) +#else +# define SORD_LOG_FUNC(fmt, arg1) +#endif + +#define NS_foaf (const uint8_t*)"http://xmlns.com/foaf/0.1/" +#define NS_owl (const uint8_t*)"http://www.w3.org/2002/07/owl#" +#define NS_rdf (const uint8_t*)"http://www.w3.org/1999/02/22-rdf-syntax-ns#" +#define NS_rdfs (const uint8_t*)"http://www.w3.org/2000/01/rdf-schema#" +#define NS_xsd (const uint8_t*)"http://www.w3.org/2001/XMLSchema#" + +typedef struct { + SordNode* foaf_Document; + SordNode* owl_AnnotationProperty; + SordNode* owl_Class; + SordNode* owl_DatatypeProperty; + SordNode* owl_FunctionalProperty; + SordNode* owl_InverseFunctionalProperty; + SordNode* owl_ObjectProperty; + SordNode* owl_OntologyProperty; + SordNode* owl_Restriction; + SordNode* owl_Thing; + SordNode* owl_cardinality; + SordNode* owl_equivalentClass; + SordNode* owl_maxCardinality; + SordNode* owl_minCardinality; + SordNode* owl_onDatatype; + SordNode* owl_onProperty; + SordNode* owl_someValuesFrom; + SordNode* owl_withRestrictions; + SordNode* rdf_PlainLiteral; + SordNode* rdf_Property; + SordNode* rdf_first; + SordNode* rdf_rest; + SordNode* rdf_type; + SordNode* rdfs_Class; + SordNode* rdfs_Literal; + SordNode* rdfs_Resource; + SordNode* rdfs_domain; + SordNode* rdfs_label; + SordNode* rdfs_range; + SordNode* rdfs_subClassOf; + SordNode* xsd_anyURI; + SordNode* xsd_decimal; + SordNode* xsd_double; + SordNode* xsd_maxInclusive; + SordNode* xsd_minInclusive; + SordNode* xsd_pattern; + SordNode* xsd_string; +} URIs; + +static int n_errors = 0; +static int n_restrictions = 0; +static bool one_line_errors = false; + +static int +print_version(void) +{ + printf("sord_validate " SORD_VERSION + " \n"); + printf("Copyright 2012-2021 David Robillard .\n" + "License: \n" + "This is free software; you are free to change and redistribute it." + "\nThere is NO WARRANTY, to the extent permitted by law.\n"); + return 0; +} + +static int +print_usage(const char* name, bool error) +{ + FILE* const os = error ? stderr : stdout; + fprintf(os, "Usage: %s [OPTION]... INPUT...\n", name); + fprintf(os, "Validate RDF data\n\n"); + fprintf(os, " -h Display this help and exit\n"); + fprintf(os, " -l Print errors on a single line.\n"); + fprintf(os, " -v Display version information and exit\n"); + fprintf(os, + "Validate RDF data. This is a simple validator which checks\n" + "that all used properties are actually defined. It does not do\n" + "any fancy file retrieval, the files passed on the command line\n" + "are the only data that is read. In other words, you must pass\n" + "the definition of all vocabularies used on the command line.\n"); + return error ? 1 : 0; +} + +static uint8_t* +absolute_path(const uint8_t* path) +{ +#ifdef _WIN32 + char* out = (char*)malloc(MAX_PATH); + GetFullPathName((const char*)path, MAX_PATH, out, NULL); + return (uint8_t*)out; +#else + return (uint8_t*)realpath((const char*)path, NULL); +#endif +} + +SORD_LOG_FUNC(2, 3) +static int +errorf(const SordQuad quad, const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + fprintf(stderr, "error: "); + vfprintf(stderr, fmt, args); + va_end(args); + + const char* sep = one_line_errors ? "\t" : "\n "; + fprintf(stderr, + "%s%s%s%s%s%s\n", + sep, + (const char*)sord_node_get_string(quad[SORD_SUBJECT]), + sep, + (const char*)sord_node_get_string(quad[SORD_PREDICATE]), + sep, + (const char*)sord_node_get_string(quad[SORD_OBJECT])); + + ++n_errors; + return 1; +} + +static bool +is_descendant_of(SordModel* model, + const URIs* uris, + const SordNode* child, + const SordNode* parent, + const SordNode* pred) +{ + if (!child) { + return false; + } else if (sord_node_equals(child, parent) || + sord_ask(model, child, uris->owl_equivalentClass, parent, NULL)) { + return true; + } + + SordIter* i = sord_search(model, child, pred, NULL, NULL); + for (; !sord_iter_end(i); sord_iter_next(i)) { + const SordNode* o = sord_iter_get_node(i, SORD_OBJECT); + if (sord_node_equals(child, o)) { + continue; // Weird class is explicitly a descendent of itself + } + if (is_descendant_of(model, uris, o, parent, pred)) { + sord_iter_free(i); + return true; + } + } + sord_iter_free(i); + + return false; +} + +static bool +regexp_match(const uint8_t* pat, const char* str) +{ +#if USE_PCRE + // Append a $ to the pattern so we only match if the entire string matches + const size_t len = strlen((const char*)pat); + char* const regx = (char*)malloc(len + 2); + memcpy(regx, pat, len); + regx[len] = '$'; + regx[len + 1] = '\0'; + + const char* err; + int erroffset; + pcre* re = pcre_compile(regx, PCRE_ANCHORED, &err, &erroffset, NULL); + free(regx); + if (!re) { + fprintf( + stderr, "Error in pattern `%s' at offset %d (%s)\n", pat, erroffset, err); + return false; + } + + const bool ret = pcre_exec(re, NULL, str, strlen(str), 0, 0, NULL, 0) >= 0; + pcre_free(re); + return ret; +#endif // USE_PCRE + return true; +} + +static int +bound_cmp(SordModel* model, + const URIs* uris, + const SordNode* literal, + const SordNode* type, + const SordNode* bound) +{ + const char* str = (const char*)sord_node_get_string(literal); + const char* bound_str = (const char*)sord_node_get_string(bound); + const SordNode* pred = uris->owl_onDatatype; + const bool is_numeric = + is_descendant_of(model, uris, type, uris->xsd_decimal, pred) || + is_descendant_of(model, uris, type, uris->xsd_double, pred); + + if (is_numeric) { + const double fbound = serd_strtod(bound_str, NULL); + const double fliteral = serd_strtod(str, NULL); + return ((fliteral < fbound) ? -1 : (fliteral > fbound) ? 1 : 0); + } else { + return strcmp(str, bound_str); + } +} + +static bool +check_restriction(SordModel* model, + const URIs* uris, + const SordNode* literal, + const SordNode* type, + const SordNode* restriction) +{ + size_t len = 0; + const char* str = (const char*)sord_node_get_string_counted(literal, &len); + + // Check xsd:pattern + SordIter* p = sord_search(model, restriction, uris->xsd_pattern, 0, 0); + if (p) { + const SordNode* pat = sord_iter_get_node(p, SORD_OBJECT); + if (!regexp_match(sord_node_get_string(pat), str)) { + fprintf(stderr, + "`%s' does not match <%s> pattern `%s'\n", + sord_node_get_string(literal), + sord_node_get_string(type), + sord_node_get_string(pat)); + sord_iter_free(p); + return false; + } + sord_iter_free(p); + ++n_restrictions; + } + + // Check xsd:minInclusive + SordIter* l = sord_search(model, restriction, uris->xsd_minInclusive, 0, 0); + if (l) { + const SordNode* lower = sord_iter_get_node(l, SORD_OBJECT); + if (bound_cmp(model, uris, literal, type, lower) < 0) { + fprintf(stderr, + "`%s' is not >= <%s> minimum `%s'\n", + sord_node_get_string(literal), + sord_node_get_string(type), + sord_node_get_string(lower)); + sord_iter_free(l); + return false; + } + sord_iter_free(l); + ++n_restrictions; + } + + // Check xsd:maxInclusive + SordIter* u = sord_search(model, restriction, uris->xsd_maxInclusive, 0, 0); + if (u) { + const SordNode* upper = sord_iter_get_node(u, SORD_OBJECT); + if (bound_cmp(model, uris, literal, type, upper) > 0) { + fprintf(stderr, + "`%s' is not <= <%s> maximum `%s'\n", + sord_node_get_string(literal), + sord_node_get_string(type), + sord_node_get_string(upper)); + sord_iter_free(u); + return false; + } + sord_iter_free(u); + ++n_restrictions; + } + + return true; // Unknown restriction, be quietly tolerant +} + +static bool +literal_is_valid(SordModel* model, + const URIs* uris, + const SordQuad quad, + const SordNode* literal, + const SordNode* type) +{ + if (!type) { + return true; + } + + /* Check that literal data is related to required type. We don't do a + strict subtype check here because e.g. an xsd:decimal might be a valid + xsd:unsignedInt, which the pattern checks will verify, but if the + literal type is not related to the required type at all + (e.g. xsd:decimal and xsd:string) there is a problem. */ + const SordNode* datatype = sord_node_get_datatype(literal); + if (datatype && datatype != type) { + if (!is_descendant_of(model, uris, datatype, type, uris->owl_onDatatype) && + !is_descendant_of(model, uris, type, datatype, uris->owl_onDatatype) && + !(sord_node_equals(datatype, uris->xsd_decimal) && + is_descendant_of( + model, uris, type, uris->xsd_double, uris->owl_onDatatype))) { + errorf(quad, + "Literal `%s' datatype <%s> is not compatible with <%s>\n", + sord_node_get_string(literal), + sord_node_get_string(datatype), + sord_node_get_string(type)); + return false; + } + } + + // Find restrictions list + SordIter* rs = sord_search(model, type, uris->owl_withRestrictions, 0, 0); + if (sord_iter_end(rs)) { + return true; // No restrictions + } + + // Walk list, checking each restriction + const SordNode* head = sord_iter_get_node(rs, SORD_OBJECT); + while (head) { + SordIter* f = sord_search(model, head, uris->rdf_first, 0, 0); + if (!f) { + break; // Reached end of restrictions list without failure + } + + // Check this restriction + const bool good = check_restriction( + model, uris, literal, type, sord_iter_get_node(f, SORD_OBJECT)); + sord_iter_free(f); + + if (!good) { + sord_iter_free(rs); + return false; // Failed, literal is invalid + } + + // Seek to next list node + SordIter* n = sord_search(model, head, uris->rdf_rest, 0, 0); + head = n ? sord_iter_get_node(n, SORD_OBJECT) : NULL; + sord_iter_free(n); + } + + sord_iter_free(rs); + + SordIter* s = sord_search(model, type, uris->owl_onDatatype, 0, 0); + if (s) { + const SordNode* super = sord_iter_get_node(s, SORD_OBJECT); + const bool good = literal_is_valid(model, uris, quad, literal, super); + sord_iter_free(s); + return good; // Match iff literal also matches supertype + } + + return true; // Matches top level type +} + +static bool +check_type(SordModel* model, + const URIs* uris, + const SordQuad quad, + const SordNode* node, + const SordNode* type) +{ + if (sord_node_equals(type, uris->rdfs_Resource) || + sord_node_equals(type, uris->owl_Thing)) { + return true; + } + + if (sord_node_get_type(node) == SORD_LITERAL) { + if (sord_node_equals(type, uris->rdfs_Literal)) { + return true; + } else if (sord_node_equals(type, uris->rdf_PlainLiteral)) { + return !sord_node_get_language(node); + } else { + return literal_is_valid(model, uris, quad, node, type); + } + } else if (sord_node_get_type(node) == SORD_URI) { + if (sord_node_equals(type, uris->foaf_Document)) { + return true; // Questionable... + } else if (is_descendant_of( + model, uris, type, uris->xsd_anyURI, uris->owl_onDatatype)) { + /* Type is any URI and this is a URI, so pass. Restrictions on + anyURI subtypes are not currently checked (very uncommon). */ + return true; // Type is anyURI, and this is a URI + } else { + SordIter* t = sord_search(model, node, uris->rdf_type, NULL, NULL); + for (; !sord_iter_end(t); sord_iter_next(t)) { + if (is_descendant_of(model, + uris, + sord_iter_get_node(t, SORD_OBJECT), + type, + uris->rdfs_subClassOf)) { + sord_iter_free(t); + return true; + } + } + sord_iter_free(t); + return false; + } + } else { + return true; // Blanks often lack explicit types, ignore + } + + return false; +} + +static uint64_t +count_non_blanks(SordIter* i, SordQuadIndex field) +{ + uint64_t n = 0; + for (; !sord_iter_end(i); sord_iter_next(i)) { + const SordNode* node = sord_iter_get_node(i, field); + if (sord_node_get_type(node) != SORD_BLANK) { + ++n; + } + } + return n; +} + +static int +check_properties(SordModel* model, URIs* uris) +{ + int st = 0; + SordIter* i = sord_begin(model); + for (; !sord_iter_end(i); sord_iter_next(i)) { + SordQuad quad; + sord_iter_get(i, quad); + + const SordNode* subj = quad[SORD_SUBJECT]; + const SordNode* pred = quad[SORD_PREDICATE]; + const SordNode* obj = quad[SORD_OBJECT]; + + bool is_any_property = false; + SordIter* t = sord_search(model, pred, uris->rdf_type, NULL, NULL); + for (; !sord_iter_end(t); sord_iter_next(t)) { + if (is_descendant_of(model, + uris, + sord_iter_get_node(t, SORD_OBJECT), + uris->rdf_Property, + uris->rdfs_subClassOf)) { + is_any_property = true; + break; + } + } + sord_iter_free(t); + + const bool is_ObjectProperty = + sord_ask(model, pred, uris->rdf_type, uris->owl_ObjectProperty, 0); + const bool is_FunctionalProperty = + sord_ask(model, pred, uris->rdf_type, uris->owl_FunctionalProperty, 0); + const bool is_InverseFunctionalProperty = sord_ask( + model, pred, uris->rdf_type, uris->owl_InverseFunctionalProperty, 0); + const bool is_DatatypeProperty = + sord_ask(model, pred, uris->rdf_type, uris->owl_DatatypeProperty, 0); + + if (!is_any_property) { + st = errorf(quad, "Use of undefined property"); + } + + if (!sord_ask(model, pred, uris->rdfs_label, NULL, NULL)) { + st = + errorf(quad, "Property <%s> has no label", sord_node_get_string(pred)); + } + + if (is_DatatypeProperty && sord_node_get_type(obj) != SORD_LITERAL) { + st = errorf(quad, "Datatype property with non-literal value"); + } + + if (is_ObjectProperty && sord_node_get_type(obj) == SORD_LITERAL) { + st = errorf(quad, "Object property with literal value"); + } + + if (is_FunctionalProperty) { + SordIter* o = sord_search(model, subj, pred, NULL, NULL); + const unsigned n = count_non_blanks(o, SORD_OBJECT); + if (n > 1) { + st = errorf(quad, "Functional property with %u objects", n); + } + sord_iter_free(o); + } + + if (is_InverseFunctionalProperty) { + SordIter* s = sord_search(model, NULL, pred, obj, NULL); + const unsigned n = count_non_blanks(s, SORD_SUBJECT); + if (n > 1) { + st = errorf(quad, "Inverse functional property with %u subjects", n); + } + sord_iter_free(s); + } + + if (sord_node_equals(pred, uris->rdf_type) && + !sord_ask(model, obj, uris->rdf_type, uris->rdfs_Class, NULL) && + !sord_ask(model, obj, uris->rdf_type, uris->owl_Class, NULL)) { + st = errorf(quad, "Type is not a rdfs:Class or owl:Class"); + } + + if (sord_node_get_type(obj) == SORD_LITERAL && + !literal_is_valid( + model, uris, quad, obj, sord_node_get_datatype(obj))) { + st = errorf(quad, "Literal does not match datatype"); + } + + SordIter* r = sord_search(model, pred, uris->rdfs_range, NULL, NULL); + for (; !sord_iter_end(r); sord_iter_next(r)) { + const SordNode* range = sord_iter_get_node(r, SORD_OBJECT); + if (!check_type(model, uris, quad, obj, range)) { + st = errorf( + quad, "Object not in range <%s>\n", sord_node_get_string(range)); + } + } + sord_iter_free(r); + + SordIter* d = sord_search(model, pred, uris->rdfs_domain, NULL, NULL); + if (d) { + const SordNode* domain = sord_iter_get_node(d, SORD_OBJECT); + if (!check_type(model, uris, quad, subj, domain)) { + st = errorf( + quad, "Subject not in domain <%s>", sord_node_get_string(domain)); + } + sord_iter_free(d); + } + } + sord_iter_free(i); + + return st; +} + +static int +check_instance(SordModel* model, + const URIs* uris, + const SordNode* restriction, + const SordQuad quad) +{ + const SordNode* instance = quad[SORD_SUBJECT]; + int st = 0; + + const SordNode* prop = + sord_get(model, restriction, uris->owl_onProperty, NULL, NULL); + if (!prop) { + return 0; + } + + const unsigned values = sord_count(model, instance, prop, NULL, NULL); + + // Check exact cardinality + const SordNode* card = + sord_get(model, restriction, uris->owl_cardinality, NULL, NULL); + if (card) { + const unsigned c = atoi((const char*)sord_node_get_string(card)); + if (values != c) { + st = errorf(quad, + "Property %s on %s has %u != %u values", + sord_node_get_string(prop), + sord_node_get_string(instance), + values, + c); + } + } + + // Check minimum cardinality + const SordNode* minCard = + sord_get(model, restriction, uris->owl_minCardinality, NULL, NULL); + if (minCard) { + const unsigned m = atoi((const char*)sord_node_get_string(minCard)); + if (values < m) { + st = errorf(quad, + "Property %s on %s has %u < %u values", + sord_node_get_string(prop), + sord_node_get_string(instance), + values, + m); + } + } + + // Check maximum cardinality + const SordNode* maxCard = + sord_get(model, restriction, uris->owl_maxCardinality, NULL, NULL); + if (maxCard) { + const unsigned m = atoi((const char*)sord_node_get_string(maxCard)); + if (values < m) { + st = errorf(quad, + "Property %s on %s has %u > %u values", + sord_node_get_string(prop), + sord_node_get_string(instance), + values, + m); + } + } + + // Check someValuesFrom + SordIter* sf = + sord_search(model, restriction, uris->owl_someValuesFrom, NULL, NULL); + if (sf) { + const SordNode* type = sord_iter_get_node(sf, SORD_OBJECT); + + SordIter* v = sord_search(model, instance, prop, NULL, NULL); + bool found = false; + for (; !sord_iter_end(v); sord_iter_next(v)) { + const SordNode* value = sord_iter_get_node(v, SORD_OBJECT); + if (check_type(model, uris, quad, value, type)) { + found = true; + break; + } + } + if (!found) { + st = errorf(quad, + "%s has no <%s> values of type <%s>\n", + sord_node_get_string(instance), + sord_node_get_string(prop), + sord_node_get_string(type)); + } + sord_iter_free(v); + } + sord_iter_free(sf); + + return st; +} + +static int +check_class_instances(SordModel* model, + const URIs* uris, + const SordNode* restriction, + const SordNode* klass) +{ + // Check immediate instances of this class + SordIter* i = sord_search(model, NULL, uris->rdf_type, klass, NULL); + for (; !sord_iter_end(i); sord_iter_next(i)) { + SordQuad quad; + sord_iter_get(i, quad); + check_instance(model, uris, restriction, quad); + } + sord_iter_free(i); + + // Check instances of all subclasses recursively + SordIter* s = sord_search(model, NULL, uris->rdfs_subClassOf, klass, NULL); + for (; !sord_iter_end(s); sord_iter_next(s)) { + const SordNode* subklass = sord_iter_get_node(s, SORD_SUBJECT); + check_class_instances(model, uris, restriction, subklass); + } + sord_iter_free(s); + + return 0; +} + +static int +check_instances(SordModel* model, const URIs* uris) +{ + int st = 0; + SordIter* r = + sord_search(model, NULL, uris->rdf_type, uris->owl_Restriction, NULL); + for (; !sord_iter_end(r); sord_iter_next(r)) { + const SordNode* restriction = sord_iter_get_node(r, SORD_SUBJECT); + const SordNode* prop = + sord_get(model, restriction, uris->owl_onProperty, NULL, NULL); + if (!prop) { + continue; + } + + SordIter* c = + sord_search(model, NULL, uris->rdfs_subClassOf, restriction, NULL); + for (; !sord_iter_end(c); sord_iter_next(c)) { + const SordNode* klass = sord_iter_get_node(c, SORD_SUBJECT); + check_class_instances(model, uris, restriction, klass); + } + sord_iter_free(c); + } + sord_iter_free(r); + + return st; +} + +int +main(int argc, char** argv) +{ + if (argc < 2) { + return print_usage(argv[0], true); + } + + int a = 1; + for (; a < argc && argv[a][0] == '-'; ++a) { + if (argv[a][1] == 'l') { + one_line_errors = true; + } else if (argv[a][1] == 'v') { + return print_version(); + } else { + fprintf(stderr, "%s: Unknown option `%s'\n", argv[0], argv[a]); + return print_usage(argv[0], true); + } + } + + SordWorld* world = sord_world_new(); + SordModel* model = sord_new(world, SORD_SPO | SORD_OPS, false); + SerdEnv* env = serd_env_new(&SERD_NODE_NULL); + SerdReader* reader = sord_new_reader(model, env, SERD_TURTLE, NULL); + + for (; a < argc; ++a) { + const uint8_t* input = (const uint8_t*)argv[a]; + uint8_t* rel_in_path = serd_file_uri_parse(input, NULL); + uint8_t* in_path = absolute_path(rel_in_path); + + free(rel_in_path); + if (!in_path) { + fprintf(stderr, "Skipping file %s\n", input); + continue; + } + + SerdURI base_uri; + SerdNode base_uri_node = + serd_node_new_file_uri(in_path, NULL, &base_uri, true); + + serd_env_set_base_uri(env, &base_uri_node); + const SerdStatus st = serd_reader_read_file(reader, in_path); + if (st) { + fprintf(stderr, "error reading %s: %s\n", in_path, serd_strerror(st)); + } + + serd_node_free(&base_uri_node); + free(in_path); + } + serd_reader_free(reader); + serd_env_free(env); + +#define URI(prefix, suffix) \ + uris.prefix##_##suffix = sord_new_uri(world, NS_##prefix #suffix) + + URIs uris; + URI(foaf, Document); + URI(owl, AnnotationProperty); + URI(owl, Class); + URI(owl, DatatypeProperty); + URI(owl, FunctionalProperty); + URI(owl, InverseFunctionalProperty); + URI(owl, ObjectProperty); + URI(owl, OntologyProperty); + URI(owl, Restriction); + URI(owl, Thing); + URI(owl, cardinality); + URI(owl, equivalentClass); + URI(owl, maxCardinality); + URI(owl, minCardinality); + URI(owl, onDatatype); + URI(owl, onProperty); + URI(owl, someValuesFrom); + URI(owl, withRestrictions); + URI(rdf, PlainLiteral); + URI(rdf, Property); + URI(rdf, first); + URI(rdf, rest); + URI(rdf, type); + URI(rdfs, Class); + URI(rdfs, Literal); + URI(rdfs, Resource); + URI(rdfs, domain); + URI(rdfs, label); + URI(rdfs, range); + URI(rdfs, subClassOf); + URI(xsd, anyURI); + URI(xsd, decimal); + URI(xsd, double); + URI(xsd, maxInclusive); + URI(xsd, minInclusive); + URI(xsd, pattern); + URI(xsd, string); + +#if !USE_PCRE + fprintf(stderr, "warning: Built without PCRE, datatypes not checked.\n"); +#endif + + const int prop_st = check_properties(model, &uris); + const int inst_st = check_instances(model, &uris); + + printf("Found %d errors among %d files (checked %d restrictions)\n", + n_errors, + argc - 1, + n_restrictions); + + sord_free(model); + sord_world_free(world); + return prop_st || inst_st; +} diff --git a/modules/juce_audio_processors/format_types/lv2/sord/src/sordi.c b/modules/juce_audio_processors/format_types/lv2/sord/src/sordi.c new file mode 100644 index 0000000000..2e96f92998 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/sord/src/sordi.c @@ -0,0 +1,216 @@ +/* + Copyright 2011-2016 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "serd/serd.h" +#include "sord/sord.h" +#include "sord_config.h" + +#ifdef _WIN32 +# include +#endif + +#include +#include +#include +#include +#include + +#define SORDI_ERROR(msg) fprintf(stderr, "sordi: " msg) +#define SORDI_ERRORF(fmt, ...) fprintf(stderr, "sordi: " fmt, __VA_ARGS__) + +typedef struct { + SerdWriter* writer; + SerdEnv* env; + SerdNode base_uri_node; + SerdURI base_uri; + SordModel* sord; +} State; + +static int +print_version(void) +{ + printf("sordi " SORD_VERSION " \n"); + printf("Copyright 2011-2021 David Robillard .\n" + "License: \n" + "This is free software; you are free to change and redistribute it." + "\nThere is NO WARRANTY, to the extent permitted by law.\n"); + return 0; +} + +static int +print_usage(const char* name, bool error) +{ + FILE* const os = error ? stderr : stdout; + fprintf(os, "%s", error ? "\n" : ""); + fprintf(os, "Usage: %s [OPTION]... INPUT [BASE_URI]\n", name); + fprintf(os, "Load and re-serialise RDF data.\n"); + fprintf(os, "Use - for INPUT to read from standard input.\n\n"); + fprintf(os, " -h Display this help and exit\n"); + fprintf(os, " -i SYNTAX Input syntax (`turtle' or `ntriples')\n"); + fprintf(os, " -o SYNTAX Output syntax (`turtle' or `ntriples')\n"); + fprintf(os, " -s INPUT Parse INPUT as string (terminates options)\n"); + fprintf(os, " -v Display version information and exit\n"); + return error ? 1 : 0; +} + +static bool +set_syntax(SerdSyntax* syntax, const char* name) +{ + if (!strcmp(name, "turtle")) { + *syntax = SERD_TURTLE; + } else if (!strcmp(name, "ntriples")) { + *syntax = SERD_NTRIPLES; + } else { + SORDI_ERRORF("unknown syntax `%s'\n", name); + return false; + } + return true; +} + +int +main(int argc, char** argv) +{ + if (argc < 2) { + return print_usage(argv[0], true); + } + + FILE* in_fd = NULL; + SerdSyntax input_syntax = SERD_TURTLE; + SerdSyntax output_syntax = SERD_NTRIPLES; + bool from_file = true; + const uint8_t* in_name = NULL; + int a = 1; + for (; a < argc && argv[a][0] == '-'; ++a) { + if (argv[a][1] == '\0') { + in_name = (const uint8_t*)"(stdin)"; + in_fd = stdin; + break; + } else if (argv[a][1] == 'h') { + return print_usage(argv[0], false); + } else if (argv[a][1] == 'v') { + return print_version(); + } else if (argv[a][1] == 's') { + in_name = (const uint8_t*)"(string)"; + from_file = false; + ++a; + break; + } else if (argv[a][1] == 'i') { + if (++a == argc) { + SORDI_ERROR("option requires an argument -- 'i'\n\n"); + return print_usage(argv[0], true); + } + if (!set_syntax(&input_syntax, argv[a])) { + return print_usage(argv[0], true); + } + } else if (argv[a][1] == 'o') { + if (++a == argc) { + SORDI_ERROR("option requires an argument -- 'o'\n\n"); + return print_usage(argv[0], true); + } + if (!set_syntax(&output_syntax, argv[a])) { + return print_usage(argv[0], true); + } + } else { + SORDI_ERRORF("invalid option -- '%s'\n", argv[a] + 1); + return print_usage(argv[0], true); + } + } + + if (a == argc) { + SORDI_ERROR("missing input\n"); + return print_usage(argv[0], true); + } + + uint8_t* input_path = NULL; + const uint8_t* input = (const uint8_t*)argv[a++]; + if (from_file) { + in_name = in_name ? in_name : input; + if (!in_fd) { + if (!strncmp((const char*)input, "file:", 5)) { + input_path = serd_file_uri_parse(input, NULL); + input = input_path; + } + if (!input || !(in_fd = fopen((const char*)input, "rb"))) { + return 1; + } + } + } + + SerdURI base_uri = SERD_URI_NULL; + SerdNode base = SERD_NODE_NULL; + if (a < argc) { // Base URI given on command line + base = + serd_node_new_uri_from_string((const uint8_t*)argv[a], NULL, &base_uri); + } else if (from_file && in_fd != stdin) { // Use input file URI + base = serd_node_new_file_uri(input, NULL, &base_uri, true); + } + + SordWorld* world = sord_world_new(); + SordModel* sord = sord_new(world, SORD_SPO | SORD_OPS, false); + SerdEnv* env = serd_env_new(&base); + SerdReader* reader = sord_new_reader(sord, env, input_syntax, NULL); + + SerdStatus status = (from_file) + ? serd_reader_read_file_handle(reader, in_fd, in_name) + : serd_reader_read_string(reader, input); + + serd_reader_free(reader); + + FILE* out_fd = stdout; + SerdEnv* write_env = serd_env_new(&base); + + int output_style = SERD_STYLE_RESOLVED; + if (output_syntax == SERD_NTRIPLES) { + output_style |= SERD_STYLE_ASCII; + } else { + output_style |= SERD_STYLE_CURIED | SERD_STYLE_ABBREVIATED; + } + + SerdWriter* writer = serd_writer_new(output_syntax, + (SerdStyle)output_style, + write_env, + &base_uri, + serd_file_sink, + stdout); + + // Write @prefix directives + serd_env_foreach(env, (SerdPrefixSink)serd_writer_set_prefix, writer); + + // Write statements + sord_write(sord, writer, NULL); + + serd_writer_finish(writer); + serd_writer_free(writer); + + serd_env_free(env); + serd_env_free(write_env); + serd_node_free(&base); + free(input_path); + + sord_free(sord); + sord_world_free(world); + + if (from_file) { + fclose(in_fd); + } + + if (fclose(out_fd)) { + perror("sordi: write error"); + status = SERD_ERR_UNKNOWN; + } + + return (status > SERD_FAILURE) ? 1 : 0; +} diff --git a/modules/juce_audio_processors/format_types/lv2/sord/src/sordmm_test.cpp b/modules/juce_audio_processors/format_types/lv2/sord/src/sordmm_test.cpp new file mode 100644 index 0000000000..41cb322079 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/sord/src/sordmm_test.cpp @@ -0,0 +1,25 @@ +/* + Copyright 2011 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "sord/sordmm.hpp" + +int +main() +{ + Sord::World world; + Sord::Model model(world, "http://example.org/"); + return 0; +} diff --git a/modules/juce_audio_processors/format_types/lv2/sord/src/syntax.c b/modules/juce_audio_processors/format_types/lv2/sord/src/syntax.c new file mode 100644 index 0000000000..4dfa0a8189 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/sord/src/syntax.c @@ -0,0 +1,203 @@ +/* + Copyright 2011-2015 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "serd/serd.h" +#include "sord/sord.h" + +#include +#include +#include +#include + +struct SordInserterImpl { + SordModel* model; + SerdEnv* env; +}; + +SordInserter* +sord_inserter_new(SordModel* model, SerdEnv* env) +{ + SordInserter* inserter = (SordInserter*)malloc(sizeof(SordInserter)); + inserter->model = model; + inserter->env = env; + return inserter; +} + +void +sord_inserter_free(SordInserter* inserter) +{ + free(inserter); +} + +SerdStatus +sord_inserter_set_base_uri(SordInserter* inserter, const SerdNode* uri) +{ + return serd_env_set_base_uri(inserter->env, uri); +} + +SerdStatus +sord_inserter_set_prefix(SordInserter* inserter, + const SerdNode* name, + const SerdNode* uri) +{ + return serd_env_set_prefix(inserter->env, name, uri); +} + +SerdStatus +sord_inserter_write_statement(SordInserter* inserter, + SerdStatementFlags flags, + const SerdNode* graph, + const SerdNode* subject, + const SerdNode* predicate, + const SerdNode* object, + const SerdNode* object_datatype, + const SerdNode* object_lang) +{ + SordWorld* world = sord_get_world(inserter->model); + SerdEnv* env = inserter->env; + + SordNode* g = sord_node_from_serd_node(world, env, graph, NULL, NULL); + SordNode* s = sord_node_from_serd_node(world, env, subject, NULL, NULL); + SordNode* p = sord_node_from_serd_node(world, env, predicate, NULL, NULL); + SordNode* o = + sord_node_from_serd_node(world, env, object, object_datatype, object_lang); + + if (!s || !p || !o) { + return SERD_ERR_BAD_ARG; + } + + const SordQuad tup = {s, p, o, g}; + sord_add(inserter->model, tup); + + sord_node_free(world, o); + sord_node_free(world, p); + sord_node_free(world, s); + sord_node_free(world, g); + + return SERD_SUCCESS; +} + +SORD_API +SerdReader* +sord_new_reader(SordModel* model, + SerdEnv* env, + SerdSyntax syntax, + SordNode* graph) +{ + SordInserter* inserter = sord_inserter_new(model, env); + + SerdReader* reader = + serd_reader_new(syntax, + inserter, + (void (*)(void* ptr))sord_inserter_free, + (SerdBaseSink)sord_inserter_set_base_uri, + (SerdPrefixSink)sord_inserter_set_prefix, + (SerdStatementSink)sord_inserter_write_statement, + NULL); + + if (graph) { + serd_reader_set_default_graph(reader, sord_node_to_serd_node(graph)); + } + + return reader; +} + +static SerdStatus +write_statement(SordModel* sord, + SerdWriter* writer, + SordQuad tup, + SerdStatementFlags flags) +{ + const SordNode* s = tup[SORD_SUBJECT]; + const SordNode* p = tup[SORD_PREDICATE]; + const SordNode* o = tup[SORD_OBJECT]; + const SordNode* d = sord_node_get_datatype(o); + const SerdNode* ss = sord_node_to_serd_node(s); + const SerdNode* sp = sord_node_to_serd_node(p); + const SerdNode* so = sord_node_to_serd_node(o); + const SerdNode* sd = sord_node_to_serd_node(d); + + const char* lang_str = sord_node_get_language(o); + size_t lang_len = lang_str ? strlen(lang_str) : 0; + SerdNode language = SERD_NODE_NULL; + if (lang_str) { + language.type = SERD_LITERAL; + language.n_bytes = lang_len; + language.n_chars = lang_len; + language.buf = (const uint8_t*)lang_str; + } + + // TODO: Subject abbreviation + + if (sord_node_is_inline_object(s) && !(flags & SERD_ANON_CONT)) { + return SERD_SUCCESS; + } + + SerdStatus st = SERD_SUCCESS; + if (sord_node_is_inline_object(o)) { + SordQuad sub_pat = {o, 0, 0, 0}; + SordIter* sub_iter = sord_find(sord, sub_pat); + + SerdStatementFlags start_flags = + flags | ((sub_iter) ? SERD_ANON_O_BEGIN : SERD_EMPTY_O); + + st = serd_writer_write_statement( + writer, start_flags, NULL, ss, sp, so, sd, &language); + + if (!st && sub_iter) { + flags |= SERD_ANON_CONT; + for (; !st && !sord_iter_end(sub_iter); sord_iter_next(sub_iter)) { + SordQuad sub_tup; + sord_iter_get(sub_iter, sub_tup); + st = write_statement(sord, writer, sub_tup, flags); + } + sord_iter_free(sub_iter); + serd_writer_end_anon(writer, so); + } + } else { + st = serd_writer_write_statement( + writer, flags, NULL, ss, sp, so, sd, &language); + } + + return st; +} + +bool +sord_write(SordModel* model, SerdWriter* writer, SordNode* graph) +{ + SordQuad pat = {0, 0, 0, graph}; + SordIter* iter = sord_find(model, pat); + return sord_write_iter(iter, writer); +} + +bool +sord_write_iter(SordIter* iter, SerdWriter* writer) +{ + if (!iter) { + return false; + } + + SordModel* model = (SordModel*)sord_iter_get_model(iter); + SerdStatus st = SERD_SUCCESS; + for (; !st && !sord_iter_end(iter); sord_iter_next(iter)) { + SordQuad tup; + sord_iter_get(iter, tup); + st = write_statement(model, writer, tup, 0); + } + sord_iter_free(iter); + + return !st; +} diff --git a/modules/juce_audio_processors/format_types/lv2/sord/src/zix/btree.c b/modules/juce_audio_processors/format_types/lv2/sord/src/zix/btree.c new file mode 100644 index 0000000000..7926a56a56 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/sord/src/zix/btree.c @@ -0,0 +1,936 @@ +/* + Copyright 2011-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "zix/btree.h" + +#include +#include +#include +#include + +// #define ZIX_BTREE_DEBUG 1 +// #define ZIX_BTREE_SORTED_CHECK 1 + +#ifndef ZIX_BTREE_PAGE_SIZE +# define ZIX_BTREE_PAGE_SIZE 4096 +#endif + +#define ZIX_BTREE_NODE_SPACE (ZIX_BTREE_PAGE_SIZE - 2 * sizeof(uint16_t)) +#define ZIX_BTREE_LEAF_VALS ((ZIX_BTREE_NODE_SPACE / sizeof(void*)) - 1) +#define ZIX_BTREE_INODE_VALS (ZIX_BTREE_LEAF_VALS / 2) + +struct ZixBTreeImpl { + ZixBTreeNode* root; + ZixDestroyFunc destroy; + ZixComparator cmp; + const void* cmp_data; + size_t size; + unsigned height; ///< Number of levels, i.e. root only has height 1 +}; + +struct ZixBTreeNodeImpl { + uint16_t is_leaf; + uint16_t n_vals; + // On 64-bit we rely on some padding here to get page-sized nodes + union { + struct { + void* vals[ZIX_BTREE_LEAF_VALS]; + } leaf; + + struct { + void* vals[ZIX_BTREE_INODE_VALS]; + ZixBTreeNode* children[ZIX_BTREE_INODE_VALS + 1]; + } inode; + } data; +}; + +#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112l) || \ + (defined(__cplusplus) && __cplusplus >= 201103L) +static_assert(sizeof(ZixBTreeNode) == ZIX_BTREE_PAGE_SIZE, ""); +#endif + +typedef struct { + ZixBTreeNode* node; + unsigned index; +} ZixBTreeIterFrame; + +struct ZixBTreeIterImpl { + unsigned n_levels; ///< Maximum depth of stack + unsigned level; ///< Current level in stack + ZixBTreeIterFrame stack[]; ///< Position stack +}; + +#ifdef ZIX_BTREE_DEBUG + +static void +print_node(const ZixBTreeNode* n, const char* prefix) +{ + printf("%s[", prefix); + for (uint16_t v = 0; v < n->n_vals; ++v) { + printf(" %lu", (uintptr_t)n->vals[v]); + } + printf(" ]\n"); +} + +static void +print_tree(const ZixBTreeNode* parent, const ZixBTreeNode* node, int level) +{ + if (node) { + if (!parent) { + printf("TREE {\n"); + } + for (int i = 0; i < level + 1; ++i) { + printf(" "); + } + print_node(node, ""); + if (!node->is_leaf) { + for (uint16_t i = 0; i < node->n_vals + 1; ++i) { + print_tree(node, node->data.inode.children[i], level + 1); + } + } + if (!parent) { + printf("}\n"); + } + } +} + +#endif // ZIX_BTREE_DEBUG + +static ZixBTreeNode* +zix_btree_node_new(const bool leaf) +{ +#if !((defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112l) || \ + (defined(__cplusplus) && __cplusplus >= 201103L)) + assert(sizeof(ZixBTreeNode) == ZIX_BTREE_PAGE_SIZE); +#endif + + ZixBTreeNode* node = (ZixBTreeNode*)malloc(sizeof(ZixBTreeNode)); + if (node) { + node->is_leaf = leaf; + node->n_vals = 0; + } + return node; +} + +static void* +zix_btree_value(const ZixBTreeNode* const node, const unsigned i) +{ + assert(i < node->n_vals); + return node->is_leaf ? node->data.leaf.vals[i] : node->data.inode.vals[i]; +} + +static ZixBTreeNode* +zix_btree_child(const ZixBTreeNode* const node, const unsigned i) +{ + assert(!node->is_leaf); + assert(i <= ZIX_BTREE_INODE_VALS); + return node->data.inode.children[i]; +} + +ZixBTree* +zix_btree_new(const ZixComparator cmp, + const void* const cmp_data, + const ZixDestroyFunc destroy) +{ + ZixBTree* t = (ZixBTree*)malloc(sizeof(ZixBTree)); + if (t) { + t->root = zix_btree_node_new(true); + t->destroy = destroy; + t->cmp = cmp; + t->cmp_data = cmp_data; + t->size = 0; + t->height = 1; + if (!t->root) { + free(t); + return NULL; + } + } + return t; +} + +static void +zix_btree_free_rec(ZixBTree* const t, ZixBTreeNode* const n) +{ + if (n) { + if (n->is_leaf) { + if (t->destroy) { + for (uint16_t i = 0; i < n->n_vals; ++i) { + t->destroy(n->data.leaf.vals[i]); + } + } + } else { + if (t->destroy) { + for (uint16_t i = 0; i < n->n_vals; ++i) { + t->destroy(n->data.inode.vals[i]); + } + } + + for (uint16_t i = 0; i < n->n_vals + 1; ++i) { + zix_btree_free_rec(t, zix_btree_child(n, i)); + } + } + + free(n); + } +} + +void +zix_btree_free(ZixBTree* const t) +{ + if (t) { + zix_btree_free_rec(t, t->root); + free(t); + } +} + +size_t +zix_btree_size(const ZixBTree* const t) +{ + return t->size; +} + +static uint16_t +zix_btree_max_vals(const ZixBTreeNode* const node) +{ + return node->is_leaf ? ZIX_BTREE_LEAF_VALS : ZIX_BTREE_INODE_VALS; +} + +static uint16_t +zix_btree_min_vals(const ZixBTreeNode* const node) +{ + return (uint16_t)(((zix_btree_max_vals(node) + 1U) / 2U) - 1U); +} + +/** Shift pointers in `array` of length `n` right starting at `i`. */ +static void +zix_btree_ainsert(void** const array, + const unsigned n, + const unsigned i, + void* const e) +{ + memmove(array + i + 1, array + i, (n - i) * sizeof(e)); + array[i] = e; +} + +/** Erase element `i` in `array` of length `n` and return erased element. */ +static void* +zix_btree_aerase(void** const array, const unsigned n, const unsigned i) +{ + void* const ret = array[i]; + memmove(array + i, array + i + 1, (n - i) * sizeof(ret)); + return ret; +} + +/** Split lhs, the i'th child of `n`, into two nodes. */ +static ZixBTreeNode* +zix_btree_split_child(ZixBTreeNode* const n, + const unsigned i, + ZixBTreeNode* const lhs) +{ + assert(lhs->n_vals == zix_btree_max_vals(lhs)); + assert(n->n_vals < ZIX_BTREE_INODE_VALS); + assert(i < n->n_vals + 1U); + assert(zix_btree_child(n, i) == lhs); + + const uint16_t max_n_vals = zix_btree_max_vals(lhs); + ZixBTreeNode* rhs = zix_btree_node_new(lhs->is_leaf); + if (!rhs) { + return NULL; + } + + // LHS and RHS get roughly half, less the middle value which moves up + lhs->n_vals = max_n_vals / 2U; + rhs->n_vals = (uint16_t)(max_n_vals - lhs->n_vals - 1); + + if (lhs->is_leaf) { + // Copy large half from LHS to new RHS node + memcpy(rhs->data.leaf.vals, + lhs->data.leaf.vals + lhs->n_vals + 1, + rhs->n_vals * sizeof(void*)); + + // Move middle value up to parent + zix_btree_ainsert( + n->data.inode.vals, n->n_vals, i, lhs->data.leaf.vals[lhs->n_vals]); + } else { + // Copy large half from LHS to new RHS node + memcpy(rhs->data.inode.vals, + lhs->data.inode.vals + lhs->n_vals + 1, + rhs->n_vals * sizeof(void*)); + memcpy(rhs->data.inode.children, + lhs->data.inode.children + lhs->n_vals + 1, + (rhs->n_vals + 1U) * sizeof(ZixBTreeNode*)); + + // Move middle value up to parent + zix_btree_ainsert( + n->data.inode.vals, n->n_vals, i, lhs->data.inode.vals[lhs->n_vals]); + } + + // Insert new RHS node in parent at position i + zix_btree_ainsert((void**)n->data.inode.children, ++n->n_vals, i + 1U, rhs); + + return rhs; +} + +#ifdef ZIX_BTREE_SORTED_CHECK +/** Check that `n` is sorted with respect to search key `e`. */ +static bool +zix_btree_node_is_sorted_with_respect_to(const ZixBTree* const t, + const ZixBTreeNode* const n, + const void* const e) +{ + if (n->n_vals <= 1) { + return true; + } + + int cmp = t->cmp(zix_btree_value(n, 0), e, t->cmp_data); + for (uint16_t i = 1; i < n->n_vals; ++i) { + const int next_cmp = t->cmp(zix_btree_value(n, i), e, t->cmp_data); + if ((cmp >= 0 && next_cmp < 0) || (cmp > 0 && next_cmp <= 0)) { + return false; + } + cmp = next_cmp; + } + + return true; +} +#endif + +/** Find the first value in `n` that is not less than `e` (lower bound). */ +static unsigned +zix_btree_node_find(const ZixBTree* const t, + const ZixBTreeNode* const n, + const void* const e, + bool* const equal) +{ +#ifdef ZIX_BTREE_SORTED_CHECK + assert(zix_btree_node_is_sorted_with_respect_to(t, n, e)); +#endif + + unsigned first = 0U; + unsigned len = n->n_vals; + while (len > 0) { + const unsigned half = len >> 1U; + const unsigned i = first + half; + const int cmp = t->cmp(zix_btree_value(n, i), e, t->cmp_data); + if (cmp == 0) { + *equal = true; + len = half; // Keep searching for wildcard matches + } else if (cmp < 0) { + const unsigned chop = half + 1U; + first += chop; + len -= chop; + } else { + len = half; + } + } + + assert(!*equal || t->cmp(zix_btree_value(n, first), e, t->cmp_data) == 0); + return first; +} + +ZixStatus +zix_btree_insert(ZixBTree* const t, void* const e) +{ + ZixBTreeNode* parent = NULL; // Parent of n + ZixBTreeNode* n = t->root; // Current node + unsigned i = 0; // Index of n in parent + while (n) { + if (n->n_vals == zix_btree_max_vals(n)) { + // Node is full, split to ensure there is space for a leaf split + if (!parent) { + // Root is full, grow tree upwards + if (!(parent = zix_btree_node_new(false))) { + return ZIX_STATUS_NO_MEM; + } + t->root = parent; + parent->data.inode.children[0] = n; + ++t->height; + } + + ZixBTreeNode* const rhs = zix_btree_split_child(parent, i, n); + if (!rhs) { + return ZIX_STATUS_NO_MEM; + } + + const int cmp = t->cmp(parent->data.inode.vals[i], e, t->cmp_data); + if (cmp == 0) { + return ZIX_STATUS_EXISTS; + } + + if (cmp < 0) { + // Move to new RHS + n = rhs; + ++i; + } + } + + assert(!parent || zix_btree_child(parent, i) == n); + + bool equal = false; + i = zix_btree_node_find(t, n, e, &equal); + if (equal) { + return ZIX_STATUS_EXISTS; + } + + if (!n->is_leaf) { + // Descend to child node left of value + parent = n; + n = zix_btree_child(n, i); + } else { + // Insert into internal node + zix_btree_ainsert(n->data.leaf.vals, n->n_vals++, i, e); + break; + } + } + + ++t->size; + + return ZIX_STATUS_SUCCESS; +} + +static ZixBTreeIter* +zix_btree_iter_new(const ZixBTree* const t) +{ + const size_t s = t->height * sizeof(ZixBTreeIterFrame); + + ZixBTreeIter* i = (ZixBTreeIter*)calloc(1, sizeof(ZixBTreeIter) + s); + if (i) { + i->n_levels = t->height; + } + return i; +} + +static void +zix_btree_iter_set_frame(ZixBTreeIter* const ti, + ZixBTreeNode* const n, + const unsigned i) +{ + if (ti) { + ti->stack[ti->level].node = n; + ti->stack[ti->level].index = i; + } +} + +static bool +zix_btree_node_is_minimal(ZixBTreeNode* const n) +{ + assert(n->n_vals >= zix_btree_min_vals(n)); + return n->n_vals == zix_btree_min_vals(n); +} + +/** Enlarge left child by stealing a value from its right sibling. */ +static ZixBTreeNode* +zix_btree_rotate_left(ZixBTreeNode* const parent, const unsigned i) +{ + ZixBTreeNode* const lhs = zix_btree_child(parent, i); + ZixBTreeNode* const rhs = zix_btree_child(parent, i + 1); + + assert(lhs->is_leaf == rhs->is_leaf); + + if (lhs->is_leaf) { + // Move parent value to end of LHS + lhs->data.leaf.vals[lhs->n_vals++] = parent->data.inode.vals[i]; + + // Move first value in RHS to parent + parent->data.inode.vals[i] = + zix_btree_aerase(rhs->data.leaf.vals, rhs->n_vals, 0); + } else { + // Move parent value to end of LHS + lhs->data.inode.vals[lhs->n_vals++] = parent->data.inode.vals[i]; + + // Move first value in RHS to parent + parent->data.inode.vals[i] = + zix_btree_aerase(rhs->data.inode.vals, rhs->n_vals, 0); + + // Move first child pointer from RHS to end of LHS + lhs->data.inode.children[lhs->n_vals] = (ZixBTreeNode*)zix_btree_aerase( + (void**)rhs->data.inode.children, rhs->n_vals, 0); + } + + --rhs->n_vals; + + return lhs; +} + +/** Enlarge right child by stealing a value from its left sibling. */ +static ZixBTreeNode* +zix_btree_rotate_right(ZixBTreeNode* const parent, const unsigned i) +{ + ZixBTreeNode* const lhs = zix_btree_child(parent, i - 1); + ZixBTreeNode* const rhs = zix_btree_child(parent, i); + + assert(lhs->is_leaf == rhs->is_leaf); + + if (lhs->is_leaf) { + // Prepend parent value to RHS + zix_btree_ainsert( + rhs->data.leaf.vals, rhs->n_vals++, 0, parent->data.inode.vals[i - 1]); + + // Move last value from LHS to parent + parent->data.inode.vals[i - 1] = lhs->data.leaf.vals[--lhs->n_vals]; + } else { + // Prepend parent value to RHS + zix_btree_ainsert( + rhs->data.inode.vals, rhs->n_vals++, 0, parent->data.inode.vals[i - 1]); + + // Move last child pointer from LHS and prepend to RHS + zix_btree_ainsert((void**)rhs->data.inode.children, + rhs->n_vals, + 0, + lhs->data.inode.children[lhs->n_vals]); + + // Move last value from LHS to parent + parent->data.inode.vals[i - 1] = lhs->data.inode.vals[--lhs->n_vals]; + } + + return rhs; +} + +/** Move n[i] down, merge the left and right child, return the merged node. */ +static ZixBTreeNode* +zix_btree_merge(ZixBTree* const t, ZixBTreeNode* const n, const unsigned i) +{ + ZixBTreeNode* const lhs = zix_btree_child(n, i); + ZixBTreeNode* const rhs = zix_btree_child(n, i + 1); + + assert(lhs->is_leaf == rhs->is_leaf); + assert(zix_btree_node_is_minimal(lhs)); + assert(lhs->n_vals + rhs->n_vals < zix_btree_max_vals(lhs)); + + // Move parent value to end of LHS + if (lhs->is_leaf) { + lhs->data.leaf.vals[lhs->n_vals++] = + zix_btree_aerase(n->data.inode.vals, n->n_vals, i); + } else { + lhs->data.inode.vals[lhs->n_vals++] = + zix_btree_aerase(n->data.inode.vals, n->n_vals, i); + } + + // Erase corresponding child pointer (to RHS) in parent + zix_btree_aerase((void**)n->data.inode.children, n->n_vals, i + 1U); + + // Add everything from RHS to end of LHS + if (lhs->is_leaf) { + memcpy(lhs->data.leaf.vals + lhs->n_vals, + rhs->data.leaf.vals, + rhs->n_vals * sizeof(void*)); + } else { + memcpy(lhs->data.inode.vals + lhs->n_vals, + rhs->data.inode.vals, + rhs->n_vals * sizeof(void*)); + memcpy(lhs->data.inode.children + lhs->n_vals, + rhs->data.inode.children, + (rhs->n_vals + 1U) * sizeof(void*)); + } + + lhs->n_vals = (uint16_t)(lhs->n_vals + rhs->n_vals); + + if (--n->n_vals == 0) { + // Root is now empty, replace it with its only child + assert(n == t->root); + t->root = lhs; + free(n); + } + + free(rhs); + return lhs; +} + +/** Remove and return the min value from the subtree rooted at `n`. */ +static void* +zix_btree_remove_min(ZixBTree* const t, ZixBTreeNode* n) +{ + while (!n->is_leaf) { + if (zix_btree_node_is_minimal(zix_btree_child(n, 0))) { + // Leftmost child is minimal, must expand + if (!zix_btree_node_is_minimal(zix_btree_child(n, 1))) { + // Child's right sibling has at least one key to steal + n = zix_btree_rotate_left(n, 0); + } else { + // Both child and right sibling are minimal, merge + n = zix_btree_merge(t, n, 0); + } + } else { + n = zix_btree_child(n, 0); + } + } + + return zix_btree_aerase(n->data.leaf.vals, --n->n_vals, 0); +} + +/** Remove and return the max value from the subtree rooted at `n`. */ +static void* +zix_btree_remove_max(ZixBTree* const t, ZixBTreeNode* n) +{ + while (!n->is_leaf) { + if (zix_btree_node_is_minimal(zix_btree_child(n, n->n_vals))) { + // Leftmost child is minimal, must expand + if (!zix_btree_node_is_minimal(zix_btree_child(n, n->n_vals - 1))) { + // Child's left sibling has at least one key to steal + n = zix_btree_rotate_right(n, n->n_vals); + } else { + // Both child and left sibling are minimal, merge + n = zix_btree_merge(t, n, n->n_vals - 1U); + } + } else { + n = zix_btree_child(n, n->n_vals); + } + } + + return n->data.leaf.vals[--n->n_vals]; +} + +ZixStatus +zix_btree_remove(ZixBTree* const t, + const void* const e, + void** const out, + ZixBTreeIter** const next) +{ + ZixBTreeNode* n = t->root; + ZixBTreeIter* ti = NULL; + const bool user_iter = next && *next; + if (next) { + if (!*next && !(*next = zix_btree_iter_new(t))) { + return ZIX_STATUS_NO_MEM; + } + ti = *next; + ti->level = 0; + } + + while (true) { + /* To remove in a single walk down, the tree is adjusted along the way + so that the current node always has at least one more value than the + minimum required in general. Thus, there is always room to remove + without adjusting on the way back up. */ + assert(n == t->root || !zix_btree_node_is_minimal(n)); + + bool equal = false; + const unsigned i = zix_btree_node_find(t, n, e, &equal); + zix_btree_iter_set_frame(ti, n, i); + if (n->is_leaf) { + if (equal) { + // Found in leaf node + *out = zix_btree_aerase(n->data.leaf.vals, --n->n_vals, i); + if (ti && i == n->n_vals) { + if (i == 0) { + ti->level = 0; + ti->stack[0].node = NULL; + } else { + --ti->stack[ti->level].index; + zix_btree_iter_increment(ti); + } + } + --t->size; + return ZIX_STATUS_SUCCESS; + } + + // Not found in leaf node, or tree + if (ti && !user_iter) { + zix_btree_iter_free(ti); + *next = NULL; + } + + return ZIX_STATUS_NOT_FOUND; + } + + if (equal) { + // Found in internal node + ZixBTreeNode* const lhs = zix_btree_child(n, i); + ZixBTreeNode* const rhs = zix_btree_child(n, i + 1); + const size_t l_size = lhs->n_vals; + const size_t r_size = rhs->n_vals; + if (zix_btree_node_is_minimal(lhs) && zix_btree_node_is_minimal(rhs)) { + // Both preceding and succeeding child are minimal + n = zix_btree_merge(t, n, i); + } else if (l_size >= r_size) { + // Left child can remove without merge + assert(!zix_btree_node_is_minimal(lhs)); + *out = n->data.inode.vals[i]; + n->data.inode.vals[i] = zix_btree_remove_max(t, lhs); + --t->size; + return ZIX_STATUS_SUCCESS; + } else { + // Right child can remove without merge + assert(!zix_btree_node_is_minimal(rhs)); + *out = n->data.inode.vals[i]; + n->data.inode.vals[i] = zix_btree_remove_min(t, rhs); + --t->size; + return ZIX_STATUS_SUCCESS; + } + } else { + // Not found in internal node, key is in/under children[i] + if (zix_btree_node_is_minimal(zix_btree_child(n, i))) { + if (i > 0 && !zix_btree_node_is_minimal(zix_btree_child(n, i - 1))) { + // Steal a key from child's left sibling + n = zix_btree_rotate_right(n, i); + } else if (i < n->n_vals && + !zix_btree_node_is_minimal(zix_btree_child(n, i + 1))) { + // Steal a key from child's right sibling + n = zix_btree_rotate_left(n, i); + } else if (n == t->root && n->n_vals == 1) { + // Root has two children, both minimal, delete it + assert(i == 0 || i == 1); + const uint16_t counts[2] = {zix_btree_child(n, 0)->n_vals, + zix_btree_child(n, 1)->n_vals}; + + n = zix_btree_merge(t, n, 0); + if (ti) { + ti->stack[ti->level].node = n; + ti->stack[ti->level].index = counts[i]; + } + } else { + // Both child's siblings are minimal, merge them + if (i < n->n_vals) { + n = zix_btree_merge(t, n, i); + } else { + n = zix_btree_merge(t, n, i - 1U); + if (ti) { + --ti->stack[ti->level].index; + } + } + } + } else { + n = zix_btree_child(n, i); + } + } + if (ti) { + ++ti->level; + } + } + + assert(false); // Not reached + return ZIX_STATUS_ERROR; +} + +ZixStatus +zix_btree_find(const ZixBTree* const t, + const void* const e, + ZixBTreeIter** const ti) +{ + ZixBTreeNode* n = t->root; + if (!(*ti = zix_btree_iter_new(t))) { + return ZIX_STATUS_NO_MEM; + } + + while (n) { + bool equal = false; + const unsigned i = zix_btree_node_find(t, n, e, &equal); + + zix_btree_iter_set_frame(*ti, n, i); + + if (equal) { + return ZIX_STATUS_SUCCESS; + } + + if (n->is_leaf) { + break; + } + + ++(*ti)->level; + n = zix_btree_child(n, i); + } + + zix_btree_iter_free(*ti); + *ti = NULL; + return ZIX_STATUS_NOT_FOUND; +} + +ZixStatus +zix_btree_lower_bound(const ZixBTree* const t, + const void* const e, + ZixBTreeIter** const ti) +{ + if (!t) { + *ti = NULL; + return ZIX_STATUS_BAD_ARG; + } + + if (!t->root) { + *ti = NULL; + return ZIX_STATUS_SUCCESS; + } + + ZixBTreeNode* n = t->root; + bool found = false; + unsigned found_level = 0; + if (!(*ti = zix_btree_iter_new(t))) { + return ZIX_STATUS_NO_MEM; + } + + while (n) { + bool equal = false; + const unsigned i = zix_btree_node_find(t, n, e, &equal); + + zix_btree_iter_set_frame(*ti, n, i); + + if (equal) { + found_level = (*ti)->level; + found = true; + } + + if (n->is_leaf) { + break; + } + + ++(*ti)->level; + n = zix_btree_child(n, i); + assert(n); + } + + const ZixBTreeIterFrame* const frame = &(*ti)->stack[(*ti)->level]; + assert(frame->node); + if (frame->index == frame->node->n_vals) { + if (found) { + // Found on a previous level but went too far + (*ti)->level = found_level; + } else { + // Reached end (key is greater than everything in tree) + (*ti)->level = 0; + (*ti)->stack[0].node = NULL; + } + } + + return ZIX_STATUS_SUCCESS; +} + +void* +zix_btree_get(const ZixBTreeIter* const ti) +{ + const ZixBTreeIterFrame* const frame = &ti->stack[ti->level]; + assert(frame->node); + assert(frame->index < frame->node->n_vals); + return zix_btree_value(frame->node, frame->index); +} + +ZixBTreeIter* +zix_btree_begin(const ZixBTree* const t) +{ + ZixBTreeIter* const i = zix_btree_iter_new(t); + if (!i) { + return NULL; + } + + if (t->size == 0) { + i->level = 0; + i->stack[0].node = NULL; + } else { + ZixBTreeNode* n = t->root; + i->stack[0].node = n; + i->stack[0].index = 0; + while (!n->is_leaf) { + n = zix_btree_child(n, 0); + ++i->level; + i->stack[i->level].node = n; + i->stack[i->level].index = 0; + } + } + + return i; +} + +ZixBTreeIter* +zix_btree_end(const ZixBTree* const t) +{ + return zix_btree_iter_new(t); +} + +ZixBTreeIter* +zix_btree_iter_copy(const ZixBTreeIter* const i) +{ + if (!i) { + return NULL; + } + + const size_t s = i->n_levels * sizeof(ZixBTreeIterFrame); + ZixBTreeIter* j = (ZixBTreeIter*)calloc(1, sizeof(ZixBTreeIter) + s); + if (j) { + memcpy(j, i, sizeof(ZixBTreeIter) + s); + } + + return j; +} + +bool +zix_btree_iter_is_end(const ZixBTreeIter* const i) +{ + return !i || (i->level == 0 && i->stack[0].node == NULL); +} + +bool +zix_btree_iter_equals(const ZixBTreeIter* const lhs, + const ZixBTreeIter* const rhs) +{ + if (zix_btree_iter_is_end(lhs) && zix_btree_iter_is_end(rhs)) { + return true; + } + + if (zix_btree_iter_is_end(lhs) || zix_btree_iter_is_end(rhs) || + lhs->level != rhs->level) { + return false; + } + + return !memcmp(lhs, + rhs, + sizeof(ZixBTreeIter) + + (lhs->level + 1) * sizeof(ZixBTreeIterFrame)); +} + +void +zix_btree_iter_increment(ZixBTreeIter* const i) +{ + ZixBTreeIterFrame* f = &i->stack[i->level]; + if (f->node->is_leaf) { + // Leaf, move right + assert(f->index < f->node->n_vals); + if (++f->index == f->node->n_vals) { + // Reached end of leaf, move up + f = &i->stack[i->level]; + while (i->level > 0 && f->index == f->node->n_vals) { + f = &i->stack[--i->level]; + assert(f->index <= f->node->n_vals); + } + + if (f->index == f->node->n_vals) { + // Reached end of tree + assert(i->level == 0); + f->node = NULL; + f->index = 0; + } + } + } else { + // Internal node, move down to next child + assert(f->index < f->node->n_vals); + ZixBTreeNode* child = zix_btree_child(f->node, ++f->index); + + f = &i->stack[++i->level]; + f->node = child; + f->index = 0; + + // Move down and left until we hit a leaf + while (!f->node->is_leaf) { + child = zix_btree_child(f->node, 0); + f = &i->stack[++i->level]; + f->node = child; + f->index = 0; + } + } +} + +void +zix_btree_iter_free(ZixBTreeIter* const i) +{ + free(i); +} diff --git a/modules/juce_audio_processors/format_types/lv2/sord/src/zix/btree.h b/modules/juce_audio_processors/format_types/lv2/sord/src/zix/btree.h new file mode 100644 index 0000000000..bec0698896 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/sord/src/zix/btree.h @@ -0,0 +1,187 @@ +/* + Copyright 2011-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef ZIX_BTREE_H +#define ZIX_BTREE_H + +#include "zix/common.h" + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + @addtogroup zix + @{ + @name BTree + @{ +*/ + +/** + A B-Tree. +*/ +typedef struct ZixBTreeImpl ZixBTree; + +/** + A B-Tree node (opaque). +*/ +typedef struct ZixBTreeNodeImpl ZixBTreeNode; + +/** + An iterator over a B-Tree. + + Note that modifying the trees invalidates all iterators, so all iterators + are const iterators. +*/ +typedef struct ZixBTreeIterImpl ZixBTreeIter; + +/** + Create a new (empty) B-Tree. +*/ +ZIX_API +ZixBTree* +zix_btree_new(ZixComparator cmp, const void* cmp_data, ZixDestroyFunc destroy); + +/** + Free `t`. +*/ +ZIX_API +void +zix_btree_free(ZixBTree* t); + +/** + Return the number of elements in `t`. +*/ +ZIX_PURE_API +size_t +zix_btree_size(const ZixBTree* t); + +/** + Insert the element `e` into `t`. +*/ +ZIX_API +ZixStatus +zix_btree_insert(ZixBTree* t, void* e); + +/** + Remove the value `e` from `t`. + + @param t Tree to remove from. + + @param e Value to remove. + + @param out Set to point to the removed pointer (which may not equal `e`). + + @param next If non-NULL, pointed to the value following `e`. If *next is + also non-NULL, the iterator is reused, otherwise a new one is allocated. To + reuse an iterator, no items may have been added since its creation. +*/ +ZIX_API +ZixStatus +zix_btree_remove(ZixBTree* t, const void* e, void** out, ZixBTreeIter** next); + +/** + Set `ti` to an element equal to `e` in `t`. + If no such item exists, `ti` is set to NULL. +*/ +ZIX_API +ZixStatus +zix_btree_find(const ZixBTree* t, const void* e, ZixBTreeIter** ti); + +/** + Set `ti` to the smallest element in `t` that is not less than `e`. + + Wildcards are supported, so if the search key `e` compares equal to many + values in the tree, `ti` will be set to the least such element. The search + key `e` is always passed as the second argument to the comparator. +*/ +ZIX_API +ZixStatus +zix_btree_lower_bound(const ZixBTree* t, const void* e, ZixBTreeIter** ti); + +/** + Return the data associated with the given tree item. +*/ +ZIX_PURE_API +void* +zix_btree_get(const ZixBTreeIter* ti); + +/** + Return an iterator to the first (smallest) element in `t`. + + The returned iterator must be freed with zix_btree_iter_free(). +*/ +ZIX_PURE_API +ZixBTreeIter* +zix_btree_begin(const ZixBTree* t); + +/** + Return an iterator to the end of `t` (one past the last element). + + The returned iterator must be freed with zix_btree_iter_free(). +*/ +ZIX_API +ZixBTreeIter* +zix_btree_end(const ZixBTree* t); + +/** + Return a new copy of `i`. +*/ +ZIX_API +ZixBTreeIter* +zix_btree_iter_copy(const ZixBTreeIter* i); + +/** + Return true iff `lhs` is equal to `rhs`. +*/ +ZIX_PURE_API +bool +zix_btree_iter_equals(const ZixBTreeIter* lhs, const ZixBTreeIter* rhs); + +/** + Return true iff `i` is an iterator to the end of its tree. +*/ +ZIX_PURE_API +bool +zix_btree_iter_is_end(const ZixBTreeIter* i); + +/** + Increment `i` to point to the next element in the tree. +*/ +ZIX_API +void +zix_btree_iter_increment(ZixBTreeIter* i); + +/** + Free `i`. +*/ +ZIX_API +void +zix_btree_iter_free(ZixBTreeIter* i); + +/** + @} + @} +*/ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* ZIX_BTREE_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/sord/src/zix/common.h b/modules/juce_audio_processors/format_types/lv2/sord/src/zix/common.h new file mode 100644 index 0000000000..d47586c606 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/sord/src/zix/common.h @@ -0,0 +1,138 @@ +/* + Copyright 2016-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef ZIX_COMMON_H +#define ZIX_COMMON_H + +#include + +/** + @addtogroup zix + @{ +*/ + +/** @cond */ +#if defined(_WIN32) && !defined(ZIX_STATIC) && defined(ZIX_INTERNAL) +# define ZIX_API __declspec(dllexport) +#elif defined(_WIN32) && !defined(ZIX_STATIC) +# define ZIX_API __declspec(dllimport) +#elif defined(__GNUC__) +# define ZIX_API __attribute__((visibility("default"))) +#else +# define ZIX_API +#endif + +#ifdef __GNUC__ +# define ZIX_PURE_FUNC __attribute__((pure)) +# define ZIX_CONST_FUNC __attribute__((const)) +# define ZIX_MALLOC_FUNC __attribute__((malloc)) +#else +# define ZIX_PURE_FUNC +# define ZIX_CONST_FUNC +# define ZIX_MALLOC_FUNC +#endif + +#define ZIX_PURE_API \ + ZIX_API \ + ZIX_PURE_FUNC + +#define ZIX_CONST_API \ + ZIX_API \ + ZIX_CONST_FUNC + +#define ZIX_MALLOC_API \ + ZIX_API \ + ZIX_MALLOC_FUNC + +/** @endcond */ + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef __GNUC__ +# define ZIX_LOG_FUNC(fmt, arg1) __attribute__((format(printf, fmt, arg1))) +#else +# define ZIX_LOG_FUNC(fmt, arg1) +#endif + +// Unused parameter macro to suppresses warnings and make it impossible to use +#if defined(__cplusplus) +# define ZIX_UNUSED(name) +#elif defined(__GNUC__) +# define ZIX_UNUSED(name) name##_unused __attribute__((__unused__)) +#else +# define ZIX_UNUSED(name) name +#endif + +typedef enum { + ZIX_STATUS_SUCCESS, + ZIX_STATUS_ERROR, + ZIX_STATUS_NO_MEM, + ZIX_STATUS_NOT_FOUND, + ZIX_STATUS_EXISTS, + ZIX_STATUS_BAD_ARG, + ZIX_STATUS_BAD_PERMS +} ZixStatus; + +static inline const char* +zix_strerror(const ZixStatus status) +{ + switch (status) { + case ZIX_STATUS_SUCCESS: + return "Success"; + case ZIX_STATUS_ERROR: + return "Unknown error"; + case ZIX_STATUS_NO_MEM: + return "Out of memory"; + case ZIX_STATUS_NOT_FOUND: + return "Not found"; + case ZIX_STATUS_EXISTS: + return "Exists"; + case ZIX_STATUS_BAD_ARG: + return "Bad argument"; + case ZIX_STATUS_BAD_PERMS: + return "Bad permissions"; + } + return "Unknown error"; +} + +/** + Function for comparing two elements. +*/ +typedef int (*ZixComparator)(const void* a, + const void* b, + const void* user_data); + +/** + Function for testing equality of two elements. +*/ +typedef bool (*ZixEqualFunc)(const void* a, const void* b); + +/** + Function to destroy an element. +*/ +typedef void (*ZixDestroyFunc)(void* ptr); + +/** + @} +*/ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* ZIX_COMMON_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/sord/src/zix/digest.c b/modules/juce_audio_processors/format_types/lv2/sord/src/zix/digest.c new file mode 100644 index 0000000000..47d27b9471 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/sord/src/zix/digest.c @@ -0,0 +1,141 @@ +/* + Copyright 2012-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "zix/digest.h" + +#ifdef __SSE4_2__ +# include +#endif + +#include +#include + +#ifdef __SSE4_2__ + +// SSE 4.2 CRC32 + +uint32_t +zix_digest_start(void) +{ + return 1; +} + +uint32_t +zix_digest_add(uint32_t hash, const void* const buf, const size_t len) +{ + const uint8_t* str = (const uint8_t*)buf; + +# ifdef __x86_64__ + for (size_t i = 0; i < (len / sizeof(uint64_t)); ++i) { + hash = (uint32_t)_mm_crc32_u64(hash, *(const uint64_t*)str); + str += sizeof(uint64_t); + } + if (len & sizeof(uint32_t)) { + hash = _mm_crc32_u32(hash, *(const uint32_t*)str); + str += sizeof(uint32_t); + } +# else + for (size_t i = 0; i < (len / sizeof(uint32_t)); ++i) { + hash = _mm_crc32_u32(hash, *(const uint32_t*)str); + str += sizeof(uint32_t); + } +# endif + if (len & sizeof(uint16_t)) { + hash = _mm_crc32_u16(hash, *(const uint16_t*)str); + str += sizeof(uint16_t); + } + if (len & sizeof(uint8_t)) { + hash = _mm_crc32_u8(hash, *(const uint8_t*)str); + } + + return hash; +} + +uint32_t +zix_digest_add_64(uint32_t hash, const void* const buf, const size_t len) +{ + assert((uintptr_t)buf % sizeof(uint64_t) == 0); + assert(len % sizeof(uint64_t) == 0); + +# ifdef __x86_64__ + const uint64_t* ptr = (const uint64_t*)buf; + + for (size_t i = 0; i < (len / sizeof(uint64_t)); ++i) { + hash = (uint32_t)_mm_crc32_u64(hash, *ptr); + ++ptr; + } + + return hash; +# else + const uint32_t* ptr = (const uint32_t*)buf; + + for (size_t i = 0; i < (len / sizeof(uint32_t)); ++i) { + hash = _mm_crc32_u32(hash, *ptr); + ++ptr; + } + + return hash; +# endif +} + +uint32_t +zix_digest_add_ptr(const uint32_t hash, const void* const ptr) +{ +# ifdef __x86_64__ + return (uint32_t)_mm_crc32_u64(hash, (uintptr_t)ptr); +# else + return _mm_crc32_u32(hash, (uintptr_t)ptr); +# endif +} + +#else + +// Classic DJB hash + +uint32_t +zix_digest_start(void) +{ + return 5381; +} + +uint32_t +zix_digest_add(uint32_t hash, const void* const buf, const size_t len) +{ + const uint8_t* str = (const uint8_t*)buf; + + for (size_t i = 0; i < len; ++i) { + hash = (hash << 5u) + hash + str[i]; + } + + return hash; +} + +uint32_t +zix_digest_add_64(uint32_t hash, const void* const buf, const size_t len) +{ + assert((uintptr_t)buf % sizeof(uint64_t) == 0); + assert(len % sizeof(uint64_t) == 0); + + return zix_digest_add(hash, buf, len); +} + +uint32_t +zix_digest_add_ptr(const uint32_t hash, const void* const ptr) +{ + return zix_digest_add(hash, &ptr, sizeof(ptr)); +} + +#endif diff --git a/modules/juce_audio_processors/format_types/lv2/sord/src/zix/digest.h b/modules/juce_audio_processors/format_types/lv2/sord/src/zix/digest.h new file mode 100644 index 0000000000..1fde77a999 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/sord/src/zix/digest.h @@ -0,0 +1,67 @@ +/* + Copyright 2012-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef ZIX_DIGEST_H +#define ZIX_DIGEST_H + +#include "zix/common.h" + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + Return an initial empty digest value. +*/ +ZIX_CONST_API +uint32_t +zix_digest_start(void); + +/** + Update `hash` to include `buf`, a buffer of `len` bytes. + + This can be used for any size or alignment. +*/ +ZIX_PURE_API +uint32_t +zix_digest_add(uint32_t hash, const void* buf, size_t len); + +/** + Update `hash` to include `buf`, a 64-bit aligned buffer of `len` bytes. + + Both `buf` and `len` must be evenly divisible by 8 (64 bits). +*/ +ZIX_PURE_API +uint32_t +zix_digest_add_64(uint32_t hash, const void* buf, size_t len); + +/** + Update `hash` to include `ptr`. + + This hashes the value of the pointer itself, and does not dereference `ptr`. +*/ +ZIX_CONST_API +uint32_t +zix_digest_add_ptr(uint32_t hash, const void* ptr); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* ZIX_DIGEST_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/sord/src/zix/hash.c b/modules/juce_audio_processors/format_types/lv2/sord/src/zix/hash.c new file mode 100644 index 0000000000..8183082049 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/sord/src/zix/hash.c @@ -0,0 +1,230 @@ +/* + Copyright 2011-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "zix/hash.h" + +#include +#include +#include + +/** + Primes, each slightly less than twice its predecessor, and as far away + from powers of two as possible. +*/ +static const unsigned sizes[] = { + 53, 97, 193, 389, 769, 1543, 3079, + 6151, 12289, 24593, 49157, 98317, 196613, 393241, + 786433, 1572869, 3145739, 6291469, 12582917, 25165843, 50331653, + 100663319, 201326611, 402653189, 805306457, 1610612741, 0}; + +typedef struct ZixHashEntry { + struct ZixHashEntry* next; ///< Next entry in bucket + uint32_t hash; ///< Non-modulo hash value + // Value follows here (access with zix_hash_value) +} ZixHashEntry; + +struct ZixHashImpl { + ZixHashFunc hash_func; + ZixEqualFunc equal_func; + ZixHashEntry** buckets; + const unsigned* n_buckets; + size_t value_size; + unsigned count; +}; + +static inline void* +zix_hash_value(ZixHashEntry* entry) +{ + return entry + 1; +} + +ZixHash* +zix_hash_new(ZixHashFunc hash_func, ZixEqualFunc equal_func, size_t value_size) +{ + ZixHash* hash = (ZixHash*)malloc(sizeof(ZixHash)); + if (hash) { + hash->hash_func = hash_func; + hash->equal_func = equal_func; + hash->n_buckets = &sizes[0]; + hash->value_size = value_size; + hash->count = 0; + if (!(hash->buckets = + (ZixHashEntry**)calloc(*hash->n_buckets, sizeof(ZixHashEntry*)))) { + free(hash); + return NULL; + } + } + return hash; +} + +void +zix_hash_free(ZixHash* hash) +{ + if (!hash) { + return; + } + + for (unsigned b = 0; b < *hash->n_buckets; ++b) { + ZixHashEntry* bucket = hash->buckets[b]; + for (ZixHashEntry* e = bucket; e;) { + ZixHashEntry* next = e->next; + free(e); + e = next; + } + } + + free(hash->buckets); + free(hash); +} + +size_t +zix_hash_size(const ZixHash* hash) +{ + return hash->count; +} + +static inline void +insert_entry(ZixHashEntry** bucket, ZixHashEntry* entry) +{ + entry->next = *bucket; + *bucket = entry; +} + +static inline ZixStatus +rehash(ZixHash* hash, unsigned new_n_buckets) +{ + ZixHashEntry** new_buckets = + (ZixHashEntry**)calloc(new_n_buckets, sizeof(ZixHashEntry*)); + if (!new_buckets) { + return ZIX_STATUS_NO_MEM; + } + + const unsigned old_n_buckets = *hash->n_buckets; + for (unsigned b = 0; b < old_n_buckets; ++b) { + for (ZixHashEntry* e = hash->buckets[b]; e;) { + ZixHashEntry* const next = e->next; + const unsigned h = e->hash % new_n_buckets; + insert_entry(&new_buckets[h], e); + e = next; + } + } + + free(hash->buckets); + hash->buckets = new_buckets; + + return ZIX_STATUS_SUCCESS; +} + +static inline ZixHashEntry* +find_entry(const ZixHash* hash, + const void* key, + const unsigned h, + const unsigned h_nomod) +{ + for (ZixHashEntry* e = hash->buckets[h]; e; e = e->next) { + if (e->hash == h_nomod && hash->equal_func(zix_hash_value(e), key)) { + return e; + } + } + return NULL; +} + +void* +zix_hash_find(const ZixHash* hash, const void* value) +{ + const unsigned h_nomod = hash->hash_func(value); + const unsigned h = h_nomod % *hash->n_buckets; + ZixHashEntry* const entry = find_entry(hash, value, h, h_nomod); + return entry ? zix_hash_value(entry) : 0; +} + +ZixStatus +zix_hash_insert(ZixHash* hash, const void* value, void** inserted) +{ + unsigned h_nomod = hash->hash_func(value); + unsigned h = h_nomod % *hash->n_buckets; + + ZixHashEntry* elem = find_entry(hash, value, h, h_nomod); + if (elem) { + assert(elem->hash == h_nomod); + if (inserted) { + *inserted = zix_hash_value(elem); + } + return ZIX_STATUS_EXISTS; + } + + elem = (ZixHashEntry*)malloc(sizeof(ZixHashEntry) + hash->value_size); + if (!elem) { + return ZIX_STATUS_NO_MEM; + } + elem->next = NULL; + elem->hash = h_nomod; + memcpy(elem + 1, value, hash->value_size); + + const unsigned next_n_buckets = *(hash->n_buckets + 1); + if (next_n_buckets != 0 && (hash->count + 1) >= next_n_buckets) { + if (!rehash(hash, next_n_buckets)) { + h = h_nomod % *(++hash->n_buckets); + } + } + + insert_entry(&hash->buckets[h], elem); + ++hash->count; + if (inserted) { + *inserted = zix_hash_value(elem); + } + return ZIX_STATUS_SUCCESS; +} + +ZixStatus +zix_hash_remove(ZixHash* hash, const void* value) +{ + const unsigned h_nomod = hash->hash_func(value); + const unsigned h = h_nomod % *hash->n_buckets; + + ZixHashEntry** next_ptr = &hash->buckets[h]; + for (ZixHashEntry* e = hash->buckets[h]; e; e = e->next) { + if (h_nomod == e->hash && hash->equal_func(zix_hash_value(e), value)) { + *next_ptr = e->next; + free(e); + return ZIX_STATUS_SUCCESS; + } + next_ptr = &e->next; + } + + if (hash->n_buckets != sizes) { + const unsigned prev_n_buckets = *(hash->n_buckets - 1); + if (hash->count - 1 <= prev_n_buckets) { + if (!rehash(hash, prev_n_buckets)) { + --hash->n_buckets; + } + } + } + + --hash->count; + return ZIX_STATUS_NOT_FOUND; +} + +void +zix_hash_foreach(ZixHash* hash, ZixHashVisitFunc f, void* user_data) +{ + for (unsigned b = 0; b < *hash->n_buckets; ++b) { + ZixHashEntry* bucket = hash->buckets[b]; + for (ZixHashEntry* e = bucket; e; e = e->next) { + f(zix_hash_value(e), user_data); + } + } +} diff --git a/modules/juce_audio_processors/format_types/lv2/sord/src/zix/hash.h b/modules/juce_audio_processors/format_types/lv2/sord/src/zix/hash.h new file mode 100644 index 0000000000..390503819a --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/sord/src/zix/hash.h @@ -0,0 +1,138 @@ +/* + Copyright 2011-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef ZIX_HASH_H +#define ZIX_HASH_H + +#include "zix/common.h" + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + @addtogroup zix + @{ + @name Hash + @{ +*/ + +typedef struct ZixHashImpl ZixHash; + +/** + Function for computing the hash of an element. +*/ +typedef uint32_t (*ZixHashFunc)(const void* value); + +/** + Function to visit a hash element. +*/ +typedef void (*ZixHashVisitFunc)(void* value, void* user_data); + +/** + Create a new hash table. + + To minimize space overhead, unlike many hash tables this stores a single + value, not a key and a value. Any size of value can be stored, but all the + values in the hash table must be the same size, and the values must be safe + to copy with memcpy. To get key:value behaviour, simply insert a struct + with a key and value into the hash. + + @param hash_func The hashing function. + @param equal_func A function to test value equality. + @param value_size The size of the values to be stored. +*/ +ZIX_API +ZixHash* +zix_hash_new(ZixHashFunc hash_func, ZixEqualFunc equal_func, size_t value_size); + +/** + Free `hash`. +*/ +ZIX_API +void +zix_hash_free(ZixHash* hash); + +/** + Return the number of elements in `hash`. +*/ +ZIX_PURE_API +size_t +zix_hash_size(const ZixHash* hash); + +/** + Insert an item into `hash`. + + If no matching value is found, ZIX_STATUS_SUCCESS will be returned, and @p + inserted will be pointed to the copy of `value` made in the new hash node. + + If a matching value already exists, ZIX_STATUS_EXISTS will be returned, and + `inserted` will be pointed to the existing value. + + @param hash The hash table. + @param value The value to be inserted. + @param inserted The copy of `value` in the hash table. + @return ZIX_STATUS_SUCCESS, ZIX_STATUS_EXISTS, or ZIX_STATUS_NO_MEM. +*/ +ZIX_API +ZixStatus +zix_hash_insert(ZixHash* hash, const void* value, void** inserted); + +/** + Remove an item from `hash`. + + @param hash The hash table. + @param value The value to remove. + @return ZIX_STATUS_SUCCES or ZIX_STATUS_NOT_FOUND. +*/ +ZIX_API +ZixStatus +zix_hash_remove(ZixHash* hash, const void* value); + +/** + Search for an item in `hash`. + + @param hash The hash table. + @param value The value to search for. +*/ +ZIX_API +void* +zix_hash_find(const ZixHash* hash, const void* value); + +/** + Call `f` on each value in `hash`. + + @param hash The hash table. + @param f The function to call on each value. + @param user_data The user_data parameter passed to `f`. +*/ +ZIX_API +void +zix_hash_foreach(ZixHash* hash, ZixHashVisitFunc f, void* user_data); + +/** + @} + @} +*/ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* ZIX_HASH_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/sord_config.h b/modules/juce_audio_processors/format_types/lv2/sord_config.h new file mode 100644 index 0000000000..9a10500203 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/sord_config.h @@ -0,0 +1,3 @@ +#pragma once + +#include "juce_lv2_config.h" diff --git a/modules/juce_audio_processors/format_types/lv2/sratom/COPYING b/modules/juce_audio_processors/format_types/lv2/sratom/COPYING new file mode 100644 index 0000000000..4d998ccc33 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/sratom/COPYING @@ -0,0 +1,13 @@ +Copyright 2012-2021 David Robillard + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/modules/juce_audio_processors/format_types/lv2/sratom/sratom/sratom.h b/modules/juce_audio_processors/format_types/lv2/sratom/sratom/sratom.h new file mode 100644 index 0000000000..26966ee3d1 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/sratom/sratom/sratom.h @@ -0,0 +1,220 @@ +/* + Copyright 2012-2021 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +/** + @file sratom.h API for Sratom, an LV2 Atom RDF serialisation library. +*/ + +#ifndef SRATOM_SRATOM_H +#define SRATOM_SRATOM_H + +#include "lv2/atom/atom.h" +#include "lv2/atom/forge.h" +#include "lv2/urid/urid.h" +#include "serd/serd.h" +#include "sord/sord.h" + +#include +#include + +#if defined(_WIN32) && !defined(SRATOM_STATIC) && defined(SRATOM_INTERNAL) +# define SRATOM_API __declspec(dllexport) +#elif defined(_WIN32) && !defined(SRATOM_STATIC) +# define SRATOM_API __declspec(dllimport) +#elif defined(__GNUC__) +# define SRATOM_API __attribute__((visibility("default"))) +#else +# define SRATOM_API +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +/** + @defgroup sratom Sratom + + A library for serialising LV2 Atoms. + + @{ +*/ + +/** + Atom serialiser. +*/ +typedef struct SratomImpl Sratom; + +/** + Mode for reading resources to LV2 Objects. + + This affects how resources (which are either blank nodes or have URIs) are + read by sratom_read(), since they may be read as simple references (a URI or + blank node ID) or a complete description (an atom "Object"). + + Currently, blank nodes are always read as Objects, but support for reading + blank node IDs may be added in the future. +*/ +typedef enum { + /** + Read blank nodes as Objects, and named resources as URIs. + */ + SRATOM_OBJECT_MODE_BLANK, + + /** + Read blank nodes and the main subject as Objects, and any other named + resources as URIs. The "main subject" is the subject parameter passed + to sratom_read(); if this is a resource it will be read as an Object, + but all other named resources encountered will be read as URIs. + */ + SRATOM_OBJECT_MODE_BLANK_SUBJECT +} SratomObjectMode; + +/** + Create a new Atom serialiser. +*/ +SRATOM_API +Sratom* +sratom_new(LV2_URID_Map* map); + +/** + Free an Atom serialisation. +*/ +SRATOM_API +void +sratom_free(Sratom* sratom); + +/** + Set the environment for reading or writing Turtle. + + This can be used to set namespace prefixes and a base URI for + sratom_to_turtle() and sratom_from_turtle(). +*/ +SRATOM_API +void +sratom_set_env(Sratom* sratom, SerdEnv* env); + +/** + Set the sink(s) where sratom will write its output. + + This must be called before calling sratom_write(). +*/ +SRATOM_API +void +sratom_set_sink(Sratom* sratom, + const char* base_uri, + SerdStatementSink sink, + SerdEndSink end_sink, + void* handle); + +/** + Write pretty numeric literals. + + If `pretty_numbers` is true, numbers will be written as pretty Turtle + literals, rather than string literals with precise types. The cost of this + is that the types might get fudged on a round-trip to RDF and back. +*/ +SRATOM_API +void +sratom_set_pretty_numbers(Sratom* sratom, bool pretty_numbers); + +/** + Configure how resources will be read to form LV2 Objects. +*/ +SRATOM_API +void +sratom_set_object_mode(Sratom* sratom, SratomObjectMode object_mode); + +/** + Write an Atom to RDF. + The serialised atom is written to the sink set by sratom_set_sink(). + @return 0 on success, or a non-zero error code otherwise. +*/ +SRATOM_API +int +sratom_write(Sratom* sratom, + LV2_URID_Unmap* unmap, + uint32_t flags, + const SerdNode* subject, + const SerdNode* predicate, + uint32_t type_urid, + uint32_t size, + const void* body); + +/** + Read an Atom from RDF. + The resulting atom will be written to `forge`. +*/ +SRATOM_API +void +sratom_read(Sratom* sratom, + LV2_Atom_Forge* forge, + SordWorld* world, + SordModel* model, + const SordNode* node); + +/** + Serialise an Atom to a Turtle string. + The returned string must be free()'d by the caller. +*/ +SRATOM_API +char* +sratom_to_turtle(Sratom* sratom, + LV2_URID_Unmap* unmap, + const char* base_uri, + const SerdNode* subject, + const SerdNode* predicate, + uint32_t type, + uint32_t size, + const void* body); + +/** + Read an Atom from a Turtle string. + The returned atom must be free()'d by the caller. +*/ +SRATOM_API +LV2_Atom* +sratom_from_turtle(Sratom* sratom, + const char* base_uri, + const SerdNode* subject, + const SerdNode* predicate, + const char* str); + +/** + A convenient resizing sink for LV2_Atom_Forge. + The handle must point to an initialized SerdChunk. +*/ +SRATOM_API +LV2_Atom_Forge_Ref +sratom_forge_sink(LV2_Atom_Forge_Sink_Handle handle, + const void* buf, + uint32_t size); + +/** + The corresponding deref function for sratom_forge_sink. +*/ +SRATOM_API +LV2_Atom* +sratom_forge_deref(LV2_Atom_Forge_Sink_Handle handle, LV2_Atom_Forge_Ref ref); + +/** + @} +*/ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* SRATOM_SRATOM_H */ diff --git a/modules/juce_audio_processors/format_types/lv2/sratom/src/sratom.c b/modules/juce_audio_processors/format_types/lv2/sratom/src/sratom.c new file mode 100644 index 0000000000..df74338469 --- /dev/null +++ b/modules/juce_audio_processors/format_types/lv2/sratom/src/sratom.c @@ -0,0 +1,919 @@ +/* + Copyright 2012-2021 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "sratom/sratom.h" + +#include "lv2/atom/atom.h" +#include "lv2/atom/forge.h" +#include "lv2/atom/util.h" +#include "lv2/midi/midi.h" +#include "lv2/urid/urid.h" + +#include +#include +#include +#include +#include + +#define NS_RDF (const uint8_t*)"http://www.w3.org/1999/02/22-rdf-syntax-ns#" +#define NS_XSD (const uint8_t*)"http://www.w3.org/2001/XMLSchema#" + +#define USTR(str) ((const uint8_t*)(str)) + +static const SerdStyle style = + (SerdStyle)(SERD_STYLE_ABBREVIATED | SERD_STYLE_RESOLVED | SERD_STYLE_CURIED); + +typedef enum { MODE_SUBJECT, MODE_BODY, MODE_SEQUENCE } ReadMode; + +struct SratomImpl { + LV2_URID_Map* map; + LV2_Atom_Forge forge; + SerdEnv* env; + SerdNode base_uri; + SerdURI base; + SerdStatementSink write_statement; + SerdEndSink end_anon; + void* handle; + LV2_URID atom_Event; + LV2_URID atom_frameTime; + LV2_URID atom_beatTime; + LV2_URID midi_MidiEvent; + unsigned next_id; + SratomObjectMode object_mode; + uint32_t seq_unit; + struct { + SordNode* atom_childType; + SordNode* atom_frameTime; + SordNode* atom_beatTime; + SordNode* rdf_first; + SordNode* rdf_rest; + SordNode* rdf_type; + SordNode* rdf_value; + SordNode* xsd_base64Binary; + } nodes; + + bool pretty_numbers; +}; + +static void +read_node(Sratom* sratom, + LV2_Atom_Forge* forge, + SordWorld* world, + SordModel* model, + const SordNode* node, + ReadMode mode); + +Sratom* +sratom_new(LV2_URID_Map* map) +{ + Sratom* sratom = (Sratom*)calloc(1, sizeof(Sratom)); + if (sratom) { + sratom->map = map; + sratom->atom_Event = map->map(map->handle, LV2_ATOM__Event); + sratom->atom_frameTime = map->map(map->handle, LV2_ATOM__frameTime); + sratom->atom_beatTime = map->map(map->handle, LV2_ATOM__beatTime); + sratom->midi_MidiEvent = map->map(map->handle, LV2_MIDI__MidiEvent); + sratom->object_mode = SRATOM_OBJECT_MODE_BLANK; + lv2_atom_forge_init(&sratom->forge, map); + } + return sratom; +} + +void +sratom_free(Sratom* sratom) +{ + if (sratom) { + serd_node_free(&sratom->base_uri); + free(sratom); + } +} + +void +sratom_set_env(Sratom* sratom, SerdEnv* env) +{ + sratom->env = env; +} + +void +sratom_set_sink(Sratom* sratom, + const char* base_uri, + SerdStatementSink sink, + SerdEndSink end_sink, + void* handle) +{ + if (base_uri) { + serd_node_free(&sratom->base_uri); + sratom->base_uri = + serd_node_new_uri_from_string(USTR(base_uri), NULL, NULL); + serd_uri_parse(sratom->base_uri.buf, &sratom->base); + } + sratom->write_statement = sink; + sratom->end_anon = end_sink; + sratom->handle = handle; +} + +void +sratom_set_pretty_numbers(Sratom* sratom, bool pretty_numbers) +{ + sratom->pretty_numbers = pretty_numbers; +} + +void +sratom_set_object_mode(Sratom* sratom, SratomObjectMode object_mode) +{ + sratom->object_mode = object_mode; +} + +static void +gensym(SerdNode* out, char c, unsigned num) +{ + out->n_bytes = out->n_chars = snprintf((char*)out->buf, 10, "%c%u", c, num); +} + +static void +list_append(Sratom* sratom, + LV2_URID_Unmap* unmap, + unsigned* flags, + SerdNode* s, + SerdNode* p, + SerdNode* node, + uint32_t size, + uint32_t type, + const void* body) +{ + // Generate a list node + gensym(node, 'l', sratom->next_id); + sratom->write_statement(sratom->handle, *flags, NULL, s, p, node, NULL, NULL); + + // _:node rdf:first value + *flags = SERD_LIST_CONT; + *p = serd_node_from_string(SERD_URI, NS_RDF "first"); + sratom_write(sratom, unmap, *flags, node, p, type, size, body); + + // Set subject to node and predicate to rdf:rest for next time + gensym(node, 'l', ++sratom->next_id); + *s = *node; + *p = serd_node_from_string(SERD_URI, NS_RDF "rest"); +} + +static void +list_end(SerdStatementSink sink, + void* handle, + const unsigned flags, + SerdNode* s, + SerdNode* p) +{ + // _:node rdf:rest rdf:nil + const SerdNode nil = serd_node_from_string(SERD_URI, NS_RDF "nil"); + sink(handle, flags, NULL, s, p, &nil, NULL, NULL); +} + +static void +start_object(Sratom* sratom, + uint32_t* flags, + const SerdNode* subject, + const SerdNode* predicate, + const SerdNode* node, + const char* type) +{ + if (subject && predicate) { + sratom->write_statement(sratom->handle, + *flags | SERD_ANON_O_BEGIN, + NULL, + subject, + predicate, + node, + NULL, + NULL); + // Start abbreviating object properties + *flags |= SERD_ANON_CONT; + + // Object is in a list, stop list abbreviating if necessary + *flags &= ~SERD_LIST_CONT; + } + + if (type) { + SerdNode p = serd_node_from_string(SERD_URI, NS_RDF "type"); + SerdNode o = serd_node_from_string(SERD_URI, USTR(type)); + sratom->write_statement( + sratom->handle, *flags, NULL, node, &p, &o, NULL, NULL); + } +} + +static bool +path_is_absolute(const char* path) +{ + return (path[0] == '/' || (isalpha(path[0]) && path[1] == ':' && + (path[2] == '/' || path[2] == '\\'))); +} + +static SerdNode +number_type(const Sratom* sratom, const uint8_t* type) +{ + if (sratom->pretty_numbers && + (!strcmp((const char*)type, (const char*)NS_XSD "int") || + !strcmp((const char*)type, (const char*)NS_XSD "long"))) { + return serd_node_from_string(SERD_URI, NS_XSD "integer"); + } + + if (sratom->pretty_numbers && + (!strcmp((const char*)type, (const char*)NS_XSD "float") || + !strcmp((const char*)type, (const char*)NS_XSD "double"))) { + return serd_node_from_string(SERD_URI, NS_XSD "decimal"); + } + + return serd_node_from_string(SERD_URI, type); +} + +int +sratom_write(Sratom* sratom, + LV2_URID_Unmap* unmap, + uint32_t flags, + const SerdNode* subject, + const SerdNode* predicate, + uint32_t type_urid, + uint32_t size, + const void* body) +{ + const char* const type = unmap->unmap(unmap->handle, type_urid); + uint8_t idbuf[12] = "b0000000000"; + SerdNode id = serd_node_from_string(SERD_BLANK, idbuf); + uint8_t nodebuf[12] = "b0000000000"; + SerdNode node = serd_node_from_string(SERD_BLANK, nodebuf); + SerdNode object = SERD_NODE_NULL; + SerdNode datatype = SERD_NODE_NULL; + SerdNode language = SERD_NODE_NULL; + bool new_node = false; + if (type_urid == 0 && size == 0) { + object = serd_node_from_string(SERD_URI, USTR(NS_RDF "nil")); + } else if (type_urid == sratom->forge.String) { + object = serd_node_from_string(SERD_LITERAL, (const uint8_t*)body); + } else if (type_urid == sratom->forge.Chunk) { + datatype = serd_node_from_string(SERD_URI, NS_XSD "base64Binary"); + object = serd_node_new_blob(body, size, true); + new_node = true; + } else if (type_urid == sratom->forge.Literal) { + const LV2_Atom_Literal_Body* lit = (const LV2_Atom_Literal_Body*)body; + const uint8_t* str = USTR(lit + 1); + + object = serd_node_from_string(SERD_LITERAL, str); + if (lit->datatype) { + datatype = serd_node_from_string( + SERD_URI, USTR(unmap->unmap(unmap->handle, lit->datatype))); + } else if (lit->lang) { + const char* lang = unmap->unmap(unmap->handle, lit->lang); + const char* prefix = "http://lexvo.org/id/iso639-3/"; + const size_t prefix_len = strlen(prefix); + if (lang && !strncmp(lang, prefix, prefix_len)) { + language = serd_node_from_string(SERD_LITERAL, USTR(lang + prefix_len)); + } else { + fprintf(stderr, "Unknown language URID %u\n", lit->lang); + } + } + } else if (type_urid == sratom->forge.URID) { + const uint32_t urid = *(const uint32_t*)body; + const uint8_t* str = USTR(unmap->unmap(unmap->handle, urid)); + + object = serd_node_from_string(SERD_URI, str); + } else if (type_urid == sratom->forge.Path) { + const uint8_t* str = USTR(body); + if (path_is_absolute((const char*)str)) { + new_node = true; + object = serd_node_new_file_uri(str, NULL, NULL, true); + } else { + if (!sratom->base_uri.buf || + strncmp((const char*)sratom->base_uri.buf, "file://", 7)) { + fprintf(stderr, "warning: Relative path but base is not a file URI.\n"); + fprintf(stderr, "warning: Writing ambiguous atom:Path literal.\n"); + object = serd_node_from_string(SERD_LITERAL, str); + datatype = serd_node_from_string(SERD_URI, USTR(LV2_ATOM__Path)); + } else { + new_node = true; + SerdNode rel = serd_node_new_file_uri(str, NULL, NULL, true); + object = serd_node_new_uri_from_node(&rel, &sratom->base, NULL); + serd_node_free(&rel); + } + } + } else if (type_urid == sratom->forge.URI) { + object = serd_node_from_string(SERD_URI, USTR(body)); + } else if (type_urid == sratom->forge.Int) { + new_node = true; + object = serd_node_new_integer(*(const int32_t*)body); + datatype = number_type(sratom, NS_XSD "int"); + } else if (type_urid == sratom->forge.Long) { + new_node = true; + object = serd_node_new_integer(*(const int64_t*)body); + datatype = number_type(sratom, NS_XSD "long"); + } else if (type_urid == sratom->forge.Float) { + new_node = true; + object = serd_node_new_decimal(*(const float*)body, 8); + datatype = number_type(sratom, NS_XSD "float"); + } else if (type_urid == sratom->forge.Double) { + new_node = true; + object = serd_node_new_decimal(*(const double*)body, 16); + datatype = number_type(sratom, NS_XSD "double"); + } else if (type_urid == sratom->forge.Bool) { + const int32_t val = *(const int32_t*)body; + + datatype = serd_node_from_string(SERD_URI, NS_XSD "boolean"); + object = serd_node_from_string(SERD_LITERAL, USTR(val ? "true" : "false")); + } else if (type_urid == sratom->midi_MidiEvent) { + new_node = true; + datatype = serd_node_from_string(SERD_URI, USTR(LV2_MIDI__MidiEvent)); + + uint8_t* str = (uint8_t*)calloc(size * 2 + 1, 1); + for (uint32_t i = 0; i < size; ++i) { + snprintf((char*)str + (2 * i), + size * 2 + 1, + "%02X", + (unsigned)*((const uint8_t*)body + i)); + } + object = serd_node_from_string(SERD_LITERAL, USTR(str)); + } else if (type_urid == sratom->atom_Event) { + const LV2_Atom_Event* ev = (const LV2_Atom_Event*)body; + gensym(&id, 'e', sratom->next_id++); + start_object(sratom, &flags, subject, predicate, &id, NULL); + SerdNode time; + SerdNode p; + if (sratom->seq_unit == sratom->atom_beatTime) { + time = serd_node_new_decimal(ev->time.beats, 16); + p = serd_node_from_string(SERD_URI, USTR(LV2_ATOM__beatTime)); + datatype = number_type(sratom, NS_XSD "double"); + } else { + time = serd_node_new_integer(ev->time.frames); + p = serd_node_from_string(SERD_URI, USTR(LV2_ATOM__frameTime)); + datatype = number_type(sratom, NS_XSD "long"); + } + sratom->write_statement(sratom->handle, + SERD_ANON_CONT, + NULL, + &id, + &p, + &time, + &datatype, + &language); + serd_node_free(&time); + + p = serd_node_from_string(SERD_URI, NS_RDF "value"); + sratom_write(sratom, + unmap, + SERD_ANON_CONT, + &id, + &p, + ev->body.type, + ev->body.size, + LV2_ATOM_BODY(&ev->body)); + if (sratom->end_anon) { + sratom->end_anon(sratom->handle, &id); + } + } else if (type_urid == sratom->forge.Tuple) { + gensym(&id, 't', sratom->next_id++); + start_object(sratom, &flags, subject, predicate, &id, type); + SerdNode p = serd_node_from_string(SERD_URI, NS_RDF "value"); + flags |= SERD_LIST_O_BEGIN; + LV2_ATOM_TUPLE_BODY_FOREACH (body, size, i) { + list_append(sratom, + unmap, + &flags, + &id, + &p, + &node, + i->size, + i->type, + LV2_ATOM_BODY(i)); + } + list_end(sratom->write_statement, sratom->handle, flags, &id, &p); + if (sratom->end_anon) { + sratom->end_anon(sratom->handle, &id); + } + } else if (type_urid == sratom->forge.Vector) { + const LV2_Atom_Vector_Body* vec = (const LV2_Atom_Vector_Body*)body; + gensym(&id, 'v', sratom->next_id++); + start_object(sratom, &flags, subject, predicate, &id, type); + SerdNode p = + serd_node_from_string(SERD_URI, (const uint8_t*)LV2_ATOM__childType); + SerdNode child_type = serd_node_from_string( + SERD_URI, (const uint8_t*)unmap->unmap(unmap->handle, vec->child_type)); + sratom->write_statement( + sratom->handle, flags, NULL, &id, &p, &child_type, NULL, NULL); + p = serd_node_from_string(SERD_URI, NS_RDF "value"); + flags |= SERD_LIST_O_BEGIN; + for (const char* i = (const char*)(vec + 1); i < (const char*)vec + size; + i += vec->child_size) { + list_append(sratom, + unmap, + &flags, + &id, + &p, + &node, + vec->child_size, + vec->child_type, + i); + } + list_end(sratom->write_statement, sratom->handle, flags, &id, &p); + if (sratom->end_anon) { + sratom->end_anon(sratom->handle, &id); + } + } else if (lv2_atom_forge_is_object_type(&sratom->forge, type_urid)) { + const LV2_Atom_Object_Body* obj = (const LV2_Atom_Object_Body*)body; + const char* otype = unmap->unmap(unmap->handle, obj->otype); + + if (lv2_atom_forge_is_blank(&sratom->forge, type_urid, obj)) { + gensym(&id, 'b', sratom->next_id++); + start_object(sratom, &flags, subject, predicate, &id, otype); + } else { + id = serd_node_from_string( + SERD_URI, (const uint8_t*)unmap->unmap(unmap->handle, obj->id)); + flags = 0; + start_object(sratom, &flags, NULL, NULL, &id, otype); + } + LV2_ATOM_OBJECT_BODY_FOREACH (obj, size, prop) { + const char* const key = unmap->unmap(unmap->handle, prop->key); + SerdNode pred = serd_node_from_string(SERD_URI, USTR(key)); + sratom_write(sratom, + unmap, + flags, + &id, + &pred, + prop->value.type, + prop->value.size, + LV2_ATOM_BODY(&prop->value)); + } + if (sratom->end_anon && (flags & SERD_ANON_CONT)) { + sratom->end_anon(sratom->handle, &id); + } + } else if (type_urid == sratom->forge.Sequence) { + const LV2_Atom_Sequence_Body* seq = (const LV2_Atom_Sequence_Body*)body; + gensym(&id, 'v', sratom->next_id++); + start_object(sratom, &flags, subject, predicate, &id, type); + SerdNode p = serd_node_from_string(SERD_URI, NS_RDF "value"); + flags |= SERD_LIST_O_BEGIN; + LV2_ATOM_SEQUENCE_BODY_FOREACH (seq, size, ev) { + sratom->seq_unit = seq->unit; + list_append(sratom, + unmap, + &flags, + &id, + &p, + &node, + sizeof(LV2_Atom_Event) + ev->body.size, + sratom->atom_Event, + ev); + } + list_end(sratom->write_statement, sratom->handle, flags, &id, &p); + if (sratom->end_anon && subject && predicate) { + sratom->end_anon(sratom->handle, &id); + } + } else { + gensym(&id, 'b', sratom->next_id++); + start_object(sratom, &flags, subject, predicate, &id, type); + SerdNode p = serd_node_from_string(SERD_URI, NS_RDF "value"); + SerdNode o = serd_node_new_blob(body, size, true); + datatype = serd_node_from_string(SERD_URI, NS_XSD "base64Binary"); + sratom->write_statement( + sratom->handle, flags, NULL, &id, &p, &o, &datatype, NULL); + if (sratom->end_anon && subject && predicate) { + sratom->end_anon(sratom->handle, &id); + } + serd_node_free(&o); + } + + if (object.buf) { + SerdNode def_s = serd_node_from_string(SERD_BLANK, USTR("atom")); + SerdNode def_p = serd_node_from_string(SERD_URI, USTR(NS_RDF "value")); + + if (!subject) { + subject = &def_s; + } + + if (!predicate) { + predicate = &def_p; + } + + sratom->write_statement(sratom->handle, + flags, + NULL, + subject, + predicate, + &object, + &datatype, + &language); + } + + if (new_node) { + serd_node_free(&object); + } + + return 0; +} + +char* +sratom_to_turtle(Sratom* sratom, + LV2_URID_Unmap* unmap, + const char* base_uri, + const SerdNode* subject, + const SerdNode* predicate, + uint32_t type, + uint32_t size, + const void* body) +{ + SerdURI buri = SERD_URI_NULL; + SerdNode base = + serd_node_new_uri_from_string(USTR(base_uri), &sratom->base, &buri); + SerdEnv* env = sratom->env ? sratom->env : serd_env_new(NULL); + SerdChunk str = {NULL, 0}; + SerdWriter* writer = + serd_writer_new(SERD_TURTLE, style, env, &buri, serd_chunk_sink, &str); + + serd_env_set_base_uri(env, &base); + sratom_set_sink(sratom, + base_uri, + (SerdStatementSink)serd_writer_write_statement, + (SerdEndSink)serd_writer_end_anon, + writer); + sratom_write( + sratom, unmap, SERD_EMPTY_S, subject, predicate, type, size, body); + serd_writer_finish(writer); + + serd_writer_free(writer); + if (!sratom->env) { + serd_env_free(env); + } + + serd_node_free(&base); + return (char*)serd_chunk_sink_finish(&str); +} + +static void +read_list_value(Sratom* sratom, + LV2_Atom_Forge* forge, + SordWorld* world, + SordModel* model, + const SordNode* node, + ReadMode mode) +{ + SordNode* fst = sord_get(model, node, sratom->nodes.rdf_first, NULL, NULL); + SordNode* rst = sord_get(model, node, sratom->nodes.rdf_rest, NULL, NULL); + if (fst && rst) { + read_node(sratom, forge, world, model, fst, mode); + read_list_value(sratom, forge, world, model, rst, mode); + } + + sord_node_free(world, rst); + sord_node_free(world, fst); +} + +static void +read_resource(Sratom* sratom, + LV2_Atom_Forge* forge, + SordWorld* world, + SordModel* model, + const SordNode* node, + LV2_URID otype) +{ + LV2_URID_Map* map = sratom->map; + SordQuad q = {node, NULL, NULL, NULL}; + SordIter* i = sord_find(model, q); + SordQuad match; + for (; !sord_iter_end(i); sord_iter_next(i)) { + sord_iter_get(i, match); + const SordNode* p = match[SORD_PREDICATE]; + const SordNode* o = match[SORD_OBJECT]; + const char* p_uri = (const char*)sord_node_get_string(p); + uint32_t p_urid = map->map(map->handle, p_uri); + if (!(sord_node_equals(p, sratom->nodes.rdf_type) && + sord_node_get_type(o) == SORD_URI && + map->map(map->handle, (const char*)sord_node_get_string(o)) == + otype)) { + lv2_atom_forge_key(forge, p_urid); + read_node(sratom, forge, world, model, o, MODE_BODY); + } + } + sord_iter_free(i); +} + +static uint32_t +atom_size(Sratom* sratom, uint32_t type_urid) +{ + if (type_urid == sratom->forge.Int || type_urid == sratom->forge.Bool) { + return sizeof(int32_t); + } + + if (type_urid == sratom->forge.Long) { + return sizeof(int64_t); + } + + if (type_urid == sratom->forge.Float) { + return sizeof(float); + } + + if (type_urid == sratom->forge.Double) { + return sizeof(double); + } + + if (type_urid == sratom->forge.URID) { + return sizeof(uint32_t); + } + + return 0; +} + +static void +read_literal(Sratom* sratom, LV2_Atom_Forge* forge, const SordNode* node) +{ + assert(sord_node_get_type(node) == SORD_LITERAL); + + size_t len = 0; + const char* str = (const char*)sord_node_get_string_counted(node, &len); + SordNode* datatype = sord_node_get_datatype(node); + const char* language = sord_node_get_language(node); + if (datatype) { + const char* type_uri = (const char*)sord_node_get_string(datatype); + if (!strcmp(type_uri, (const char*)NS_XSD "int") || + !strcmp(type_uri, (const char*)NS_XSD "integer")) { + lv2_atom_forge_int(forge, strtol(str, NULL, 10)); + } else if (!strcmp(type_uri, (const char*)NS_XSD "long")) { + lv2_atom_forge_long(forge, strtol(str, NULL, 10)); + } else if (!strcmp(type_uri, (const char*)NS_XSD "float") || + !strcmp(type_uri, (const char*)NS_XSD "decimal")) { + lv2_atom_forge_float(forge, (float)serd_strtod(str, NULL)); + } else if (!strcmp(type_uri, (const char*)NS_XSD "double")) { + lv2_atom_forge_double(forge, serd_strtod(str, NULL)); + } else if (!strcmp(type_uri, (const char*)NS_XSD "boolean")) { + lv2_atom_forge_bool(forge, !strcmp(str, "true")); + } else if (!strcmp(type_uri, (const char*)NS_XSD "base64Binary")) { + size_t size = 0; + void* body = serd_base64_decode(USTR(str), len, &size); + lv2_atom_forge_atom(forge, size, forge->Chunk); + lv2_atom_forge_write(forge, body, size); + free(body); + } else if (!strcmp(type_uri, LV2_ATOM__Path)) { + lv2_atom_forge_path(forge, str, len); + } else if (!strcmp(type_uri, LV2_MIDI__MidiEvent)) { + lv2_atom_forge_atom(forge, len / 2, sratom->midi_MidiEvent); + for (const char* s = str; s < str + len; s += 2) { + unsigned num = 0u; + sscanf(s, "%2X", &num); + const uint8_t c = num; + lv2_atom_forge_raw(forge, &c, 1); + } + lv2_atom_forge_pad(forge, len / 2); + } else { + lv2_atom_forge_literal( + forge, str, len, sratom->map->map(sratom->map->handle, type_uri), 0); + } + } else if (language) { + const char* prefix = "http://lexvo.org/id/iso639-3/"; + const size_t lang_len = strlen(prefix) + strlen(language); + char* lang_uri = (char*)calloc(lang_len + 1, 1); + snprintf(lang_uri, lang_len + 1, "%s%s", prefix, language); + lv2_atom_forge_literal( + forge, str, len, 0, sratom->map->map(sratom->map->handle, lang_uri)); + free(lang_uri); + } else { + lv2_atom_forge_string(forge, str, len); + } +} + +static void +read_object(Sratom* sratom, + LV2_Atom_Forge* forge, + SordWorld* world, + SordModel* model, + const SordNode* node, + ReadMode mode) +{ + LV2_URID_Map* map = sratom->map; + size_t len = 0; + const char* str = (const char*)sord_node_get_string_counted(node, &len); + + SordNode* type = sord_get(model, node, sratom->nodes.rdf_type, NULL, NULL); + SordNode* value = sord_get(model, node, sratom->nodes.rdf_value, NULL, NULL); + + const uint8_t* type_uri = NULL; + uint32_t type_urid = 0; + if (type) { + type_uri = sord_node_get_string(type); + type_urid = map->map(map->handle, (const char*)type_uri); + } + + LV2_Atom_Forge_Frame frame = {0, 0}; + if (mode == MODE_SEQUENCE) { + SordNode* time = + sord_get(model, node, sratom->nodes.atom_beatTime, NULL, NULL); + uint32_t seq_unit = 0u; + if (time) { + const char* time_str = (const char*)sord_node_get_string(time); + lv2_atom_forge_beat_time(forge, serd_strtod(time_str, NULL)); + seq_unit = sratom->atom_beatTime; + } else { + time = sord_get(model, node, sratom->nodes.atom_frameTime, NULL, NULL); + const char* time_str = + time ? (const char*)sord_node_get_string(time) : ""; + lv2_atom_forge_frame_time(forge, serd_strtod(time_str, NULL)); + seq_unit = sratom->atom_frameTime; + } + read_node(sratom, forge, world, model, value, MODE_BODY); + sord_node_free(world, time); + sratom->seq_unit = seq_unit; + } else if (type_urid == sratom->forge.Tuple) { + lv2_atom_forge_tuple(forge, &frame); + read_list_value(sratom, forge, world, model, value, MODE_BODY); + } else if (type_urid == sratom->forge.Sequence) { + const LV2_Atom_Forge_Ref ref = + lv2_atom_forge_sequence_head(forge, &frame, 0); + sratom->seq_unit = 0; + read_list_value(sratom, forge, world, model, value, MODE_SEQUENCE); + + LV2_Atom_Sequence* seq = + (LV2_Atom_Sequence*)lv2_atom_forge_deref(forge, ref); + seq->body.unit = + (sratom->seq_unit == sratom->atom_frameTime) ? 0 : sratom->seq_unit; + } else if (type_urid == sratom->forge.Vector) { + SordNode* child_type_node = + sord_get(model, node, sratom->nodes.atom_childType, NULL, NULL); + uint32_t child_type = + map->map(map->handle, (const char*)sord_node_get_string(child_type_node)); + uint32_t child_size = atom_size(sratom, child_type); + if (child_size > 0) { + LV2_Atom_Forge_Ref ref = + lv2_atom_forge_vector_head(forge, &frame, child_size, child_type); + read_list_value(sratom, forge, world, model, value, MODE_BODY); + lv2_atom_forge_pop(forge, &frame); + frame.ref = 0; + lv2_atom_forge_pad(forge, lv2_atom_forge_deref(forge, ref)->size); + } + sord_node_free(world, child_type_node); + } else if (value && sord_node_equals(sord_node_get_datatype(value), + sratom->nodes.xsd_base64Binary)) { + size_t vlen = 0; + const uint8_t* vstr = sord_node_get_string_counted(value, &vlen); + size_t size = 0; + void* body = serd_base64_decode(vstr, vlen, &size); + lv2_atom_forge_atom(forge, size, type_urid); + lv2_atom_forge_write(forge, body, size); + free(body); + } else if (sord_node_get_type(node) == SORD_URI) { + lv2_atom_forge_object(forge, &frame, map->map(map->handle, str), type_urid); + read_resource(sratom, forge, world, model, node, type_urid); + } else { + lv2_atom_forge_object(forge, &frame, 0, type_urid); + read_resource(sratom, forge, world, model, node, type_urid); + } + + if (frame.ref) { + lv2_atom_forge_pop(forge, &frame); + } + sord_node_free(world, value); + sord_node_free(world, type); +} + +static void +read_node(Sratom* sratom, + LV2_Atom_Forge* forge, + SordWorld* world, + SordModel* model, + const SordNode* node, + ReadMode mode) +{ + LV2_URID_Map* map = sratom->map; + size_t len = 0; + const char* str = (const char*)sord_node_get_string_counted(node, &len); + if (sord_node_get_type(node) == SORD_LITERAL) { + read_literal(sratom, forge, node); + } else if (sord_node_get_type(node) == SORD_URI && + !(sratom->object_mode == SRATOM_OBJECT_MODE_BLANK_SUBJECT && + mode == MODE_SUBJECT)) { + if (!strcmp(str, (const char*)NS_RDF "nil")) { + lv2_atom_forge_atom(forge, 0, 0); + } else if (!strncmp(str, "file://", 7)) { + SerdURI uri; + serd_uri_parse((const uint8_t*)str, &uri); + + SerdNode rel = + serd_node_new_relative_uri(&uri, &sratom->base, NULL, NULL); + uint8_t* path = serd_file_uri_parse(rel.buf, NULL); + if (path) { + lv2_atom_forge_path( + forge, (const char*)path, strlen((const char*)path)); + serd_free(path); + } else { + // FIXME: Report errors (required API change) + lv2_atom_forge_atom(forge, 0, 0); + } + serd_node_free(&rel); + } else { + lv2_atom_forge_urid(forge, map->map(map->handle, str)); + } + } else { + read_object(sratom, forge, world, model, node, mode); + } +} + +void +sratom_read(Sratom* sratom, + LV2_Atom_Forge* forge, + SordWorld* world, + SordModel* model, + const SordNode* node) +{ + sratom->nodes.atom_childType = sord_new_uri(world, USTR(LV2_ATOM__childType)); + sratom->nodes.atom_frameTime = sord_new_uri(world, USTR(LV2_ATOM__frameTime)); + sratom->nodes.atom_beatTime = sord_new_uri(world, USTR(LV2_ATOM__beatTime)); + sratom->nodes.rdf_first = sord_new_uri(world, NS_RDF "first"); + sratom->nodes.rdf_rest = sord_new_uri(world, NS_RDF "rest"); + sratom->nodes.rdf_type = sord_new_uri(world, NS_RDF "type"); + sratom->nodes.rdf_value = sord_new_uri(world, NS_RDF "value"); + sratom->nodes.xsd_base64Binary = sord_new_uri(world, NS_XSD "base64Binary"); + + sratom->next_id = 1; + read_node(sratom, forge, world, model, node, MODE_SUBJECT); + + sord_node_free(world, sratom->nodes.xsd_base64Binary); + sord_node_free(world, sratom->nodes.rdf_value); + sord_node_free(world, sratom->nodes.rdf_type); + sord_node_free(world, sratom->nodes.rdf_rest); + sord_node_free(world, sratom->nodes.rdf_first); + sord_node_free(world, sratom->nodes.atom_frameTime); + sord_node_free(world, sratom->nodes.atom_beatTime); + sord_node_free(world, sratom->nodes.atom_childType); + memset(&sratom->nodes, 0, sizeof(sratom->nodes)); +} + +LV2_Atom_Forge_Ref +sratom_forge_sink(LV2_Atom_Forge_Sink_Handle handle, + const void* buf, + uint32_t size) +{ + SerdChunk* chunk = (SerdChunk*)handle; + const LV2_Atom_Forge_Ref ref = chunk->len + 1; + serd_chunk_sink(buf, size, chunk); + return ref; +} + +LV2_Atom* +sratom_forge_deref(LV2_Atom_Forge_Sink_Handle handle, LV2_Atom_Forge_Ref ref) +{ + SerdChunk* chunk = (SerdChunk*)handle; + return (LV2_Atom*)(chunk->buf + ref - 1); +} + +LV2_Atom* +sratom_from_turtle(Sratom* sratom, + const char* base_uri, + const SerdNode* subject, + const SerdNode* predicate, + const char* str) +{ + SerdChunk out = {NULL, 0}; + SerdNode base = + serd_node_new_uri_from_string(USTR(base_uri), &sratom->base, NULL); + SordWorld* world = sord_world_new(); + SordModel* model = sord_new(world, SORD_SPO, false); + SerdEnv* env = sratom->env ? sratom->env : serd_env_new(&base); + SerdReader* reader = sord_new_reader(model, env, SERD_TURTLE, NULL); + + if (!serd_reader_read_string(reader, (const uint8_t*)str)) { + SordNode* s = sord_node_from_serd_node(world, env, subject, 0, 0); + lv2_atom_forge_set_sink( + &sratom->forge, sratom_forge_sink, sratom_forge_deref, &out); + if (subject && predicate) { + SordNode* p = sord_node_from_serd_node(world, env, predicate, 0, 0); + SordNode* o = sord_get(model, s, p, NULL, NULL); + if (o) { + sratom_read(sratom, &sratom->forge, world, model, o); + sord_node_free(world, o); + } else { + fprintf(stderr, "Failed to find node\n"); + } + } else { + sratom_read(sratom, &sratom->forge, world, model, s); + } + } else { + fprintf(stderr, "Failed to read Turtle\n"); + } + + serd_reader_free(reader); + if (!sratom->env) { + serd_env_free(env); + } + + sord_free(model); + sord_world_free(world); + serd_node_free(&base); + + return (LV2_Atom*)out.buf; +}