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.

CarlaLadspaUtils.hpp 6.6KB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Carla LADSPA utils
  3. * Copyright (C) 2011-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. #ifndef CARLA_LADSPA_UTILS_HPP_INCLUDED
  18. #define CARLA_LADSPA_UTILS_HPP_INCLUDED
  19. #include "CarlaUtils.hpp"
  20. #include "ladspa/ladspa.h"
  21. #include "ladspa_rdf.hpp"
  22. #include <cmath>
  23. // -----------------------------------------------------------------------
  24. // Copy RDF object
  25. static inline
  26. const LADSPA_RDF_Descriptor* ladspa_rdf_dup(const LADSPA_RDF_Descriptor* const oldDescriptor)
  27. {
  28. CARLA_SAFE_ASSERT_RETURN(oldDescriptor != nullptr, nullptr);
  29. LADSPA_RDF_Descriptor* const newDescriptor(new LADSPA_RDF_Descriptor());
  30. newDescriptor->Type = oldDescriptor->Type;
  31. newDescriptor->UniqueID = oldDescriptor->UniqueID;
  32. newDescriptor->PortCount = oldDescriptor->PortCount;
  33. if (oldDescriptor->Title != nullptr)
  34. newDescriptor->Title = carla_strdup(oldDescriptor->Title);
  35. if (oldDescriptor->Creator != nullptr)
  36. newDescriptor->Creator = carla_strdup(oldDescriptor->Creator);
  37. if (newDescriptor->PortCount > 0)
  38. {
  39. newDescriptor->Ports = new LADSPA_RDF_Port[newDescriptor->PortCount];
  40. for (ulong i=0; i < newDescriptor->PortCount; ++i)
  41. {
  42. LADSPA_RDF_Port* const oldPort(&oldDescriptor->Ports[i]);
  43. LADSPA_RDF_Port* const newPort(&newDescriptor->Ports[i]);
  44. newPort->Type = oldPort->Type;
  45. newPort->Hints = oldPort->Hints;
  46. newPort->Default = oldPort->Default;
  47. newPort->Unit = oldPort->Unit;
  48. newPort->ScalePointCount = oldPort->ScalePointCount;
  49. if (oldPort->Label != nullptr)
  50. newPort->Label = carla_strdup(oldPort->Label);
  51. if (oldPort->ScalePointCount > 0)
  52. {
  53. newPort->ScalePoints = new LADSPA_RDF_ScalePoint[oldPort->ScalePointCount];
  54. for (ulong j=0; j < oldPort->ScalePointCount; ++j)
  55. {
  56. LADSPA_RDF_ScalePoint* const oldScalePoint(&oldPort->ScalePoints[j]);
  57. LADSPA_RDF_ScalePoint* const newScalePoint(&newPort->ScalePoints[j]);
  58. newScalePoint->Value = oldScalePoint->Value;
  59. if (oldScalePoint->Label != nullptr)
  60. newScalePoint->Label = carla_strdup(oldScalePoint->Label);
  61. }
  62. }
  63. }
  64. }
  65. return newDescriptor;
  66. }
  67. // -----------------------------------------------------------------------
  68. // Check if 2 ports match types
  69. static inline
  70. bool is_ladspa_port_good(const LADSPA_PortDescriptor port1, const LADSPA_PortDescriptor port2) noexcept
  71. {
  72. if (port1 == 0x0 || port2 == 0x0)
  73. return false;
  74. if (LADSPA_IS_PORT_INPUT(port1) && ! LADSPA_IS_PORT_INPUT(port2))
  75. return false;
  76. if (LADSPA_IS_PORT_OUTPUT(port1) && ! LADSPA_IS_PORT_OUTPUT(port2))
  77. return false;
  78. if (LADSPA_IS_PORT_CONTROL(port1) && ! LADSPA_IS_PORT_CONTROL(port2))
  79. return false;
  80. if (LADSPA_IS_PORT_AUDIO(port1) && ! LADSPA_IS_PORT_AUDIO(port2))
  81. return false;
  82. return true;
  83. }
  84. // -----------------------------------------------------------------------
  85. // Check if rdf data matches descriptor
  86. static inline
  87. bool is_ladspa_rdf_descriptor_valid(const LADSPA_RDF_Descriptor* const rdfDescriptor, const LADSPA_Descriptor* const descriptor) noexcept
  88. {
  89. if (rdfDescriptor == nullptr || descriptor == nullptr)
  90. return false;
  91. if (rdfDescriptor->UniqueID != descriptor->UniqueID)
  92. {
  93. carla_stderr("WARNING - Plugin has wrong UniqueID: %li != %li", rdfDescriptor->UniqueID, descriptor->UniqueID);
  94. return false;
  95. }
  96. if (rdfDescriptor->PortCount > descriptor->PortCount)
  97. {
  98. carla_stderr("WARNING - Plugin has RDF data, but invalid PortCount: %li > %li", rdfDescriptor->PortCount, descriptor->PortCount);
  99. return false;
  100. }
  101. for (ulong i=0; i < rdfDescriptor->PortCount; ++i)
  102. {
  103. if (! is_ladspa_port_good(rdfDescriptor->Ports[i].Type, descriptor->PortDescriptors[i]))
  104. {
  105. carla_stderr("WARNING - Plugin has RDF data, but invalid PortTypes: %i != %i", rdfDescriptor->Ports[i].Type, descriptor->PortDescriptors[i]);
  106. return false;
  107. }
  108. }
  109. return true;
  110. }
  111. // -----------------------------------------------------------------------
  112. // Get default control port value
  113. static inline
  114. LADSPA_Data get_default_ladspa_port_value(const LADSPA_PortRangeHintDescriptor hintDescriptor, const LADSPA_Data min, const LADSPA_Data max) noexcept
  115. {
  116. if (LADSPA_IS_HINT_HAS_DEFAULT(hintDescriptor))
  117. {
  118. switch (hintDescriptor & LADSPA_HINT_DEFAULT_MASK)
  119. {
  120. case LADSPA_HINT_DEFAULT_MINIMUM:
  121. return min;
  122. case LADSPA_HINT_DEFAULT_MAXIMUM:
  123. return max;
  124. case LADSPA_HINT_DEFAULT_0:
  125. return 0.0f;
  126. case LADSPA_HINT_DEFAULT_1:
  127. return 1.0f;
  128. case LADSPA_HINT_DEFAULT_100:
  129. return 100.0f;
  130. case LADSPA_HINT_DEFAULT_440:
  131. return 440.0f;
  132. case LADSPA_HINT_DEFAULT_LOW:
  133. if (LADSPA_IS_HINT_LOGARITHMIC(hintDescriptor))
  134. {
  135. try {
  136. return std::exp((std::log(min)*0.75f) + (std::log(max)*0.25f));
  137. } CARLA_SAFE_EXCEPTION("exp(log)");
  138. }
  139. return (min*0.75f) + (max*0.25f);
  140. case LADSPA_HINT_DEFAULT_MIDDLE:
  141. if (LADSPA_IS_HINT_LOGARITHMIC(hintDescriptor))
  142. {
  143. try {
  144. return std::sqrt(min*max);
  145. } CARLA_SAFE_EXCEPTION("sqrt");
  146. }
  147. return (min+max)/2;
  148. case LADSPA_HINT_DEFAULT_HIGH:
  149. if (LADSPA_IS_HINT_LOGARITHMIC(hintDescriptor))
  150. {
  151. try {
  152. return std::exp((std::log(min)*0.25f) + (std::log(max)*0.75f));
  153. } CARLA_SAFE_EXCEPTION("exp(log)");
  154. }
  155. return (min*0.25f) + (max*0.75f);
  156. }
  157. }
  158. // no default value
  159. if (min < 0.0f && max > 0.0f)
  160. return 0.0f;
  161. return min;
  162. }
  163. // -----------------------------------------------------------------------
  164. #endif // CARLA_LADSPA_UTILS_HPP_INCLUDED