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.

lv2bench.c 7.0KB

9 years ago
9 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. Copyright 2012 David Robillard <http://drobilla.net>
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  13. */
  14. #define _POSIX_C_SOURCE 199309L
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include "lilv/lilv.h"
  19. #include "lv2/lv2plug.in/ns/ext/atom/atom.h"
  20. #include "lilv_config.h"
  21. #include "bench.h"
  22. #include "uri_table.h"
  23. static LilvNode* atom_AtomPort = NULL;
  24. static LilvNode* atom_Sequence = NULL;
  25. static LilvNode* lv2_AudioPort = NULL;
  26. static LilvNode* lv2_CVPort = NULL;
  27. static LilvNode* lv2_ControlPort = NULL;
  28. static LilvNode* lv2_InputPort = NULL;
  29. static LilvNode* lv2_OutputPort = NULL;
  30. static LilvNode* urid_map = NULL;
  31. static bool full_output = false;
  32. static void
  33. print_version(void)
  34. {
  35. printf(
  36. "lv2bench (lilv) " LILV_VERSION "\n"
  37. "Copyright 2012 David Robillard <http://drobilla.net>\n"
  38. "License: <http://www.opensource.org/licenses/isc-license>\n"
  39. "This is free software: you are free to change and redistribute it.\n"
  40. "There is NO WARRANTY, to the extent permitted by law.\n");
  41. }
  42. static void
  43. print_usage(void)
  44. {
  45. printf("lv2bench - Benchmark all installed and supported LV2 plugins.\n");
  46. printf("Usage: lv2bench [OPTIONS]\n");
  47. printf("\n");
  48. printf(" -b BLOCK_SIZE Specify block size, in audio frames.\n");
  49. printf(" -f, --full Full plottable output.\n");
  50. printf(" -h, --help Display this help and exit.\n");
  51. printf(" -n FRAMES Total number of audio frames to process\n");
  52. printf(" --version Display version information and exit\n");
  53. }
  54. static double
  55. bench(const LilvPlugin* p, uint32_t sample_count, uint32_t block_size)
  56. {
  57. URITable uri_table;
  58. uri_table_init(&uri_table);
  59. LV2_URID_Map map = { &uri_table, uri_table_map };
  60. LV2_Feature map_feature = { LV2_URID_MAP_URI, &map };
  61. LV2_URID_Unmap unmap = { &uri_table, uri_table_unmap };
  62. LV2_Feature unmap_feature = { LV2_URID_UNMAP_URI, &unmap };
  63. const LV2_Feature* features[] = { &map_feature, &unmap_feature, NULL };
  64. float* const buf = (float*)calloc(block_size * 2, sizeof(float));
  65. float* const in = buf;
  66. float* const out = buf + block_size;
  67. if (!buf) {
  68. fprintf(stderr, "Out of memory\n");
  69. return 0.0;
  70. }
  71. LV2_Atom_Sequence seq = {
  72. { sizeof(LV2_Atom_Sequence_Body),
  73. uri_table_map(&uri_table, LV2_ATOM__Sequence) },
  74. { 0, 0 } };
  75. const char* uri = lilv_node_as_string(lilv_plugin_get_uri(p));
  76. LilvNodes* required = lilv_plugin_get_required_features(p);
  77. LILV_FOREACH(nodes, i, required) {
  78. const LilvNode* feature = lilv_nodes_get(required, i);
  79. if (!lilv_node_equals(feature, urid_map)) {
  80. fprintf(stderr, "<%s> requires feature <%s>, skipping\n",
  81. uri, lilv_node_as_uri(feature));
  82. free(buf);
  83. uri_table_destroy(&uri_table);
  84. return 0.0;
  85. }
  86. }
  87. LilvInstance* instance = lilv_plugin_instantiate(p, 48000.0, features);
  88. if (!instance) {
  89. fprintf(stderr, "Failed to instantiate <%s>\n",
  90. lilv_node_as_uri(lilv_plugin_get_uri(p)));
  91. free(buf);
  92. uri_table_destroy(&uri_table);
  93. return 0.0;
  94. }
  95. float* controls = (float*)calloc(
  96. lilv_plugin_get_num_ports(p), sizeof(float));
  97. lilv_plugin_get_port_ranges_float(p, NULL, NULL, controls);
  98. const uint32_t n_ports = lilv_plugin_get_num_ports(p);
  99. for (uint32_t index = 0; index < n_ports; ++index) {
  100. const LilvPort* port = lilv_plugin_get_port_by_index(p, index);
  101. if (lilv_port_is_a(p, port, lv2_ControlPort)) {
  102. lilv_instance_connect_port(instance, index, &controls[index]);
  103. } else if (lilv_port_is_a(p, port, lv2_AudioPort) ||
  104. lilv_port_is_a(p, port, lv2_CVPort)) {
  105. if (lilv_port_is_a(p, port, lv2_InputPort)) {
  106. lilv_instance_connect_port(instance, index, in);
  107. } else if (lilv_port_is_a(p, port, lv2_OutputPort)) {
  108. lilv_instance_connect_port(instance, index, out);
  109. } else {
  110. fprintf(stderr, "<%s> port %d neither input nor output, skipping\n",
  111. uri, index);
  112. lilv_instance_free(instance);
  113. free(buf);
  114. free(controls);
  115. uri_table_destroy(&uri_table);
  116. return 0.0;
  117. }
  118. } else if (lilv_port_is_a(p, port, atom_AtomPort)) {
  119. lilv_instance_connect_port(instance, index, &seq);
  120. } else {
  121. fprintf(stderr, "<%s> port %d has unknown type, skipping\n",
  122. uri, index);
  123. lilv_instance_free(instance);
  124. free(buf);
  125. free(controls);
  126. uri_table_destroy(&uri_table);
  127. return 0.0;
  128. }
  129. }
  130. lilv_instance_activate(instance);
  131. struct timespec ts = bench_start();
  132. for (uint32_t i = 0; i < (sample_count / block_size); ++i) {
  133. lilv_instance_run(instance, block_size);
  134. }
  135. const double elapsed = bench_end(&ts);
  136. lilv_instance_deactivate(instance);
  137. lilv_instance_free(instance);
  138. uri_table_destroy(&uri_table);
  139. if (full_output) {
  140. printf("%d %d ", block_size, sample_count);
  141. }
  142. printf("%lf %s\n", elapsed, uri);
  143. free(buf);
  144. free(controls);
  145. return elapsed;
  146. }
  147. int
  148. main(int argc, char** argv)
  149. {
  150. uint32_t block_size = 512;
  151. uint32_t sample_count = (1 << 19);
  152. for (int i = 1; i < argc; ++i) {
  153. if (!strcmp(argv[i], "--version")) {
  154. print_version();
  155. return 0;
  156. } else if (!strcmp(argv[i], "--help")) {
  157. print_usage();
  158. return 0;
  159. } else if (!strcmp(argv[i], "-f")) {
  160. full_output = true;
  161. } else if (!strcmp(argv[i], "-n") && (i + 1 < argc)) {
  162. sample_count = atoi(argv[++i]);
  163. } else if (!strcmp(argv[i], "-b") && (i + 1 < argc)) {
  164. block_size = atoi(argv[++i]);
  165. } else {
  166. print_usage();
  167. return 1;
  168. }
  169. }
  170. LilvWorld* world = lilv_world_new();
  171. lilv_world_load_all(world);
  172. atom_AtomPort = lilv_new_uri(world, LV2_ATOM__AtomPort);
  173. atom_Sequence = lilv_new_uri(world, LV2_ATOM__Sequence);
  174. lv2_AudioPort = lilv_new_uri(world, LV2_CORE__AudioPort);
  175. lv2_CVPort = lilv_new_uri(world, LV2_CORE__CVPort);
  176. lv2_ControlPort = lilv_new_uri(world, LV2_CORE__ControlPort);
  177. lv2_InputPort = lilv_new_uri(world, LV2_CORE__InputPort);
  178. lv2_OutputPort = lilv_new_uri(world, LV2_CORE__OutputPort);
  179. urid_map = lilv_new_uri(world, LV2_URID__map);
  180. if (full_output) {
  181. printf("# Block Samples Time Plugin\n");
  182. }
  183. const LilvPlugins* plugins = lilv_world_get_all_plugins(world);
  184. LILV_FOREACH(plugins, i, plugins) {
  185. bench(lilv_plugins_get(plugins, i), sample_count, block_size);
  186. }
  187. lilv_node_free(urid_map);
  188. lilv_node_free(lv2_OutputPort);
  189. lilv_node_free(lv2_InputPort);
  190. lilv_node_free(lv2_ControlPort);
  191. lilv_node_free(lv2_CVPort);
  192. lilv_node_free(lv2_AudioPort);
  193. lilv_node_free(atom_Sequence);
  194. lilv_node_free(atom_AtomPort);
  195. lilv_world_free(world);
  196. return 0;
  197. }