hide on cleanup
[lv2fil.git] / lv2filter.c
blobdcfe30d16eb224c10dd9a3223db3f714202a488f
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*****************************************************************************
4 * Copyright (C) 2006,2007,2008,2009 Nedko Arnaudov <nedko@arnaudov.name>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *****************************************************************************/
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <assert.h>
25 #include <math.h>
26 #include <stdbool.h>
27 #include <lv2.h>
29 #include "lv2filter.h"
30 #include "filter.h"
31 #define LOG_LEVEL LOG_LEVEL_ERROR
32 #include "log.h"
34 #define BANDS_COUNT 4
36 #define LV2_PORT_MONO_AUDIO_IN 0
37 #define LV2_PORT_MONO_AUDIO_OUT 1
38 #define LV2_MONO_AUDIO_PORT_COUNT 2
39 #define LV2_PORTS_COUNT_MONO (LV2_MONO_AUDIO_PORT_COUNT + GLOBAL_PARAMETERS_COUNT + BANDS_COUNT * BAND_PARAMETERS_COUNT)
41 #define LV2_PORT_LEFT_AUDIO_IN 0
42 #define LV2_PORT_RIGHT_AUDIO_IN 1
43 #define LV2_PORT_LEFT_AUDIO_OUT 2
44 #define LV2_PORT_RIGHT_AUDIO_OUT 3
45 #define LV2_STEREO_AUDIO_PORT_COUNT 4
46 #define LV2_PORTS_COUNT_STEREO (LV2_STEREO_AUDIO_PORT_COUNT + GLOBAL_PARAMETERS_COUNT + BANDS_COUNT * BAND_PARAMETERS_COUNT)
48 struct lv2filter
50 bool stereo;
51 filter_handle filter;
52 filter_handle filter_right;
53 char * bundle_path;
54 const float * audio_in;
55 const float * audio_in_right;
56 float * audio_out;
57 float * audio_out_right;
58 const LV2_Feature * const * host_features;
61 LV2_Handle
62 lv2filter_instantiate(
63 const LV2_Descriptor * descriptor,
64 double sample_rate,
65 const char * bundle_path,
66 const LV2_Feature * const * host_features)
68 struct lv2filter * lv2filter_ptr;
69 const LV2_Feature * const * feature_ptr_ptr;
71 LOG_DEBUG("lv2filter_create_plugin_instance() called.");
72 LOG_DEBUG("uri = \"%s\"", descriptor->URI);
73 LOG_DEBUG("sample_rate = %f", sample_rate);
74 LOG_DEBUG("bundle_path = \"%s\"", bundle_path);
76 feature_ptr_ptr = host_features;
77 while (*feature_ptr_ptr)
79 LOG_DEBUG("Host feature <%s> detected", (*feature_ptr_ptr)->URI);
81 feature_ptr_ptr++;
84 lv2filter_ptr = malloc(sizeof(struct lv2filter));
85 if (lv2filter_ptr == NULL)
87 goto fail;
90 if (strcmp(descriptor->URI, LV2FILTER_STEREO_URI) == 0)
92 lv2filter_ptr->stereo = true;
94 else if (strcmp(descriptor->URI, LV2FILTER_MONO_URI) == 0)
96 lv2filter_ptr->stereo = false;
98 else
100 assert(false);
101 goto fail_free_instance;
104 lv2filter_ptr->host_features = host_features;
106 lv2filter_ptr->bundle_path = strdup(bundle_path);
107 if (lv2filter_ptr->bundle_path == NULL)
109 goto fail_free_instance;
112 if (!filter_create(sample_rate, BANDS_COUNT, &lv2filter_ptr->filter))
114 goto fail_free_bundle_path;
117 if (lv2filter_ptr->stereo)
119 if (!filter_create(sample_rate, BANDS_COUNT, &lv2filter_ptr->filter_right))
121 goto fail_destroy_filter;
125 return (LV2_Handle)lv2filter_ptr;
127 fail_destroy_filter:
128 filter_destroy(lv2filter_ptr->filter);
130 fail_free_bundle_path:
131 free(lv2filter_ptr->bundle_path);
133 fail_free_instance:
134 free(lv2filter_ptr);
136 fail:
137 return NULL;
140 #define lv2filter_ptr ((struct lv2filter *)instance)
142 /* The run() callback. This is the function that gets called by the host
143 when it wants to run the plugin. The parameter is the number of sample
144 frames to process. */
145 void
146 lv2filter_run(
147 LV2_Handle instance,
148 uint32_t samples_count)
150 LOG_DEBUG("lv2filter_run");
151 filter_run(
152 lv2filter_ptr->filter,
153 lv2filter_ptr->audio_in,
154 lv2filter_ptr->audio_out,
155 samples_count);
157 if (lv2filter_ptr->stereo)
159 filter_run(
160 lv2filter_ptr->filter_right,
161 lv2filter_ptr->audio_in_right,
162 lv2filter_ptr->audio_out_right,
163 samples_count);
167 void
168 lv2filter_cleanup(
169 LV2_Handle instance)
171 filter_destroy(lv2filter_ptr->filter);
173 if (lv2filter_ptr->stereo)
175 filter_destroy(lv2filter_ptr->filter_right);
178 free(lv2filter_ptr->bundle_path);
179 free(lv2filter_ptr);
182 void
183 lv2filter_connect_port(
184 LV2_Handle instance,
185 uint32_t port,
186 void * data_location)
188 LOG_DEBUG("lv2filter_connect_port %u %p", (unsigned int)port, data_location);
190 if (lv2filter_ptr->stereo)
192 if (port >= LV2_PORTS_COUNT_STEREO)
194 assert(0);
195 return;
198 if (port == LV2_PORT_LEFT_AUDIO_IN)
200 lv2filter_ptr->audio_in = data_location;
202 else if (port == LV2_PORT_LEFT_AUDIO_OUT)
204 lv2filter_ptr->audio_out = data_location;
206 else if (port == LV2_PORT_RIGHT_AUDIO_IN)
208 lv2filter_ptr->audio_in_right = data_location;
210 else if (port == LV2_PORT_RIGHT_AUDIO_OUT)
212 lv2filter_ptr->audio_out_right = data_location;
214 else
216 assert(port >= LV2_STEREO_AUDIO_PORT_COUNT);
217 port -= LV2_STEREO_AUDIO_PORT_COUNT;
218 if (port < GLOBAL_PARAMETERS_COUNT)
220 filter_connect_global_parameter(lv2filter_ptr->filter, port, data_location);
221 filter_connect_global_parameter(lv2filter_ptr->filter_right, port, data_location);
223 else
225 assert(port >= GLOBAL_PARAMETERS_COUNT);
226 port -= GLOBAL_PARAMETERS_COUNT;
228 filter_connect_band_parameter(lv2filter_ptr->filter, port / BANDS_COUNT, port % BANDS_COUNT, data_location);
229 filter_connect_band_parameter(lv2filter_ptr->filter_right, port / BANDS_COUNT, port % BANDS_COUNT, data_location);
233 else
235 if (port >= LV2_PORTS_COUNT_MONO)
237 assert(0);
238 return;
241 if (port == LV2_PORT_MONO_AUDIO_IN)
243 lv2filter_ptr->audio_in = data_location;
245 else if (port == LV2_PORT_MONO_AUDIO_OUT)
247 lv2filter_ptr->audio_out = data_location;
249 else
251 port -= LV2_MONO_AUDIO_PORT_COUNT;
252 if (port < GLOBAL_PARAMETERS_COUNT)
254 filter_connect_global_parameter(lv2filter_ptr->filter, port, data_location);
256 else
258 port -= GLOBAL_PARAMETERS_COUNT;
260 filter_connect_band_parameter(lv2filter_ptr->filter, port / BANDS_COUNT, port % BANDS_COUNT, data_location);
266 const void *
267 lv2filter_extension_data(
268 const char * URI)
270 return NULL;