1 /*
2 Copyright 2006-2011 David Robillard <d@drobilla.net>
3 Copyright 2006 Steve Harris <steve@plugin.org.uk>
4
5 Permission to use, copy, modify, and/or distribute this software for any
6 purpose with or without fee is hereby granted, provided that the above
7 copyright notice and this permission notice appear in all copies.
8
9 THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18 /** Include standard C headers */
19 #include <math.h>
20 #include <stdlib.h>
21
22 /**
23 LV2 headers are based on the URI of the specification they come from, so a
24 consistent convention can be used even for unofficial extensions. The URI
25 of the core LV2 specification is <http://lv2plug.in/ns/lv2core>, by
26 replacing `http:/` with `lv2` any header in the specification bundle can be
27 included, in this case `lv2.h`.
28 */
29 #include "lv2/lv2plug.in/ns/lv2core/lv2.h"
30
31 /**
32 The URI is the identifier for a plugin, and how the host associates this
33 implementation in code with its description in data. In this plugin it is
34 only used once in the code, but defining the plugin URI at the top of the
35 file is a good convention to follow. If this URI does not match that used
36 in the data files, the host will fail to load the plugin.
37 */
38 #define TEST_URI "http://example.org/lilv-bindings-test-plugin"
39
40 /**
41 In code, ports are referred to by index. An enumeration of port indices
42 should be defined for readability.
43 */
44 typedef enum {
45 TEST_CONTROL_IN = 0,
46 TEST_CONTROL_OUT = 1,
47 TEST_AUDIO_IN = 2,
48 TEST_AUDIO_OUT = 3
49 } PortIndex;
50
51 /**
52 Every plugin defines a private structure for the plugin instance. All data
53 associated with a plugin instance is stored here, and is available to
54 every instance method. In this simple plugin, only port buffers need to be
55 stored, since there is no additional instance data. */
56 typedef struct {
57 float* buf;
58 } Test;
59
60 /**
61 The instantiate() function is called by the host to create a new plugin
62 instance. The host passes the plugin descriptor, sample rate, and bundle
63 path for plugins that need to load additional resources (e.g. waveforms).
64 The features parameter contains host-provided features defined in LV2
65 extensions, but this simple plugin does not use any.
66
67 This function is in the ``instantiation'' threading class, so no other
68 methods on this instance will be called concurrently with it.
69 */
70 static LV2_Handle
instantiate(const LV2_Descriptor * descriptor,double rate,const char * bundle_path,const LV2_Feature * const * features)71 instantiate(const LV2_Descriptor* descriptor,
72 double rate,
73 const char* bundle_path,
74 const LV2_Feature* const* features)
75 {
76 Test* test = (Test*)malloc(sizeof(Test));
77
78 return (LV2_Handle)test;
79 }
80
81 /**
82 The connect_port() method is called by the host to connect a particular port
83 to a buffer. The plugin must store the data location, but data may not be
84 accessed except in run().
85
86 This method is in the ``audio'' threading class, and is called in the same
87 context as run().
88 */
89 static void
connect_port(LV2_Handle instance,uint32_t port,void * data)90 connect_port(LV2_Handle instance,
91 uint32_t port,
92 void* data)
93 {
94 }
95
96 /**
97 The activate() method is called by the host to initialise and prepare the
98 plugin instance for running. The plugin must reset all internal state
99 except for buffer locations set by connect_port(). Since this plugin has
100 no other internal state, this method does nothing.
101
102 This method is in the ``instantiation'' threading class, so no other
103 methods on this instance will be called concurrently with it.
104 */
105 static void
activate(LV2_Handle instance)106 activate(LV2_Handle instance)
107 {
108 }
109
110 /** Process a block of audio (audio thread, must be RT safe). */
111 static void
run(LV2_Handle instance,uint32_t n_samples)112 run(LV2_Handle instance, uint32_t n_samples)
113 {
114 }
115
116 /**
117 The deactivate() method is the counterpart to activate() called by the host
118 after running the plugin. It indicates that the host will not call run()
119 again until another call to activate() and is mainly useful for more
120 advanced plugins with ``live'' characteristics such as those with auxiliary
121 processing threads. As with activate(), this plugin has no use for this
122 information so this method does nothing.
123
124 This method is in the ``instantiation'' threading class, so no other
125 methods on this instance will be called concurrently with it.
126 */
127 static void
deactivate(LV2_Handle instance)128 deactivate(LV2_Handle instance)
129 {
130 }
131
132 /**
133 Destroy a plugin instance (counterpart to instantiate()).
134
135 This method is in the ``instantiation'' threading class, so no other
136 methods on this instance will be called concurrently with it.
137 */
138 static void
cleanup(LV2_Handle instance)139 cleanup(LV2_Handle instance)
140 {
141 free(instance);
142 }
143
144 /**
145 The extension_data function returns any extension data supported by the
146 plugin. Note that this is not an instance method, but a function on the
147 plugin descriptor. It is usually used by plugins to implement additional
148 interfaces. This plugin does not have any extension data, so this function
149 returns NULL.
150
151 This method is in the ``discovery'' threading class, so no other functions
152 or methods in this plugin library will be called concurrently with it.
153 */
154 static const void*
extension_data(const char * uri)155 extension_data(const char* uri)
156 {
157 return NULL;
158 }
159
160 /**
161 Define the LV2_Descriptor for this plugin. It is best to define descriptors
162 statically to avoid leaking memory and non-portable shared library
163 constructors and destructors to clean up properly.
164 */
165 static const LV2_Descriptor descriptor = {
166 TEST_URI,
167 instantiate,
168 connect_port,
169 activate,
170 run,
171 deactivate,
172 cleanup,
173 extension_data
174 };
175
176 /**
177 The lv2_descriptor() function is the entry point to the plugin library. The
178 host will load the library and call this function repeatedly with increasing
179 indices to find all the plugins defined in the library. The index is not an
180 indentifier, the URI of the returned descriptor is used to determine the
181 identify of the plugin.
182
183 This method is in the ``discovery'' threading class, so no other functions
184 or methods in this plugin library will be called concurrently with it.
185 */
186 LV2_SYMBOL_EXPORT
187 const LV2_Descriptor*
lv2_descriptor(uint32_t index)188 lv2_descriptor(uint32_t index)
189 {
190 switch (index) {
191 case 0:
192 return &descriptor;
193 default:
194 return NULL;
195 }
196 }
197