Add initial bits for Qt6 support
[carla.git] / source / modules / ysfx / sources / ysfx_config.cpp
blob35552de0bc293e4761a97abee727cbd297dfc25e
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.
15 // SPDX-License-Identifier: Apache-2.0
18 #include "ysfx_config.hpp"
19 #include "ysfx_utils.hpp"
20 #include "ysfx_audio_wav.hpp"
21 #include "ysfx_audio_flac.hpp"
22 #include <cassert>
24 ysfx_config_t *ysfx_config_new()
26 return new ysfx_config_t;
29 void ysfx_config_free(ysfx_config_t *config)
31 if (!config)
32 return;
34 if (config->ref_count.fetch_sub(1, std::memory_order_acq_rel) == 1)
35 delete config;
38 void ysfx_config_add_ref(ysfx_config_t *config)
40 config->ref_count.fetch_add(1, std::memory_order_relaxed);
43 void ysfx_set_import_root(ysfx_config_t *config, const char *root)
45 config->import_root = ysfx::path_ensure_final_separator(root ? root : "");
48 void ysfx_set_data_root(ysfx_config_t *config, const char *root)
50 config->data_root = ysfx::path_ensure_final_separator(root ? root : "");
53 const char *ysfx_get_import_root(ysfx_config_t *config)
55 return config->import_root.c_str();
58 const char *ysfx_get_data_root(ysfx_config_t *config)
60 return config->data_root.c_str();
63 void ysfx_guess_file_roots(ysfx_config_t *config, const char *sourcepath)
65 if (config->import_root.empty()) {
66 bool stop = false;
67 const std::string sourcedir = ysfx::path_directory(sourcepath);
69 std::string cur_dir = sourcedir + "../";
70 ysfx::file_uid cur_uid;
71 if (!ysfx::get_file_uid(cur_dir.c_str(), cur_uid))
72 stop = true;
74 while (!stop) {
75 bool match =
76 ysfx::exists((cur_dir + "Effects/").c_str()) &&
77 ysfx::exists((cur_dir + "Data/").c_str());
78 if (match) {
79 stop = true;
80 config->import_root = cur_dir + "Effects/";
82 else {
83 cur_dir += "../";
84 ysfx::file_uid old_uid = cur_uid;
85 if (!ysfx::get_file_uid(cur_dir.c_str(), cur_uid) || old_uid == cur_uid)
86 stop = true;
91 if (config->data_root.empty() && !config->import_root.empty()) {
92 const std::string datadir = config->import_root + "../Data/";
94 bool match = ysfx::exists(datadir.c_str());
95 if (match)
96 config->data_root = datadir;
100 void ysfx_register_audio_format(ysfx_config_t *config, ysfx_audio_format_t *afmt)
102 config->audio_formats.push_back(*afmt);
105 void ysfx_register_builtin_audio_formats(ysfx_config_t *config)
107 config->audio_formats.push_back(ysfx_audio_format_wav);
108 config->audio_formats.push_back(ysfx_audio_format_flac);
111 void ysfx_set_log_reporter(ysfx_config_t *config, ysfx_log_reporter_t *reporter)
113 config->log_reporter = reporter;
116 void ysfx_set_user_data(ysfx_config_t *config, intptr_t userdata)
118 config->userdata = userdata;
121 //------------------------------------------------------------------------------
122 const char *ysfx_log_level_string(ysfx_log_level level)
124 switch (level) {
125 case ysfx_log_info:
126 return "info";
127 case ysfx_log_warning:
128 return "warning";
129 case ysfx_log_error:
130 return "error";
131 default:
132 assert(false);
133 return "?";
137 void ysfx_log(ysfx_config_t &conf, ysfx_log_level level, const char *message)
139 if (conf.log_reporter)
140 conf.log_reporter(conf.userdata, level, message);
141 else
142 fprintf(stderr, "[ysfx] %s: %s\n", ysfx_log_level_string(level), message);
145 void ysfx_logfv(ysfx_config_t &conf, ysfx_log_level level, const char *format, va_list ap)
147 char buf[256];
148 vsnprintf(buf, sizeof(buf), format, ap);
149 buf[sizeof(buf)-1] = '\0';
150 ysfx_log(conf, level, buf);
153 void ysfx_logf(ysfx_config_t &conf, ysfx_log_level level, const char *format, ...)
155 va_list ap;
156 va_start(ap, format);
157 ysfx_logfv(conf, level, format, ap);
158 va_end(ap);