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.

230 lines
7.2KB

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