prepare fix for copying plugin state
[ardour.git] / libs / ardour / ardour / utils.h
1 /*
2     Copyright (C) 1999 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifndef __ardour_utils_h__
21 #define __ardour_utils_h__
22
23 #ifdef WAF_BUILD
24 #include "libardour-config.h"
25 #endif
26
27 #include <iostream>
28 #include <string>
29 #include <cmath>
30
31 #if __APPLE__
32 #include <CoreFoundation/CoreFoundation.h>
33 #endif /* __APPLE__ */
34
35 #include "ardour/libardour_visibility.h"
36 #include "ardour/ardour.h"
37 #include "ardour/data_type.h"
38 #include "ardour/dB.h"
39
40 class XMLNode;
41
42 namespace ARDOUR {
43
44 LIBARDOUR_API std::string legalize_for_path (const std::string& str);
45 LIBARDOUR_API std::string legalize_for_universal_path (const std::string& str);
46 LIBARDOUR_API std::string legalize_for_uri (const std::string& str);
47 LIBARDOUR_API std::string legalize_for_path_2X (const std::string& str);
48 LIBARDOUR_API XMLNode* find_named_node (const XMLNode& node, std::string name);
49 LIBARDOUR_API std::string bool_as_string (bool);
50
51 static inline float f_max(float x, float a) {
52         x -= a;
53         x += fabsf (x);
54         x *= 0.5f;
55         x += a;
56
57         return (x);
58 }
59
60 LIBARDOUR_API std::string bump_name_once(const std::string& s, char delimiter);
61 LIBARDOUR_API std::string bump_name_number(const std::string& s);
62
63 LIBARDOUR_API int cmp_nocase (const std::string& s, const std::string& s2);
64 LIBARDOUR_API int cmp_nocase_utf8 (const std::string& s1, const std::string& s2);
65
66 LIBARDOUR_API std::string region_name_from_path (std::string path, bool strip_channels, bool add_channel_suffix = false, uint32_t total = 0, uint32_t this_one = 0);
67 LIBARDOUR_API bool path_is_paired (std::string path, std::string& pair_base);
68
69 LIBARDOUR_API void compute_equal_power_fades (ARDOUR::framecnt_t nframes, float* in, float* out);
70
71 LIBARDOUR_API const char* sync_source_to_string (ARDOUR::SyncSource src, bool sh = false);
72 LIBARDOUR_API ARDOUR::SyncSource string_to_sync_source (std::string str);
73
74 LIBARDOUR_API const char* edit_mode_to_string (ARDOUR::EditMode);
75 LIBARDOUR_API ARDOUR::EditMode string_to_edit_mode (std::string);
76
77 #undef  OLD_GAIN_MATH
78 #define OLD_GAIN_MATH
79
80 static inline double
81 gain_to_slider_position (ARDOUR::gain_t g)
82 {
83         if (g == 0) return 0;
84
85 #ifndef OLD_GAIN_MATH
86         /* Power Law With Exponential Cutoff 2D, fit to data from TC Spectra
87            console (image of fader gradations
88
89            y = C * x(-T) * exp(-x/K)
90
91            C =  8.2857630370864188E-01
92            T = -5.1526743785019269E-01
93            K =  7.8990885960495589E+00
94
95          */
96
97         return 8.2857630370864188E-01 * pow(g,5.1526743785019269E-01) * exp (-g/7.8990885960495589E+00);
98 #else
99         return pow((6.0*log(g)/log(2.0)+192.0)/198.0, 8.0);
100 #endif
101 }
102
103 static inline ARDOUR::gain_t
104 slider_position_to_gain (double pos)
105 {
106         if (pos == 0.0) {
107                 return 0.0;
108         }
109
110 #ifndef OLD_GAIN_MATH
111         /* 5th order polynomial function fit to data from a TC Spectra console
112            fader (image of fader gradations).
113
114            y = a + bx1 + cx2 + dx3 + fx4 + gx5
115
116            a = -1.1945480381045521E-02
117            b =  1.5809476525537265E+00
118            c = -1.5850710838966151E+01
119            d =  6.1643128605961991E+01
120            f = -8.5525246160607693E+01
121            g =  4.1312725896188283E+01
122
123         */
124
125         double p = pos;
126         double g = -1.1945480381045521E-02;
127
128         g +=  1.5809476525537265E+00 * pos;
129         pos *= p;
130         g += -1.5850710838966151E+01 * pos;
131         pos *= p;
132         g += 6.1643128605961991E+01 * pos;
133         pos *= p;
134         g += -8.5525246160607693E+01 * pos;
135         pos *= p;
136         g += 4.1312725896188283E+01 * pos;
137
138         return g;
139 #else
140         /* XXX Marcus writes: this doesn't seem right to me. but i don't have a better answer ... */
141         if (pos == 0.0) return 0;
142         return pow (2.0,(sqrt(sqrt(sqrt(pos)))*198.0-192.0)/6.0);
143 #endif
144 }
145 #undef OLD_GAIN_MATH
146
147 LIBARDOUR_API double gain_to_slider_position_with_max (double g, double max_gain = 2.0);
148 LIBARDOUR_API double slider_position_to_gain_with_max (double g, double max_gain = 2.0);
149
150 /* I don't really like hard-coding these falloff rates here
151  * Probably should use a map of some kind that could be configured
152  * These rates are db/sec.
153 */
154
155 #define METER_FALLOFF_OFF       0.0f
156 #define METER_FALLOFF_SLOWEST   6.6f  // BBC standard
157 #define METER_FALLOFF_SLOW      8.6f  // BBC standard, EBU  24dB / 2.8sec
158 #define METER_FALLOFF_SLOWISH   12.0f // DIN  20dB / 1.7 sec
159 #define METER_FALLOFF_MODERATE  13.3f // EBU-PPM, IRT PPM-   20dB / 1.5 sec
160 #define METER_FALLOFF_MEDIUM    20.0f
161 #define METER_FALLOFF_FAST      32.0f
162
163 LIBARDOUR_API float meter_falloff_to_float (ARDOUR::MeterFalloff);
164 LIBARDOUR_API ARDOUR::MeterFalloff meter_falloff_from_float (float);
165 LIBARDOUR_API float meter_falloff_to_db_per_sec (float);
166
167 LIBARDOUR_API const char* native_header_format_extension (ARDOUR::HeaderFormat, const ARDOUR::DataType& type);
168 LIBARDOUR_API bool matching_unsuffixed_filename_exists_in (const std::string& dir, const std::string& name);
169
170 LIBARDOUR_API uint32_t how_many_dsp_threads ();
171
172 #if __APPLE__
173 LIBARDOUR_API std::string CFStringRefToStdString(CFStringRef stringRef);
174 #endif // __APPLE__
175
176 } //namespave
177
178 #endif /* __ardour_utils_h__ */
179