Add initial bits for Qt6 support
[carla.git] / source / native-plugins / midi-transpose.c
blob2e1d3a4bf06dcef9b9ef471e89f266db57762efc
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>
23 // -----------------------------------------------------------------------
25 typedef enum {
26 PARAM_OCTAVES = 0,
27 PARAM_SEMITONES,
28 PARAM_COUNT
29 } MidiTransposeParams;
31 typedef struct {
32 const NativeHostDescriptor* host;
33 int octaves;
34 int semitones;
35 } MidiTransposeHandle;
37 // -----------------------------------------------------------------------
39 static NativePluginHandle miditranspose_instantiate(const NativeHostDescriptor* host)
41 MidiTransposeHandle* const handle = (MidiTransposeHandle*)malloc(sizeof(MidiTransposeHandle));
43 if (handle == NULL)
44 return NULL;
46 handle->host = host;
47 handle->octaves = 0;
48 handle->semitones = 0;
49 return handle;
52 #define handlePtr ((MidiTransposeHandle*)handle)
54 static void miditranspose_cleanup(NativePluginHandle handle)
56 free(handlePtr);
59 static uint32_t miditranspose_get_parameter_count(NativePluginHandle handle)
61 return PARAM_COUNT;
63 // unused
64 (void)handle;
67 static const NativeParameter* miditranspose_get_parameter_info(NativePluginHandle handle, uint32_t index)
69 if (index > PARAM_COUNT)
70 return NULL;
72 static NativeParameter param;
74 param.hints = NATIVE_PARAMETER_IS_ENABLED|NATIVE_PARAMETER_IS_AUTOMATABLE|NATIVE_PARAMETER_IS_INTEGER;
75 param.unit = NULL;
76 param.scalePointCount = 0;
77 param.scalePoints = NULL;
79 switch (index)
81 case PARAM_OCTAVES:
82 param.name = "Octaves";
83 param.ranges.def = 0.0f,
84 param.ranges.min = -8.0f,
85 param.ranges.max = 8.0f,
86 param.ranges.step = 1.0f;
87 param.ranges.stepSmall = 1.0f;
88 param.ranges.stepLarge = 4.0f;
89 break;
90 case PARAM_SEMITONES:
91 param.name = "Semitones";
92 param.ranges.def = 0.0f,
93 param.ranges.min = -12.0f,
94 param.ranges.max = 12.0f,
95 param.ranges.step = 1.0f;
96 param.ranges.stepSmall = 1.0f;
97 param.ranges.stepLarge = 4.0f;
98 break;
101 return &param;
103 // unused
104 (void)handle;
107 static float miditranspose_get_parameter_value(NativePluginHandle handle, uint32_t index)
109 switch (index)
111 case PARAM_OCTAVES:
112 return (float)handlePtr->octaves;
113 case PARAM_SEMITONES:
114 return (float)handlePtr->semitones;
115 default:
116 return 0.0f;
120 static void miditranspose_set_parameter_value(NativePluginHandle handle, uint32_t index, float value)
122 switch (index)
124 case PARAM_OCTAVES:
125 handlePtr->octaves = (int)value;
126 break;
127 case PARAM_SEMITONES:
128 handlePtr->semitones = (int)value;
129 break;
133 // FIXME for v3.0, use const for the input buffer
134 static void miditranspose_process(NativePluginHandle handle,
135 float** inBuffer, float** outBuffer, uint32_t frames,
136 const NativeMidiEvent* midiEvents, uint32_t midiEventCount)
138 const NativeHostDescriptor* const host = handlePtr->host;
139 const int octaves = handlePtr->octaves;
140 const int semitones = handlePtr->semitones;
141 NativeMidiEvent tmpEvent;
143 for (uint32_t i=0; i < midiEventCount; ++i)
145 const NativeMidiEvent* const midiEvent = &midiEvents[i];
147 const uint8_t status = (uint8_t)MIDI_GET_STATUS_FROM_DATA(midiEvent->data);
149 if (status == MIDI_STATUS_NOTE_OFF || status == MIDI_STATUS_NOTE_ON)
151 int oldnote = midiEvent->data[1];
152 int newnote = oldnote + octaves*12 + semitones;
154 if (newnote < 0 || newnote >= MAX_MIDI_NOTE)
155 continue;
157 tmpEvent.port = midiEvent->port;
158 tmpEvent.time = midiEvent->time;
159 tmpEvent.data[0] = midiEvent->data[0];
160 tmpEvent.data[1] = (uint8_t)newnote;
161 tmpEvent.data[2] = midiEvent->data[2];
162 tmpEvent.data[3] = midiEvent->data[3];
163 tmpEvent.size = midiEvent->size;
165 host->write_midi_event(host->handle, &tmpEvent);
167 else
168 host->write_midi_event(host->handle, midiEvent);
171 return;
173 // unused
174 (void)inBuffer;
175 (void)outBuffer;
176 (void)frames;
179 #undef handlePtr
181 // -----------------------------------------------------------------------
183 static const NativePluginDescriptor miditransposeDesc = {
184 .category = NATIVE_PLUGIN_CATEGORY_UTILITY,
185 .hints = NATIVE_PLUGIN_IS_RTSAFE,
186 .supports = NATIVE_PLUGIN_SUPPORTS_EVERYTHING,
187 .audioIns = 0,
188 .audioOuts = 0,
189 .cvIns = 0,
190 .cvOuts = 0,
191 .midiIns = 1,
192 .midiOuts = 1,
193 .paramIns = 2,
194 .paramOuts = 0,
195 .name = "MIDI Transpose",
196 .label = "miditranspose",
197 .maker = "falkTX",
198 .copyright = "GNU GPL v2+",
200 .instantiate = miditranspose_instantiate,
201 .cleanup = miditranspose_cleanup,
203 .get_parameter_count = miditranspose_get_parameter_count,
204 .get_parameter_info = miditranspose_get_parameter_info,
205 .get_parameter_value = miditranspose_get_parameter_value,
207 .get_midi_program_count = NULL,
208 .get_midi_program_info = NULL,
210 .set_parameter_value = miditranspose_set_parameter_value,
211 .set_midi_program = NULL,
212 .set_custom_data = NULL,
214 .ui_show = NULL,
215 .ui_idle = NULL,
217 .ui_set_parameter_value = NULL,
218 .ui_set_midi_program = NULL,
219 .ui_set_custom_data = NULL,
221 .activate = NULL,
222 .deactivate = NULL,
223 .process = miditranspose_process,
225 .get_state = NULL,
226 .set_state = NULL,
228 .dispatcher = NULL
231 // -----------------------------------------------------------------------
233 void carla_register_native_plugin_miditranspose(void);
235 void carla_register_native_plugin_miditranspose(void)
237 carla_register_native_plugin(&miditransposeDesc);
240 // -----------------------------------------------------------------------