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.

194 lines
5.0KB

  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 GPL.txt file
  16. */
  17. #include "CarlaNative.h"
  18. #include "CarlaMIDI.h"
  19. #include <stdlib.h>
  20. typedef struct _MidiTransposeHandle {
  21. HostDescriptor* host;
  22. int octaves;
  23. } MidiTransposeHandle;
  24. static PluginHandle midiTranspose_instantiate(const PluginDescriptor* _this_, HostDescriptor* host)
  25. {
  26. MidiTransposeHandle* const handle = (MidiTransposeHandle*)malloc(sizeof(MidiTransposeHandle));
  27. if (handle != NULL)
  28. {
  29. handle->host = host;
  30. handle->octaves = 0;
  31. return handle;
  32. }
  33. return NULL;
  34. // unused
  35. (void)_this_;
  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.name = "Octaves";
  54. param.unit = NULL;
  55. param.hints = PARAMETER_IS_ENABLED|PARAMETER_IS_AUTOMABLE|PARAMETER_IS_INTEGER;
  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, uint32_t midiEventCount, const MidiEvent* midiEvents)
  81. {
  82. HostDescriptor* const host = handlePtr->host;
  83. const int octaves = handlePtr->octaves;
  84. MidiEvent tmpEvent;
  85. for (uint32_t i=0; i < midiEventCount; i++)
  86. {
  87. const MidiEvent* const midiEvent = &midiEvents[i];
  88. const uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent->data);
  89. if (status == MIDI_STATUS_NOTE_OFF || status == MIDI_STATUS_NOTE_ON)
  90. {
  91. int note = midiEvent->data[1];
  92. int rnote = note + octaves*12;
  93. if (rnote < 0 || rnote >= MAX_MIDI_NOTE)
  94. continue;
  95. tmpEvent.port = midiEvent->port;
  96. tmpEvent.time = midiEvent->time;
  97. tmpEvent.data[0] = midiEvent->data[0];
  98. tmpEvent.data[1] = rnote;
  99. tmpEvent.data[2] = midiEvent->data[2];
  100. host->write_midi_event(host->handle, &tmpEvent);
  101. }
  102. else
  103. host->write_midi_event(host->handle, &midiEvents[i]);
  104. }
  105. return;
  106. // unused
  107. (void)inBuffer;
  108. (void)outBuffer;
  109. (void)frames;
  110. }
  111. #undef handlePtr
  112. // -----------------------------------------------------------------------
  113. static const PluginDescriptor midiTransposeDesc = {
  114. .category = PLUGIN_CATEGORY_NONE,
  115. .hints = PLUGIN_IS_RTSAFE,
  116. .audioIns = 0,
  117. .audioOuts = 0,
  118. .midiIns = 1,
  119. .midiOuts = 1,
  120. .parameterIns = 1,
  121. .parameterOuts = 0,
  122. .name = "MIDI Transpose",
  123. .label = "midiTranspose",
  124. .maker = "falkTX",
  125. .copyright = "GNU GPL v2+",
  126. .instantiate = midiTranspose_instantiate,
  127. .cleanup = midiTranspose_cleanup,
  128. .get_parameter_count = midiTranspose_get_parameter_count,
  129. .get_parameter_info = midiTranspose_get_parameter_info,
  130. .get_parameter_value = midiTranspose_get_parameter_value,
  131. .get_parameter_text = NULL,
  132. .get_midi_program_count = NULL,
  133. .get_midi_program_info = NULL,
  134. .set_parameter_value = midiTranspose_set_parameter_value,
  135. .set_midi_program = NULL,
  136. .set_custom_data = NULL,
  137. .ui_show = NULL,
  138. .ui_idle = NULL,
  139. .ui_set_parameter_value = NULL,
  140. .ui_set_midi_program = NULL,
  141. .ui_set_custom_data = NULL,
  142. .activate = NULL,
  143. .deactivate = NULL,
  144. .process = midiTranspose_process
  145. };
  146. // -----------------------------------------------------------------------
  147. void carla_register_native_plugin_midiTranspose()
  148. {
  149. carla_register_native_plugin(&midiTransposeDesc);
  150. }
  151. // -----------------------------------------------------------------------