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.

236 lines
6.3KB

  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2012-2018 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_OCTAVES = 0,
  23. PARAM_SEMITONES,
  24. PARAM_COUNT
  25. } MidiTransposeParams;
  26. typedef struct {
  27. const NativeHostDescriptor* host;
  28. int octaves;
  29. int semitones;
  30. } MidiTransposeHandle;
  31. // -----------------------------------------------------------------------
  32. static NativePluginHandle miditranspose_instantiate(const NativeHostDescriptor* host)
  33. {
  34. MidiTransposeHandle* const handle = (MidiTransposeHandle*)malloc(sizeof(MidiTransposeHandle));
  35. if (handle == NULL)
  36. return NULL;
  37. handle->host = host;
  38. handle->octaves = 0;
  39. handle->semitones = 0;
  40. return handle;
  41. }
  42. #define handlePtr ((MidiTransposeHandle*)handle)
  43. static void miditranspose_cleanup(NativePluginHandle handle)
  44. {
  45. free(handlePtr);
  46. }
  47. static uint32_t miditranspose_get_parameter_count(NativePluginHandle handle)
  48. {
  49. return PARAM_COUNT;
  50. // unused
  51. (void)handle;
  52. }
  53. static const NativeParameter* miditranspose_get_parameter_info(NativePluginHandle handle, uint32_t index)
  54. {
  55. if (index > PARAM_COUNT)
  56. return NULL;
  57. static NativeParameter param;
  58. param.hints = NATIVE_PARAMETER_IS_ENABLED|NATIVE_PARAMETER_IS_AUTOMABLE|NATIVE_PARAMETER_IS_INTEGER;
  59. param.unit = NULL;
  60. param.scalePointCount = 0;
  61. param.scalePoints = NULL;
  62. switch (index)
  63. {
  64. case PARAM_OCTAVES:
  65. param.name = "Octaves";
  66. param.ranges.def = 0.0f,
  67. param.ranges.min = -8.0f,
  68. param.ranges.max = 8.0f,
  69. param.ranges.step = 1.0f;
  70. param.ranges.stepSmall = 1.0f;
  71. param.ranges.stepLarge = 4.0f;
  72. break;
  73. case PARAM_SEMITONES:
  74. param.name = "Semitones";
  75. param.ranges.def = 0.0f,
  76. param.ranges.min = -12.0f,
  77. param.ranges.max = 12.0f,
  78. param.ranges.step = 1.0f;
  79. param.ranges.stepSmall = 1.0f;
  80. param.ranges.stepLarge = 4.0f;
  81. break;
  82. }
  83. return &param;
  84. // unused
  85. (void)handle;
  86. }
  87. static float miditranspose_get_parameter_value(NativePluginHandle handle, uint32_t index)
  88. {
  89. switch (index)
  90. {
  91. case PARAM_OCTAVES:
  92. return (float)handlePtr->octaves;
  93. case PARAM_SEMITONES:
  94. return (float)handlePtr->semitones;
  95. default:
  96. return 0.0f;
  97. }
  98. }
  99. static void miditranspose_set_parameter_value(NativePluginHandle handle, uint32_t index, float value)
  100. {
  101. switch (index)
  102. {
  103. case PARAM_OCTAVES:
  104. handlePtr->octaves = (int)value;
  105. break;
  106. case PARAM_SEMITONES:
  107. handlePtr->semitones = (int)value;
  108. break;
  109. }
  110. }
  111. static void miditranspose_process(NativePluginHandle handle, float** inBuffer, float** outBuffer, uint32_t frames, const NativeMidiEvent* midiEvents, uint32_t midiEventCount)
  112. {
  113. const NativeHostDescriptor* const host = handlePtr->host;
  114. const int octaves = handlePtr->octaves;
  115. const int semitones = handlePtr->semitones;
  116. NativeMidiEvent tmpEvent;
  117. for (uint32_t i=0; i < midiEventCount; ++i)
  118. {
  119. const NativeMidiEvent* const midiEvent = &midiEvents[i];
  120. const uint8_t status = (uint8_t)MIDI_GET_STATUS_FROM_DATA(midiEvent->data);
  121. if (status == MIDI_STATUS_NOTE_OFF || status == MIDI_STATUS_NOTE_ON)
  122. {
  123. int oldnote = midiEvent->data[1];
  124. int newnote = oldnote + octaves*12 + semitones;
  125. if (newnote < 0 || newnote >= MAX_MIDI_NOTE)
  126. continue;
  127. tmpEvent.port = midiEvent->port;
  128. tmpEvent.time = midiEvent->time;
  129. tmpEvent.data[0] = midiEvent->data[0];
  130. tmpEvent.data[1] = (uint8_t)newnote;
  131. tmpEvent.data[2] = midiEvent->data[2];
  132. tmpEvent.data[3] = midiEvent->data[3];
  133. tmpEvent.size = midiEvent->size;
  134. host->write_midi_event(host->handle, &tmpEvent);
  135. }
  136. else
  137. host->write_midi_event(host->handle, midiEvent);
  138. }
  139. return;
  140. // unused
  141. (void)inBuffer;
  142. (void)outBuffer;
  143. (void)frames;
  144. }
  145. #undef handlePtr
  146. // -----------------------------------------------------------------------
  147. static const NativePluginDescriptor miditransposeDesc = {
  148. .category = NATIVE_PLUGIN_CATEGORY_UTILITY,
  149. .hints = NATIVE_PLUGIN_IS_RTSAFE,
  150. .supports = NATIVE_PLUGIN_SUPPORTS_EVERYTHING,
  151. .audioIns = 0,
  152. .audioOuts = 0,
  153. .midiIns = 1,
  154. .midiOuts = 1,
  155. .paramIns = 2,
  156. .paramOuts = 0,
  157. .name = "MIDI Transpose",
  158. .label = "miditranspose",
  159. .maker = "falkTX",
  160. .copyright = "GNU GPL v2+",
  161. .instantiate = miditranspose_instantiate,
  162. .cleanup = miditranspose_cleanup,
  163. .get_parameter_count = miditranspose_get_parameter_count,
  164. .get_parameter_info = miditranspose_get_parameter_info,
  165. .get_parameter_value = miditranspose_get_parameter_value,
  166. .get_midi_program_count = NULL,
  167. .get_midi_program_info = NULL,
  168. .set_parameter_value = miditranspose_set_parameter_value,
  169. .set_midi_program = NULL,
  170. .set_custom_data = NULL,
  171. .ui_show = NULL,
  172. .ui_idle = NULL,
  173. .ui_set_parameter_value = NULL,
  174. .ui_set_midi_program = NULL,
  175. .ui_set_custom_data = NULL,
  176. .activate = NULL,
  177. .deactivate = NULL,
  178. .process = miditranspose_process,
  179. .get_state = NULL,
  180. .set_state = NULL,
  181. .dispatcher = NULL
  182. };
  183. // -----------------------------------------------------------------------
  184. void carla_register_native_plugin_miditranspose(void);
  185. void carla_register_native_plugin_miditranspose(void)
  186. {
  187. carla_register_native_plugin(&miditransposeDesc);
  188. }
  189. // -----------------------------------------------------------------------