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.

212 lines
6.6KB

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