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 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. tmpEvent.data[3] = midiEvent->data[3];
  101. tmpEvent.size = midiEvent->size;
  102. host->write_midi_event(host->handle, &tmpEvent);
  103. }
  104. else
  105. host->write_midi_event(host->handle, &midiEvents[i]);
  106. }
  107. return;
  108. // unused
  109. (void)inBuffer;
  110. (void)outBuffer;
  111. (void)frames;
  112. }
  113. #undef handlePtr
  114. // -----------------------------------------------------------------------
  115. static const PluginDescriptor midiTransposeDesc = {
  116. .category = PLUGIN_CATEGORY_NONE,
  117. .hints = PLUGIN_IS_RTSAFE,
  118. .audioIns = 0,
  119. .audioOuts = 0,
  120. .midiIns = 1,
  121. .midiOuts = 1,
  122. .parameterIns = 1,
  123. .parameterOuts = 0,
  124. .name = "MIDI Transpose",
  125. .label = "midiTranspose",
  126. .maker = "falkTX",
  127. .copyright = "GNU GPL v2+",
  128. .instantiate = midiTranspose_instantiate,
  129. .cleanup = midiTranspose_cleanup,
  130. .get_parameter_count = midiTranspose_get_parameter_count,
  131. .get_parameter_info = midiTranspose_get_parameter_info,
  132. .get_parameter_value = midiTranspose_get_parameter_value,
  133. .get_parameter_text = NULL,
  134. .get_midi_program_count = NULL,
  135. .get_midi_program_info = NULL,
  136. .set_parameter_value = midiTranspose_set_parameter_value,
  137. .set_midi_program = NULL,
  138. .set_custom_data = NULL,
  139. .ui_show = NULL,
  140. .ui_idle = NULL,
  141. .ui_set_parameter_value = NULL,
  142. .ui_set_midi_program = NULL,
  143. .ui_set_custom_data = NULL,
  144. .activate = NULL,
  145. .deactivate = NULL,
  146. .process = midiTranspose_process,
  147. .get_state = NULL,
  148. .set_state = NULL,
  149. .dispatcher = NULL
  150. };
  151. // -----------------------------------------------------------------------
  152. void carla_register_native_plugin_midiTranspose()
  153. {
  154. carla_register_native_plugin(&midiTransposeDesc);
  155. }
  156. // -----------------------------------------------------------------------