Add initial bits for Qt6 support
[carla.git] / source / native-plugins / midi-channel-ab.c
blobbcb5c6dcdeeacb0400c075a079fa1e2bb59630a5
1 /*
2 * Carla Native Plugins
3 * Copyright (C) 2012-2019 Filipe Coelho <falktx@falktx.com>
4 * Copyright (C) 2018 Milk Brewster <code@milkmiruku.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * For a full copy of the GNU General Public License see the doc/GPL.txt file.
19 #include "CarlaNative.h"
20 #include "CarlaMIDI.h"
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
26 // -----------------------------------------------------------------------
28 typedef struct {
29 const NativeHostDescriptor* host;
30 bool channels[MAX_MIDI_CHANNELS];
31 } MidiGainHandle;
33 // -----------------------------------------------------------------------
35 static NativePluginHandle midichanab_instantiate(const NativeHostDescriptor* host)
37 MidiGainHandle* const handle = (MidiGainHandle*)malloc(sizeof(MidiGainHandle));
39 if (handle == NULL)
40 return NULL;
42 handle->host = host;
44 memset(handle->channels, 0, MAX_MIDI_CHANNELS);
46 return handle;
49 #define handlePtr ((MidiGainHandle*)handle)
51 static void midichanab_cleanup(NativePluginHandle handle)
53 free(handlePtr);
56 static uint32_t midichanab_get_parameter_count(NativePluginHandle handle)
58 return MAX_MIDI_CHANNELS;
60 // unused
61 (void)handle;
64 static const NativeParameter* midichanab_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] = { { "Output A", 0 }, { "Output B", 1 } };
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 = 0;
77 param.ranges.min = 0;
78 param.ranges.max = 1;
79 param.ranges.step = 1;
80 param.ranges.stepSmall = 1;
81 param.ranges.stepLarge = 1;
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 midichanab_get_parameter_value(NativePluginHandle handle, uint32_t index)
95 if (index >= MAX_MIDI_CHANNELS)
96 return 0;
98 return handlePtr->channels[index] ? 1 : 0;
101 static void midichanab_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 midichanab_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;
116 NativeMidiEvent tmpEvent;
118 for (uint32_t i=0; i < midiEventCount; ++i)
120 const NativeMidiEvent* const midiEvent = &midiEvents[i];
122 const uint8_t status = (uint8_t)MIDI_GET_STATUS_FROM_DATA(midiEvent->data);
124 if (MIDI_IS_CHANNEL_MESSAGE(status))
126 const uint8_t channel = (uint8_t)MIDI_GET_CHANNEL_FROM_DATA(midiEvent->data);
128 if (channel >= MAX_MIDI_CHANNELS)
129 continue;
131 if (channels[channel])
133 memcpy(&tmpEvent, midiEvent, sizeof(NativeMidiEvent));
134 ++tmpEvent.port;
135 host->write_midi_event(host->handle, &tmpEvent);
137 else
139 host->write_midi_event(host->handle, midiEvent);
142 else
144 // pass through all non-message events
145 host->write_midi_event(host->handle, midiEvent);
149 return;
151 // unused
152 (void)inBuffer;
153 (void)outBuffer;
154 (void)frames;
157 // -----------------------------------------------------------------------
159 static const NativePluginDescriptor midichanabDesc = {
160 .category = NATIVE_PLUGIN_CATEGORY_UTILITY,
161 .hints = NATIVE_PLUGIN_IS_RTSAFE,
162 .supports = NATIVE_PLUGIN_SUPPORTS_EVERYTHING,
163 .audioIns = 0,
164 .audioOuts = 0,
165 .cvIns = 0,
166 .cvOuts = 0,
167 .midiIns = 1,
168 .midiOuts = 2,
169 .paramIns = 0,
170 .paramOuts = 0,
171 .name = "MIDI Channel A/B",
172 .label = "midichanab",
173 .maker = "Milk Brewster",
174 .copyright = "GNU GPL v2+",
176 .instantiate = midichanab_instantiate,
177 .cleanup = midichanab_cleanup,
179 .get_parameter_count = midichanab_get_parameter_count,
180 .get_parameter_info = midichanab_get_parameter_info,
181 .get_parameter_value = midichanab_get_parameter_value,
183 .get_midi_program_count = NULL,
184 .get_midi_program_info = NULL,
186 .set_parameter_value = midichanab_set_parameter_value,
187 .set_midi_program = NULL,
188 .set_custom_data = NULL,
190 .ui_show = NULL,
191 .ui_idle = NULL,
193 .ui_set_parameter_value = NULL,
194 .ui_set_midi_program = NULL,
195 .ui_set_custom_data = NULL,
197 .activate = NULL,
198 .deactivate = NULL,
199 .process = midichanab_process,
201 .get_state = NULL,
202 .set_state = NULL,
204 .dispatcher = NULL
207 // -----------------------------------------------------------------------
209 void carla_register_native_plugin_midichanab(void);
211 void carla_register_native_plugin_midichanab(void)
213 carla_register_native_plugin(&midichanabDesc);
216 // -----------------------------------------------------------------------