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.

228 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 >= miditranspose_get_parameter_count(handle))
  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. case 1:
  102. handlePtr->semitones = (int)value;
  103. default:
  104. return;
  105. }
  106. }
  107. static void miditranspose_process(NativePluginHandle handle, float** inBuffer, float** outBuffer, uint32_t frames, const NativeMidiEvent* midiEvents, uint32_t midiEventCount)
  108. {
  109. const NativeHostDescriptor* const host = handlePtr->host;
  110. const int octaves = handlePtr->octaves;
  111. const int semitones = handlePtr->semitones;
  112. NativeMidiEvent tmpEvent;
  113. for (uint32_t i=0; i < midiEventCount; ++i)
  114. {
  115. const NativeMidiEvent* const midiEvent = &midiEvents[i];
  116. const uint8_t status = (uint8_t)MIDI_GET_STATUS_FROM_DATA(midiEvent->data);
  117. if (status == MIDI_STATUS_NOTE_OFF || status == MIDI_STATUS_NOTE_ON)
  118. {
  119. int oldnote = midiEvent->data[1];
  120. int newnote = oldnote + octaves*12 + semitones;
  121. if (newnote < 0 || newnote >= MAX_MIDI_NOTE)
  122. continue;
  123. tmpEvent.port = midiEvent->port;
  124. tmpEvent.time = midiEvent->time;
  125. tmpEvent.data[0] = midiEvent->data[0];
  126. tmpEvent.data[1] = (uint8_t)newnote;
  127. tmpEvent.data[2] = midiEvent->data[2];
  128. tmpEvent.data[3] = midiEvent->data[3];
  129. tmpEvent.size = midiEvent->size;
  130. host->write_midi_event(host->handle, &tmpEvent);
  131. }
  132. else
  133. host->write_midi_event(host->handle, midiEvent);
  134. }
  135. return;
  136. // unused
  137. (void)inBuffer;
  138. (void)outBuffer;
  139. (void)frames;
  140. }
  141. #undef handlePtr
  142. // -----------------------------------------------------------------------
  143. static const NativePluginDescriptor miditransposeDesc = {
  144. .category = NATIVE_PLUGIN_CATEGORY_UTILITY,
  145. .hints = NATIVE_PLUGIN_IS_RTSAFE,
  146. .supports = NATIVE_PLUGIN_SUPPORTS_EVERYTHING,
  147. .audioIns = 0,
  148. .audioOuts = 0,
  149. .midiIns = 1,
  150. .midiOuts = 1,
  151. .paramIns = 2,
  152. .paramOuts = 0,
  153. .name = "MIDI Transpose",
  154. .label = "miditranspose",
  155. .maker = "falkTX",
  156. .copyright = "GNU GPL v2+",
  157. .instantiate = miditranspose_instantiate,
  158. .cleanup = miditranspose_cleanup,
  159. .get_parameter_count = miditranspose_get_parameter_count,
  160. .get_parameter_info = miditranspose_get_parameter_info,
  161. .get_parameter_value = miditranspose_get_parameter_value,
  162. .get_midi_program_count = NULL,
  163. .get_midi_program_info = NULL,
  164. .set_parameter_value = miditranspose_set_parameter_value,
  165. .set_midi_program = NULL,
  166. .set_custom_data = NULL,
  167. .ui_show = NULL,
  168. .ui_idle = NULL,
  169. .ui_set_parameter_value = NULL,
  170. .ui_set_midi_program = NULL,
  171. .ui_set_custom_data = NULL,
  172. .activate = NULL,
  173. .deactivate = NULL,
  174. .process = miditranspose_process,
  175. .get_state = NULL,
  176. .set_state = NULL,
  177. .dispatcher = NULL
  178. };
  179. // -----------------------------------------------------------------------
  180. void carla_register_native_plugin_miditranspose(void);
  181. void carla_register_native_plugin_miditranspose(void)
  182. {
  183. carla_register_native_plugin(&miditransposeDesc);
  184. }
  185. // -----------------------------------------------------------------------