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

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2012-2015 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. #include <stdio.h>
  21. #include <string.h>
  22. // -----------------------------------------------------------------------
  23. typedef struct {
  24. const NativeHostDescriptor* host;
  25. bool channels[MAX_MIDI_CHANNELS];
  26. } MidiGainHandle;
  27. // -----------------------------------------------------------------------
  28. static NativePluginHandle midichanfilter_instantiate(const NativeHostDescriptor* host)
  29. {
  30. MidiGainHandle* const handle = (MidiGainHandle*)malloc(sizeof(MidiGainHandle));
  31. if (handle == NULL)
  32. return NULL;
  33. handle->host = host;
  34. for (int i=MAX_MIDI_CHANNELS; --i>=0;)
  35. handle->channels[i] = 1.0f;
  36. return handle;
  37. }
  38. #define handlePtr ((MidiGainHandle*)handle)
  39. static void midichanfilter_cleanup(NativePluginHandle handle)
  40. {
  41. free(handlePtr);
  42. }
  43. static uint32_t midichanfilter_get_parameter_count(NativePluginHandle handle)
  44. {
  45. return MAX_MIDI_CHANNELS;
  46. // unused
  47. (void)handle;
  48. }
  49. static const NativeParameter* midichanfilter_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] = { { "Off", 0.0f }, { "On", 1.0f } };
  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 = 1.0f;
  60. param.ranges.min = 0.0f;
  61. param.ranges.max = 1.0f;
  62. param.ranges.step = 1.0f;
  63. param.ranges.stepSmall = 1.0f;
  64. param.ranges.stepLarge = 1.0f;
  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 midichanfilter_get_parameter_value(NativePluginHandle handle, uint32_t index)
  73. {
  74. if (index > MAX_MIDI_CHANNELS)
  75. return 0.0f;
  76. return handlePtr->channels[index] ? 1.0f : 0.0f;
  77. }
  78. static void midichanfilter_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 midichanfilter_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. for (uint32_t i=0; i < midiEventCount; ++i)
  89. {
  90. const NativeMidiEvent* const midiEvent = &midiEvents[i];
  91. const uint8_t status = (uint8_t)MIDI_GET_STATUS_FROM_DATA(midiEvent->data);
  92. if (MIDI_IS_CHANNEL_MESSAGE(status))
  93. {
  94. const uint8_t channel = (uint8_t)MIDI_GET_CHANNEL_FROM_DATA(midiEvent->data);
  95. if (channel >= MAX_MIDI_CHANNELS)
  96. continue;
  97. if (channels[channel])
  98. host->write_midi_event(host->handle, midiEvent);
  99. }
  100. else
  101. {
  102. // pass through all non-message events
  103. host->write_midi_event(host->handle, midiEvent);
  104. }
  105. }
  106. return;
  107. // unused
  108. (void)inBuffer;
  109. (void)outBuffer;
  110. (void)frames;
  111. }
  112. // -----------------------------------------------------------------------
  113. static const NativePluginDescriptor midichanfilterDesc = {
  114. .category = NATIVE_PLUGIN_CATEGORY_UTILITY,
  115. .hints = NATIVE_PLUGIN_IS_RTSAFE,
  116. .supports = NATIVE_PLUGIN_SUPPORTS_EVERYTHING,
  117. .audioIns = 0,
  118. .audioOuts = 0,
  119. .midiIns = 1,
  120. .midiOuts = 1,
  121. .paramIns = 0,
  122. .paramOuts = 0,
  123. .name = "MIDI Channel Filter",
  124. .label = "midichanfilter",
  125. .maker = "falkTX",
  126. .copyright = "GNU GPL v2+",
  127. .instantiate = midichanfilter_instantiate,
  128. .cleanup = midichanfilter_cleanup,
  129. .get_parameter_count = midichanfilter_get_parameter_count,
  130. .get_parameter_info = midichanfilter_get_parameter_info,
  131. .get_parameter_value = midichanfilter_get_parameter_value,
  132. .get_parameter_text = NULL,
  133. .get_midi_program_count = NULL,
  134. .get_midi_program_info = NULL,
  135. .set_parameter_value = midichanfilter_set_parameter_value,
  136. .set_midi_program = NULL,
  137. .set_custom_data = NULL,
  138. .ui_show = NULL,
  139. .ui_idle = NULL,
  140. .ui_set_parameter_value = NULL,
  141. .ui_set_midi_program = NULL,
  142. .ui_set_custom_data = NULL,
  143. .activate = NULL,
  144. .deactivate = NULL,
  145. .process = midichanfilter_process,
  146. .get_state = NULL,
  147. .set_state = NULL,
  148. .dispatcher = NULL
  149. };
  150. // -----------------------------------------------------------------------
  151. void carla_register_native_plugin_midichanfilter(void);
  152. void carla_register_native_plugin_midichanfilter(void)
  153. {
  154. carla_register_native_plugin(&midichanfilterDesc);
  155. }
  156. // -----------------------------------------------------------------------