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.

233 lines
7.3KB

  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. #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. Q_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. // Delete object
  66. static inline
  67. void ladspa_rdf_free(const LADSPA_RDF_Descriptor* const rdf_descriptor)
  68. {
  69. if (rdf_descriptor->Title)
  70. free((void*)rdf_descriptor->Title);
  71. if (rdf_descriptor->Creator)
  72. free((void*)rdf_descriptor->Creator);
  73. if (rdf_descriptor->PortCount > 0)
  74. {
  75. for (unsigned long i=0; i < rdf_descriptor->PortCount; i++)
  76. {
  77. const LADSPA_RDF_Port* const Port = &rdf_descriptor->Ports[i];
  78. if (Port->Label)
  79. free((void*)Port->Label);
  80. if (Port->ScalePointCount > 0)
  81. {
  82. for (unsigned long j=0; j < Port->ScalePointCount; j++)
  83. {
  84. const LADSPA_RDF_ScalePoint* const ScalePoint = &Port->ScalePoints[j];
  85. if (ScalePoint->Label)
  86. free((void*)ScalePoint->Label);
  87. }
  88. delete[] Port->ScalePoints;
  89. }
  90. }
  91. delete[] rdf_descriptor->Ports;
  92. }
  93. delete rdf_descriptor;
  94. }
  95. // ------------------------------------------------------------------------------------------------
  96. static inline
  97. bool is_ladspa_port_good(const LADSPA_PortDescriptor port1, const LADSPA_PortDescriptor port2)
  98. {
  99. if (LADSPA_IS_PORT_INPUT(port1) && ! LADSPA_IS_PORT_INPUT(port2))
  100. return false;
  101. if (LADSPA_IS_PORT_OUTPUT(port1) && ! LADSPA_IS_PORT_OUTPUT(port2))
  102. return false;
  103. if (LADSPA_IS_PORT_CONTROL(port1) && ! LADSPA_IS_PORT_CONTROL(port2))
  104. return false;
  105. if (LADSPA_IS_PORT_AUDIO(port1) && ! LADSPA_IS_PORT_AUDIO(port2))
  106. return false;
  107. return true;
  108. }
  109. static inline
  110. bool is_ladspa_rdf_descriptor_valid(const LADSPA_RDF_Descriptor* const rdf_descriptor, const LADSPA_Descriptor* const descriptor)
  111. {
  112. Q_ASSERT(descriptor);
  113. if (! rdf_descriptor)
  114. return false;
  115. if (rdf_descriptor->UniqueID != descriptor->UniqueID)
  116. {
  117. qWarning("WARNING - Plugin has wrong UniqueID: %li != %li", rdf_descriptor->UniqueID, descriptor->UniqueID);
  118. return false;
  119. }
  120. if (rdf_descriptor->PortCount > descriptor->PortCount)
  121. {
  122. qWarning("WARNING - Plugin has RDF data, but invalid PortCount: %li > %li", rdf_descriptor->PortCount, descriptor->PortCount);
  123. return false;
  124. }
  125. for (unsigned long i=0; i < rdf_descriptor->PortCount; i++)
  126. {
  127. if (! is_ladspa_port_good(rdf_descriptor->Ports[i].Type, descriptor->PortDescriptors[i]))
  128. {
  129. qWarning("WARNING - Plugin has RDF data, but invalid PortTypes: %i != %i", rdf_descriptor->Ports[i].Type, descriptor->PortDescriptors[i]);
  130. return false;
  131. }
  132. }
  133. return true;
  134. }
  135. static inline
  136. LADSPA_Data get_default_ladspa_port_value(const LADSPA_PortRangeHintDescriptor hintDescriptor, const LADSPA_Data min, const LADSPA_Data max)
  137. {
  138. LADSPA_Data def;
  139. if (LADSPA_IS_HINT_HAS_DEFAULT(hintDescriptor))
  140. {
  141. switch (hintDescriptor & LADSPA_HINT_DEFAULT_MASK)
  142. {
  143. case LADSPA_HINT_DEFAULT_MINIMUM:
  144. def = min;
  145. break;
  146. case LADSPA_HINT_DEFAULT_MAXIMUM:
  147. def = max;
  148. break;
  149. case LADSPA_HINT_DEFAULT_0:
  150. def = 0.0f;
  151. break;
  152. case LADSPA_HINT_DEFAULT_1:
  153. def = 1.0f;
  154. break;
  155. case LADSPA_HINT_DEFAULT_100:
  156. def = 100.0f;
  157. break;
  158. case LADSPA_HINT_DEFAULT_440:
  159. def = 440.0f;
  160. break;
  161. case LADSPA_HINT_DEFAULT_LOW:
  162. if (LADSPA_IS_HINT_LOGARITHMIC(hintDescriptor))
  163. def = exp((log(min)*0.75f) + (log(max)*0.25f));
  164. else
  165. def = (min*0.75f) + (max*0.25f);
  166. break;
  167. case LADSPA_HINT_DEFAULT_MIDDLE:
  168. if (LADSPA_IS_HINT_LOGARITHMIC(hintDescriptor))
  169. def = sqrt(min*max);
  170. else
  171. def = (min+max)/2;
  172. break;
  173. case LADSPA_HINT_DEFAULT_HIGH:
  174. if (LADSPA_IS_HINT_LOGARITHMIC(hintDescriptor))
  175. def = exp((log(min)*0.25) + (log(max)*0.75));
  176. else
  177. def = (min*0.25) + (max*0.75);
  178. break;
  179. default:
  180. if (min < 0.0 && max > 0.0)
  181. def = 0.0;
  182. else
  183. def = min;
  184. break;
  185. }
  186. }
  187. else
  188. {
  189. // no default value
  190. if (min < 0.0 && max > 0.0)
  191. def = 0.0;
  192. else
  193. def = min;
  194. }
  195. return def;
  196. }
  197. #endif // CARLA_LADSPA_INCLUDES_H