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.

212 lines
5.8KB

  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2012-2018 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, float** inBuffer, float** outBuffer, uint32_t frames, const NativeMidiEvent* midiEvents, uint32_t midiEventCount)
  85. {
  86. const NativeHostDescriptor* const host = handlePtr->host;
  87. const bool* const channels = handlePtr->channels;
  88. NativeMidiEvent tmpEvent;
  89. for (uint32_t i=0; i < midiEventCount; ++i)
  90. {
  91. const NativeMidiEvent* const midiEvent = &midiEvents[i];
  92. const uint8_t status = (uint8_t)MIDI_GET_STATUS_FROM_DATA(midiEvent->data);
  93. if (MIDI_IS_CHANNEL_MESSAGE(status))
  94. {
  95. const uint8_t channel = (uint8_t)MIDI_GET_CHANNEL_FROM_DATA(midiEvent->data);
  96. if (channel >= MAX_MIDI_CHANNELS)
  97. continue;
  98. if (channels[channel])
  99. {
  100. memcpy(&tmpEvent, midiEvent, sizeof(NativeMidiEvent));
  101. ++tmpEvent.port;
  102. host->write_midi_event(host->handle, &tmpEvent);
  103. }
  104. else
  105. {
  106. host->write_midi_event(host->handle, midiEvent);
  107. }
  108. }
  109. else
  110. {
  111. // pass through all non-message events
  112. host->write_midi_event(host->handle, midiEvent);
  113. }
  114. }
  115. return;
  116. // unused
  117. (void)inBuffer;
  118. (void)outBuffer;
  119. (void)frames;
  120. }
  121. // -----------------------------------------------------------------------
  122. static const NativePluginDescriptor midichanabDesc = {
  123. .category = NATIVE_PLUGIN_CATEGORY_UTILITY,
  124. .hints = NATIVE_PLUGIN_IS_RTSAFE,
  125. .supports = NATIVE_PLUGIN_SUPPORTS_EVERYTHING,
  126. .audioIns = 0,
  127. .audioOuts = 0,
  128. .midiIns = 1,
  129. .midiOuts = 2,
  130. .paramIns = 0,
  131. .paramOuts = 0,
  132. .name = "MIDI Channel A/B",
  133. .label = "midichanab",
  134. .maker = "Milk Brewster",
  135. .copyright = "GNU GPL v2+",
  136. .instantiate = midichanab_instantiate,
  137. .cleanup = midichanab_cleanup,
  138. .get_parameter_count = midichanab_get_parameter_count,
  139. .get_parameter_info = midichanab_get_parameter_info,
  140. .get_parameter_value = midichanab_get_parameter_value,
  141. .get_midi_program_count = NULL,
  142. .get_midi_program_info = NULL,
  143. .set_parameter_value = midichanab_set_parameter_value,
  144. .set_midi_program = NULL,
  145. .set_custom_data = NULL,
  146. .ui_show = NULL,
  147. .ui_idle = NULL,
  148. .ui_set_parameter_value = NULL,
  149. .ui_set_midi_program = NULL,
  150. .ui_set_custom_data = NULL,
  151. .activate = NULL,
  152. .deactivate = NULL,
  153. .process = midichanab_process,
  154. .get_state = NULL,
  155. .set_state = NULL,
  156. .dispatcher = NULL
  157. };
  158. // -----------------------------------------------------------------------
  159. void carla_register_native_plugin_midichanab(void);
  160. void carla_register_native_plugin_midichanab(void)
  161. {
  162. carla_register_native_plugin(&midichanabDesc);
  163. }
  164. // -----------------------------------------------------------------------