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-channelize.c 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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_AUTOMABLE|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. static void midichannelize_process(NativePluginHandle handle,
  95. const float** inBuffer, float** outBuffer, uint32_t frames,
  96. const NativeMidiEvent* midiEvents, uint32_t midiEventCount)
  97. {
  98. const NativeHostDescriptor* const host = handlePtr->host;
  99. const int channel = handlePtr->channel;
  100. NativeMidiEvent tmpEvent;
  101. for (uint32_t i=0; i < midiEventCount; ++i)
  102. {
  103. const NativeMidiEvent* const midiEvent = &midiEvents[i];
  104. const uint8_t status = (uint8_t)MIDI_GET_STATUS_FROM_DATA(midiEvent->data);
  105. if (MIDI_IS_CHANNEL_MESSAGE(status))
  106. {
  107. tmpEvent.port = midiEvent->port;
  108. tmpEvent.time = midiEvent->time;
  109. tmpEvent.data[0] = (uint8_t)(status | (channel - 1));
  110. tmpEvent.data[1] = midiEvent->data[1];
  111. tmpEvent.data[2] = midiEvent->data[2];
  112. tmpEvent.data[3] = midiEvent->data[3];
  113. tmpEvent.size = midiEvent->size;
  114. host->write_midi_event(host->handle, &tmpEvent);
  115. }
  116. }
  117. return;
  118. // unused
  119. (void)inBuffer;
  120. (void)outBuffer;
  121. (void)frames;
  122. }
  123. #undef handlePtr
  124. // -----------------------------------------------------------------------
  125. static const NativePluginDescriptor midichannelizeDesc = {
  126. .category = NATIVE_PLUGIN_CATEGORY_UTILITY,
  127. .hints = NATIVE_PLUGIN_IS_RTSAFE,
  128. .supports = NATIVE_PLUGIN_SUPPORTS_EVERYTHING,
  129. .audioIns = 0,
  130. .audioOuts = 0,
  131. .midiIns = 1,
  132. .midiOuts = 1,
  133. .paramIns = 1,
  134. .paramOuts = 0,
  135. .name = "MIDI Channelize",
  136. .label = "midichannelize",
  137. .maker = "falkTX",
  138. .copyright = "GNU GPL v2+",
  139. .instantiate = midichannelize_instantiate,
  140. .cleanup = midichannelize_cleanup,
  141. .get_parameter_count = midichannelize_get_parameter_count,
  142. .get_parameter_info = midichannelize_get_parameter_info,
  143. .get_parameter_value = midichannelize_get_parameter_value,
  144. .get_midi_program_count = NULL,
  145. .get_midi_program_info = NULL,
  146. .set_parameter_value = midichannelize_set_parameter_value,
  147. .set_midi_program = NULL,
  148. .set_custom_data = NULL,
  149. .ui_show = NULL,
  150. .ui_idle = NULL,
  151. .ui_set_parameter_value = NULL,
  152. .ui_set_midi_program = NULL,
  153. .ui_set_custom_data = NULL,
  154. .activate = NULL,
  155. .deactivate = NULL,
  156. .process = midichannelize_process,
  157. .get_state = NULL,
  158. .set_state = NULL,
  159. .dispatcher = NULL
  160. };
  161. // -----------------------------------------------------------------------
  162. void carla_register_native_plugin_midichannelize(void);
  163. void carla_register_native_plugin_midichannelize(void)
  164. {
  165. carla_register_native_plugin(&midichannelizeDesc);
  166. }
  167. // -----------------------------------------------------------------------