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-transpose.c 6.5KB

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