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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Carla LADSPA utils
  3. * Copyright (C) 2011-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. #ifndef __CARLA_LADSPA_UTILS_HPP__
  18. #define __CARLA_LADSPA_UTILS_HPP__
  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_ASSERT(oldDescriptor != nullptr);
  29. if (oldDescriptor == nullptr)
  30. return nullptr;
  31. LADSPA_RDF_Descriptor* const newDescriptor = new LADSPA_RDF_Descriptor;
  32. newDescriptor->Type = oldDescriptor->Type;
  33. newDescriptor->UniqueID = oldDescriptor->UniqueID;
  34. newDescriptor->PortCount = oldDescriptor->PortCount;
  35. if (oldDescriptor->Title != nullptr)
  36. newDescriptor->Title = carla_strdup(oldDescriptor->Title);
  37. if (oldDescriptor->Creator != nullptr)
  38. newDescriptor->Creator = carla_strdup(oldDescriptor->Creator);
  39. if (newDescriptor->PortCount > 0)
  40. {
  41. newDescriptor->Ports = new LADSPA_RDF_Port[newDescriptor->PortCount];
  42. for (unsigned long i=0; i < newDescriptor->PortCount; i++)
  43. {
  44. LADSPA_RDF_Port* const oldPort = &oldDescriptor->Ports[i];
  45. LADSPA_RDF_Port* const newPort = &newDescriptor->Ports[i];
  46. newPort->Type = oldPort->Type;
  47. newPort->Hints = oldPort->Hints;
  48. newPort->Default = oldPort->Default;
  49. newPort->Unit = oldPort->Unit;
  50. newPort->ScalePointCount = oldPort->ScalePointCount;
  51. if (oldPort->Label != nullptr)
  52. newPort->Label = carla_strdup(oldPort->Label);
  53. if (oldPort->ScalePointCount > 0)
  54. {
  55. newPort->ScalePoints = new LADSPA_RDF_ScalePoint[oldPort->ScalePointCount];
  56. for (unsigned long j=0; j < oldPort->ScalePointCount; j++)
  57. {
  58. LADSPA_RDF_ScalePoint* const oldScalePoint = &oldPort->ScalePoints[j];
  59. LADSPA_RDF_ScalePoint* const newScalePoint = &newPort->ScalePoints[j];
  60. newScalePoint->Value = oldScalePoint->Value;
  61. if (oldScalePoint->Label != nullptr)
  62. newScalePoint->Label = carla_strdup(oldScalePoint->Label);
  63. }
  64. }
  65. }
  66. }
  67. return newDescriptor;
  68. }
  69. // -------------------------------------------------
  70. // Check if 2 ports match types
  71. static inline
  72. bool is_ladspa_port_good(const LADSPA_PortDescriptor port1, const LADSPA_PortDescriptor port2)
  73. {
  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)
  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 (unsigned long 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)
  115. {
  116. LADSPA_Data def;
  117. if (LADSPA_IS_HINT_HAS_DEFAULT(hintDescriptor))
  118. {
  119. switch (hintDescriptor & LADSPA_HINT_DEFAULT_MASK)
  120. {
  121. case LADSPA_HINT_DEFAULT_MINIMUM:
  122. def = min;
  123. break;
  124. case LADSPA_HINT_DEFAULT_MAXIMUM:
  125. def = max;
  126. break;
  127. case LADSPA_HINT_DEFAULT_0:
  128. def = 0.0f;
  129. break;
  130. case LADSPA_HINT_DEFAULT_1:
  131. def = 1.0f;
  132. break;
  133. case LADSPA_HINT_DEFAULT_100:
  134. def = 100.0f;
  135. break;
  136. case LADSPA_HINT_DEFAULT_440:
  137. def = 440.0f;
  138. break;
  139. case LADSPA_HINT_DEFAULT_LOW:
  140. if (LADSPA_IS_HINT_LOGARITHMIC(hintDescriptor))
  141. def = std::exp((std::log(min)*0.75f) + (std::log(max)*0.25f));
  142. else
  143. def = (min*0.75f) + (max*0.25f);
  144. break;
  145. case LADSPA_HINT_DEFAULT_MIDDLE:
  146. if (LADSPA_IS_HINT_LOGARITHMIC(hintDescriptor))
  147. def = std::sqrt(min*max);
  148. else
  149. def = (min+max)/2;
  150. break;
  151. case LADSPA_HINT_DEFAULT_HIGH:
  152. if (LADSPA_IS_HINT_LOGARITHMIC(hintDescriptor))
  153. def = std::exp((std::log(min)*0.25f) + (std::log(max)*0.75f));
  154. else
  155. def = (min*0.25f) + (max*0.75f);
  156. break;
  157. default:
  158. if (min < 0.0f && max > 0.0f)
  159. def = 0.0f;
  160. else
  161. def = min;
  162. break;
  163. }
  164. }
  165. else
  166. {
  167. // no default value
  168. if (min < 0.0f && max > 0.0f)
  169. def = 0.0f;
  170. else
  171. def = min;
  172. }
  173. return def;
  174. }
  175. // -------------------------------------------------
  176. #endif // __CARLA_LADSPA_UTILS_HPP__