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.

229 lines
6.3KB

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