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.

242 lines
7.1KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2022 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. START_NAMESPACE_DISTRHO
  18. /* ------------------------------------------------------------------------------------------------------------
  19. * Static data, see DistrhoPluginInternal.hpp */
  20. uint32_t d_nextBufferSize = 0;
  21. double d_nextSampleRate = 0.0;
  22. const char* d_nextBundlePath = nullptr;
  23. bool d_nextPluginIsDummy = false;
  24. bool d_nextCanRequestParameterValueChanges = false;
  25. /* ------------------------------------------------------------------------------------------------------------
  26. * Static fallback data, see DistrhoPluginInternal.hpp */
  27. const String PluginExporter::sFallbackString;
  28. /* */ AudioPortWithBusId PluginExporter::sFallbackAudioPort;
  29. const ParameterRanges PluginExporter::sFallbackRanges;
  30. const ParameterEnumerationValues PluginExporter::sFallbackEnumValues;
  31. const PortGroupWithId PluginExporter::sFallbackPortGroup;
  32. /* ------------------------------------------------------------------------------------------------------------
  33. * Plugin */
  34. Plugin::Plugin(uint32_t parameterCount, uint32_t programCount, uint32_t stateCount)
  35. : pData(new PrivateData())
  36. {
  37. #if DISTRHO_PLUGIN_NUM_INPUTS+DISTRHO_PLUGIN_NUM_OUTPUTS > 0
  38. pData->audioPorts = new AudioPortWithBusId[DISTRHO_PLUGIN_NUM_INPUTS+DISTRHO_PLUGIN_NUM_OUTPUTS];
  39. #endif
  40. #ifdef DPF_ABORT_ON_ERROR
  41. # define DPF_ABORT abort();
  42. #else
  43. # define DPF_ABORT
  44. #endif
  45. if (parameterCount > 0)
  46. {
  47. pData->parameterCount = parameterCount;
  48. pData->parameters = new Parameter[parameterCount];
  49. }
  50. if (programCount > 0)
  51. {
  52. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  53. pData->programCount = programCount;
  54. pData->programNames = new String[programCount];
  55. #else
  56. d_stderr2("DPF warning: Plugins with programs must define `DISTRHO_PLUGIN_WANT_PROGRAMS` to 1");
  57. DPF_ABORT
  58. #endif
  59. }
  60. if (stateCount > 0)
  61. {
  62. #if DISTRHO_PLUGIN_WANT_STATE
  63. pData->stateCount = stateCount;
  64. pData->states = new State[stateCount];
  65. #else
  66. d_stderr2("DPF warning: Plugins with state must define `DISTRHO_PLUGIN_WANT_STATE` to 1");
  67. DPF_ABORT
  68. #endif
  69. }
  70. #undef DPF_ABORT
  71. }
  72. Plugin::~Plugin()
  73. {
  74. delete pData;
  75. }
  76. /* ------------------------------------------------------------------------------------------------------------
  77. * Host state */
  78. uint32_t Plugin::getBufferSize() const noexcept
  79. {
  80. return pData->bufferSize;
  81. }
  82. double Plugin::getSampleRate() const noexcept
  83. {
  84. return pData->sampleRate;
  85. }
  86. const char* Plugin::getBundlePath() const noexcept
  87. {
  88. return pData->bundlePath;
  89. }
  90. bool Plugin::isDummyInstance() const noexcept
  91. {
  92. return pData->isDummy;
  93. }
  94. #if DISTRHO_PLUGIN_WANT_TIMEPOS
  95. const TimePosition& Plugin::getTimePosition() const noexcept
  96. {
  97. return pData->timePosition;
  98. }
  99. #endif
  100. #if DISTRHO_PLUGIN_WANT_LATENCY
  101. void Plugin::setLatency(uint32_t frames) noexcept
  102. {
  103. pData->latency = frames;
  104. }
  105. #endif
  106. #if DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
  107. bool Plugin::writeMidiEvent(const MidiEvent& midiEvent) noexcept
  108. {
  109. return pData->writeMidiCallback(midiEvent);
  110. }
  111. #endif
  112. #if DISTRHO_PLUGIN_WANT_PARAMETER_VALUE_CHANGE_REQUEST
  113. bool Plugin::canRequestParameterValueChanges() const noexcept
  114. {
  115. return pData->canRequestParameterValueChanges;
  116. }
  117. bool Plugin::requestParameterValueChange(const uint32_t index, const float value) noexcept
  118. {
  119. return pData->requestParameterValueChangeCallback(index, value);
  120. }
  121. #endif
  122. #if DISTRHO_PLUGIN_WANT_STATE
  123. bool Plugin::updateStateValue(const char* const key, const char* const value) noexcept
  124. {
  125. return pData->updateStateValueCallback(key, value);
  126. }
  127. #endif
  128. /* ------------------------------------------------------------------------------------------------------------
  129. * Init */
  130. void Plugin::initAudioPort(bool input, uint32_t index, AudioPort& port)
  131. {
  132. if (port.hints & kAudioPortIsCV)
  133. {
  134. port.name = input ? "CV Input " : "CV Output ";
  135. port.name += String(index+1);
  136. port.symbol = input ? "cv_in_" : "cv_out_";
  137. port.symbol += String(index+1);
  138. }
  139. else
  140. {
  141. port.name = input ? "Audio Input " : "Audio Output ";
  142. port.name += String(index+1);
  143. port.symbol = input ? "audio_in_" : "audio_out_";
  144. port.symbol += String(index+1);
  145. }
  146. }
  147. void Plugin::initParameter(uint32_t, Parameter&) {}
  148. void Plugin::initPortGroup(const uint32_t groupId, PortGroup& portGroup)
  149. {
  150. fillInPredefinedPortGroupData(groupId, portGroup);
  151. }
  152. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  153. void Plugin::initProgramName(uint32_t, String&) {}
  154. #endif
  155. #if DISTRHO_PLUGIN_WANT_STATE
  156. void Plugin::initState(const uint32_t index, State& state)
  157. {
  158. uint hints = 0x0;
  159. String stateKey, defaultStateValue;
  160. #if defined(__clang__)
  161. #pragma clang diagnostic push
  162. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  163. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  164. #pragma GCC diagnostic push
  165. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  166. #endif
  167. initState(index, stateKey, defaultStateValue);
  168. if (isStateFile(index))
  169. hints = kStateIsFilenamePath;
  170. #if defined(__clang__)
  171. #pragma clang diagnostic pop
  172. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  173. #pragma GCC diagnostic pop
  174. #endif
  175. state.hints = hints;
  176. state.key = stateKey;
  177. state.label = stateKey;
  178. state.defaultValue = defaultStateValue;
  179. }
  180. #endif
  181. /* ------------------------------------------------------------------------------------------------------------
  182. * Init */
  183. float Plugin::getParameterValue(uint32_t) const { return 0.0f; }
  184. void Plugin::setParameterValue(uint32_t, float) {}
  185. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  186. void Plugin::loadProgram(uint32_t) {}
  187. #endif
  188. #if DISTRHO_PLUGIN_WANT_FULL_STATE
  189. String Plugin::getState(const char*) const { return String(); }
  190. #endif
  191. #if DISTRHO_PLUGIN_WANT_STATE
  192. void Plugin::setState(const char*, const char*) {}
  193. #endif
  194. /* ------------------------------------------------------------------------------------------------------------
  195. * Callbacks (optional) */
  196. void Plugin::bufferSizeChanged(uint32_t) {}
  197. void Plugin::sampleRateChanged(double) {}
  198. // -----------------------------------------------------------------------------------------------------------
  199. END_NAMESPACE_DISTRHO