DISTRHO Plugin Framework
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.

159 lines
4.5KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2025 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "DistrhoPluginInternal.hpp"
  17. #ifndef DISTRHO_NO_WARNINGS
  18. # if DISTRHO_PLUGIN_WANT_PARAMETER_VALUE_CHANGE_REQUEST
  19. # error Cannot use parameter value change request with MAPI
  20. # endif
  21. # if DISTRHO_PLUGIN_WANT_MIDI_INPUT || DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
  22. # error Cannot use MIDI with MAPI
  23. # endif
  24. # if DISTRHO_PLUGIN_WANT_FULL_STATE
  25. # error Cannot use full state with MAPI
  26. # endif
  27. # if DISTRHO_PLUGIN_WANT_TIMEPOS
  28. # error Cannot use time position with MAPI
  29. # endif
  30. #endif
  31. #include "mapi/mapi.h"
  32. START_NAMESPACE_DISTRHO
  33. // --------------------------------------------------------------------------------------------------------------------
  34. class PluginMAPI
  35. {
  36. public:
  37. PluginMAPI()
  38. : fPlugin(nullptr, nullptr, nullptr, nullptr)
  39. {
  40. fPlugin.activate();
  41. }
  42. ~PluginMAPI() noexcept
  43. {
  44. fPlugin.deactivate();
  45. }
  46. // ----------------------------------------------------------------------------------------------------------------
  47. void process(const float* const* ins, float** outs, unsigned int frames)
  48. {
  49. #if DISTRHO_PLUGIN_WANT_MIDI_INPUT
  50. fPlugin.run(const_cast<const float**>(ins), outs, frames, nullptr, 0);
  51. #else
  52. fPlugin.run(const_cast<const float**>(ins), outs, frames);
  53. #endif
  54. updateParameterOutputsAndTriggers();
  55. }
  56. void setParameter(unsigned int index, float value)
  57. {
  58. fPlugin.setParameterValue(index, fPlugin.getParameterRanges(index).getFixedValue(value));
  59. }
  60. void setState(const char* key, const char* value)
  61. {
  62. #if DISTRHO_PLUGIN_WANT_STATE
  63. fPlugin.setState(key, value);
  64. #else
  65. // unused
  66. (void)key;
  67. (void)value;
  68. #endif
  69. }
  70. // ----------------------------------------------------------------------------------------------------------------
  71. private:
  72. PluginExporter fPlugin;
  73. void updateParameterOutputsAndTriggers()
  74. {
  75. float value;
  76. for (uint32_t i = 0, count = fPlugin.getParameterCount(); i < count; ++i)
  77. {
  78. if ((fPlugin.getParameterHints(i) & kParameterIsTrigger) == kParameterIsTrigger)
  79. {
  80. // NOTE: no trigger support in MAPI, simulate it here
  81. value = fPlugin.getParameterRanges(i).def;
  82. if (d_isEqual(value, fPlugin.getParameterValue(i)))
  83. continue;
  84. fPlugin.setParameterValue(i, value);
  85. }
  86. }
  87. }
  88. };
  89. // --------------------------------------------------------------------------------------------------------------------
  90. MAPI_EXPORT
  91. mapi_handle_t mapi_create(unsigned int sample_rate)
  92. {
  93. if (d_nextBufferSize == 0)
  94. {
  95. #if defined(_DARKGLASS_DEVICE_PABLITO)
  96. d_nextBufferSize = 16;
  97. #elif defined(__MOD_DEVICES__)
  98. d_nextBufferSize = 128;
  99. #else
  100. d_nextBufferSize = 2048;
  101. #endif
  102. }
  103. d_nextSampleRate = sample_rate;
  104. return new PluginMAPI();
  105. }
  106. MAPI_EXPORT
  107. void mapi_process(mapi_handle_t handle,
  108. const float* const* ins,
  109. float** outs,
  110. unsigned int frames)
  111. {
  112. static_cast<PluginMAPI*>(handle)->process(ins, outs, frames);
  113. }
  114. MAPI_EXPORT
  115. void mapi_set_parameter(mapi_handle_t handle, unsigned int index, float value)
  116. {
  117. static_cast<PluginMAPI*>(handle)->setParameter(index, value);
  118. }
  119. MAPI_EXPORT
  120. void mapi_set_state(mapi_handle_t handle, const char* key, const char* value)
  121. {
  122. static_cast<PluginMAPI*>(handle)->setState(key, value);
  123. }
  124. MAPI_EXPORT
  125. void mapi_destroy(mapi_handle_t handle)
  126. {
  127. delete static_cast<PluginMAPI*>(handle);
  128. }
  129. // --------------------------------------------------------------------------------------------------------------------
  130. END_NAMESPACE_DISTRHO