Fix potential wrong-over-optimization in math utilities
[carla.git] / source / native-plugins / midi-join.c
blob08a33121e0584960cbe8b051d16c28d4258abc83
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>
24 // -----------------------------------------------------------------------
26 typedef struct {
27 const NativeHostDescriptor* host;
28 } MidiSplitHandle;
30 // -----------------------------------------------------------------------
32 static NativePluginHandle midijoin_instantiate(const NativeHostDescriptor* host)
34 MidiSplitHandle* const handle = (MidiSplitHandle*)malloc(sizeof(MidiSplitHandle));
36 if (handle == NULL)
37 return NULL;
39 handle->host = host;
40 return handle;
43 #define handlePtr ((MidiSplitHandle*)handle)
45 static void midijoin_cleanup(NativePluginHandle handle)
47 free(handlePtr);
50 // FIXME for v3.0, use const for the input buffer
51 static void midijoin_process(NativePluginHandle handle,
52 float** inBuffer, float** outBuffer, uint32_t frames,
53 const NativeMidiEvent* midiEvents, uint32_t midiEventCount)
55 const NativeHostDescriptor* const host = handlePtr->host;
56 NativeMidiEvent tmpEvent;
58 for (uint32_t i=0; i < midiEventCount; ++i)
60 const NativeMidiEvent* const midiEvent = &midiEvents[i];
62 if (midiEvent->port >= MAX_MIDI_CHANNELS)
64 printf("Assertion error: midiEvent->port:%u >= MAX_MIDI_CHANNELS\n", midiEvent->port);
65 continue;
68 const uint8_t statusByte = midiEvent->data[0];
70 if (MIDI_IS_CHANNEL_MESSAGE(statusByte))
71 tmpEvent.data[0] = (uint8_t)((statusByte & MIDI_STATUS_BIT) | (midiEvent->port & MIDI_CHANNEL_BIT));
72 else
73 tmpEvent.data[0] = statusByte;
75 tmpEvent.port = 0;
76 tmpEvent.time = midiEvent->time;
77 tmpEvent.data[1] = midiEvent->data[1];
78 tmpEvent.data[2] = midiEvent->data[2];
79 tmpEvent.data[3] = midiEvent->data[3];
80 tmpEvent.size = midiEvent->size;
82 host->write_midi_event(host->handle, &tmpEvent);
85 return;
87 // unused
88 (void)inBuffer;
89 (void)outBuffer;
90 (void)frames;
93 #undef handlePtr
95 // -----------------------------------------------------------------------
97 static const NativePluginDescriptor midijoinDesc = {
98 .category = NATIVE_PLUGIN_CATEGORY_UTILITY,
99 .hints = NATIVE_PLUGIN_IS_RTSAFE,
100 .supports = NATIVE_PLUGIN_SUPPORTS_EVERYTHING,
101 .audioIns = 0,
102 .audioOuts = 0,
103 .cvIns = 0,
104 .cvOuts = 0,
105 .midiIns = MAX_MIDI_CHANNELS,
106 .midiOuts = 1,
107 .paramIns = 0,
108 .paramOuts = 0,
109 .name = "MIDI Join",
110 .label = "midijoin",
111 .maker = "falkTX",
112 .copyright = "GNU GPL v2+",
114 .instantiate = midijoin_instantiate,
115 .cleanup = midijoin_cleanup,
117 .get_parameter_count = NULL,
118 .get_parameter_info = NULL,
119 .get_parameter_value = NULL,
121 .get_midi_program_count = NULL,
122 .get_midi_program_info = NULL,
124 .set_parameter_value = NULL,
125 .set_midi_program = NULL,
126 .set_custom_data = NULL,
128 .ui_show = NULL,
129 .ui_idle = NULL,
131 .ui_set_parameter_value = NULL,
132 .ui_set_midi_program = NULL,
133 .ui_set_custom_data = NULL,
135 .activate = NULL,
136 .deactivate = NULL,
137 .process = midijoin_process,
139 .get_state = NULL,
140 .set_state = NULL,
142 .dispatcher = NULL
145 // -----------------------------------------------------------------------
147 void carla_register_native_plugin_midijoin(void);
149 void carla_register_native_plugin_midijoin(void)
151 carla_register_native_plugin(&midijoinDesc);
154 // -----------------------------------------------------------------------