# Plugins

After bundles are loaded, all discovered plugins can be accessed via `lilv_world_get_all_plugins()`:

    LilvPlugins* plugins = lilv_world_get_all_plugins(world);

`LilvPlugins` is a collection of plugins that can be accessed by index or plugin URI. The convenicne macro `LILV_FOREACH` is provided to make iterating over collections simple. For example, to print the URI of every plugin:

    LILV_FOREACH (plugins, i, list) {
      const LilvPlugin* p = lilv_plugins_get(list, i);
        printf("%s\n", lilv_node_as_uri(lilv_plugin_get_uri(p)));
      }
    }

More typically, you want to load a specific plugin, which can be done with `lilv_plugins_get_by_uri()`:

    LilvNode* plugin_uri = lilv_new_uri(world, "http://example.org/Osc");

    const LilvPlugin* plugin = lilv_plugins_get_by_uri(list, plugin_uri);

`LilvPlugin` has various accessors that can be used to get information about the plugin. See the API reference for details.

## Instances

`LilvPlugin` only represents the data of the plugin, it does not load or access the actual plugin code. To do that, you must instantiate a plugin to create `LilvInstance`:

    LilvInstance* instance = lilv_plugin_instantiate(plugin, 48000.0, NULL);

### Connecting Ports

Before running a plugin instance, its ports must be connected to some data. This is done with `lilv_instance_connect_port()`. Assuming the plugins has two control input ports and one audio output port, in that order:

    float control_in_1 = 0.0f;
    float control_in_2 = 0.0f;

    float audio_out[128];

    lilv_instance_connect_port(instance, 0, &control_in_1);
    lilv_instance_connect_port(instance, 1, &control_in_2);
    lilv_instance_connect_port(instance, 2, &audio_out);

### Processing Data

Once the ports are connected, the instance can be activated and run:

    lilv_instance_activate(instance);

    lilv_instance_run(instance, 128);
    // Copy buffers around and probably run several times here...

    lilv_instance_deactivate(instance);

Once you are done with an instance, it can be destroyed with `lilv_instance_free()`:

    lilv_instance_free(instance);