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.

160 lines
4.3KB

  1. // Copyright 2021 Jean Pierre Cimalando
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // SPDX-License-Identifier: Apache-2.0
  16. //
  17. #include "ysfx_config.hpp"
  18. #include "ysfx_utils.hpp"
  19. #include "ysfx_audio_wav.hpp"
  20. #include "ysfx_audio_flac.hpp"
  21. #include <cassert>
  22. ysfx_config_t *ysfx_config_new()
  23. {
  24. return new ysfx_config_t;
  25. }
  26. void ysfx_config_free(ysfx_config_t *config)
  27. {
  28. if (!config)
  29. return;
  30. if (config->ref_count.fetch_sub(1, std::memory_order_acq_rel) == 1)
  31. delete config;
  32. }
  33. void ysfx_config_add_ref(ysfx_config_t *config)
  34. {
  35. config->ref_count.fetch_add(1, std::memory_order_relaxed);
  36. }
  37. void ysfx_set_import_root(ysfx_config_t *config, const char *root)
  38. {
  39. config->import_root = ysfx::path_ensure_final_separator(root ? root : "");
  40. }
  41. void ysfx_set_data_root(ysfx_config_t *config, const char *root)
  42. {
  43. config->data_root = ysfx::path_ensure_final_separator(root ? root : "");
  44. }
  45. const char *ysfx_get_import_root(ysfx_config_t *config)
  46. {
  47. return config->import_root.c_str();
  48. }
  49. const char *ysfx_get_data_root(ysfx_config_t *config)
  50. {
  51. return config->data_root.c_str();
  52. }
  53. void ysfx_guess_file_roots(ysfx_config_t *config, const char *sourcepath)
  54. {
  55. if (config->import_root.empty()) {
  56. bool stop = false;
  57. const std::string sourcedir = ysfx::path_directory(sourcepath);
  58. std::string cur_dir = sourcedir + "../";
  59. ysfx::file_uid cur_uid;
  60. if (!ysfx::get_file_uid(cur_dir.c_str(), cur_uid))
  61. stop = true;
  62. while (!stop) {
  63. bool match =
  64. ysfx::exists((cur_dir + "Effects/").c_str()) &&
  65. ysfx::exists((cur_dir + "Data/").c_str());
  66. if (match) {
  67. stop = true;
  68. config->import_root = cur_dir + "Effects/";
  69. }
  70. else {
  71. cur_dir += "../";
  72. ysfx::file_uid old_uid = cur_uid;
  73. if (!ysfx::get_file_uid(cur_dir.c_str(), cur_uid) || old_uid == cur_uid)
  74. stop = true;
  75. }
  76. }
  77. }
  78. if (config->data_root.empty() && !config->import_root.empty()) {
  79. const std::string datadir = config->import_root + "../Data/";
  80. bool match = ysfx::exists(datadir.c_str());
  81. if (match)
  82. config->data_root = datadir;
  83. }
  84. }
  85. void ysfx_register_audio_format(ysfx_config_t *config, ysfx_audio_format_t *afmt)
  86. {
  87. config->audio_formats.push_back(*afmt);
  88. }
  89. void ysfx_register_builtin_audio_formats(ysfx_config_t *config)
  90. {
  91. config->audio_formats.push_back(ysfx_audio_format_wav);
  92. config->audio_formats.push_back(ysfx_audio_format_flac);
  93. }
  94. void ysfx_set_log_reporter(ysfx_config_t *config, ysfx_log_reporter_t *reporter)
  95. {
  96. config->log_reporter = reporter;
  97. }
  98. void ysfx_set_user_data(ysfx_config_t *config, intptr_t userdata)
  99. {
  100. config->userdata = userdata;
  101. }
  102. //------------------------------------------------------------------------------
  103. const char *ysfx_log_level_string(ysfx_log_level level)
  104. {
  105. switch (level) {
  106. case ysfx_log_info:
  107. return "info";
  108. case ysfx_log_warning:
  109. return "warning";
  110. case ysfx_log_error:
  111. return "error";
  112. default:
  113. assert(false);
  114. return "?";
  115. }
  116. }
  117. void ysfx_log(ysfx_config_t &conf, ysfx_log_level level, const char *message)
  118. {
  119. if (conf.log_reporter)
  120. conf.log_reporter(conf.userdata, level, message);
  121. else
  122. fprintf(stderr, "[ysfx] %s: %s\n", ysfx_log_level_string(level), message);
  123. }
  124. void ysfx_logfv(ysfx_config_t &conf, ysfx_log_level level, const char *format, va_list ap)
  125. {
  126. char buf[256];
  127. vsnprintf(buf, sizeof(buf), format, ap);
  128. buf[sizeof(buf)-1] = '\0';
  129. ysfx_log(conf, level, buf);
  130. }
  131. void ysfx_logf(ysfx_config_t &conf, ysfx_log_level level, const char *format, ...)
  132. {
  133. va_list ap;
  134. va_start(ap, format);
  135. ysfx_logfv(conf, level, format, ap);
  136. va_end(ap);
  137. }