Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
5.7KB

  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. #include "CarlaNative.h"
  18. #include "CarlaMIDI.h"
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. // -----------------------------------------------------------------------
  23. typedef struct {
  24. const NativeHostDescriptor* host;
  25. bool channels[MAX_MIDI_CHANNELS];
  26. } MidiGainHandle;
  27. // -----------------------------------------------------------------------
  28. static NativePluginHandle midichanfilter_instantiate(const NativeHostDescriptor* host)
  29. {
  30. MidiGainHandle* const handle = (MidiGainHandle*)malloc(sizeof(MidiGainHandle));
  31. if (handle == NULL)
  32. return NULL;
  33. handle->host = host;
  34. for (int i=MAX_MIDI_CHANNELS; --i>=0;)
  35. handle->channels[i] = 1.0f;
  36. return handle;
  37. }
  38. #define handlePtr ((MidiGainHandle*)handle)
  39. static void midichanfilter_cleanup(NativePluginHandle handle)
  40. {
  41. free(handlePtr);
  42. }
  43. static uint32_t midichanfilter_get_parameter_count(NativePluginHandle handle)
  44. {
  45. return MAX_MIDI_CHANNELS;
  46. // unused
  47. (void)handle;
  48. }
  49. static const NativeParameter* midichanfilter_get_parameter_info(NativePluginHandle handle, uint32_t index)
  50. {
  51. if (index > MAX_MIDI_CHANNELS)
  52. return NULL;
  53. static NativeParameter param;
  54. static const NativeParameterScalePoint scalePoints[2] = { { "Off", 0.0f }, { "On", 1.0f } };
  55. static char paramName[24];
  56. param.hints = NATIVE_PARAMETER_IS_ENABLED|NATIVE_PARAMETER_IS_AUTOMABLE|NATIVE_PARAMETER_IS_BOOLEAN|NATIVE_PARAMETER_USES_SCALEPOINTS;
  57. param.name = paramName;
  58. param.unit = NULL;
  59. param.ranges.def = 1.0f;
  60. param.ranges.min = 0.0f;
  61. param.ranges.max = 1.0f;
  62. param.ranges.step = 1.0f;
  63. param.ranges.stepSmall = 1.0f;
  64. param.ranges.stepLarge = 1.0f;
  65. param.scalePointCount = 2;
  66. param.scalePoints = scalePoints;
  67. snprintf(paramName, 24, "%u", index+1);
  68. return &param;
  69. // unused
  70. (void)handle;
  71. }
  72. static float midichanfilter_get_parameter_value(NativePluginHandle handle, uint32_t index)
  73. {
  74. if (index > MAX_MIDI_CHANNELS)
  75. return 0.0f;
  76. return handlePtr->channels[index] ? 1.0f : 0.0f;
  77. }
  78. static void midichanfilter_set_parameter_value(NativePluginHandle handle, uint32_t index, float value)
  79. {
  80. if (index > MAX_MIDI_CHANNELS)
  81. return;
  82. handlePtr->channels[index] = (value >= 0.5f);
  83. }
  84. static void midichanfilter_process(NativePluginHandle handle,
  85. const float** inBuffer, float** outBuffer, uint32_t frames,
  86. const NativeMidiEvent* midiEvents, uint32_t midiEventCount)
  87. {
  88. const NativeHostDescriptor* const host = handlePtr->host;
  89. const bool* const channels = handlePtr->channels;
  90. for (uint32_t i=0; i < midiEventCount; ++i)
  91. {
  92. const NativeMidiEvent* const midiEvent = &midiEvents[i];
  93. const uint8_t status = (uint8_t)MIDI_GET_STATUS_FROM_DATA(midiEvent->data);
  94. if (MIDI_IS_CHANNEL_MESSAGE(status))
  95. {
  96. const uint8_t channel = (uint8_t)MIDI_GET_CHANNEL_FROM_DATA(midiEvent->data);
  97. if (channel >= MAX_MIDI_CHANNELS)
  98. continue;
  99. if (channels[channel])
  100. host->write_midi_event(host->handle, midiEvent);
  101. }
  102. else
  103. {
  104. // pass through all non-message events
  105. host->write_midi_event(host->handle, midiEvent);
  106. }
  107. }
  108. return;
  109. // unused
  110. (void)inBuffer;
  111. (void)outBuffer;
  112. (void)frames;
  113. }
  114. // -----------------------------------------------------------------------
  115. static const NativePluginDescriptor midichanfilterDesc = {
  116. .category = NATIVE_PLUGIN_CATEGORY_UTILITY,
  117. .hints = NATIVE_PLUGIN_IS_RTSAFE,
  118. .supports = NATIVE_PLUGIN_SUPPORTS_EVERYTHING,
  119. .audioIns = 0,
  120. .audioOuts = 0,
  121. .cvIns = 0,
  122. .cvOuts = 0,
  123. .midiIns = 1,
  124. .midiOuts = 1,
  125. .paramIns = 0,
  126. .paramOuts = 0,
  127. .name = "MIDI Channel Filter",
  128. .label = "midichanfilter",
  129. .maker = "falkTX",
  130. .copyright = "GNU GPL v2+",
  131. .instantiate = midichanfilter_instantiate,
  132. .cleanup = midichanfilter_cleanup,
  133. .get_parameter_count = midichanfilter_get_parameter_count,
  134. .get_parameter_info = midichanfilter_get_parameter_info,
  135. .get_parameter_value = midichanfilter_get_parameter_value,
  136. .get_midi_program_count = NULL,
  137. .get_midi_program_info = NULL,
  138. .set_parameter_value = midichanfilter_set_parameter_value,
  139. .set_midi_program = NULL,
  140. .set_custom_data = NULL,
  141. .ui_show = NULL,
  142. .ui_idle = NULL,
  143. .ui_set_parameter_value = NULL,
  144. .ui_set_midi_program = NULL,
  145. .ui_set_custom_data = NULL,
  146. .activate = NULL,
  147. .deactivate = NULL,
  148. .process = midichanfilter_process,
  149. .get_state = NULL,
  150. .set_state = NULL,
  151. .dispatcher = NULL,
  152. .render_inline_display = NULL
  153. };
  154. // -----------------------------------------------------------------------
  155. void carla_register_native_plugin_midichanfilter(void);
  156. void carla_register_native_plugin_midichanfilter(void)
  157. {
  158. carla_register_native_plugin(&midichanfilterDesc);
  159. }
  160. // -----------------------------------------------------------------------