Collection of tools useful for audio production
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.

206 lines
6.6KB

  1. /*
  2. * Carla LADSPA utils
  3. * Copyright (C) 2011-2012 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 newPort = &newDescriptor->Ports[i];
  46. newPort->Type = oldDescriptor->Ports[i].Type;
  47. newPort->Hints = oldDescriptor->Ports[i].Hints;
  48. newPort->Default = oldDescriptor->Ports[i].Default;
  49. newPort->Unit = oldDescriptor->Ports[i].Unit;
  50. newPort->ScalePointCount = oldDescriptor->Ports[i].ScalePointCount;
  51. if (oldDescriptor->Ports[i].Label)
  52. newPort->Label = strdup(oldDescriptor->Ports[i].Label);
  53. if (newPort->ScalePointCount > 0)
  54. {
  55. newPort->ScalePoints = new LADSPA_RDF_ScalePoint[newPort->ScalePointCount];
  56. for (unsigned long j=0; j < newPort->ScalePointCount; j++)
  57. {
  58. LADSPA_RDF_ScalePoint* const newScalePoint = &newPort->ScalePoints[j];
  59. newScalePoint->Value = oldDescriptor->Ports[i].ScalePoints[j].Value;
  60. if (oldDescriptor->Ports[i].ScalePoints[j].Label)
  61. newScalePoint->Label = strdup(oldDescriptor->Ports[i].ScalePoints[j].Label);
  62. }
  63. }
  64. }
  65. }
  66. return newDescriptor;
  67. }
  68. // ------------------------------------------------------------------------------------------------
  69. static inline
  70. bool is_ladspa_port_good(const LADSPA_PortDescriptor port1, const LADSPA_PortDescriptor port2)
  71. {
  72. if (LADSPA_IS_PORT_INPUT(port1) && ! LADSPA_IS_PORT_INPUT(port2))
  73. return false;
  74. if (LADSPA_IS_PORT_OUTPUT(port1) && ! LADSPA_IS_PORT_OUTPUT(port2))
  75. return false;
  76. if (LADSPA_IS_PORT_CONTROL(port1) && ! LADSPA_IS_PORT_CONTROL(port2))
  77. return false;
  78. if (LADSPA_IS_PORT_AUDIO(port1) && ! LADSPA_IS_PORT_AUDIO(port2))
  79. return false;
  80. return true;
  81. }
  82. static inline
  83. bool is_ladspa_rdf_descriptor_valid(const LADSPA_RDF_Descriptor* const rdfDescriptor, const LADSPA_Descriptor* const descriptor)
  84. {
  85. CARLA_ASSERT(rdfDescriptor);
  86. CARLA_ASSERT(descriptor);
  87. if (! rdfDescriptor)
  88. return false;
  89. if (! descriptor)
  90. return false;
  91. if (rdfDescriptor->UniqueID != descriptor->UniqueID)
  92. {
  93. qWarning("WARNING - Plugin has wrong UniqueID: %li != %li", rdfDescriptor->UniqueID, descriptor->UniqueID);
  94. return false;
  95. }
  96. if (rdfDescriptor->PortCount > descriptor->PortCount)
  97. {
  98. qWarning("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. qWarning("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. static inline
  112. LADSPA_Data get_default_ladspa_port_value(const LADSPA_PortRangeHintDescriptor hintDescriptor, const LADSPA_Data min, const LADSPA_Data max)
  113. {
  114. LADSPA_Data def;
  115. if (LADSPA_IS_HINT_HAS_DEFAULT(hintDescriptor))
  116. {
  117. switch (hintDescriptor & LADSPA_HINT_DEFAULT_MASK)
  118. {
  119. case LADSPA_HINT_DEFAULT_MINIMUM:
  120. def = min;
  121. break;
  122. case LADSPA_HINT_DEFAULT_MAXIMUM:
  123. def = max;
  124. break;
  125. case LADSPA_HINT_DEFAULT_0:
  126. def = 0.0f;
  127. break;
  128. case LADSPA_HINT_DEFAULT_1:
  129. def = 1.0f;
  130. break;
  131. case LADSPA_HINT_DEFAULT_100:
  132. def = 100.0f;
  133. break;
  134. case LADSPA_HINT_DEFAULT_440:
  135. def = 440.0f;
  136. break;
  137. case LADSPA_HINT_DEFAULT_LOW:
  138. if (LADSPA_IS_HINT_LOGARITHMIC(hintDescriptor))
  139. def = std::exp((std::log(min)*0.75f) + (std::log(max)*0.25f));
  140. else
  141. def = (min*0.75f) + (max*0.25f);
  142. break;
  143. case LADSPA_HINT_DEFAULT_MIDDLE:
  144. if (LADSPA_IS_HINT_LOGARITHMIC(hintDescriptor))
  145. def = std::sqrt(min*max);
  146. else
  147. def = (min+max)/2;
  148. break;
  149. case LADSPA_HINT_DEFAULT_HIGH:
  150. if (LADSPA_IS_HINT_LOGARITHMIC(hintDescriptor))
  151. def = std::exp((std::log(min)*0.25f) + (std::log(max)*0.75f));
  152. else
  153. def = (min*0.25f) + (max*0.75f);
  154. break;
  155. default:
  156. if (min < 0.0f && max > 0.0f)
  157. def = 0.0f;
  158. else
  159. def = min;
  160. break;
  161. }
  162. }
  163. else
  164. {
  165. // no default value
  166. if (min < 0.0f && max > 0.0f)
  167. def = 0.0f;
  168. else
  169. def = min;
  170. }
  171. return def;
  172. }
  173. // ------------------------------------------------------------------------------------------------
  174. #endif // CARLA_LADSPA_UTILS_HPP