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.

216 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. // -----------------------------------------------------------------------
  21. typedef enum {
  22. PARAM_CHANNEL = 0,
  23. PARAM_COUNT
  24. } MidiChannelizeParams;
  25. typedef struct {
  26. const NativeHostDescriptor* host;
  27. int channel;
  28. } MidiChannelizeHandle;
  29. // -----------------------------------------------------------------------
  30. static NativePluginHandle midichannelize_instantiate(const NativeHostDescriptor* host)
  31. {
  32. MidiChannelizeHandle* const handle = (MidiChannelizeHandle*)malloc(sizeof(MidiChannelizeHandle));
  33. if (handle == NULL)
  34. return NULL;
  35. handle->host = host;
  36. handle->channel = 1;
  37. return handle;
  38. }
  39. #define handlePtr ((MidiChannelizeHandle*)handle)
  40. static void midichannelize_cleanup(NativePluginHandle handle)
  41. {
  42. free(handlePtr);
  43. }
  44. static uint32_t midichannelize_get_parameter_count(NativePluginHandle handle)
  45. {
  46. return PARAM_COUNT;
  47. // unused
  48. (void)handle;
  49. }
  50. static const NativeParameter* midichannelize_get_parameter_info(NativePluginHandle handle, uint32_t index)
  51. {
  52. if (index > PARAM_COUNT)
  53. return NULL;
  54. static NativeParameter param;
  55. param.hints = NATIVE_PARAMETER_IS_ENABLED|NATIVE_PARAMETER_IS_AUTOMATABLE|NATIVE_PARAMETER_IS_INTEGER;
  56. param.unit = NULL;
  57. param.scalePointCount = 0;
  58. param.scalePoints = NULL;
  59. switch (index)
  60. {
  61. case PARAM_CHANNEL:
  62. param.name = "Channel";
  63. param.ranges.def = 1.0f,
  64. param.ranges.min = 1.0f,
  65. param.ranges.max = 16.0f,
  66. param.ranges.step = 1.0f;
  67. param.ranges.stepSmall = 1.0f;
  68. param.ranges.stepLarge = 1.0f;
  69. break;
  70. }
  71. return &param;
  72. // unused
  73. (void)handle;
  74. }
  75. static float midichannelize_get_parameter_value(NativePluginHandle handle, uint32_t index)
  76. {
  77. switch (index)
  78. {
  79. case PARAM_CHANNEL:
  80. return (float)handlePtr->channel;
  81. default:
  82. return 0.0f;
  83. }
  84. }
  85. static void midichannelize_set_parameter_value(NativePluginHandle handle, uint32_t index, float value)
  86. {
  87. switch (index)
  88. {
  89. case PARAM_CHANNEL:
  90. handlePtr->channel = (int)value;
  91. break;
  92. }
  93. }
  94. // FIXME for v3.0, use const for the input buffer
  95. static void midichannelize_process(NativePluginHandle handle,
  96. float** inBuffer, float** outBuffer, uint32_t frames,
  97. const NativeMidiEvent* midiEvents, uint32_t midiEventCount)
  98. {
  99. const NativeHostDescriptor* const host = handlePtr->host;
  100. const int channel = handlePtr->channel;
  101. NativeMidiEvent tmpEvent;
  102. for (uint32_t i=0; i < midiEventCount; ++i)
  103. {
  104. const NativeMidiEvent* const midiEvent = &midiEvents[i];
  105. const uint8_t status = (uint8_t)MIDI_GET_STATUS_FROM_DATA(midiEvent->data);
  106. if (MIDI_IS_CHANNEL_MESSAGE(status))
  107. {
  108. tmpEvent.port = midiEvent->port;
  109. tmpEvent.time = midiEvent->time;
  110. tmpEvent.data[0] = (uint8_t)(status | (channel - 1));
  111. tmpEvent.data[1] = midiEvent->data[1];
  112. tmpEvent.data[2] = midiEvent->data[2];
  113. tmpEvent.data[3] = midiEvent->data[3];
  114. tmpEvent.size = midiEvent->size;
  115. host->write_midi_event(host->handle, &tmpEvent);
  116. }
  117. }
  118. return;
  119. // unused
  120. (void)inBuffer;
  121. (void)outBuffer;
  122. (void)frames;
  123. }
  124. #undef handlePtr
  125. // -----------------------------------------------------------------------
  126. static const NativePluginDescriptor midichannelizeDesc = {
  127. .category = NATIVE_PLUGIN_CATEGORY_UTILITY,
  128. .hints = NATIVE_PLUGIN_IS_RTSAFE,
  129. .supports = NATIVE_PLUGIN_SUPPORTS_EVERYTHING,
  130. .audioIns = 0,
  131. .audioOuts = 0,
  132. .cvIns = 0,
  133. .cvOuts = 0,
  134. .midiIns = 1,
  135. .midiOuts = 1,
  136. .paramIns = 1,
  137. .paramOuts = 0,
  138. .name = "MIDI Channelize",
  139. .label = "midichannelize",
  140. .maker = "falkTX",
  141. .copyright = "GNU GPL v2+",
  142. .instantiate = midichannelize_instantiate,
  143. .cleanup = midichannelize_cleanup,
  144. .get_parameter_count = midichannelize_get_parameter_count,
  145. .get_parameter_info = midichannelize_get_parameter_info,
  146. .get_parameter_value = midichannelize_get_parameter_value,
  147. .get_midi_program_count = NULL,
  148. .get_midi_program_info = NULL,
  149. .set_parameter_value = midichannelize_set_parameter_value,
  150. .set_midi_program = NULL,
  151. .set_custom_data = NULL,
  152. .ui_show = NULL,
  153. .ui_idle = NULL,
  154. .ui_set_parameter_value = NULL,
  155. .ui_set_midi_program = NULL,
  156. .ui_set_custom_data = NULL,
  157. .activate = NULL,
  158. .deactivate = NULL,
  159. .process = midichannelize_process,
  160. .get_state = NULL,
  161. .set_state = NULL,
  162. .dispatcher = NULL
  163. };
  164. // -----------------------------------------------------------------------
  165. void carla_register_native_plugin_midichannelize(void);
  166. void carla_register_native_plugin_midichannelize(void)
  167. {
  168. carla_register_native_plugin(&midichannelizeDesc);
  169. }
  170. // -----------------------------------------------------------------------