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.

196 lines
6.3KB

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