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.

199 lines
5.2KB

  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. return NULL;
  29. handle->host = host;
  30. handle->octaves = 0;
  31. return handle;
  32. // unused
  33. (void)_this_;
  34. }
  35. #define handlePtr ((MidiTransposeHandle*)handle)
  36. static void midiTranspose_cleanup(PluginHandle handle)
  37. {
  38. free(handlePtr);
  39. }
  40. static uint32_t midiTranspose_get_parameter_count(PluginHandle handle)
  41. {
  42. return 1;
  43. // unused
  44. (void)handle;
  45. }
  46. const Parameter* midiTranspose_get_parameter_info(PluginHandle handle, uint32_t index)
  47. {
  48. if (index != 0)
  49. return NULL;
  50. static Parameter param;
  51. param.name = "Octaves";
  52. param.unit = NULL;
  53. param.hints = PARAMETER_IS_ENABLED|PARAMETER_IS_AUTOMABLE|PARAMETER_IS_INTEGER;
  54. param.ranges.def = 0.0f;
  55. param.ranges.min = -8.0f;
  56. param.ranges.max = 8.0f;
  57. param.ranges.step = 1.0f;
  58. param.ranges.stepSmall = 1.0f;
  59. param.ranges.stepLarge = 1.0f;
  60. param.scalePointCount = 0;
  61. param.scalePoints = NULL;
  62. return &param;
  63. // unused
  64. (void)handle;
  65. }
  66. static float midiTranspose_get_parameter_value(PluginHandle handle, uint32_t index)
  67. {
  68. if (index != 0)
  69. return 0.0f;
  70. return (float)handlePtr->octaves;
  71. }
  72. static void midiTranspose_set_parameter_value(PluginHandle handle, uint32_t index, float value)
  73. {
  74. if (index != 0)
  75. return;
  76. handlePtr->octaves = (int)value;
  77. }
  78. static void midiTranspose_process(PluginHandle handle, float** inBuffer, float** outBuffer, uint32_t frames, uint32_t midiEventCount, const MidiEvent* midiEvents)
  79. {
  80. HostDescriptor* const host = handlePtr->host;
  81. const int octaves = handlePtr->octaves;
  82. MidiEvent tmpEvent;
  83. for (uint32_t i=0; i < midiEventCount; ++i)
  84. {
  85. const MidiEvent* const midiEvent = &midiEvents[i];
  86. const uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent->data);
  87. if (status == MIDI_STATUS_NOTE_OFF || status == MIDI_STATUS_NOTE_ON)
  88. {
  89. int oldnote = midiEvent->data[1];
  90. int newnote = oldnote + octaves*12;
  91. if (newnote < 0 || newnote >= MAX_MIDI_NOTE)
  92. continue;
  93. tmpEvent.port = midiEvent->port;
  94. tmpEvent.time = midiEvent->time;
  95. tmpEvent.data[0] = midiEvent->data[0];
  96. tmpEvent.data[1] = newnote;
  97. tmpEvent.data[2] = midiEvent->data[2];
  98. tmpEvent.data[3] = midiEvent->data[3];
  99. tmpEvent.size = midiEvent->size;
  100. host->write_midi_event(host->handle, &tmpEvent);
  101. }
  102. else
  103. host->write_midi_event(host->handle, midiEvent);
  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_UTILITY,
  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. .get_state = NULL,
  146. .set_state = NULL,
  147. .dispatcher = NULL
  148. };
  149. // -----------------------------------------------------------------------
  150. void carla_register_native_plugin_midiTranspose()
  151. {
  152. carla_register_native_plugin(&midiTransposeDesc);
  153. }
  154. // -----------------------------------------------------------------------