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.

215 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 modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * 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 COPYING 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. CARLA_ASSERT(rdfDescriptor);
  91. CARLA_ASSERT(descriptor);
  92. if (! (rdfDescriptor && descriptor))
  93. return false;
  94. if (rdfDescriptor->UniqueID != descriptor->UniqueID)
  95. {
  96. qWarning("WARNING - Plugin has wrong UniqueID: %li != %li", rdfDescriptor->UniqueID, descriptor->UniqueID);
  97. return false;
  98. }
  99. if (rdfDescriptor->PortCount > descriptor->PortCount)
  100. {
  101. qWarning("WARNING - Plugin has RDF data, but invalid PortCount: %li > %li", rdfDescriptor->PortCount, descriptor->PortCount);
  102. return false;
  103. }
  104. for (unsigned long i=0; i < rdfDescriptor->PortCount; i++)
  105. {
  106. if (! is_ladspa_port_good(rdfDescriptor->Ports[i].Type, descriptor->PortDescriptors[i]))
  107. {
  108. qWarning("WARNING - Plugin has RDF data, but invalid PortTypes: %i != %i", rdfDescriptor->Ports[i].Type, descriptor->PortDescriptors[i]);
  109. return false;
  110. }
  111. }
  112. return true;
  113. }
  114. // -------------------------------------------------
  115. // Get default control port value
  116. static inline
  117. LADSPA_Data get_default_ladspa_port_value(const LADSPA_PortRangeHintDescriptor hintDescriptor, const LADSPA_Data min, const LADSPA_Data max)
  118. {
  119. LADSPA_Data def;
  120. if (LADSPA_IS_HINT_HAS_DEFAULT(hintDescriptor))
  121. {
  122. switch (hintDescriptor & LADSPA_HINT_DEFAULT_MASK)
  123. {
  124. case LADSPA_HINT_DEFAULT_MINIMUM:
  125. def = min;
  126. break;
  127. case LADSPA_HINT_DEFAULT_MAXIMUM:
  128. def = max;
  129. break;
  130. case LADSPA_HINT_DEFAULT_0:
  131. def = 0.0f;
  132. break;
  133. case LADSPA_HINT_DEFAULT_1:
  134. def = 1.0f;
  135. break;
  136. case LADSPA_HINT_DEFAULT_100:
  137. def = 100.0f;
  138. break;
  139. case LADSPA_HINT_DEFAULT_440:
  140. def = 440.0f;
  141. break;
  142. case LADSPA_HINT_DEFAULT_LOW:
  143. if (LADSPA_IS_HINT_LOGARITHMIC(hintDescriptor))
  144. def = std::exp((std::log(min)*0.75f) + (std::log(max)*0.25f));
  145. else
  146. def = (min*0.75f) + (max*0.25f);
  147. break;
  148. case LADSPA_HINT_DEFAULT_MIDDLE:
  149. if (LADSPA_IS_HINT_LOGARITHMIC(hintDescriptor))
  150. def = std::sqrt(min*max);
  151. else
  152. def = (min+max)/2;
  153. break;
  154. case LADSPA_HINT_DEFAULT_HIGH:
  155. if (LADSPA_IS_HINT_LOGARITHMIC(hintDescriptor))
  156. def = std::exp((std::log(min)*0.25f) + (std::log(max)*0.75f));
  157. else
  158. def = (min*0.25f) + (max*0.75f);
  159. break;
  160. default:
  161. if (min < 0.0f && max > 0.0f)
  162. def = 0.0f;
  163. else
  164. def = min;
  165. break;
  166. }
  167. }
  168. else
  169. {
  170. // no default value
  171. if (min < 0.0f && max > 0.0f)
  172. def = 0.0f;
  173. else
  174. def = min;
  175. }
  176. return def;
  177. }
  178. // -------------------------------------------------
  179. #endif // __CARLA_LADSPA_UTILS_HPP__