Fix wine build
[carla.git] / source / native-plugins / midi-channel-filter.c
blobd5b9e18ddebe7d4805ff5cf31c4b55d22e18d87e
1 /*
2 * Carla Native Plugins
3 * Copyright (C) 2012-2019 Filipe Coelho <falktx@falktx.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or any later version.
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 * For a full copy of the GNU General Public License see the doc/GPL.txt file.
18 #include "CarlaNative.h"
19 #include "CarlaMIDI.h"
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
25 // -----------------------------------------------------------------------
27 typedef struct {
28 const NativeHostDescriptor* host;
29 bool channels[MAX_MIDI_CHANNELS];
30 } MidiGainHandle;
32 // -----------------------------------------------------------------------
34 static NativePluginHandle midichanfilter_instantiate(const NativeHostDescriptor* host)
36 MidiGainHandle* const handle = (MidiGainHandle*)malloc(sizeof(MidiGainHandle));
38 if (handle == NULL)
39 return NULL;
41 handle->host = host;
43 for (int i=MAX_MIDI_CHANNELS; --i>=0;)
44 handle->channels[i] = 1.0f;
46 return handle;
49 #define handlePtr ((MidiGainHandle*)handle)
51 static void midichanfilter_cleanup(NativePluginHandle handle)
53 free(handlePtr);
56 static uint32_t midichanfilter_get_parameter_count(NativePluginHandle handle)
58 return MAX_MIDI_CHANNELS;
60 // unused
61 (void)handle;
64 static const NativeParameter* midichanfilter_get_parameter_info(NativePluginHandle handle, uint32_t index)
66 if (index > MAX_MIDI_CHANNELS)
67 return NULL;
69 static NativeParameter param;
70 static const NativeParameterScalePoint scalePoints[2] = { { "Off", 0.0f }, { "On", 1.0f } };
71 static char paramName[24];
73 param.hints = NATIVE_PARAMETER_IS_ENABLED|NATIVE_PARAMETER_IS_AUTOMATABLE|NATIVE_PARAMETER_IS_BOOLEAN|NATIVE_PARAMETER_USES_SCALEPOINTS;
74 param.name = paramName;
75 param.unit = NULL;
76 param.ranges.def = 1.0f;
77 param.ranges.min = 0.0f;
78 param.ranges.max = 1.0f;
79 param.ranges.step = 1.0f;
80 param.ranges.stepSmall = 1.0f;
81 param.ranges.stepLarge = 1.0f;
82 param.scalePointCount = 2;
83 param.scalePoints = scalePoints;
85 snprintf(paramName, 24, "%u", index+1);
87 return &param;
89 // unused
90 (void)handle;
93 static float midichanfilter_get_parameter_value(NativePluginHandle handle, uint32_t index)
95 if (index > MAX_MIDI_CHANNELS)
96 return 0.0f;
98 return handlePtr->channels[index] ? 1.0f : 0.0f;
101 static void midichanfilter_set_parameter_value(NativePluginHandle handle, uint32_t index, float value)
103 if (index > MAX_MIDI_CHANNELS)
104 return;
106 handlePtr->channels[index] = (value >= 0.5f);
109 // FIXME for v3.0, use const for the input buffer
110 static void midichanfilter_process(NativePluginHandle handle,
111 float** inBuffer, float** outBuffer, uint32_t frames,
112 const NativeMidiEvent* midiEvents, uint32_t midiEventCount)
114 const NativeHostDescriptor* const host = handlePtr->host;
115 const bool* const channels = handlePtr->channels;
117 for (uint32_t i=0; i < midiEventCount; ++i)
119 const NativeMidiEvent* const midiEvent = &midiEvents[i];
121 const uint8_t status = (uint8_t)MIDI_GET_STATUS_FROM_DATA(midiEvent->data);
123 if (MIDI_IS_CHANNEL_MESSAGE(status))
125 const uint8_t channel = (uint8_t)MIDI_GET_CHANNEL_FROM_DATA(midiEvent->data);
127 if (channel >= MAX_MIDI_CHANNELS)
128 continue;
130 if (channels[channel])
131 host->write_midi_event(host->handle, midiEvent);
133 else
135 // pass through all non-message events
136 host->write_midi_event(host->handle, midiEvent);
140 return;
142 // unused
143 (void)inBuffer;
144 (void)outBuffer;
145 (void)frames;
148 // -----------------------------------------------------------------------
150 static const NativePluginDescriptor midichanfilterDesc = {
151 .category = NATIVE_PLUGIN_CATEGORY_UTILITY,
152 .hints = NATIVE_PLUGIN_IS_RTSAFE,
153 .supports = NATIVE_PLUGIN_SUPPORTS_EVERYTHING,
154 .audioIns = 0,
155 .audioOuts = 0,
156 .cvIns = 0,
157 .cvOuts = 0,
158 .midiIns = 1,
159 .midiOuts = 1,
160 .paramIns = 0,
161 .paramOuts = 0,
162 .name = "MIDI Channel Filter",
163 .label = "midichanfilter",
164 .maker = "falkTX",
165 .copyright = "GNU GPL v2+",
167 .instantiate = midichanfilter_instantiate,
168 .cleanup = midichanfilter_cleanup,
170 .get_parameter_count = midichanfilter_get_parameter_count,
171 .get_parameter_info = midichanfilter_get_parameter_info,
172 .get_parameter_value = midichanfilter_get_parameter_value,
174 .get_midi_program_count = NULL,
175 .get_midi_program_info = NULL,
177 .set_parameter_value = midichanfilter_set_parameter_value,
178 .set_midi_program = NULL,
179 .set_custom_data = NULL,
181 .ui_show = NULL,
182 .ui_idle = NULL,
184 .ui_set_parameter_value = NULL,
185 .ui_set_midi_program = NULL,
186 .ui_set_custom_data = NULL,
188 .activate = NULL,
189 .deactivate = NULL,
190 .process = midichanfilter_process,
192 .get_state = NULL,
193 .set_state = NULL,
195 .dispatcher = NULL
198 // -----------------------------------------------------------------------
200 void carla_register_native_plugin_midichanfilter(void);
202 void carla_register_native_plugin_midichanfilter(void)
204 carla_register_native_plugin(&midichanfilterDesc);
207 // -----------------------------------------------------------------------