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.

midi-channel-ab.c 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2012-2019 Filipe Coelho <falktx@falktx.com>
  4. * Copyright (C) 2018 Milk Brewster <code@milkmiruku.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  17. */
  18. #include "CarlaNative.h"
  19. #include "CarlaMIDI.h"
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. // -----------------------------------------------------------------------
  24. typedef struct {
  25. const NativeHostDescriptor* host;
  26. bool channels[MAX_MIDI_CHANNELS];
  27. } MidiGainHandle;
  28. // -----------------------------------------------------------------------
  29. static NativePluginHandle midichanab_instantiate(const NativeHostDescriptor* host)
  30. {
  31. MidiGainHandle* const handle = (MidiGainHandle*)malloc(sizeof(MidiGainHandle));
  32. if (handle == NULL)
  33. return NULL;
  34. handle->host = host;
  35. memset(handle->channels, 0, MAX_MIDI_CHANNELS);
  36. return handle;
  37. }
  38. #define handlePtr ((MidiGainHandle*)handle)
  39. static void midichanab_cleanup(NativePluginHandle handle)
  40. {
  41. free(handlePtr);
  42. }
  43. static uint32_t midichanab_get_parameter_count(NativePluginHandle handle)
  44. {
  45. return MAX_MIDI_CHANNELS;
  46. // unused
  47. (void)handle;
  48. }
  49. static const NativeParameter* midichanab_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] = { { "Output A", 0 }, { "Output B", 1 } };
  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 = 0;
  60. param.ranges.min = 0;
  61. param.ranges.max = 1;
  62. param.ranges.step = 1;
  63. param.ranges.stepSmall = 1;
  64. param.ranges.stepLarge = 1;
  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 midichanab_get_parameter_value(NativePluginHandle handle, uint32_t index)
  73. {
  74. if (index >= MAX_MIDI_CHANNELS)
  75. return 0;
  76. return handlePtr->channels[index] ? 1 : 0;
  77. }
  78. static void midichanab_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 midichanab_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. NativeMidiEvent tmpEvent;
  91. for (uint32_t i=0; i < midiEventCount; ++i)
  92. {
  93. const NativeMidiEvent* const midiEvent = &midiEvents[i];
  94. const uint8_t status = (uint8_t)MIDI_GET_STATUS_FROM_DATA(midiEvent->data);
  95. if (MIDI_IS_CHANNEL_MESSAGE(status))
  96. {
  97. const uint8_t channel = (uint8_t)MIDI_GET_CHANNEL_FROM_DATA(midiEvent->data);
  98. if (channel >= MAX_MIDI_CHANNELS)
  99. continue;
  100. if (channels[channel])
  101. {
  102. memcpy(&tmpEvent, midiEvent, sizeof(NativeMidiEvent));
  103. ++tmpEvent.port;
  104. host->write_midi_event(host->handle, &tmpEvent);
  105. }
  106. else
  107. {
  108. host->write_midi_event(host->handle, midiEvent);
  109. }
  110. }
  111. else
  112. {
  113. // pass through all non-message events
  114. host->write_midi_event(host->handle, midiEvent);
  115. }
  116. }
  117. return;
  118. // unused
  119. (void)inBuffer;
  120. (void)outBuffer;
  121. (void)frames;
  122. }
  123. // -----------------------------------------------------------------------
  124. static const NativePluginDescriptor midichanabDesc = {
  125. .category = NATIVE_PLUGIN_CATEGORY_UTILITY,
  126. .hints = NATIVE_PLUGIN_IS_RTSAFE,
  127. .supports = NATIVE_PLUGIN_SUPPORTS_EVERYTHING,
  128. .audioIns = 0,
  129. .audioOuts = 0,
  130. .midiIns = 1,
  131. .midiOuts = 2,
  132. .paramIns = 0,
  133. .paramOuts = 0,
  134. .name = "MIDI Channel A/B",
  135. .label = "midichanab",
  136. .maker = "Milk Brewster",
  137. .copyright = "GNU GPL v2+",
  138. .instantiate = midichanab_instantiate,
  139. .cleanup = midichanab_cleanup,
  140. .get_parameter_count = midichanab_get_parameter_count,
  141. .get_parameter_info = midichanab_get_parameter_info,
  142. .get_parameter_value = midichanab_get_parameter_value,
  143. .get_midi_program_count = NULL,
  144. .get_midi_program_info = NULL,
  145. .set_parameter_value = midichanab_set_parameter_value,
  146. .set_midi_program = NULL,
  147. .set_custom_data = NULL,
  148. .ui_show = NULL,
  149. .ui_idle = NULL,
  150. .ui_set_parameter_value = NULL,
  151. .ui_set_midi_program = NULL,
  152. .ui_set_custom_data = NULL,
  153. .activate = NULL,
  154. .deactivate = NULL,
  155. .process = midichanab_process,
  156. .get_state = NULL,
  157. .set_state = NULL,
  158. .dispatcher = NULL,
  159. .render_inline_display = NULL
  160. };
  161. // -----------------------------------------------------------------------
  162. void carla_register_native_plugin_midichanab(void);
  163. void carla_register_native_plugin_midichanab(void)
  164. {
  165. carla_register_native_plugin(&midichanabDesc);
  166. }
  167. // -----------------------------------------------------------------------