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.

205 lines
5.5KB

  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2012-2013 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 PluginHostDescriptor* host;
  23. MappedValue map_midi;
  24. int octaves;
  25. } MidiTransposeHandle;
  26. // -----------------------------------------------------------------------
  27. static PluginHandle midiTranspose_instantiate(const PluginHostDescriptor* host)
  28. {
  29. MidiTransposeHandle* const handle = (MidiTransposeHandle*)malloc(sizeof(MidiTransposeHandle));
  30. if (handle == NULL)
  31. return NULL;
  32. handle->host = host;
  33. handle->map_midi = host->map_value(host->handle, EVENT_TYPE_MIDI);
  34. handle->octaves = 0;
  35. return handle;
  36. }
  37. #define handlePtr ((MidiTransposeHandle*)handle)
  38. static void midiTranspose_cleanup(PluginHandle handle)
  39. {
  40. free(handlePtr);
  41. }
  42. static uint32_t midiTranspose_get_parameter_count(PluginHandle handle)
  43. {
  44. return 1;
  45. // unused
  46. (void)handle;
  47. }
  48. const Parameter* midiTranspose_get_parameter_info(PluginHandle handle, uint32_t index)
  49. {
  50. if (index != 0)
  51. return NULL;
  52. static Parameter param;
  53. param.hints = PARAMETER_IS_ENABLED ":" PARAMETER_IS_AUTOMABLE ":" PARAMETER_IS_INTEGER;
  54. param.name = "Octaves";
  55. param.unit = NULL;
  56. param.ranges.def = 0.0f;
  57. param.ranges.min = -8.0f;
  58. param.ranges.max = 8.0f;
  59. param.ranges.step = 1.0f;
  60. param.ranges.stepSmall = 1.0f;
  61. param.ranges.stepLarge = 1.0f;
  62. param.scalePointCount = 0;
  63. param.scalePoints = NULL;
  64. return &param;
  65. // unused
  66. (void)handle;
  67. }
  68. static float midiTranspose_get_parameter_value(PluginHandle handle, uint32_t index)
  69. {
  70. if (index != 0)
  71. return 0.0f;
  72. return (float)handlePtr->octaves;
  73. }
  74. static void midiTranspose_set_parameter_value(PluginHandle handle, uint32_t index, float value)
  75. {
  76. if (index != 0)
  77. return;
  78. handlePtr->octaves = (int)value;
  79. }
  80. static void midiTranspose_process(PluginHandle handle, float** inBuffer, float** outBuffer, uint32_t frames, const Event* events, uint32_t eventCount)
  81. {
  82. const PluginHostDescriptor* const host = handlePtr->host;
  83. const MappedValue map_midi = handlePtr->map_midi;
  84. const int octaves = handlePtr->octaves;
  85. MidiEvent tmpEvent;
  86. tmpEvent.e.type = map_midi;
  87. for (uint32_t i=0; i < eventCount; ++i)
  88. {
  89. if (events[i].type != map_midi)
  90. continue;
  91. const MidiEvent* const midiEvent = (const MidiEvent*)&events[i];
  92. const uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent->data);
  93. if (status == MIDI_STATUS_NOTE_OFF || status == MIDI_STATUS_NOTE_ON)
  94. {
  95. int oldnote = midiEvent->data[1];
  96. int newnote = oldnote + octaves*12;
  97. if (newnote < 0 || newnote >= MAX_MIDI_NOTE)
  98. continue;
  99. tmpEvent.e.frame = midiEvent->e.frame;
  100. tmpEvent.port = midiEvent->port;
  101. tmpEvent.data[0] = midiEvent->data[0];
  102. tmpEvent.data[1] = newnote;
  103. tmpEvent.data[2] = midiEvent->data[2];
  104. tmpEvent.data[3] = midiEvent->data[3];
  105. tmpEvent.size = midiEvent->size;
  106. host->write_event(host->handle, (const Event*)&tmpEvent);
  107. }
  108. else
  109. host->write_event(host->handle, &events[i]);
  110. }
  111. return;
  112. // unused
  113. (void)inBuffer;
  114. (void)outBuffer;
  115. (void)frames;
  116. }
  117. #undef handlePtr
  118. // -----------------------------------------------------------------------
  119. static const PluginDescriptor midiTransposeDesc = {
  120. .api = CARLA_NATIVE_API_VERSION,
  121. .categories = PLUGIN_CATEGORY_UTILITY ":midi",
  122. .features = "rtsafe",
  123. .supports = PLUGIN_SUPPORTS_EVERYTHING,
  124. .metadata = NULL,
  125. .audioIns = 0,
  126. .audioOuts = 0,
  127. .midiIns = 1,
  128. .midiOuts = 1,
  129. .paramIns = 1,
  130. .paramOuts = 0,
  131. .author = "falkTX",
  132. .name = "MIDI Transpose",
  133. .label = "midiTranspose",
  134. .copyright = "GNU GPL v2+",
  135. .version = 0x1000,
  136. .instantiate = midiTranspose_instantiate,
  137. .cleanup = midiTranspose_cleanup,
  138. .get_parameter_count = midiTranspose_get_parameter_count,
  139. .get_parameter_info = midiTranspose_get_parameter_info,
  140. .get_parameter_value = midiTranspose_get_parameter_value,
  141. .get_parameter_text = NULL,
  142. .set_parameter_value = midiTranspose_set_parameter_value,
  143. .get_midi_program_count = NULL,
  144. .get_midi_program_info = NULL,
  145. .set_midi_program = NULL,
  146. .idle = NULL,
  147. .get_state = NULL,
  148. .set_state = NULL,
  149. .activate = NULL,
  150. .deactivate = NULL,
  151. .process = midiTranspose_process,
  152. .dispatcher = NULL
  153. };
  154. // -----------------------------------------------------------------------
  155. void carla_register_native_plugin_midiTranspose()
  156. {
  157. carla_register_native_plugin(&midiTransposeDesc);
  158. }
  159. // -----------------------------------------------------------------------