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.

192 lines
4.9KB

  1. #include <ostream>
  2. #include <rtosc/ports.h>
  3. using namespace rtosc;
  4. /*
  5. * root :
  6. * - 'parameters' : [parameter...]
  7. * - 'actions' : [action...]
  8. * parameter :
  9. * - 'path' : path-id
  10. * - 'name' : string
  11. * - 'shortname' : string [OPTIONAL]
  12. * - 'tooltip' : string [OPTIONAL]
  13. * - 'type' : type
  14. * - 'domain' : range [OPTIONAL]
  15. * type : {'int', 'float', 'boolean'}
  16. * action :
  17. * - 'path' : path-id
  18. * - 'args' : [arg...]
  19. * arg :
  20. * - 'type' : type
  21. * - 'domain' : range [OPTIONAL]
  22. */
  23. void walk_ports2(const rtosc::Ports *base,
  24. char *name_buffer,
  25. size_t buffer_size,
  26. void *data,
  27. rtosc::port_walker_t walker);
  28. using std::ostream;
  29. using std::string;
  30. static int enum_min(Port::MetaContainer meta)
  31. {
  32. int min = 0;
  33. for(auto m:meta)
  34. if(strstr(m.title, "map "))
  35. min = atoi(m.title+4);
  36. for(auto m:meta)
  37. if(strstr(m.title, "map "))
  38. min = min>atoi(m.title+4) ? atoi(m.title+4) : min;
  39. return min;
  40. }
  41. static int enum_max(Port::MetaContainer meta)
  42. {
  43. int max = 0;
  44. for(auto m:meta)
  45. if(strstr(m.title, "map "))
  46. max = atoi(m.title+4);
  47. for(auto m:meta)
  48. if(strstr(m.title, "map "))
  49. max = max<atoi(m.title+4) ? atoi(m.title+4) : max;
  50. return max;
  51. }
  52. static ostream &add_options(ostream &o, Port::MetaContainer meta)
  53. {
  54. string sym_names = "xyzabcdefghijklmnopqrstuvw";
  55. int sym_idx = 0;
  56. bool has_options = false;
  57. for(auto m:meta)
  58. if(strstr(m.title, "map "))
  59. has_options = true;
  60. for(auto m:meta)
  61. if(strcmp(m.title, "documentation") &&
  62. strcmp(m.title, "parameter") &&
  63. strcmp(m.title, "max") &&
  64. strcmp(m.title, "min"))
  65. printf("m.title = <%s>\n", m.title);
  66. if(!has_options)
  67. return o;
  68. o << " <hints>\n";
  69. for(auto m:meta) {
  70. if(strstr(m.title, "map ")) {
  71. o << " <point symbol=\"" << sym_names[sym_idx++] << "\" value=\"";
  72. o << m.title+4 << "\">" << m.value << "</point>\n";
  73. }
  74. }
  75. o << " </hints>\n";
  76. return o;
  77. }
  78. /*
  79. * parameter :
  80. * - 'path' : path-id
  81. * - 'name' : string
  82. * - 'shortname' : string [OPTIONAL]
  83. * - 'tooltip' : string [OPTIONAL]
  84. * - 'type' : type
  85. * - 'domain' : range [OPTIONAL]
  86. */
  87. static bool first = true;
  88. void dump_param_cb(const rtosc::Port *p, const char *name, void *v)
  89. {
  90. std::ostream &o = *(std::ostream*)v;
  91. auto meta = p->meta();
  92. const char *args = strchr(p->name, ':');
  93. auto mparameter = meta.find("parameter");
  94. auto mdoc = meta.find("documentation");
  95. auto msname = meta.find("shortname");
  96. string doc;
  97. //Escape Characters
  98. if(mdoc != p->meta().end()) {
  99. while(*mdoc.value) {
  100. if(*mdoc.value == '\n')
  101. doc += "\\n";
  102. else if(*mdoc.value == '\"')
  103. doc += "\\\"";
  104. else
  105. doc += *mdoc.value;
  106. mdoc.value++;
  107. }
  108. }
  109. if(meta.find("internal") != meta.end())
  110. return;
  111. char type = 0;
  112. if(mparameter != p->meta().end()) {
  113. if(args) {
  114. if(strchr(args, 'f'))
  115. type = 'f';
  116. else if(strchr(args, 'i'))
  117. type = 'i';
  118. else if(strchr(args, 'c'))
  119. type = 'c';
  120. else if(strchr(args, 'T'))
  121. type = 't';
  122. else if(strchr(args, 's'))
  123. type = 's';
  124. }
  125. if(!type) {
  126. fprintf(stderr, "rtosc port dumper: Cannot handle '%s'\n", name);
  127. fprintf(stderr, " args = <%s>\n", args);
  128. return;
  129. }
  130. } else {
  131. //fprintf(stderr, "Skipping \"%s\"\n", name);
  132. //if(args) {
  133. // fprintf(stderr, " type = %s\n", args);
  134. //}
  135. return;
  136. }
  137. const char *min = meta["min"];
  138. const char *max = meta["max"];
  139. if(!first)
  140. o << ",\n";
  141. else
  142. first = false;
  143. o << " {\n";
  144. o << " \"path\" : \"" << name << "\",\n";
  145. if(msname != meta.end())
  146. o << " \"shortname\": \"" << msname.value << "\",\n";
  147. o << " \"name\" : \"" << p->name << "\",\n";
  148. o << " \"tooltip\" : \"" << doc << "\",\n";
  149. o << " \"type\" : \"" << type << "\"";
  150. if(min && max)
  151. o << ",\n \"range\" : [" << min << "," << max << "]\n";
  152. else
  153. o << "\n";
  154. o << " }";
  155. }
  156. void dump_json(std::ostream &o, const rtosc::Ports &p)
  157. {
  158. first = true;
  159. o << "{\n";
  160. o << " \"parameter\" : [\n";
  161. char buffer[1024];
  162. memset(buffer, 0, sizeof(buffer));
  163. walk_ports2(&p, buffer, 1024, &o, dump_param_cb);
  164. o << "\n ],\n";
  165. o << " \"actions\" : [\n";
  166. //walk_ports2(formatter.p, buffer, 1024, &o, dump_action_cb);
  167. o << " ]\n";
  168. o << "}";
  169. }