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"
25 // -----------------------------------------------------------------------
28 const NativeHostDescriptor
* host
;
29 bool channels
[MAX_MIDI_CHANNELS
];
32 // -----------------------------------------------------------------------
34 static NativePluginHandle
midichanfilter_instantiate(const NativeHostDescriptor
* host
)
36 MidiGainHandle
* const handle
= (MidiGainHandle
*)malloc(sizeof(MidiGainHandle
));
43 for (int i
=MAX_MIDI_CHANNELS
; --i
>=0;)
44 handle
->channels
[i
] = 1.0f
;
49 #define handlePtr ((MidiGainHandle*)handle)
51 static void midichanfilter_cleanup(NativePluginHandle handle
)
56 static uint32_t midichanfilter_get_parameter_count(NativePluginHandle handle
)
58 return MAX_MIDI_CHANNELS
;
64 static const NativeParameter
* midichanfilter_get_parameter_info(NativePluginHandle handle
, uint32_t index
)
66 if (index
> MAX_MIDI_CHANNELS
)
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
;
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);
93 static float midichanfilter_get_parameter_value(NativePluginHandle handle
, uint32_t index
)
95 if (index
> MAX_MIDI_CHANNELS
)
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
)
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
)
130 if (channels
[channel
])
131 host
->write_midi_event(host
->handle
, midiEvent
);
135 // pass through all non-message events
136 host
->write_midi_event(host
->handle
, midiEvent
);
148 // -----------------------------------------------------------------------
150 static const NativePluginDescriptor midichanfilterDesc
= {
151 .category
= NATIVE_PLUGIN_CATEGORY_UTILITY
,
152 .hints
= NATIVE_PLUGIN_IS_RTSAFE
,
153 .supports
= NATIVE_PLUGIN_SUPPORTS_EVERYTHING
,
162 .name
= "MIDI Channel Filter",
163 .label
= "midichanfilter",
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
,
184 .ui_set_parameter_value
= NULL
,
185 .ui_set_midi_program
= NULL
,
186 .ui_set_custom_data
= NULL
,
190 .process
= midichanfilter_process
,
198 // -----------------------------------------------------------------------
200 void carla_register_native_plugin_midichanfilter(void);
202 void carla_register_native_plugin_midichanfilter(void)
204 carla_register_native_plugin(&midichanfilterDesc
);
207 // -----------------------------------------------------------------------