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.5KB

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