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 6.9KB

12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. return 0.0;
  84. }
  85. }
  86. LilvInstance* instance = lilv_plugin_instantiate(p, 48000.0, features);
  87. if (!instance) {
  88. fprintf(stderr, "Failed to instantiate <%s>\n",
  89. lilv_node_as_uri(lilv_plugin_get_uri(p)));
  90. free(buf);
  91. return 0.0;
  92. }
  93. float* controls = (float*)calloc(
  94. lilv_plugin_get_num_ports(p), sizeof(float));
  95. lilv_plugin_get_port_ranges_float(p, NULL, NULL, controls);
  96. const uint32_t n_ports = lilv_plugin_get_num_ports(p);
  97. for (uint32_t index = 0; index < n_ports; ++index) {
  98. const LilvPort* port = lilv_plugin_get_port_by_index(p, index);
  99. if (lilv_port_is_a(p, port, lv2_ControlPort)) {
  100. lilv_instance_connect_port(instance, index, &controls[index]);
  101. } else if (lilv_port_is_a(p, port, lv2_AudioPort) ||
  102. lilv_port_is_a(p, port, lv2_CVPort)) {
  103. if (lilv_port_is_a(p, port, lv2_InputPort)) {
  104. lilv_instance_connect_port(instance, index, in);
  105. } else if (lilv_port_is_a(p, port, lv2_OutputPort)) {
  106. lilv_instance_connect_port(instance, index, out);
  107. } else {
  108. fprintf(stderr, "<%s> port %d neither input nor output, skipping\n",
  109. uri, index);
  110. lilv_instance_free(instance);
  111. free(buf);
  112. free(controls);
  113. return 0.0;
  114. }
  115. } else if (lilv_port_is_a(p, port, atom_AtomPort)) {
  116. lilv_instance_connect_port(instance, index, &seq);
  117. } else {
  118. fprintf(stderr, "<%s> port %d has unknown type, skipping\n",
  119. uri, index);
  120. lilv_instance_free(instance);
  121. free(buf);
  122. free(controls);
  123. return 0.0;
  124. }
  125. }
  126. lilv_instance_activate(instance);
  127. struct timespec ts = bench_start();
  128. for (uint32_t i = 0; i < (sample_count / block_size); ++i) {
  129. lilv_instance_run(instance, block_size);
  130. }
  131. const double elapsed = bench_end(&ts);
  132. lilv_instance_deactivate(instance);
  133. lilv_instance_free(instance);
  134. uri_table_destroy(&uri_table);
  135. if (full_output) {
  136. printf("%d %d ", block_size, sample_count);
  137. }
  138. printf("%lf %s\n", elapsed, uri);
  139. free(buf);
  140. free(controls);
  141. return elapsed;
  142. }
  143. int
  144. main(int argc, char** argv)
  145. {
  146. uint32_t block_size = 512;
  147. uint32_t sample_count = (1 << 19);
  148. for (int i = 1; i < argc; ++i) {
  149. if (!strcmp(argv[i], "--version")) {
  150. print_version();
  151. return 0;
  152. } else if (!strcmp(argv[i], "--help")) {
  153. print_usage();
  154. return 0;
  155. } else if (!strcmp(argv[i], "-f")) {
  156. full_output = true;
  157. } else if (!strcmp(argv[i], "-n") && (i + 1 < argc)) {
  158. sample_count = atoi(argv[++i]);
  159. } else if (!strcmp(argv[i], "-b") && (i + 1 < argc)) {
  160. block_size = atoi(argv[++i]);
  161. } else {
  162. print_usage();
  163. return 1;
  164. }
  165. }
  166. LilvWorld* world = lilv_world_new();
  167. lilv_world_load_all(world);
  168. atom_AtomPort = lilv_new_uri(world, LV2_ATOM__AtomPort);
  169. atom_Sequence = lilv_new_uri(world, LV2_ATOM__Sequence);
  170. lv2_AudioPort = lilv_new_uri(world, LV2_CORE__AudioPort);
  171. lv2_CVPort = lilv_new_uri(world, LV2_CORE__CVPort);
  172. lv2_ControlPort = lilv_new_uri(world, LV2_CORE__ControlPort);
  173. lv2_InputPort = lilv_new_uri(world, LV2_CORE__InputPort);
  174. lv2_OutputPort = lilv_new_uri(world, LV2_CORE__OutputPort);
  175. urid_map = lilv_new_uri(world, LV2_URID__map);
  176. if (full_output) {
  177. printf("# Block Samples Time Plugin\n");
  178. }
  179. const LilvPlugins* plugins = lilv_world_get_all_plugins(world);
  180. LILV_FOREACH(plugins, i, plugins) {
  181. bench(lilv_plugins_get(plugins, i), sample_count, block_size);
  182. }
  183. lilv_node_free(urid_map);
  184. lilv_node_free(lv2_OutputPort);
  185. lilv_node_free(lv2_InputPort);
  186. lilv_node_free(lv2_ControlPort);
  187. lilv_node_free(lv2_CVPort);
  188. lilv_node_free(lv2_AudioPort);
  189. lilv_node_free(atom_Sequence);
  190. lilv_node_free(atom_AtomPort);
  191. lilv_world_free(world);
  192. return 0;
  193. }