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.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
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2012-2014 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 NativeHostDescriptor* host;
  23. int octaves;
  24. } MidiTransposeHandle;
  25. // -----------------------------------------------------------------------
  26. static NativePluginHandle miditranspose_instantiate(const NativeHostDescriptor* host)
  27. {
  28. MidiTransposeHandle* const handle = (MidiTransposeHandle*)malloc(sizeof(MidiTransposeHandle));
  29. if (handle == NULL)
  30. return NULL;
  31. handle->host = host;
  32. handle->octaves = 0;
  33. return handle;
  34. }
  35. #define handlePtr ((MidiTransposeHandle*)handle)
  36. static void miditranspose_cleanup(NativePluginHandle handle)
  37. {
  38. free(handlePtr);
  39. }
  40. static uint32_t miditranspose_get_parameter_count(NativePluginHandle handle)
  41. {
  42. return 1;
  43. // unused
  44. (void)handle;
  45. }
  46. static const NativeParameter* miditranspose_get_parameter_info(NativePluginHandle handle, uint32_t index)
  47. {
  48. if (index != 0)
  49. return NULL;
  50. static NativeParameter 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(NativePluginHandle 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(NativePluginHandle handle, uint32_t index, float value)
  73. {
  74. if (index != 0)
  75. return;
  76. handlePtr->octaves = (int)value;
  77. }
  78. static void miditranspose_process(NativePluginHandle handle, float** inBuffer, float** outBuffer, uint32_t frames, const NativeMidiEvent* midiEvents, uint32_t midiEventCount)
  79. {
  80. const NativeHostDescriptor* const host = handlePtr->host;
  81. const int octaves = handlePtr->octaves;
  82. NativeMidiEvent tmpEvent;
  83. for (uint32_t i=0; i < midiEventCount; ++i)
  84. {
  85. const NativeMidiEvent* const midiEvent = &midiEvents[i];
  86. const uint8_t status = (uint8_t)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] = (uint8_t)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 NativePluginDescriptor miditransposeDesc = {
  114. .category = PLUGIN_CATEGORY_UTILITY,
  115. .hints = PLUGIN_IS_RTSAFE,
  116. .supports = PLUGIN_SUPPORTS_EVERYTHING,
  117. .audioIns = 0,
  118. .audioOuts = 0,
  119. .midiIns = 1,
  120. .midiOuts = 1,
  121. .paramIns = 1,
  122. .paramOuts = 0,
  123. .name = "MIDI Transpose",
  124. .label = "miditranspose",
  125. .maker = "falkTX",
  126. .copyright = "GNU GPL v2+",
  127. .instantiate = miditranspose_instantiate,
  128. .cleanup = miditranspose_cleanup,
  129. .get_parameter_count = miditranspose_get_parameter_count,
  130. .get_parameter_info = miditranspose_get_parameter_info,
  131. .get_parameter_value = miditranspose_get_parameter_value,
  132. .get_parameter_text = NULL,
  133. .get_midi_program_count = NULL,
  134. .get_midi_program_info = NULL,
  135. .set_parameter_value = miditranspose_set_parameter_value,
  136. .set_midi_program = NULL,
  137. .set_custom_data = NULL,
  138. .ui_show = NULL,
  139. .ui_idle = NULL,
  140. .ui_set_parameter_value = NULL,
  141. .ui_set_midi_program = NULL,
  142. .ui_set_custom_data = NULL,
  143. .activate = NULL,
  144. .deactivate = NULL,
  145. .process = miditranspose_process,
  146. .get_state = NULL,
  147. .set_state = NULL,
  148. .dispatcher = NULL
  149. };
  150. // -----------------------------------------------------------------------
  151. void carla_register_native_plugin_miditranspose(void);
  152. void carla_register_native_plugin_miditranspose(void)
  153. {
  154. carla_register_native_plugin(&miditransposeDesc);
  155. }
  156. // -----------------------------------------------------------------------