1 /*
2  * Carla Native Plugins
3  * Copyright (C) 2012-2019 Filipe Coelho <falktx@falktx.com>
4  *
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.
9  *
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.
14  *
15  * For a full copy of the GNU General Public License see the doc/GPL.txt file.
16  */
17 
18 #include "CarlaNative.h"
19 #include "CarlaMIDI.h"
20 
21 #include <stdlib.h>
22 
23 // -----------------------------------------------------------------------
24 
25 typedef struct {
26     const NativeHostDescriptor* host;
27 } MidiSplitHandle;
28 
29 // -----------------------------------------------------------------------
30 
midisplit_instantiate(const NativeHostDescriptor * host)31 static NativePluginHandle midisplit_instantiate(const NativeHostDescriptor* host)
32 {
33     MidiSplitHandle* const handle = (MidiSplitHandle*)malloc(sizeof(MidiSplitHandle));
34 
35     if (handle == NULL)
36         return NULL;
37 
38     handle->host = host;
39     return handle;
40 }
41 
42 #define handlePtr ((MidiSplitHandle*)handle)
43 
midisplit_cleanup(NativePluginHandle handle)44 static void midisplit_cleanup(NativePluginHandle handle)
45 {
46     free(handlePtr);
47 }
48 
49 // FIXME for v3.0, use const for the input buffer
midisplit_process(NativePluginHandle handle,float ** inBuffer,float ** outBuffer,uint32_t frames,const NativeMidiEvent * midiEvents,uint32_t midiEventCount)50 static void midisplit_process(NativePluginHandle handle,
51                               float** inBuffer, float** outBuffer, uint32_t frames,
52                               const NativeMidiEvent* midiEvents, uint32_t midiEventCount)
53 {
54     const NativeHostDescriptor* const host = handlePtr->host;
55     NativeMidiEvent tmpEvent;
56 
57     for (uint32_t i=0; i < midiEventCount; ++i)
58     {
59         const NativeMidiEvent* const midiEvent = &midiEvents[i];
60 
61         const uint8_t status  = (uint8_t)MIDI_GET_STATUS_FROM_DATA(midiEvent->data);
62         const uint8_t channel = (uint8_t)MIDI_GET_CHANNEL_FROM_DATA(midiEvent->data);
63 
64         if (channel >= MAX_MIDI_CHANNELS)
65             continue;
66 
67         tmpEvent.port    = channel;
68         tmpEvent.time    = midiEvent->time;
69         tmpEvent.data[0] = status;
70         tmpEvent.data[1] = midiEvent->data[1];
71         tmpEvent.data[2] = midiEvent->data[2];
72         tmpEvent.data[3] = midiEvent->data[3];
73         tmpEvent.size    = midiEvent->size;
74 
75         host->write_midi_event(host->handle, &tmpEvent);
76     }
77 
78     return;
79 
80     // unused
81     (void)inBuffer;
82     (void)outBuffer;
83     (void)frames;
84 }
85 
86 #undef handlePtr
87 
88 // -----------------------------------------------------------------------
89 
90 static const NativePluginDescriptor midisplitDesc = {
91     .category  = NATIVE_PLUGIN_CATEGORY_UTILITY,
92     .hints     = NATIVE_PLUGIN_IS_RTSAFE,
93     .supports  = NATIVE_PLUGIN_SUPPORTS_EVERYTHING,
94     .audioIns  = 0,
95     .audioOuts = 0,
96     .cvIns     = 0,
97     .cvOuts    = 0,
98     .midiIns   = 1,
99     .midiOuts  = MAX_MIDI_CHANNELS,
100     .paramIns  = 0,
101     .paramOuts = 0,
102     .name      = "MIDI Split",
103     .label     = "midisplit",
104     .maker     = "falkTX",
105     .copyright = "GNU GPL v2+",
106 
107     .instantiate = midisplit_instantiate,
108     .cleanup     = midisplit_cleanup,
109 
110     .get_parameter_count = NULL,
111     .get_parameter_info  = NULL,
112     .get_parameter_value = NULL,
113 
114     .get_midi_program_count = NULL,
115     .get_midi_program_info  = NULL,
116 
117     .set_parameter_value = NULL,
118     .set_midi_program    = NULL,
119     .set_custom_data     = NULL,
120 
121     .ui_show = NULL,
122     .ui_idle = NULL,
123 
124     .ui_set_parameter_value = NULL,
125     .ui_set_midi_program    = NULL,
126     .ui_set_custom_data     = NULL,
127 
128     .activate   = NULL,
129     .deactivate = NULL,
130     .process    = midisplit_process,
131 
132     .get_state = NULL,
133     .set_state = NULL,
134 
135     .dispatcher = NULL
136 };
137 
138 // -----------------------------------------------------------------------
139 
140 void carla_register_native_plugin_midisplit(void);
141 
carla_register_native_plugin_midisplit(void)142 void carla_register_native_plugin_midisplit(void)
143 {
144     carla_register_native_plugin(&midisplitDesc);
145 }
146 
147 // -----------------------------------------------------------------------
148