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.

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